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

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