home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / gnu / emacs / help / 5144 < prev    next >
Encoding:
Text File  |  1992-12-21  |  5.2 KB  |  156 lines

  1. Xref: sparky gnu.emacs.help:5144 gnu.emacs.sources:902
  2. Path: sparky!uunet!pipex!bnr.co.uk!uknet!pavo.csi.cam.ac.uk!camcus!nj104
  3. From: nj104@cus.cam.ac.uk (Neil Jerram)
  4. Newsgroups: gnu.emacs.help,gnu.emacs.sources
  5. Subject: Tracking Line Numbers in Emacs
  6. Message-ID: <NJ104.92Dec21171157@bootes.cus.cam.ac.uk>
  7. Date: 21 Dec 92 17:12:01 GMT
  8. Sender: news@infodev.cam.ac.uk (USENET news)
  9. Distribution: gnu
  10. Organization: U of Cambridge, England
  11. Lines: 142
  12. Nntp-Posting-Host: bootes.cus.cam.ac.uk
  13.  
  14. There have recently been postings in gnu.emacs.help about
  15.  
  16. (i) Tracking the line number in Emacs' buffers, and
  17. (ii) Automatic horizontal scrolling of Emacs windows.
  18.  
  19. The posted solution to (i) involved redefining every Emacs command
  20. which could move the cursor up or down, while the solution to (ii)
  21. used an external process to nudge Emacs every now and then and cause
  22. it to check the horizontal position of the cursor.
  23.  
  24. Clearly it would be possible to apply the solution method of (ii) to
  25. the problem of (i) (and vice versa, if anyone wanted to...).  Here a
  26. piece of Emacs Lisp which does just that.  Its disadvantage is that
  27. there is a delay (half a second on average) before the correct line
  28. number is shown. Its advantages are (a) that the solution is far less
  29. cumbersome than the redefining-functions approach; (b) the previous
  30. solution will fail if any new Lisp function which moves the cursor
  31. vertically is loaded and executed, whereas the new solution will still
  32. work correctly.
  33.  
  34. Neil Jerram.
  35.  
  36. ;---------------cut here-----------------------------------
  37. ; linenumbers.el
  38. ;
  39. ; A mix'n'match of linenumbers.el by Ajay Shekhawat
  40. ;              and hscroll.el by Wayne Mesard
  41. ;
  42. ; OR...
  43. ;
  44. ; ``If it's acceptable to have 1 second polling for horizontal scroll
  45. ;   checking, then why not do line numbers the easy way ?''
  46. ;
  47. ; Mished and mashed by Neil Jerram <nj104@cus.cam.ac.uk>,
  48. ; Monday 21 December 1992.
  49. ;
  50. ;;; This program is free software; you can redistribute it and/or modify
  51. ;;; it under the terms of the GNU General Public License as published by
  52. ;;; the Free Software Foundation; either version 1, or (at your option)
  53. ;;; any later version.
  54. ;;;
  55. ;;; This program is distributed in the hope that it will be useful,
  56. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  57. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  58. ;;; GNU General Public License for more details.
  59. ;;;
  60. ;;; The GNU General Public License is available by anonymouse ftp from
  61. ;;; prep.ai.mit.edu in pub/gnu/COPYING.  Alternately, you can write to
  62. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  63. ;;; USA.
  64.  
  65. (provide 'linenumbers)
  66.  
  67. ; ----------------
  68. ; Public variables
  69. ; ----------------
  70.  
  71. (defvar linenumber-poll-period "1"
  72.   "*Interval between checking the current line number (in seconds).
  73. If nil, it will test continuously (but this is not recommended, 
  74. since it will slow down your machine and annoy Emacs).")
  75.  
  76. (defvar linenumber-mode nil 
  77.   "Whether linenumber-mode is enabled for the current buffer.
  78. Ordinarily set indirectly (via \\[linenumber-mode]).  However,
  79.    (setq-default linenumber-mode t)
  80. will put buffers in Linenumber mode by default.
  81. Automatically becomes local when set.")
  82.  
  83. ; ----------------
  84. ; Public functions
  85. ; ----------------
  86.  
  87. (defun linenumber-mode (&optional onoff)
  88.   "Toggle Line# mode in the current buffer.
  89. With arg, turn Line# mode on if arg is positive, off otherwise."
  90.   (interactive "P")
  91.   (if (null linenumber-process)
  92.       (progn
  93.     (make-variable-buffer-local 'linenumber-mode)
  94.     ;; So that the last line in this func will do the right thing
  95.     ;; when default value is t and this is the first buffer.
  96.     (setq linenumber-mode nil)
  97.     (setq linenumber-process 
  98.           (start-process "linenumber" nil
  99.                  (concat exec-directory "wakeup")
  100.                  (or linenumber-poll-period
  101.                  "0")))
  102.     (set-process-sentinel linenumber-process 'linenumber-sentinel)
  103.     (set-process-filter linenumber-process 'linenumber-filter)
  104.     (process-kill-without-query linenumber-process)))
  105.   (setq linenumber-mode (if onoff
  106.                 (> (if (numberp onoff) onoff
  107.                  (prefix-numeric-value onoff))
  108.                    0)
  109.               (not linenumber-mode)))
  110.   (setq mode-line-buffer-identification
  111.     (if linenumber-mode
  112.         '("Emacs: %1b" " l." line-number)
  113.       '("Emacs: %1b"))))
  114.  
  115.  
  116. (defun linenumber-shutdown ()
  117.   "Disable Line# mode in all buffers, and terminate the Line# subprocess.
  118. This command is an \"emergency switch\" for use if the subprocess starts
  119. hogging up too many system resources."
  120.   (interactive)
  121.   (if (eq 'run (process-status linenumber-process))
  122.       (kill-process linenumber-process))
  123.   (setq linenumber-process nil))
  124.  
  125. ; ---------
  126. ; Internals
  127. ; ---------
  128.  
  129. (make-variable-buffer-local 'line-number)
  130.  
  131. (defvar linenumber-process nil)
  132.  
  133. (defun which-line ()
  134.   "RETURN the current line number (in the buffer) of point."
  135.   (save-restriction
  136.     (widen)
  137.     (save-excursion
  138.       (beginning-of-line)
  139.       (concat (1+ (count-lines 1 (point)))))))
  140.  
  141. (defun linenumber-filter (ignore ignore)
  142.   ;; Don't even bother if we're not in the mode.
  143.   (if linenumber-mode
  144.       (setq line-number (which-line))))
  145.  
  146.  
  147. (defun linenumber-sentinel (ignore reason)
  148.   (linenumber-shutdown)
  149.   (error "Whoa: the Line# process died unexpectedly: %s." reason))
  150.  
  151. ; ---
  152. ; End
  153. ; ---
  154.  
  155. ;---------------cut here-----------------------------------
  156.