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