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