(rmail-summary-rmail-update): Update rmail buffer even if it isn't displayed.
[emacs.git] / lisp / scroll-bar.el
blob462fdad55aec2203a7335ecbbb60f524e63ac247
1 ;;; scroll-bar.el --- window system-independent scroll bar support.
3 ;;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: hardware
8 ;;; This file is part of GNU Emacs.
10 ;;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2, or (at your option)
13 ;;; any later version.
15 ;;; GNU Emacs is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Code:
26 ;;; Commentary:
28 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
29 ;; Presently emulates the scroll-bar behavior of xterm.
30 ;;; Code:
32 (require 'mouse)
35 ;;;; Utilities.
37 (defun scroll-bar-scale (num-denom whole)
38 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
39 This is handy for scaling a position on a scroll bar into real units,
40 like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
41 from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
42 \(buffer-size)) is the position in the current buffer corresponding to
43 that scroll bar position."
44 ;; We multiply before we divide to maintain precision.
45 ;; We use floating point because the product of a large buffer size
46 ;; with a large scroll bar portion can easily overflow a lisp int.
47 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
50 ;;;; Helpful functions for enabling and disabling scroll bars.
52 (defun scroll-bar-mode (flag)
53 "Toggle display of vertical scroll bars on each frame.
54 This command applies to all frames that exist and frames to be
55 created in the future.
56 With a numeric argument, if the argument is negative,
57 turn off scroll bars; otherwise, turn on scroll bars."
58 (interactive "P")
59 (if flag (setq flag (prefix-numeric-value flag)))
61 ;; Obtain the current setting by looking at default-frame-alist.
62 (let ((scroll-bar-mode
63 (let ((assq (assq 'vertical-scroll-bars default-frame-alist)))
64 (if assq (cdr assq) t))))
66 ;; Tweedle it according to the argument.
67 (setq scroll-bar-mode (if (null flag) (not scroll-bar-mode)
68 (or (not (numberp flag)) (>= flag 0))))
70 ;; Apply it to default-frame-alist.
71 (mapcar
72 (function
73 (lambda (param-name)
74 (let ((parameter (assq param-name default-frame-alist)))
75 (if (consp parameter)
76 (setcdr parameter scroll-bar-mode)
77 (setq default-frame-alist
78 (cons (cons param-name scroll-bar-mode)
79 default-frame-alist))))))
80 '(vertical-scroll-bars horizontal-scroll-bars))
82 ;; Apply it to existing frames.
83 (let ((frames (frame-list)))
84 (while frames
85 (modify-frame-parameters
86 (car frames)
87 (list (cons 'vertical-scroll-bars scroll-bar-mode)
88 (cons 'horizontal-scroll-bars scroll-bar-mode)))
89 (setq frames (cdr frames))))))
91 ;;;; Buffer navigation using the scroll bar.
93 ;;; This was used for up-events on button 2, but no longer.
94 (defun scroll-bar-set-window-start (event)
95 "Set the window start according to where the scroll bar is dragged.
96 EVENT should be a scroll bar click or drag event."
97 (interactive "e")
98 (let* ((end-position (event-end event))
99 (window (nth 0 end-position))
100 (portion-whole (nth 2 end-position)))
101 (save-excursion
102 (set-buffer (window-buffer window))
103 (save-excursion
104 (goto-char (+ (point-min)
105 (scroll-bar-scale portion-whole
106 (- (point-max) (point-min)))))
107 (beginning-of-line)
108 (set-window-start window (point))))))
110 ;; Scroll the window to the proper position for EVENT.
111 (defun scroll-bar-drag-1 (event)
112 (let* ((start-position (event-start event))
113 (window (nth 0 start-position))
114 (portion-whole (nth 2 start-position)))
115 (save-excursion
116 (set-buffer (window-buffer window))
117 ;; Calculate position relative to the accessible part of the buffer.
118 (goto-char (+ (point-min)
119 (scroll-bar-scale portion-whole
120 (- (point-max) (point-min)))))
121 (beginning-of-line)
122 (set-window-start window (point)))))
124 (defun scroll-bar-drag (event)
125 "Scroll the window by dragging the scroll bar slider.
126 If you click outside the slider, the window scrolls to bring the slider there."
127 (interactive "e")
128 (let* (done)
129 (scroll-bar-drag-1 event)
130 (track-mouse
131 (while (not done)
132 (setq event (read-event))
133 (if (eq (car-safe event) 'mouse-movement)
134 (setq event (read-event)))
135 (cond ((eq (car-safe event) 'scroll-bar-movement)
136 (scroll-bar-drag-1 event))
138 ;; Exit when we get the drag event; ignore that event.
139 (setq done t)))))))
141 (defun scroll-bar-scroll-down (event)
142 "Scroll the window's top line down to the location of the scroll bar click.
143 EVENT should be a scroll bar click."
144 (interactive "e")
145 (let ((old-selected-window (selected-window)))
146 (unwind-protect
147 (progn
148 (let* ((end-position (event-end event))
149 (window (nth 0 end-position))
150 (portion-whole (nth 2 end-position)))
151 (select-window window)
152 (scroll-down
153 (scroll-bar-scale portion-whole (1- (window-height))))))
154 (select-window old-selected-window))))
156 (defun scroll-bar-scroll-up (event)
157 "Scroll the line next to the scroll bar click to the top of the window.
158 EVENT should be a scroll bar click."
159 (interactive "e")
160 (let ((old-selected-window (selected-window)))
161 (unwind-protect
162 (progn
163 (let* ((end-position (event-end event))
164 (window (nth 0 end-position))
165 (portion-whole (nth 2 end-position)))
166 (select-window window)
167 (scroll-up
168 (scroll-bar-scale portion-whole (1- (window-height))))))
169 (select-window old-selected-window))))
172 ;;;; Bindings.
174 ;;; For now, we'll set things up to work like xterm.
175 (global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
176 (global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
178 (global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
180 (global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
181 (global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
184 (provide 'scroll-bar)
186 ;;; scroll-bar.el ends here