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