home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / emacs / sources / 917 < prev    next >
Encoding:
Text File  |  1993-01-06  |  7.9 KB  |  228 lines

  1. Path: sparky!uunet!pipex!bnr.co.uk!uknet!pavo.csi.cam.ac.uk!camcus!nj104
  2. From: nj104@cus.cam.ac.uk (Neil Jerram)
  3. Newsgroups: gnu.emacs.sources
  4. Subject: Improved version of linenumbers.el
  5. Message-ID: <NJ104.93Jan6143715@bootes.cus.cam.ac.uk>
  6. Date: 6 Jan 93 14:37:18 GMT
  7. Sender: news@infodev.cam.ac.uk (USENET news)
  8. Distribution: gnu
  9. Organization: U of Cambridge, England
  10. Lines: 215
  11. Nntp-Posting-Host: bootes.cus.cam.ac.uk
  12.  
  13. Apologies for the linenumbers.el which I posted just before Christmas
  14. and which contained some laughably fraudulent claims in its source
  15. code (viz. the bit about (setq-default linenumber-mode t)...).
  16.  
  17. Here is a much improved version which does what it claims to do.  The
  18. format of line number display is now customisable and *NB* most
  19. variables and internal functions have been renamed to begin with "ln-"
  20. rather than "linenumber-".
  21.  
  22. I'm afraid that `enjoy' is a transitive verb this side of the pond :-)
  23.  
  24. Neil Jerram.
  25.  
  26. ;--------------------cut here------------------------------
  27. ; linenumbers.el
  28. ;
  29. ; A mix'n'match of linenumbers.el by Ajay Shekhawat
  30. ;              and hscroll.el by Wayne Mesard
  31. ;
  32. ; OR...
  33. ;
  34. ; ``If it's acceptable to have 1 second polling for horizontal scroll
  35. ;   checking, then why not do line numbers the easy way ?''
  36. ;
  37. ; Mished and mashed by Neil Jerram <nj104@cus.cam.ac.uk>,
  38. ; Monday 21 December 1992.
  39. ;
  40. ; Wednesday 6 January 1993:
  41. ; * Modified considerably so that the claim about
  42. ;      (setq-default ln-mode t)
  43. ;   is true.
  44. ; * Added a new public variable ln-format to allow customisation of
  45. ;   the line number display.
  46. ; * Renamed most variables and internal functions so they begin
  47. ;   with "ln-" rather than "linenumber-".
  48. ; * Improved mode-line modification so as to interfere minimally with
  49. ;   the representation before linenumbers are switched on.
  50. ;   (Try linenumbers in *info* for an example of this.)
  51. ; * Improved linenumbers-shutdown so that it switches off linenumbering
  52. ;   in all exisiting buffers.  Otherwise you are left with a lingering
  53. ;   but meaningless number in your mode-line.
  54. ; * Generally tidied up, split isolated bits of functionality
  55. ;   (like starting the external process) into their own functions, etc.
  56.  
  57. ;;; This program is free software; you can redistribute it and/or modify
  58. ;;; it under the terms of the GNU General Public License as published by
  59. ;;; the Free Software Foundation; either version 1, or (at your option)
  60. ;;; any later version.
  61. ;;;
  62. ;;; This program is distributed in the hope that it will be useful,
  63. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  64. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  65. ;;; GNU General Public License for more details.
  66. ;;;
  67. ;;; The GNU General Public License is available by anonymouse ftp from
  68. ;;; prep.ai.mit.edu in pub/gnu/COPYING.  Alternately, you can write to
  69. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  70. ;;; USA.
  71.  
  72. (provide 'linenumbers)
  73.  
  74. ; ----------------
  75. ; Public variables
  76. ; ----------------
  77.  
  78. (defvar ln-poll-period "1"
  79.   "*Interval between checking the current line number (in seconds).
  80. If nil, it will test continuously (but this is not recommended, 
  81. since it will slow down your machine and annoy Emacs).")
  82.  
  83. (defvar ln-mode nil 
  84.   "*Whether ln-mode is enabled for the current buffer.
  85. Ordinarily set indirectly (via \\[linenumbers]).  However,
  86.    (setq-default ln-mode t)
  87. will put buffers in linenumbers mode by default.
  88. Automatically becomes local when set.")
  89.  
  90. (defvar ln-format "l.%d"
  91.   "*A format string describing the presentation of the line number.
  92. The \"%d\" should be included somewhere in this string: it stands for 
  93. the line number itself.")
  94.  
  95. ; As an example of alternative formats, you might write
  96. ;  (setq ln-format "-= line %d =-")
  97. ; in your `.emacs' file after the instruction to load in the lisp code.
  98. ; The code as it stands does not allow the format to be different in
  99. ; different buffers, but you can achieve this if you want it by adding
  100. ;  (make-variable-buffer-local 'ln-format)
  101. ; in your `.emacs' file.
  102. ;   Hard cases might want to use %o or %x instead of %d.
  103. ;   Changes to ln-format will not carry into other buffers until they
  104. ; become active (even 'though they may be visible in another window).
  105. ; This could be remedied, by making ln-filter cycle through all existing
  106. ; buffers every time it gets a signal from "Wakeup!", but I don't
  107. ; really think it's worth it.
  108.  
  109. ; ----------------
  110. ; Public functions
  111. ; ----------------
  112.  
  113. (defun linenumbers (&optional onoff)
  114.   "Toggle linenumbers mode in the current buffer.
  115. With arg, turn linenumbers mode on if arg is positive, off otherwise."
  116.   (interactive "P")
  117.   (setq ln-mode (if onoff
  118.             (> (prefix-numeric-value onoff) 0)
  119.           (not ln-mode)))
  120.   (or ln-mode
  121.       (not ln-usual-mode-line)
  122.       (setq mode-line-buffer-identification ln-usual-mode-line
  123.         ln-usual-mode-line nil))
  124.   (and ln-mode
  125.        (null ln-process)
  126.        (ln-start-process)))
  127.  
  128. (defun linenumbers-shutdown ()
  129.   "Disable linenumbers mode in all buffers, and terminate the linenumbers subprocess.
  130. This command is an \"emergency switch\" for use if the subprocess starts
  131. hogging up too many system resources."
  132.   (interactive)
  133.   (if (processp ln-process)
  134.       (progn
  135.     (set-process-filter ln-process nil)
  136.     (set-process-sentinel ln-process nil)
  137.     (if (eq 'run (process-status ln-process)) 
  138.         (kill-process ln-process))))
  139.   (setq ln-process nil)
  140.   (let* ((buffers (buffer-list))
  141.      (i (length buffers)))
  142.     (while (>= (setq i (1- i)) 0)
  143.       (set-buffer (nth i buffers))
  144.       (linenumbers -1))))
  145.  
  146. ; ---------
  147. ; Internals
  148. ; ---------
  149.  
  150. (defvar ln-usual-mode-line nil
  151.   "A buffer-specific variable to store the usual mode line style
  152. for each buffer.  When linenumbering is switched off the mode line
  153. should revert to its default style.")
  154.  
  155. (defvar ln-string nil
  156.   "String printed in the mode line describing the current line number.")
  157.  
  158. (defvar ln-process nil
  159.   "Variable holding the linenumbers process object.")
  160.  
  161. (defvar ln-initialised nil
  162.   "A variable which is t once local variables have been set up.")
  163.  
  164. (defun ln-initialise ()
  165.   "Sets up local variables and their initial values."
  166.   (make-variable-buffer-local 'ln-mode)
  167.   (make-variable-buffer-local 'ln-usual-mode-line)
  168.   (make-variable-buffer-local 'ln-string)
  169.   (setq-default ln-usual-mode-line nil)
  170.   (setq ln-initialised t))
  171.  
  172. (defun ln-start-process ()
  173.   "Starts the \"wakeup\" process for linenumbers mode."
  174.   (setq ln-process (start-process "linenumbers" nil
  175.                   (concat exec-directory "wakeup")
  176.                   (or ln-poll-period "0")))
  177.   (set-process-sentinel ln-process 'ln-sentinel)
  178.   (set-process-filter ln-process 'ln-filter)
  179.   (process-kill-without-query ln-process))
  180.  
  181. (defun ln-modified-buffer-id ()
  182.   "Returns the modified form of mode-line-buffer-identification, q.v."
  183.   (cond
  184.    ((listp mode-line-buffer-identification)
  185.     (append mode-line-buffer-identification '(ln-string)))
  186.    ((stringp mode-line-buffer-identification)
  187.     (if (string-match " $" mode-line-buffer-identification)
  188.     (append (list mode-line-buffer-identification) '(ln-string))
  189.       (append (list mode-line-buffer-identification " ") '(ln-string))))
  190.    (t
  191.     (append (list mode-line-buffer-identification) '(ln-string)))))
  192.  
  193. (defun which-line ()
  194.   "RETURN the current line number (in the buffer) of point."
  195.   (save-restriction
  196.     (widen)
  197.     (save-excursion
  198.       (beginning-of-line)
  199.       (1+ (count-lines 1 (point))))))
  200.  
  201. (defun ln-filter (ignore ignore)
  202.   ;; Don't even bother if we're not in the mode.
  203.   (if ln-mode
  204.       (progn
  205.     (or ln-initialised (ln-initialise))
  206.     (setq ln-string (format ln-format (which-line)))
  207.     (or ln-usual-mode-line
  208.         (setq ln-usual-mode-line
  209.           mode-line-buffer-identification
  210.           mode-line-buffer-identification
  211.           (ln-modified-buffer-id))))))
  212.  
  213. (defun ln-sentinel (ignore reason)
  214.   (linenumbers-shutdown)
  215.   (error "Whoa: the linenumbers process died unexpectedly: %s." reason))
  216.  
  217. ; --------------------------
  218. ; Start the external process
  219. ; --------------------------
  220.  
  221. (or ln-process
  222.     (ln-start-process))
  223.  
  224. ; ---
  225. ; End
  226. ; ---
  227. ;--------------------cut here------------------------------
  228.