unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: "Pascal J. Bourguignon" <pjb@informatimago.com>, help-gnu-emacs@gnu.org
Subject: RE: Sorting lines by length
Date: Tue, 16 Sep 2014 19:53:43 -0700 (PDT)	[thread overview]
Message-ID: <790f6664-2267-4bda-b2ed-5620859fd093@default> (raw)
In-Reply-To: <87fvfryovx.fsf@kuiper.lan.informatimago.com>

> 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	other threads:[~2014-09-17  2:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-16 12:56 Sorting lines by length Loris Bennett
2014-09-16 13:48 ` Doug Lewan
2014-09-16 13:56 ` Michael Heerdegen
2014-09-17  2:14 ` Pascal J. Bourguignon
2014-09-17  2:53   ` Drew Adams [this message]

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=790f6664-2267-4bda-b2ed-5620859fd093@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=pjb@informatimago.com \
    /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).