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