home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / gnu / emacs / sources / 899 < prev    next >
Encoding:
Text File  |  1992-12-21  |  7.9 KB  |  222 lines

  1. Path: sparky!uunet!olivea!spool.mu.edu!agate!stanford.edu!CSD-NewsHost.Stanford.EDU!times!wmesard
  2. From: wmesard@cs.stanford.edu (Wayne Mesard)
  3. Newsgroups: gnu.emacs.sources
  4. Subject: Re: Horizantal scrol (how to get rid of $ and see what I am typing)
  5. Message-ID: <WMESARD.92Dec20214016@Waimea.Stanford.EDU>
  6. Date: 21 Dec 92 05:40:16 GMT
  7. References: <TEEGE.92Dec15160754@unknown.informatik.tu-muenchen.de>
  8.     <mcook.724548304@fendahl.dev.cdx.mot.com>
  9.     <NICK.92Dec17013931@alphaville.osf.org>
  10.     <1992Dec18.022352.24083@math.ufl.edu>
  11. Sender: news@CSD-NewsHost.Stanford.EDU
  12. Organization: Distributed Systems Group, Stanford University
  13. Lines: 206
  14. In-Reply-To: wang@math.ufl.edu's message of 18 Dec 92 02:23:52 GMT
  15.  
  16. In gnu.emacs.help wang@math.ufl.edu writes:
  17. > In article <NICK.92Dec17013931@alphaville.osf.org> nick@osf.org (Nick Dokos) writes:
  18. >>Since Wayne did not mention his hscroll.el package, let me put in a plug
  19. >>for it. You can put a buffer in hscroll-mode and it will automatically scroll
  20. >>left or right following the cursor.
  21. > Could  you post it here so that anyone who is interested in can have a
  22. > chance to try it?
  23.  
  24. No.  Well...okay.  Here it is.
  25.  
  26. Wayne();
  27.  
  28. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  29. ;;; hscroll.el: Minor mode to automatically scroll truncated lines horizontally
  30. ;;; Copyright (C) 1992 Wayne Mesard
  31. ;;;
  32. ;;; This program is free software; you can redistribute it and/or modify
  33. ;;; it under the terms of the GNU General Public License as published by
  34. ;;; the Free Software Foundation; either version 1, or (at your option)
  35. ;;; any later version.
  36. ;;;
  37. ;;; This program is distributed in the hope that it will be useful,
  38. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  40. ;;; GNU General Public License for more details.
  41. ;;;
  42. ;;; The GNU General Public License is available by anonymouse ftp from
  43. ;;; prep.ai.mit.edu in pub/gnu/COPYING.  Alternately, you can write to
  44. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  45. ;;; USA.
  46. ;;--------------------------------------------------------------------
  47.  
  48. ;;; DESCRIPTION
  49. ;;    Automatically scroll horizontally when the point moves off the
  50. ;;    left or right edge of the window.  Type "M-x hscroll-mode" to
  51. ;;    invoke it in the current buffer.  This only has effect when
  52. ;;    the current line is truncated by Emacs.  Say "Control-h f 
  53. ;;    hscroll-truncate-lines" for details.
  54. ;;
  55. ;;    HScroll's sensitivity is controlled by the variable hscroll-margin.
  56. ;;    How much HScroll adjusts the window is determined by hscroll-step.
  57. ;;
  58. ;;    Most users won't have to mess with the other variables and functions 
  59. ;;    defined here.  But they're all documented, and they all start with 
  60. ;;    "hscroll-" if you're curious.
  61. ;;
  62. ;;    Oh, you should also know that if you set the hscroll-margin and
  63. ;;    hscroll-step large enough, you can get an interesting, but
  64. ;;    undesired ping-pong effect as the point bounces from one edge to
  65. ;;    the other.
  66. ;;
  67. ;;    WMesard@cs.stanford.edu
  68.  
  69. ;;; HISTORY
  70. ;;    1.1 wmesard - Aug 18, 1992: Fixed setq-default bug
  71. ;;    1.0 wmesard - Aug 11, 1992: Created
  72.  
  73. ;;; 
  74. ;;; PUBLIC VARIABLES
  75. ;;; 
  76.  
  77. (defvar hscroll-margin 5 
  78.   "*How many columns away from the edge of the window point is allowed to get
  79. before HScroll will horizontally scroll the window.")
  80.  
  81. (defvar hscroll-step 25
  82.   "*How far away to place the point from the window's edge when scrolling.
  83. Expressed as a percentage of the window's width.")
  84.  
  85. (defvar hscroll-poll-period "1"
  86.   "*Interval between polling for HScroll mode (in seconds).
  87. This is how often HScroll will test to see if the point has exceeded
  88. a horizontal margin.  If nil, it will test continuously (but this is
  89. not recommended, since it will slow down your machine and annoy Emacs).")
  90.  
  91. (defvar hscroll-mode nil 
  92.   "Whether hscroll-mode is enabled for the current buffer.
  93. Ordinarily set indirectly (via \\[hscroll-mode]).  However,
  94.    (setq-default hscroll-mode t)
  95. will put buffers in HScroll mode by default.  Automatically becomes local
  96. when set.")
  97.  
  98.  
  99. ;;; 
  100. ;;; PRIVATE VARIABLES
  101. ;;; 
  102.  
  103. (defvar hscroll-process nil)
  104.  
  105. ;;; 
  106. ;;; PUBLIC FUNCTIONS
  107. ;;; 
  108.  
  109. (defun hscroll-mode (&optional onoff)
  110.   "Toggle HScroll mode in the current buffer.
  111. With arg, turn HScroll mode on if arg is positive, off otherwise.
  112. In HScroll mode, truncated lines will automatically scroll left or right
  113. when point gets near either edge of the window."
  114.   (interactive "P")
  115.   (if (null hscroll-process)
  116.       (progn
  117.     (make-variable-buffer-local 'hscroll-mode)
  118.     ;; So that the last line in this func will do the right thing
  119.     ;; when default value is t and this is the first buffer.
  120.     (setq hscroll-mode nil)
  121.     (or (assq 'hscroll-mode minor-mode-alist)
  122.         (setq minor-mode-alist
  123.           (cons '(hscroll-mode " HScr")
  124.             minor-mode-alist)))
  125.     (setq hscroll-process (start-process "hscroll" nil
  126.                          (concat exec-directory "wakeup")
  127.                          (or hscroll-poll-period
  128.                          "0")))
  129.     (set-process-sentinel hscroll-process 'hscroll-sentinel)
  130.     (set-process-filter hscroll-process 'hscroll-filter)
  131.     (process-kill-without-query hscroll-process)
  132.     ))
  133.   (setq hscroll-mode (if onoff
  134.              (> (if (numberp onoff) onoff
  135.                   (prefix-numeric-value onoff))
  136.                 0)
  137.                (not hscroll-mode))
  138.     ))
  139.  
  140.  
  141. (defun hscroll-shutdown ()
  142.   "Disable HScroll mode in all buffers, and terminate the HScroll subprocess.
  143. This command is an \"emergency switch\" for use if the subprocess starts
  144. hogging up too many system resources."
  145.   (interactive)
  146.   (or (assq 'hscroll-mode minor-mode-alist)
  147.       (setq minor-mode-alist
  148.         (cons '(hscroll-mode "")
  149.           minor-mode-alist)))
  150.   (if (eq 'run (process-status hscroll-process))
  151.       (kill-process hscroll-process))
  152.   (setq hscroll-process nil)
  153.   )
  154.  
  155.  
  156. (defun hscroll-truncate-lines (&optional onoff)
  157.   "Toggle the value of the Emacs variable truncate-lines in the current buffer.  
  158. With arg, set to t if arg is positive, nil otherwise.  This is just a
  159. convenience function and not really part of HScroll.  Without it, you'd
  160. have to use set-variable to change the value of truncate-lines.
  161.  
  162. Say \\[describe-variable] truncate-lines and \\[describe-variable] \
  163. truncate-partial-width-windows for details."
  164.   (interactive "P")
  165.   (setq truncate-lines (if onoff
  166.                (> (if (numberp onoff) onoff 
  167.                 (prefix-numeric-value onoff))
  168.                   0)
  169.              (not truncate-lines))
  170.     ))
  171.  
  172.  
  173. (defun hscroll-window-maybe ()
  174.   "Scroll horizontally if point is off or nearly off the edge of the window.
  175. This is called automatically when in HScroll mode, but it can be explicitly
  176. invoked as well."
  177.   (interactive)
  178.   ;; Only consider scrolling if truncate-lines is true, 
  179.   ;; the window is already scrolled or partial-widths is true and this is
  180.   ;; a partial width window.  See display_text_line() in xdisp.c.
  181.   (if (or truncate-lines
  182.       (not (zerop (window-hscroll)))
  183.       (and truncate-partial-width-windows
  184.            (< (window-width) (screen-width))))
  185.       (let ((linelen (save-excursion (end-of-line) (current-column)))
  186.         (rightmost-char (+ (window-width) (window-hscroll)))
  187.         )
  188.     (if (>= (current-column)
  189.         (- rightmost-char hscroll-margin
  190.            ;; Off-by-one if the left edge is scrolled
  191.            (if (not (zerop (window-hscroll))) 1 0)
  192.            ;; Off by one if the right edge is scrolled
  193.            (if (> linelen rightmost-char) 1 0)))
  194.         ;; Scroll to the left a proportion of the window's width.
  195.         (set-window-hscroll 
  196.          (selected-window) 
  197.          (- (+ (current-column) 
  198.            (/ (* (window-width) hscroll-step) 100))
  199.         (window-width)))
  200.       (if (< (current-column) (+ (window-hscroll) hscroll-margin))
  201.           ;; Scroll to the right a proportion of the window's width.
  202.           (set-window-hscroll
  203.            (selected-window)
  204.            (- (current-column) (/ (* (window-width) hscroll-step) 100)))
  205.         ))
  206.     )))
  207.  
  208.  
  209. ;;; 
  210. ;;; PRIVATE FUNCTIONS
  211. ;;; 
  212.  
  213. (defun hscroll-filter (ignore ignore)
  214.   ;; Don't even bother if we're not in the mode.
  215.   (if hscroll-mode
  216.       (hscroll-window-maybe)))
  217.  
  218.  
  219. (defun hscroll-sentinel (ignore reason)
  220.   (hscroll-shutdown)
  221.   (error "Whoa: the HScroll process died unexpectedly: %s." reason))
  222.