mouse-sel.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. ;;; mouse-sel.el --- multi-click selection support
  2. ;; Copyright (C) 1993-1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Mike Williams <mdub@bigfoot.com>
  4. ;; Keywords: mouse
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This module provides multi-click mouse support for GNU Emacs versions
  18. ;; 19.18 and later. I've tried to make it behave more like standard X
  19. ;; clients (eg. xterm) than the default Emacs 19 mouse selection handlers.
  20. ;; Basically:
  21. ;;
  22. ;; * Clicking mouse-1 starts (cancels) selection, dragging extends it.
  23. ;;
  24. ;; * Clicking or dragging mouse-3 extends the selection as well.
  25. ;;
  26. ;; * Double-clicking on word constituents selects words.
  27. ;; Double-clicking on symbol constituents selects symbols.
  28. ;; Double-clicking on quotes or parentheses selects sexps.
  29. ;; Double-clicking on whitespace selects whitespace.
  30. ;; Triple-clicking selects lines.
  31. ;; Quad-clicking selects paragraphs.
  32. ;;
  33. ;; * Selecting sets the region & X primary selection, but does NOT affect
  34. ;; the kill-ring. Because the mouse handlers set the primary selection
  35. ;; directly, mouse-sel sets the variables interprogram-cut-function
  36. ;; and interprogram-paste-function to nil.
  37. ;;
  38. ;; * Clicking mouse-2 inserts the contents of the primary selection at
  39. ;; the mouse position (or point, if mouse-yank-at-point is non-nil).
  40. ;;
  41. ;; * Pressing mouse-2 while selecting or extending copies selection
  42. ;; to the kill ring. Pressing mouse-1 or mouse-3 kills it.
  43. ;;
  44. ;; * Double-clicking mouse-3 also kills selection.
  45. ;;
  46. ;; * M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2
  47. ;; & mouse-3, but operate on the X secondary selection rather than the
  48. ;; primary selection and region.
  49. ;;
  50. ;; This module requires my thingatpt.el module, which it uses to find the
  51. ;; bounds of words, lines, sexps, etc.
  52. ;;
  53. ;; Thanks to KevinB@bartley.demon.co.uk for his useful input.
  54. ;;
  55. ;;--- Customization -------------------------------------------------------
  56. ;;
  57. ;; * You may want to use none or more of following:
  58. ;;
  59. ;; ;; Enable region highlight
  60. ;; (transient-mark-mode 1)
  61. ;;
  62. ;; ;; But only in the selected window
  63. ;; (setq highlight-nonselected-windows nil)
  64. ;;
  65. ;; ;; Enable pending-delete
  66. ;; (delete-selection-mode 1)
  67. ;;
  68. ;; * You can control the way mouse-sel binds its keys by setting the value
  69. ;; of mouse-sel-default-bindings before loading mouse-sel.
  70. ;;
  71. ;; (a) If mouse-sel-default-bindings = t (the default)
  72. ;;
  73. ;; Mouse sets and insert selection
  74. ;; mouse-1 mouse-select
  75. ;; mouse-2 mouse-insert-selection
  76. ;; mouse-3 mouse-extend
  77. ;;
  78. ;; Selection/kill-ring interaction is disabled
  79. ;; interprogram-cut-function = nil
  80. ;; interprogram-paste-function = nil
  81. ;;
  82. ;; (b) If mouse-sel-default-bindings = 'interprogram-cut-paste
  83. ;;
  84. ;; Mouse sets selection, and pastes from kill-ring
  85. ;; mouse-1 mouse-select
  86. ;; mouse-2 mouse-insert-selection
  87. ;; mouse-3 mouse-extend
  88. ;; In this mode, mouse-insert-selection just calls mouse-yank-at-click.
  89. ;;
  90. ;; Selection/kill-ring interaction is retained
  91. ;; interprogram-cut-function = x-select-text
  92. ;; interprogram-paste-function = x-selection-value
  93. ;;
  94. ;; What you lose is the ability to select some text in
  95. ;; delete-selection-mode and yank over the top of it.
  96. ;;
  97. ;; (c) If mouse-sel-default-bindings = nil, no bindings are made.
  98. ;;
  99. ;; * By default, mouse-insert-selection (mouse-2) inserts the selection at
  100. ;; the mouse position. You can tell it to insert at point instead with:
  101. ;;
  102. ;; (setq mouse-yank-at-point t)
  103. ;;
  104. ;; * I like to leave point at the end of the region nearest to where the
  105. ;; mouse was, even though this makes region highlighting mis-leading (the
  106. ;; cursor makes it look like one extra character is selected). You can
  107. ;; disable this behavior with:
  108. ;;
  109. ;; (setq mouse-sel-leave-point-near-mouse nil)
  110. ;;
  111. ;; * By default, mouse-select cycles the click count after 4 clicks. That
  112. ;; is, clicking mouse-1 five times has the same effect as clicking it
  113. ;; once, clicking six times has the same effect as clicking twice, etc.
  114. ;; Disable this behavior with:
  115. ;;
  116. ;; (setq mouse-sel-cycle-clicks nil)
  117. ;;
  118. ;; * The variables mouse-sel-{set,get}-selection-function control how the
  119. ;; selection is handled. Under X Windows, these variables default so
  120. ;; that the X primary selection is used. Under other windowing systems,
  121. ;; alternate functions are used, which simply store the selection value
  122. ;; in a variable.
  123. ;;; Code:
  124. (require 'mouse)
  125. (require 'thingatpt)
  126. (eval-when-compile
  127. (require 'cl))
  128. ;;=== User Variables ======================================================
  129. (defgroup mouse-sel nil
  130. "Mouse selection enhancement."
  131. :group 'mouse)
  132. (defcustom mouse-sel-leave-point-near-mouse t
  133. "Leave point near last mouse position.
  134. If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end
  135. of the region nearest to where the mouse last was.
  136. If nil, point will always be placed at the beginning of the region."
  137. :type 'boolean
  138. :group 'mouse-sel)
  139. (defcustom mouse-sel-cycle-clicks t
  140. "If non-nil, \\[mouse-select] cycles the click-counts after 4 clicks."
  141. :type 'boolean
  142. :group 'mouse-sel)
  143. (defcustom mouse-sel-default-bindings t
  144. "Control mouse bindings."
  145. :type '(choice (const :tag "none" nil)
  146. (const :tag "cut and paste" interprogram-cut-paste)
  147. (other :tag "default bindings" t))
  148. :group 'mouse-sel)
  149. ;;=== Key bindings ========================================================
  150. (defconst mouse-sel-bound-events
  151. '(;; Primary selection bindings.
  152. ;;
  153. ;; Bind keys to `ignore' instead of unsetting them because modes may
  154. ;; bind `down-mouse-1', for instance, without binding `mouse-1'.
  155. ;; If we unset `mouse-1', this leads to a bitch_at_user when the
  156. ;; mouse goes up because no matching binding is found for that.
  157. ([mouse-1] . ignore)
  158. ([drag-mouse-1] . ignore)
  159. ([mouse-3] . ignore)
  160. ([down-mouse-1] . mouse-select)
  161. ([down-mouse-3] . mouse-extend)
  162. ([mouse-2] . mouse-insert-selection)
  163. ;; Secondary selection bindings.
  164. ([M-mouse-1] . ignore)
  165. ([M-drag-mouse-1] . ignore)
  166. ([M-mouse-3] . ignore)
  167. ([M-down-mouse-1] . mouse-select-secondary)
  168. ([M-mouse-2] . mouse-insert-secondary)
  169. ([M-down-mouse-3] . mouse-extend-secondary))
  170. "An alist of events that `mouse-sel-mode' binds.")
  171. ;;=== User Command ========================================================
  172. (defvar mouse-sel-has-been-enabled nil
  173. "Non-nil if Mouse Sel mode has been enabled at least once.")
  174. (defvar mouse-sel-original-bindings nil)
  175. (defvar mouse-sel-original-interprogram-cut-function nil)
  176. (defvar mouse-sel-original-interprogram-paste-function nil)
  177. ;;;###autoload
  178. (define-minor-mode mouse-sel-mode
  179. "Toggle Mouse Sel mode.
  180. With a prefix argument ARG, enable Mouse Sel mode if ARG is
  181. positive, and disable it otherwise. If called from Lisp, enable
  182. the mode if ARG is omitted or nil.
  183. Mouse Sel mode is a global minor mode. When enabled, mouse
  184. selection is enhanced in various ways:
  185. - Double-clicking on symbol constituents selects symbols.
  186. Double-clicking on quotes or parentheses selects sexps.
  187. Double-clicking on whitespace selects whitespace.
  188. Triple-clicking selects lines.
  189. Quad-clicking selects paragraphs.
  190. - Selecting sets the region & X primary selection, but does NOT affect
  191. the `kill-ring', nor do the kill-ring functions change the X selection.
  192. Because the mouse handlers set the primary selection directly,
  193. mouse-sel sets the variables `interprogram-cut-function' and
  194. `interprogram-paste-function' to nil.
  195. - Clicking mouse-2 inserts the contents of the primary selection at
  196. the mouse position (or point, if `mouse-yank-at-point' is non-nil).
  197. - mouse-2 while selecting or extending copies selection to the
  198. kill ring; mouse-1 or mouse-3 kills it."
  199. :global t
  200. :group 'mouse-sel
  201. (if mouse-sel-mode
  202. (progn
  203. ;; If mouse-2 has never been done by the user, initialize the
  204. ;; `event-kind' property to ensure that `follow-link' clicks
  205. ;; are interpreted correctly.
  206. (put 'mouse-2 'event-kind 'mouse-click)
  207. (add-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
  208. (when mouse-sel-default-bindings
  209. ;; Save original bindings and replace them with new ones.
  210. (setq mouse-sel-original-bindings
  211. (mapcar (lambda (binding)
  212. (let ((event (car binding)))
  213. (prog1 (cons event (lookup-key global-map event))
  214. (global-set-key event (cdr binding)))))
  215. mouse-sel-bound-events))
  216. ;; Update interprogram functions.
  217. (setq mouse-sel-original-interprogram-cut-function
  218. interprogram-cut-function
  219. mouse-sel-original-interprogram-paste-function
  220. interprogram-paste-function
  221. mouse-sel-has-been-enabled t)
  222. (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste)
  223. (setq interprogram-cut-function nil
  224. interprogram-paste-function nil))))
  225. ;; Restore original bindings
  226. (remove-hook 'x-lost-selection-functions 'mouse-sel-lost-selection-hook)
  227. (dolist (binding mouse-sel-original-bindings)
  228. (global-set-key (car binding) (cdr binding)))
  229. ;; Restore the old values of these variables,
  230. ;; only if they were actually saved previously.
  231. (if mouse-sel-has-been-enabled
  232. (setq interprogram-cut-function
  233. mouse-sel-original-interprogram-cut-function
  234. interprogram-paste-function
  235. mouse-sel-original-interprogram-paste-function))))
  236. ;;=== Internal Variables/Constants ========================================
  237. (defvar mouse-sel-primary-thing nil
  238. "Type of PRIMARY selection in current buffer.")
  239. (make-variable-buffer-local 'mouse-sel-primary-thing)
  240. (defvar mouse-sel-secondary-thing nil
  241. "Type of SECONDARY selection in current buffer.")
  242. (make-variable-buffer-local 'mouse-sel-secondary-thing)
  243. ;; Ensure that secondary overlay is defined
  244. (unless (overlayp mouse-secondary-overlay)
  245. (setq mouse-secondary-overlay (make-overlay 1 1))
  246. (overlay-put mouse-secondary-overlay 'face 'secondary-selection))
  247. (defconst mouse-sel-primary-overlay
  248. (let ((ol (make-overlay (point-min) (point-min))))
  249. (delete-overlay ol)
  250. (overlay-put ol 'face 'region)
  251. ol)
  252. "An overlay which records the current primary selection.
  253. This is used by Mouse Sel mode only.")
  254. (defconst mouse-sel-selection-alist
  255. '((PRIMARY mouse-sel-primary-overlay mouse-sel-primary-thing)
  256. (SECONDARY mouse-secondary-overlay mouse-sel-secondary-thing))
  257. "Alist associating selections with variables.
  258. Each element is of the form:
  259. (SELECTION-NAME OVERLAY-SYMBOL SELECTION-THING-SYMBOL)
  260. where SELECTION-NAME = name of selection
  261. OVERLAY-SYMBOL = name of variable containing overlay to use
  262. SELECTION-THING-SYMBOL = name of variable where the current selection
  263. type for this selection should be stored.")
  264. (declare-function x-select-text "term/common-win" (text))
  265. (defvar mouse-sel-set-selection-function
  266. (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
  267. 'x-set-selection
  268. (lambda (selection value)
  269. (if (eq selection 'PRIMARY)
  270. (x-select-text value)
  271. (x-set-selection selection value))))
  272. "Function to call to set selection.
  273. Called with two arguments:
  274. SELECTION, the name of the selection concerned, and
  275. VALUE, the text to store.
  276. This sets the selection, unless `mouse-sel-default-bindings'
  277. is `interprogram-cut-paste'.")
  278. (declare-function x-selection-value "term/x-win" ())
  279. (defvar mouse-sel-get-selection-function
  280. (lambda (selection)
  281. (if (eq selection 'PRIMARY)
  282. (or (x-selection-value)
  283. (bound-and-true-p x-last-selected-text)
  284. (bound-and-true-p x-last-selected-text-primary))
  285. (x-get-selection selection)))
  286. "Function to call to get the selection.
  287. Called with one argument:
  288. SELECTION: the name of the selection concerned.")
  289. ;;=== Support/access functions ============================================
  290. (defun mouse-sel-determine-selection-thing (nclicks)
  291. "Determine what `thing' `mouse-sel' should operate on.
  292. The first argument is NCLICKS, is the number of consecutive
  293. mouse clicks at the same position.
  294. Double-clicking on word constituents selects words.
  295. Double-clicking on symbol constituents selects symbols.
  296. Double-clicking on quotes or parentheses selects sexps.
  297. Double-clicking on whitespace selects whitespace.
  298. Triple-clicking selects lines.
  299. Quad-clicking selects paragraphs.
  300. Feel free to re-define this function to support your own desired
  301. multi-click semantics."
  302. (let* ((next-char (char-after (point)))
  303. (char-syntax (if next-char (char-syntax next-char))))
  304. (if mouse-sel-cycle-clicks
  305. (setq nclicks (1+ (% (1- nclicks) 4))))
  306. (cond
  307. ((= nclicks 1) nil)
  308. ((= nclicks 3) 'line)
  309. ((>= nclicks 4) 'paragraph)
  310. ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp)
  311. ((memq next-char '(?\s ?\t ?\n)) 'whitespace)
  312. ((eq char-syntax ?_) 'symbol)
  313. ((eq char-syntax ?w) 'word))))
  314. (defun mouse-sel-set-selection (selection value)
  315. "Set the specified SELECTION to VALUE."
  316. (if mouse-sel-set-selection-function
  317. (funcall mouse-sel-set-selection-function selection value)
  318. (put 'mouse-sel-internal-selection selection value)))
  319. (defun mouse-sel-get-selection (selection)
  320. "Get the value of the specified SELECTION."
  321. (if mouse-sel-get-selection-function
  322. (funcall mouse-sel-get-selection-function selection)
  323. (get 'mouse-sel-internal-selection selection)))
  324. (defun mouse-sel-selection-overlay (selection)
  325. "Return overlay corresponding to SELECTION."
  326. (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist))))
  327. (or symbol (error "No overlay corresponding to %s selection" selection))
  328. (symbol-value symbol)))
  329. (defun mouse-sel-selection-thing (selection)
  330. "Return overlay corresponding to SELECTION."
  331. (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist))))
  332. (or symbol (error "No symbol corresponding to %s selection" selection))
  333. symbol))
  334. (defun mouse-sel-region-to-primary (orig-window)
  335. "Convert region to PRIMARY overlay and deactivate region.
  336. Argument ORIG-WINDOW specifies the window the cursor was in when the
  337. originating command was issued, and is used to determine whether the
  338. region was visible or not."
  339. (if transient-mark-mode
  340. (let ((overlay (mouse-sel-selection-overlay 'PRIMARY)))
  341. (cond
  342. ((and mark-active
  343. (or highlight-nonselected-windows
  344. (eq orig-window (selected-window))))
  345. ;; Region was visible, so convert region to overlay
  346. (move-overlay overlay (region-beginning) (region-end)
  347. (current-buffer)))
  348. ((eq orig-window (selected-window))
  349. ;; Point was visible, so set overlay at point
  350. (move-overlay overlay (point) (point) (current-buffer)))
  351. (t
  352. ;; Nothing was visible, so remove overlay
  353. (delete-overlay overlay)))
  354. (setq mark-active nil))))
  355. (defun mouse-sel-primary-to-region (&optional direction)
  356. "Convert PRIMARY overlay to region.
  357. Optional argument DIRECTION specifies the mouse drag direction: a value of
  358. 1 indicates that the mouse was dragged left-to-right, otherwise it was
  359. dragged right-to-left."
  360. (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY))
  361. (start (overlay-start overlay))
  362. (end (overlay-end overlay)))
  363. (if (eq start end)
  364. (progn
  365. (if start (goto-char start))
  366. (deactivate-mark))
  367. (if (and mouse-sel-leave-point-near-mouse (eq direction 1))
  368. (progn
  369. (goto-char end)
  370. (push-mark start 'nomsg 'active))
  371. (goto-char start)
  372. (push-mark end 'nomsg 'active)))
  373. (if transient-mark-mode (delete-overlay overlay))))
  374. (defmacro mouse-sel-eval-at-event-end (event &rest forms)
  375. "Evaluate forms at mouse position.
  376. Move to the end position of EVENT, execute FORMS, and restore original
  377. point and window."
  378. `(let ((posn (event-end ,event)))
  379. (if posn (mouse-minibuffer-check ,event))
  380. (if (and posn (not (windowp (posn-window posn))))
  381. (error "Cursor not in text area of window"))
  382. (let (orig-window orig-point-marker)
  383. (setq orig-window (selected-window))
  384. (if posn (select-window (posn-window posn)))
  385. (setq orig-point-marker (point-marker))
  386. (if (and posn (numberp (posn-point posn)))
  387. (goto-char (posn-point posn)))
  388. (unwind-protect
  389. (progn
  390. ,@forms)
  391. (goto-char (marker-position orig-point-marker))
  392. (move-marker orig-point-marker nil)
  393. (select-window orig-window)))))
  394. (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1)
  395. ;;=== Select ==============================================================
  396. (defun mouse-select (event)
  397. "Set region/selection using the mouse.
  398. Click sets point & mark to click position.
  399. Dragging extends region/selection.
  400. Multi-clicking selects word/lines/paragraphs, as determined by
  401. 'mouse-sel-determine-selection-thing.
  402. Clicking mouse-2 while selecting copies selected text to the kill-ring.
  403. Clicking mouse-1 or mouse-3 kills the selected text.
  404. This should be bound to a down-mouse event."
  405. (interactive "@e")
  406. (let (select)
  407. (unwind-protect
  408. (setq select (mouse-select-internal 'PRIMARY event))
  409. (if (and select (listp select))
  410. (push (cons 'mouse-2 (cdr event)) unread-command-events)
  411. (mouse-sel-primary-to-region select)))))
  412. (defun mouse-select-secondary (event)
  413. "Set secondary selection using the mouse.
  414. Click sets the start of the secondary selection to click position.
  415. Dragging extends the secondary selection.
  416. Multi-clicking selects word/lines/paragraphs, as determined by
  417. 'mouse-sel-determine-selection-thing.
  418. Clicking mouse-2 while selecting copies selected text to the kill-ring.
  419. Clicking mouse-1 or mouse-3 kills the selected text.
  420. This should be bound to a down-mouse event."
  421. (interactive "e")
  422. (mouse-select-internal 'SECONDARY event))
  423. (defun mouse-select-internal (selection event)
  424. "Set SELECTION using the mouse, with EVENT as the initial down-event.
  425. Normally, this returns the direction in which the selection was
  426. made: a value of 1 indicates that the mouse was dragged
  427. left-to-right, otherwise it was dragged right-to-left.
  428. However, if `mouse-1-click-follows-link' is non-nil and the
  429. subsequent mouse events specify following a link, this returns
  430. the final mouse-event. In that case, the selection is not set."
  431. (mouse-sel-eval-at-event-end event
  432. (let ((thing-symbol (mouse-sel-selection-thing selection))
  433. (overlay (mouse-sel-selection-overlay selection)))
  434. (set thing-symbol
  435. (mouse-sel-determine-selection-thing (event-click-count event)))
  436. (let ((object-bounds (bounds-of-thing-at-point
  437. (symbol-value thing-symbol))))
  438. (if object-bounds
  439. (progn
  440. (move-overlay overlay
  441. (car object-bounds) (cdr object-bounds)
  442. (current-buffer)))
  443. (move-overlay overlay (point) (point) (current-buffer)))))
  444. (catch 'follow-link
  445. (mouse-extend-internal selection event t))))
  446. ;;=== Extend ==============================================================
  447. (defun mouse-extend (event)
  448. "Extend region/selection using the mouse."
  449. (interactive "e")
  450. (let ((orig-window (selected-window))
  451. direction)
  452. (select-window (posn-window (event-end event)))
  453. (unwind-protect
  454. (progn
  455. (mouse-sel-region-to-primary orig-window)
  456. (setq direction (mouse-extend-internal 'PRIMARY event)))
  457. (mouse-sel-primary-to-region direction))))
  458. (defun mouse-extend-secondary (event)
  459. "Extend secondary selection using the mouse."
  460. (interactive "e")
  461. (save-window-excursion
  462. (mouse-extend-internal 'SECONDARY event)))
  463. (defun mouse-extend-internal (selection &optional initial-event no-process)
  464. "Extend specified SELECTION using the mouse.
  465. Track mouse-motion events, adjusting the SELECTION appropriately.
  466. Optional argument INITIAL-EVENT specifies an initial down-mouse event.
  467. Optional argument NO-PROCESS means not to process the initial
  468. event.
  469. See documentation for mouse-select-internal for more details."
  470. (mouse-sel-eval-at-event-end initial-event
  471. (let ((orig-cursor-type
  472. (cdr (assoc 'cursor-type (frame-parameters (selected-frame))))))
  473. (unwind-protect
  474. (let* ((thing-symbol (mouse-sel-selection-thing selection))
  475. (overlay (mouse-sel-selection-overlay selection))
  476. (orig-window (selected-window))
  477. (top (nth 1 (window-edges orig-window)))
  478. (bottom (nth 3 (window-edges orig-window)))
  479. (mark-active nil) ; inhibit normal region highlight
  480. (echo-keystrokes 0) ; don't echo mouse events
  481. min max
  482. direction
  483. event)
  484. ;; Get current bounds of overlay
  485. (if (eq (overlay-buffer overlay) (current-buffer))
  486. (setq min (overlay-start overlay)
  487. max (overlay-end overlay))
  488. (setq min (point)
  489. max min)
  490. (set thing-symbol nil))
  491. ;; Bar cursor
  492. (if (fboundp 'modify-frame-parameters)
  493. (modify-frame-parameters (selected-frame)
  494. '((cursor-type . bar))))
  495. ;; Handle dragging
  496. (track-mouse
  497. (while (if (and initial-event (not no-process))
  498. ;; Use initial event
  499. (prog1
  500. (setq event initial-event)
  501. (setq initial-event nil))
  502. (setq event (read-event))
  503. (and (consp event)
  504. (memq (car event) '(mouse-movement switch-frame))))
  505. (let ((selection-thing (symbol-value thing-symbol))
  506. (end (event-end event)))
  507. (cond
  508. ;; Ignore any movement outside the frame
  509. ((eq (car-safe event) 'switch-frame) nil)
  510. ((and (posn-window end)
  511. (not (eq (let ((posn-w (posn-window end)))
  512. (if (windowp posn-w)
  513. (window-frame posn-w)
  514. posn-w))
  515. (window-frame orig-window)))) nil)
  516. ;; Different window, same frame
  517. ((not (eq (posn-window end) orig-window))
  518. (let ((end-row (cdr (cdr (mouse-position)))))
  519. (cond
  520. ((and end-row (not (bobp)) (< end-row top))
  521. (mouse-scroll-subr orig-window (- end-row top)
  522. overlay max))
  523. ((and end-row (not (eobp)) (>= end-row bottom))
  524. (mouse-scroll-subr orig-window (1+ (- end-row bottom))
  525. overlay min))
  526. )))
  527. ;; On the mode line
  528. ((eq (posn-point end) 'mode-line)
  529. (mouse-scroll-subr orig-window 1 overlay min))
  530. ;; In original window
  531. (t (goto-char (posn-point end)))
  532. )
  533. ;; Determine direction of drag
  534. (cond
  535. ((and (not direction) (not (eq min max)))
  536. (setq direction (if (< (point) (/ (+ min max) 2)) -1 1)))
  537. ((and (not (eq direction -1)) (<= (point) min))
  538. (setq direction -1))
  539. ((and (not (eq direction 1)) (>= (point) max))
  540. (setq direction 1)))
  541. (if (not selection-thing) nil
  542. ;; If dragging forward, goal is next character
  543. (if (and (eq direction 1) (not (eobp))) (forward-char 1))
  544. ;; Move to start/end of selected thing
  545. (let ((goal (point)))
  546. (goto-char (if (eq 1 direction) min max))
  547. (condition-case nil
  548. (progn
  549. (while (> (* direction (- goal (point))) 0)
  550. (forward-thing selection-thing direction))
  551. (let ((end (point)))
  552. (forward-thing selection-thing (- direction))
  553. (goto-char
  554. (if (> (* direction (- goal (point))) 0)
  555. end (point)))))
  556. (error))))
  557. ;; Move overlay
  558. (move-overlay overlay
  559. (if (eq 1 direction) min (point))
  560. (if (eq -1 direction) max (point))
  561. (current-buffer))
  562. ))) ; end track-mouse
  563. ;; Detect follow-link events
  564. (when (mouse-sel-follow-link-p initial-event event)
  565. (throw 'follow-link event))
  566. ;; Finish up after dragging
  567. (let ((overlay-start (overlay-start overlay))
  568. (overlay-end (overlay-end overlay)))
  569. ;; Set selection
  570. (if (not (eq overlay-start overlay-end))
  571. (mouse-sel-set-selection
  572. selection
  573. (buffer-substring overlay-start overlay-end)))
  574. ;; Handle copy/kill
  575. (let (this-command)
  576. (cond
  577. ((eq (event-basic-type last-input-event) 'mouse-2)
  578. (copy-region-as-kill overlay-start overlay-end)
  579. (read-event) (read-event))
  580. ((and (memq (event-basic-type last-input-event)
  581. '(mouse-1 mouse-3))
  582. (memq 'down (event-modifiers last-input-event)))
  583. (kill-region overlay-start overlay-end)
  584. (move-overlay overlay overlay-start overlay-start)
  585. (read-event) (read-event))
  586. ((and (eq (event-basic-type last-input-event) 'mouse-3)
  587. (memq 'double (event-modifiers last-input-event)))
  588. (kill-region overlay-start overlay-end)
  589. (move-overlay overlay overlay-start overlay-start)))))
  590. direction)
  591. ;; Restore cursor
  592. (if (fboundp 'modify-frame-parameters)
  593. (modify-frame-parameters
  594. (selected-frame) (list (cons 'cursor-type orig-cursor-type))))
  595. ))))
  596. (defun mouse-sel-follow-link-p (initial final)
  597. "Return t if we should follow a link, given INITIAL and FINAL mouse events.
  598. See `mouse-1-click-follows-link' for details. Currently, Mouse
  599. Sel mode does not support using a `double' value to follow links
  600. using double-clicks."
  601. (and initial final mouse-1-click-follows-link
  602. (eq (car initial) 'down-mouse-1)
  603. (mouse-on-link-p (event-start initial))
  604. (= (posn-point (event-start initial))
  605. (posn-point (event-end final)))
  606. (= (event-click-count initial) 1)
  607. (or (not (integerp mouse-1-click-follows-link))
  608. (let ((t0 (posn-timestamp (event-start initial)))
  609. (t1 (posn-timestamp (event-end final))))
  610. (and (integerp t0) (integerp t1)
  611. (if (> mouse-1-click-follows-link 0)
  612. (<= (- t1 t0) mouse-1-click-follows-link)
  613. (< (- t0 t1) mouse-1-click-follows-link)))))))
  614. ;;=== Paste ===============================================================
  615. (defun mouse-insert-selection (event arg)
  616. "Insert the contents of the PRIMARY selection at mouse click.
  617. If `mouse-yank-at-point' is non-nil, insert at point instead."
  618. (interactive "e\nP")
  619. (if (eq mouse-sel-default-bindings 'interprogram-cut-paste)
  620. (mouse-yank-at-click event arg)
  621. (mouse-insert-selection-internal 'PRIMARY event)))
  622. (defun mouse-insert-secondary (event)
  623. "Insert the contents of the SECONDARY selection at mouse click.
  624. If `mouse-yank-at-point' is non-nil, insert at point instead."
  625. (interactive "e")
  626. (mouse-insert-selection-internal 'SECONDARY event))
  627. (defun mouse-insert-selection-internal (selection event)
  628. "Insert the contents of the named SELECTION at mouse click.
  629. If `mouse-yank-at-point' is non-nil, insert at point instead."
  630. (unless mouse-yank-at-point
  631. (mouse-set-point event))
  632. (when mouse-sel-get-selection-function
  633. (push-mark (point) 'nomsg)
  634. (insert-for-yank
  635. (or (funcall mouse-sel-get-selection-function selection) ""))))
  636. ;;=== Handle loss of selections ===========================================
  637. (defun mouse-sel-lost-selection-hook (selection)
  638. "Remove the overlay for a lost selection."
  639. (let ((overlay (mouse-sel-selection-overlay selection)))
  640. (delete-overlay overlay)))
  641. (provide 'mouse-sel)
  642. ;;; mouse-sel.el ends here