(propertized-buffer-identification): Use face
[bpt/emacs.git] / lisp / mouse.el
1 ;;; mouse.el --- window system-independent mouse support
2
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: hardware, mouse
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This package provides various useful commands (including help
29 ;; system access) through the mouse. All this code assumes that mouse
30 ;; interpretation has been abstracted into Emacs input events.
31 ;;
32 ;; The code is rather X-dependent.
33
34 ;;; Code:
35
36 ;;; Utility functions.
37
38 ;;; Indent track-mouse like progn.
39 (put 'track-mouse 'lisp-indent-function 0)
40
41 (defcustom mouse-yank-at-point nil
42 "*If non-nil, mouse yank commands yank at point instead of at click."
43 :type 'boolean
44 :group 'mouse)
45
46 (defcustom mouse-drag-copy-region t
47 "*If non-nil, mouse drag copies region to kill-ring."
48 :type 'boolean
49 :version "22.1"
50 :group 'mouse)
51
52 (defcustom mouse-1-click-follows-link 450
53 "Non-nil means that clicking Mouse-1 on a link follows the link.
54
55 With the default setting, an ordinary Mouse-1 click on a link
56 performs the same action as Mouse-2 on that link, while a longer
57 Mouse-1 click \(hold down the Mouse-1 button for more than 450
58 milliseconds) performs the original Mouse-1 binding \(which
59 typically sets point where you click the mouse).
60
61 If value is an integer, the time elapsed between pressing and
62 releasing the mouse button determines whether to follow the link
63 or perform the normal Mouse-1 action (typically set point).
64 The absolute numeric value specifices the maximum duration of a
65 \"short click\" in milliseconds. A positive value means that a
66 short click follows the link, and a longer click performs the
67 normal action. A negative value gives the opposite behavior.
68
69 If value is `double', a double click follows the link.
70
71 Otherwise, a single Mouse-1 click unconditionally follows the link.
72
73 Note that dragging the mouse never follows the link.
74
75 This feature only works in modes that specifically identify
76 clickable text as links, so it may not work with some external
77 packages. See `mouse-on-link-p' for details."
78 :version "22.1"
79 :type '(choice (const :tag "Disabled" nil)
80 (const :tag "Double click" double)
81 (number :tag "Single click time limit" :value 450)
82 (other :tag "Single click" t))
83 :group 'mouse)
84
85 (defcustom mouse-1-click-in-non-selected-windows t
86 "*If non-nil, a Mouse-1 click also follows links in non-selected windows.
87
88 If nil, a Mouse-1 click on a link in a non-selected window performs
89 the normal mouse-1 binding, typically selects the window and sets
90 point at the click position."
91 :type 'boolean
92 :version "22.1"
93 :group 'mouse)
94
95
96 \f
97 ;; Provide a mode-specific menu on a mouse button.
98
99 (defun popup-menu (menu &optional position prefix)
100 "Popup the given menu and call the selected option.
101 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
102 `x-popup-menu'.
103 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
104 the current mouse position.
105 PREFIX is the prefix argument (if any) to pass to the command."
106 (let* ((map (cond
107 ((keymapp menu) menu)
108 ((and (listp menu) (keymapp (car menu))) menu)
109 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
110 (filter (when (symbolp map)
111 (plist-get (get map 'menu-prop) :filter))))
112 (if filter (funcall filter (symbol-function map)) map)))))
113 event cmd)
114 (unless position
115 (let ((mp (mouse-pixel-position)))
116 (setq position (list (list (cadr mp) (cddr mp)) (car mp)))))
117 ;; The looping behavior was taken from lmenu's popup-menu-popup
118 (while (and map (setq event
119 ;; map could be a prefix key, in which case
120 ;; we need to get its function cell
121 ;; definition.
122 (x-popup-menu position (indirect-function map))))
123 ;; Strangely x-popup-menu returns a list.
124 ;; mouse-major-mode-menu was using a weird:
125 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
126 (setq cmd
127 (if (and (not (keymapp map)) (listp map))
128 ;; We were given a list of keymaps. Search them all
129 ;; in sequence until a first binding is found.
130 (let ((mouse-click (apply 'vector event))
131 binding)
132 (while (and map (null binding))
133 (setq binding (lookup-key (car map) mouse-click))
134 (if (numberp binding) ; `too long'
135 (setq binding nil))
136 (setq map (cdr map)))
137 binding)
138 ;; We were given a single keymap.
139 (lookup-key map (apply 'vector event))))
140 ;; Clear out echoing, which perhaps shows a prefix arg.
141 (message "")
142 ;; Maybe try again but with the submap.
143 (setq map (if (keymapp cmd) cmd)))
144 ;; If the user did not cancel by refusing to select,
145 ;; and if the result is a command, run it.
146 (when (and (null map) (commandp cmd))
147 (setq prefix-arg prefix)
148 ;; `setup-specified-language-environment', for instance,
149 ;; expects this to be set from a menu keymap.
150 (setq last-command-event (car (last event)))
151 ;; mouse-major-mode-menu was using `command-execute' instead.
152 (call-interactively cmd))))
153
154 (defvar mouse-major-mode-menu-prefix) ; dynamically bound
155
156 (defun mouse-major-mode-menu (event &optional prefix)
157 "Pop up a mode-specific menu of mouse commands.
158 Default to the Edit menu if the major mode doesn't define a menu."
159 ;; Switch to the window clicked on, because otherwise
160 ;; the mode's commands may not make sense.
161 (interactive "@e\nP")
162 ;; Let the mode update its menus first.
163 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
164 (let* (;; This is where mouse-major-mode-menu-prefix
165 ;; returns the prefix we should use (after menu-bar).
166 ;; It is either nil or (SOME-SYMBOL).
167 (mouse-major-mode-menu-prefix nil)
168 ;; Keymap from which to inherit; may be null.
169 (ancestor (mouse-major-mode-menu-1
170 (and (current-local-map)
171 (local-key-binding [menu-bar]))))
172 ;; Make a keymap in which our last command leads to a menu or
173 ;; default to the edit menu.
174 (newmap (if ancestor
175 (make-sparse-keymap (concat mode-name " Mode"))
176 menu-bar-edit-menu)))
177 (if ancestor
178 ;; Make our menu inherit from the desired keymap which we want
179 ;; to display as the menu now.
180 (set-keymap-parent newmap ancestor))
181 (popup-menu newmap event prefix)))
182
183
184 ;; Compute and cache the equivalent keys in MENU and all its submenus.
185 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
186 ;;; (and (eq (car menu) 'keymap)
187 ;;; (x-popup-menu nil menu))
188 ;;; (while menu
189 ;;; (and (consp (car menu))
190 ;;; (consp (cdr (car menu)))
191 ;;; (let ((tail (cdr (car menu))))
192 ;;; (while (and (consp tail)
193 ;;; (not (eq (car tail) 'keymap)))
194 ;;; (setq tail (cdr tail)))
195 ;;; (if (consp tail)
196 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
197 ;;; (setq menu (cdr menu))))
198
199 ;; Given a mode's menu bar keymap,
200 ;; if it defines exactly one menu bar menu,
201 ;; return just that menu.
202 ;; Otherwise return a menu for all of them.
203 (defun mouse-major-mode-menu-1 (menubar)
204 (if menubar
205 (let ((tail menubar)
206 submap)
207 (while tail
208 (if (consp (car tail))
209 (if submap
210 (setq submap t)
211 (setq submap (car tail))))
212 (setq tail (cdr tail)))
213 (if (eq submap t)
214 menubar
215 (setq mouse-major-mode-menu-prefix (list (car submap)))
216 (lookup-key menubar (vector (car submap)))))))
217
218 (defun mouse-popup-menubar (event prefix)
219 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
220 The contents are the items that would be in the menu bar whether or
221 not it is actually displayed."
222 (interactive "@e \nP")
223 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
224 (let* ((local-menu (and (current-local-map)
225 (lookup-key (current-local-map) [menu-bar])))
226 (global-menu (lookup-key global-map [menu-bar]))
227 ;; If a keymap doesn't have a prompt string (a lazy
228 ;; programmer didn't bother to provide one), create it and
229 ;; insert it into the keymap; each keymap gets its own
230 ;; prompt. This is required for non-toolkit versions to
231 ;; display non-empty menu pane names.
232 (minor-mode-menus
233 (mapcar
234 (function
235 (lambda (menu)
236 (let* ((minor-mode (car menu))
237 (menu (cdr menu))
238 (title-or-map (cadr menu)))
239 (or (stringp title-or-map)
240 (setq menu
241 (cons 'keymap
242 (cons (concat
243 (capitalize (subst-char-in-string
244 ?- ?\ (symbol-name
245 minor-mode)))
246 " Menu")
247 (cdr menu)))))
248 menu)))
249 (minor-mode-key-binding [menu-bar])))
250 (local-title-or-map (and local-menu (cadr local-menu)))
251 (global-title-or-map (cadr global-menu)))
252 (or (null local-menu)
253 (stringp local-title-or-map)
254 (setq local-menu (cons 'keymap
255 (cons (concat mode-name " Mode Menu")
256 (cdr local-menu)))))
257 (or (stringp global-title-or-map)
258 (setq global-menu (cons 'keymap
259 (cons "Global Menu"
260 (cdr global-menu)))))
261 ;; Supplying the list is faster than making a new map.
262 (popup-menu (append (list global-menu)
263 (if local-menu
264 (list local-menu))
265 minor-mode-menus)
266 event prefix)))
267
268 (defun mouse-popup-menubar-stuff (event prefix)
269 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
270 Use the former if the menu bar is showing, otherwise the latter."
271 (interactive "@e \nP")
272 (if (zerop (assoc-default 'menu-bar-lines (frame-parameters) 'eq 0))
273 (mouse-popup-menubar event prefix)
274 (mouse-major-mode-menu event prefix)))
275 \f
276 ;; Commands that operate on windows.
277
278 (defun mouse-minibuffer-check (event)
279 (let ((w (posn-window (event-start event))))
280 (and (window-minibuffer-p w)
281 (not (minibuffer-window-active-p w))
282 (error "Minibuffer window is not active")))
283 ;; Give temporary modes such as isearch a chance to turn off.
284 (run-hooks 'mouse-leave-buffer-hook))
285
286 (defun mouse-delete-window (click)
287 "Delete the window you click on.
288 Do nothing if the frame has just one window.
289 This command must be bound to a mouse click."
290 (interactive "e")
291 (unless (one-window-p t)
292 (mouse-minibuffer-check click)
293 (delete-window (posn-window (event-start click)))))
294
295 (defun mouse-select-window (click)
296 "Select the window clicked on; don't move point."
297 (interactive "e")
298 (mouse-minibuffer-check click)
299 (let ((oframe (selected-frame))
300 (frame (window-frame (posn-window (event-start click)))))
301 (select-window (posn-window (event-start click)))
302 (raise-frame frame)
303 (select-frame frame)
304 (or (eq frame oframe)
305 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
306
307 (defun mouse-tear-off-window (click)
308 "Delete the window clicked on, and create a new frame displaying its buffer."
309 (interactive "e")
310 (mouse-minibuffer-check click)
311 (let* ((window (posn-window (event-start click)))
312 (buf (window-buffer window))
313 (frame (make-frame)))
314 (select-frame frame)
315 (switch-to-buffer buf)
316 (delete-window window)))
317
318 (defun mouse-delete-other-windows ()
319 "Delete all windows except the one you click on."
320 (interactive "@")
321 (delete-other-windows))
322
323 (defun mouse-split-window-vertically (click)
324 "Select Emacs window mouse is on, then split it vertically in half.
325 The window is split at the line clicked on.
326 This command must be bound to a mouse click."
327 (interactive "@e")
328 (mouse-minibuffer-check click)
329 (let ((start (event-start click)))
330 (select-window (posn-window start))
331 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
332 (first-line window-min-height)
333 (last-line (- (window-height) window-min-height)))
334 (if (< last-line first-line)
335 (error "Window too short to split")
336 (split-window-vertically
337 (min (max new-height first-line) last-line))))))
338
339 (defun mouse-split-window-horizontally (click)
340 "Select Emacs window mouse is on, then split it horizontally in half.
341 The window is split at the column clicked on.
342 This command must be bound to a mouse click."
343 (interactive "@e")
344 (mouse-minibuffer-check click)
345 (let ((start (event-start click)))
346 (select-window (posn-window start))
347 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
348 (first-col window-min-width)
349 (last-col (- (window-width) window-min-width)))
350 (if (< last-col first-col)
351 (error "Window too narrow to split")
352 (split-window-horizontally
353 (min (max new-width first-col) last-col))))))
354
355 (defun mouse-drag-window-above (window)
356 "Return the (or a) window directly above WINDOW.
357 That means one whose bottom edge is at the same height as WINDOW's top edge."
358 (let ((start-top (nth 1 (window-edges window)))
359 (start-left (nth 0 (window-edges window)))
360 (start-right (nth 2 (window-edges window)))
361 (start-window window)
362 above-window)
363 (setq window (previous-window window 0))
364 (while (and (not above-window) (not (eq window start-window)))
365 (let ((left (nth 0 (window-edges window)))
366 (right (nth 2 (window-edges window))))
367 (when (and (= (+ (window-height window) (nth 1 (window-edges window)))
368 start-top)
369 (or (and (<= left start-left) (<= start-right right))
370 (and (<= start-left left) (<= left start-right))
371 (and (<= start-left right) (<= right start-right))))
372 (setq above-window window)))
373 (setq window (previous-window window)))
374 above-window))
375
376 (defun mouse-drag-move-window-bottom (window growth)
377 "Move the bottom of WINDOW up or down by GROWTH lines.
378 Move it down if GROWTH is positive, or up if GROWTH is negative.
379 If this would make WINDOW too short,
380 shrink the window or windows above it to make room."
381 (condition-case nil
382 (adjust-window-trailing-edge window growth nil)
383 (error nil)))
384
385 (defsubst mouse-drag-move-window-top (window growth)
386 "Move the top of WINDOW up or down by GROWTH lines.
387 Move it down if GROWTH is positive, or up if GROWTH is negative.
388 If this would make WINDOW too short, shrink the window or windows
389 above it to make room."
390 ;; Moving the top of WINDOW is actually moving the bottom of the
391 ;; window above.
392 (let ((window-above (mouse-drag-window-above window)))
393 (and window-above
394 (mouse-drag-move-window-bottom window-above (- growth)))))
395
396 (defun mouse-drag-mode-line-1 (start-event mode-line-p)
397 "Change the height of a window by dragging on the mode or header line.
398 START-EVENT is the starting mouse-event of the drag action.
399 MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
400 ;; Give temporary modes such as isearch a chance to turn off.
401 (run-hooks 'mouse-leave-buffer-hook)
402 (let* ((done nil)
403 (echo-keystrokes 0)
404 (start (event-start start-event))
405 (start-event-window (posn-window start))
406 (start-event-frame (window-frame start-event-window))
407 (start-nwindows (count-windows t))
408 (minibuffer (frame-parameter nil 'minibuffer))
409 should-enlarge-minibuffer event mouse y top bot edges wconfig growth)
410 (track-mouse
411 (progn
412 ;; enlarge-window only works on the selected window, so
413 ;; we must select the window where the start event originated.
414 ;; unwind-protect will restore the old selected window later.
415 (select-window start-event-window)
416
417 ;; if this is the bottommost ordinary window, then to
418 ;; move its modeline the minibuffer must be enlarged.
419 (setq should-enlarge-minibuffer
420 (and minibuffer
421 mode-line-p
422 (not (one-window-p t))
423 (= (nth 1 (window-edges minibuffer))
424 (nth 3 (window-edges)))))
425
426 ;; loop reading events and sampling the position of
427 ;; the mouse.
428 (while (not done)
429 (setq event (read-event)
430 mouse (mouse-position))
431
432 ;; do nothing if
433 ;; - there is a switch-frame event.
434 ;; - the mouse isn't in the frame that we started in
435 ;; - the mouse isn't in any Emacs frame
436 ;; drag if
437 ;; - there is a mouse-movement event
438 ;; - there is a scroll-bar-movement event
439 ;; (same as mouse movement for our purposes)
440 ;; quit if
441 ;; - there is a keyboard event or some other unknown event
442 ;; unknown event.
443 (cond ((integerp event)
444 (setq done t))
445
446 ((memq (car event) '(switch-frame select-window))
447 nil)
448
449 ((not (memq (car event) '(mouse-movement scroll-bar-movement)))
450 (when (consp event)
451 (push event unread-command-events))
452 (setq done t))
453
454 ((not (eq (car mouse) start-event-frame))
455 nil)
456
457 ((null (car (cdr mouse)))
458 nil)
459
460 (t
461 (setq y (cdr (cdr mouse))
462 edges (window-edges)
463 top (nth 1 edges)
464 bot (nth 3 edges))
465
466 ;; compute size change needed
467 (cond (mode-line-p
468 (setq growth (- y bot -1)))
469 (t ; header line
470 (when (< (- bot y) window-min-height)
471 (setq y (- bot window-min-height)))
472 ;; The window's top includes the header line!
473 (setq growth (- top y))))
474 (setq wconfig (current-window-configuration))
475
476 ;; Check for an error case.
477 (when (and (/= growth 0)
478 (not minibuffer)
479 (one-window-p t))
480 (error "Attempt to resize sole window"))
481
482 ;; grow/shrink minibuffer?
483 (if should-enlarge-minibuffer
484 (progn
485 ;; yes. briefly select minibuffer so
486 ;; enlarge-window will affect the
487 ;; correct window.
488 (select-window minibuffer)
489 ;; scale back shrinkage if it would
490 ;; make the minibuffer less than 1
491 ;; line tall.
492 (if (and (> growth 0)
493 (< (- (window-height minibuffer)
494 growth)
495 1))
496 (setq growth (1- (window-height minibuffer))))
497 (enlarge-window (- growth))
498 (select-window start-event-window))
499 ;; no. grow/shrink the selected window
500 ;(message "growth = %d" growth)
501 (if mode-line-p
502 (mouse-drag-move-window-bottom start-event-window growth)
503 (mouse-drag-move-window-top start-event-window growth)))
504
505 ;; if this window's growth caused another
506 ;; window to be deleted because it was too
507 ;; short, rescind the change.
508 ;;
509 ;; if size change caused space to be stolen
510 ;; from a window above this one, rescind the
511 ;; change, but only if we didn't grow/shrink
512 ;; the minibuffer. minibuffer size changes
513 ;; can cause all windows to shrink... no way
514 ;; around it.
515 (when (or (/= start-nwindows (count-windows t))
516 (and (not should-enlarge-minibuffer)
517 (> growth 0)
518 mode-line-p
519 (/= top (nth 1 (window-edges)))))
520 (set-window-configuration wconfig)))))))))
521
522 (defun mouse-drag-mode-line (start-event)
523 "Change the height of a window by dragging on the mode line."
524 (interactive "e")
525 (mouse-drag-mode-line-1 start-event t))
526
527 (defun mouse-drag-header-line (start-event)
528 "Change the height of a window by dragging on the header line.
529 Windows whose header-lines are at the top of the frame cannot be
530 resized by dragging their header-line."
531 (interactive "e")
532 ;; Changing the window's size by dragging its header-line when the
533 ;; header-line is at the top of the frame is somewhat strange,
534 ;; because the header-line doesn't move, so don't do it.
535 (let* ((start (event-start start-event))
536 (window (posn-window start))
537 (frame (window-frame window))
538 (first-window (frame-first-window frame)))
539 (unless (or (eq window first-window)
540 (= (nth 1 (window-edges window))
541 (nth 1 (window-edges first-window))))
542 (mouse-drag-mode-line-1 start-event nil))))
543
544 \f
545 (defun mouse-drag-vertical-line (start-event)
546 "Change the width of a window by dragging on the vertical line."
547 (interactive "e")
548 ;; Give temporary modes such as isearch a chance to turn off.
549 (run-hooks 'mouse-leave-buffer-hook)
550 (let* ((done nil)
551 (echo-keystrokes 0)
552 (start-event-frame (window-frame (car (car (cdr start-event)))))
553 (start-event-window (car (car (cdr start-event))))
554 (start-nwindows (count-windows t))
555 event mouse x left right edges wconfig growth
556 (which-side
557 (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
558 'right)))
559 (if (one-window-p t)
560 (error "Attempt to resize sole ordinary window"))
561 (if (eq which-side 'right)
562 (if (= (nth 2 (window-edges start-event-window))
563 (frame-width start-event-frame))
564 (error "Attempt to drag rightmost scrollbar"))
565 (if (= (nth 0 (window-edges start-event-window)) 0)
566 (error "Attempt to drag leftmost scrollbar")))
567 (track-mouse
568 (progn
569 ;; enlarge-window only works on the selected window, so
570 ;; we must select the window where the start event originated.
571 ;; unwind-protect will restore the old selected window later.
572 (select-window start-event-window)
573 ;; loop reading events and sampling the position of
574 ;; the mouse.
575 (while (not done)
576 (setq event (read-event)
577 mouse (mouse-position))
578 ;; do nothing if
579 ;; - there is a switch-frame event.
580 ;; - the mouse isn't in the frame that we started in
581 ;; - the mouse isn't in any Emacs frame
582 ;; drag if
583 ;; - there is a mouse-movement event
584 ;; - there is a scroll-bar-movement event
585 ;; (same as mouse movement for our purposes)
586 ;; quit if
587 ;; - there is a keyboard event or some other unknown event
588 ;; unknown event.
589 (cond ((integerp event)
590 (setq done t))
591 ((memq (car event) '(switch-frame select-window))
592 nil)
593 ((not (memq (car event)
594 '(mouse-movement scroll-bar-movement)))
595 (if (consp event)
596 (setq unread-command-events
597 (cons event unread-command-events)))
598 (setq done t))
599 ((not (eq (car mouse) start-event-frame))
600 nil)
601 ((null (car (cdr mouse)))
602 nil)
603 (t
604 (let ((window
605 ;; If the scroll bar is on the window's left,
606 ;; adjust the window on the left.
607 (if (eq which-side 'right)
608 (selected-window)
609 (previous-window))))
610 (setq x (- (car (cdr mouse))
611 (if (eq which-side 'right) 0 2))
612 edges (window-edges window)
613 left (nth 0 edges)
614 right (nth 2 edges))
615 ;; scale back a move that would make the
616 ;; window too thin.
617 (if (< (- x left -1) window-min-width)
618 (setq x (+ left window-min-width -1)))
619 ;; compute size change needed
620 (setq growth (- x right -1))
621 (condition-case nil
622 (adjust-window-trailing-edge window growth t)
623 (error nil))))))))))
624 \f
625 (defun mouse-set-point (event)
626 "Move point to the position clicked on with the mouse.
627 This should be bound to a mouse click event type."
628 (interactive "e")
629 (mouse-minibuffer-check event)
630 ;; Use event-end in case called from mouse-drag-region.
631 ;; If EVENT is a click, event-end and event-start give same value.
632 (posn-set-point (event-end event)))
633
634 (defvar mouse-last-region-beg nil)
635 (defvar mouse-last-region-end nil)
636 (defvar mouse-last-region-tick nil)
637
638 (defun mouse-region-match ()
639 "Return non-nil if there's an active region that was set with the mouse."
640 (and (mark t) mark-active
641 (eq mouse-last-region-beg (region-beginning))
642 (eq mouse-last-region-end (region-end))
643 (eq mouse-last-region-tick (buffer-modified-tick))))
644
645 (defun mouse-set-region (click)
646 "Set the region to the text dragged over, and copy to kill ring.
647 This should be bound to a mouse drag event."
648 (interactive "e")
649 (mouse-minibuffer-check click)
650 (let ((posn (event-start click))
651 (end (event-end click)))
652 (select-window (posn-window posn))
653 (if (numberp (posn-point posn))
654 (goto-char (posn-point posn)))
655 ;; If mark is highlighted, no need to bounce the cursor.
656 ;; On X, we highlight while dragging, thus once again no need to bounce.
657 (or transient-mark-mode
658 (memq (framep (selected-frame)) '(x pc w32 mac))
659 (sit-for 1))
660 (push-mark)
661 (set-mark (point))
662 (if (numberp (posn-point end))
663 (goto-char (posn-point end)))
664 ;; Don't set this-command to kill-region, so that a following
665 ;; C-w will not double the text in the kill ring.
666 ;; Ignore last-command so we don't append to a preceding kill.
667 (when mouse-drag-copy-region
668 (let (this-command last-command deactivate-mark)
669 (copy-region-as-kill (mark) (point))))
670 (mouse-set-region-1)))
671
672 (defun mouse-set-region-1 ()
673 ;; Set transient-mark-mode for a little while.
674 (if (memq transient-mark-mode '(nil identity))
675 (setq transient-mark-mode 'only))
676 (setq mouse-last-region-beg (region-beginning))
677 (setq mouse-last-region-end (region-end))
678 (setq mouse-last-region-tick (buffer-modified-tick)))
679
680 (defcustom mouse-scroll-delay 0.25
681 "*The pause between scroll steps caused by mouse drags, in seconds.
682 If you drag the mouse beyond the edge of a window, Emacs scrolls the
683 window to bring the text beyond that edge into view, with a delay of
684 this many seconds between scroll steps. Scrolling stops when you move
685 the mouse back into the window, or release the button.
686 This variable's value may be non-integral.
687 Setting this to zero causes Emacs to scroll as fast as it can."
688 :type 'number
689 :group 'mouse)
690
691 (defcustom mouse-scroll-min-lines 1
692 "*The minimum number of lines scrolled by dragging mouse out of window.
693 Moving the mouse out the top or bottom edge of the window begins
694 scrolling repeatedly. The number of lines scrolled per repetition
695 is normally equal to the number of lines beyond the window edge that
696 the mouse has moved. However, it always scrolls at least the number
697 of lines specified by this variable."
698 :type 'integer
699 :group 'mouse)
700
701 (defun mouse-scroll-subr (window jump &optional overlay start)
702 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
703 If OVERLAY is an overlay, let it stretch from START to the far edge of
704 the newly visible text.
705 Upon exit, point is at the far edge of the newly visible text."
706 (cond
707 ((and (> jump 0) (< jump mouse-scroll-min-lines))
708 (setq jump mouse-scroll-min-lines))
709 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
710 (setq jump (- mouse-scroll-min-lines))))
711 (let ((opoint (point)))
712 (while (progn
713 (goto-char (window-start window))
714 (if (not (zerop (vertical-motion jump window)))
715 (progn
716 (set-window-start window (point))
717 (if (natnump jump)
718 (if (window-end window)
719 (progn
720 (goto-char (window-end window))
721 ;; window-end doesn't reflect the window's new
722 ;; start position until the next redisplay.
723 (vertical-motion (1- jump) window))
724 (vertical-motion (- (window-height window) 2)))
725 (goto-char (window-start window)))
726 (if overlay
727 (move-overlay overlay start (point)))
728 ;; Now that we have scrolled WINDOW properly,
729 ;; put point back where it was for the redisplay
730 ;; so that we don't mess up the selected window.
731 (or (eq window (selected-window))
732 (goto-char opoint))
733 (sit-for mouse-scroll-delay)))))
734 (or (eq window (selected-window))
735 (goto-char opoint))))
736
737 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
738 (defconst mouse-drag-overlay
739 (let ((ol (make-overlay (point-min) (point-min))))
740 (delete-overlay ol)
741 (overlay-put ol 'face 'region)
742 ol))
743
744 (defvar mouse-selection-click-count 0)
745
746 (defvar mouse-selection-click-count-buffer nil)
747
748 (defun mouse-drag-region (start-event)
749 "Set the region to the text that the mouse is dragged over.
750 Highlight the drag area as you move the mouse.
751 This must be bound to a button-down mouse event.
752 In Transient Mark mode, the highlighting remains as long as the mark
753 remains active. Otherwise, it remains until the next input event.
754
755 If the click is in the echo area, display the `*Messages*' buffer."
756 (interactive "e")
757 (let ((w (posn-window (event-start start-event))))
758 (if (and (window-minibuffer-p w)
759 (not (minibuffer-window-active-p w)))
760 (save-excursion
761 ;; Swallow the up-event.
762 (read-event)
763 (set-buffer (get-buffer-create "*Messages*"))
764 (goto-char (point-max))
765 (display-buffer (current-buffer)))
766 ;; Give temporary modes such as isearch a chance to turn off.
767 (run-hooks 'mouse-leave-buffer-hook)
768 (mouse-drag-track start-event t))))
769
770
771 (defun mouse-on-link-p (pos)
772 "Return non-nil if POS is on a link in the current buffer.
773 POS must be a buffer position in the current buffer or an mouse
774 event location in the selected window, see `event-start'.
775 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
776 POS may be a mouse event location in any window.
777
778 A clickable link is identified by one of the following methods:
779
780 - If the character at POS has a non-nil `follow-link' text or
781 overlay property, the value of that property determines what to do.
782
783 - If there is a local key-binding or a keybinding at position POS
784 for the `follow-link' event, the binding of that event determines
785 what to do.
786
787 The resulting value determine whether POS is inside a link:
788
789 - If the value is `mouse-face', POS is inside a link if there
790 is a non-nil `mouse-face' property at POS. Return t in this case.
791
792 - If the value is a function, FUNC, POS is inside a link if
793 the call \(FUNC POS) returns non-nil. Return the return value
794 from that call. Arg is \(posn-point POS) if POS is a mouse event,
795
796 - Otherwise, return the value itself.
797
798 The return value is interpreted as follows:
799
800 - If it is a string, the mouse-1 event is translated into the
801 first character of the string, i.e. the action of the mouse-1
802 click is the local or global binding of that character.
803
804 - If it is a vector, the mouse-1 event is translated into the
805 first element of that vector, i.e. the action of the mouse-1
806 click is the local or global binding of that event.
807
808 - Otherwise, the mouse-1 event is translated into a mouse-2 event
809 at the same position."
810 (let ((w (and (consp pos) (posn-window pos))))
811 (if (consp pos)
812 (setq pos (and (or mouse-1-click-in-non-selected-windows
813 (eq (selected-window) w))
814 (posn-point pos))))
815 (when pos
816 (with-current-buffer (window-buffer w)
817 (let ((action
818 (or (get-char-property pos 'follow-link)
819 (save-excursion
820 (goto-char pos)
821 (key-binding [follow-link] nil t)))))
822 (cond
823 ((eq action 'mouse-face)
824 (and (get-char-property pos 'mouse-face) t))
825 ((functionp action)
826 (funcall action pos))
827 (t action)))))))
828
829 (defun mouse-fixup-help-message (msg)
830 "Fix help message MSG for `mouse-1-click-follows-link'."
831 (let (mp pos)
832 (if (and mouse-1-click-follows-link
833 (stringp msg)
834 (save-match-data
835 (string-match "^mouse-2" msg))
836 (setq mp (mouse-pixel-position))
837 (consp (setq pos (cdr mp)))
838 (car pos) (>= (car pos) 0)
839 (cdr pos) (>= (cdr pos) 0)
840 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
841 (windowp (posn-window pos)))
842 (with-current-buffer (window-buffer (posn-window pos))
843 (if (mouse-on-link-p pos)
844 (setq msg (concat
845 (cond
846 ((eq mouse-1-click-follows-link 'double) "double-")
847 ((and (integerp mouse-1-click-follows-link)
848 (< mouse-1-click-follows-link 0)) "Long ")
849 (t ""))
850 "mouse-1" (substring msg 7)))))))
851 msg)
852
853 (defun mouse-move-drag-overlay (ol start end mode)
854 (unless (= start end)
855 ;; Go to START first, so that when we move to END, if it's in the middle
856 ;; of intangible text, point jumps in the direction away from START.
857 ;; Don't do it if START=END otherwise a single click risks selecting
858 ;; a region if it's on intangible text. This exception was originally
859 ;; only applied on entry to mouse-drag-region, which had the problem
860 ;; that a tiny move during a single-click would cause the intangible
861 ;; text to be selected.
862 (goto-char start)
863 (goto-char end)
864 (setq end (point)))
865 (let ((range (mouse-start-end start end mode)))
866 (move-overlay ol (car range) (nth 1 range))))
867
868 (defun mouse-drag-track (start-event &optional
869 do-mouse-drag-region-post-process)
870 "Track mouse drags by highlighting area between point and cursor.
871 The region will be defined with mark and point, and the overlay
872 will be deleted after return. DO-MOUSE-DRAG-REGION-POST-PROCESS
873 should only be used by mouse-drag-region."
874 (mouse-minibuffer-check start-event)
875 (setq mouse-selection-click-count-buffer (current-buffer))
876 (let* ((original-window (selected-window))
877 ;; We've recorded what we needed from the current buffer and
878 ;; window, now let's jump to the place of the event, where things
879 ;; are happening.
880 (_ (mouse-set-point start-event))
881 (echo-keystrokes 0)
882 (start-posn (event-start start-event))
883 (start-point (posn-point start-posn))
884 (start-window (posn-window start-posn))
885 (start-window-start (window-start start-window))
886 (start-hscroll (window-hscroll start-window))
887 (bounds (window-edges start-window))
888 (make-cursor-line-fully-visible nil)
889 (top (nth 1 bounds))
890 (bottom (if (window-minibuffer-p start-window)
891 (nth 3 bounds)
892 ;; Don't count the mode line.
893 (1- (nth 3 bounds))))
894 (on-link (and mouse-1-click-follows-link
895 (or mouse-1-click-in-non-selected-windows
896 (eq start-window original-window))
897 ;; Use start-point before the intangibility
898 ;; treatment, in case we click on a link inside an
899 ;; intangible text.
900 (mouse-on-link-p start-point)))
901 (click-count (1- (event-click-count start-event)))
902 (remap-double-click (and on-link
903 (eq mouse-1-click-follows-link 'double)
904 (= click-count 1))))
905 (setq mouse-selection-click-count click-count)
906 ;; In case the down click is in the middle of some intangible text,
907 ;; use the end of that text, and put it in START-POINT.
908 (if (< (point) start-point)
909 (goto-char start-point))
910 (setq start-point (point))
911 (if remap-double-click ;; Don't expand mouse overlay in links
912 (setq click-count 0))
913 (mouse-move-drag-overlay mouse-drag-overlay start-point start-point
914 click-count)
915 (overlay-put mouse-drag-overlay 'window start-window)
916 (deactivate-mark)
917 (let (event end end-point last-end-point)
918 (track-mouse
919 (while (progn
920 (setq event (read-event))
921 (or (mouse-movement-p event)
922 (memq (car-safe event) '(switch-frame select-window))))
923 (if (memq (car-safe event) '(switch-frame select-window))
924 nil
925 (setq end (event-end event)
926 end-point (posn-point end))
927 (if (numberp end-point)
928 (setq last-end-point end-point))
929
930 (cond
931 ;; Are we moving within the original window?
932 ((and (eq (posn-window end) start-window)
933 (integer-or-marker-p end-point))
934 (mouse-move-drag-overlay mouse-drag-overlay start-point end-point click-count))
935
936 (t
937 (let ((mouse-row (cdr (cdr (mouse-position)))))
938 (cond
939 ((null mouse-row))
940 ((< mouse-row top)
941 (mouse-scroll-subr start-window (- mouse-row top)
942 mouse-drag-overlay start-point))
943 ((>= mouse-row bottom)
944 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
945 mouse-drag-overlay start-point)))))))))
946
947 ;; In case we did not get a mouse-motion event
948 ;; for the final move of the mouse before a drag event
949 ;; pretend that we did get one.
950 (when (and (memq 'drag (event-modifiers (car-safe event)))
951 (setq end (event-end event)
952 end-point (posn-point end))
953 (eq (posn-window end) start-window)
954 (integer-or-marker-p end-point))
955 (mouse-move-drag-overlay mouse-drag-overlay start-point end-point click-count))
956
957 ;; Handle the terminating event
958 (if (consp event)
959 (let* ((fun (key-binding (vector (car event))))
960 (do-multi-click (and (> (event-click-count event) 0)
961 (functionp fun)
962 (not (memq fun
963 '(mouse-set-point
964 mouse-set-region))))))
965 ;; Run the binding of the terminating up-event, if possible.
966 (if (and (not (= (overlay-start mouse-drag-overlay)
967 (overlay-end mouse-drag-overlay)))
968 (not do-multi-click))
969 (let* ((stop-point
970 (if (numberp (posn-point (event-end event)))
971 (posn-point (event-end event))
972 last-end-point))
973 ;; The end that comes from where we ended the drag.
974 ;; Point goes here.
975 (region-termination
976 (if (and stop-point (< stop-point start-point))
977 (overlay-start mouse-drag-overlay)
978 (overlay-end mouse-drag-overlay)))
979 ;; The end that comes from where we started the drag.
980 ;; Mark goes there.
981 (region-commencement
982 (- (+ (overlay-end mouse-drag-overlay)
983 (overlay-start mouse-drag-overlay))
984 region-termination))
985 last-command this-command)
986 (push-mark region-commencement t t)
987 (goto-char region-termination)
988 (if (not do-mouse-drag-region-post-process)
989 ;; Skip all post-event handling, return immediately.
990 (delete-overlay mouse-drag-overlay)
991 ;; Don't let copy-region-as-kill set deactivate-mark.
992 (when mouse-drag-copy-region
993 (let (deactivate-mark)
994 (copy-region-as-kill (point) (mark t))))
995 (let ((buffer (current-buffer)))
996 (mouse-show-mark)
997 ;; mouse-show-mark can call read-event,
998 ;; and that means the Emacs server could switch buffers
999 ;; under us. If that happened,
1000 ;; avoid trying to use the region.
1001 (and (mark t) mark-active
1002 (eq buffer (current-buffer))
1003 (mouse-set-region-1)))))
1004 ;; Run the binding of the terminating up-event.
1005 ;; If a multiple click is not bound to mouse-set-point,
1006 ;; cancel the effects of mouse-move-drag-overlay to
1007 ;; avoid producing wrong results.
1008 (if do-multi-click (goto-char start-point))
1009 (delete-overlay mouse-drag-overlay)
1010 (when (and (functionp fun)
1011 (= start-hscroll (window-hscroll start-window))
1012 ;; Don't run the up-event handler if the
1013 ;; window start changed in a redisplay after
1014 ;; the mouse-set-point for the down-mouse
1015 ;; event at the beginning of this function.
1016 ;; When the window start has changed, the
1017 ;; up-mouse event will contain a different
1018 ;; position due to the new window contents,
1019 ;; and point is set again.
1020 (or end-point
1021 (= (window-start start-window)
1022 start-window-start)))
1023 (when (and on-link
1024 (or (not end-point) (= end-point start-point))
1025 (consp event)
1026 (or remap-double-click
1027 (and
1028 (not (eq mouse-1-click-follows-link 'double))
1029 (= click-count 0)
1030 (= (event-click-count event) 1)
1031 (not (input-pending-p))
1032 (or (not (integerp mouse-1-click-follows-link))
1033 (let ((t0 (posn-timestamp (event-start start-event)))
1034 (t1 (posn-timestamp (event-end event))))
1035 (and (integerp t0) (integerp t1)
1036 (if (> mouse-1-click-follows-link 0)
1037 (<= (- t1 t0) mouse-1-click-follows-link)
1038 (< (- t0 t1) mouse-1-click-follows-link))))))))
1039 ;; Reselect previous selected window,
1040 ;; so that the mouse-2 event runs in the same
1041 ;; situation as if user had clicked it directly.
1042 ;; Fixes the bug reported by juri@jurta.org on 2005-12-27.
1043 (select-window original-window)
1044 (if (or (vectorp on-link) (stringp on-link))
1045 (setq event (aref on-link 0))
1046 (setcar event 'mouse-2)
1047 ;; If this mouse click has never been done by
1048 ;; the user, it doesn't have the necessary
1049 ;; property to be interpreted correctly.
1050 (put 'mouse-2 'event-kind 'mouse-click)))
1051 (push event unread-command-events))))
1052
1053 ;; Case where the end-event is not a cons cell (it's just a boring
1054 ;; char-key-press).
1055 (delete-overlay mouse-drag-overlay)))))
1056 \f
1057 ;; Commands to handle xterm-style multiple clicks.
1058 (defun mouse-skip-word (dir)
1059 "Skip over word, over whitespace, or over identical punctuation.
1060 If DIR is positive skip forward; if negative, skip backward."
1061 (let* ((char (following-char))
1062 (syntax (char-to-string (char-syntax char))))
1063 (cond ((string= syntax "w")
1064 ;; Here, we can't use skip-syntax-forward/backward because
1065 ;; they don't pay attention to word-separating-categories,
1066 ;; and thus they will skip over a true word boundary. So,
1067 ;; we simularte the original behaviour by using
1068 ;; forward-word.
1069 (if (< dir 0)
1070 (if (not (looking-at "\\<"))
1071 (forward-word -1))
1072 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1073 (forward-word 1))))
1074 ((string= syntax " ")
1075 (if (< dir 0)
1076 (skip-syntax-backward syntax)
1077 (skip-syntax-forward syntax)))
1078 ((string= syntax "_")
1079 (if (< dir 0)
1080 (skip-syntax-backward "w_")
1081 (skip-syntax-forward "w_")))
1082 ((< dir 0)
1083 (while (and (not (bobp)) (= (preceding-char) char))
1084 (forward-char -1)))
1085 (t
1086 (while (and (not (eobp)) (= (following-char) char))
1087 (forward-char 1))))))
1088
1089 (defun mouse-start-end (start end mode)
1090 "Return a list of region bounds based on START and END according to MODE.
1091 If MODE is 0 then set point to (min START END), mark to (max START END).
1092 If MODE is 1 then set point to start of word at (min START END),
1093 mark to end of word at (max START END).
1094 If MODE is 2 then do the same for lines."
1095 (if (> start end)
1096 (let ((temp start))
1097 (setq start end
1098 end temp)))
1099 (setq mode (mod mode 3))
1100 (cond ((= mode 0)
1101 (list start end))
1102 ((and (= mode 1)
1103 (= start end)
1104 (char-after start)
1105 (= (char-syntax (char-after start)) ?\())
1106 (list start
1107 (save-excursion
1108 (goto-char start)
1109 (forward-sexp 1)
1110 (point))))
1111 ((and (= mode 1)
1112 (= start end)
1113 (char-after start)
1114 (= (char-syntax (char-after start)) ?\)))
1115 (list (save-excursion
1116 (goto-char (1+ start))
1117 (backward-sexp 1)
1118 (point))
1119 (1+ start)))
1120 ((and (= mode 1)
1121 (= start end)
1122 (char-after start)
1123 (= (char-syntax (char-after start)) ?\"))
1124 (let ((open (or (eq start (point-min))
1125 (save-excursion
1126 (goto-char (- start 1))
1127 (looking-at "\\s(\\|\\s \\|\\s>")))))
1128 (if open
1129 (list start
1130 (save-excursion
1131 (condition-case nil
1132 (progn
1133 (goto-char start)
1134 (forward-sexp 1)
1135 (point))
1136 (error end))))
1137 (list (save-excursion
1138 (condition-case nil
1139 (progn
1140 (goto-char (1+ start))
1141 (backward-sexp 1)
1142 (point))
1143 (error end)))
1144 (1+ start)))))
1145 ((= mode 1)
1146 (list (save-excursion
1147 (goto-char start)
1148 (mouse-skip-word -1)
1149 (point))
1150 (save-excursion
1151 (goto-char end)
1152 (mouse-skip-word 1)
1153 (point))))
1154 ((= mode 2)
1155 (list (save-excursion
1156 (goto-char start)
1157 (beginning-of-line 1)
1158 (point))
1159 (save-excursion
1160 (goto-char end)
1161 (forward-line 1)
1162 (point))))))
1163 \f
1164 ;; Subroutine: set the mark where CLICK happened,
1165 ;; but don't do anything else.
1166 (defun mouse-set-mark-fast (click)
1167 (mouse-minibuffer-check click)
1168 (let ((posn (event-start click)))
1169 (select-window (posn-window posn))
1170 (if (numberp (posn-point posn))
1171 (push-mark (posn-point posn) t t))))
1172
1173 (defun mouse-undouble-last-event (events)
1174 (let* ((index (1- (length events)))
1175 (last (nthcdr index events))
1176 (event (car last))
1177 (basic (event-basic-type event))
1178 (old-modifiers (event-modifiers event))
1179 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1180 (new
1181 (if (consp event)
1182 ;; Use reverse, not nreverse, since event-modifiers
1183 ;; does not copy the list it returns.
1184 (cons (event-convert-list (reverse (cons basic modifiers)))
1185 (cdr event))
1186 event)))
1187 (setcar last new)
1188 (if (and (not (equal modifiers old-modifiers))
1189 (key-binding (apply 'vector events)))
1190 t
1191 (setcar last event)
1192 nil)))
1193
1194 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1195
1196 (defcustom mouse-region-delete-keys '([delete] [deletechar] [backspace])
1197 "List of keys that should cause the mouse region to be deleted."
1198 :group 'mouse
1199 :type '(repeat key-sequence))
1200
1201 (defun mouse-show-mark ()
1202 (let ((inhibit-quit t)
1203 (echo-keystrokes 0)
1204 event events key ignore
1205 (x-lost-selection-functions
1206 (when (boundp 'x-lost-selection-functions)
1207 (copy-sequence x-lost-selection-functions))))
1208 (add-hook 'x-lost-selection-functions
1209 (lambda (seltype)
1210 (when (eq seltype 'PRIMARY)
1211 (setq ignore t)
1212 (throw 'mouse-show-mark t))))
1213 (if transient-mark-mode
1214 (delete-overlay mouse-drag-overlay)
1215 (move-overlay mouse-drag-overlay (point) (mark t)))
1216 (catch 'mouse-show-mark
1217 ;; In this loop, execute scroll bar and switch-frame events.
1218 ;; Should we similarly handle `select-window' events? --Stef
1219 ;; Also ignore down-events that are undefined.
1220 (while (progn (setq event (read-event))
1221 (setq events (append events (list event)))
1222 (setq key (apply 'vector events))
1223 (or (and (consp event)
1224 (eq (car event) 'switch-frame))
1225 (and (consp event)
1226 (eq (posn-point (event-end event))
1227 'vertical-scroll-bar))
1228 (and (memq 'down (event-modifiers event))
1229 (not (key-binding key))
1230 (not (mouse-undouble-last-event events))
1231 (not (member key mouse-region-delete-keys)))))
1232 (and (consp event)
1233 (or (eq (car event) 'switch-frame)
1234 (eq (posn-point (event-end event))
1235 'vertical-scroll-bar))
1236 (let ((keys (vector 'vertical-scroll-bar event)))
1237 (and (key-binding keys)
1238 (progn
1239 (call-interactively (key-binding keys)
1240 nil keys)
1241 (setq events nil)))))))
1242 ;; If we lost the selection, just turn off the highlighting.
1243 (unless ignore
1244 ;; For certain special keys, delete the region.
1245 (if (member key mouse-region-delete-keys)
1246 (delete-region (mark t) (point))
1247 ;; Otherwise, unread the key so it gets executed normally.
1248 (setq unread-command-events
1249 (nconc events unread-command-events))))
1250 (setq quit-flag nil)
1251 (unless transient-mark-mode
1252 (delete-overlay mouse-drag-overlay))))
1253
1254 (defun mouse-set-mark (click)
1255 "Set mark at the position clicked on with the mouse.
1256 Display cursor at that position for a second.
1257 This must be bound to a mouse click."
1258 (interactive "e")
1259 (mouse-minibuffer-check click)
1260 (select-window (posn-window (event-start click)))
1261 ;; We don't use save-excursion because that preserves the mark too.
1262 (let ((point-save (point)))
1263 (unwind-protect
1264 (progn (mouse-set-point click)
1265 (push-mark nil t t)
1266 (or transient-mark-mode
1267 (sit-for 1)))
1268 (goto-char point-save))))
1269
1270 (defun mouse-kill (click)
1271 "Kill the region between point and the mouse click.
1272 The text is saved in the kill ring, as with \\[kill-region]."
1273 (interactive "e")
1274 (mouse-minibuffer-check click)
1275 (let* ((posn (event-start click))
1276 (click-posn (posn-point posn)))
1277 (select-window (posn-window posn))
1278 (if (numberp click-posn)
1279 (kill-region (min (point) click-posn)
1280 (max (point) click-posn)))))
1281
1282 (defun mouse-yank-at-click (click arg)
1283 "Insert the last stretch of killed text at the position clicked on.
1284 Also move point to one end of the text thus inserted (normally the end),
1285 and set mark at the beginning.
1286 Prefix arguments are interpreted as with \\[yank].
1287 If `mouse-yank-at-point' is non-nil, insert at point
1288 regardless of where you click."
1289 (interactive "e\nP")
1290 ;; Give temporary modes such as isearch a chance to turn off.
1291 (run-hooks 'mouse-leave-buffer-hook)
1292 (or mouse-yank-at-point (mouse-set-point click))
1293 (setq this-command 'yank)
1294 (setq mouse-selection-click-count 0)
1295 (yank arg))
1296
1297 (defun mouse-kill-ring-save (click)
1298 "Copy the region between point and the mouse click in the kill ring.
1299 This does not delete the region; it acts like \\[kill-ring-save]."
1300 (interactive "e")
1301 (mouse-set-mark-fast click)
1302 (let (this-command last-command)
1303 (kill-ring-save (point) (mark t)))
1304 (mouse-show-mark))
1305
1306 ;;; This function used to delete the text between point and the mouse
1307 ;;; whenever it was equal to the front of the kill ring, but some
1308 ;;; people found that confusing.
1309
1310 ;;; A list (TEXT START END), describing the text and position of the last
1311 ;;; invocation of mouse-save-then-kill.
1312 (defvar mouse-save-then-kill-posn nil)
1313
1314 (defun mouse-save-then-kill-delete-region (beg end)
1315 ;; We must make our own undo boundaries
1316 ;; because they happen automatically only for the current buffer.
1317 (undo-boundary)
1318 (if (or (= beg end) (eq buffer-undo-list t))
1319 ;; If we have no undo list in this buffer,
1320 ;; just delete.
1321 (delete-region beg end)
1322 ;; Delete, but make the undo-list entry share with the kill ring.
1323 ;; First, delete just one char, so in case buffer is being modified
1324 ;; for the first time, the undo list records that fact.
1325 (let (before-change-functions after-change-functions)
1326 (delete-region beg
1327 (+ beg (if (> end beg) 1 -1))))
1328 (let ((buffer-undo-list buffer-undo-list))
1329 ;; Undo that deletion--but don't change the undo list!
1330 (let (before-change-functions after-change-functions)
1331 (primitive-undo 1 buffer-undo-list))
1332 ;; Now delete the rest of the specified region,
1333 ;; but don't record it.
1334 (setq buffer-undo-list t)
1335 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1336 (error "Lossage in mouse-save-then-kill-delete-region"))
1337 (delete-region beg end))
1338 (let ((tail buffer-undo-list))
1339 ;; Search back in buffer-undo-list for the string
1340 ;; that came from deleting one character.
1341 (while (and tail (not (stringp (car (car tail)))))
1342 (setq tail (cdr tail)))
1343 ;; Replace it with an entry for the entire deleted text.
1344 (and tail
1345 (setcar tail (cons (car kill-ring) (min beg end))))))
1346 (undo-boundary))
1347
1348 (defun mouse-save-then-kill (click)
1349 "Save text to point in kill ring; the second time, kill the text.
1350 If the text between point and the mouse is the same as what's
1351 at the front of the kill ring, this deletes the text.
1352 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1353 which prepares for a second click to delete the text.
1354
1355 If you have selected words or lines, this command extends the
1356 selection through the word or line clicked on. If you do this
1357 again in a different position, it extends the selection again.
1358 If you do this twice in the same position, the selection is killed."
1359 (interactive "e")
1360 (let ((before-scroll
1361 (with-current-buffer (window-buffer (posn-window (event-start click)))
1362 point-before-scroll)))
1363 (mouse-minibuffer-check click)
1364 (let ((click-posn (posn-point (event-start click)))
1365 ;; Don't let a subsequent kill command append to this one:
1366 ;; prevent setting this-command to kill-region.
1367 (this-command this-command))
1368 (if (and (with-current-buffer
1369 (window-buffer (posn-window (event-start click)))
1370 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1371 ;; Don't be fooled by a recent click in some other buffer.
1372 (eq mouse-selection-click-count-buffer
1373 (current-buffer)))))
1374 (if (not (and (eq last-command 'mouse-save-then-kill)
1375 (equal click-posn
1376 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1377 ;; Find both ends of the object selected by this click.
1378 (let* ((range
1379 (mouse-start-end click-posn click-posn
1380 mouse-selection-click-count)))
1381 ;; Move whichever end is closer to the click.
1382 ;; That's what xterm does, and it seems reasonable.
1383 (if (< (abs (- click-posn (mark t)))
1384 (abs (- click-posn (point))))
1385 (set-mark (car range))
1386 (goto-char (nth 1 range)))
1387 ;; We have already put the old region in the kill ring.
1388 ;; Replace it with the extended region.
1389 ;; (It would be annoying to make a separate entry.)
1390 (kill-new (buffer-substring (point) (mark t)) t)
1391 (mouse-set-region-1)
1392 ;; Arrange for a repeated mouse-3 to kill this region.
1393 (setq mouse-save-then-kill-posn
1394 (list (car kill-ring) (point) click-posn))
1395 (mouse-show-mark))
1396 ;; If we click this button again without moving it,
1397 ;; that time kill.
1398 (mouse-save-then-kill-delete-region (mark) (point))
1399 (setq mouse-selection-click-count 0)
1400 (setq mouse-save-then-kill-posn nil))
1401 (if (and (eq last-command 'mouse-save-then-kill)
1402 mouse-save-then-kill-posn
1403 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1404 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1405 ;; If this is the second time we've called
1406 ;; mouse-save-then-kill, delete the text from the buffer.
1407 (progn
1408 (mouse-save-then-kill-delete-region (point) (mark))
1409 ;; After we kill, another click counts as "the first time".
1410 (setq mouse-save-then-kill-posn nil))
1411 ;; This is not a repetition.
1412 ;; We are adjusting an old selection or creating a new one.
1413 (if (or (and (eq last-command 'mouse-save-then-kill)
1414 mouse-save-then-kill-posn)
1415 (and mark-active transient-mark-mode)
1416 (and (memq last-command
1417 '(mouse-drag-region mouse-set-region))
1418 (or mark-even-if-inactive
1419 (not transient-mark-mode))))
1420 ;; We have a selection or suitable region, so adjust it.
1421 (let* ((posn (event-start click))
1422 (new (posn-point posn)))
1423 (select-window (posn-window posn))
1424 (if (numberp new)
1425 (progn
1426 ;; Move whichever end of the region is closer to the click.
1427 ;; That is what xterm does, and it seems reasonable.
1428 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1429 (goto-char new)
1430 (set-mark new))
1431 (setq deactivate-mark nil)))
1432 (kill-new (buffer-substring (point) (mark t)) t))
1433 ;; Set the mark where point is, then move where clicked.
1434 (mouse-set-mark-fast click)
1435 (if before-scroll
1436 (goto-char before-scroll))
1437 (exchange-point-and-mark) ;Why??? --Stef
1438 (kill-new (buffer-substring (point) (mark t))))
1439 (mouse-show-mark)
1440 (mouse-set-region-1)
1441 (setq mouse-save-then-kill-posn
1442 (list (car kill-ring) (point) click-posn)))))))
1443 \f
1444 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1445 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1446 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1447 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1448 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1449
1450 (defconst mouse-secondary-overlay
1451 (let ((ol (make-overlay (point-min) (point-min))))
1452 (delete-overlay ol)
1453 (overlay-put ol 'face 'secondary-selection)
1454 ol)
1455 "An overlay which records the current secondary selection.
1456 It is deleted when there is no secondary selection.")
1457
1458 (defvar mouse-secondary-click-count 0)
1459
1460 ;; A marker which records the specified first end for a secondary selection.
1461 ;; May be nil.
1462 (defvar mouse-secondary-start nil)
1463
1464 (defun mouse-start-secondary (click)
1465 "Set one end of the secondary selection to the position clicked on.
1466 Use \\[mouse-secondary-save-then-kill] to set the other end
1467 and complete the secondary selection."
1468 (interactive "e")
1469 (mouse-minibuffer-check click)
1470 (let ((posn (event-start click)))
1471 (with-current-buffer (window-buffer (posn-window posn))
1472 ;; Cancel any preexisting secondary selection.
1473 (delete-overlay mouse-secondary-overlay)
1474 (if (numberp (posn-point posn))
1475 (progn
1476 (or mouse-secondary-start
1477 (setq mouse-secondary-start (make-marker)))
1478 (move-marker mouse-secondary-start (posn-point posn)))))))
1479
1480 (defun mouse-set-secondary (click)
1481 "Set the secondary selection to the text that the mouse is dragged over.
1482 This must be bound to a mouse drag event."
1483 (interactive "e")
1484 (mouse-minibuffer-check click)
1485 (let ((posn (event-start click))
1486 beg
1487 (end (event-end click)))
1488 (with-current-buffer (window-buffer (posn-window posn))
1489 (if (numberp (posn-point posn))
1490 (setq beg (posn-point posn)))
1491 (move-overlay mouse-secondary-overlay beg (posn-point end)))))
1492
1493 (defun mouse-drag-secondary (start-event)
1494 "Set the secondary selection to the text that the mouse is dragged over.
1495 Highlight the drag area as you move the mouse.
1496 This must be bound to a button-down mouse event.
1497 The function returns a non-nil value if it creates a secondary selection."
1498 (interactive "e")
1499 (mouse-minibuffer-check start-event)
1500 (let* ((echo-keystrokes 0)
1501 (start-posn (event-start start-event))
1502 (start-point (posn-point start-posn))
1503 (start-window (posn-window start-posn))
1504 (bounds (window-edges start-window))
1505 (top (nth 1 bounds))
1506 (bottom (if (window-minibuffer-p start-window)
1507 (nth 3 bounds)
1508 ;; Don't count the mode line.
1509 (1- (nth 3 bounds))))
1510 (click-count (1- (event-click-count start-event))))
1511 (with-current-buffer (window-buffer start-window)
1512 (setq mouse-secondary-click-count click-count)
1513 (if (> (mod click-count 3) 0)
1514 ;; Double or triple press: make an initial selection
1515 ;; of one word or line.
1516 (let ((range (mouse-start-end start-point start-point click-count)))
1517 (set-marker mouse-secondary-start nil)
1518 ;; Why the double move? --Stef
1519 ;; (move-overlay mouse-secondary-overlay 1 1
1520 ;; (window-buffer start-window))
1521 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1522 (window-buffer start-window)))
1523 ;; Single-press: cancel any preexisting secondary selection.
1524 (or mouse-secondary-start
1525 (setq mouse-secondary-start (make-marker)))
1526 (set-marker mouse-secondary-start start-point)
1527 (delete-overlay mouse-secondary-overlay))
1528 (let (event end end-point)
1529 (track-mouse
1530 (while (progn
1531 (setq event (read-event))
1532 (or (mouse-movement-p event)
1533 (memq (car-safe event) '(switch-frame select-window))))
1534
1535 (if (memq (car-safe event) '(switch-frame select-window))
1536 nil
1537 (setq end (event-end event)
1538 end-point (posn-point end))
1539 (cond
1540 ;; Are we moving within the original window?
1541 ((and (eq (posn-window end) start-window)
1542 (integer-or-marker-p end-point))
1543 (let ((range (mouse-start-end start-point end-point
1544 click-count)))
1545 (if (or (/= start-point end-point)
1546 (null (marker-position mouse-secondary-start)))
1547 (progn
1548 (set-marker mouse-secondary-start nil)
1549 (move-overlay mouse-secondary-overlay
1550 (car range) (nth 1 range))))))
1551 (t
1552 (let ((mouse-row (cdr (cdr (mouse-position)))))
1553 (cond
1554 ((null mouse-row))
1555 ((< mouse-row top)
1556 (mouse-scroll-subr start-window (- mouse-row top)
1557 mouse-secondary-overlay start-point))
1558 ((>= mouse-row bottom)
1559 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1560 mouse-secondary-overlay start-point)))))))))
1561
1562 (if (consp event)
1563 (if (marker-position mouse-secondary-start)
1564 (save-window-excursion
1565 (delete-overlay mouse-secondary-overlay)
1566 (x-set-selection 'SECONDARY nil)
1567 (select-window start-window)
1568 (save-excursion
1569 (goto-char mouse-secondary-start)
1570 (sit-for 1)
1571 nil))
1572 (x-set-selection
1573 'SECONDARY
1574 (buffer-substring (overlay-start mouse-secondary-overlay)
1575 (overlay-end mouse-secondary-overlay)))))))))
1576
1577 (defun mouse-yank-secondary (click)
1578 "Insert the secondary selection at the position clicked on.
1579 Move point to the end of the inserted text.
1580 If `mouse-yank-at-point' is non-nil, insert at point
1581 regardless of where you click."
1582 (interactive "e")
1583 ;; Give temporary modes such as isearch a chance to turn off.
1584 (run-hooks 'mouse-leave-buffer-hook)
1585 (or mouse-yank-at-point (mouse-set-point click))
1586 (insert (x-get-selection 'SECONDARY)))
1587
1588 (defun mouse-kill-secondary ()
1589 "Kill the text in the secondary selection.
1590 This is intended more as a keyboard command than as a mouse command
1591 but it can work as either one.
1592
1593 The current buffer (in case of keyboard use), or the buffer clicked on,
1594 must be the one that the secondary selection is in. This requirement
1595 is to prevent accidents."
1596 (interactive)
1597 (let* ((keys (this-command-keys))
1598 (click (elt keys (1- (length keys)))))
1599 (or (eq (overlay-buffer mouse-secondary-overlay)
1600 (if (listp click)
1601 (window-buffer (posn-window (event-start click)))
1602 (current-buffer)))
1603 (error "Select or click on the buffer where the secondary selection is")))
1604 (let (this-command)
1605 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1606 (kill-region (overlay-start mouse-secondary-overlay)
1607 (overlay-end mouse-secondary-overlay))))
1608 (delete-overlay mouse-secondary-overlay)
1609 ;;; (x-set-selection 'SECONDARY nil)
1610 )
1611
1612 (defun mouse-secondary-save-then-kill (click)
1613 "Save text to point in kill ring; the second time, kill the text.
1614 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1615 If the text between where you did \\[mouse-start-secondary] and where
1616 you use this command matches the text at the front of the kill ring,
1617 this command deletes the text.
1618 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1619 which prepares for a second click with this command to delete the text.
1620
1621 If you have already made a secondary selection in that buffer,
1622 this command extends or retracts the selection to where you click.
1623 If you do this again in a different position, it extends or retracts
1624 again. If you do this twice in the same position, it kills the selection."
1625 (interactive "e")
1626 (mouse-minibuffer-check click)
1627 (let ((posn (event-start click))
1628 (click-posn (posn-point (event-start click)))
1629 ;; Don't let a subsequent kill command append to this one:
1630 ;; prevent setting this-command to kill-region.
1631 (this-command this-command))
1632 (or (eq (window-buffer (posn-window posn))
1633 (or (overlay-buffer mouse-secondary-overlay)
1634 (if mouse-secondary-start
1635 (marker-buffer mouse-secondary-start))))
1636 (error "Wrong buffer"))
1637 (with-current-buffer (window-buffer (posn-window posn))
1638 (if (> (mod mouse-secondary-click-count 3) 0)
1639 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1640 (equal click-posn
1641 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1642 ;; Find both ends of the object selected by this click.
1643 (let* ((range
1644 (mouse-start-end click-posn click-posn
1645 mouse-secondary-click-count)))
1646 ;; Move whichever end is closer to the click.
1647 ;; That's what xterm does, and it seems reasonable.
1648 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1649 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1650 (move-overlay mouse-secondary-overlay (car range)
1651 (overlay-end mouse-secondary-overlay))
1652 (move-overlay mouse-secondary-overlay
1653 (overlay-start mouse-secondary-overlay)
1654 (nth 1 range)))
1655 ;; We have already put the old region in the kill ring.
1656 ;; Replace it with the extended region.
1657 ;; (It would be annoying to make a separate entry.)
1658 (kill-new (buffer-substring
1659 (overlay-start mouse-secondary-overlay)
1660 (overlay-end mouse-secondary-overlay)) t)
1661 ;; Arrange for a repeated mouse-3 to kill this region.
1662 (setq mouse-save-then-kill-posn
1663 (list (car kill-ring) (point) click-posn)))
1664 ;; If we click this button again without moving it,
1665 ;; that time kill.
1666 (progn
1667 (mouse-save-then-kill-delete-region
1668 (overlay-start mouse-secondary-overlay)
1669 (overlay-end mouse-secondary-overlay))
1670 (setq mouse-save-then-kill-posn nil)
1671 (setq mouse-secondary-click-count 0)
1672 (delete-overlay mouse-secondary-overlay)))
1673 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1674 mouse-save-then-kill-posn
1675 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1676 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1677 ;; If this is the second time we've called
1678 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1679 (progn
1680 (mouse-save-then-kill-delete-region
1681 (overlay-start mouse-secondary-overlay)
1682 (overlay-end mouse-secondary-overlay))
1683 (setq mouse-save-then-kill-posn nil)
1684 (delete-overlay mouse-secondary-overlay))
1685 (if (overlay-start mouse-secondary-overlay)
1686 ;; We have a selection, so adjust it.
1687 (progn
1688 (if (numberp click-posn)
1689 (progn
1690 ;; Move whichever end of the region is closer to the click.
1691 ;; That is what xterm does, and it seems reasonable.
1692 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1693 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1694 (move-overlay mouse-secondary-overlay click-posn
1695 (overlay-end mouse-secondary-overlay))
1696 (move-overlay mouse-secondary-overlay
1697 (overlay-start mouse-secondary-overlay)
1698 click-posn))
1699 (setq deactivate-mark nil)))
1700 (if (eq last-command 'mouse-secondary-save-then-kill)
1701 ;; If the front of the kill ring comes from
1702 ;; an immediately previous use of this command,
1703 ;; replace it with the extended region.
1704 ;; (It would be annoying to make a separate entry.)
1705 (kill-new (buffer-substring
1706 (overlay-start mouse-secondary-overlay)
1707 (overlay-end mouse-secondary-overlay)) t)
1708 (let (deactivate-mark)
1709 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1710 (overlay-end mouse-secondary-overlay)))))
1711 (if mouse-secondary-start
1712 ;; All we have is one end of a selection,
1713 ;; so put the other end here.
1714 (let ((start (+ 0 mouse-secondary-start)))
1715 (kill-ring-save start click-posn)
1716 (move-overlay mouse-secondary-overlay start click-posn))))
1717 (setq mouse-save-then-kill-posn
1718 (list (car kill-ring) (point) click-posn))))
1719 (if (overlay-buffer mouse-secondary-overlay)
1720 (x-set-selection 'SECONDARY
1721 (buffer-substring
1722 (overlay-start mouse-secondary-overlay)
1723 (overlay-end mouse-secondary-overlay)))))))
1724 \f
1725 (defcustom mouse-buffer-menu-maxlen 20
1726 "*Number of buffers in one pane (submenu) of the buffer menu.
1727 If we have lots of buffers, divide them into groups of
1728 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1729 :type 'integer
1730 :group 'mouse)
1731
1732 (defcustom mouse-buffer-menu-mode-mult 4
1733 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1734 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1735 will split the buffer menu by the major modes (see
1736 `mouse-buffer-menu-mode-groups') or just by menu length.
1737 Set to 1 (or even 0!) if you want to group by major mode always, and to
1738 a large number if you prefer a mixed multitude. The default is 4."
1739 :type 'integer
1740 :group 'mouse
1741 :version "20.3")
1742
1743 (defvar mouse-buffer-menu-mode-groups
1744 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1745 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1746 . "Mail/News")
1747 ("\\<C\\>" . "C")
1748 ("ObjC" . "C")
1749 ("Text" . "Text")
1750 ("Outline" . "Text")
1751 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1752 ("log\\|diff\\|vc\\|cvs" . "Version Control") ; "Change Management"?
1753 ("Lisp" . "Lisp"))
1754 "How to group various major modes together in \\[mouse-buffer-menu].
1755 Each element has the form (REGEXP . GROUPNAME).
1756 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1757
1758 (defun mouse-buffer-menu (event)
1759 "Pop up a menu of buffers for selection with the mouse.
1760 This switches buffers in the window that you clicked on,
1761 and selects that window."
1762 (interactive "e")
1763 (mouse-minibuffer-check event)
1764 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1765 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1766 (let ((tail buffers))
1767 (while tail
1768 ;; Divide all buffers into buckets for various major modes.
1769 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1770 (with-current-buffer (car tail)
1771 (let* ((adjusted-major-mode major-mode) elt)
1772 (let ((tail mouse-buffer-menu-mode-groups))
1773 (while tail
1774 (if (string-match (car (car tail)) mode-name)
1775 (setq adjusted-major-mode (cdr (car tail))))
1776 (setq tail (cdr tail))))
1777 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1778 (if (null elt)
1779 (setq elt (list adjusted-major-mode
1780 (if (stringp adjusted-major-mode)
1781 adjusted-major-mode
1782 mode-name))
1783 split-by-major-mode (cons elt split-by-major-mode)))
1784 (or (memq (car tail) (cdr (cdr elt)))
1785 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1786 (setq tail (cdr tail))))
1787 ;; Compute the sum of squares of sizes of the major-mode buckets.
1788 (let ((tail split-by-major-mode))
1789 (setq sum-of-squares 0)
1790 (while tail
1791 (setq sum-of-squares
1792 (+ sum-of-squares
1793 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1794 (setq tail (cdr tail))))
1795 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1796 (* (length buffers) (length buffers)))
1797 ;; Subdividing by major modes really helps, so let's do it.
1798 (let (subdivided-menus (buffers-left (length buffers)))
1799 ;; Sort the list to put the most popular major modes first.
1800 (setq split-by-major-mode
1801 (sort split-by-major-mode
1802 (function (lambda (elt1 elt2)
1803 (> (length elt1) (length elt2))))))
1804 ;; Make a separate submenu for each major mode
1805 ;; that has more than one buffer,
1806 ;; unless all the remaining buffers are less than 1/10 of them.
1807 (while (and split-by-major-mode
1808 (and (> (length (car split-by-major-mode)) 3)
1809 (> (* buffers-left 10) (length buffers))))
1810 (let ((this-mode-list (mouse-buffer-menu-alist
1811 (cdr (cdr (car split-by-major-mode))))))
1812 (and this-mode-list
1813 (setq subdivided-menus
1814 (cons (cons
1815 (nth 1 (car split-by-major-mode))
1816 this-mode-list)
1817 subdivided-menus))))
1818 (setq buffers-left
1819 (- buffers-left (length (cdr (car split-by-major-mode)))))
1820 (setq split-by-major-mode (cdr split-by-major-mode)))
1821 ;; If any major modes are left over,
1822 ;; make a single submenu for them.
1823 (if split-by-major-mode
1824 (let ((others-list
1825 (mouse-buffer-menu-alist
1826 ;; we don't need split-by-major-mode any more,
1827 ;; so we can ditch it with nconc.
1828 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1829 (and others-list
1830 (setq subdivided-menus
1831 (cons (cons "Others" others-list)
1832 subdivided-menus)))))
1833 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1834 (progn
1835 (setq alist (mouse-buffer-menu-alist buffers))
1836 (setq menu (cons "Buffer Menu"
1837 (mouse-buffer-menu-split "Select Buffer" alist)))))
1838 (let ((buf (x-popup-menu event menu))
1839 (window (posn-window (event-start event))))
1840 (when buf
1841 (select-window
1842 (if (framep window) (frame-selected-window window)
1843 window))
1844 (switch-to-buffer buf)))))
1845
1846 (defun mouse-buffer-menu-alist (buffers)
1847 (let (tail
1848 (maxlen 0)
1849 head)
1850 (setq buffers
1851 (sort buffers
1852 (function (lambda (elt1 elt2)
1853 (string< (buffer-name elt1) (buffer-name elt2))))))
1854 (setq tail buffers)
1855 (while tail
1856 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1857 (setq maxlen
1858 (max maxlen
1859 (length (buffer-name (car tail))))))
1860 (setq tail (cdr tail)))
1861 (setq tail buffers)
1862 (while tail
1863 (let ((elt (car tail)))
1864 (if (/= (aref (buffer-name elt) 0) ?\ )
1865 (setq head
1866 (cons
1867 (cons
1868 (format
1869 (format "%%%ds %%s%%s %%s" maxlen)
1870 (buffer-name elt)
1871 (if (buffer-modified-p elt) "*" " ")
1872 (save-excursion
1873 (set-buffer elt)
1874 (if buffer-read-only "%" " "))
1875 (or (buffer-file-name elt)
1876 (save-excursion
1877 (set-buffer elt)
1878 (if list-buffers-directory
1879 (expand-file-name
1880 list-buffers-directory)))
1881 ""))
1882 elt)
1883 head))))
1884 (setq tail (cdr tail)))
1885 ;; Compensate for the reversal that the above loop does.
1886 (nreverse head)))
1887
1888 (defun mouse-buffer-menu-split (title alist)
1889 ;; If we have lots of buffers, divide them into groups of 20
1890 ;; and make a pane (or submenu) for each one.
1891 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1892 (let ((alist alist) sublists next
1893 (i 1))
1894 (while alist
1895 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1896 ;; and make them the next element of sublist.
1897 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1898 (if next
1899 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1900 nil))
1901 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1902 sublists))
1903 (setq i (1+ i))
1904 (setq alist next))
1905 (nreverse sublists))
1906 ;; Few buffers--put them all in one pane.
1907 (list (cons title alist))))
1908 \f
1909 ;;; These need to be rewritten for the new scroll bar implementation.
1910
1911 ;;;!! ;; Commands for the scroll bar.
1912 ;;;!!
1913 ;;;!! (defun mouse-scroll-down (click)
1914 ;;;!! (interactive "@e")
1915 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1916 ;;;!!
1917 ;;;!! (defun mouse-scroll-up (click)
1918 ;;;!! (interactive "@e")
1919 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1920 ;;;!!
1921 ;;;!! (defun mouse-scroll-down-full ()
1922 ;;;!! (interactive "@")
1923 ;;;!! (scroll-down nil))
1924 ;;;!!
1925 ;;;!! (defun mouse-scroll-up-full ()
1926 ;;;!! (interactive "@")
1927 ;;;!! (scroll-up nil))
1928 ;;;!!
1929 ;;;!! (defun mouse-scroll-move-cursor (click)
1930 ;;;!! (interactive "@e")
1931 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1932 ;;;!!
1933 ;;;!! (defun mouse-scroll-absolute (event)
1934 ;;;!! (interactive "@e")
1935 ;;;!! (let* ((pos (car event))
1936 ;;;!! (position (car pos))
1937 ;;;!! (length (car (cdr pos))))
1938 ;;;!! (if (<= length 0) (setq length 1))
1939 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1940 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1941 ;;;!! position)
1942 ;;;!! length)
1943 ;;;!! scale-factor)))
1944 ;;;!! (goto-char newpos)
1945 ;;;!! (recenter '(4)))))
1946 ;;;!!
1947 ;;;!! (defun mouse-scroll-left (click)
1948 ;;;!! (interactive "@e")
1949 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1950 ;;;!!
1951 ;;;!! (defun mouse-scroll-right (click)
1952 ;;;!! (interactive "@e")
1953 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1954 ;;;!!
1955 ;;;!! (defun mouse-scroll-left-full ()
1956 ;;;!! (interactive "@")
1957 ;;;!! (scroll-left nil))
1958 ;;;!!
1959 ;;;!! (defun mouse-scroll-right-full ()
1960 ;;;!! (interactive "@")
1961 ;;;!! (scroll-right nil))
1962 ;;;!!
1963 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1964 ;;;!! (interactive "@e")
1965 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1966 ;;;!!
1967 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1968 ;;;!! (interactive "@e")
1969 ;;;!! (let* ((pos (car event))
1970 ;;;!! (position (car pos))
1971 ;;;!! (length (car (cdr pos))))
1972 ;;;!! (set-window-hscroll (selected-window) 33)))
1973 ;;;!!
1974 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1975 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1976 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1977 ;;;!!
1978 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1979 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1980 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1981 ;;;!!
1982 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1983 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1984 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1985 ;;;!!
1986 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1987 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1988 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1989 ;;;!!
1990 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1991 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1992 ;;;!! 'mouse-scroll-absolute-horizontally)
1993 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1994 ;;;!!
1995 ;;;!! (global-set-key [horizontal-slider mouse-1]
1996 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1997 ;;;!! (global-set-key [horizontal-slider mouse-2]
1998 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1999 ;;;!! (global-set-key [horizontal-slider mouse-3]
2000 ;;;!! 'mouse-scroll-move-cursor-horizontally)
2001 ;;;!!
2002 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
2003 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
2004 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
2005 ;;;!!
2006 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
2007 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
2008 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
2009 ;;;!!
2010 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
2011 ;;;!! 'mouse-split-window-horizontally)
2012 ;;;!! (global-set-key [mode-line S-mouse-2]
2013 ;;;!! 'mouse-split-window-horizontally)
2014 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
2015 ;;;!! 'mouse-split-window)
2016 \f
2017 ;;;!! ;;;;
2018 ;;;!! ;;;; Here are experimental things being tested. Mouse events
2019 ;;;!! ;;;; are of the form:
2020 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
2021 ;;;!! ;;
2022 ;;;!! ;;;;
2023 ;;;!! ;;;; Dynamically track mouse coordinates
2024 ;;;!! ;;;;
2025 ;;;!! ;;
2026 ;;;!! ;;(defun track-mouse (event)
2027 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
2028 ;;;!! ;; (interactive "@e")
2029 ;;;!! ;; (while mouse-grabbed
2030 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
2031 ;;;!! ;; (abs-x (car pos))
2032 ;;;!! ;; (abs-y (cdr pos))
2033 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
2034 ;;;!! ;; (list (car pos) (cdr pos))
2035 ;;;!! ;; (selected-window))))
2036 ;;;!! ;; (if (consp relative-coordinate)
2037 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
2038 ;;;!! ;; (car relative-coordinate)
2039 ;;;!! ;; (car (cdr relative-coordinate)))
2040 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
2041 ;;;!!
2042 ;;;!! ;;
2043 ;;;!! ;; Dynamically put a box around the line indicated by point
2044 ;;;!! ;;
2045 ;;;!! ;;
2046 ;;;!! ;;(require 'backquote)
2047 ;;;!! ;;
2048 ;;;!! ;;(defun mouse-select-buffer-line (event)
2049 ;;;!! ;; (interactive "@e")
2050 ;;;!! ;; (let ((relative-coordinate
2051 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
2052 ;;;!! ;; (abs-y (car (cdr (car event)))))
2053 ;;;!! ;; (if (consp relative-coordinate)
2054 ;;;!! ;; (progn
2055 ;;;!! ;; (save-excursion
2056 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2057 ;;;!! ;; (x-draw-rectangle
2058 ;;;!! ;; (selected-screen)
2059 ;;;!! ;; abs-y 0
2060 ;;;!! ;; (save-excursion
2061 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2062 ;;;!! ;; (end-of-line)
2063 ;;;!! ;; (push-mark nil t)
2064 ;;;!! ;; (beginning-of-line)
2065 ;;;!! ;; (- (region-end) (region-beginning))) 1))
2066 ;;;!! ;; (sit-for 1)
2067 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
2068 ;;;!! ;;
2069 ;;;!! ;;(defvar last-line-drawn nil)
2070 ;;;!! ;;(defvar begin-delim "[^ \t]")
2071 ;;;!! ;;(defvar end-delim "[^ \t]")
2072 ;;;!! ;;
2073 ;;;!! ;;(defun mouse-boxing (event)
2074 ;;;!! ;; (interactive "@e")
2075 ;;;!! ;; (save-excursion
2076 ;;;!! ;; (let ((screen (selected-screen)))
2077 ;;;!! ;; (while (= (x-mouse-events) 0)
2078 ;;;!! ;; (let* ((pos (read-mouse-position screen))
2079 ;;;!! ;; (abs-x (car pos))
2080 ;;;!! ;; (abs-y (cdr pos))
2081 ;;;!! ;; (relative-coordinate
2082 ;;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
2083 ;;;!! ;; (selected-window)))
2084 ;;;!! ;; (begin-reg nil)
2085 ;;;!! ;; (end-reg nil)
2086 ;;;!! ;; (end-column nil)
2087 ;;;!! ;; (begin-column nil))
2088 ;;;!! ;; (if (and (consp relative-coordinate)
2089 ;;;!! ;; (or (not last-line-drawn)
2090 ;;;!! ;; (not (= last-line-drawn abs-y))))
2091 ;;;!! ;; (progn
2092 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2093 ;;;!! ;; (if (= (following-char) 10)
2094 ;;;!! ;; ()
2095 ;;;!! ;; (progn
2096 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
2097 ;;;!! ;; (setq begin-column (1- (current-column)))
2098 ;;;!! ;; (end-of-line)
2099 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
2100 ;;;!! ;; (setq end-column (1+ (current-column)))
2101 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
2102 ;;;!! ;; (x-draw-rectangle screen
2103 ;;;!! ;; (setq last-line-drawn abs-y)
2104 ;;;!! ;; begin-column
2105 ;;;!! ;; (- end-column begin-column) 1))))))))))
2106 ;;;!! ;;
2107 ;;;!! ;;(defun mouse-erase-box ()
2108 ;;;!! ;; (interactive)
2109 ;;;!! ;; (if last-line-drawn
2110 ;;;!! ;; (progn
2111 ;;;!! ;; (x-erase-rectangle (selected-screen))
2112 ;;;!! ;; (setq last-line-drawn nil))))
2113 ;;;!!
2114 ;;;!! ;;; (defun test-x-rectangle ()
2115 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
2116 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
2117 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
2118 ;;;!!
2119 ;;;!! ;;
2120 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
2121 ;;;!! ;;
2122 ;;;!!
2123 ;;;!! (defvar double-start nil)
2124 ;;;!! (defconst double-click-interval 300
2125 ;;;!! "Max ticks between clicks")
2126 ;;;!!
2127 ;;;!! (defun double-down (event)
2128 ;;;!! (interactive "@e")
2129 ;;;!! (if double-start
2130 ;;;!! (let ((interval (- (nth 4 event) double-start)))
2131 ;;;!! (if (< interval double-click-interval)
2132 ;;;!! (progn
2133 ;;;!! (backward-up-list 1)
2134 ;;;!! ;; (message "Interval %d" interval)
2135 ;;;!! (sleep-for 1)))
2136 ;;;!! (setq double-start nil))
2137 ;;;!! (setq double-start (nth 4 event))))
2138 ;;;!!
2139 ;;;!! (defun double-up (event)
2140 ;;;!! (interactive "@e")
2141 ;;;!! (and double-start
2142 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
2143 ;;;!! (setq double-start nil)))
2144 ;;;!!
2145 ;;;!! ;;; (defun x-test-doubleclick ()
2146 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
2147 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
2148 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
2149 ;;;!!
2150 ;;;!! ;;
2151 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
2152 ;;;!! ;;
2153 ;;;!!
2154 ;;;!! (defvar scrolled-lines 0)
2155 ;;;!! (defconst scroll-speed 1)
2156 ;;;!!
2157 ;;;!! (defun incr-scroll-down (event)
2158 ;;;!! (interactive "@e")
2159 ;;;!! (setq scrolled-lines 0)
2160 ;;;!! (incremental-scroll scroll-speed))
2161 ;;;!!
2162 ;;;!! (defun incr-scroll-up (event)
2163 ;;;!! (interactive "@e")
2164 ;;;!! (setq scrolled-lines 0)
2165 ;;;!! (incremental-scroll (- scroll-speed)))
2166 ;;;!!
2167 ;;;!! (defun incremental-scroll (n)
2168 ;;;!! (while (= (x-mouse-events) 0)
2169 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
2170 ;;;!! (scroll-down n)
2171 ;;;!! (sit-for 300 t)))
2172 ;;;!!
2173 ;;;!! (defun incr-scroll-stop (event)
2174 ;;;!! (interactive "@e")
2175 ;;;!! (message "Scrolled %d lines" scrolled-lines)
2176 ;;;!! (setq scrolled-lines 0)
2177 ;;;!! (sleep-for 1))
2178 ;;;!!
2179 ;;;!! ;;; (defun x-testing-scroll ()
2180 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
2181 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
2182 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
2183 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
2184 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
2185 ;;;!!
2186 ;;;!! ;;
2187 ;;;!! ;; Some playthings suitable for picture mode? They need work.
2188 ;;;!! ;;
2189 ;;;!!
2190 ;;;!! (defun mouse-kill-rectangle (event)
2191 ;;;!! "Kill the rectangle between point and the mouse cursor."
2192 ;;;!! (interactive "@e")
2193 ;;;!! (let ((point-save (point)))
2194 ;;;!! (save-excursion
2195 ;;;!! (mouse-set-point event)
2196 ;;;!! (push-mark nil t)
2197 ;;;!! (if (> point-save (point))
2198 ;;;!! (kill-rectangle (point) point-save)
2199 ;;;!! (kill-rectangle point-save (point))))))
2200 ;;;!!
2201 ;;;!! (defun mouse-open-rectangle (event)
2202 ;;;!! "Kill the rectangle between point and the mouse cursor."
2203 ;;;!! (interactive "@e")
2204 ;;;!! (let ((point-save (point)))
2205 ;;;!! (save-excursion
2206 ;;;!! (mouse-set-point event)
2207 ;;;!! (push-mark nil t)
2208 ;;;!! (if (> point-save (point))
2209 ;;;!! (open-rectangle (point) point-save)
2210 ;;;!! (open-rectangle point-save (point))))))
2211 ;;;!!
2212 ;;;!! ;; Must be a better way to do this.
2213 ;;;!!
2214 ;;;!! (defun mouse-multiple-insert (n char)
2215 ;;;!! (while (> n 0)
2216 ;;;!! (insert char)
2217 ;;;!! (setq n (1- n))))
2218 ;;;!!
2219 ;;;!! ;; What this could do is not finalize until button was released.
2220 ;;;!!
2221 ;;;!! (defun mouse-move-text (event)
2222 ;;;!! "Move text from point to cursor position, inserting spaces."
2223 ;;;!! (interactive "@e")
2224 ;;;!! (let* ((relative-coordinate
2225 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
2226 ;;;!! (if (consp relative-coordinate)
2227 ;;;!! (cond ((> (current-column) (car relative-coordinate))
2228 ;;;!! (delete-char
2229 ;;;!! (- (car relative-coordinate) (current-column))))
2230 ;;;!! ((< (current-column) (car relative-coordinate))
2231 ;;;!! (mouse-multiple-insert
2232 ;;;!! (- (car relative-coordinate) (current-column)) " "))
2233 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
2234 \f
2235 ;; Choose a completion with the mouse.
2236
2237 (defun mouse-choose-completion (event)
2238 "Click on an alternative in the `*Completions*' buffer to choose it."
2239 (interactive "e")
2240 ;; Give temporary modes such as isearch a chance to turn off.
2241 (run-hooks 'mouse-leave-buffer-hook)
2242 (let ((buffer (window-buffer))
2243 choice
2244 base-size)
2245 (save-excursion
2246 (set-buffer (window-buffer (posn-window (event-start event))))
2247 (if completion-reference-buffer
2248 (setq buffer completion-reference-buffer))
2249 (setq base-size completion-base-size)
2250 (save-excursion
2251 (goto-char (posn-point (event-start event)))
2252 (let (beg end)
2253 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2254 (setq end (point) beg (1+ (point))))
2255 (if (null beg)
2256 (error "No completion here"))
2257 (setq beg (previous-single-property-change beg 'mouse-face))
2258 (setq end (or (next-single-property-change end 'mouse-face)
2259 (point-max)))
2260 (setq choice (buffer-substring-no-properties beg end)))))
2261 (let ((owindow (selected-window)))
2262 (select-window (posn-window (event-start event)))
2263 (if (and (one-window-p t 'selected-frame)
2264 (window-dedicated-p (selected-window)))
2265 ;; This is a special buffer's frame
2266 (iconify-frame (selected-frame))
2267 (or (window-dedicated-p (selected-window))
2268 (bury-buffer)))
2269 (select-window owindow))
2270 (choose-completion-string choice buffer base-size)))
2271 \f
2272 ;; Font selection.
2273
2274 (defun font-menu-add-default ()
2275 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2276 (font-alist x-fixed-font-alist)
2277 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2278 (if (assoc "Default" elt)
2279 (delete (assoc "Default" elt) elt))
2280 (setcdr elt
2281 (cons (list "Default" default)
2282 (cdr elt)))))
2283
2284 (defvar x-fixed-font-alist
2285 '("Font menu"
2286 ("Misc"
2287 ;; For these, we specify the pixel height and width.
2288 ("fixed" "fixed")
2289 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2290 ("6x12"
2291 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2292 ("6x13"
2293 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2294 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2295 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2296 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2297 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2298 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2299 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2300 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2301 ("")
2302 ("clean 5x8"
2303 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2304 ("clean 6x8"
2305 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2306 ("clean 8x8"
2307 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2308 ("clean 8x10"
2309 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2310 ("clean 8x14"
2311 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2312 ("clean 8x16"
2313 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2314 ("")
2315 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2316 ;;; We don't seem to have these; who knows what they are.
2317 ;;; ("fg-18" "fg-18")
2318 ;;; ("fg-25" "fg-25")
2319 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2320 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2321 ("lucidasanstypewriter-bold-24"
2322 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2323 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2324 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2325 )
2326 ("Courier"
2327 ;; For these, we specify the point height.
2328 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2329 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2330 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2331 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2332 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2333 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2334 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2335 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2336 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2337 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2338 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2339 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2340 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2341 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2342 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2343 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2344 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2345 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2346 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2347 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2348 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2349 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2350 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2351 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2352 )
2353 "X fonts suitable for use in Emacs.")
2354
2355 (defun mouse-set-font (&rest fonts)
2356 "Select an emacs font from a list of known good fonts and fontsets."
2357 (interactive
2358 (progn (unless (display-multi-font-p)
2359 (error "Cannot change fonts on this display"))
2360 (x-popup-menu
2361 (if (listp last-nonmenu-event)
2362 last-nonmenu-event
2363 (list '(0 0) (selected-window)))
2364 ;; Append list of fontsets currently defined.
2365 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2366 (if fonts
2367 (let (font)
2368 (while fonts
2369 (condition-case nil
2370 (progn
2371 (set-default-font (car fonts))
2372 (setq font (car fonts))
2373 (setq fonts nil))
2374 (error
2375 (setq fonts (cdr fonts)))))
2376 (if (null font)
2377 (error "Font not found")))))
2378 \f
2379 ;;; Bindings for mouse commands.
2380
2381 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2382 (global-set-key [mouse-1] 'mouse-set-point)
2383 (global-set-key [drag-mouse-1] 'mouse-set-region)
2384
2385 ;; These are tested for in mouse-drag-region.
2386 (global-set-key [double-mouse-1] 'mouse-set-point)
2387 (global-set-key [triple-mouse-1] 'mouse-set-point)
2388
2389 ;; Clicking on the fringes causes hscrolling:
2390 (global-set-key [left-fringe mouse-1] 'mouse-set-point)
2391 (global-set-key [right-fringe mouse-1] 'mouse-set-point)
2392
2393 (global-set-key [mouse-2] 'mouse-yank-at-click)
2394 (global-set-key [mouse-3] 'mouse-save-then-kill)
2395
2396 ;; By binding these to down-going events, we let the user use the up-going
2397 ;; event to make the selection, saving a click.
2398 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2399 (if (not (eq system-type 'ms-dos))
2400 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2401 ;; C-down-mouse-2 is bound in facemenu.el.
2402 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2403
2404
2405 ;; Replaced with dragging mouse-1
2406 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2407
2408 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2409 ;; vertical-line prevents Emacs from signaling an error when the mouse
2410 ;; button is released after dragging these lines, on non-toolkit
2411 ;; versions.
2412 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2413 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2414 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2415 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2416 (global-set-key [header-line mouse-1] 'mouse-select-window)
2417 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2418 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2419 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2420 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2421 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2422 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2423 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2424
2425 (provide 'mouse)
2426
2427 ;; This file contains the functionality of the old mldrag.el.
2428 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2429 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2430 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2431 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2432 (provide 'mldrag)
2433
2434 ;; arch-tag: 9a710ce1-914a-4923-9b81-697f7bf82ab3
2435 ;;; mouse.el ends here