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

[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."
  (interactive)
  (save-excursion
    (save-restriction
      (widen)
      (let ((fmt (format "%%0%dd "
                         (1+ (truncate 
                              (log (count-lines (point-min) (point-max)) 
                                   10)))))
            (i 0))
        (goto-char (point-min))
        (while (< (point) (point-max))
          (setq i (1+ i))
          (insert (format fmt i))
          (forward-line)))))
  );;insert-line-numbers


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."
  (interactive)
  (newline)
  (let ((nlpt (point))
        (line (progn
                (forward-line -1)
                (beginning-of-line)
                (if (looking-at "[0-9]+")
                    (let ((curr (string-to-number (match-string 0))))
                      (forward-line -1)
                      (beginning-of-line)
                      (if (looking-at "[0-9]+")
                          (let ((prev (string-to-number (match-string 0))))
                            (+ curr (abs (- curr prev))))
                        (+ 10 curr)))
                  10))))
    (goto-char nlpt)
    (beginning-of-line)
    (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)

and:
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 ()
  (interactive)
  (save-excursion
    (save-restriction
      (widen)
        (goto-char (point-min))
        (while (< (point) (point-max))
          (if (looking-at "[0-9][0-9]* ")
            (delete-region (match-beginning 0) (match-end 0)))
          (forward-line))))
  );;delete-line-numbers


(defun renumber-lines ()
    (interactive)
    (delete-line-numbers)
    (insert-line-numbers))



-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.


reply via email to

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