(A-GT-K02849): New abstract node; unify A-U+8FB0-itaiji-001.
[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-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."
100   (interactive "P")
101   (setq auto-show-mode
102         (if (null arg)
103             (not auto-show-mode)
104           (> (prefix-numeric-value arg) 0))))
105
106 ;; XEmacs addition:
107 (defvar auto-show-inhibiting-commands
108   '(scrollbar-char-left
109     scrollbar-char-right
110     scrollbar-page-left
111     scrollbar-page-right
112     scrollbar-to-left
113     scrollbar-to-right
114     scrollbar-horizontal-drag)
115   "Commands that inhibit auto-show behavior.
116 This normally includes the horizontal scrollbar commands.")
117
118 ;; XEmacs addition:
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))))
123
124 ;; XEmacs addition:
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) 
134                          (if (> scroll 0)
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
142                nil)
143               ((< start-col scroll)
144                (scroll-right (- scroll start-col)))
145               (t
146                (scroll-left (- end-col right-col)))))))
147
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'."
153   (interactive)
154   ;; XEmacs change
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) 
159                          (if (> scroll 0)
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))
164                  (> scroll 0))
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)
170                          (not (eolp))))
171                 (scroll-left (+ auto-show-shift-amount 
172                                 (- col (+ scroll w-width))))))))))
173
174 ;; XEmacs change:
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
177 ;; move point).
178 ;; Do auto-scrolling after commands.
179 ;;(add-hook 'post-command-hook 'auto-show-make-point-visible)
180
181 ;; If being dumped, turn it on right away.
182 (when (boundp 'pureload)
183   (auto-show-mode 1))
184
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)
188
189 (provide 'auto-show)
190
191 ;;; auto-show.el ends here