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