Commit | Line | Data |
---|---|---|
6d62a90e JB |
1 | ;;; scrollbar.el -- window system-independent scrollbar 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 | ||
dbc4e1c1 JB |
24 | (require 'mouse) |
25 | ||
6d62a90e JB |
26 | \f |
27 | ;;;; Utilities. | |
28 | ||
29 | (defun scrollbar-scale (num-denom whole) | |
30 | "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM). | |
31 | This is handy for scaling a position on a scrollbar into real units, | |
32 | like buffer positions. If SCROLLBAR-POS is the (PORTION . WHOLE) pair | |
33 | from a scrollbar event, then (scrollbar-scale SCROLLBAR-POS | |
34 | \(buffer-size)) is the position in the current buffer corresponding to | |
35 | that scrollbar position." | |
36 | ;; We multiply before we divide to maintain precision. | |
37 | ;; We use floating point because the product of a large buffer size | |
38 | ;; with a large scrollbar portion can easily overflow a lisp int. | |
39 | (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom)))) | |
40 | ||
41 | \f | |
42 | ;;;; Buffer navigation using the scrollbar. | |
43 | ||
44 | (defun scrollbar-set-window-start (event) | |
45 | "Set the window start according to where the scrollbar is dragged. | |
46 | EVENT should be a scrollbar click or drag event." | |
47 | (interactive "e") | |
dbc4e1c1 | 48 | (let* ((end-position (event-end event)) |
6d62a90e JB |
49 | (window (nth 0 end-position)) |
50 | (portion-whole (nth 2 end-position))) | |
51 | (save-excursion | |
52 | (set-buffer (window-buffer window)) | |
53 | (save-excursion | |
54 | (goto-char (scrollbar-scale portion-whole (buffer-size))) | |
55 | (beginning-of-line) | |
56 | (set-window-start window (point)))))) | |
57 | ||
58 | (defun scrollbar-scroll-down (event) | |
59 | "Scroll the window's top line down to the location of the scrollbar click. | |
60 | EVENT should be a scrollbar click." | |
61 | (interactive "e") | |
62 | (let ((old-selected-window (selected-window))) | |
63 | (unwind-protect | |
64 | (progn | |
dbc4e1c1 | 65 | (let* ((end-position (event-end event)) |
6d62a90e JB |
66 | (window (nth 0 end-position)) |
67 | (portion-whole (nth 2 end-position))) | |
68 | (select-window window) | |
69 | (scroll-down | |
70 | (scrollbar-scale portion-whole (1- (window-height)))))) | |
71 | (select-window old-selected-window)))) | |
72 | ||
73 | (defun scrollbar-scroll-up (event) | |
74 | "Scroll the line next to the scrollbar click to the top of the window. | |
75 | EVENT should be a scrollbar click." | |
76 | (interactive "e") | |
77 | (let ((old-selected-window (selected-window))) | |
78 | (unwind-protect | |
79 | (progn | |
dbc4e1c1 | 80 | (let* ((end-position (event-end event)) |
6d62a90e JB |
81 | (window (nth 0 end-position)) |
82 | (portion-whole (nth 2 end-position))) | |
83 | (select-window window) | |
84 | (scroll-up | |
85 | (scrollbar-scale portion-whole (1- (window-height)))))) | |
86 | (select-window old-selected-window)))) | |
87 | ||
88 | \f | |
89 | ;;;; Bindings. | |
90 | ||
91 | ;;; For now, we'll set things up to work like xterm. | |
92 | (global-set-key [vertical-scrollbar mouse-1] 'scrollbar-scroll-up) | |
93 | (global-set-key [vertical-scrollbar drag-mouse-1] 'scrollbar-scroll-up) | |
94 | ||
95 | (global-set-key [vertical-scrollbar mouse-2] 'scrollbar-set-window-start) | |
96 | (global-set-key [vertical-scrollbar drag-mouse-2] 'scrollbar-set-window-start) | |
97 | ||
98 | (global-set-key [vertical-scrollbar mouse-3] 'scrollbar-scroll-down) | |
99 | (global-set-key [vertical-scrollbar drag-mouse-3] 'scrollbar-scroll-down) | |
100 | ||
101 | \f | |
102 | (provide 'scrollbar) | |
103 | ||
104 | ;;; scrollbar.el ends here |