all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* find longest lines during isearch
@ 2008-01-24  0:00 Drew Adams
  2008-01-24  0:06 ` Miles Bader
  2008-01-24  0:16 ` Lennart Borgman (gmail)
  0 siblings, 2 replies; 6+ messages in thread
From: Drew Adams @ 2008-01-24  0:00 UTC (permalink / raw)
  To: Emacs-Devel

I use this several times a day. I bind it to `C-end' in `isearch-mode-map'.
(`C-end' is generally not available from a terminal, however.)

I also bind it to `C-x L' globally, but I nearly always use it with isearch.
In particular, isearch's C-g is handy here. I know that Richard doesn't like
to add more isearch bindings, but that's where I think this belongs.

Any interest in adding it? Please give it a try.

It works with a region or without, and whether the buffer is narrowed or
not. the region is searched if it is active, but you can keep searching
beyond it (you are notified when you exit the region). The target line is
highlighted (until the next command).

You can use this to see what it's like in isearch:

(add-hook
  'isearch-mode-hook
  (lambda ()
    (when window-system
      (define-key isearch-mode-map [(control end)]
                  'goto-longest-line))))

(defun 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)."
  (interactive
   (if (or (not mark-active) (null (mark)))
       (list (point-min) (point-max))
     (if (< (point) (mark))
         (list (point) (mark))
       (list (mark) (point)))))
  (when (and (not mark-active) (= beg end))
    (error "The buffer is empty"))
  (when (and mark-active (> (point) (mark))) (exchange-point-and-mark))
  (when (< end beg) (setq end (prog1 beg (setq beg end))))
  (when (eq this-command last-command)
    (forward-line 1) (setq beg (point)))
  (goto-char beg)
  (when (eobp) (error "End of buffer"))
  (cond ((<= end (save-excursion
                   (goto-char beg) (forward-line 1) (point)))
         (beginning-of-line)
         (when (require 'hl-line nil t)
           (let ((hl-line-mode t)) (hl-line-highlight))
           (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
         (let ((lineno (line-number-at-pos))
               (chars (save-excursion (end-of-line) (current-column))))
           (message "Only line %d: %d chars" lineno chars)
           (let ((visible-bell t)) (ding))
           (setq mark-active nil)
           (list lineno chars nil 1)))
        (t
         (let* ((start-line (line-number-at-pos))
                (max-width 0)
                (line start-line)
                long-lines col)
           (when (eobp) (error "End of buffer"))
           (while (and (not (eobp)) (< (point) end))
             (end-of-line)
             (setq col (current-column))
             (when (>= col max-width)
               (if (= col max-width)
                   (setq long-lines (cons line long-lines))
                 (setq long-lines (list line)))
               (setq max-width col))
             (forward-line 1)
             (setq line (1+ line)))
           (setq long-lines (nreverse long-lines))
           (let ((lines long-lines))
             (while (and lines (> start-line (car lines))) (pop lines))
             (goto-char (point-min))
             (when (car lines) (forward-line (1- (car lines)))))
           (when (require 'hl-line nil t)
             (let ((hl-line-mode t)) (hl-line-highlight))
             (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
           (when (interactive-p)
             (let ((others (cdr long-lines)))
               (message
                "Line %d: %d chars%s (%d lines measured)"
                (car long-lines) max-width
                (concat
                 (and others
                      (format ", Others: {%s}"
                              (mapconcat
                               (lambda (line) (format "%d" line))
                               (cdr long-lines) ", "))))
                (- line start-line))))
           (list (car long-lines) max-width (cdr long-lines)
                 (- line start-line))))))

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: find longest lines during isearch
  2008-01-24  0:00 find longest lines during isearch Drew Adams
@ 2008-01-24  0:06 ` Miles Bader
  2008-01-24  1:46   ` Drew Adams
  2008-01-24  0:16 ` Lennart Borgman (gmail)
  1 sibling, 1 reply; 6+ messages in thread
From: Miles Bader @ 2008-01-24  0:06 UTC (permalink / raw)
  To: Drew Adams; +Cc: Emacs-Devel

"Drew Adams" <drew.adams@oracle.com> writes:
> I use this several times a day. I bind it to `C-end' in `isearch-mode-map'.
> (`C-end' is generally not available from a terminal, however.)

_Why_ do you use it, just out of curiousity?

[I mean, what situation are you in where knowing the long line in a file
is an operation you need to do several times a day...?]

-Miles

-- 
The automobile has not merely taken over the street, it has dissolved the
living tissue of the city.  Its appetite for space is absolutely insatiable;
moving and parked, it devours urban land, leaving the buildings as mere islands
of habitable space in a sea of dangerous and ugly traffic.
[James Marston Fitch, New York Times, 1 May 1960]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: find longest lines during isearch
  2008-01-24  0:00 find longest lines during isearch Drew Adams
  2008-01-24  0:06 ` Miles Bader
@ 2008-01-24  0:16 ` Lennart Borgman (gmail)
  2008-01-24  1:38   ` Drew Adams
  1 sibling, 1 reply; 6+ messages in thread
From: Lennart Borgman (gmail) @ 2008-01-24  0:16 UTC (permalink / raw)
  To: Drew Adams; +Cc: Emacs-Devel

Drew Adams wrote:
> I use this several times a day. I bind it to `C-end' in `isearch-mode-map'.
> (`C-end' is generally not available from a terminal, however.)
> 
> I also bind it to `C-x L' globally, but I nearly always use it with isearch.
> In particular, isearch's C-g is handy here. I know that Richard doesn't like
> to add more isearch bindings, but that's where I think this belongs.


Are you aware of lineker.el which can mark lines that are too long:

   http://www.helsinki.fi/~sjpaavol/programs/lineker.el

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: find longest lines during isearch
  2008-01-24  0:16 ` Lennart Borgman (gmail)
@ 2008-01-24  1:38   ` Drew Adams
  0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2008-01-24  1:38 UTC (permalink / raw)
  To: Lennart Borgman (gmail); +Cc: Emacs-Devel

> > I use this several times a day. I bind it to `C-end' in
> > `isearch-mode-map'.
>
> Are you aware of lineker.el which can mark lines that are too long:
>
>    http://www.helsinki.fi/~sjpaavol/programs/lineker.el

No, but I am aware of other ways to indicate lines that are too long,
including some I wrote (I think one was added to Emacs: highlight column in
mode line, but I don't know).

This is about finding (navigating), not indicating, long lines, and it's not
about lines that are "too" long.

I looked at lineker.el anyway. It lets you highlight and move to "overlong"
lines, which are those longer than a limit that you define (customize).

This is a different approach - no relation. There is no line length limit
here, no correct length, no notion of "too long". If you try it, you will
see what I mean. If you don't find it useful, that's OK by me.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: find longest lines during isearch
  2008-01-24  0:06 ` Miles Bader
@ 2008-01-24  1:46   ` Drew Adams
  2008-01-24  2:17     ` Miles Bader
  0 siblings, 1 reply; 6+ messages in thread
From: Drew Adams @ 2008-01-24  1:46 UTC (permalink / raw)
  To: Miles Bader; +Cc: Emacs-Devel

> _Why_ do you use it, just out of curiousity?
>
> [I mean, what situation are you in where knowing the long line in a file
> is an operation you need to do several times a day...?]

It doesn't matter. If you can't imagine using it, then it's probably not
useful for you. If you don't see the utility, then don't add it.

FWIW, I didn't expect to use it that much when I wrote it, but it turns out
that I do, for various reasons.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: find longest lines during isearch
  2008-01-24  1:46   ` Drew Adams
@ 2008-01-24  2:17     ` Miles Bader
  0 siblings, 0 replies; 6+ messages in thread
From: Miles Bader @ 2008-01-24  2:17 UTC (permalink / raw)
  To: Drew Adams; +Cc: Emacs-Devel

"Drew Adams" <drew.adams@oracle.com> writes:
>> _Why_ do you use it, just out of curiousity?
>>
>> [I mean, what situation are you in where knowing the long line in a file
>> is an operation you need to do several times a day...?]
>
> It doesn't matter. If you can't imagine using it, then it's probably not
> useful for you. If you don't see the utility, then don't add it.

C'mon, maybe some proposed features are so obviously useful that no
explanation is needed, but not every one is like that.  Seeing "use cases"
(which seems to the currently fashionable term :-) can help a lot in the
latter case...

-Miles

-- 
Cat, n. A soft, indestructible automaton provided by nature to be kicked when
things go wrong in the domestic circle.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-01-24  2:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-24  0:00 find longest lines during isearch Drew Adams
2008-01-24  0:06 ` Miles Bader
2008-01-24  1:46   ` Drew Adams
2008-01-24  2:17     ` Miles Bader
2008-01-24  0:16 ` Lennart Borgman (gmail)
2008-01-24  1:38   ` Drew Adams

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.