Added or corrected Commentary headers
[bpt/emacs.git] / lisp / scroll-bar.el
1 ;;; scroll-bar.el --- window system-independent scroll bar support.
2
3 ;;; Copyright (C) 1993 Free Software Foundation, Inc.
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
24 ;;; Code:
25
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
32 (require 'mouse)
33
34 \f
35 ;;;; Utilities.
36
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))))
48
49 \f
50 ;;;; Helpful functions for enabling and disabling scroll bars.
51 (defvar scroll-bar-mode nil)
52
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."
59 (interactive "P")
60 (setq scroll-bar-mode (if (null flag) (not scroll-bar-mode)
61 (or (not (numberp flag)) (>= flag 0))))
62 (mapcar
63 (function
64 (lambda (param-name)
65 (let ((parameter (assq param-name default-frame-alist)))
66 (if (consp parameter)
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)))
73 (while frames
74 (modify-frame-parameters
75 (car frames)
76 (list (cons 'vertical-scroll-bars scroll-bar-mode)
77 (cons 'horizontal-scroll-bars scroll-bar-mode)))
78 (setq frames (cdr frames)))))
79 \f
80 ;;;; Buffer navigation using the scroll bar.
81
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."
85 (interactive "e")
86 (let* ((end-position (event-end event))
87 (window (nth 0 end-position))
88 (portion-whole (nth 2 end-position)))
89 (save-excursion
90 (set-buffer (window-buffer window))
91 (save-excursion
92 (goto-char (scroll-bar-scale portion-whole (buffer-size)))
93 (beginning-of-line)
94 (set-window-start window (point))))))
95
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."
99 (interactive "e")
100 (let ((old-selected-window (selected-window)))
101 (unwind-protect
102 (progn
103 (let* ((end-position (event-end event))
104 (window (nth 0 end-position))
105 (portion-whole (nth 2 end-position)))
106 (select-window window)
107 (scroll-down
108 (scroll-bar-scale portion-whole (1- (window-height))))))
109 (select-window old-selected-window))))
110
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."
114 (interactive "e")
115 (let ((old-selected-window (selected-window)))
116 (unwind-protect
117 (progn
118 (let* ((end-position (event-end event))
119 (window (nth 0 end-position))
120 (portion-whole (nth 2 end-position)))
121 (select-window window)
122 (scroll-up
123 (scroll-bar-scale portion-whole (1- (window-height))))))
124 (select-window old-selected-window))))
125
126 \f
127 ;;;; Bindings.
128
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)
132
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)
135
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)
138
139 \f
140 (provide 'scroll-bar)
141
142 ;;; scroll-bar.el ends here