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

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

RE: Sorting lines by length


From: Drew Adams
Subject: RE: Sorting lines by length
Date: Tue, 16 Sep 2014 19:53:43 -0700 (PDT)

> You want the longest line.  This is quite different than wanting to
> sort lines.  Finding the longest line is a O(n) operation.
> Sorting lines is a O(n*log(n)) operation.
> 
> (defun goto-longest-line ()
>   (interactive)
>   (let ((max-line-start 0)
>         (max-line-length 0))
>     (goto-char (point-min))
>     (while (< (point) (point-max))
>       (let ((start (point)))
>         (forward-line)
>         (let ((length (- (point) start)))
>           (if (< max-line-length length)
>              (setf max-line-length length
>                    max-line-start  start)))))
>     (goto-char max-line-start)
>     (set-mark (+ max-line-start max-line-length))))

FWIW -

The version of `goto-longest-line' (coincidentally the same name) in
`misc-cmds.el' is similar but does a bit more.  Here is the doc string:

,----
| goto-longest-line is an interactive Lisp function in `misc-cmds.el'.
| (goto-longest-line BEG END)
| 
| Go to the first of the longest lines in the region or buffer.
| If the region is active, it is checked.
| If not, the buffer (or its restriction) is checked.
| 
| Returns a list of three elements:
| 
|  (LINE LINE-LENGTH OTHER-LINES LINES-CHECKED)
| 
| LINE is the first of the longest lines measured.
| LINE-LENGTH is the length of LINE.
| OTHER-LINES is a list of other lines checked that are as long as LINE.
| LINES-CHECKED is the number of lines measured.
| 
| Interactively, a message displays this information.
| 
| If there is only one line in the active region, then the region is
| deactivated after this command, and the message mentions only LINE and
| LINE-LENGTH.
| 
| If this command is repeated, it checks for the longest line after the
| cursor.  That is *not* necessarily the longest line other than the
| current line.  That longest line could be before or after the current
| line.
| 
| To search only from the current line forward, not throughout the
| buffer, you can use `C-SPC' to set the mark, then use this
| (repeatedly).
`----

And if you use `isearch+.el' then `C-end' is (by default) bound to
this command during Isearch. `C-g' puts you back where you left off
searching, `C-s' resumes searching from wherever you stop hitting
`C-end', etc.  I use this quite often during Isearch.

http://www.emacswiki.org/emacs-en/download/misc-cmds.el
http://www.emacswiki.org/emacs-en/download/isearch%2b.el

http://www.emacswiki.org/IsearchPlus



reply via email to

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