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