1 ;;; scroll-bar.el --- window system-independent scroll bar support.
3 ;;; Copyright (C) 1993 Free Software Foundation, Inc.
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.
28 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
29 ;; Presently emulates the scroll-bar behavior of xterm.
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.
51 (defvar scroll-bar-mode nil
)
53 (defun scroll-bar-mode (flag)
54 "Toggle display of vertical scroll bars on each frame.
55 This command applies to all frames that exist and frames to be
56 created in the future.
57 With a numeric argument, if the argument is negative,
58 turn off scroll bars; otherwise, turn on scroll bars."
60 (setq scroll-bar-mode
(if (null flag
) (not scroll-bar-mode
)
61 (or (not (numberp flag
)) (>= flag
0))))
65 (let ((parameter (assq param-name default-frame-alist
)))
67 (setcdr parameter scroll-bar-mode
)
68 (setq default-frame-alist
69 (cons (cons param-name scroll-bar-mode
)
70 default-frame-alist
))))))
71 '(vertical-scroll-bars horizontal-scroll-bars
))
72 (let ((frames (frame-list)))
74 (modify-frame-parameters
76 (list (cons 'vertical-scroll-bars scroll-bar-mode
)
77 (cons 'horizontal-scroll-bars scroll-bar-mode
)))
78 (setq frames
(cdr frames
)))))
80 ;;;; Buffer navigation using the scroll bar.
82 (defun scroll-bar-set-window-start (event)
83 "Set the window start according to where the scroll bar is dragged.
84 EVENT should be a scroll bar click or drag event."
86 (let* ((end-position (event-end event
))
87 (window (nth 0 end-position
))
88 (portion-whole (nth 2 end-position
)))
90 (set-buffer (window-buffer window
))
92 (goto-char (scroll-bar-scale portion-whole
(buffer-size)))
94 (set-window-start window
(point))))))
96 (defun scroll-bar-scroll-down (event)
97 "Scroll the window's top line down to the location of the scroll bar click.
98 EVENT should be a scroll bar click."
100 (let ((old-selected-window (selected-window)))
103 (let* ((end-position (event-end event
))
104 (window (nth 0 end-position
))
105 (portion-whole (nth 2 end-position
)))
106 (select-window window
)
108 (scroll-bar-scale portion-whole
(1- (window-height))))))
109 (select-window old-selected-window
))))
111 (defun scroll-bar-scroll-up (event)
112 "Scroll the line next to the scroll bar click to the top of the window.
113 EVENT should be a scroll bar click."
115 (let ((old-selected-window (selected-window)))
118 (let* ((end-position (event-end event
))
119 (window (nth 0 end-position
))
120 (portion-whole (nth 2 end-position
)))
121 (select-window window
)
123 (scroll-bar-scale portion-whole
(1- (window-height))))))
124 (select-window old-selected-window
))))
129 ;;; For now, we'll set things up to work like xterm.
130 (global-set-key [vertical-scroll-bar mouse-1
] 'scroll-bar-scroll-up
)
131 (global-set-key [vertical-scroll-bar drag-mouse-1
] 'scroll-bar-scroll-up
)
133 (global-set-key [vertical-scroll-bar mouse-2
] 'scroll-bar-set-window-start
)
134 (global-set-key [vertical-scroll-bar drag-mouse-2
] 'scroll-bar-set-window-start
)
136 (global-set-key [vertical-scroll-bar mouse-3
] 'scroll-bar-scroll-down
)
137 (global-set-key [vertical-scroll-bar drag-mouse-3
] 'scroll-bar-scroll-down
)
140 (provide 'scroll-bar
)
142 ;;; scroll-bar.el ends here