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