* how to sort words in a line
@ 2007-07-17 10:01 Rainer Stengele
2007-07-17 10:26 ` Pascal Bourguignon
0 siblings, 1 reply; 12+ messages in thread
From: Rainer Stengele @ 2007-07-17 10:01 UTC (permalink / raw)
To: help-gnu-emacs
hi,
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?
thanx.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
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:57 ` Rainer Stengele
0 siblings, 2 replies; 12+ messages in thread
From: Pascal Bourguignon @ 2007-07-17 10:26 UTC (permalink / raw)
To: help-gnu-emacs
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)))
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
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 17:57 ` Rainer Stengele
1 sibling, 1 reply; 12+ messages in thread
From: Harald Hanche-Olsen @ 2007-07-17 13:46 UTC (permalink / raw)
To: help-gnu-emacs
+ Pascal Bourguignon <pjb@informatimago.com>:
| 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:
You could also pipe the line through the command
tr ' ' \n | sort | tr \n ' '
or variations thereof, depending on your shell's ability to interpret
escape sequences like \n.
--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
when there is no ground whatsoever for supposing it is true.
-- Bertrand Russell
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 13:46 ` Harald Hanche-Olsen
@ 2007-07-17 17:54 ` Rainer Stengele
2007-07-17 20:12 ` Harald Hanche-Olsen
0 siblings, 1 reply; 12+ messages in thread
From: Rainer Stengele @ 2007-07-17 17:54 UTC (permalink / raw)
To: help-gnu-emacs
Harald Hanche-Olsen schrieb:
> + Pascal Bourguignon <pjb@informatimago.com>:
>
> | 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:
>
> You could also pipe the line through the command
>
> tr ' ' \n | sort | tr \n ' '
>
> or variations thereof, depending on your shell's ability to interpret
> escape sequences like \n.
>
thanx!
calling external processes to sort a line seems to me quite a little
overhead.
rainer
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 17:54 ` Rainer Stengele
@ 2007-07-17 20:12 ` Harald Hanche-Olsen
0 siblings, 0 replies; 12+ messages in thread
From: Harald Hanche-Olsen @ 2007-07-17 20:12 UTC (permalink / raw)
To: help-gnu-emacs
+ Rainer Stengele <rainer.stengele@online.de>:
| Harald Hanche-Olsen schrieb:
|> You could also pipe the line through the command
|>
|> tr ' ' \n | sort | tr \n ' '
|
| calling external processes to sort a line seems to me quite a little
| overhead.
It all depends. For example, it depends on how often you need to do
this thing. If you do it only very rarely, I would assume that the
savings of your time (seconds? minutes?) is more valuable than the
wasted milliseconds of CPU time. But, your milage may vary, as they
say. Perhaps your time is worthless, and CPU cycles are really
valuable. Lots of people behave like this is so. I fall into the
trap myself from time to time.
--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
when there is no ground whatsoever for supposing it is true.
-- Bertrand Russell
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 10:26 ` Pascal Bourguignon
2007-07-17 13:46 ` Harald Hanche-Olsen
@ 2007-07-17 17:57 ` Rainer Stengele
2007-07-17 18:07 ` Pascal Bourguignon
` (3 more replies)
1 sibling, 4 replies; 12+ messages in thread
From: Rainer Stengele @ 2007-07-17 17:57 UTC (permalink / raw)
To: help-gnu-emacs
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?
rainer
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 17:57 ` Rainer Stengele
@ 2007-07-17 18:07 ` Pascal Bourguignon
2007-07-17 18:20 ` Rainer Stengele
2007-07-17 19:20 ` Thien-Thi Nguyen
` (2 subsequent siblings)
3 siblings, 1 reply; 12+ messages in thread
From: Pascal Bourguignon @ 2007-07-17 18:07 UTC (permalink / raw)
To: help-gnu-emacs
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
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 18:07 ` Pascal Bourguignon
@ 2007-07-17 18:20 ` Rainer Stengele
0 siblings, 0 replies; 12+ messages in thread
From: Rainer Stengele @ 2007-07-17 18:20 UTC (permalink / raw)
To: help-gnu-emacs
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)))
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 17:57 ` Rainer Stengele
2007-07-17 18:07 ` Pascal Bourguignon
@ 2007-07-17 19:20 ` Thien-Thi Nguyen
2007-07-17 22:43 ` Nikolaj Schumacher
2007-07-18 7:21 ` Mathias Dahl
3 siblings, 0 replies; 12+ messages in thread
From: Thien-Thi Nguyen @ 2007-07-17 19:20 UTC (permalink / raw)
To: help-gnu-emacs
() Rainer Stengele <rainer.stengele@online.de>
() Tue, 17 Jul 2007 19:57:17 +0200
simple one-liner doing the work.
perhaps one expression, nicely (readably) laid out, would please you:
(defun sort-words-on-line ()
(interactive)
(insert (mapconcat 'identity
(sort (split-string
(delete-and-extract-region
(point) (1+ (line-end-position))))
'string<)
" ")
"\n"))
for this command to work, you have to be at bol and the line must end
w/ newline. in exchange for these restrictions, it has the feature of
moving you to the next line automatically (handy for kbd macro use).
thi
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 17:57 ` Rainer Stengele
2007-07-17 18:07 ` Pascal Bourguignon
2007-07-17 19:20 ` Thien-Thi Nguyen
@ 2007-07-17 22:43 ` Nikolaj Schumacher
2007-07-18 7:21 ` Mathias Dahl
3 siblings, 0 replies; 12+ messages in thread
From: Nikolaj Schumacher @ 2007-07-17 22:43 UTC (permalink / raw)
To: help-gnu-emacs
Rainer Stengele <rainer.stengele@online.de> wrote:
> 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.
Note that the actual work is in fact one line:
(sort (split-string ...) (function string-lessp))
The rest is just input and output.
regards,
Nikolaj Schumacher
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-17 17:57 ` Rainer Stengele
` (2 preceding siblings ...)
2007-07-17 22:43 ` Nikolaj Schumacher
@ 2007-07-18 7:21 ` Mathias Dahl
2007-07-18 19:25 ` Rainer Stengele
3 siblings, 1 reply; 12+ messages in thread
From: Mathias Dahl @ 2007-07-18 7:21 UTC (permalink / raw)
To: help-gnu-emacs
Rainer Stengele <rainer.stengele@online.de> writes:
> 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.
Well, what is a one-liner? I mean, behind each one-liner in perl lies
probably a lot of code that implements that feature in perl. The
difference here is that the particular feature you wanted wasn't
implemented in Emacs. Yet.
> 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.
Some day maybe someone will propose this command to be added to Emacs
and then it will be "built-in"... :)
And, if you paste Pascal's example into your .emacs file it will, for
all practical purposes, built-in.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: how to sort words in a line
2007-07-18 7:21 ` Mathias Dahl
@ 2007-07-18 19:25 ` Rainer Stengele
0 siblings, 0 replies; 12+ messages in thread
From: Rainer Stengele @ 2007-07-18 19:25 UTC (permalink / raw)
To: help-gnu-emacs
Mathias Dahl schrieb:
> Rainer Stengele <rainer.stengele@online.de> writes:
>
>> 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.
>
> Well, what is a one-liner? I mean, behind each one-liner in perl lies
> probably a lot of code that implements that feature in perl. The
> difference here is that the particular feature you wanted wasn't
> implemented in Emacs. Yet.
>
>> 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.
>
> Some day maybe someone will propose this command to be added to Emacs
> and then it will be "built-in"... :)
>
> And, if you paste Pascal's example into your .emacs file it will, for
> all practical purposes, built-in.
agreed. for any more than trivial function this is the way to go.
for "simple" functions I'd like to have them available "in the wild",
finding an emacs and not having my .emacs with me ...
comes down to decide whats a simlpe enough function to have it built in.
BTW Thank everybody who is maintaining a wonderful EMACS!
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2007-07-18 19:25 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
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).