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