[Top][All Lists]

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

Re: prefix line with number WAS: Are Windows-Emacs' scrollbars broken?

From: Pascal Bourguignon
Subject: Re: prefix line with number WAS: Are Windows-Emacs' scrollbars broken?
Date: 18 Apr 2003 02:16:24 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

John Kliff Jochens <address@hidden> writes:

> On Sat, 2003-04-12 at 07:07, Eli Zaretskii wrote:
> > 
> > > It doesn't scroll all the way to the bottom, stopping a couple of
> > > lines before the end of the buffer.
> > > And the scrolling text jitters up & down while scrolling.
> > 
> > This is because Emacs sets the scroll-bar thumb size based on the
> > number of characters, not lines.  One reason is that Emacs doesn't
> > count lines in the buffer unless specifically required to do so by
> > some command.
> Now that you mentioned it, how can I have each line of the buffer
> prefixed with its number? All I managed to do was to show on the line
> with the file name (and etc), as well as the column, but I'd preffer to
> have the line shown at the begginig of line.
> Anyone knows how to do that (GNU Emacs 21.2.1)?

Perhaps  faster than  searching  anything  on the  web  and trying  to
install it inside emacs would be to write this in one's .emacs:

(defun insert-line-numbers ()
    "Insert a line number on all lines of the current buffer."
      (let ((fmt (format "%%0%dd "
                         (1+ (truncate 
                              (log (count-lines (point-min) (point-max)) 
            (i 0))
        (goto-char (point-min))
        (while (< (point) (point-max))
          (setq i (1+ i))
          (insert (format fmt i))

Then you may want to have line numbers inserted on new lines:

(defun lse-newline ()
  "Insert newline and line number incremented with the same step 
   as previously."
  (let ((nlpt (point))
        (line (progn
                (forward-line -1)
                (if (looking-at "[0-9]+")
                    (let ((curr (string-to-number (match-string 0))))
                      (forward-line -1)
                      (if (looking-at "[0-9]+")
                          (let ((prev (string-to-number (match-string 0))))
                            (+ curr (abs (- curr prev))))
                        (+ 10 curr)))
    (goto-char nlpt)
    (insert (format "%d " line))
    (when (looking-at " +")
      (delete-region (match-beginning 0) (match-end 0)))))

And activating with:

(setq line-num-map (make-sparse-keymap))
(define-key line-num-map "\n"  'lse-newline)
(define-key line-num-map "\r"  'lse-newline)

M-x eval-expression RETURN (use-local-map line-num-map) RETURN 
in the buffer.
(or you can add these keys to the map of the mode of these buffers).

But then you have to renumber lines sometimes:

(defun delete-line-numbers ()
        (goto-char (point-min))
        (while (< (point) (point-max))
          (if (looking-at "[0-9][0-9]* ")
            (delete-region (match-beginning 0) (match-end 0)))

(defun renumber-lines ()

Do not adjust your mind, there is a fault in reality.

reply via email to

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