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.
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.
| Rainer Stengele <rainer....@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
thanx!
calling external processes to sort a line seems to me quite a little
overhead.
rainer
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
Of course there is a one-liner!
You select your lines, and type M-x sort-words-in-lines RET
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)))
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
| 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.
> 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
> 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!