From: Rainer Stengele <rainer.stengele@online.de>
To: help-gnu-emacs@gnu.org
Subject: Re: how to sort words in a line
Date: Tue, 17 Jul 2007 20:20:52 +0200 [thread overview]
Message-ID: <f7j1a2$jll$1@registered.motzarella.org> (raw)
In-Reply-To: <87lkdekio7.fsf@thalassa.lan.informatimago.com>
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)))
next prev parent reply other threads:[~2007-07-17 18:20 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-17 10:01 how to sort words in a line Rainer Stengele
2007-07-17 10:26 ` Pascal Bourguignon
2007-07-17 13:46 ` Harald Hanche-Olsen
2007-07-17 17:54 ` Rainer Stengele
2007-07-17 20:12 ` Harald Hanche-Olsen
2007-07-17 17:57 ` Rainer Stengele
2007-07-17 18:07 ` Pascal Bourguignon
2007-07-17 18:20 ` Rainer Stengele [this message]
2007-07-17 19:20 ` Thien-Thi Nguyen
2007-07-17 22:43 ` Nikolaj Schumacher
2007-07-18 7:21 ` Mathias Dahl
2007-07-18 19:25 ` Rainer Stengele
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='f7j1a2$jll$1@registered.motzarella.org' \
--to=rainer.stengele@online.de \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).