1 ;;; auto-show.el --- perform automatic horizontal scrolling as point moves
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; This file is in the public domain.
6 ;; Author: Pete Ware <ware@cis.ohio-state.edu>
7 ;; Maintainer: XEmacs Development Team
8 ;; Keywords: extensions, internal, dumped
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 ;;; Synched up with: Emacs/Mule zeta.
31 ;; This file is dumped with XEmacs.
33 ;; Modified by: Ben Wing <ben@xemacs.org>
35 ;; This file provides functions that
36 ;; automatically scroll the window horizontally when the point moves
37 ;; off the left or right side of the window.
39 ;; Once this library is loaded, automatic horizontal scrolling
40 ;; occurs whenever long lines are being truncated.
41 ;; To request truncation of long lines, set the variable
42 ;; Setting the variable `truncate-lines' to non-nil.
43 ;; You can do this for all buffers as follows:
45 ;; (set-default 'truncate-lines t)
47 ;; Here is how to do it for C mode only:
49 ;; (set-default 'truncate-lines nil) ; this is the original value
50 ;; (defun my-c-mode-hook ()
51 ;; "Run when C-mode starts up. Changes ..."
52 ;; ... set various personal preferences ...
53 ;; (setq truncate-lines t))
54 ;; (add-hook 'c-mode-hook 'my-c-mode-hook)
57 ;; As a finer level of control, you can still have truncated lines but
58 ;; without the automatic horizontal scrolling by setting the buffer
59 ;; local variable `auto-show-mode' to nil. The default value is t.
60 ;; The command `auto-show-mode' toggles the value of the variable
65 (defgroup auto-show nil
66 "Perform automatic horizontal scrolling as point moves."
70 ;; This is preloaded, so we don't need special :set, :require, etc.
71 (defcustom auto-show-mode t
72 "*Non-nil enables automatic horizontal scrolling, when lines are truncated.
73 The default value is t. To change the default, do this:
74 (set-default 'auto-show-mode nil)
75 See also command `auto-show-mode'.
76 This variable has no effect when lines are not being truncated.
77 This variable is automatically local in each buffer where it is set."
81 (make-variable-buffer-local 'auto-show-mode)
83 (defcustom auto-show-shift-amount 8
84 "*Extra columns to scroll. for automatic horizontal scrolling."
88 (defcustom auto-show-show-left-margin-threshold 50
89 "*Threshold column for automatic horizontal scrolling to the right.
90 If point is before this column, we try to scroll to make the left margin
91 visible. Setting this to 0 disables this feature."
95 (defun auto-show-mode (arg)
96 "Turn automatic horizontal scroll mode on or off.
97 With arg, turn auto scrolling on if arg is positive, off otherwise.
98 This mode is enabled or disabled for each buffer individually.
99 It takes effect only when `truncate-lines' is non-nil."
104 (> (prefix-numeric-value arg) 0))))
107 (defvar auto-show-inhibiting-commands
108 '(scrollbar-char-left
114 scrollbar-horizontal-drag)
115 "Commands that inhibit auto-show behavior.
116 This normally includes the horizontal scrollbar commands.")
119 (defun auto-show-should-take-action-p ()
120 (and auto-show-mode (window-truncated-p)
121 (equal (window-buffer) (current-buffer))
122 (not (memq this-command auto-show-inhibiting-commands))))
125 (defun auto-show-make-region-visible (start end)
126 "Move point in such a way that the region (START, END) is visible.
127 This only does anything if auto-show-mode is enabled, and it doesn't
128 actually do any horizontal scrolling; rather, it just sets things up so
129 that the region will be visible when `auto-show-make-point-visible'
130 is next called (this happens after every command)."
131 (if (auto-show-should-take-action-p)
132 (let* ((scroll (window-hscroll)) ;how far window is scrolled
133 (w-width (- (window-width)
135 2 1))) ;how wide window is on the screen
136 (right-col (+ scroll w-width))
137 (start-col (save-excursion (goto-char start) (current-column)))
138 (end-col (save-excursion (goto-char end) (current-column))))
139 (cond ((and (>= start-col scroll)
140 (<= end-col right-col))
141 ;; already completely visible
143 ((< start-col scroll)
144 (scroll-right (- scroll start-col)))
146 (scroll-left (- end-col right-col)))))))
148 (defun auto-show-make-point-visible (&optional ignore-arg)
149 "Scroll horizontally to make point visible, if that is enabled.
150 This function only does something if `auto-show-mode' is non-nil
151 and longlines are being truncated in the selected window.
152 See also the command `auto-show-mode'."
155 (if (auto-show-should-take-action-p)
156 (let* ((col (current-column)) ;column on line point is at
157 (scroll (window-hscroll)) ;how far window is scrolled
158 (w-width (- (window-width)
160 2 1))) ;how wide window is on the screen
161 (right-col (+ scroll w-width)))
162 (if (and (< col auto-show-show-left-margin-threshold)
163 (< col (window-width))
165 (scroll-right scroll)
166 (if (< col scroll) ;to the left of the screen
167 (scroll-right (+ (- scroll col) auto-show-shift-amount))
168 (if (or (> col right-col) ;to the right of the screen
169 (and (= col right-col)
171 (scroll-left (+ auto-show-shift-amount
172 (- col (+ scroll w-width))))))))))
175 ;; #### instead of this, we kludgily call it from the C code, to make sure
176 ;; that it's done after any other things on post-command-hook (which might
178 ;; Do auto-scrolling after commands.
179 ;;(add-hook 'post-command-hook 'auto-show-make-point-visible)
181 ;; If being dumped, turn it on right away.
182 (when (boundp 'pureload)
185 ;; Do auto-scrolling in comint buffers after process output also.
186 ; XEmacs -- don't do this now, it messes up comint.
187 ;(add-hook 'comint-output-filter-functions 'auto-show-make-point-visible t)
191 ;;; auto-show.el ends here