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-truncationp ()
96 "True if line truncation is enabled for the selected window."
97 ;; XEmacs change (use specifiers)
98 ;; ### There should be a more straightforward way to do this from elisp.
100 (and truncate-partial-width-windows
102 (specifier-instance left-margin-width)
103 (specifier-instance right-margin-width))
106 (defun auto-show-mode (arg)
107 "Turn automatic horizontal scroll mode on or off.
108 With arg, turn auto scrolling on if arg is positive, off otherwise.
109 This mode is enabled or disabled for each buffer individually.
110 It takes effect only when `truncate-lines' is non-nil."
115 (> (prefix-numeric-value arg) 0))))
118 (defvar auto-show-inhibiting-commands
119 '(scrollbar-char-left
125 scrollbar-horizontal-drag)
126 "Commands that inhibit auto-show behavior.
127 This normally includes the horizontal scrollbar commands.")
130 (defun auto-show-should-take-action-p ()
131 (and auto-show-mode (auto-show-truncationp)
132 (equal (window-buffer) (current-buffer))
133 (not (memq this-command auto-show-inhibiting-commands))))
136 (defun auto-show-make-region-visible (start end)
137 "Move point in such a way that the region (START, END) is visible.
138 This only does anything if auto-show-mode is enabled, and it doesn't
139 actually do any horizontal scrolling; rather, it just sets things up so
140 that the region will be visible when `auto-show-make-point-visible'
141 is next called (this happens after every command)."
142 (if (auto-show-should-take-action-p)
143 (let* ((scroll (window-hscroll)) ;how far window is scrolled
144 (w-width (- (window-width)
146 2 1))) ;how wide window is on the screen
147 (right-col (+ scroll w-width))
148 (start-col (save-excursion (goto-char start) (current-column)))
149 (end-col (save-excursion (goto-char end) (current-column))))
150 (cond ((and (>= start-col scroll)
151 (<= end-col right-col))
152 ;; already completely visible
154 ((< start-col scroll)
155 (scroll-right (- scroll start-col)))
157 (scroll-left (- end-col right-col)))))))
159 (defun auto-show-make-point-visible (&optional ignore-arg)
160 "Scroll horizontally to make point visible, if that is enabled.
161 This function only does something if `auto-show-mode' is non-nil
162 and longlines are being truncated in the selected window.
163 See also the command `auto-show-mode'."
166 (if (auto-show-should-take-action-p)
167 (let* ((col (current-column)) ;column on line point is at
168 (scroll (window-hscroll)) ;how far window is scrolled
169 (w-width (- (window-width)
171 2 1))) ;how wide window is on the screen
172 (right-col (+ scroll w-width)))
173 (if (and (< col auto-show-show-left-margin-threshold)
174 (< col (window-width))
176 (scroll-right scroll)
177 (if (< col scroll) ;to the left of the screen
178 (scroll-right (+ (- scroll col) auto-show-shift-amount))
179 (if (or (> col right-col) ;to the right of the screen
180 (and (= col right-col)
182 (scroll-left (+ auto-show-shift-amount
183 (- col (+ scroll w-width))))))))))
186 ;; #### instead of this, we kludgily call it from the C code, to make sure
187 ;; that it's done after any other things on post-command-hook (which might
189 ;; Do auto-scrolling after commands.
190 ;;(add-hook 'post-command-hook 'auto-show-make-point-visible)
192 ;; If being dumped, turn it on right away.
193 (when (boundp 'pureload)
196 ;; Do auto-scrolling in comint buffers after process output also.
197 ; XEmacs -- don't do this now, it messes up comint.
198 ;(add-hook 'comint-output-filter-functions 'auto-show-make-point-visible t)
202 ;;; auto-show.el ends here