Commit | Line | Data |
---|---|---|
6594deb0 | 1 | ;;; mouse.el --- window system-independent mouse support. |
84176303 | 2 | |
f8c25f1b | 3 | ;;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. |
eea8d4ef | 4 | |
84176303 | 5 | ;; Maintainer: FSF |
84176303 ER |
6 | ;; Keywords: hardware |
7 | ||
cc0a8174 | 8 | ;;; This file is part of GNU Emacs. |
72ea54a4 | 9 | |
cc0a8174 JB |
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 | |
87ef29fd | 12 | ;;; the Free Software Foundation; either version 2, or (at your option) |
cc0a8174 | 13 | ;;; any later version. |
72ea54a4 | 14 | |
cc0a8174 JB |
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. | |
72ea54a4 | 19 | |
cc0a8174 JB |
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 | |
22 | ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
72ea54a4 | 23 | |
edbd2f74 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; This package provides various useful commands (including help | |
27 | ;; system access) through the mouse. All this code assumes that mouse | |
28 | ;; interpretation has been abstracted into Emacs input events. | |
29 | ;; | |
30 | ;; The code is rather X-dependent. | |
31 | ||
aae56ea7 ER |
32 | ;;; Code: |
33 | ||
cc0a8174 | 34 | ;;; Utility functions. |
72ea54a4 | 35 | |
cc0a8174 JB |
36 | ;;; Indent track-mouse like progn. |
37 | (put 'track-mouse 'lisp-indent-function 0) | |
72ea54a4 | 38 | |
50f58001 RS |
39 | (defvar mouse-yank-at-point nil |
40 | "*If non-nil, mouse yank commands yank at point instead of at click.") | |
cc0a8174 | 41 | \f |
95132d1c RS |
42 | ;; Provide a mode-specific menu on a mouse button. |
43 | ||
44 | (defun mouse-major-mode-menu (event) | |
45 | "Pop up a mode-specific menu of mouse commands." | |
46 | ;; Switch to the window clicked on, because otherwise | |
47 | ;; the mode's commands may not make sense. | |
48 | (interactive "@e") | |
49 | (let ((newmap (make-sparse-keymap)) | |
50 | (unread-command-events (list event))) | |
51 | ;; Make a keymap in which our last command leads to a menu | |
52 | (define-key newmap (vector (car event)) | |
53 | (nconc (make-sparse-keymap "Menu") | |
54 | (mouse-major-mode-menu-1 | |
6917cfb0 KH |
55 | (and (current-local-map) |
56 | (lookup-key (current-local-map) [menu-bar]))))) | |
95132d1c RS |
57 | (mouse-major-mode-menu-compute-equiv-keys newmap) |
58 | (command-execute | |
59 | ;; Make NEWMAP override the usual definition | |
60 | ;; of the mouse button that got us here. | |
61 | ;; Then read the user's menu choice. | |
62 | (let ((minor-mode-map-alist | |
63 | (cons (cons t newmap) minor-mode-map-alist))) | |
64 | (lookup-key newmap (read-key-sequence "")))))) | |
65 | ||
66 | ;; Compute and cache the equivalent keys in MENU and all its submenus. | |
67 | (defun mouse-major-mode-menu-compute-equiv-keys (menu) | |
68 | (and (eq (car menu) 'keymap) | |
69 | (x-popup-menu nil menu)) | |
70 | (while menu | |
71 | (and (consp (car menu)) | |
72 | (consp (cdr (car menu))) | |
73 | (let ((tail (cdr (car menu)))) | |
74 | (while (and (consp tail) | |
75 | (not (eq (car tail) 'keymap))) | |
76 | (setq tail (cdr tail))) | |
77 | (if (consp tail) | |
78 | (mouse-major-mode-menu-compute-equiv-keys tail)))) | |
79 | (setq menu (cdr menu)))) | |
80 | ||
81 | ;; Given a mode's menu bar keymap, | |
82 | ;; if it defines exactly one menu bar menu, | |
83 | ;; return just that menu. | |
84 | ;; Otherwise return a menu for all of them. | |
85 | (defun mouse-major-mode-menu-1 (menubar) | |
86 | (if menubar | |
87 | (let ((tail menubar) | |
88 | submap) | |
89 | (while tail | |
90 | (if (consp (car tail)) | |
91 | (if submap | |
92 | (setq submap t) | |
93 | (setq submap (cdr (car tail))))) | |
94 | (setq tail (cdr tail))) | |
95 | (if (eq submap t) menubar | |
96 | submap)))) | |
97 | \f | |
544e7e73 RS |
98 | ;; Commands that operate on windows. |
99 | ||
d65147f6 KH |
100 | (defun mouse-minibuffer-check (event) |
101 | (let ((w (posn-window (event-start event)))) | |
102 | (and (window-minibuffer-p w) | |
103 | (not (minibuffer-window-active-p w)) | |
33c448cd RS |
104 | (error "Minibuffer window is not active"))) |
105 | ;; Give temporary modes such as isearch a chance to turn off. | |
106 | (run-hooks 'mouse-leave-buffer-hook)) | |
d65147f6 | 107 | |
cc0a8174 | 108 | (defun mouse-delete-window (click) |
947da0c4 | 109 | "Delete the window you click on. |
cc0a8174 | 110 | This must be bound to a mouse click." |
ec558adc | 111 | (interactive "e") |
d65147f6 | 112 | (mouse-minibuffer-check click) |
b5370f03 | 113 | (delete-window (posn-window (event-start click)))) |
cc0a8174 | 114 | |
3c2dd2c0 RS |
115 | (defun mouse-select-window (click) |
116 | "Select the window clicked on; don't move point." | |
117 | (interactive "e") | |
d65147f6 | 118 | (mouse-minibuffer-check click) |
3c2dd2c0 RS |
119 | (let ((oframe (selected-frame)) |
120 | (frame (window-frame (posn-window (event-start click))))) | |
121 | (select-window (posn-window (event-start click))) | |
122 | (raise-frame frame) | |
123 | (select-frame frame) | |
124 | (or (eq frame oframe) | |
125 | (set-mouse-position (selected-frame) (1- (frame-width)) 0)) | |
126 | (unfocus-frame))) | |
127 | ||
b0f3a26b JB |
128 | (defun mouse-tear-off-window (click) |
129 | "Delete the window clicked on, and create a new frame displaying its buffer." | |
130 | (interactive "e") | |
d65147f6 | 131 | (mouse-minibuffer-check click) |
b0f3a26b JB |
132 | (let* ((window (posn-window (event-start click))) |
133 | (buf (window-buffer window)) | |
01a911e3 | 134 | (frame (make-frame))) |
b0f3a26b JB |
135 | (select-frame frame) |
136 | (switch-to-buffer buf) | |
137 | (delete-window window))) | |
138 | ||
b5370f03 | 139 | (defun mouse-delete-other-windows () |
947da0c4 | 140 | "Delete all window except the one you click on." |
b5370f03 | 141 | (interactive "@") |
cc0a8174 | 142 | (delete-other-windows)) |
72ea54a4 | 143 | |
cc0a8174 JB |
144 | (defun mouse-split-window-vertically (click) |
145 | "Select Emacs window mouse is on, then split it vertically in half. | |
146 | The window is split at the line clicked on. | |
147 | This command must be bound to a mouse click." | |
947da0c4 | 148 | (interactive "@e") |
d65147f6 | 149 | (mouse-minibuffer-check click) |
b5370f03 JB |
150 | (let ((start (event-start click))) |
151 | (select-window (posn-window start)) | |
85d6b80b | 152 | (let ((new-height (1+ (cdr (posn-col-row (event-end click))))) |
5ba2dc3f JB |
153 | (first-line window-min-height) |
154 | (last-line (- (window-height) window-min-height))) | |
155 | (if (< last-line first-line) | |
0a50b993 | 156 | (error "Window too short to split") |
5ba2dc3f JB |
157 | (split-window-vertically |
158 | (min (max new-height first-line) last-line)))))) | |
cc0a8174 | 159 | |
947da0c4 RS |
160 | (defun mouse-split-window-horizontally (click) |
161 | "Select Emacs window mouse is on, then split it horizontally in half. | |
162 | The window is split at the column clicked on. | |
163 | This command must be bound to a mouse click." | |
164 | (interactive "@e") | |
d65147f6 | 165 | (mouse-minibuffer-check click) |
5ba2dc3f JB |
166 | (let ((start (event-start click))) |
167 | (select-window (posn-window start)) | |
168 | (let ((new-width (1+ (car (posn-col-row (event-end click))))) | |
169 | (first-col window-min-width) | |
170 | (last-col (- (window-width) window-min-width))) | |
171 | (if (< last-col first-col) | |
0a50b993 | 172 | (error "Window too narrow to split") |
5ba2dc3f JB |
173 | (split-window-horizontally |
174 | (min (max new-width first-col) last-col)))))) | |
947da0c4 | 175 | |
544e7e73 RS |
176 | (defun mouse-drag-mode-line (start-event) |
177 | "Change the height of a window by dragging on the mode line." | |
178 | (interactive "e") | |
33c448cd RS |
179 | ;; Give temporary modes such as isearch a chance to turn off. |
180 | (run-hooks 'mouse-leave-buffer-hook) | |
544e7e73 RS |
181 | (let ((done nil) |
182 | (echo-keystrokes 0) | |
183 | (start-event-frame (window-frame (car (car (cdr start-event))))) | |
184 | (start-event-window (car (car (cdr start-event)))) | |
185 | (start-nwindows (count-windows t)) | |
186 | (old-selected-window (selected-window)) | |
187 | should-enlarge-minibuffer | |
188 | event mouse minibuffer y top bot edges wconfig params growth) | |
189 | (setq params (frame-parameters)) | |
190 | (if (and (not (setq minibuffer (cdr (assq 'minibuffer params)))) | |
191 | (one-window-p t)) | |
192 | (error "Attempt to resize sole window")) | |
193 | (track-mouse | |
194 | (progn | |
195 | ;; enlarge-window only works on the selected window, so | |
196 | ;; we must select the window where the start event originated. | |
197 | ;; unwind-protect will restore the old selected window later. | |
198 | (select-window start-event-window) | |
199 | ;; if this is the bottommost ordinary window, then to | |
200 | ;; move its modeline the minibuffer must be enlarged. | |
201 | (setq should-enlarge-minibuffer | |
202 | (and minibuffer | |
203 | (not (one-window-p t)) | |
204 | (= (nth 1 (window-edges minibuffer)) | |
205 | (nth 3 (window-edges))))) | |
206 | ;; loop reading events and sampling the position of | |
207 | ;; the mouse. | |
208 | (while (not done) | |
209 | (setq event (read-event) | |
210 | mouse (mouse-position)) | |
211 | ;; do nothing if | |
212 | ;; - there is a switch-frame event. | |
213 | ;; - the mouse isn't in the frame that we started in | |
214 | ;; - the mouse isn't in any Emacs frame | |
215 | ;; drag if | |
216 | ;; - there is a mouse-movement event | |
217 | ;; - there is a scroll-bar-movement event | |
218 | ;; (same as mouse movement for our purposes) | |
219 | ;; quit if | |
220 | ;; - there is a keyboard event or some other unknown event | |
221 | ;; unknown event. | |
222 | (cond ((integerp event) | |
223 | (setq done t)) | |
224 | ((eq (car event) 'switch-frame) | |
225 | nil) | |
226 | ((not (memq (car event) | |
227 | '(mouse-movement scroll-bar-movement))) | |
228 | (if (consp event) | |
229 | (setq unread-command-events | |
230 | (cons event unread-command-events))) | |
231 | (setq done t)) | |
232 | ((not (eq (car mouse) start-event-frame)) | |
233 | nil) | |
234 | ((null (car (cdr mouse))) | |
235 | nil) | |
236 | (t | |
237 | (setq y (cdr (cdr mouse)) | |
238 | edges (window-edges) | |
239 | top (nth 1 edges) | |
240 | bot (nth 3 edges)) | |
241 | ;; scale back a move that would make the | |
242 | ;; window too short. | |
243 | (cond ((< (- y top -1) window-min-height) | |
244 | (setq y (+ top window-min-height -1)))) | |
245 | ;; compute size change needed | |
246 | (setq growth (- y bot -1) | |
247 | wconfig (current-window-configuration)) | |
248 | ;; grow/shrink minibuffer? | |
249 | (if should-enlarge-minibuffer | |
250 | (progn | |
251 | ;; yes. briefly select minibuffer so | |
252 | ;; enlarge-window will affect the | |
253 | ;; correct window. | |
254 | (select-window minibuffer) | |
255 | ;; scale back shrinkage if it would | |
256 | ;; make the minibuffer less than 1 | |
257 | ;; line tall. | |
258 | (if (and (> growth 0) | |
259 | (< (- (window-height minibuffer) | |
260 | growth) | |
261 | 1)) | |
262 | (setq growth (1- (window-height minibuffer)))) | |
263 | (enlarge-window (- growth)) | |
264 | (select-window start-event-window)) | |
265 | ;; no. grow/shrink the selected window | |
266 | (enlarge-window growth)) | |
267 | ;; if this window's growth caused another | |
268 | ;; window to be deleted because it was too | |
269 | ;; short, rescind the change. | |
270 | ;; | |
271 | ;; if size change caused space to be stolen | |
272 | ;; from a window above this one, rescind the | |
273 | ;; change, but only if we didn't grow/srhink | |
274 | ;; the minibuffer. minibuffer size changes | |
275 | ;; can cause all windows to shrink... no way | |
276 | ;; around it. | |
277 | (if (or (/= start-nwindows (count-windows t)) | |
278 | (and (not should-enlarge-minibuffer) | |
279 | (/= top (nth 1 (window-edges))))) | |
280 | (set-window-configuration wconfig))))))))) | |
281 | \f | |
2a5fa27b | 282 | (defun mouse-set-point (event) |
cc0a8174 | 283 | "Move point to the position clicked on with the mouse. |
2a5fa27b | 284 | This should be bound to a mouse click event type." |
ec558adc | 285 | (interactive "e") |
d65147f6 | 286 | (mouse-minibuffer-check event) |
2a5fa27b RS |
287 | ;; Use event-end in case called from mouse-drag-region. |
288 | ;; If EVENT is a click, event-end and event-start give same value. | |
289 | (let ((posn (event-end event))) | |
0a50b993 RS |
290 | (if (not (windowp (posn-window posn))) |
291 | (error "Cursor not in text area of window")) | |
b5370f03 JB |
292 | (select-window (posn-window posn)) |
293 | (if (numberp (posn-point posn)) | |
294 | (goto-char (posn-point posn))))) | |
cc0a8174 | 295 | |
e6c2f5d4 RS |
296 | (defvar mouse-last-region-beg nil) |
297 | (defvar mouse-last-region-end nil) | |
298 | (defvar mouse-last-region-tick nil) | |
299 | ||
300 | (defun mouse-region-match () | |
301 | "Return non-nil if there's an active region that was set with the mouse." | |
302 | (and (mark t) mark-active | |
303 | (eq mouse-last-region-beg (region-beginning)) | |
304 | (eq mouse-last-region-end (region-end)) | |
305 | (eq mouse-last-region-tick (buffer-modified-tick)))) | |
306 | ||
652ccd35 | 307 | (defun mouse-set-region (click) |
e37de120 | 308 | "Set the region to the text dragged over, and copy to kill ring. |
2a5fa27b | 309 | This should be bound to a mouse drag event." |
652ccd35 | 310 | (interactive "e") |
d65147f6 | 311 | (mouse-minibuffer-check click) |
652ccd35 RS |
312 | (let ((posn (event-start click)) |
313 | (end (event-end click))) | |
314 | (select-window (posn-window posn)) | |
315 | (if (numberp (posn-point posn)) | |
316 | (goto-char (posn-point posn))) | |
fcfc3c63 | 317 | ;; If mark is highlighted, no need to bounce the cursor. |
33a35434 KH |
318 | ;; On X, we highlight while dragging, thus once again no need to bounce. |
319 | (or transient-mark-mode | |
320 | (eq (framep (selected-frame)) 'x) | |
fcfc3c63 | 321 | (sit-for 1)) |
652ccd35 | 322 | (push-mark) |
1cc8a3f4 | 323 | (set-mark (point)) |
652ccd35 | 324 | (if (numberp (posn-point end)) |
e37de120 RS |
325 | (goto-char (posn-point end))) |
326 | ;; Don't set this-command to kill-region, so that a following | |
327 | ;; C-w will not double the text in the kill ring. | |
91a6bc10 RS |
328 | ;; Ignore last-command so we don't append to a preceding kill. |
329 | (let (this-command last-command) | |
e6c2f5d4 RS |
330 | (copy-region-as-kill (mark) (point))) |
331 | (mouse-set-region-1))) | |
332 | ||
333 | (defun mouse-set-region-1 () | |
334 | (setq mouse-last-region-beg (region-beginning)) | |
335 | (setq mouse-last-region-end (region-end)) | |
336 | (setq mouse-last-region-tick (buffer-modified-tick))) | |
652ccd35 | 337 | |
600c6e3a JB |
338 | (defvar mouse-scroll-delay 0.25 |
339 | "*The pause between scroll steps caused by mouse drags, in seconds. | |
340 | If you drag the mouse beyond the edge of a window, Emacs scrolls the | |
341 | window to bring the text beyond that edge into view, with a delay of | |
342 | this many seconds between scroll steps. Scrolling stops when you move | |
343 | the mouse back into the window, or release the button. | |
344 | This variable's value may be non-integral. | |
345 | Setting this to zero causes Emacs to scroll as fast as it can.") | |
346 | ||
e919a622 RS |
347 | (defun mouse-scroll-subr (window jump &optional overlay start) |
348 | "Scroll the window WINDOW, JUMP lines at a time, until new input arrives. | |
600c6e3a JB |
349 | If OVERLAY is an overlay, let it stretch from START to the far edge of |
350 | the newly visible text. | |
351 | Upon exit, point is at the far edge of the newly visible text." | |
4e399a53 RS |
352 | (let ((opoint (point))) |
353 | (while (progn | |
354 | (goto-char (window-start window)) | |
355 | (if (not (zerop (vertical-motion jump window))) | |
356 | (progn | |
357 | (set-window-start window (point)) | |
358 | (if (natnump jump) | |
359 | (progn | |
360 | (goto-char (window-end window)) | |
361 | ;; window-end doesn't reflect the window's new | |
362 | ;; start position until the next redisplay. Hurrah. | |
363 | (vertical-motion (1- jump) window)) | |
364 | (goto-char (window-start window))) | |
365 | (if overlay | |
366 | (move-overlay overlay start (point))) | |
367 | ;; Now that we have scrolled WINDOW properly, | |
368 | ;; put point back where it was for the redisplay | |
369 | ;; so that we don't mess up the selected window. | |
370 | (or (eq window (selected-window)) | |
371 | (goto-char opoint)) | |
d2287ded | 372 | (sit-for mouse-scroll-delay))))) |
4e399a53 RS |
373 | (or (eq window (selected-window)) |
374 | (goto-char opoint)))) | |
fcfc3c63 | 375 | |
dc269e81 | 376 | ;; Create an overlay and immediately delete it, to get "overlay in no buffer". |
600c6e3a | 377 | (defvar mouse-drag-overlay (make-overlay 1 1)) |
dc269e81 | 378 | (delete-overlay mouse-drag-overlay) |
600c6e3a JB |
379 | (overlay-put mouse-drag-overlay 'face 'region) |
380 | ||
dd524dbd | 381 | (defvar mouse-selection-click-count 0) |
eb6ff46f | 382 | |
600c6e3a | 383 | (defun mouse-drag-region (start-event) |
bcd5aef1 | 384 | "Set the region to the text that the mouse is dragged over. |
78210c95 RS |
385 | Highlight the drag area as you move the mouse. |
386 | This must be bound to a button-down mouse event. | |
387 | In Transient Mark mode, the highlighting remains once you | |
388 | release the mouse button. Otherwise, it does not." | |
bcd5aef1 | 389 | (interactive "e") |
d65147f6 | 390 | (mouse-minibuffer-check start-event) |
600c6e3a JB |
391 | (let* ((start-posn (event-start start-event)) |
392 | (start-point (posn-point start-posn)) | |
393 | (start-window (posn-window start-posn)) | |
b846d039 | 394 | (start-frame (window-frame start-window)) |
600c6e3a JB |
395 | (bounds (window-edges start-window)) |
396 | (top (nth 1 bounds)) | |
397 | (bottom (if (window-minibuffer-p start-window) | |
398 | (nth 3 bounds) | |
399 | ;; Don't count the mode line. | |
e37de120 RS |
400 | (1- (nth 3 bounds)))) |
401 | (click-count (1- (event-click-count start-event)))) | |
eb6ff46f | 402 | (setq mouse-selection-click-count click-count) |
b80f1928 | 403 | (mouse-set-point start-event) |
e37de120 RS |
404 | (let ((range (mouse-start-end start-point start-point click-count))) |
405 | (move-overlay mouse-drag-overlay (car range) (nth 1 range) | |
406 | (window-buffer start-window))) | |
f767385c | 407 | (deactivate-mark) |
600c6e3a | 408 | (let (event end end-point) |
bcd5aef1 | 409 | (track-mouse |
600c6e3a | 410 | (while (progn |
b846d039 JB |
411 | (setq event (read-event)) |
412 | (or (mouse-movement-p event) | |
413 | (eq (car-safe event) 'switch-frame))) | |
b846d039 JB |
414 | (if (eq (car-safe event) 'switch-frame) |
415 | nil | |
416 | (setq end (event-end event) | |
417 | end-point (posn-point end)) | |
418 | ||
419 | (cond | |
b846d039 JB |
420 | ;; Are we moving within the original window? |
421 | ((and (eq (posn-window end) start-window) | |
422 | (integer-or-marker-p end-point)) | |
423 | (goto-char end-point) | |
e37de120 RS |
424 | (let ((range (mouse-start-end start-point (point) click-count))) |
425 | (move-overlay mouse-drag-overlay (car range) (nth 1 range)))) | |
b846d039 | 426 | |
50e527bc KH |
427 | (t |
428 | (let ((mouse-row (cdr (cdr (mouse-position))))) | |
b846d039 | 429 | (cond |
50e527bc | 430 | ((null mouse-row)) |
b846d039 | 431 | ((< mouse-row top) |
e919a622 RS |
432 | (mouse-scroll-subr start-window (- mouse-row top) |
433 | mouse-drag-overlay start-point)) | |
d2287ded | 434 | ((>= mouse-row bottom) |
e919a622 | 435 | (mouse-scroll-subr start-window (1+ (- mouse-row bottom)) |
50e527bc | 436 | mouse-drag-overlay start-point))))))))) |
4e399a53 | 437 | (if (consp event) |
e37de120 | 438 | (let ((fun (key-binding (vector (car event))))) |
bfbcf12e RS |
439 | ;; Run the binding of the terminating up-event, if possible. |
440 | ;; In the case of a multiple click, it gives the wrong results, | |
441 | ;; because it would fail to set up a region. | |
442 | (if (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun)) | |
b64548c7 RS |
443 | (progn |
444 | (setq this-command fun) | |
e6c2f5d4 RS |
445 | ;; Delete the overlay before calling the function, |
446 | ;; because delete-overlay increases buffer-modified-tick. | |
447 | (delete-overlay mouse-drag-overlay) | |
b64548c7 | 448 | (funcall fun event)) |
44dc5252 RS |
449 | (if (not (= (overlay-start mouse-drag-overlay) |
450 | (overlay-end mouse-drag-overlay))) | |
451 | (let (last-command this-command) | |
452 | (push-mark (overlay-start mouse-drag-overlay) t t) | |
453 | (goto-char (overlay-end mouse-drag-overlay)) | |
e6c2f5d4 RS |
454 | (delete-overlay mouse-drag-overlay) |
455 | (copy-region-as-kill (point) (mark t)) | |
456 | (mouse-set-region-1)) | |
44dc5252 | 457 | (goto-char (overlay-end mouse-drag-overlay)) |
e6c2f5d4 RS |
458 | (setq this-command 'mouse-set-point) |
459 | (delete-overlay mouse-drag-overlay)))) | |
460 | (delete-overlay mouse-drag-overlay))))) | |
e37de120 RS |
461 | \f |
462 | ;; Commands to handle xterm-style multiple clicks. | |
600c6e3a | 463 | |
e37de120 RS |
464 | (defun mouse-skip-word (dir) |
465 | "Skip over word, over whitespace, or over identical punctuation. | |
466 | If DIR is positive skip forward; if negative, skip backward." | |
467 | (let* ((char (following-char)) | |
468 | (syntax (char-to-string (char-syntax char)))) | |
469 | (if (or (string= syntax "w") (string= syntax " ")) | |
470 | (if (< dir 0) | |
471 | (skip-syntax-backward syntax) | |
472 | (skip-syntax-forward syntax)) | |
473 | (if (< dir 0) | |
d89a4a47 | 474 | (while (and (not (bobp)) (= (preceding-char) char)) |
e37de120 | 475 | (forward-char -1)) |
d89a4a47 | 476 | (while (and (not (eobp)) (= (following-char) char)) |
e37de120 RS |
477 | (forward-char 1)))))) |
478 | ||
479 | ;; Return a list of region bounds based on START and END according to MODE. | |
480 | ;; If MODE is 0 then set point to (min START END), mark to (max START END). | |
481 | ;; If MODE is 1 then set point to start of word at (min START END), | |
482 | ;; mark to end of word at (max START END). | |
483 | ;; If MODE is 2 then do the same for lines. | |
eb6ff46f | 484 | (defun mouse-start-end (start end mode) |
e37de120 RS |
485 | (if (> start end) |
486 | (let ((temp start)) | |
487 | (setq start end | |
488 | end temp))) | |
9a974c88 | 489 | (setq mode (mod mode 3)) |
e37de120 RS |
490 | (cond ((= mode 0) |
491 | (list start end)) | |
492 | ((and (= mode 1) | |
493 | (= start end) | |
1ec71583 | 494 | (char-after start) |
e37de120 | 495 | (= (char-syntax (char-after start)) ?\()) |
6f482eec RS |
496 | (list start |
497 | (save-excursion | |
498 | (goto-char start) | |
499 | (forward-sexp 1) | |
500 | (point)))) | |
e37de120 RS |
501 | ((and (= mode 1) |
502 | (= start end) | |
1ec71583 | 503 | (char-after start) |
e37de120 RS |
504 | (= (char-syntax (char-after start)) ?\))) |
505 | (list (save-excursion | |
506 | (goto-char (1+ start)) | |
d89a4a47 RS |
507 | (backward-sexp 1) |
508 | (point)) | |
e37de120 RS |
509 | (1+ start))) |
510 | ((= mode 1) | |
511 | (list (save-excursion | |
512 | (goto-char start) | |
513 | (mouse-skip-word -1) | |
514 | (point)) | |
515 | (save-excursion | |
516 | (goto-char end) | |
517 | (mouse-skip-word 1) | |
518 | (point)))) | |
519 | ((= mode 2) | |
520 | (list (save-excursion | |
521 | (goto-char start) | |
522 | (beginning-of-line 1) | |
523 | (point)) | |
524 | (save-excursion | |
525 | (goto-char end) | |
526 | (forward-line 1) | |
527 | (point)))))) | |
e66feb07 | 528 | \f |
3f26b32a RS |
529 | ;; Subroutine: set the mark where CLICK happened, |
530 | ;; but don't do anything else. | |
531 | (defun mouse-set-mark-fast (click) | |
d65147f6 | 532 | (mouse-minibuffer-check click) |
3f26b32a RS |
533 | (let ((posn (event-start click))) |
534 | (select-window (posn-window posn)) | |
535 | (if (numberp (posn-point posn)) | |
536 | (push-mark (posn-point posn) t t)))) | |
537 | ||
538 | ;; Momentarily show where the mark is, if highlighting doesn't show it. | |
539 | (defun mouse-show-mark () | |
540 | (or transient-mark-mode | |
541 | (save-excursion | |
542 | (goto-char (mark t)) | |
543 | (sit-for 1)))) | |
544 | ||
cc0a8174 JB |
545 | (defun mouse-set-mark (click) |
546 | "Set mark at the position clicked on with the mouse. | |
547 | Display cursor at that position for a second. | |
548 | This must be bound to a mouse click." | |
ec558adc | 549 | (interactive "e") |
f598fb03 RS |
550 | (mouse-minibuffer-check click) |
551 | (select-window (posn-window (event-start click))) | |
552 | ;; We don't use save-excursion because that preserves the mark too. | |
72ea54a4 RS |
553 | (let ((point-save (point))) |
554 | (unwind-protect | |
cc0a8174 | 555 | (progn (mouse-set-point click) |
897897e3 RS |
556 | (push-mark nil t t) |
557 | (or transient-mark-mode | |
558 | (sit-for 1))) | |
72ea54a4 RS |
559 | (goto-char point-save)))) |
560 | ||
cc0a8174 JB |
561 | (defun mouse-kill (click) |
562 | "Kill the region between point and the mouse click. | |
563 | The text is saved in the kill ring, as with \\[kill-region]." | |
ec558adc | 564 | (interactive "e") |
d65147f6 | 565 | (mouse-minibuffer-check click) |
142c7672 KH |
566 | (let* ((posn (event-start click)) |
567 | (click-posn (posn-point posn))) | |
568 | (select-window (posn-window posn)) | |
bd307392 JB |
569 | (if (numberp click-posn) |
570 | (kill-region (min (point) click-posn) | |
571 | (max (point) click-posn))))) | |
72ea54a4 | 572 | |
87ef29fd JB |
573 | (defun mouse-yank-at-click (click arg) |
574 | "Insert the last stretch of killed text at the position clicked on. | |
50f58001 RS |
575 | Also move point to one end of the text thus inserted (normally the end). |
576 | Prefix arguments are interpreted as with \\[yank]. | |
577 | If `mouse-yank-at-point' is non-nil, insert at point | |
578 | regardless of where you click." | |
ec558adc | 579 | (interactive "e\nP") |
33c448cd RS |
580 | ;; Give temporary modes such as isearch a chance to turn off. |
581 | (run-hooks 'mouse-leave-buffer-hook) | |
50f58001 | 582 | (or mouse-yank-at-point (mouse-set-point click)) |
d89a4a47 | 583 | (setq this-command 'yank) |
a814e9df | 584 | (setq mouse-selection-click-count 0) |
87ef29fd JB |
585 | (yank arg)) |
586 | ||
587 | (defun mouse-kill-ring-save (click) | |
cc0a8174 JB |
588 | "Copy the region between point and the mouse click in the kill ring. |
589 | This does not delete the region; it acts like \\[kill-ring-save]." | |
ec558adc | 590 | (interactive "e") |
3f26b32a | 591 | (mouse-set-mark-fast click) |
6452d8a6 RS |
592 | (let (this-command last-command) |
593 | (kill-ring-save (point) (mark t))) | |
3f26b32a | 594 | (mouse-show-mark)) |
72ea54a4 | 595 | |
dbc4e1c1 JB |
596 | ;;; This function used to delete the text between point and the mouse |
597 | ;;; whenever it was equal to the front of the kill ring, but some | |
598 | ;;; people found that confusing. | |
599 | ||
600 | ;;; A list (TEXT START END), describing the text and position of the last | |
601 | ;;; invocation of mouse-save-then-kill. | |
602 | (defvar mouse-save-then-kill-posn nil) | |
603 | ||
26d280b9 | 604 | (defun mouse-save-then-kill-delete-region (beg end) |
9a974c88 RS |
605 | ;; We must make our own undo boundaries |
606 | ;; because they happen automatically only for the current buffer. | |
607 | (undo-boundary) | |
dd524dbd RS |
608 | (if (or (= beg end) (eq buffer-undo-list t)) |
609 | ;; If we have no undo list in this buffer, | |
610 | ;; just delete. | |
611 | (delete-region beg end) | |
612 | ;; Delete, but make the undo-list entry share with the kill ring. | |
613 | ;; First, delete just one char, so in case buffer is being modified | |
614 | ;; for the first time, the undo list records that fact. | |
2f4b15ef RS |
615 | (let (before-change-function after-change-function |
616 | before-change-functions after-change-functions) | |
617 | (delete-region beg | |
618 | (+ beg (if (> end beg) 1 -1)))) | |
dd524dbd RS |
619 | (let ((buffer-undo-list buffer-undo-list)) |
620 | ;; Undo that deletion--but don't change the undo list! | |
2f4b15ef RS |
621 | (let (before-change-function after-change-function |
622 | before-change-functions after-change-functions) | |
623 | (primitive-undo 1 buffer-undo-list)) | |
dd524dbd RS |
624 | ;; Now delete the rest of the specified region, |
625 | ;; but don't record it. | |
626 | (setq buffer-undo-list t) | |
9a974c88 RS |
627 | (if (/= (length (car kill-ring)) (- (max end beg) (min end beg))) |
628 | (error "Lossage in mouse-save-then-kill-delete-region")) | |
dd524dbd RS |
629 | (delete-region beg end)) |
630 | (let ((tail buffer-undo-list)) | |
631 | ;; Search back in buffer-undo-list for the string | |
632 | ;; that came from deleting one character. | |
633 | (while (and tail (not (stringp (car (car tail))))) | |
634 | (setq tail (cdr tail))) | |
635 | ;; Replace it with an entry for the entire deleted text. | |
636 | (and tail | |
9a974c88 RS |
637 | (setcar tail (cons (car kill-ring) (min beg end)))))) |
638 | (undo-boundary)) | |
eb6ff46f | 639 | |
947da0c4 | 640 | (defun mouse-save-then-kill (click) |
40a45a9f RS |
641 | "Save text to point in kill ring; the second time, kill the text. |
642 | If the text between point and the mouse is the same as what's | |
643 | at the front of the kill ring, this deletes the text. | |
644 | Otherwise, it adds the text to the kill ring, like \\[kill-ring-save], | |
eb6ff46f RS |
645 | which prepares for a second click to delete the text. |
646 | ||
647 | If you have selected words or lines, this command extends the | |
648 | selection through the word or line clicked on. If you do this | |
649 | again in a different position, it extends the selection again. | |
650 | If you do this twice in the same position, the selection is killed." | |
947da0c4 | 651 | (interactive "e") |
b64548c7 RS |
652 | (let ((before-scroll point-before-scroll)) |
653 | (mouse-minibuffer-check click) | |
654 | (let ((click-posn (posn-point (event-start click))) | |
655 | ;; Don't let a subsequent kill command append to this one: | |
656 | ;; prevent setting this-command to kill-region. | |
657 | (this-command this-command)) | |
658 | (if (and (mark t) (> (mod mouse-selection-click-count 3) 0)) | |
659 | (if (not (and (eq last-command 'mouse-save-then-kill) | |
660 | (equal click-posn | |
661 | (car (cdr-safe (cdr-safe mouse-save-then-kill-posn)))))) | |
662 | ;; Find both ends of the object selected by this click. | |
663 | (let* ((range | |
664 | (mouse-start-end click-posn click-posn | |
665 | mouse-selection-click-count))) | |
666 | ;; Move whichever end is closer to the click. | |
667 | ;; That's what xterm does, and it seems reasonable. | |
668 | (if (< (abs (- click-posn (mark t))) | |
669 | (abs (- click-posn (point)))) | |
670 | (set-mark (car range)) | |
671 | (goto-char (nth 1 range))) | |
672 | ;; We have already put the old region in the kill ring. | |
673 | ;; Replace it with the extended region. | |
674 | ;; (It would be annoying to make a separate entry.) | |
675 | (kill-new (buffer-substring (point) (mark t)) t) | |
e6c2f5d4 | 676 | (mouse-set-region-1) |
b64548c7 RS |
677 | ;; Arrange for a repeated mouse-3 to kill this region. |
678 | (setq mouse-save-then-kill-posn | |
679 | (list (car kill-ring) (point) click-posn)) | |
680 | (mouse-show-mark)) | |
681 | ;; If we click this button again without moving it, | |
682 | ;; that time kill. | |
bcde3748 | 683 | (mouse-save-then-kill-delete-region (mark) (point)) |
b64548c7 | 684 | (setq mouse-selection-click-count 0) |
dd524dbd | 685 | (setq mouse-save-then-kill-posn nil)) |
b64548c7 RS |
686 | (if (and (eq last-command 'mouse-save-then-kill) |
687 | mouse-save-then-kill-posn | |
688 | (eq (car mouse-save-then-kill-posn) (car kill-ring)) | |
689 | (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn))) | |
690 | ;; If this is the second time we've called | |
691 | ;; mouse-save-then-kill, delete the text from the buffer. | |
692 | (progn | |
693 | (mouse-save-then-kill-delete-region (point) (mark)) | |
694 | ;; After we kill, another click counts as "the first time". | |
695 | (setq mouse-save-then-kill-posn nil)) | |
696 | (if (or (and (eq last-command 'mouse-save-then-kill) | |
697 | mouse-save-then-kill-posn) | |
698 | (and mark-active transient-mark-mode) | |
699 | (and (memq last-command | |
700 | '(mouse-drag-region mouse-set-region)) | |
701 | (or mark-even-if-inactive | |
702 | (not transient-mark-mode)))) | |
703 | ;; We have a selection or suitable region, so adjust it. | |
704 | (let* ((posn (event-start click)) | |
705 | (new (posn-point posn))) | |
706 | (select-window (posn-window posn)) | |
707 | (if (numberp new) | |
708 | (progn | |
709 | ;; Move whichever end of the region is closer to the click. | |
710 | ;; That is what xterm does, and it seems reasonable. | |
711 | (if (< (abs (- new (point))) (abs (- new (mark t)))) | |
712 | (goto-char new) | |
713 | (set-mark new)) | |
714 | (setq deactivate-mark nil))) | |
bcde3748 RS |
715 | (kill-new (buffer-substring (point) (mark t)) t) |
716 | (mouse-show-mark)) | |
b64548c7 RS |
717 | ;; Set the mark where point is, then move where clicked. |
718 | (mouse-set-mark-fast click) | |
719 | (if before-scroll | |
720 | (goto-char before-scroll)) | |
721 | (exchange-point-and-mark) | |
eb9780b0 | 722 | (kill-new (buffer-substring (point) (mark t)))) |
e6c2f5d4 | 723 | (mouse-set-region-1) |
b64548c7 RS |
724 | (setq mouse-save-then-kill-posn |
725 | (list (car kill-ring) (point) click-posn))))))) | |
e66feb07 RS |
726 | \f |
727 | (global-set-key [M-mouse-1] 'mouse-start-secondary) | |
728 | (global-set-key [M-drag-mouse-1] 'mouse-set-secondary) | |
729 | (global-set-key [M-down-mouse-1] 'mouse-drag-secondary) | |
730 | (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill) | |
9a974c88 | 731 | (global-set-key [M-mouse-2] 'mouse-yank-secondary) |
e66feb07 RS |
732 | |
733 | ;; An overlay which records the current secondary selection | |
734 | ;; or else is deleted when there is no secondary selection. | |
735 | ;; May be nil. | |
736 | (defvar mouse-secondary-overlay nil) | |
737 | ||
a94c7fcc RS |
738 | (defvar mouse-secondary-click-count 0) |
739 | ||
e66feb07 RS |
740 | ;; A marker which records the specified first end for a secondary selection. |
741 | ;; May be nil. | |
742 | (defvar mouse-secondary-start nil) | |
743 | ||
744 | (defun mouse-start-secondary (click) | |
745 | "Set one end of the secondary selection to the position clicked on. | |
746 | Use \\[mouse-secondary-save-then-kill] to set the other end | |
747 | and complete the secondary selection." | |
748 | (interactive "e") | |
d65147f6 | 749 | (mouse-minibuffer-check click) |
e66feb07 | 750 | (let ((posn (event-start click))) |
230aaa73 RS |
751 | (save-excursion |
752 | (set-buffer (window-buffer (posn-window posn))) | |
753 | ;; Cancel any preexisting secondary selection. | |
754 | (if mouse-secondary-overlay | |
755 | (delete-overlay mouse-secondary-overlay)) | |
756 | (if (numberp (posn-point posn)) | |
757 | (progn | |
758 | (or mouse-secondary-start | |
759 | (setq mouse-secondary-start (make-marker))) | |
760 | (move-marker mouse-secondary-start (posn-point posn))))))) | |
e66feb07 RS |
761 | |
762 | (defun mouse-set-secondary (click) | |
763 | "Set the secondary selection to the text that the mouse is dragged over. | |
764 | This must be bound to a mouse drag event." | |
765 | (interactive "e") | |
d65147f6 | 766 | (mouse-minibuffer-check click) |
e66feb07 RS |
767 | (let ((posn (event-start click)) |
768 | beg | |
769 | (end (event-end click))) | |
230aaa73 RS |
770 | (save-excursion |
771 | (set-buffer (window-buffer (posn-window posn))) | |
772 | (if (numberp (posn-point posn)) | |
773 | (setq beg (posn-point posn))) | |
774 | (if mouse-secondary-overlay | |
775 | (move-overlay mouse-secondary-overlay beg (posn-point end)) | |
776 | (setq mouse-secondary-overlay (make-overlay beg (posn-point end)))) | |
777 | (overlay-put mouse-secondary-overlay 'face 'secondary-selection)))) | |
947da0c4 | 778 | |
d89a4a47 | 779 | (defun mouse-drag-secondary (start-event) |
e66feb07 | 780 | "Set the secondary selection to the text that the mouse is dragged over. |
d89a4a47 | 781 | Highlight the drag area as you move the mouse. |
e66feb07 RS |
782 | This must be bound to a button-down mouse event." |
783 | (interactive "e") | |
d65147f6 | 784 | (mouse-minibuffer-check start-event) |
d89a4a47 RS |
785 | (let* ((start-posn (event-start start-event)) |
786 | (start-point (posn-point start-posn)) | |
787 | (start-window (posn-window start-posn)) | |
788 | (start-frame (window-frame start-window)) | |
789 | (bounds (window-edges start-window)) | |
790 | (top (nth 1 bounds)) | |
791 | (bottom (if (window-minibuffer-p start-window) | |
792 | (nth 3 bounds) | |
793 | ;; Don't count the mode line. | |
794 | (1- (nth 3 bounds)))) | |
795 | (click-count (1- (event-click-count start-event)))) | |
796 | (save-excursion | |
797 | (set-buffer (window-buffer start-window)) | |
a94c7fcc | 798 | (setq mouse-secondary-click-count click-count) |
d89a4a47 RS |
799 | (or mouse-secondary-overlay |
800 | (setq mouse-secondary-overlay | |
801 | (make-overlay (point) (point)))) | |
26d280b9 | 802 | (overlay-put mouse-secondary-overlay 'face 'secondary-selection) |
9a974c88 | 803 | (if (> (mod click-count 3) 0) |
26d280b9 RS |
804 | ;; Double or triple press: make an initial selection |
805 | ;; of one word or line. | |
d89a4a47 RS |
806 | (let ((range (mouse-start-end start-point start-point click-count))) |
807 | (set-marker mouse-secondary-start nil) | |
808 | (move-overlay mouse-secondary-overlay 1 1 | |
809 | (window-buffer start-window)) | |
810 | (move-overlay mouse-secondary-overlay (car range) (nth 1 range) | |
811 | (window-buffer start-window))) | |
26d280b9 | 812 | ;; Single-press: cancel any preexisting secondary selection. |
d89a4a47 RS |
813 | (or mouse-secondary-start |
814 | (setq mouse-secondary-start (make-marker))) | |
815 | (set-marker mouse-secondary-start start-point) | |
816 | (delete-overlay mouse-secondary-overlay)) | |
817 | (let (event end end-point) | |
818 | (track-mouse | |
819 | (while (progn | |
820 | (setq event (read-event)) | |
821 | (or (mouse-movement-p event) | |
822 | (eq (car-safe event) 'switch-frame))) | |
823 | ||
824 | (if (eq (car-safe event) 'switch-frame) | |
825 | nil | |
826 | (setq end (event-end event) | |
827 | end-point (posn-point end)) | |
828 | (cond | |
d89a4a47 RS |
829 | ;; Are we moving within the original window? |
830 | ((and (eq (posn-window end) start-window) | |
831 | (integer-or-marker-p end-point)) | |
d89a4a47 RS |
832 | (let ((range (mouse-start-end start-point end-point |
833 | click-count))) | |
0136e1e3 RS |
834 | (if (or (/= start-point end-point) |
835 | (null (marker-position mouse-secondary-start))) | |
836 | (progn | |
837 | (set-marker mouse-secondary-start nil) | |
838 | (move-overlay mouse-secondary-overlay | |
839 | (car range) (nth 1 range)))))) | |
3b0aebe9 RS |
840 | (t |
841 | (let ((mouse-row (cdr (cdr (mouse-position))))) | |
842 | (cond | |
843 | ((null mouse-row)) | |
844 | ((< mouse-row top) | |
e919a622 RS |
845 | (mouse-scroll-subr start-window (- mouse-row top) |
846 | mouse-secondary-overlay start-point)) | |
d2287ded | 847 | ((>= mouse-row bottom) |
e919a622 | 848 | (mouse-scroll-subr start-window (1+ (- mouse-row bottom)) |
3b0aebe9 | 849 | mouse-secondary-overlay start-point))))))))) |
d89a4a47 | 850 | |
4e399a53 RS |
851 | (if (consp event) |
852 | ;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click) | |
853 | ;;; (eq (posn-window (event-end event)) start-window) | |
854 | ;;; (numberp (posn-point (event-end event))) | |
d89a4a47 RS |
855 | (if (marker-position mouse-secondary-start) |
856 | (save-window-excursion | |
857 | (delete-overlay mouse-secondary-overlay) | |
9a974c88 | 858 | (x-set-selection 'SECONDARY nil) |
d89a4a47 RS |
859 | (select-window start-window) |
860 | (save-excursion | |
861 | (goto-char mouse-secondary-start) | |
862 | (sit-for 1))) | |
9a974c88 RS |
863 | (x-set-selection |
864 | 'SECONDARY | |
865 | (buffer-substring (overlay-start mouse-secondary-overlay) | |
866 | (overlay-end mouse-secondary-overlay))))))))) | |
e66feb07 | 867 | |
9a974c88 | 868 | (defun mouse-yank-secondary (click) |
50f58001 RS |
869 | "Insert the secondary selection at the position clicked on. |
870 | Move point to the end of the inserted text. | |
871 | If `mouse-yank-at-point' is non-nil, insert at point | |
872 | regardless of where you click." | |
9a974c88 | 873 | (interactive "e") |
33c448cd RS |
874 | ;; Give temporary modes such as isearch a chance to turn off. |
875 | (run-hooks 'mouse-leave-buffer-hook) | |
50f58001 RS |
876 | (or mouse-yank-at-point (mouse-set-point click)) |
877 | (insert (x-get-selection 'SECONDARY))) | |
9a974c88 | 878 | |
7e6404f6 | 879 | (defun mouse-kill-secondary () |
9a974c88 RS |
880 | "Kill the text in the secondary selection. |
881 | This is intended more as a keyboard command than as a mouse command | |
882 | but it can work as either one. | |
883 | ||
884 | The current buffer (in case of keyboard use), or the buffer clicked on, | |
885 | must be the one that the secondary selection is in. This requirement | |
886 | is to prevent accidents." | |
7e6404f6 RS |
887 | (interactive) |
888 | (let* ((keys (this-command-keys)) | |
889 | (click (elt keys (1- (length keys))))) | |
890 | (or (eq (overlay-buffer mouse-secondary-overlay) | |
891 | (if (listp click) | |
892 | (window-buffer (posn-window (event-start click))) | |
893 | (current-buffer))) | |
894 | (error "Select or click on the buffer where the secondary selection is"))) | |
9dfab550 RS |
895 | (let (this-command) |
896 | (save-excursion | |
897 | (set-buffer (overlay-buffer mouse-secondary-overlay)) | |
898 | (kill-region (overlay-start mouse-secondary-overlay) | |
899 | (overlay-end mouse-secondary-overlay)))) | |
e66feb07 | 900 | (delete-overlay mouse-secondary-overlay) |
9dfab550 | 901 | ;;; (x-set-selection 'SECONDARY nil) |
e66feb07 RS |
902 | (setq mouse-secondary-overlay nil)) |
903 | ||
904 | (defun mouse-secondary-save-then-kill (click) | |
d89a4a47 | 905 | "Save text to point in kill ring; the second time, kill the text. |
7bbe2cc7 RS |
906 | You must use this in a buffer where you have recently done \\[mouse-start-secondary]. |
907 | If the text between where you did \\[mouse-start-secondary] and where | |
908 | you use this command matches the text at the front of the kill ring, | |
909 | this command deletes the text. | |
e66feb07 | 910 | Otherwise, it adds the text to the kill ring, like \\[kill-ring-save], |
7bbe2cc7 | 911 | which prepares for a second click with this command to delete the text. |
d89a4a47 | 912 | |
7bbe2cc7 RS |
913 | If you have already made a secondary selection in that buffer, |
914 | this command extends or retracts the selection to where you click. | |
915 | If you do this again in a different position, it extends or retracts | |
916 | again. If you do this twice in the same position, it kills the selection." | |
e66feb07 | 917 | (interactive "e") |
d65147f6 | 918 | (mouse-minibuffer-check click) |
d89a4a47 RS |
919 | (let ((posn (event-start click)) |
920 | (click-posn (posn-point (event-start click))) | |
e66feb07 RS |
921 | ;; Don't let a subsequent kill command append to this one: |
922 | ;; prevent setting this-command to kill-region. | |
923 | (this-command this-command)) | |
9a974c88 RS |
924 | (or (eq (window-buffer (posn-window posn)) |
925 | (or (and mouse-secondary-overlay | |
926 | (overlay-buffer mouse-secondary-overlay)) | |
927 | (if mouse-secondary-start | |
928 | (marker-buffer mouse-secondary-start)))) | |
929 | (error "Wrong buffer")) | |
930 | (save-excursion | |
931 | (set-buffer (window-buffer (posn-window posn))) | |
a94c7fcc | 932 | (if (> (mod mouse-secondary-click-count 3) 0) |
9a974c88 RS |
933 | (if (not (and (eq last-command 'mouse-secondary-save-then-kill) |
934 | (equal click-posn | |
935 | (car (cdr-safe (cdr-safe mouse-save-then-kill-posn)))))) | |
936 | ;; Find both ends of the object selected by this click. | |
937 | (let* ((range | |
938 | (mouse-start-end click-posn click-posn | |
a94c7fcc | 939 | mouse-secondary-click-count))) |
9a974c88 RS |
940 | ;; Move whichever end is closer to the click. |
941 | ;; That's what xterm does, and it seems reasonable. | |
942 | (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay))) | |
943 | (abs (- click-posn (overlay-end mouse-secondary-overlay)))) | |
944 | (move-overlay mouse-secondary-overlay (car range) | |
945 | (overlay-end mouse-secondary-overlay)) | |
d89a4a47 RS |
946 | (move-overlay mouse-secondary-overlay |
947 | (overlay-start mouse-secondary-overlay) | |
948 | (nth 1 range))) | |
9a974c88 RS |
949 | ;; We have already put the old region in the kill ring. |
950 | ;; Replace it with the extended region. | |
951 | ;; (It would be annoying to make a separate entry.) | |
f69140fd KH |
952 | (kill-new (buffer-substring |
953 | (overlay-start mouse-secondary-overlay) | |
954 | (overlay-end mouse-secondary-overlay)) t) | |
9a974c88 RS |
955 | ;; Arrange for a repeated mouse-3 to kill this region. |
956 | (setq mouse-save-then-kill-posn | |
957 | (list (car kill-ring) (point) click-posn))) | |
958 | ;; If we click this button again without moving it, | |
959 | ;; that time kill. | |
d89a4a47 | 960 | (progn |
9a974c88 RS |
961 | (mouse-save-then-kill-delete-region |
962 | (overlay-start mouse-secondary-overlay) | |
963 | (overlay-end mouse-secondary-overlay)) | |
964 | (setq mouse-save-then-kill-posn nil) | |
a94c7fcc | 965 | (setq mouse-secondary-click-count 0) |
9a974c88 RS |
966 | (delete-overlay mouse-secondary-overlay))) |
967 | (if (and (eq last-command 'mouse-secondary-save-then-kill) | |
968 | mouse-save-then-kill-posn | |
969 | (eq (car mouse-save-then-kill-posn) (car kill-ring)) | |
970 | (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn))) | |
971 | ;; If this is the second time we've called | |
972 | ;; mouse-secondary-save-then-kill, delete the text from the buffer. | |
973 | (progn | |
974 | (mouse-save-then-kill-delete-region | |
975 | (overlay-start mouse-secondary-overlay) | |
976 | (overlay-end mouse-secondary-overlay)) | |
977 | (setq mouse-save-then-kill-posn nil) | |
978 | (delete-overlay mouse-secondary-overlay)) | |
979 | (if (overlay-start mouse-secondary-overlay) | |
980 | ;; We have a selection, so adjust it. | |
981 | (progn | |
982 | (if (numberp click-posn) | |
983 | (progn | |
984 | ;; Move whichever end of the region is closer to the click. | |
985 | ;; That is what xterm does, and it seems reasonable. | |
986 | (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay))) | |
987 | (abs (- click-posn (overlay-end mouse-secondary-overlay)))) | |
988 | (move-overlay mouse-secondary-overlay click-posn | |
989 | (overlay-end mouse-secondary-overlay)) | |
d89a4a47 RS |
990 | (move-overlay mouse-secondary-overlay |
991 | (overlay-start mouse-secondary-overlay) | |
992 | click-posn)) | |
9a974c88 | 993 | (setq deactivate-mark nil))) |
0136e1e3 | 994 | (if (eq last-command 'mouse-secondary-save-then-kill) |
f69140fd KH |
995 | ;; If the front of the kill ring comes from |
996 | ;; an immediately previous use of this command, | |
997 | ;; replace it with the extended region. | |
998 | ;; (It would be annoying to make a separate entry.) | |
999 | (kill-new (buffer-substring | |
0136e1e3 | 1000 | (overlay-start mouse-secondary-overlay) |
f69140fd | 1001 | (overlay-end mouse-secondary-overlay)) t) |
0136e1e3 RS |
1002 | (copy-region-as-kill (overlay-start mouse-secondary-overlay) |
1003 | (overlay-end mouse-secondary-overlay)))) | |
9a974c88 RS |
1004 | (if mouse-secondary-start |
1005 | ;; All we have is one end of a selection, | |
1006 | ;; so put the other end here. | |
1007 | (let ((start (+ 0 mouse-secondary-start))) | |
1008 | (kill-ring-save start click-posn) | |
1009 | (if mouse-secondary-overlay | |
1010 | (move-overlay mouse-secondary-overlay start click-posn) | |
1011 | (setq mouse-secondary-overlay (make-overlay start click-posn))) | |
1012 | (overlay-put mouse-secondary-overlay 'face 'secondary-selection)))) | |
1013 | (setq mouse-save-then-kill-posn | |
1014 | (list (car kill-ring) (point) click-posn)))) | |
fb28fd5b RS |
1015 | (if (overlay-buffer mouse-secondary-overlay) |
1016 | (x-set-selection 'SECONDARY | |
9a974c88 RS |
1017 | (buffer-substring |
1018 | (overlay-start mouse-secondary-overlay) | |
1019 | (overlay-end mouse-secondary-overlay))))))) | |
e66feb07 | 1020 | \f |
8b34e79d | 1021 | (defun mouse-buffer-menu (event) |
2d82f7b9 RS |
1022 | "Pop up a menu of buffers for selection with the mouse. |
1023 | This switches buffers in the window that you clicked on, | |
1024 | and selects that window." | |
ec558adc | 1025 | (interactive "e") |
d65147f6 | 1026 | (mouse-minibuffer-check event) |
8b34e79d RS |
1027 | (let ((menu |
1028 | (list "Buffer Menu" | |
1029 | (cons "Select Buffer" | |
1030 | (let ((tail (buffer-list)) | |
af2a85fe | 1031 | (maxbuf 0) |
8b34e79d | 1032 | head) |
af2a85fe RS |
1033 | (while tail |
1034 | (or (eq ?\ (aref (buffer-name (car tail)) 0)) | |
1035 | (setq maxbuf | |
1036 | (max maxbuf | |
1037 | (length (buffer-name (car tail)))))) | |
1038 | (setq tail (cdr tail))) | |
1039 | (setq tail (buffer-list)) | |
8b34e79d RS |
1040 | (while tail |
1041 | (let ((elt (car tail))) | |
1042 | (if (not (string-match "^ " | |
1043 | (buffer-name elt))) | |
95e5bde9 SM |
1044 | (setq head |
1045 | (cons | |
1046 | (cons | |
1047 | (format | |
1048 | (format "%%%ds %%s%%s %%s" maxbuf) | |
1049 | (buffer-name elt) | |
1050 | (if (buffer-modified-p elt) "*" " ") | |
1051 | (save-excursion | |
1052 | (set-buffer elt) | |
1053 | (if buffer-read-only "%" " ")) | |
1054 | (or (buffer-file-name elt) | |
1055 | (save-excursion | |
1056 | (set-buffer elt) | |
1057 | (if list-buffers-directory | |
1058 | (expand-file-name | |
1059 | list-buffers-directory))) | |
1060 | "")) | |
1061 | elt) | |
1062 | head)))) | |
8b34e79d RS |
1063 | (setq tail (cdr tail))) |
1064 | (reverse head)))))) | |
2d82f7b9 RS |
1065 | (let ((buf (x-popup-menu event menu)) |
1066 | (window (posn-window (event-start event)))) | |
1067 | (if buf | |
1068 | (progn | |
c0bb9f3b | 1069 | (or (framep window) (select-window window)) |
2d82f7b9 | 1070 | (switch-to-buffer buf)))))) |
72ea54a4 | 1071 | \f |
5ba2dc3f | 1072 | ;;; These need to be rewritten for the new scroll bar implementation. |
dbc4e1c1 JB |
1073 | |
1074 | ;;;!! ;; Commands for the scroll bar. | |
1075 | ;;;!! | |
1076 | ;;;!! (defun mouse-scroll-down (click) | |
1077 | ;;;!! (interactive "@e") | |
1078 | ;;;!! (scroll-down (1+ (cdr (mouse-coords click))))) | |
1079 | ;;;!! | |
1080 | ;;;!! (defun mouse-scroll-up (click) | |
1081 | ;;;!! (interactive "@e") | |
1082 | ;;;!! (scroll-up (1+ (cdr (mouse-coords click))))) | |
1083 | ;;;!! | |
1084 | ;;;!! (defun mouse-scroll-down-full () | |
1085 | ;;;!! (interactive "@") | |
1086 | ;;;!! (scroll-down nil)) | |
1087 | ;;;!! | |
1088 | ;;;!! (defun mouse-scroll-up-full () | |
1089 | ;;;!! (interactive "@") | |
1090 | ;;;!! (scroll-up nil)) | |
1091 | ;;;!! | |
1092 | ;;;!! (defun mouse-scroll-move-cursor (click) | |
1093 | ;;;!! (interactive "@e") | |
1094 | ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click))))) | |
1095 | ;;;!! | |
1096 | ;;;!! (defun mouse-scroll-absolute (event) | |
1097 | ;;;!! (interactive "@e") | |
1098 | ;;;!! (let* ((pos (car event)) | |
1099 | ;;;!! (position (car pos)) | |
1100 | ;;;!! (length (car (cdr pos)))) | |
1101 | ;;;!! (if (<= length 0) (setq length 1)) | |
1102 | ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size))))) | |
1103 | ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor) | |
1104 | ;;;!! position) | |
1105 | ;;;!! length) | |
1106 | ;;;!! scale-factor))) | |
1107 | ;;;!! (goto-char newpos) | |
1108 | ;;;!! (recenter '(4))))) | |
1109 | ;;;!! | |
1110 | ;;;!! (defun mouse-scroll-left (click) | |
1111 | ;;;!! (interactive "@e") | |
1112 | ;;;!! (scroll-left (1+ (car (mouse-coords click))))) | |
1113 | ;;;!! | |
1114 | ;;;!! (defun mouse-scroll-right (click) | |
1115 | ;;;!! (interactive "@e") | |
1116 | ;;;!! (scroll-right (1+ (car (mouse-coords click))))) | |
1117 | ;;;!! | |
1118 | ;;;!! (defun mouse-scroll-left-full () | |
1119 | ;;;!! (interactive "@") | |
1120 | ;;;!! (scroll-left nil)) | |
1121 | ;;;!! | |
1122 | ;;;!! (defun mouse-scroll-right-full () | |
1123 | ;;;!! (interactive "@") | |
1124 | ;;;!! (scroll-right nil)) | |
1125 | ;;;!! | |
1126 | ;;;!! (defun mouse-scroll-move-cursor-horizontally (click) | |
1127 | ;;;!! (interactive "@e") | |
1128 | ;;;!! (move-to-column (1+ (car (mouse-coords click))))) | |
1129 | ;;;!! | |
1130 | ;;;!! (defun mouse-scroll-absolute-horizontally (event) | |
1131 | ;;;!! (interactive "@e") | |
1132 | ;;;!! (let* ((pos (car event)) | |
1133 | ;;;!! (position (car pos)) | |
1134 | ;;;!! (length (car (cdr pos)))) | |
1135 | ;;;!! (set-window-hscroll (selected-window) 33))) | |
1136 | ;;;!! | |
1137 | ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up) | |
1138 | ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute) | |
1139 | ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down) | |
1140 | ;;;!! | |
1141 | ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor) | |
1142 | ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor) | |
1143 | ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor) | |
1144 | ;;;!! | |
1145 | ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full) | |
1146 | ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full) | |
1147 | ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full) | |
1148 | ;;;!! | |
1149 | ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full) | |
1150 | ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full) | |
1151 | ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full) | |
1152 | ;;;!! | |
1153 | ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left) | |
1154 | ;;;!! (global-set-key [horizontal-scroll-bar mouse-2] | |
1155 | ;;;!! 'mouse-scroll-absolute-horizontally) | |
1156 | ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right) | |
1157 | ;;;!! | |
1158 | ;;;!! (global-set-key [horizontal-slider mouse-1] | |
1159 | ;;;!! 'mouse-scroll-move-cursor-horizontally) | |
1160 | ;;;!! (global-set-key [horizontal-slider mouse-2] | |
1161 | ;;;!! 'mouse-scroll-move-cursor-horizontally) | |
1162 | ;;;!! (global-set-key [horizontal-slider mouse-3] | |
1163 | ;;;!! 'mouse-scroll-move-cursor-horizontally) | |
1164 | ;;;!! | |
1165 | ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full) | |
1166 | ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full) | |
1167 | ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full) | |
1168 | ;;;!! | |
1169 | ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full) | |
1170 | ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full) | |
1171 | ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full) | |
1172 | ;;;!! | |
1173 | ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2] | |
1174 | ;;;!! 'mouse-split-window-horizontally) | |
1175 | ;;;!! (global-set-key [mode-line S-mouse-2] | |
1176 | ;;;!! 'mouse-split-window-horizontally) | |
1177 | ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2] | |
1178 | ;;;!! 'mouse-split-window) | |
6b2154de | 1179 | \f |
dbc4e1c1 JB |
1180 | ;;;!! ;;;; |
1181 | ;;;!! ;;;; Here are experimental things being tested. Mouse events | |
1182 | ;;;!! ;;;; are of the form: | |
1183 | ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp) | |
1184 | ;;;!! ;; | |
1185 | ;;;!! ;;;; | |
1186 | ;;;!! ;;;; Dynamically track mouse coordinates | |
1187 | ;;;!! ;;;; | |
1188 | ;;;!! ;; | |
1189 | ;;;!! ;;(defun track-mouse (event) | |
1190 | ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse." | |
1191 | ;;;!! ;; (interactive "@e") | |
1192 | ;;;!! ;; (while mouse-grabbed | |
1193 | ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen))) | |
1194 | ;;;!! ;; (abs-x (car pos)) | |
1195 | ;;;!! ;; (abs-y (cdr pos)) | |
1196 | ;;;!! ;; (relative-coordinate (coordinates-in-window-p | |
1197 | ;;;!! ;; (list (car pos) (cdr pos)) | |
1198 | ;;;!! ;; (selected-window)))) | |
1199 | ;;;!! ;; (if (consp relative-coordinate) | |
1200 | ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y | |
1201 | ;;;!! ;; (car relative-coordinate) | |
1202 | ;;;!! ;; (car (cdr relative-coordinate))) | |
1203 | ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y))))) | |
1204 | ;;;!! | |
1205 | ;;;!! ;; | |
1206 | ;;;!! ;; Dynamically put a box around the line indicated by point | |
1207 | ;;;!! ;; | |
1208 | ;;;!! ;; | |
1209 | ;;;!! ;;(require 'backquote) | |
1210 | ;;;!! ;; | |
1211 | ;;;!! ;;(defun mouse-select-buffer-line (event) | |
1212 | ;;;!! ;; (interactive "@e") | |
1213 | ;;;!! ;; (let ((relative-coordinate | |
1214 | ;;;!! ;; (coordinates-in-window-p (car event) (selected-window))) | |
1215 | ;;;!! ;; (abs-y (car (cdr (car event))))) | |
1216 | ;;;!! ;; (if (consp relative-coordinate) | |
1217 | ;;;!! ;; (progn | |
1218 | ;;;!! ;; (save-excursion | |
1219 | ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate))) | |
1220 | ;;;!! ;; (x-draw-rectangle | |
1221 | ;;;!! ;; (selected-screen) | |
1222 | ;;;!! ;; abs-y 0 | |
1223 | ;;;!! ;; (save-excursion | |
1224 | ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate))) | |
1225 | ;;;!! ;; (end-of-line) | |
1226 | ;;;!! ;; (push-mark nil t) | |
1227 | ;;;!! ;; (beginning-of-line) | |
1228 | ;;;!! ;; (- (region-end) (region-beginning))) 1)) | |
1229 | ;;;!! ;; (sit-for 1) | |
1230 | ;;;!! ;; (x-erase-rectangle (selected-screen)))))) | |
1231 | ;;;!! ;; | |
1232 | ;;;!! ;;(defvar last-line-drawn nil) | |
1233 | ;;;!! ;;(defvar begin-delim "[^ \t]") | |
1234 | ;;;!! ;;(defvar end-delim "[^ \t]") | |
1235 | ;;;!! ;; | |
1236 | ;;;!! ;;(defun mouse-boxing (event) | |
1237 | ;;;!! ;; (interactive "@e") | |
1238 | ;;;!! ;; (save-excursion | |
1239 | ;;;!! ;; (let ((screen (selected-screen))) | |
1240 | ;;;!! ;; (while (= (x-mouse-events) 0) | |
1241 | ;;;!! ;; (let* ((pos (read-mouse-position screen)) | |
1242 | ;;;!! ;; (abs-x (car pos)) | |
1243 | ;;;!! ;; (abs-y (cdr pos)) | |
1244 | ;;;!! ;; (relative-coordinate | |
1245 | ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y))) | |
1246 | ;;;!! ;; (selected-window))) | |
1247 | ;;;!! ;; (begin-reg nil) | |
1248 | ;;;!! ;; (end-reg nil) | |
1249 | ;;;!! ;; (end-column nil) | |
1250 | ;;;!! ;; (begin-column nil)) | |
1251 | ;;;!! ;; (if (and (consp relative-coordinate) | |
1252 | ;;;!! ;; (or (not last-line-drawn) | |
1253 | ;;;!! ;; (not (= last-line-drawn abs-y)))) | |
1254 | ;;;!! ;; (progn | |
1255 | ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate))) | |
1256 | ;;;!! ;; (if (= (following-char) 10) | |
1257 | ;;;!! ;; () | |
1258 | ;;;!! ;; (progn | |
1259 | ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim))) | |
1260 | ;;;!! ;; (setq begin-column (1- (current-column))) | |
1261 | ;;;!! ;; (end-of-line) | |
1262 | ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim))) | |
1263 | ;;;!! ;; (setq end-column (1+ (current-column))) | |
1264 | ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg)) | |
1265 | ;;;!! ;; (x-draw-rectangle screen | |
1266 | ;;;!! ;; (setq last-line-drawn abs-y) | |
1267 | ;;;!! ;; begin-column | |
1268 | ;;;!! ;; (- end-column begin-column) 1)))))))))) | |
1269 | ;;;!! ;; | |
1270 | ;;;!! ;;(defun mouse-erase-box () | |
1271 | ;;;!! ;; (interactive) | |
1272 | ;;;!! ;; (if last-line-drawn | |
1273 | ;;;!! ;; (progn | |
1274 | ;;;!! ;; (x-erase-rectangle (selected-screen)) | |
1275 | ;;;!! ;; (setq last-line-drawn nil)))) | |
1276 | ;;;!! | |
1277 | ;;;!! ;;; (defun test-x-rectangle () | |
1278 | ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap))) | |
1279 | ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing) | |
1280 | ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box)) | |
1281 | ;;;!! | |
1282 | ;;;!! ;; | |
1283 | ;;;!! ;; Here is how to do double clicking in lisp. About to change. | |
1284 | ;;;!! ;; | |
1285 | ;;;!! | |
1286 | ;;;!! (defvar double-start nil) | |
1287 | ;;;!! (defconst double-click-interval 300 | |
1288 | ;;;!! "Max ticks between clicks") | |
1289 | ;;;!! | |
1290 | ;;;!! (defun double-down (event) | |
1291 | ;;;!! (interactive "@e") | |
1292 | ;;;!! (if double-start | |
1293 | ;;;!! (let ((interval (- (nth 4 event) double-start))) | |
1294 | ;;;!! (if (< interval double-click-interval) | |
1295 | ;;;!! (progn | |
1296 | ;;;!! (backward-up-list 1) | |
1297 | ;;;!! ;; (message "Interval %d" interval) | |
1298 | ;;;!! (sleep-for 1))) | |
1299 | ;;;!! (setq double-start nil)) | |
1300 | ;;;!! (setq double-start (nth 4 event)))) | |
1301 | ;;;!! | |
1302 | ;;;!! (defun double-up (event) | |
1303 | ;;;!! (interactive "@e") | |
1304 | ;;;!! (and double-start | |
1305 | ;;;!! (> (- (nth 4 event ) double-start) double-click-interval) | |
1306 | ;;;!! (setq double-start nil))) | |
1307 | ;;;!! | |
1308 | ;;;!! ;;; (defun x-test-doubleclick () | |
1309 | ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap))) | |
1310 | ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down) | |
1311 | ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up)) | |
1312 | ;;;!! | |
1313 | ;;;!! ;; | |
5ba2dc3f | 1314 | ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar. |
dbc4e1c1 JB |
1315 | ;;;!! ;; |
1316 | ;;;!! | |
1317 | ;;;!! (defvar scrolled-lines 0) | |
1318 | ;;;!! (defconst scroll-speed 1) | |
1319 | ;;;!! | |
1320 | ;;;!! (defun incr-scroll-down (event) | |
1321 | ;;;!! (interactive "@e") | |
1322 | ;;;!! (setq scrolled-lines 0) | |
1323 | ;;;!! (incremental-scroll scroll-speed)) | |
1324 | ;;;!! | |
1325 | ;;;!! (defun incr-scroll-up (event) | |
1326 | ;;;!! (interactive "@e") | |
1327 | ;;;!! (setq scrolled-lines 0) | |
1328 | ;;;!! (incremental-scroll (- scroll-speed))) | |
1329 | ;;;!! | |
1330 | ;;;!! (defun incremental-scroll (n) | |
1331 | ;;;!! (while (= (x-mouse-events) 0) | |
1332 | ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines))) | |
1333 | ;;;!! (scroll-down n) | |
1334 | ;;;!! (sit-for 300 t))) | |
1335 | ;;;!! | |
1336 | ;;;!! (defun incr-scroll-stop (event) | |
1337 | ;;;!! (interactive "@e") | |
1338 | ;;;!! (message "Scrolled %d lines" scrolled-lines) | |
1339 | ;;;!! (setq scrolled-lines 0) | |
1340 | ;;;!! (sleep-for 1)) | |
1341 | ;;;!! | |
1342 | ;;;!! ;;; (defun x-testing-scroll () | |
1343 | ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix))) | |
1344 | ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down) | |
1345 | ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up) | |
1346 | ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop) | |
1347 | ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop))) | |
1348 | ;;;!! | |
1349 | ;;;!! ;; | |
1350 | ;;;!! ;; Some playthings suitable for picture mode? They need work. | |
1351 | ;;;!! ;; | |
1352 | ;;;!! | |
1353 | ;;;!! (defun mouse-kill-rectangle (event) | |
1354 | ;;;!! "Kill the rectangle between point and the mouse cursor." | |
1355 | ;;;!! (interactive "@e") | |
1356 | ;;;!! (let ((point-save (point))) | |
1357 | ;;;!! (save-excursion | |
1358 | ;;;!! (mouse-set-point event) | |
1359 | ;;;!! (push-mark nil t) | |
1360 | ;;;!! (if (> point-save (point)) | |
1361 | ;;;!! (kill-rectangle (point) point-save) | |
1362 | ;;;!! (kill-rectangle point-save (point)))))) | |
1363 | ;;;!! | |
1364 | ;;;!! (defun mouse-open-rectangle (event) | |
1365 | ;;;!! "Kill the rectangle between point and the mouse cursor." | |
1366 | ;;;!! (interactive "@e") | |
1367 | ;;;!! (let ((point-save (point))) | |
1368 | ;;;!! (save-excursion | |
1369 | ;;;!! (mouse-set-point event) | |
1370 | ;;;!! (push-mark nil t) | |
1371 | ;;;!! (if (> point-save (point)) | |
1372 | ;;;!! (open-rectangle (point) point-save) | |
1373 | ;;;!! (open-rectangle point-save (point)))))) | |
1374 | ;;;!! | |
1375 | ;;;!! ;; Must be a better way to do this. | |
1376 | ;;;!! | |
1377 | ;;;!! (defun mouse-multiple-insert (n char) | |
1378 | ;;;!! (while (> n 0) | |
1379 | ;;;!! (insert char) | |
1380 | ;;;!! (setq n (1- n)))) | |
1381 | ;;;!! | |
1382 | ;;;!! ;; What this could do is not finalize until button was released. | |
1383 | ;;;!! | |
1384 | ;;;!! (defun mouse-move-text (event) | |
1385 | ;;;!! "Move text from point to cursor position, inserting spaces." | |
1386 | ;;;!! (interactive "@e") | |
1387 | ;;;!! (let* ((relative-coordinate | |
1388 | ;;;!! (coordinates-in-window-p (car event) (selected-window)))) | |
1389 | ;;;!! (if (consp relative-coordinate) | |
1390 | ;;;!! (cond ((> (current-column) (car relative-coordinate)) | |
1391 | ;;;!! (delete-char | |
1392 | ;;;!! (- (car relative-coordinate) (current-column)))) | |
1393 | ;;;!! ((< (current-column) (car relative-coordinate)) | |
1394 | ;;;!! (mouse-multiple-insert | |
1395 | ;;;!! (- (car relative-coordinate) (current-column)) " ")) | |
1396 | ;;;!! ((= (current-column) (car relative-coordinate)) (ding)))))) | |
07a78410 | 1397 | \f |
f936ae06 RS |
1398 | ;; Choose a completion with the mouse. |
1399 | ||
1400 | (defun mouse-choose-completion (event) | |
49e61c42 | 1401 | "Click on an alternative in the `*Completions*' buffer to choose it." |
f936ae06 | 1402 | (interactive "e") |
33c448cd RS |
1403 | ;; Give temporary modes such as isearch a chance to turn off. |
1404 | (run-hooks 'mouse-leave-buffer-hook) | |
d89a4a47 | 1405 | (let ((buffer (window-buffer)) |
02680e9b RS |
1406 | choice |
1407 | base-size) | |
f936ae06 RS |
1408 | (save-excursion |
1409 | (set-buffer (window-buffer (posn-window (event-start event)))) | |
f36f4e9e RS |
1410 | (if completion-reference-buffer |
1411 | (setq buffer completion-reference-buffer)) | |
02680e9b | 1412 | (setq base-size completion-base-size) |
f936ae06 RS |
1413 | (save-excursion |
1414 | (goto-char (posn-point (event-start event))) | |
b6be5d95 | 1415 | (let (beg end) |
9a43a594 RS |
1416 | (if (and (not (eobp)) (get-text-property (point) 'mouse-face)) |
1417 | (setq end (point) beg (1+ (point)))) | |
1418 | (if (null beg) | |
1419 | (error "No completion here")) | |
1420 | (setq beg (previous-single-property-change beg 'mouse-face)) | |
2f5ed2e8 RS |
1421 | (setq end (or (next-single-property-change end 'mouse-face) |
1422 | (point-max))) | |
b6be5d95 | 1423 | (setq choice (buffer-substring beg end))))) |
73e2025f RS |
1424 | (let ((owindow (selected-window))) |
1425 | (select-window (posn-window (event-start event))) | |
874a2cbd RS |
1426 | (if (and (one-window-p t 'selected-frame) |
1427 | (window-dedicated-p (selected-window))) | |
1428 | ;; This is a special buffer's frame | |
1429 | (iconify-frame (selected-frame)) | |
1430 | (or (window-dedicated-p (selected-window)) | |
1431 | (bury-buffer))) | |
73e2025f | 1432 | (select-window owindow)) |
02680e9b | 1433 | (choose-completion-string choice buffer base-size))) |
f936ae06 | 1434 | \f |
07a78410 RS |
1435 | ;; Font selection. |
1436 | ||
0eb9fef3 RS |
1437 | (defun font-menu-add-default () |
1438 | (let* ((default (cdr (assq 'font (frame-parameters (selected-frame))))) | |
1439 | (font-alist x-fixed-font-alist) | |
0d94f5ca | 1440 | (elt (or (assoc "Misc" font-alist) (nth 1 font-alist)))) |
0eb9fef3 RS |
1441 | (if (assoc "Default" elt) |
1442 | (delete (assoc "Default" elt) elt)) | |
1443 | (setcdr elt | |
3d64d15b | 1444 | (cons (list "Default" |
0eb9fef3 RS |
1445 | (cdr (assq 'font (frame-parameters (selected-frame))))) |
1446 | (cdr elt))))) | |
1447 | ||
07a78410 RS |
1448 | (defvar x-fixed-font-alist |
1449 | '("Font menu" | |
1450 | ("Misc" | |
19d973e8 RS |
1451 | ;; For these, we specify the pixel height and width. |
1452 | ("fixed" "fixed") | |
1453 | ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10") | |
1454 | ("6x12" | |
1455 | "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12") | |
1456 | ("6x13" | |
1457 | "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13") | |
1458 | ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13") | |
1459 | ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14") | |
1460 | ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13") | |
1461 | ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15") | |
1462 | ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20") | |
1463 | ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18") | |
1464 | ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24") | |
fa21fdec | 1465 | ("") |
f29c3695 RS |
1466 | ("clean 5x8" |
1467 | "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1") | |
1468 | ("clean 6x8" | |
1469 | "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1") | |
19d973e8 RS |
1470 | ("clean 8x8" |
1471 | "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1") | |
1472 | ("clean 8x10" | |
1473 | "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1") | |
1474 | ("clean 8x14" | |
1475 | "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1") | |
1476 | ("clean 8x16" | |
1477 | "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1") | |
fa21fdec | 1478 | ("") |
19d973e8 | 1479 | ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")) |
07a78410 RS |
1480 | ;;; We don't seem to have these; who knows what they are. |
1481 | ;;; ("fg-18" "fg-18") | |
1482 | ;;; ("fg-25" "fg-25") | |
1483 | ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12") | |
1484 | ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14") | |
1485 | ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24") | |
1486 | ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1") | |
1487 | ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*") | |
1488 | ("Courier" | |
19d973e8 | 1489 | ;; For these, we specify the point height. |
82c048a9 RS |
1490 | ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1") |
1491 | ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1") | |
1492 | ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1") | |
1493 | ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1") | |
1494 | ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1") | |
1495 | ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1") | |
1496 | ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1") | |
1497 | ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1") | |
1498 | ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1") | |
1499 | ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1") | |
1500 | ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1") | |
1501 | ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1") | |
1502 | ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1") | |
1503 | ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1") | |
1504 | ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1") | |
1505 | ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1") | |
1506 | ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1") | |
1507 | ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1") | |
1508 | ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1") | |
1509 | ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1") | |
1510 | ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1") | |
1511 | ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1") | |
1512 | ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1") | |
1513 | ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")) | |
07a78410 RS |
1514 | ) |
1515 | "X fonts suitable for use in Emacs.") | |
1516 | ||
1900a92b | 1517 | (defun mouse-set-font (&rest fonts) |
07a78410 RS |
1518 | "Select an emacs font from a list of known good fonts" |
1519 | (interactive | |
1520 | (x-popup-menu last-nonmenu-event x-fixed-font-alist)) | |
df4de8c6 KH |
1521 | (if fonts |
1522 | (let (font) | |
1523 | (while fonts | |
1524 | (condition-case nil | |
1525 | (progn | |
3fadec1a | 1526 | (set-default-font (car fonts)) |
df4de8c6 KH |
1527 | (setq font (car fonts)) |
1528 | (setq fonts nil)) | |
3fadec1a RS |
1529 | (error |
1530 | (setq fonts (cdr fonts))))) | |
df4de8c6 | 1531 | (if (null font) |
3fadec1a | 1532 | (error "Font not found"))))) |
cc0a8174 JB |
1533 | \f |
1534 | ;;; Bindings for mouse commands. | |
1535 | ||
fcfc3c63 | 1536 | (define-key global-map [down-mouse-1] 'mouse-drag-region) |
dbc4e1c1 | 1537 | (global-set-key [mouse-1] 'mouse-set-point) |
dbc4e1c1 | 1538 | (global-set-key [drag-mouse-1] 'mouse-set-region) |
fcfc3c63 | 1539 | |
e37de120 RS |
1540 | ;; These are tested for in mouse-drag-region. |
1541 | (global-set-key [double-mouse-1] 'mouse-set-point) | |
1542 | (global-set-key [triple-mouse-1] 'mouse-set-point) | |
1543 | ||
dbc4e1c1 JB |
1544 | (global-set-key [mouse-2] 'mouse-yank-at-click) |
1545 | (global-set-key [mouse-3] 'mouse-save-then-kill) | |
8b34e79d | 1546 | |
dbc4e1c1 JB |
1547 | ;; By binding these to down-going events, we let the user use the up-going |
1548 | ;; event to make the selection, saving a click. | |
eef805d7 RS |
1549 | (global-set-key [C-down-mouse-1] 'mouse-set-font) |
1550 | ;; C-down-mouse-2 is bound in facemenu.el. | |
95132d1c RS |
1551 | (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu) |
1552 | ||
07a78410 | 1553 | |
8b34e79d RS |
1554 | ;; Replaced with dragging mouse-1 |
1555 | ;; (global-set-key [S-mouse-1] 'mouse-set-mark) | |
947da0c4 | 1556 | |
3c2dd2c0 | 1557 | (global-set-key [mode-line mouse-1] 'mouse-select-window) |
544e7e73 RS |
1558 | (global-set-key [mode-line drag-mouse-1] 'mouse-select-window) |
1559 | (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line) | |
3c2dd2c0 | 1560 | (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows) |
dbc4e1c1 | 1561 | (global-set-key [mode-line mouse-3] 'mouse-delete-window) |
3c2dd2c0 | 1562 | (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally) |
9926ab64 | 1563 | (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically) |
b6522df6 | 1564 | (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically) |
49116ac0 JB |
1565 | |
1566 | (provide 'mouse) | |
1567 | ||
6594deb0 | 1568 | ;;; mouse.el ends here |