help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to sort words in a line


From: Rainer Stengele
Subject: Re: how to sort words in a line
Date: Tue, 17 Jul 2007 20:20:52 +0200
User-agent: Thunderbird 2.0.0.4 (Windows/20070604)

Pascal Bourguignon schrieb:
> Rainer Stengele <rainer.stengele@online.de> writes:
> 
>> Pascal Bourguignon schrieb:
>>> Rainer Stengele <rainer.stengele@diplan.de> writes:
>>>> I just couldn't find a fast solution to sort a line of words:
>>>>
>>>> zzz aaa hhhh
>>>>
>>>> -->
>>>>
>>>> aaa hhhh zzz
>>>>
>>>>
>>>> Did I miss a simple command?
>>> AFAIK, no.
>>>
>>> But it's rather simple a command to write:
>>>
>>> (defun sort-words-in-lines (start end)
>>>    (interactive "r")
>>>    (goto-char start)
>>>    (beginning-of-line)
>>>    (while (< (setq start (point)) end)
>>>       (let ((words (sort (split-string (buffer-substring start 
>>> (line-end-position)))
>>>                          (function string-lessp))))
>>>         (delete-region start (line-end-position))
>>>         (dolist (word words ) (insert word " ")))
>>>       (beginning-of-line) (forward-line 1)))
>>>
>>>
>> thanx!
>>
>> Wow! Thats quite a piece of code for a not-yet Lisp programmer.
>> Coming from perl this would be a simple one-liner doing the work.
>> Don't misunderstand me - I understand the power and flexibility of elisp
>> in emacs. I just wonder if there is not a built in solution.
>>
>> Somebody?
> 
> Of course there is a one-liner!
> 
> You select your lines, and type M-x sort-words-in-lines RET
> 
yes - ok.

btw - your code adds whitespaces. This works:

(defun sort-words-in-lines (start end)
  (interactive "r")
  (goto-char start)
  (beginning-of-line)
  (while (< (setq start (point)) end)
    (let ((words (sort (split-string (buffer-substring start
(line-end-position)))
                       (function string-lessp))))
      (delete-region start (line-end-position))
      (dolist (word words ) (insert word " "))
      (delete-trailing-whitespace))
    (beginning-of-line) (forward-line 1)))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]