(Fsignal_process): Allow signal names as well as numbers.
[bpt/emacs.git] / lisp / scroll-bar.el
CommitLineData
aae56ea7 1;;; scroll-bar.el --- window system-independent scroll bar support.
6d62a90e 2
8f1204db 3;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
6d62a90e
JB
4
5;; Maintainer: FSF
6;; Keywords: hardware
7
8;;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
23
76550a57
ER
24;;; Code:
25
d9ecc911
ER
26;;; Commentary:
27
28;; Window-system-independent bindings of mouse clicks on the scroll bar.
29;; Presently emulates the scroll-bar behavior of xterm.
30;;; Code:
31
dbc4e1c1
JB
32(require 'mouse)
33
6d62a90e
JB
34\f
35;;;; Utilities.
36
86828678
RS
37(defun scroll-bar-event-ratio (event)
38 "Given a scroll bar event EVENT, return the scroll bar position as a ratio.
39The value is a cons cell (PORTION . WHOLE) containing two integers
40whose ratio gives the event's vertical position in the scroll bar, with 0
41referring to the top and 1 to the bottom."
42 (nth 2 event))
43
bf3c8a70 44(defun scroll-bar-scale (num-denom whole)
6d62a90e 45 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
bf3c8a70
JB
46This is handy for scaling a position on a scroll bar into real units,
47like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
48from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
6d62a90e 49\(buffer-size)) is the position in the current buffer corresponding to
bf3c8a70 50that scroll bar position."
6d62a90e
JB
51 ;; We multiply before we divide to maintain precision.
52 ;; We use floating point because the product of a large buffer size
bf3c8a70 53 ;; with a large scroll bar portion can easily overflow a lisp int.
6d62a90e
JB
54 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
55
56\f
fe48f821 57;;;; Helpful functions for enabling and disabling scroll bars.
fe48f821
JB
58
59(defun scroll-bar-mode (flag)
60 "Toggle display of vertical scroll bars on each frame.
61This command applies to all frames that exist and frames to be
62created in the future.
63With a numeric argument, if the argument is negative,
64turn off scroll bars; otherwise, turn on scroll bars."
65 (interactive "P")
6ee9f953 66 (if flag (setq flag (prefix-numeric-value flag)))
ada464a7
JB
67
68 ;; Obtain the current setting by looking at default-frame-alist.
69 (let ((scroll-bar-mode
70 (let ((assq (assq 'vertical-scroll-bars default-frame-alist)))
71 (if assq (cdr assq) t))))
72
73 ;; Tweedle it according to the argument.
74 (setq scroll-bar-mode (if (null flag) (not scroll-bar-mode)
75 (or (not (numberp flag)) (>= flag 0))))
76
77 ;; Apply it to default-frame-alist.
78 (mapcar
79 (function
80 (lambda (param-name)
81 (let ((parameter (assq param-name default-frame-alist)))
82 (if (consp parameter)
83 (setcdr parameter scroll-bar-mode)
84 (setq default-frame-alist
85 (cons (cons param-name scroll-bar-mode)
86 default-frame-alist))))))
87 '(vertical-scroll-bars horizontal-scroll-bars))
88
89 ;; Apply it to existing frames.
90 (let ((frames (frame-list)))
91 (while frames
92 (modify-frame-parameters
93 (car frames)
94 (list (cons 'vertical-scroll-bars scroll-bar-mode)
95 (cons 'horizontal-scroll-bars scroll-bar-mode)))
96 (setq frames (cdr frames))))))
fe48f821 97\f
bf3c8a70 98;;;; Buffer navigation using the scroll bar.
6d62a90e 99
e532b016 100;;; This was used for up-events on button 2, but no longer.
bf3c8a70
JB
101(defun scroll-bar-set-window-start (event)
102 "Set the window start according to where the scroll bar is dragged.
103EVENT should be a scroll bar click or drag event."
6d62a90e 104 (interactive "e")
dbc4e1c1 105 (let* ((end-position (event-end event))
6d62a90e
JB
106 (window (nth 0 end-position))
107 (portion-whole (nth 2 end-position)))
108 (save-excursion
109 (set-buffer (window-buffer window))
110 (save-excursion
4cad38d5
JB
111 (goto-char (+ (point-min)
112 (scroll-bar-scale portion-whole
113 (- (point-max) (point-min)))))
6d62a90e
JB
114 (beginning-of-line)
115 (set-window-start window (point))))))
116
e532b016
RS
117;; Scroll the window to the proper position for EVENT.
118(defun scroll-bar-drag-1 (event)
119 (let* ((start-position (event-start event))
120 (window (nth 0 start-position))
121 (portion-whole (nth 2 start-position)))
122 (save-excursion
123 (set-buffer (window-buffer window))
d2ae6f7e
RS
124 ;; Calculate position relative to the accessible part of the buffer.
125 (goto-char (+ (point-min)
126 (scroll-bar-scale portion-whole
127 (- (point-max) (point-min)))))
e532b016
RS
128 (beginning-of-line)
129 (set-window-start window (point)))))
130
131(defun scroll-bar-drag (event)
132 "Scroll the window by dragging the scroll bar slider.
133If you click outside the slider, the window scrolls to bring the slider there."
134 (interactive "e")
135 (let* (done)
8f37c4f3
RS
136 (or point-before-scroll
137 (setq point-before-scroll (point)))
e532b016 138 (scroll-bar-drag-1 event)
8f37c4f3
RS
139 (let (point-before-scroll)
140 (track-mouse
141 (while (not done)
142 (setq event (read-event))
143 (if (eq (car-safe event) 'mouse-movement)
144 (setq event (read-event)))
145 (cond ((eq (car-safe event) 'scroll-bar-movement)
146 (scroll-bar-drag-1 event))
147 (t
148 ;; Exit when we get the drag event; ignore that event.
149 (setq done t))))))))
e532b016 150
bf3c8a70
JB
151(defun scroll-bar-scroll-down (event)
152 "Scroll the window's top line down to the location of the scroll bar click.
153EVENT should be a scroll bar click."
6d62a90e
JB
154 (interactive "e")
155 (let ((old-selected-window (selected-window)))
156 (unwind-protect
157 (progn
dbc4e1c1 158 (let* ((end-position (event-end event))
6d62a90e
JB
159 (window (nth 0 end-position))
160 (portion-whole (nth 2 end-position)))
8f37c4f3
RS
161 (let (point-before-scroll)
162 (select-window window))
163 (or point-before-scroll
164 (setq point-before-scroll (point)))
165 (let (point-before-scroll)
166 (scroll-down
167 (scroll-bar-scale portion-whole (1- (window-height)))))))
6d62a90e
JB
168 (select-window old-selected-window))))
169
bf3c8a70
JB
170(defun scroll-bar-scroll-up (event)
171 "Scroll the line next to the scroll bar click to the top of the window.
172EVENT should be a scroll bar click."
6d62a90e
JB
173 (interactive "e")
174 (let ((old-selected-window (selected-window)))
175 (unwind-protect
176 (progn
dbc4e1c1 177 (let* ((end-position (event-end event))
6d62a90e
JB
178 (window (nth 0 end-position))
179 (portion-whole (nth 2 end-position)))
8f37c4f3
RS
180 (let (point-before-scroll)
181 (select-window window))
182 (or point-before-scroll
183 (setq point-before-scroll (point)))
184 (let (point-before-scroll)
185 (scroll-up
186 (scroll-bar-scale portion-whole (1- (window-height)))))))
6d62a90e
JB
187 (select-window old-selected-window))))
188
189\f
190;;;; Bindings.
191
192;;; For now, we'll set things up to work like xterm.
bf3c8a70
JB
193(global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
194(global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
6d62a90e 195
e532b016
RS
196(global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
197
bf3c8a70
JB
198(global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
199(global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
6d62a90e
JB
200
201\f
dc14eed2 202(provide 'scroll-bar)
6d62a90e 203
bf3c8a70 204;;; scroll-bar.el ends here