XEmacs 21.2.14.
[chise/xemacs-chise.git.1] / lisp / auto-show.el
1 ;;; auto-show.el --- perform automatic horizontal scrolling as point moves
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; This file is in the public domain.
5
6 ;; Author: Pete Ware <ware@cis.ohio-state.edu>
7 ;; Maintainer: XEmacs Development Team
8 ;; Keywords: extensions, internal, dumped
9
10 ;; This file is part of XEmacs.
11
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)
15 ;; any later version.
16
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.
21
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
25 ;; 02111-1307, USA.
26
27 ;;; Synched up with: Emacs/Mule zeta.
28
29 ;;; Commentary:
30
31 ;; This file is dumped with XEmacs.
32
33 ;; Modified by: Ben Wing <ben@xemacs.org>
34
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.
38
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:
44 ;;
45 ;; (set-default 'truncate-lines t)
46
47 ;; Here is how to do it for C mode only:
48 ;;
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)
55 ;;
56 ;;
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
61 ;; `auto-show-mode'.
62
63 ;;; Code:
64
65 (defgroup auto-show nil
66   "Perform automatic horizontal scrolling as point moves."
67   :group 'display
68   :group 'extensions)
69
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."
78   :type 'boolean
79   :group 'auto-show)
80
81 (make-variable-buffer-local 'auto-show-mode)
82
83 (defcustom auto-show-shift-amount 8 
84   "*Extra columns to scroll. for automatic horizontal scrolling."
85   :type 'integer
86   :group 'auto-show)
87
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."
92   :type 'number
93   :group 'auto-show)
94
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.
99   (or truncate-lines 
100       (and truncate-partial-width-windows
101            (< (+ (window-width)
102                  (specifier-instance left-margin-width)
103                  (specifier-instance right-margin-width))
104               (frame-width)))))
105
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."
111   (interactive "P")
112   (setq auto-show-mode
113         (if (null arg)
114             (not auto-show-mode)
115           (> (prefix-numeric-value arg) 0))))
116
117 ;; XEmacs addition:
118 (defvar auto-show-inhibiting-commands
119   '(scrollbar-char-left
120     scrollbar-char-right
121     scrollbar-page-left
122     scrollbar-page-right
123     scrollbar-to-left
124     scrollbar-to-right
125     scrollbar-horizontal-drag)
126   "Commands that inhibit auto-show behavior.
127 This normally includes the horizontal scrollbar commands.")
128
129 ;; XEmacs addition:
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))))
134
135 ;; XEmacs addition:
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) 
145                          (if (> scroll 0)
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
153                nil)
154               ((< start-col scroll)
155                (scroll-right (- scroll start-col)))
156               (t
157                (scroll-left (- end-col right-col)))))))
158
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'."
164   (interactive)
165   ;; XEmacs change
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) 
170                          (if (> scroll 0)
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))
175                  (> scroll 0))
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)
181                          (not (eolp))))
182                 (scroll-left (+ auto-show-shift-amount 
183                                 (- col (+ scroll w-width))))))))))
184
185 ;; XEmacs change:
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
188 ;; move point).
189 ;; Do auto-scrolling after commands.
190 ;;(add-hook 'post-command-hook 'auto-show-make-point-visible)
191
192 ;; If being dumped, turn it on right away.
193 (when (boundp 'pureload)
194   (auto-show-mode 1))
195
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)
199
200 (provide 'auto-show)
201
202 ;;; auto-show.el ends here