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 | ||
6b61353c KH |
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 SM |
242 | :global t |
243 | (if mouse-sel-mode | |
244 | (progn | |
ad3f1e65 | 245 | (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook) |
640201f8 SM |
246 | (when mouse-sel-default-bindings |
247 | ;; Save original bindings and replace them with new ones. | |
248 | (setq mouse-sel-original-bindings | |
249 | (mapcar (lambda (binding) | |
250 | (let ((event (car binding))) | |
251 | (prog1 (cons event (lookup-key global-map event)) | |
252 | (global-set-key event (cdr binding))))) | |
253 | mouse-sel-bound-events)) | |
254 | ;; Update interprogram functions. | |
255 | (setq mouse-sel-original-interprogram-cut-function | |
256 | interprogram-cut-function | |
257 | mouse-sel-original-interprogram-paste-function | |
6b61353c KH |
258 | interprogram-paste-function |
259 | mouse-sel-has-been-enabled t) | |
640201f8 SM |
260 | (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste) |
261 | (setq interprogram-cut-function nil | |
262 | interprogram-paste-function nil)))) | |
ad3f1e65 | 263 | |
4bc65152 | 264 | ;; Restore original bindings |
640201f8 SM |
265 | (remove-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook) |
266 | (dolist (binding mouse-sel-original-bindings) | |
267 | (global-set-key (car binding) (cdr binding))) | |
6b61353c KH |
268 | ;; Restore the old values of these variables, |
269 | ;; only if they were actually saved previously. | |
270 | (if mouse-sel-has-been-enabled | |
271 | (setq interprogram-cut-function | |
272 | mouse-sel-original-interprogram-cut-function | |
273 | interprogram-paste-function | |
274 | mouse-sel-original-interprogram-paste-function)))) | |
1a2b6c52 | 275 | |
f2506826 RS |
276 | ;;=== Internal Variables/Constants ======================================== |
277 | ||
8a946354 | 278 | (defvar mouse-sel-primary-thing nil |
f2506826 RS |
279 | "Type of PRIMARY selection in current buffer.") |
280 | (make-variable-buffer-local 'mouse-sel-primary-thing) | |
281 | ||
8a946354 | 282 | (defvar mouse-sel-secondary-thing nil |
f2506826 RS |
283 | "Type of SECONDARY selection in current buffer.") |
284 | (make-variable-buffer-local 'mouse-sel-secondary-thing) | |
285 | ||
286 | ;; Ensure that secondary overlay is defined | |
ad3f1e65 | 287 | (unless (overlayp mouse-secondary-overlay) |
f2506826 RS |
288 | (setq mouse-secondary-overlay (make-overlay 1 1)) |
289 | (overlay-put mouse-secondary-overlay 'face 'secondary-selection)) | |
1a2b6c52 | 290 | |
f2506826 RS |
291 | (defconst mouse-sel-selection-alist |
292 | '((PRIMARY mouse-drag-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 | |
ad3f1e65 SM |
304 | (defvar mouse-sel-set-selection-function |
305 | (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) | |
306 | 'x-set-selection | |
307 | (lambda (selection value) | |
308 | (if (eq selection 'PRIMARY) | |
309 | (x-select-text value) | |
310 | (x-set-selection selection value)))) | |
1a2b6c52 | 311 | "Function to call to set selection. |
f2506826 RS |
312 | Called with two arguments: |
313 | ||
314 | SELECTION, the name of the selection concerned, and | |
0a6231f5 | 315 | VALUE, the text to store. |
ad3f1e65 SM |
316 | |
317 | This sets the selection as well as the cut buffer for the older applications, | |
318 | unless `mouse-sel-default-bindings' is `interprogram-cut-paste'.") | |
1a2b6c52 RS |
319 | |
320 | (defvar mouse-sel-get-selection-function | |
ad3f1e65 SM |
321 | (lambda (selection) |
322 | (if (eq selection 'PRIMARY) | |
497cfacf JPW |
323 | (or (x-cut-buffer-or-selection-value) |
324 | (bound-and-true-p x-last-selected-text) | |
325 | (bound-and-true-p x-last-selected-text-primary)) | |
ad3f1e65 | 326 | (x-get-selection selection))) |
1a2b6c52 | 327 | "Function to call to get the selection. |
f2506826 | 328 | Called with one argument: |
1a2b6c52 | 329 | |
f2506826 RS |
330 | SELECTION: the name of the selection concerned.") |
331 | ||
f2506826 RS |
332 | ;;=== Support/access functions ============================================ |
333 | ||
334 | (defun mouse-sel-determine-selection-thing (nclicks) | |
335 | "Determine what `thing' `mouse-sel' should operate on. | |
336 | The first argument is NCLICKS, is the number of consecutive | |
337 | mouse clicks at the same position. | |
338 | ||
339 | Double-clicking on word constituents selects words. | |
340 | Double-clicking on symbol constituents selects symbols. | |
341 | Double-clicking on quotes or parentheses selects sexps. | |
342 | Double-clicking on whitespace selects whitespace. | |
343 | Triple-clicking selects lines. | |
344 | Quad-clicking selects paragraphs. | |
345 | ||
346 | Feel free to re-define this function to support your own desired | |
347 | multi-click semantics." | |
1a2b6c52 | 348 | (let* ((next-char (char-after (point))) |
f2506826 | 349 | (char-syntax (if next-char (char-syntax next-char)))) |
8a946354 | 350 | (if mouse-sel-cycle-clicks |
f2506826 | 351 | (setq nclicks (1+ (% (1- nclicks) 4)))) |
1a2b6c52 RS |
352 | (cond |
353 | ((= nclicks 1) nil) | |
f2506826 RS |
354 | ((= nclicks 3) 'line) |
355 | ((>= nclicks 4) 'paragraph) | |
1a2b6c52 | 356 | ((memq char-syntax '(?\( ?\) ?\" ?')) 'sexp) |
69aac59d | 357 | ((memq next-char '(?\s ?\t ?\n)) 'whitespace) |
1a2b6c52 RS |
358 | ((eq char-syntax ?_) 'symbol) |
359 | ((eq char-syntax ?w) 'word)))) | |
360 | ||
f2506826 RS |
361 | (defun mouse-sel-set-selection (selection value) |
362 | "Set the specified SELECTION to VALUE." | |
363 | (if mouse-sel-set-selection-function | |
364 | (funcall mouse-sel-set-selection-function selection value) | |
365 | (put 'mouse-sel-internal-selection selection value))) | |
366 | ||
367 | (defun mouse-sel-get-selection (selection) | |
368 | "Get the value of the specified SELECTION." | |
369 | (if mouse-sel-get-selection-function | |
370 | (funcall mouse-sel-get-selection-function selection) | |
371 | (get 'mouse-sel-internal-selection selection))) | |
372 | ||
f2506826 RS |
373 | (defun mouse-sel-selection-overlay (selection) |
374 | "Return overlay corresponding to SELECTION." | |
375 | (let ((symbol (nth 1 (assoc selection mouse-sel-selection-alist)))) | |
376 | (or symbol (error "No overlay corresponding to %s selection" selection)) | |
377 | (symbol-value symbol))) | |
378 | ||
379 | (defun mouse-sel-selection-thing (selection) | |
380 | "Return overlay corresponding to SELECTION." | |
381 | (let ((symbol (nth 2 (assoc selection mouse-sel-selection-alist)))) | |
382 | (or symbol (error "No symbol corresponding to %s selection" selection)) | |
383 | symbol)) | |
384 | ||
385 | (defun mouse-sel-region-to-primary (orig-window) | |
386 | "Convert region to PRIMARY overlay and deactivate region. | |
8a946354 SS |
387 | Argument ORIG-WINDOW specifies the window the cursor was in when the |
388 | originating command was issued, and is used to determine whether the | |
f2506826 RS |
389 | region was visible or not." |
390 | (if transient-mark-mode | |
391 | (let ((overlay (mouse-sel-selection-overlay 'PRIMARY))) | |
392 | (cond | |
8a946354 SS |
393 | ((and mark-active |
394 | (or highlight-nonselected-windows | |
f2506826 RS |
395 | (eq orig-window (selected-window)))) |
396 | ;; Region was visible, so convert region to overlay | |
8a946354 | 397 | (move-overlay overlay (region-beginning) (region-end) |
f2506826 RS |
398 | (current-buffer))) |
399 | ((eq orig-window (selected-window)) | |
400 | ;; Point was visible, so set overlay at point | |
401 | (move-overlay overlay (point) (point) (current-buffer))) | |
402 | (t | |
403 | ;; Nothing was visible, so remove overlay | |
404 | (delete-overlay overlay))) | |
405 | (setq mark-active nil)))) | |
406 | ||
407 | (defun mouse-sel-primary-to-region (&optional direction) | |
408 | "Convert PRIMARY overlay to region. | |
409 | Optional argument DIRECTION specifies the mouse drag direction: a value of | |
410 | 1 indicates that the mouse was dragged left-to-right, otherwise it was | |
411 | dragged right-to-left." | |
412 | (let* ((overlay (mouse-sel-selection-overlay 'PRIMARY)) | |
413 | (start (overlay-start overlay)) | |
414 | (end (overlay-end overlay))) | |
415 | (if (eq start end) | |
416 | (progn | |
417 | (if start (goto-char start)) | |
418 | (deactivate-mark)) | |
419 | (if (and mouse-sel-leave-point-near-mouse (eq direction 1)) | |
420 | (progn | |
421 | (goto-char end) | |
422 | (push-mark start 'nomsg 'active)) | |
423 | (goto-char start) | |
424 | (push-mark end 'nomsg 'active))) | |
425 | (if transient-mark-mode (delete-overlay overlay)))) | |
426 | ||
427 | (defmacro mouse-sel-eval-at-event-end (event &rest forms) | |
428 | "Evaluate forms at mouse position. | |
429 | Move to the end position of EVENT, execute FORMS, and restore original | |
430 | point and window." | |
8a946354 SS |
431 | `(let ((posn (event-end ,event))) |
432 | (if posn (mouse-minibuffer-check ,event)) | |
433 | (if (and posn (not (windowp (posn-window posn)))) | |
434 | (error "Cursor not in text area of window")) | |
435 | (let (orig-window orig-point-marker) | |
436 | (setq orig-window (selected-window)) | |
437 | (if posn (select-window (posn-window posn))) | |
438 | (setq orig-point-marker (point-marker)) | |
439 | (if (and posn (numberp (posn-point posn))) | |
440 | (goto-char (posn-point posn))) | |
441 | (unwind-protect | |
442 | (progn | |
443 | ,@forms) | |
444 | (goto-char (marker-position orig-point-marker)) | |
445 | (move-marker orig-point-marker nil) | |
446 | (select-window orig-window))))) | |
f2506826 RS |
447 | |
448 | (put 'mouse-sel-eval-at-event-end 'lisp-indent-hook 1) | |
449 | ||
450 | ;;=== Select ============================================================== | |
451 | ||
452 | (defun mouse-select (event) | |
1a2b6c52 RS |
453 | "Set region/selection using the mouse. |
454 | ||
f2506826 | 455 | Click sets point & mark to click position. |
1a2b6c52 RS |
456 | Dragging extends region/selection. |
457 | ||
8a946354 | 458 | Multi-clicking selects word/lines/paragraphs, as determined by |
f2506826 | 459 | 'mouse-sel-determine-selection-thing. |
1a2b6c52 | 460 | |
f2506826 RS |
461 | Clicking mouse-2 while selecting copies selected text to the kill-ring. |
462 | Clicking mouse-1 or mouse-3 kills the selected text. | |
1a2b6c52 RS |
463 | |
464 | This should be bound to a down-mouse event." | |
f2506826 RS |
465 | (interactive "@e") |
466 | (let (direction) | |
467 | (unwind-protect | |
468 | (setq direction (mouse-select-internal 'PRIMARY event)) | |
469 | (mouse-sel-primary-to-region direction)))) | |
470 | ||
471 | (defun mouse-select-secondary (event) | |
ad3f1e65 | 472 | "Set secondary selection using the mouse. |
f2506826 RS |
473 | |
474 | Click sets the start of the secondary selection to click position. | |
475 | Dragging extends the secondary selection. | |
1a2b6c52 | 476 | |
8a946354 | 477 | Multi-clicking selects word/lines/paragraphs, as determined by |
f2506826 | 478 | 'mouse-sel-determine-selection-thing. |
1a2b6c52 | 479 | |
f2506826 RS |
480 | Clicking mouse-2 while selecting copies selected text to the kill-ring. |
481 | Clicking mouse-1 or mouse-3 kills the selected text. | |
1a2b6c52 RS |
482 | |
483 | This should be bound to a down-mouse event." | |
ad3f1e65 | 484 | (interactive "e") |
f2506826 RS |
485 | (mouse-select-internal 'SECONDARY event)) |
486 | ||
487 | (defun mouse-select-internal (selection event) | |
488 | "Set SELECTION using the mouse." | |
489 | (mouse-sel-eval-at-event-end event | |
490 | (let ((thing-symbol (mouse-sel-selection-thing selection)) | |
491 | (overlay (mouse-sel-selection-overlay selection))) | |
492 | (set thing-symbol | |
493 | (mouse-sel-determine-selection-thing (event-click-count event))) | |
494 | (let ((object-bounds (bounds-of-thing-at-point | |
495 | (symbol-value thing-symbol)))) | |
496 | (if object-bounds | |
497 | (progn | |
498 | (move-overlay overlay | |
499 | (car object-bounds) (cdr object-bounds) | |
500 | (current-buffer))) | |
501 | (move-overlay overlay (point) (point) (current-buffer))))) | |
502 | (mouse-extend-internal selection))) | |
503 | ||
504 | ;;=== Extend ============================================================== | |
505 | ||
506 | (defun mouse-extend (event) | |
507 | "Extend region/selection using the mouse." | |
1a2b6c52 | 508 | (interactive "e") |
f2506826 RS |
509 | (let ((orig-window (selected-window)) |
510 | direction) | |
511 | (select-window (posn-window (event-end event))) | |
1a2b6c52 | 512 | (unwind-protect |
f2506826 RS |
513 | (progn |
514 | (mouse-sel-region-to-primary orig-window) | |
515 | (setq direction (mouse-extend-internal 'PRIMARY event))) | |
516 | (mouse-sel-primary-to-region direction)))) | |
517 | ||
518 | (defun mouse-extend-secondary (event) | |
519 | "Extend secondary selection using the mouse." | |
520 | (interactive "e") | |
521 | (save-window-excursion | |
522 | (mouse-extend-internal 'SECONDARY event))) | |
523 | ||
524 | (defun mouse-extend-internal (selection &optional initial-event) | |
525 | "Extend specified SELECTION using the mouse. | |
526 | Track mouse-motion events, adjusting the SELECTION appropriately. | |
8a946354 SS |
527 | Optional argument INITIAL-EVENT specifies an initial down-mouse event to |
528 | process. | |
f2506826 RS |
529 | |
530 | See documentation for mouse-select-internal for more details." | |
531 | (mouse-sel-eval-at-event-end initial-event | |
8a946354 | 532 | (let ((orig-cursor-type |
f2506826 RS |
533 | (cdr (assoc 'cursor-type (frame-parameters (selected-frame)))))) |
534 | (unwind-protect | |
535 | ||
536 | (let* ((thing-symbol (mouse-sel-selection-thing selection)) | |
537 | (overlay (mouse-sel-selection-overlay selection)) | |
538 | (orig-window (selected-window)) | |
539 | (orig-window-frame (window-frame orig-window)) | |
540 | (top (nth 1 (window-edges orig-window))) | |
541 | (bottom (nth 3 (window-edges orig-window))) | |
542 | (mark-active nil) ; inhibit normal region highlight | |
543 | (echo-keystrokes 0) ; don't echo mouse events | |
544 | min max | |
545 | direction | |
546 | event) | |
547 | ||
548 | ;; Get current bounds of overlay | |
549 | (if (eq (overlay-buffer overlay) (current-buffer)) | |
550 | (setq min (overlay-start overlay) | |
551 | max (overlay-end overlay)) | |
552 | (setq min (point) | |
553 | max min) | |
554 | (set thing-symbol nil)) | |
8a946354 | 555 | |
f2506826 RS |
556 | |
557 | ;; Bar cursor | |
558 | (if (fboundp 'modify-frame-parameters) | |
559 | (modify-frame-parameters (selected-frame) | |
560 | '((cursor-type . bar)))) | |
8a946354 | 561 | |
f2506826 RS |
562 | ;; Handle dragging |
563 | (track-mouse | |
8a946354 | 564 | |
f2506826 RS |
565 | (while (if initial-event ; Use initial event |
566 | (prog1 | |
567 | (setq event initial-event) | |
568 | (setq initial-event nil)) | |
569 | (setq event (read-event)) | |
570 | (and (consp event) | |
571 | (memq (car event) '(mouse-movement switch-frame)))) | |
8a946354 | 572 | |
f2506826 RS |
573 | (let ((selection-thing (symbol-value thing-symbol)) |
574 | (end (event-end event))) | |
8a946354 | 575 | |
f2506826 | 576 | (cond |
8a946354 | 577 | |
f2506826 RS |
578 | ;; Ignore any movement outside the frame |
579 | ((eq (car-safe event) 'switch-frame) nil) | |
580 | ((and (posn-window end) | |
581 | (not (eq (let ((posn-w (posn-window end))) | |
582 | (if (windowp posn-w) | |
583 | (window-frame posn-w) | |
584 | posn-w)) | |
585 | (window-frame orig-window)))) nil) | |
8a946354 | 586 | |
f2506826 RS |
587 | ;; Different window, same frame |
588 | ((not (eq (posn-window end) orig-window)) | |
589 | (let ((end-row (cdr (cdr (mouse-position))))) | |
590 | (cond | |
591 | ((and end-row (not (bobp)) (< end-row top)) | |
592 | (mouse-scroll-subr orig-window (- end-row top) | |
593 | overlay max)) | |
594 | ((and end-row (not (eobp)) (>= end-row bottom)) | |
595 | (mouse-scroll-subr orig-window (1+ (- end-row bottom)) | |
596 | overlay min)) | |
597 | ))) | |
8a946354 | 598 | |
f2506826 RS |
599 | ;; On the mode line |
600 | ((eq (posn-point end) 'mode-line) | |
601 | (mouse-scroll-subr orig-window 1 overlay min)) | |
8a946354 | 602 | |
f2506826 RS |
603 | ;; In original window |
604 | (t (goto-char (posn-point end))) | |
8a946354 | 605 | |
f2506826 | 606 | ) |
8a946354 | 607 | |
f2506826 RS |
608 | ;; Determine direction of drag |
609 | (cond | |
610 | ((and (not direction) (not (eq min max))) | |
611 | (setq direction (if (< (point) (/ (+ min max) 2)) -1 1))) | |
612 | ((and (not (eq direction -1)) (<= (point) min)) | |
613 | (setq direction -1)) | |
614 | ((and (not (eq direction 1)) (>= (point) max)) | |
615 | (setq direction 1))) | |
8a946354 | 616 | |
f2506826 | 617 | (if (not selection-thing) nil |
8a946354 | 618 | |
f2506826 RS |
619 | ;; If dragging forward, goal is next character |
620 | (if (and (eq direction 1) (not (eobp))) (forward-char 1)) | |
8a946354 | 621 | |
f2506826 | 622 | ;; Move to start/end of selected thing |
67384793 | 623 | (let ((goal (point))) |
f2506826 RS |
624 | (goto-char (if (eq 1 direction) min max)) |
625 | (condition-case nil | |
626 | (progn | |
627 | (while (> (* direction (- goal (point))) 0) | |
f2506826 RS |
628 | (forward-thing selection-thing direction)) |
629 | (let ((end (point))) | |
630 | (forward-thing selection-thing (- direction)) | |
631 | (goto-char | |
632 | (if (> (* direction (- goal (point))) 0) | |
67384793 | 633 | end (point))))) |
f2506826 | 634 | (error)))) |
8a946354 | 635 | |
f2506826 RS |
636 | ;; Move overlay |
637 | (move-overlay overlay | |
638 | (if (eq 1 direction) min (point)) | |
639 | (if (eq -1 direction) max (point)) | |
640 | (current-buffer)) | |
8a946354 | 641 | |
f2506826 RS |
642 | ))) ; end track-mouse |
643 | ||
644 | ;; Finish up after dragging | |
645 | (let ((overlay-start (overlay-start overlay)) | |
646 | (overlay-end (overlay-end overlay))) | |
8a946354 | 647 | |
f2506826 RS |
648 | ;; Set selection |
649 | (if (not (eq overlay-start overlay-end)) | |
650 | (mouse-sel-set-selection | |
651 | selection | |
652 | (buffer-substring overlay-start overlay-end))) | |
8a946354 | 653 | |
f2506826 RS |
654 | ;; Handle copy/kill |
655 | (let (this-command) | |
656 | (cond | |
657 | ((eq (event-basic-type last-input-event) 'mouse-2) | |
658 | (copy-region-as-kill overlay-start overlay-end) | |
659 | (read-event) (read-event)) | |
660 | ((and (memq (event-basic-type last-input-event) | |
661 | '(mouse-1 mouse-3)) | |
662 | (memq 'down (event-modifiers last-input-event))) | |
663 | (kill-region overlay-start overlay-end) | |
664 | (move-overlay overlay overlay-start overlay-start) | |
665 | (read-event) (read-event)) | |
666 | ((and (eq (event-basic-type last-input-event) 'mouse-3) | |
667 | (memq 'double (event-modifiers last-input-event))) | |
668 | (kill-region overlay-start overlay-end) | |
669 | (move-overlay overlay overlay-start overlay-start))))) | |
670 | ||
671 | direction) | |
672 | ||
673 | ;; Restore cursor | |
674 | (if (fboundp 'modify-frame-parameters) | |
8a946354 | 675 | (modify-frame-parameters |
f2506826 | 676 | (selected-frame) (list (cons 'cursor-type orig-cursor-type)))) |
8a946354 | 677 | |
f2506826 RS |
678 | )))) |
679 | ||
680 | ;;=== Paste =============================================================== | |
681 | ||
640201f8 | 682 | (defun mouse-insert-selection (event arg) |
f2506826 RS |
683 | "Insert the contents of the PRIMARY selection at mouse click. |
684 | If `mouse-yank-at-point' is non-nil, insert at point instead." | |
640201f8 SM |
685 | (interactive "e\nP") |
686 | (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) | |
687 | (mouse-yank-at-click event arg) | |
688 | (mouse-insert-selection-internal 'PRIMARY event))) | |
f2506826 RS |
689 | |
690 | (defun mouse-insert-secondary (event) | |
691 | "Insert the contents of the SECONDARY selection at mouse click. | |
6b4dd332 | 692 | If `mouse-yank-at-point' is non-nil, insert at point instead." |
1a2b6c52 | 693 | (interactive "e") |
f2506826 RS |
694 | (mouse-insert-selection-internal 'SECONDARY event)) |
695 | ||
696 | (defun mouse-insert-selection-internal (selection event) | |
697 | "Insert the contents of the named SELECTION at mouse click. | |
698 | If `mouse-yank-at-point' is non-nil, insert at point instead." | |
8a946354 | 699 | (unless mouse-yank-at-point |
ad3f1e65 SM |
700 | (mouse-set-point event)) |
701 | (when mouse-sel-get-selection-function | |
702 | (push-mark (point) 'nomsg) | |
703 | (insert (or (funcall mouse-sel-get-selection-function selection) "")))) | |
f2506826 | 704 | |
67384793 RS |
705 | ;;=== Handle loss of selections =========================================== |
706 | ||
707 | (defun mouse-sel-lost-selection-hook (selection) | |
708 | "Remove the overlay for a lost selection." | |
7b990caf | 709 | (let ((overlay (mouse-sel-selection-overlay selection))) |
67384793 RS |
710 | (delete-overlay overlay))) |
711 | ||
ad3f1e65 | 712 | (provide 'mouse-sel) |
f2506826 | 713 | |
6b61353c | 714 | ;;; arch-tag: 86e6c73f-deaa-48d3-a24e-c565fda1f7d7 |
e8af40ee | 715 | ;;; mouse-sel.el ends here |