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