scroll-bar.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. ;;; scroll-bar.el --- window system-independent scroll bar support
  2. ;; Copyright (C) 1993-1995, 1999-2017 Free Software Foundation, Inc.
  3. ;; Maintainer: emacs-devel@gnu.org
  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-lib))
  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. (defun scroll-bar-lines ()
  58. "Return the height, measured in lines, of the horizontal scrollbar."
  59. (let* ((wsb (window-scroll-bars))
  60. (htype (nth 5 wsb))
  61. (lines (nth 4 wsb)))
  62. (cond
  63. (htype lines)
  64. ((frame-parameter nil 'horizontal-scroll-bars)
  65. ;; nil means it's a non-toolkit scroll bar (which is currently
  66. ;; impossible), and its height in lines is 14 pixels rounded up.
  67. (ceiling (or (frame-parameter nil 'scroll-bar-height) 14)
  68. (frame-char-width)))
  69. (0))))
  70. ;;;; Helpful functions for enabling and disabling scroll bars.
  71. (defvar scroll-bar-mode)
  72. (defvar horizontal-scroll-bar-mode)
  73. (defvar previous-scroll-bar-mode nil)
  74. (defvar scroll-bar-mode-explicit nil
  75. "Non-nil means `set-scroll-bar-mode' should really do something.
  76. This is nil while loading `scroll-bar.el', and t afterward.")
  77. (defun set-scroll-bar-mode (value)
  78. "Set the scroll bar mode to VALUE and put the new value into effect.
  79. See the `scroll-bar-mode' variable for possible values to use."
  80. (if scroll-bar-mode
  81. (setq previous-scroll-bar-mode scroll-bar-mode))
  82. (setq scroll-bar-mode value)
  83. (when scroll-bar-mode-explicit
  84. (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
  85. scroll-bar-mode)))))
  86. (defcustom scroll-bar-mode default-frame-scroll-bars
  87. "Specify whether to have vertical scroll bars, and on which side.
  88. Possible values are nil (no scroll bars), `left' (scroll bars on left)
  89. and `right' (scroll bars on right).
  90. To set this variable in a Lisp program, use `set-scroll-bar-mode'
  91. to make it take real effect.
  92. Setting the variable with a customization buffer also takes effect."
  93. :type '(choice (const :tag "none (nil)" nil)
  94. (const left)
  95. (const right))
  96. :group 'frames
  97. ;; The default value for :initialize would try to use :set
  98. ;; when processing the file in cus-dep.el.
  99. :initialize 'custom-initialize-default
  100. :set (lambda (_sym val) (set-scroll-bar-mode val)))
  101. ;; We just set scroll-bar-mode, but that was the default.
  102. ;; If it is set again, that is for real.
  103. (setq scroll-bar-mode-explicit t)
  104. (defun get-scroll-bar-mode ()
  105. (declare (gv-setter set-scroll-bar-mode))
  106. scroll-bar-mode)
  107. (define-minor-mode scroll-bar-mode
  108. "Toggle vertical scroll bars on all frames (Scroll Bar mode).
  109. With a prefix argument ARG, enable Scroll Bar mode if ARG is
  110. positive, and disable it otherwise. If called from Lisp, enable
  111. the mode if ARG is omitted or nil.
  112. This command applies to all frames that exist and frames to be
  113. created in the future."
  114. :variable ((get-scroll-bar-mode)
  115. . (lambda (v) (set-scroll-bar-mode
  116. (if v (or previous-scroll-bar-mode
  117. default-frame-scroll-bars))))))
  118. (defun horizontal-scroll-bars-available-p ()
  119. "Return non-nil when horizontal scroll bars are available on this system."
  120. (and (display-graphic-p)
  121. (boundp 'x-toolkit-scroll-bars)
  122. x-toolkit-scroll-bars))
  123. (define-minor-mode horizontal-scroll-bar-mode
  124. "Toggle horizontal scroll bars on all frames (Horizontal Scroll Bar mode).
  125. With a prefix argument ARG, enable Horizontal Scroll Bar mode if
  126. ARG is positive, and disable it otherwise. If called from Lisp,
  127. enable the mode if ARG is omitted or nil.
  128. This command applies to all frames that exist and frames to be
  129. created in the future."
  130. :init-value nil
  131. :global t
  132. :group 'frames
  133. (if (and horizontal-scroll-bar-mode
  134. (not (horizontal-scroll-bars-available-p)))
  135. (progn
  136. (setq horizontal-scroll-bar-mode nil)
  137. (message "Horizontal scroll bars are not implemented on this system"))
  138. (dolist (frame (frame-list))
  139. (set-frame-parameter
  140. frame 'horizontal-scroll-bars horizontal-scroll-bar-mode))
  141. ;; Handle `default-frame-alist' entry.
  142. (setq default-frame-alist
  143. (cons (cons 'horizontal-scroll-bars horizontal-scroll-bar-mode)
  144. (assq-delete-all 'horizontal-scroll-bars
  145. default-frame-alist)))))
  146. (defun toggle-scroll-bar (arg)
  147. "Toggle whether or not the selected frame has vertical scroll bars.
  148. With ARG, turn vertical scroll bars on if and only if ARG is positive.
  149. The variable `scroll-bar-mode' controls which side the scroll bars are on
  150. when they are turned on; if it is nil, they go on the left."
  151. (interactive "P")
  152. (if (null arg)
  153. (setq arg
  154. (if (frame-parameter nil 'vertical-scroll-bars) -1 1))
  155. (setq arg (prefix-numeric-value arg)))
  156. (modify-frame-parameters
  157. (selected-frame)
  158. (list (cons 'vertical-scroll-bars
  159. (if (> arg 0)
  160. (or scroll-bar-mode default-frame-scroll-bars))))))
  161. (defun toggle-horizontal-scroll-bar (arg)
  162. "Toggle whether or not the selected frame has horizontal scroll bars.
  163. With ARG, turn vertical scroll bars on if and only if ARG is positive."
  164. (interactive "P")
  165. (if (null arg)
  166. (setq arg
  167. (if (frame-parameter nil 'horizontal-scroll-bars) -1 1))
  168. (setq arg (prefix-numeric-value arg)))
  169. (modify-frame-parameters
  170. (selected-frame)
  171. (list (cons 'horizontal-scroll-bars
  172. (when (> arg 0) 'bottom)))))
  173. ;;;; Buffer navigation using the scroll bar.
  174. ;; This was used for up-events on button 2, but no longer.
  175. (defun scroll-bar-set-window-start (event)
  176. "Set the window start according to where the scroll bar is dragged.
  177. EVENT should be a scroll bar click or drag event."
  178. (interactive "e")
  179. (let* ((end-position (event-end event))
  180. (window (nth 0 end-position))
  181. (portion-whole (nth 2 end-position)))
  182. (with-current-buffer (window-buffer window)
  183. (save-excursion
  184. (goto-char (+ (point-min)
  185. (scroll-bar-scale portion-whole
  186. (- (point-max) (point-min)))))
  187. (beginning-of-line)
  188. (set-window-start window (point))))))
  189. (defun scroll-bar-drag-position (portion-whole)
  190. "Calculate new window start for drag event."
  191. (save-excursion
  192. (goto-char (+ (point-min)
  193. (scroll-bar-scale portion-whole
  194. (- (point-max) (point-min)))))
  195. (beginning-of-line)
  196. (point)))
  197. (defun scroll-bar-maybe-set-window-start (event)
  198. "Set the window start according to where the scroll bar is dragged.
  199. Only change window start if the new start is substantially different.
  200. EVENT should be a scroll bar click or drag event."
  201. (interactive "e")
  202. (let* ((end-position (event-end event))
  203. (window (nth 0 end-position))
  204. (portion-whole (nth 2 end-position))
  205. (next-portion-whole (cons (1+ (car portion-whole))
  206. (cdr portion-whole)))
  207. portion-start
  208. next-portion-start
  209. (current-start (window-start window)))
  210. (with-current-buffer (window-buffer window)
  211. (setq portion-start (scroll-bar-drag-position portion-whole))
  212. (setq next-portion-start (max
  213. (scroll-bar-drag-position next-portion-whole)
  214. (1+ portion-start)))
  215. (if (or (>= current-start next-portion-start)
  216. (< current-start portion-start))
  217. (set-window-start window portion-start)
  218. ;; Always set window start, to ensure scroll bar position is updated.
  219. (set-window-start window current-start)))))
  220. ;; Scroll the window to the proper position for EVENT.
  221. (defun scroll-bar-drag-1 (event)
  222. (let* ((start-position (event-start event))
  223. (window (nth 0 start-position))
  224. (portion-whole (nth 2 start-position)))
  225. (save-excursion
  226. (with-current-buffer (window-buffer window)
  227. ;; Calculate position relative to the accessible part of the buffer.
  228. (goto-char (+ (point-min)
  229. (scroll-bar-scale portion-whole
  230. (- (point-max) (point-min)))))
  231. (vertical-motion 0 window)
  232. (set-window-start window (point))))))
  233. (defun scroll-bar-drag (event)
  234. "Scroll the window by dragging the scroll bar slider.
  235. If you click outside the slider, the window scrolls to bring the slider there."
  236. (interactive "e")
  237. (let* (done
  238. (echo-keystrokes 0)
  239. (end-position (event-end event))
  240. (window (nth 0 end-position))
  241. (before-scroll))
  242. (with-current-buffer (window-buffer window)
  243. (setq before-scroll point-before-scroll))
  244. (save-selected-window
  245. (select-window window 'mark-for-redisplay)
  246. (setq before-scroll
  247. (or before-scroll (point))))
  248. (scroll-bar-drag-1 event)
  249. (track-mouse
  250. (while (not done)
  251. (setq event (read-event))
  252. (if (eq (car-safe event) 'mouse-movement)
  253. (setq event (read-event)))
  254. (cond ((eq (car-safe event) 'scroll-bar-movement)
  255. (scroll-bar-drag-1 event))
  256. (t
  257. ;; Exit when we get the drag event; ignore that event.
  258. (setq done t)))))
  259. (sit-for 0)
  260. (with-current-buffer (window-buffer window)
  261. (setq point-before-scroll before-scroll))))
  262. ;; Scroll the window to the proper position for EVENT.
  263. (defun scroll-bar-horizontal-drag-1 (event)
  264. (let* ((start-position (event-start event))
  265. (window (nth 0 start-position))
  266. (portion-whole (nth 2 start-position))
  267. (unit (frame-char-width (window-frame window))))
  268. (if (eq (current-bidi-paragraph-direction (window-buffer window))
  269. 'left-to-right)
  270. (set-window-hscroll
  271. window (/ (+ (car portion-whole) (1- unit)) unit))
  272. (set-window-hscroll
  273. window (/ (+ (- (cdr portion-whole) (car portion-whole))
  274. (1- unit))
  275. unit)))))
  276. (defun scroll-bar-horizontal-drag (event)
  277. "Scroll the window horizontally by dragging the scroll bar slider.
  278. If you click outside the slider, the window scrolls to bring the slider there."
  279. (interactive "e")
  280. (let* (done
  281. (echo-keystrokes 0)
  282. (end-position (event-end event))
  283. (window (nth 0 end-position))
  284. (before-scroll))
  285. (with-current-buffer (window-buffer window)
  286. (setq before-scroll point-before-scroll))
  287. (save-selected-window
  288. (select-window window 'mark-for-redisplay)
  289. (setq before-scroll
  290. (or before-scroll (point))))
  291. (scroll-bar-horizontal-drag-1 event)
  292. (track-mouse
  293. (while (not done)
  294. (setq event (read-event))
  295. (if (eq (car-safe event) 'mouse-movement)
  296. (setq event (read-event)))
  297. (cond ((eq (car-safe event) 'scroll-bar-movement)
  298. (scroll-bar-horizontal-drag-1 event))
  299. (t
  300. ;; Exit when we get the drag event; ignore that event.
  301. (setq done t)))))
  302. (sit-for 0)
  303. (with-current-buffer (window-buffer window)
  304. (setq point-before-scroll before-scroll))))
  305. (defun scroll-bar-scroll-down (event)
  306. "Scroll the window's top line down to the location of the scroll bar click.
  307. EVENT should be a scroll bar click."
  308. (interactive "e")
  309. (let* ((end-position (event-end event))
  310. (window (nth 0 end-position))
  311. (before-scroll))
  312. (with-current-buffer (window-buffer window)
  313. (setq before-scroll point-before-scroll))
  314. (unwind-protect
  315. (save-selected-window
  316. (let ((portion-whole (nth 2 end-position)))
  317. (select-window window 'mark-for-redisplay)
  318. (setq before-scroll
  319. (or before-scroll (point)))
  320. (scroll-down
  321. (scroll-bar-scale portion-whole (1- (window-height)))))
  322. (sit-for 0))
  323. (with-current-buffer (window-buffer window)
  324. (setq point-before-scroll before-scroll)))))
  325. (defun scroll-bar-scroll-up (event)
  326. "Scroll the line next to the scroll bar click to the top of the window.
  327. EVENT should be a scroll bar click."
  328. (interactive "e")
  329. (let* ((end-position (event-end event))
  330. (window (nth 0 end-position))
  331. (before-scroll))
  332. (with-current-buffer (window-buffer window)
  333. (setq before-scroll point-before-scroll))
  334. (unwind-protect
  335. (save-selected-window
  336. (let ((portion-whole (nth 2 end-position)))
  337. (select-window window 'mark-for-redisplay)
  338. (setq before-scroll
  339. (or before-scroll (point)))
  340. (scroll-up
  341. (scroll-bar-scale portion-whole (1- (window-height)))))
  342. (sit-for 0))
  343. (with-current-buffer (window-buffer window)
  344. (setq point-before-scroll before-scroll)))))
  345. ;;; Tookit scroll bars.
  346. (defun scroll-bar-toolkit-scroll (event)
  347. "Handle event EVENT on vertical scroll bar."
  348. (interactive "e")
  349. (let* ((end-position (event-end event))
  350. (window (nth 0 end-position))
  351. (part (nth 4 end-position))
  352. before-scroll)
  353. (cond
  354. ((eq part 'end-scroll))
  355. (t
  356. (with-current-buffer (window-buffer window)
  357. (setq before-scroll point-before-scroll))
  358. (save-selected-window
  359. (select-window window 'mark-for-redisplay)
  360. (setq before-scroll (or before-scroll (point)))
  361. (cond
  362. ((eq part 'above-handle)
  363. (scroll-up '-))
  364. ((eq part 'below-handle)
  365. (scroll-up nil))
  366. ((eq part 'ratio)
  367. (let* ((portion-whole (nth 2 end-position))
  368. (lines (scroll-bar-scale portion-whole
  369. (1- (window-height)))))
  370. (scroll-up (cond ((not (zerop lines)) lines)
  371. ((< (car portion-whole) 0) -1)
  372. (t 1)))))
  373. ((eq part 'up)
  374. (scroll-up -1))
  375. ((eq part 'down)
  376. (scroll-up 1))
  377. ((eq part 'top)
  378. (set-window-start window (point-min)))
  379. ((eq part 'bottom)
  380. (goto-char (point-max))
  381. (recenter))
  382. ((eq part 'handle)
  383. (scroll-bar-drag-1 event))))
  384. (sit-for 0)
  385. (with-current-buffer (window-buffer window)
  386. (setq point-before-scroll before-scroll))))))
  387. (defun scroll-bar-toolkit-horizontal-scroll (event)
  388. "Handle event EVENT on horizontal scroll bar."
  389. (interactive "e")
  390. (let* ((end-position (event-end event))
  391. (window (nth 0 end-position))
  392. (part (nth 4 end-position))
  393. (bidi-factor
  394. (if (eq (current-bidi-paragraph-direction (window-buffer window))
  395. 'left-to-right)
  396. 1
  397. -1))
  398. before-scroll)
  399. (cond
  400. ((eq part 'end-scroll))
  401. (t
  402. (with-current-buffer (window-buffer window)
  403. (setq before-scroll point-before-scroll))
  404. (save-selected-window
  405. (select-window window 'mark-for-redisplay)
  406. (setq before-scroll (or before-scroll (point)))
  407. (cond
  408. ((eq part 'before-handle)
  409. (scroll-right (* bidi-factor 4)))
  410. ((eq part 'after-handle)
  411. (scroll-left (* bidi-factor 4)))
  412. ((eq part 'ratio)
  413. (let* ((portion-whole (nth 2 end-position))
  414. (columns (scroll-bar-scale portion-whole
  415. (1- (window-width)))))
  416. (scroll-right
  417. (* (cond
  418. ((not (zerop columns))
  419. columns)
  420. ((< (car portion-whole) 0) -1)
  421. (t 1))
  422. bidi-factor))))
  423. ((eq part 'left)
  424. (scroll-right (* bidi-factor 1)))
  425. ((eq part 'right)
  426. (scroll-left (* bidi-factor 1)))
  427. ((eq part 'leftmost)
  428. (goto-char (if (eq bidi-factor 1)
  429. (line-beginning-position)
  430. (line-end-position))))
  431. ((eq part 'rightmost)
  432. (goto-char (if (eq bidi-factor 1)
  433. (line-end-position)
  434. (line-beginning-position))))
  435. ((eq part 'horizontal-handle)
  436. (scroll-bar-horizontal-drag-1 event))))
  437. (sit-for 0)
  438. (with-current-buffer (window-buffer window)
  439. (setq point-before-scroll before-scroll))))))
  440. ;;;; Bindings.
  441. ;; For now, we'll set things up to work like xterm.
  442. (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
  443. (global-set-key [vertical-scroll-bar mouse-1]
  444. 'scroll-bar-toolkit-scroll)
  445. (global-set-key [horizontal-scroll-bar mouse-1]
  446. 'scroll-bar-toolkit-horizontal-scroll))
  447. (t
  448. (global-set-key [vertical-scroll-bar mouse-1]
  449. 'scroll-bar-scroll-up)
  450. (global-set-key [vertical-scroll-bar drag-mouse-1]
  451. 'scroll-bar-scroll-up)
  452. (global-set-key [vertical-scroll-bar down-mouse-2]
  453. 'scroll-bar-drag)
  454. (global-set-key [vertical-scroll-bar mouse-3]
  455. 'scroll-bar-scroll-down)
  456. (global-set-key [vertical-scroll-bar drag-mouse-3]
  457. 'scroll-bar-scroll-down)))
  458. (provide 'scroll-bar)
  459. ;;; scroll-bar.el ends here