[net.sources] Line-numbering in emacs

ardis@wivax.UUCP (Mark A. Ardis) (04/04/84)

Here is an emacs package for adding and deleting line numbers to
text within emacs.  Note that the line numbers are added at the
beginning of each line, not overlaid.  The definition of a line
number is via a too-simplistic pattern, so lines with colons may be
damaged when line numbers are removed.
----------
; number.ml -- functions to insert and delete line numbers
;
;	Mark Ardis
;	Wang Institute of Graduate Studies
;	4/8/83
;
;	Revision Log:
;
;	9/26/83 -- added nmbr-beginning-of-line, leading-zero format
;	4/4/84	-- cleaned up for publication on usenet
;
; Functions:
;
;	number-lines:	Inserts line numbers at the beginning of each line
;
;	unnumber-lines:	Removes line numbers
;
;	nmbr-beginning-of-line:	Skips over the line number to get to the
;				"real" beginning-of-line
;
; Globals:
;
;	number-pattern:	Simplistic pattern for finding existing line numbers

;**********************************************************************
;	GLOBAL DECLARATIONS

(declare-global number-pattern)
(setq-default   number-pattern "^.*: ")

;**********************************************************************
;	FUNCTIONS

(defun
	(number-lines	; number lines sequentially
; VAR
	start		; starting number, may have leading zeroes
	incr		; increment, assumed positive
	line		; current line number
	num-lnth	; length of line numbers (for fixed-length)
; BEGIN
	(save-window-excursion
			; Arguments are: start, incr
			; Prompts for values if	called interactively
			; Defaults are "000001" and 1.
			; Note that a leading zero in the "start" value
			;	causes fixed-length line numbers to be used.
			; "Fixed-length" is compromised by large line numbers.
			; Negative values will work for both arguments,
			;	but not if leading zeroes are used.
		(setq start (arg 1 "Starting number? [000001] "))
		(if (= start "") (setq start         "000001"))
		(if (= (string-to-char start) '0')
			(setq num-lnth (length start))
			(setq num-lnth 0))
		(setq incr (arg 2 "Increment? [1] "))
		(if (= incr "") (setq incr 1))
			; Remove old line numbers if present
		(beginning-of-file)
		(if (looking-at "^[0-9]*: ")
			(unnumber-lines))
			; For each line, insert a line number
		(setq line start)
		(while (! (eobp))
			(beginning-of-line)
			(insert-string line)
			(insert-string ": ")
			(setq line (+ line incr))
				; Add leading zeros if needed
			(while (< (length line) num-lnth)
				(setq line (concat "0" line))
			) ;while
			(next-line)
		) ;while
	) ;save
; RETURN
	(novalue)
; END
	) ;number-lines
) ;defun

;----------------------------------------------------------------------

(defun
	(unnumber-lines		; remove the line numbers
; BEGIN
	(save-window-excursion
		(beginning-of-file)
				; Note the simplisitic definition of
				;	a line number
		(re-replace-string number-pattern "")
	) ;save
; RETURN
	(novalue)
; END
	) ;unnumber-lines
) ;defun

;----------------------------------------------------------------------

(defun
	(nmbr-beginning-of-line		; skip over line numbers if present
; BEGIN
	(beginning-of-line)
				; Note the simplistic definition of
				;	a line number
	(if (looking-at number-pattern) (search-forward ": "))
; RETURN
	(novalue)
; END
	) ;nmbr-beginning-of-line
) ;defun

;**********************************************************************
;	END number.ml
-- 
Mark A. Ardis
Wang Insitute of Graduate Studies
Tyng Road, Tyngsboro, MA 01879

ardis.wang-inst@csnet-relay
...!decvax!wivax!ardis