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