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