;; Example 13.28 (Figure 13.9)

;; To use, run the following command in emacs:
;;      M-x load-file number_lines.el
;; Now mark a region (e.g. by sweeping it out with the mouse), and then
;; type C-# (control-number_sign).  Optionally, precede that with "C-u 10"
;; (or some other number) to specify the starting line number.

(setq-default line-number-prefix "")
(setq-default line-number-suffix ") ")
(defun number-region (start end &optional initial)
  "Add line numbers to all lines in region.
With optional prefix argument, start numbering at num.
Line number is bracketed by strings line-number-prefix
and line-number-suffix (default \"\" and \") \")."
  (interactive "*r\np")   ; how to parse args when invoked from keyboard
  (let* ((i (or initial 1))
         (num-lines (+ -1 initial (count-lines start end)))
         (fmt (format "%%%dd" (length (number-to-string num-lines))))
         (finish (set-marker (make-marker) end)))
    (save-excursion
      (goto-char start)
      (beginning-of-line)
      (while (< (point) finish)
        (insert line-number-prefix (format fmt i) line-number-suffix)
        (setq i (1+ i))
        (forward-line 1))
      (set-marker finish nil))))

(define-key global-map [?\C-#] 'number-region)
