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

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

Re: Possible to get line numbers?


From: Pascal Bourguignon
Subject: Re: Possible to get line numbers?
Date: 30 Mar 2005 00:25:55 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

JS <d44sf@44ada.com> writes:
> Is it possible to get line numbers in the left margin of emacs??

ISTR that a mode passed on gnu.emacs.sources to do that.  Otherwise,
there's the line-number-mode and column-number-mode that display the
line and column number in the mode line.  The point is that it's
rather costly to maintain line numbers while editing a buffer: you
have to renumber all lines every time something's cut, pasted or C-o
or RETURN is typed, etc.


You could use this M-x insert-line-numbers command to insert line
numbers, M-x delete-line-numbers to remove them, M-x renumber-lines to
update them.

And M-x local-set-key RET RET lse-newline RET to get new line numbers
inserted automatically when you type return (actually, not _line_
numbers, but a statement number like in LSE or BASIC where you may
number lines 5 b 5 or 10 by 10.


I don't know if there's a way to maintain an area on the screen
distinct from the buffer contents, but scroll-synchronized  with the
buffer, like the "fringe", were we could put the line numbers; it
doesn't seems so...



(defun insert-line-numbers ()
  (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))))))


(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)))))

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


(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)))));;lse-newline


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.


reply via email to

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