scroll-bar.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. ;;; scroll-bar.el --- window system-independent scroll bar support
  2. ;; Copyright (C) 1993-1995, 1999-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: hardware
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Window-system-independent bindings of mouse clicks on the scroll bar.
  19. ;; Presently emulates the scroll-bar behavior of xterm.
  20. ;;; Code:
  21. (require 'mouse)
  22. (eval-when-compile (require 'cl))
  23. ;;;; Utilities.
  24. (defun scroll-bar-event-ratio (event)
  25. "Given a scroll bar event EVENT, return the scroll bar position as a ratio.
  26. The value is a cons cell (PORTION . WHOLE) containing two integers
  27. whose ratio gives the event's vertical position in the scroll bar, with 0
  28. referring to the top and 1 to the bottom."
  29. (nth 2 event))
  30. (defun scroll-bar-scale (num-denom whole)
  31. "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
  32. This is handy for scaling a position on a scroll bar into real units,
  33. like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
  34. from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
  35. \(buffer-size)) is the position in the current buffer corresponding to
  36. that scroll bar position."
  37. ;; We multiply before we divide to maintain precision.
  38. ;; We use floating point because the product of a large buffer size
  39. ;; with a large scroll bar portion can easily overflow a lisp int.
  40. (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
  41. (defun scroll-bar-columns (side)
  42. "Return the width, measured in columns, of the vertical scrollbar on SIDE.
  43. SIDE must be the symbol `left' or `right'."
  44. (let* ((wsb (window-scroll-bars))
  45. (vtype (nth 2 wsb))
  46. (cols (nth 1 wsb)))
  47. (cond
  48. ((not (memq side '(left right)))
  49. (error "`left' or `right' expected instead of %S" side))
  50. ((and (eq vtype side) cols))
  51. ((eq (frame-parameter nil 'vertical-scroll-bars) side)
  52. ;; nil means it's a non-toolkit scroll bar, and its width in
  53. ;; columns is 14 pixels rounded up.
  54. (ceiling (or (frame-parameter nil 'scroll-bar-width) 14)
  55. (frame-char-width)))
  56. (0))))
  57. ;;;; Helpful functions for enabling and disabling scroll bars.
  58. (defvar scroll-bar-mode)
  59. (defvar previous-scroll-bar-mode nil)
  60. (defvar scroll-bar-mode-explicit nil
  61. "Non-nil means `set-scroll-bar-mode' should really do something.
  62. This is nil while loading `scroll-bar.el', and t afterward.")
  63. (defun set-scroll-bar-mode (value)
  64. "Set the scroll bar mode to VALUE and put the new value into effect.
  65. See the `scroll-bar-mode' variable for possible values to use."
  66. (if scroll-bar-mode
  67. (setq previous-scroll-bar-mode scroll-bar-mode))
  68. (setq scroll-bar-mode value)
  69. (when scroll-bar-mode-explicit
  70. (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
  71. scroll-bar-mode)))))
  72. (defcustom scroll-bar-mode default-frame-scroll-bars
  73. "Specify whether to have vertical scroll bars, and on which side.
  74. Possible values are nil (no scroll bars), `left' (scroll bars on left)
  75. and `right' (scroll bars on right).
  76. To set this variable in a Lisp program, use `set-scroll-bar-mode'
  77. to make it take real effect.
  78. Setting the variable with a customization buffer also takes effect."
  79. :type '(choice (const :tag "none (nil)" nil)
  80. (const left)
  81. (const right))
  82. :group 'frames
  83. ;; The default value for :initialize would try to use :set
  84. ;; when processing the file in cus-dep.el.
  85. :initialize 'custom-initialize-default
  86. :set (lambda (_sym val) (set-scroll-bar-mode val)))
  87. ;; We just set scroll-bar-mode, but that was the default.
  88. ;; If it is set again, that is for real.
  89. (setq scroll-bar-mode-explicit t)
  90. (defun get-scroll-bar-mode () scroll-bar-mode)
  91. (defsetf get-scroll-bar-mode set-scroll-bar-mode)
  92. (define-minor-mode scroll-bar-mode
  93. "Toggle vertical scroll bars on all frames (Scroll Bar mode).
  94. With a prefix argument ARG, enable Scroll Bar mode if ARG is
  95. positive, and disable it otherwise. If called from Lisp, enable
  96. the mode if ARG is omitted or nil.
  97. This command applies to all frames that exist and frames to be
  98. created in the future."
  99. :variable (eq (get-scroll-bar-mode)
  100. (or previous-scroll-bar-mode
  101. default-frame-scroll-bars)))
  102. (defun toggle-scroll-bar (arg)
  103. "Toggle whether or not the selected frame has vertical scroll bars.
  104. With arg, turn vertical scroll bars on if and only if arg is positive.
  105. The variable `scroll-bar-mode' controls which side the scroll bars are on
  106. when they are turned on; if it is nil, they go on the left."
  107. (interactive "P")
  108. (if (null arg)
  109. (setq arg
  110. (if (cdr (assq 'vertical-scroll-bars
  111. (frame-parameters (selected-frame))))
  112. -1 1))
  113. (setq arg (prefix-numeric-value arg)))
  114. (modify-frame-parameters
  115. (selected-frame)
  116. (list (cons 'vertical-scroll-bars
  117. (if (> arg 0)
  118. (or scroll-bar-mode default-frame-scroll-bars))))))
  119. (defun toggle-horizontal-scroll-bar (_arg)
  120. "Toggle whether or not the selected frame has horizontal scroll bars.
  121. With arg, turn horizontal scroll bars on if and only if arg is positive.
  122. Horizontal scroll bars aren't implemented yet."
  123. (interactive "P")
  124. (error "Horizontal scroll bars aren't implemented yet"))
  125. ;;;; Buffer navigation using the scroll bar.
  126. ;; This was used for up-events on button 2, but no longer.
  127. (defun scroll-bar-set-window-start (event)
  128. "Set the window start according to where the scroll bar is dragged.
  129. EVENT should be a scroll bar click or drag event."
  130. (interactive "e")
  131. (let* ((end-position (event-end event))
  132. (window (nth 0 end-position))
  133. (portion-whole (nth 2 end-position)))
  134. (with-current-buffer (window-buffer window)
  135. (save-excursion
  136. (goto-char (+ (point-min)
  137. (scroll-bar-scale portion-whole
  138. (- (point-max) (point-min)))))
  139. (beginning-of-line)
  140. (set-window-start window (point))))))
  141. (defun scroll-bar-drag-position (portion-whole)
  142. "Calculate new window start for drag event."
  143. (save-excursion
  144. (goto-char (+ (point-min)
  145. (scroll-bar-scale portion-whole
  146. (- (point-max) (point-min)))))
  147. (beginning-of-line)
  148. (point)))
  149. (defun scroll-bar-maybe-set-window-start (event)
  150. "Set the window start according to where the scroll bar is dragged.
  151. Only change window start if the new start is substantially different.
  152. EVENT should be a scroll bar click or drag event."
  153. (interactive "e")
  154. (let* ((end-position (event-end event))
  155. (window (nth 0 end-position))
  156. (portion-whole (nth 2 end-position))
  157. (next-portion-whole (cons (1+ (car portion-whole))
  158. (cdr portion-whole)))
  159. portion-start
  160. next-portion-start
  161. (current-start (window-start window)))
  162. (with-current-buffer (window-buffer window)
  163. (setq portion-start (scroll-bar-drag-position portion-whole))
  164. (setq next-portion-start (max
  165. (scroll-bar-drag-position next-portion-whole)
  166. (1+ portion-start)))
  167. (if (or (>= current-start next-portion-start)
  168. (< current-start portion-start))
  169. (set-window-start window portion-start)
  170. ;; Always set window start, to ensure scroll bar position is updated.
  171. (set-window-start window current-start)))))
  172. ;; Scroll the window to the proper position for EVENT.
  173. (defun scroll-bar-drag-1 (event)
  174. (let* ((start-position (event-start event))
  175. (window (nth 0 start-position))
  176. (portion-whole (nth 2 start-position)))
  177. (save-excursion
  178. (with-current-buffer (window-buffer window)
  179. ;; Calculate position relative to the accessible part of the buffer.
  180. (goto-char (+ (point-min)
  181. (scroll-bar-scale portion-whole
  182. (- (point-max) (point-min)))))
  183. (vertical-motion 0 window)
  184. (set-window-start window (point))))))
  185. (defun scroll-bar-drag (event)
  186. "Scroll the window by dragging the scroll bar slider.
  187. If you click outside the slider, the window scrolls to bring the slider there."
  188. (interactive "e")
  189. (let* (done
  190. (echo-keystrokes 0)
  191. (end-position (event-end event))
  192. (window (nth 0 end-position))
  193. (before-scroll))
  194. (with-current-buffer (window-buffer window)
  195. (setq before-scroll point-before-scroll))
  196. (save-selected-window
  197. (select-window window)
  198. (setq before-scroll
  199. (or before-scroll (point))))
  200. (scroll-bar-drag-1 event)
  201. (track-mouse
  202. (while (not done)
  203. (setq event (read-event))
  204. (if (eq (car-safe event) 'mouse-movement)
  205. (setq event (read-event)))
  206. (cond ((eq (car-safe event) 'scroll-bar-movement)
  207. (scroll-bar-drag-1 event))
  208. (t
  209. ;; Exit when we get the drag event; ignore that event.
  210. (setq done t)))))
  211. (sit-for 0)
  212. (with-current-buffer (window-buffer window)
  213. (setq point-before-scroll before-scroll))))
  214. (defun scroll-bar-scroll-down (event)
  215. "Scroll the window's top line down to the location of the scroll bar click.
  216. EVENT should be a scroll bar click."
  217. (interactive "e")
  218. (let* ((end-position (event-end event))
  219. (window (nth 0 end-position))
  220. (before-scroll))
  221. (with-current-buffer (window-buffer window)
  222. (setq before-scroll point-before-scroll))
  223. (unwind-protect
  224. (save-selected-window
  225. (let ((portion-whole (nth 2 end-position)))
  226. (select-window window)
  227. (setq before-scroll
  228. (or before-scroll (point)))
  229. (scroll-down
  230. (scroll-bar-scale portion-whole (1- (window-height)))))
  231. (sit-for 0))
  232. (with-current-buffer (window-buffer window)
  233. (setq point-before-scroll before-scroll)))))
  234. (defun scroll-bar-scroll-up (event)
  235. "Scroll the line next to the scroll bar click to the top of the window.
  236. EVENT should be a scroll bar click."
  237. (interactive "e")
  238. (let* ((end-position (event-end event))
  239. (window (nth 0 end-position))
  240. (before-scroll))
  241. (with-current-buffer (window-buffer window)
  242. (setq before-scroll point-before-scroll))
  243. (unwind-protect
  244. (save-selected-window
  245. (let ((portion-whole (nth 2 end-position)))
  246. (select-window window)
  247. (setq before-scroll
  248. (or before-scroll (point)))
  249. (scroll-up
  250. (scroll-bar-scale portion-whole (1- (window-height)))))
  251. (sit-for 0))
  252. (with-current-buffer (window-buffer window)
  253. (setq point-before-scroll before-scroll)))))
  254. ;;; Tookit scroll bars.
  255. (defun scroll-bar-toolkit-scroll (event)
  256. (interactive "e")
  257. (let* ((end-position (event-end event))
  258. (window (nth 0 end-position))
  259. (part (nth 4 end-position))
  260. before-scroll)
  261. (cond ((eq part 'end-scroll))
  262. (t
  263. (with-current-buffer (window-buffer window)
  264. (setq before-scroll point-before-scroll))
  265. (save-selected-window
  266. (select-window window)
  267. (setq before-scroll (or before-scroll (point)))
  268. (cond ((eq part 'above-handle)
  269. (scroll-up '-))
  270. ((eq part 'below-handle)
  271. (scroll-up nil))
  272. ((eq part 'ratio)
  273. (let* ((portion-whole (nth 2 end-position))
  274. (lines (scroll-bar-scale portion-whole
  275. (1- (window-height)))))
  276. (scroll-up (cond ((not (zerop lines)) lines)
  277. ((< (car portion-whole) 0) -1)
  278. (t 1)))))
  279. ((eq part 'up)
  280. (scroll-up -1))
  281. ((eq part 'down)
  282. (scroll-up 1))
  283. ((eq part 'top)
  284. (set-window-start window (point-min)))
  285. ((eq part 'bottom)
  286. (goto-char (point-max))
  287. (recenter))
  288. ((eq part 'handle)
  289. (scroll-bar-drag-1 event))))
  290. (sit-for 0)
  291. (with-current-buffer (window-buffer window)
  292. (setq point-before-scroll before-scroll))))))
  293. ;;;; Bindings.
  294. ;; For now, we'll set things up to work like xterm.
  295. (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
  296. (global-set-key [vertical-scroll-bar mouse-1]
  297. 'scroll-bar-toolkit-scroll))
  298. (t
  299. (global-set-key [vertical-scroll-bar mouse-1]
  300. 'scroll-bar-scroll-up)
  301. (global-set-key [vertical-scroll-bar drag-mouse-1]
  302. 'scroll-bar-scroll-up)
  303. (global-set-key [vertical-scroll-bar down-mouse-2]
  304. 'scroll-bar-drag)
  305. (global-set-key [vertical-scroll-bar mouse-3]
  306. 'scroll-bar-scroll-down)
  307. (global-set-key [vertical-scroll-bar drag-mouse-3]
  308. 'scroll-bar-scroll-down)))
  309. (provide 'scroll-bar)
  310. ;;; scroll-bar.el ends here