comparison lisp/modes/auto-show.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; auto-show.el --- perform automatic horizontal scrolling as point moves
2 ;;; This file is in the public domain.
3
4 ;;; Keywords: scroll display minor-mode
5 ;;; Author: Pete Ware <ware@cis.ohio-state.edu>
6 ;;; Modified by: Ben Wing <wing@666.com>
7 ;;; Maintainer: FSF
8
9 ;;; Synched up with: FSF 19.30.
10
11 ;;; Commentary:
12
13 ;;; This file provides functions that
14 ;;; automatically scroll the window horizontally when the point moves
15 ;;; off the left or right side of the window.
16
17 ;;; Once this library is loaded, automatic horizontal scrolling
18 ;;; occurs whenever long lines are being truncated.
19 ;;; To request truncation of long lines, set the variable
20 ;;; Setting the variable `truncate-lines' to non-nil.
21 ;;; You can do this for all buffers as follows:
22 ;;;
23 ;;; (set-default 'truncate-lines t)
24
25 ;;; Here is how to do it for C mode only:
26 ;;;
27 ;;; (set-default 'truncate-lines nil) ; this is the original value
28 ;;; (defun my-c-mode-hook ()
29 ;;; "Run when C-mode starts up. Changes ..."
30 ;;; ... set various personal preferences ...
31 ;;; (setq truncate-lines t))
32 ;;; (add-hook 'c-mode-hook 'my-c-mode-hook)
33 ;;;
34 ;;;
35 ;;; As a finer level of control, you can still have truncated lines but
36 ;;; without the automatic horizontal scrolling by setting the buffer
37 ;;; local variable `auto-show-mode' to nil. The default value is t.
38 ;;; The command `auto-show-mode' toggles the value of the variable
39 ;;; `auto-show-mode'.
40
41 ;;; Code:
42
43 (defvar auto-show-mode t
44 "*Non-nil enables automatic horizontal scrolling, when lines are truncated.
45 The default value is t. To change the default, do this:
46 (set-default 'auto-show-mode nil)
47 See also command `auto-show-mode'.
48 This variable has no effect when lines are not being truncated.")
49
50 (make-variable-buffer-local 'auto-show-mode)
51
52 (defvar auto-show-shift-amount 8
53 "*Extra columns to scroll. for automatic horizontal scrolling.")
54
55 (defvar auto-show-show-left-margin-threshold 50
56 "*Threshold column for automatic horizontal scrolling to the right.
57 If point is before this column, we try to scroll to make the left margin
58 visible. Setting this to 0 disables this feature.")
59
60 (defun auto-show-truncationp ()
61 "True if line truncation is enabled for the selected window."
62 ;; ### There should be a more straightforward way to do this from elisp.
63 (or truncate-lines
64 (and truncate-partial-width-windows
65 (< (+ (window-width)
66 (specifier-instance left-margin-width)
67 (specifier-instance right-margin-width))
68 (frame-width)))))
69
70 (defun auto-show-mode (arg)
71 "Turn automatic horizontal scroll mode on or off.
72 With arg, turn auto scrolling on if arg is positive, off otherwise."
73 (interactive "P")
74 (setq auto-show-mode
75 (if (null arg)
76 (not auto-show-mode)
77 (> (prefix-numeric-value arg) 0))))
78
79 (defvar auto-show-inhibiting-commands
80 '(scrollbar-char-left
81 scrollbar-char-right
82 scrollbar-page-left
83 scrollbar-page-right
84 scrollbar-to-left
85 scrollbar-to-right
86 scrollbar-horizontal-drag)
87 "Commands that inhibit auto-show behavior.
88 This normally includes the horizontal scrollbar commands.")
89
90 (defun auto-show-should-take-action-p ()
91 (and auto-show-mode (auto-show-truncationp)
92 (equal (window-buffer) (current-buffer))
93 (not (memq this-command auto-show-inhibiting-commands))))
94
95 ;; XEmacs addition:
96 (defun auto-show-make-region-visible (start end)
97 "Move point in such a way that the region (START, END) is visible.
98 This only does anything if auto-show-mode is enabled, and it doesn't
99 actually do any horizontal scrolling; rather, it just sets things up so
100 that the region will be visible when `auto-show-make-point-visible'
101 is next called (this happens after every command)."
102 (if (auto-show-should-take-action-p)
103 (let* ((col (current-column)) ;column on line point is at
104 (scroll (window-hscroll));how far window is scrolled
105 (w-width (- (window-width)
106 (if (> scroll 0)
107 2 1))) ;how wide window is on the screen
108 (right-col (+ scroll w-width))
109 (start-col (save-excursion (goto-char start) (current-column)))
110 (end-col (save-excursion (goto-char end) (current-column))))
111 (cond ((and (>= start-col scroll)
112 (<= end-col right-col))
113 ;; already completely visible
114 nil)
115 ((< start-col scroll)
116 (scroll-right (- scroll start-col)))
117 (t
118 (scroll-left (- end-col right-col)))))))
119
120 (defun auto-show-make-point-visible (&optional ignore-arg)
121 "Scroll horizontally to make point visible, if that is enabled.
122 This function only does something if `auto-show-mode' is non-nil
123 and longlines are being truncated in the selected window.
124 See also the command `auto-show-toggle'."
125 (interactive)
126 (if (auto-show-should-take-action-p)
127 (let* ((col (current-column)) ;column on line point is at
128 (scroll (window-hscroll)) ;how far window is scrolled
129 (w-width (- (window-width)
130 (if (> scroll 0)
131 2 1))) ;how wide window is on the screen
132 (right-col (+ scroll w-width)))
133 (if (and (< col auto-show-show-left-margin-threshold)
134 (< col (window-width))
135 (> scroll 0))
136 (scroll-right scroll)
137 (if (< col scroll) ;to the left of the screen
138 (scroll-right (+ (- scroll col) auto-show-shift-amount))
139 (if (or (> col right-col) ;to the right of the screen
140 (and (= col right-col)
141 (not (eolp))))
142 (scroll-left (+ auto-show-shift-amount
143 (- col (+ scroll w-width))))
144 )
145 )
146 )
147 )
148 )
149 )
150
151 ;; XEmacs change:
152 ;; #### instead of this, we kludgily call it from the C code, to make sure
153 ;; that it's done after any other things on post-command-hook (which might
154 ;; move point).
155 ;; Do auto-scrolling after commands.
156 ;;(add-hook 'post-command-hook 'auto-show-make-point-visible)
157
158 ;; If being dumped, turn it on right away.
159 (and (noninteractive) (auto-show-mode 1))
160
161 ;; Do auto-scrolling in comint buffers after process output also.
162 ; XEmacs -- don't do this now, it messes up comint.
163 ;(add-hook 'comint-output-filter-functions 'auto-show-make-point-visible t)
164
165 (provide 'auto-show)
166
167 ;; auto-show.el ends here
168