13337
|
1 ;;; auto-show.el --- perform automatic horizontal scrolling as point moves
|
10974
|
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 ;;; Maintainer: FSF
|
|
7
|
|
8 ;;; Commentary:
|
10973
|
9
|
10974
|
10 ;;; This file provides functions that
|
10973
|
11 ;;; automatically scroll the window horizontally when the point moves
|
10974
|
12 ;;; off the left or right side of the window.
|
|
13
|
|
14 ;;; Once this library is loaded, automatic horizontal scrolling
|
|
15 ;;; occurs whenever long lines are being truncated.
|
|
16 ;;; To request truncation of long lines, set the variable
|
|
17 ;;; Setting the variable `truncate-lines' to non-nil.
|
|
18 ;;; You can do this for all buffers as follows:
|
10973
|
19 ;;;
|
|
20 ;;; (set-default 'truncate-lines t)
|
|
21
|
10974
|
22 ;;; Here is how to do it for C mode only:
|
10973
|
23 ;;;
|
|
24 ;;; (set-default 'truncate-lines nil) ; this is the original value
|
|
25 ;;; (defun my-c-mode-hook ()
|
|
26 ;;; "Run when C-mode starts up. Changes ..."
|
|
27 ;;; ... set various personal preferences ...
|
|
28 ;;; (setq truncate-lines t))
|
|
29 ;;; (add-hook 'c-mode-hook 'my-c-mode-hook)
|
|
30 ;;;
|
|
31 ;;;
|
10974
|
32 ;;; As a finer level of control, you can still have truncated lines but
|
|
33 ;;; without the automatic horizontal scrolling by setting the buffer
|
|
34 ;;; local variable `auto-show-mode' to nil. The default value is t.
|
|
35 ;;; The command `auto-show-mode' toggles the value of the variable
|
|
36 ;;; `auto-show-mode'.
|
10973
|
37
|
10974
|
38 ;;; Code:
|
10973
|
39
|
10974
|
40 (defvar auto-show-mode t
|
|
41 "*Non-nil enables automatic horizontal scrolling, when lines are truncated.
|
|
42 The default value is t. To change the default, do this:
|
|
43 (set-default 'auto-show-mode nil)
|
|
44 See also command `auto-show-mode'.
|
16979
|
45 This variable has no effect when lines are not being truncated.
|
|
46 This variable is automatically local in each buffer where it is set.")
|
10973
|
47
|
10974
|
48 (make-variable-buffer-local 'auto-show-mode)
|
10973
|
49
|
|
50 (defvar auto-show-shift-amount 8
|
10974
|
51 "*Extra columns to scroll. for automatic horizontal scrolling.")
|
10973
|
52
|
|
53 (defvar auto-show-show-left-margin-threshold 50
|
10974
|
54 "*Threshold column for automatic horizontal scrolling to the right.
|
|
55 If point is before this column, we try to scroll to make the left margin
|
10973
|
56 visible. Setting this to 0 disables this feature.")
|
|
57
|
|
58 (defun auto-show-truncationp ()
|
10974
|
59 "True if line truncation is enabled for the selected window."
|
10973
|
60 (or truncate-lines
|
|
61 (and truncate-partial-width-windows
|
|
62 (< (window-width) (frame-width)))))
|
|
63
|
10974
|
64 ;;;###autoload
|
|
65 (defun auto-show-mode (arg)
|
|
66 "Turn automatic horizontal scroll mode on or off.
|
16979
|
67 With arg, turn auto scrolling on if arg is positive, off otherwise.
|
|
68 This mode is enabled or disabled for each buffer individually.
|
|
69 It takes effect only when `truncate-lines' is non-nil."
|
10974
|
70 (interactive "P")
|
|
71 (setq auto-show-mode
|
|
72 (if (null arg)
|
|
73 (not auto-show-mode)
|
|
74 (> (prefix-numeric-value arg) 0))))
|
10973
|
75
|
|
76 (defun auto-show-make-point-visible (&optional ignore-arg)
|
10974
|
77 "Scroll horizontally to make point visible, if that is enabled.
|
|
78 This function only does something if `auto-show-mode' is non-nil
|
|
79 and longlines are being truncated in the selected window.
|
16979
|
80 See also the command `auto-show-mode'."
|
10973
|
81 (interactive)
|
10974
|
82 (if (and auto-show-mode (auto-show-truncationp)
|
10973
|
83 (equal (window-buffer) (current-buffer)))
|
|
84 (let* ((col (current-column)) ;column on line point is at
|
|
85 (scroll (window-hscroll)) ;how far window is scrolled
|
|
86 (w-width (- (window-width)
|
|
87 (if (> scroll 0)
|
|
88 2 1))) ;how wide window is on the screen
|
|
89 (right-col (+ scroll w-width)))
|
|
90 (if (and (< col auto-show-show-left-margin-threshold)
|
|
91 (< col (window-width))
|
|
92 (> scroll 0))
|
|
93 (scroll-right scroll)
|
|
94 (if (< col scroll) ;to the left of the screen
|
|
95 (scroll-right (+ (- scroll col) auto-show-shift-amount))
|
|
96 (if (or (> col right-col) ;to the right of the screen
|
|
97 (and (= col right-col)
|
|
98 (not (eolp))))
|
|
99 (scroll-left (+ auto-show-shift-amount
|
|
100 (- col (+ scroll w-width))))
|
|
101 )
|
|
102 )
|
|
103 )
|
|
104 )
|
|
105 )
|
|
106 )
|
|
107
|
10974
|
108 ;; Do auto-scrolling after commands.
|
|
109 (add-hook 'post-command-hook 'auto-show-make-point-visible)
|
10973
|
110
|
10974
|
111 ;; Do auto-scrolling in comint buffers after process output also.
|
|
112 (add-hook 'comint-output-filter-functions 'auto-show-make-point-visible t)
|
10973
|
113
|
10974
|
114 (provide 'auto-show)
|
10973
|
115
|
10974
|
116 ;; auto-show.el ends here
|
|
117
|