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