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