Fix 2010-11-22 change to diff.el.
[emacs.git] / lisp / mouse.el
blobaa5dadf84ff170cb1b61c59f76fa0000922b515b
1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: hardware, mouse
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This package provides various useful commands (including help
28 ;; system access) through the mouse. All this code assumes that mouse
29 ;; interpretation has been abstracted into Emacs input events.
31 ;; The code is rather X-dependent.
33 ;;; Code:
35 ;;; Utility functions.
37 ;; Indent track-mouse like progn.
38 (put 'track-mouse 'lisp-indent-function 0)
40 (defcustom mouse-yank-at-point nil
41 "If non-nil, mouse yank commands yank at point instead of at click."
42 :type 'boolean
43 :group 'mouse)
45 (defcustom mouse-drag-copy-region nil
46 "If non-nil, copy to kill-ring upon mouse adjustments of the region.
48 This affects `mouse-save-then-kill' (\\[mouse-save-then-kill]) in
49 addition to mouse drags."
50 :type 'boolean
51 :version "24.1"
52 :group 'mouse)
54 (defcustom mouse-1-click-follows-link 450
55 "Non-nil means that clicking Mouse-1 on a link follows the link.
57 With the default setting, an ordinary Mouse-1 click on a link
58 performs the same action as Mouse-2 on that link, while a longer
59 Mouse-1 click \(hold down the Mouse-1 button for more than 450
60 milliseconds) performs the original Mouse-1 binding \(which
61 typically sets point where you click the mouse).
63 If value is an integer, the time elapsed between pressing and
64 releasing the mouse button determines whether to follow the link
65 or perform the normal Mouse-1 action (typically set point).
66 The absolute numeric value specifices the maximum duration of a
67 \"short click\" in milliseconds. A positive value means that a
68 short click follows the link, and a longer click performs the
69 normal action. A negative value gives the opposite behavior.
71 If value is `double', a double click follows the link.
73 Otherwise, a single Mouse-1 click unconditionally follows the link.
75 Note that dragging the mouse never follows the link.
77 This feature only works in modes that specifically identify
78 clickable text as links, so it may not work with some external
79 packages. See `mouse-on-link-p' for details."
80 :version "22.1"
81 :type '(choice (const :tag "Disabled" nil)
82 (const :tag "Double click" double)
83 (number :tag "Single click time limit" :value 450)
84 (other :tag "Single click" t))
85 :group 'mouse)
87 (defcustom mouse-1-click-in-non-selected-windows t
88 "If non-nil, a Mouse-1 click also follows links in non-selected windows.
90 If nil, a Mouse-1 click on a link in a non-selected window performs
91 the normal mouse-1 binding, typically selects the window and sets
92 point at the click position."
93 :type 'boolean
94 :version "22.1"
95 :group 'mouse)
99 ;; Provide a mode-specific menu on a mouse button.
101 (defun popup-menu (menu &optional position prefix)
102 "Popup the given menu and call the selected option.
103 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
104 `x-popup-menu'.
105 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
106 the current mouse position.
107 PREFIX is the prefix argument (if any) to pass to the command."
108 (let* ((map (cond
109 ((keymapp menu) menu)
110 ((and (listp menu) (keymapp (car menu))) menu)
111 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
112 (filter (when (symbolp map)
113 (plist-get (get map 'menu-prop) :filter))))
114 (if filter (funcall filter (symbol-function map)) map)))))
115 event cmd)
116 (unless position
117 (let ((mp (mouse-pixel-position)))
118 (setq position (list (list (cadr mp) (cddr mp)) (car mp)))))
119 ;; The looping behavior was taken from lmenu's popup-menu-popup
120 (while (and map (setq event
121 ;; map could be a prefix key, in which case
122 ;; we need to get its function cell
123 ;; definition.
124 (x-popup-menu position (indirect-function map))))
125 ;; Strangely x-popup-menu returns a list.
126 ;; mouse-major-mode-menu was using a weird:
127 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
128 (setq cmd
129 (if (and (not (keymapp map)) (listp map))
130 ;; We were given a list of keymaps. Search them all
131 ;; in sequence until a first binding is found.
132 (let ((mouse-click (apply 'vector event))
133 binding)
134 (while (and map (null binding))
135 (setq binding (lookup-key (car map) mouse-click))
136 (if (numberp binding) ; `too long'
137 (setq binding nil))
138 (setq map (cdr map)))
139 binding)
140 ;; We were given a single keymap.
141 (lookup-key map (apply 'vector event))))
142 ;; Clear out echoing, which perhaps shows a prefix arg.
143 (message "")
144 ;; Maybe try again but with the submap.
145 (setq map (if (keymapp cmd) cmd)))
146 ;; If the user did not cancel by refusing to select,
147 ;; and if the result is a command, run it.
148 (when (and (null map) (commandp cmd))
149 (setq prefix-arg prefix)
150 ;; `setup-specified-language-environment', for instance,
151 ;; expects this to be set from a menu keymap.
152 (setq last-command-event (car (last event)))
153 ;; mouse-major-mode-menu was using `command-execute' instead.
154 (call-interactively cmd))))
156 (defun minor-mode-menu-from-indicator (indicator)
157 "Show menu for minor mode specified by INDICATOR.
158 Interactively, INDICATOR is read using completion.
159 If there is no menu defined for the minor mode, then create one with
160 items `Turn Off' and `Help'."
161 (interactive
162 (list (completing-read
163 "Minor mode indicator: "
164 (describe-minor-mode-completion-table-for-indicator))))
165 (let* ((minor-mode (lookup-minor-mode-from-indicator indicator))
166 (mm-fun (or (get minor-mode :minor-mode-function) minor-mode)))
167 (unless minor-mode (error "Cannot find minor mode for `%s'" indicator))
168 (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist)))
169 (menu (and (keymapp map) (lookup-key map [menu-bar]))))
170 (setq menu
171 (if menu
172 (mouse-menu-non-singleton menu)
173 `(keymap
174 ,indicator
175 (turn-off menu-item "Turn Off minor mode" ,mm-fun)
176 (help menu-item "Help for minor mode"
177 (lambda () (interactive)
178 (describe-function ',mm-fun))))))
179 (popup-menu menu))))
181 (defun mouse-minor-mode-menu (event)
182 "Show minor-mode menu for EVENT on minor modes area of the mode line."
183 (interactive "@e")
184 (let ((indicator (car (nth 4 (car (cdr event))))))
185 (minor-mode-menu-from-indicator indicator)))
187 (defun mouse-menu-major-mode-map ()
188 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
189 (let* (;; Keymap from which to inherit; may be null.
190 (ancestor (mouse-menu-non-singleton
191 (and (current-local-map)
192 (local-key-binding [menu-bar]))))
193 ;; Make a keymap in which our last command leads to a menu or
194 ;; default to the edit menu.
195 (newmap (if ancestor
196 (make-sparse-keymap (concat (format-mode-line mode-name)
197 " Mode"))
198 menu-bar-edit-menu))
199 uniq)
200 (if ancestor
201 (set-keymap-parent newmap ancestor))
202 newmap))
204 (defun mouse-menu-non-singleton (menubar)
205 "Given menu keymap,
206 if it defines exactly one submenu, return just that submenu.
207 Otherwise return the whole menu."
208 (if menubar
209 (let (submap)
210 (map-keymap
211 (lambda (k v) (setq submap (if submap t (cons k v))))
212 (keymap-canonicalize menubar))
213 (if (eq submap t)
214 menubar
215 (lookup-key menubar (vector (car submap)))))))
217 (defun mouse-menu-bar-map ()
218 "Return a keymap equivalent to the menu bar.
219 The contents are the items that would be in the menu bar whether or
220 not it is actually displayed."
221 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
222 (let* ((local-menu (and (current-local-map)
223 (lookup-key (current-local-map) [menu-bar])))
224 (global-menu (lookup-key global-map [menu-bar]))
225 ;; If a keymap doesn't have a prompt string (a lazy
226 ;; programmer didn't bother to provide one), create it and
227 ;; insert it into the keymap; each keymap gets its own
228 ;; prompt. This is required for non-toolkit versions to
229 ;; display non-empty menu pane names.
230 (minor-mode-menus
231 (mapcar
232 (lambda (menu)
233 (let* ((minor-mode (car menu))
234 (menu (cdr menu))
235 (title-or-map (cadr menu)))
236 (or (stringp title-or-map)
237 (setq menu
238 (cons 'keymap
239 (cons (concat
240 (capitalize (subst-char-in-string
241 ?- ?\s (symbol-name
242 minor-mode)))
243 " Menu")
244 (cdr menu)))))
245 menu))
246 (minor-mode-key-binding [menu-bar])))
247 (local-title-or-map (and local-menu (cadr local-menu)))
248 (global-title-or-map (cadr global-menu)))
249 (or (null local-menu)
250 (stringp local-title-or-map)
251 (setq local-menu (cons 'keymap
252 (cons (concat (format-mode-line mode-name)
253 " Mode Menu")
254 (cdr local-menu)))))
255 (or (stringp global-title-or-map)
256 (setq global-menu (cons 'keymap
257 (cons "Global Menu"
258 (cdr global-menu)))))
259 ;; Supplying the list is faster than making a new map.
260 ;; FIXME: We have a problem here: we have to use the global/local/minor
261 ;; so they're displayed in the expected order, but later on in the command
262 ;; loop, they're actually looked up in the opposite order.
263 (apply 'append
264 global-menu
265 local-menu
266 minor-mode-menus)))
268 (defun mouse-major-mode-menu (event &optional prefix)
269 "Pop up a mode-specific menu of mouse commands.
270 Default to the Edit menu if the major mode doesn't define a menu."
271 (interactive "@e\nP")
272 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
273 (popup-menu (mouse-menu-major-mode-map) event prefix))
274 (make-obsolete 'mouse-major-mode-menu 'mouse-menu-major-mode-map "23.1")
276 (defun mouse-popup-menubar (event prefix)
277 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
278 The contents are the items that would be in the menu bar whether or
279 not it is actually displayed."
280 (interactive "@e \nP")
281 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
282 (popup-menu (mouse-menu-bar-map) event prefix))
283 (make-obsolete 'mouse-popup-menubar 'mouse-menu-bar-map "23.1")
285 (defun mouse-popup-menubar-stuff (event prefix)
286 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
287 Use the former if the menu bar is showing, otherwise the latter."
288 (interactive "@e\nP")
289 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
290 (popup-menu
291 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
292 (mouse-menu-bar-map)
293 (mouse-menu-major-mode-map))
294 event prefix))
295 (make-obsolete 'mouse-popup-menubar-stuff nil "23.1")
297 ;; Commands that operate on windows.
299 (defun mouse-minibuffer-check (event)
300 (let ((w (posn-window (event-start event))))
301 (and (window-minibuffer-p w)
302 (not (minibuffer-window-active-p w))
303 (error "Minibuffer window is not active")))
304 ;; Give temporary modes such as isearch a chance to turn off.
305 (run-hooks 'mouse-leave-buffer-hook))
307 (defun mouse-delete-window (click)
308 "Delete the window you click on.
309 Do nothing if the frame has just one window.
310 This command must be bound to a mouse click."
311 (interactive "e")
312 (unless (one-window-p t)
313 (mouse-minibuffer-check click)
314 (delete-window (posn-window (event-start click)))))
316 (defun mouse-select-window (click)
317 "Select the window clicked on; don't move point."
318 (interactive "e")
319 (mouse-minibuffer-check click)
320 (let ((oframe (selected-frame))
321 (frame (window-frame (posn-window (event-start click)))))
322 (select-window (posn-window (event-start click)))
323 (raise-frame frame)
324 (select-frame frame)
325 (or (eq frame oframe)
326 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
328 (defun mouse-tear-off-window (click)
329 "Delete the window clicked on, and create a new frame displaying its buffer."
330 (interactive "e")
331 (mouse-minibuffer-check click)
332 (let* ((window (posn-window (event-start click)))
333 (buf (window-buffer window))
334 (frame (make-frame)))
335 (select-frame frame)
336 (switch-to-buffer buf)
337 (delete-window window)))
339 (defun mouse-delete-other-windows ()
340 "Delete all windows except the one you click on."
341 (interactive "@")
342 (delete-other-windows))
344 (defun mouse-split-window-vertically (click)
345 "Select Emacs window mouse is on, then split it vertically in half.
346 The window is split at the line clicked on.
347 This command must be bound to a mouse click."
348 (interactive "@e")
349 (mouse-minibuffer-check click)
350 (let ((start (event-start click)))
351 (select-window (posn-window start))
352 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
353 (first-line window-min-height)
354 (last-line (- (window-height) window-min-height)))
355 (if (< last-line first-line)
356 (error "Window too short to split")
357 (split-window-vertically
358 (min (max new-height first-line) last-line))))))
360 (defun mouse-split-window-horizontally (click)
361 "Select Emacs window mouse is on, then split it horizontally in half.
362 The window is split at the column clicked on.
363 This command must be bound to a mouse click."
364 (interactive "@e")
365 (mouse-minibuffer-check click)
366 (let ((start (event-start click)))
367 (select-window (posn-window start))
368 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
369 (first-col window-min-width)
370 (last-col (- (window-width) window-min-width)))
371 (if (< last-col first-col)
372 (error "Window too narrow to split")
373 (split-window-horizontally
374 (min (max new-width first-col) last-col))))))
376 (defun mouse-drag-window-above (window)
377 "Return the (or a) window directly above WINDOW.
378 That means one whose bottom edge is at the same height as WINDOW's top edge."
379 (let ((start-top (nth 1 (window-edges window)))
380 (start-left (nth 0 (window-edges window)))
381 (start-right (nth 2 (window-edges window)))
382 (start-window window)
383 above-window)
384 (setq window (previous-window window 0))
385 (while (and (not above-window) (not (eq window start-window)))
386 (let ((left (nth 0 (window-edges window)))
387 (right (nth 2 (window-edges window))))
388 (when (and (= (+ (window-height window) (nth 1 (window-edges window)))
389 start-top)
390 (or (and (<= left start-left) (<= start-right right))
391 (and (<= start-left left) (<= left start-right))
392 (and (<= start-left right) (<= right start-right))))
393 (setq above-window window)))
394 (setq window (previous-window window)))
395 above-window))
397 (defun mouse-drag-move-window-bottom (window growth)
398 "Move the bottom of WINDOW up or down by GROWTH lines.
399 Move it down if GROWTH is positive, or up if GROWTH is negative.
400 If this would make WINDOW too short,
401 shrink the window or windows above it to make room."
402 (condition-case nil
403 (adjust-window-trailing-edge window growth nil)
404 (error nil)))
406 (defsubst mouse-drag-move-window-top (window growth)
407 "Move the top of WINDOW up or down by GROWTH lines.
408 Move it down if GROWTH is positive, or up if GROWTH is negative.
409 If this would make WINDOW too short, shrink the window or windows
410 above it to make room."
411 ;; Moving the top of WINDOW is actually moving the bottom of the
412 ;; window above.
413 (let ((window-above (mouse-drag-window-above window)))
414 (and window-above
415 (mouse-drag-move-window-bottom window-above (- growth)))))
417 (defun mouse-drag-mode-line-1 (start-event mode-line-p)
418 "Change the height of a window by dragging on the mode or header line.
419 START-EVENT is the starting mouse-event of the drag action.
420 MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
421 ;; Give temporary modes such as isearch a chance to turn off.
422 (run-hooks 'mouse-leave-buffer-hook)
423 (let* ((done nil)
424 (echo-keystrokes 0)
425 (start (event-start start-event))
426 (start-event-window (posn-window start))
427 (start-event-frame (window-frame start-event-window))
428 (start-nwindows (count-windows t))
429 (on-link (and mouse-1-click-follows-link
430 (or mouse-1-click-in-non-selected-windows
431 (eq (posn-window start) (selected-window)))
432 (mouse-on-link-p start)))
433 (minibuffer (frame-parameter nil 'minibuffer))
434 should-enlarge-minibuffer event mouse y top bot edges wconfig growth)
435 (track-mouse
436 (progn
437 ;; if this is the bottommost ordinary window, then to
438 ;; move its modeline the minibuffer must be enlarged.
439 (setq should-enlarge-minibuffer
440 (and minibuffer
441 mode-line-p
442 (not (one-window-p t))
443 (= (nth 1 (window-edges minibuffer))
444 (nth 3 (window-edges start-event-window)))))
446 ;; loop reading events and sampling the position of
447 ;; the mouse.
448 (while (not done)
449 (setq event (read-event)
450 mouse (mouse-position))
452 ;; do nothing if
453 ;; - there is a switch-frame event.
454 ;; - the mouse isn't in the frame that we started in
455 ;; - the mouse isn't in any Emacs frame
456 ;; drag if
457 ;; - there is a mouse-movement event
458 ;; - there is a scroll-bar-movement event
459 ;; (same as mouse movement for our purposes)
460 ;; quit if
461 ;; - there is a keyboard event or some other unknown event.
462 (cond ((not (consp event))
463 (setq done t))
465 ((memq (car event) '(switch-frame select-window))
466 nil)
468 ((not (memq (car event) '(mouse-movement scroll-bar-movement)))
469 (when (consp event)
470 ;; Do not unread a drag-mouse-1 event since it will cause the
471 ;; selection of the window above when dragging the modeline
472 ;; above the selected window.
473 (unless (eq (car event) 'drag-mouse-1)
474 (push event unread-command-events)))
475 (setq done t))
477 ((not (eq (car mouse) start-event-frame))
478 nil)
480 ((null (car (cdr mouse)))
481 nil)
484 (setq y (cdr (cdr mouse))
485 edges (window-edges start-event-window)
486 top (nth 1 edges)
487 bot (nth 3 edges))
489 ;; compute size change needed
490 (cond (mode-line-p
491 (setq growth (- y bot -1)))
492 (t ; header line
493 (when (< (- bot y) window-min-height)
494 (setq y (- bot window-min-height)))
495 ;; The window's top includes the header line!
496 (setq growth (- top y))))
497 (setq wconfig (current-window-configuration))
499 ;; Check for an error case.
500 (when (and (/= growth 0)
501 (not minibuffer)
502 (one-window-p t))
503 (error "Attempt to resize sole window"))
505 ;; If we ever move, make sure we don't mistakenly treat
506 ;; some unexpected `mouse-1' final event as a sign that
507 ;; this whole drag was nothing more than a click.
508 (if (/= growth 0) (setq on-link nil))
510 ;; grow/shrink minibuffer?
511 (if should-enlarge-minibuffer
512 (unless resize-mini-windows
513 (mouse-drag-move-window-bottom start-event-window growth))
514 ;; no. grow/shrink the selected window
515 ;(message "growth = %d" growth)
516 (if mode-line-p
517 (mouse-drag-move-window-bottom start-event-window growth)
518 (mouse-drag-move-window-top start-event-window growth)))
520 ;; if this window's growth caused another
521 ;; window to be deleted because it was too
522 ;; short, rescind the change.
524 ;; if size change caused space to be stolen
525 ;; from a window above this one, rescind the
526 ;; change, but only if we didn't grow/shrink
527 ;; the minibuffer. minibuffer size changes
528 ;; can cause all windows to shrink... no way
529 ;; around it.
530 (when (or (/= start-nwindows (count-windows t))
531 (and (not should-enlarge-minibuffer)
532 (> growth 0)
533 mode-line-p
534 (/= top
535 (nth 1 (window-edges
536 ;; Choose right window.
537 start-event-window)))))
538 (set-window-configuration wconfig)))))
540 ;; Presumably if this was just a click, the last event should
541 ;; be `mouse-1', whereas if this did move the mouse, it should be
542 ;; a `drag-mouse-1'. In any case `on-link' would have been nulled
543 ;; above if there had been any significant mouse movement.
544 (when (and on-link (eq 'mouse-1 (car-safe event)))
545 (push (cons 'mouse-2 (cdr event)) unread-command-events))))))
547 (defun mouse-drag-mode-line (start-event)
548 "Change the height of a window by dragging on the mode line."
549 (interactive "e")
550 (mouse-drag-mode-line-1 start-event t))
552 (defun mouse-drag-header-line (start-event)
553 "Change the height of a window by dragging on the header line.
554 Windows whose header-lines are at the top of the frame cannot be
555 resized by dragging their header-line."
556 (interactive "e")
557 ;; Changing the window's size by dragging its header-line when the
558 ;; header-line is at the top of the frame is somewhat strange,
559 ;; because the header-line doesn't move, so don't do it.
560 (let* ((start (event-start start-event))
561 (window (posn-window start))
562 (frame (window-frame window))
563 (first-window (frame-first-window frame)))
564 (unless (or (eq window first-window)
565 (= (nth 1 (window-edges window))
566 (nth 1 (window-edges first-window))))
567 (mouse-drag-mode-line-1 start-event nil))))
570 (defun mouse-drag-vertical-line-rightward-window (window)
571 "Return a window that is immediately to the right of WINDOW, or nil."
572 (let ((bottom (nth 3 (window-inside-edges window)))
573 (left (nth 0 (window-inside-edges window)))
574 best best-right
575 (try (previous-window window)))
576 (while (not (eq try window))
577 (let ((try-top (nth 1 (window-inside-edges try)))
578 (try-bottom (nth 3 (window-inside-edges try)))
579 (try-right (nth 2 (window-inside-edges try))))
580 (if (and (< try-top bottom)
581 (>= try-bottom bottom)
582 (< try-right left)
583 (or (null best-right) (> try-right best-right)))
584 (setq best-right try-right best try)))
585 (setq try (previous-window try)))
586 best))
588 (defun mouse-drag-vertical-line (start-event)
589 "Change the width of a window by dragging on the vertical line."
590 (interactive "e")
591 ;; Give temporary modes such as isearch a chance to turn off.
592 (run-hooks 'mouse-leave-buffer-hook)
593 (let* ((done nil)
594 (echo-keystrokes 0)
595 (start-event-frame (window-frame (car (car (cdr start-event)))))
596 (start-event-window (car (car (cdr start-event))))
597 event mouse x left right edges growth
598 (which-side
599 (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
600 'right)))
601 (cond
602 ((one-window-p t)
603 (error "Attempt to resize sole ordinary window"))
604 ((and (eq which-side 'right)
605 (>= (nth 2 (window-inside-edges start-event-window))
606 (frame-width start-event-frame)))
607 (error "Attempt to drag rightmost scrollbar"))
608 ((and (eq which-side 'left)
609 (= (nth 0 (window-inside-edges start-event-window)) 0))
610 (error "Attempt to drag leftmost scrollbar")))
611 (track-mouse
612 (progn
613 ;; loop reading events and sampling the position of
614 ;; the mouse.
615 (while (not done)
616 (setq event (read-event)
617 mouse (mouse-position))
618 ;; do nothing if
619 ;; - there is a switch-frame event.
620 ;; - the mouse isn't in the frame that we started in
621 ;; - the mouse isn't in any Emacs frame
622 ;; drag if
623 ;; - there is a mouse-movement event
624 ;; - there is a scroll-bar-movement event
625 ;; (same as mouse movement for our purposes)
626 ;; quit if
627 ;; - there is a keyboard event or some other unknown event
628 ;; unknown event.
629 (cond ((integerp event)
630 (setq done t))
631 ((memq (car event) '(switch-frame select-window))
632 nil)
633 ((not (memq (car event)
634 '(mouse-movement scroll-bar-movement)))
635 (if (consp event)
636 (setq unread-command-events
637 (cons event unread-command-events)))
638 (setq done t))
639 ((not (eq (car mouse) start-event-frame))
640 nil)
641 ((null (car (cdr mouse)))
642 nil)
644 (let ((window
645 ;; If the scroll bar is on the window's left,
646 ;; adjust the window on the left.
647 (if (eq which-side 'right)
648 start-event-window
649 (mouse-drag-vertical-line-rightward-window
650 start-event-window))))
651 (setq x (- (car (cdr mouse))
652 (if (eq which-side 'right) 0 2))
653 edges (window-edges window)
654 left (nth 0 edges)
655 right (nth 2 edges))
656 ;; scale back a move that would make the
657 ;; window too thin.
658 (if (< (- x left -1) window-min-width)
659 (setq x (+ left window-min-width -1)))
660 ;; compute size change needed
661 (setq growth (- x right -1))
662 (condition-case nil
663 (adjust-window-trailing-edge window growth t)
664 (error nil))))))))))
666 (defun mouse-set-point (event)
667 "Move point to the position clicked on with the mouse.
668 This should be bound to a mouse click event type."
669 (interactive "e")
670 (mouse-minibuffer-check event)
671 ;; Use event-end in case called from mouse-drag-region.
672 ;; If EVENT is a click, event-end and event-start give same value.
673 (posn-set-point (event-end event)))
675 (defvar mouse-last-region-beg nil)
676 (defvar mouse-last-region-end nil)
677 (defvar mouse-last-region-tick nil)
679 (defun mouse-region-match ()
680 "Return non-nil if there's an active region that was set with the mouse."
681 (and (mark t) mark-active
682 (eq mouse-last-region-beg (region-beginning))
683 (eq mouse-last-region-end (region-end))
684 (eq mouse-last-region-tick (buffer-modified-tick))))
686 (defun mouse-set-region (click)
687 "Set the region to the text dragged over, and copy to kill ring.
688 This should be bound to a mouse drag event."
689 (interactive "e")
690 (mouse-minibuffer-check click)
691 (select-window (posn-window (event-start click)))
692 (let ((beg (posn-point (event-start click)))
693 (end (posn-point (event-end click))))
694 (and mouse-drag-copy-region (integerp beg) (integerp end)
695 ;; Don't set this-command to `kill-region', so a following
696 ;; C-w won't double the text in the kill ring. Ignore
697 ;; `last-command' so we don't append to a preceding kill.
698 (let (this-command last-command deactivate-mark)
699 (copy-region-as-kill beg end)))
700 (if (numberp beg) (goto-char beg))
701 ;; On a text terminal, bounce the cursor.
702 (or transient-mark-mode
703 (window-system)
704 (sit-for 1))
705 (push-mark)
706 (set-mark (point))
707 (if (numberp end) (goto-char end))
708 (mouse-set-region-1)))
710 (defun mouse-set-region-1 ()
711 ;; Set transient-mark-mode for a little while.
712 (unless (eq (car-safe transient-mark-mode) 'only)
713 (setq transient-mark-mode
714 (cons 'only
715 (unless (eq transient-mark-mode 'lambda)
716 transient-mark-mode))))
717 (setq mouse-last-region-beg (region-beginning))
718 (setq mouse-last-region-end (region-end))
719 (setq mouse-last-region-tick (buffer-modified-tick)))
721 (defcustom mouse-scroll-delay 0.25
722 "The pause between scroll steps caused by mouse drags, in seconds.
723 If you drag the mouse beyond the edge of a window, Emacs scrolls the
724 window to bring the text beyond that edge into view, with a delay of
725 this many seconds between scroll steps. Scrolling stops when you move
726 the mouse back into the window, or release the button.
727 This variable's value may be non-integral.
728 Setting this to zero causes Emacs to scroll as fast as it can."
729 :type 'number
730 :group 'mouse)
732 (defcustom mouse-scroll-min-lines 1
733 "The minimum number of lines scrolled by dragging mouse out of window.
734 Moving the mouse out the top or bottom edge of the window begins
735 scrolling repeatedly. The number of lines scrolled per repetition
736 is normally equal to the number of lines beyond the window edge that
737 the mouse has moved. However, it always scrolls at least the number
738 of lines specified by this variable."
739 :type 'integer
740 :group 'mouse)
742 (defun mouse-scroll-subr (window jump &optional overlay start)
743 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
744 If OVERLAY is an overlay, let it stretch from START to the far edge of
745 the newly visible text.
746 Upon exit, point is at the far edge of the newly visible text."
747 (cond
748 ((and (> jump 0) (< jump mouse-scroll-min-lines))
749 (setq jump mouse-scroll-min-lines))
750 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
751 (setq jump (- mouse-scroll-min-lines))))
752 (let ((opoint (point)))
753 (while (progn
754 (goto-char (window-start window))
755 (if (not (zerop (vertical-motion jump window)))
756 (progn
757 (set-window-start window (point))
758 (if (natnump jump)
759 (if (window-end window)
760 (progn
761 (goto-char (window-end window))
762 ;; window-end doesn't reflect the window's new
763 ;; start position until the next redisplay.
764 (vertical-motion (1- jump) window))
765 (vertical-motion (- (window-height window) 2)))
766 (goto-char (window-start window)))
767 (if overlay
768 (move-overlay overlay start (point)))
769 ;; Now that we have scrolled WINDOW properly,
770 ;; put point back where it was for the redisplay
771 ;; so that we don't mess up the selected window.
772 (or (eq window (selected-window))
773 (goto-char opoint))
774 (sit-for mouse-scroll-delay)))))
775 (or (eq window (selected-window))
776 (goto-char opoint))))
778 (defvar mouse-selection-click-count 0)
780 (defvar mouse-selection-click-count-buffer nil)
782 (defun mouse-drag-region (start-event)
783 "Set the region to the text that the mouse is dragged over.
784 Highlight the drag area as you move the mouse.
785 This must be bound to a button-down mouse event.
786 In Transient Mark mode, the highlighting remains as long as the mark
787 remains active. Otherwise, it remains until the next input event.
789 If the click is in the echo area, display the `*Messages*' buffer."
790 (interactive "e")
791 (let ((w (posn-window (event-start start-event))))
792 (if (and (window-minibuffer-p w)
793 (not (minibuffer-window-active-p w)))
794 (save-excursion
795 ;; Swallow the up-event.
796 (read-event)
797 (set-buffer (get-buffer-create "*Messages*"))
798 (goto-char (point-max))
799 (display-buffer (current-buffer)))
800 ;; Give temporary modes such as isearch a chance to turn off.
801 (run-hooks 'mouse-leave-buffer-hook)
802 (mouse-drag-track start-event t))))
805 (defun mouse-posn-property (pos property)
806 "Look for a property at click position.
807 POS may be either a buffer position or a click position like
808 those returned from `event-start'. If the click position is on
809 a string, the text property PROPERTY is examined.
810 If this is nil or the click is not on a string, then
811 the corresponding buffer position is searched for PROPERTY.
812 If PROPERTY is encountered in one of those places,
813 its value is returned."
814 (if (consp pos)
815 (let ((w (posn-window pos)) (pt (posn-point pos))
816 (str (posn-string pos)))
817 (or (and str
818 (get-text-property (cdr str) property (car str)))
819 (and pt
820 (get-char-property pt property w))))
821 (get-char-property pos property)))
823 (defun mouse-on-link-p (pos)
824 "Return non-nil if POS is on a link in the current buffer.
825 POS must be a buffer position in the current buffer or a mouse
826 event location in the selected window (see `event-start').
827 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
828 POS may be a mouse event location in any window.
830 A clickable link is identified by one of the following methods:
832 - If the character at POS has a non-nil `follow-link' text or
833 overlay property, the value of that property determines what to do.
835 - If there is a local key-binding or a keybinding at position POS
836 for the `follow-link' event, the binding of that event determines
837 what to do.
839 The resulting value determine whether POS is inside a link:
841 - If the value is `mouse-face', POS is inside a link if there
842 is a non-nil `mouse-face' property at POS. Return t in this case.
844 - If the value is a function, FUNC, POS is inside a link if
845 the call \(FUNC POS) returns non-nil. Return the return value
846 from that call. Arg is \(posn-point POS) if POS is a mouse event.
848 - Otherwise, return the value itself.
850 The return value is interpreted as follows:
852 - If it is a string, the mouse-1 event is translated into the
853 first character of the string, i.e. the action of the mouse-1
854 click is the local or global binding of that character.
856 - If it is a vector, the mouse-1 event is translated into the
857 first element of that vector, i.e. the action of the mouse-1
858 click is the local or global binding of that event.
860 - Otherwise, the mouse-1 event is translated into a mouse-2 event
861 at the same position."
862 (let ((action
863 (and (or (not (consp pos))
864 mouse-1-click-in-non-selected-windows
865 (eq (selected-window) (posn-window pos)))
866 (or (mouse-posn-property pos 'follow-link)
867 (key-binding [follow-link] nil t pos)))))
868 (cond
869 ((eq action 'mouse-face)
870 (and (mouse-posn-property pos 'mouse-face) t))
871 ((functionp action)
872 ;; FIXME: This seems questionable if the click is not in a buffer.
873 ;; Should we instead decide that `action' takes a `posn'?
874 (if (consp pos)
875 (with-current-buffer (window-buffer (posn-window pos))
876 (funcall action (posn-point pos)))
877 (funcall action pos)))
878 (t action))))
880 (defun mouse-fixup-help-message (msg)
881 "Fix help message MSG for `mouse-1-click-follows-link'."
882 (let (mp pos)
883 (if (and mouse-1-click-follows-link
884 (stringp msg)
885 (string-match-p "\\`mouse-2" msg)
886 (setq mp (mouse-pixel-position))
887 (consp (setq pos (cdr mp)))
888 (car pos) (>= (car pos) 0)
889 (cdr pos) (>= (cdr pos) 0)
890 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
891 (windowp (posn-window pos)))
892 (with-current-buffer (window-buffer (posn-window pos))
893 (if (mouse-on-link-p pos)
894 (setq msg (concat
895 (cond
896 ((eq mouse-1-click-follows-link 'double) "double-")
897 ((and (integerp mouse-1-click-follows-link)
898 (< mouse-1-click-follows-link 0)) "Long ")
899 (t ""))
900 "mouse-1" (substring msg 7)))))))
901 msg)
903 (defun mouse-drag-track (start-event &optional
904 do-mouse-drag-region-post-process)
905 "Track mouse drags by highlighting area between point and cursor.
906 The region will be defined with mark and point.
907 DO-MOUSE-DRAG-REGION-POST-PROCESS should only be used by
908 `mouse-drag-region'."
909 (mouse-minibuffer-check start-event)
910 (setq mouse-selection-click-count-buffer (current-buffer))
911 (deactivate-mark)
912 (let* ((original-window (selected-window))
913 ;; We've recorded what we needed from the current buffer and
914 ;; window, now let's jump to the place of the event, where things
915 ;; are happening.
916 (_ (mouse-set-point start-event))
917 (echo-keystrokes 0)
918 (start-posn (event-start start-event))
919 (start-point (posn-point start-posn))
920 (start-window (posn-window start-posn))
921 (start-window-start (window-start start-window))
922 (start-hscroll (window-hscroll start-window))
923 (bounds (window-edges start-window))
924 (make-cursor-line-fully-visible nil)
925 (top (nth 1 bounds))
926 (bottom (if (window-minibuffer-p start-window)
927 (nth 3 bounds)
928 ;; Don't count the mode line.
929 (1- (nth 3 bounds))))
930 (on-link (and mouse-1-click-follows-link
931 (or mouse-1-click-in-non-selected-windows
932 (eq start-window original-window))
933 ;; Use start-point before the intangibility
934 ;; treatment, in case we click on a link inside an
935 ;; intangible text.
936 (mouse-on-link-p start-posn)))
937 (click-count (1- (event-click-count start-event)))
938 (remap-double-click (and on-link
939 (eq mouse-1-click-follows-link 'double)
940 (= click-count 1)))
941 ;; Suppress automatic hscrolling, because that is a nuisance
942 ;; when setting point near the right fringe (but see below).
943 (automatic-hscrolling-saved automatic-hscrolling)
944 (automatic-hscrolling nil)
945 event end end-point)
947 (setq mouse-selection-click-count click-count)
948 ;; In case the down click is in the middle of some intangible text,
949 ;; use the end of that text, and put it in START-POINT.
950 (if (< (point) start-point)
951 (goto-char start-point))
952 (setq start-point (point))
953 (if remap-double-click
954 (setq click-count 0))
956 ;; Activate the region, using `mouse-start-end' to determine where
957 ;; to put point and mark (e.g., double-click will select a word).
958 (setq transient-mark-mode
959 (if (eq transient-mark-mode 'lambda)
960 '(only)
961 (cons 'only transient-mark-mode)))
962 (let ((range (mouse-start-end start-point start-point click-count)))
963 (push-mark (nth 0 range) t t)
964 (goto-char (nth 1 range)))
966 ;; Track the mouse until we get a non-movement event.
967 (track-mouse
968 (while (progn
969 (setq event (read-event))
970 (or (mouse-movement-p event)
971 (memq (car-safe event) '(switch-frame select-window))))
972 (unless (memq (car-safe event) '(switch-frame select-window))
973 ;; Automatic hscrolling did not occur during the call to
974 ;; `read-event'; but if the user subsequently drags the
975 ;; mouse, go ahead and hscroll.
976 (let ((automatic-hscrolling automatic-hscrolling-saved))
977 (redisplay))
978 (setq end (event-end event)
979 end-point (posn-point end))
980 (if (and (eq (posn-window end) start-window)
981 (integer-or-marker-p end-point))
982 (mouse--drag-set-mark-and-point start-point
983 end-point click-count)
984 (let ((mouse-row (cdr (cdr (mouse-position)))))
985 (cond
986 ((null mouse-row))
987 ((< mouse-row top)
988 (mouse-scroll-subr start-window (- mouse-row top)
989 nil start-point))
990 ((>= mouse-row bottom)
991 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
992 nil start-point))))))))
994 ;; Handle the terminating event if possible.
995 (when (consp event)
996 ;; Ensure that point is on the end of the last event.
997 (when (and (setq end-point (posn-point (event-end event)))
998 (eq (posn-window end) start-window)
999 (integer-or-marker-p end-point)
1000 (/= start-point end-point))
1001 (mouse--drag-set-mark-and-point start-point
1002 end-point click-count))
1004 ;; Find its binding.
1005 (let* ((fun (key-binding (vector (car event))))
1006 (do-multi-click (and (> (event-click-count event) 0)
1007 (functionp fun)
1008 (not (memq fun '(mouse-set-point
1009 mouse-set-region))))))
1010 (if (and (/= (mark) (point))
1011 (not do-multi-click))
1013 ;; If point has moved, finish the drag.
1014 (let* (last-command this-command)
1015 (and mouse-drag-copy-region
1016 do-mouse-drag-region-post-process
1017 (let (deactivate-mark)
1018 (copy-region-as-kill (mark) (point)))))
1020 ;; If point hasn't moved, run the binding of the
1021 ;; terminating up-event.
1022 (if do-multi-click
1023 (goto-char start-point)
1024 (deactivate-mark))
1025 (when (and (functionp fun)
1026 (= start-hscroll (window-hscroll start-window))
1027 ;; Don't run the up-event handler if the window
1028 ;; start changed in a redisplay after the
1029 ;; mouse-set-point for the down-mouse event at
1030 ;; the beginning of this function. When the
1031 ;; window start has changed, the up-mouse event
1032 ;; contains a different position due to the new
1033 ;; window contents, and point is set again.
1034 (or end-point
1035 (= (window-start start-window)
1036 start-window-start)))
1037 (when (and on-link
1038 (= start-point (point))
1039 (mouse--remap-link-click-p start-event event))
1040 ;; If we rebind to mouse-2, reselect previous selected
1041 ;; window, so that the mouse-2 event runs in the same
1042 ;; situation as if user had clicked it directly. Fixes
1043 ;; the bug reported by juri@jurta.org on 2005-12-27.
1044 (if (or (vectorp on-link) (stringp on-link))
1045 (setq event (aref on-link 0))
1046 (select-window original-window)
1047 (setcar event 'mouse-2)
1048 ;; If this mouse click has never been done by the
1049 ;; user, it doesn't have the necessary property to be
1050 ;; interpreted correctly.
1051 (put 'mouse-2 'event-kind 'mouse-click)))
1052 (push event unread-command-events)))))))
1054 (defun mouse--drag-set-mark-and-point (start click click-count)
1055 (let* ((range (mouse-start-end start click click-count))
1056 (beg (nth 0 range))
1057 (end (nth 1 range)))
1058 (cond ((eq (mark) beg)
1059 (goto-char end))
1060 ((eq (mark) end)
1061 (goto-char beg))
1062 ((< click (mark))
1063 (set-mark end)
1064 (goto-char beg))
1066 (set-mark beg)
1067 (goto-char end)))))
1069 (defun mouse--remap-link-click-p (start-event end-event)
1070 (or (and (eq mouse-1-click-follows-link 'double)
1071 (= (event-click-count start-event) 2))
1072 (and
1073 (not (eq mouse-1-click-follows-link 'double))
1074 (= (event-click-count start-event) 1)
1075 (= (event-click-count end-event) 1)
1076 (or (not (integerp mouse-1-click-follows-link))
1077 (let ((t0 (posn-timestamp (event-start start-event)))
1078 (t1 (posn-timestamp (event-end end-event))))
1079 (and (integerp t0) (integerp t1)
1080 (if (> mouse-1-click-follows-link 0)
1081 (<= (- t1 t0) mouse-1-click-follows-link)
1082 (< (- t0 t1) mouse-1-click-follows-link))))))))
1085 ;; Commands to handle xterm-style multiple clicks.
1086 (defun mouse-skip-word (dir)
1087 "Skip over word, over whitespace, or over identical punctuation.
1088 If DIR is positive skip forward; if negative, skip backward."
1089 (let* ((char (following-char))
1090 (syntax (char-to-string (char-syntax char))))
1091 (cond ((string= syntax "w")
1092 ;; Here, we can't use skip-syntax-forward/backward because
1093 ;; they don't pay attention to word-separating-categories,
1094 ;; and thus they will skip over a true word boundary. So,
1095 ;; we simulate the original behavior by using forward-word.
1096 (if (< dir 0)
1097 (if (not (looking-at "\\<"))
1098 (forward-word -1))
1099 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1100 (forward-word 1))))
1101 ((string= syntax " ")
1102 (if (< dir 0)
1103 (skip-syntax-backward syntax)
1104 (skip-syntax-forward syntax)))
1105 ((string= syntax "_")
1106 (if (< dir 0)
1107 (skip-syntax-backward "w_")
1108 (skip-syntax-forward "w_")))
1109 ((< dir 0)
1110 (while (and (not (bobp)) (= (preceding-char) char))
1111 (forward-char -1)))
1113 (while (and (not (eobp)) (= (following-char) char))
1114 (forward-char 1))))))
1116 (defun mouse-start-end (start end mode)
1117 "Return a list of region bounds based on START and END according to MODE.
1118 If MODE is 0 then set point to (min START END), mark to (max START END).
1119 If MODE is 1 then set point to start of word at (min START END),
1120 mark to end of word at (max START END).
1121 If MODE is 2 then do the same for lines."
1122 (if (> start end)
1123 (let ((temp start))
1124 (setq start end
1125 end temp)))
1126 (setq mode (mod mode 3))
1127 (cond ((= mode 0)
1128 (list start end))
1129 ((and (= mode 1)
1130 (= start end)
1131 (char-after start)
1132 (= (char-syntax (char-after start)) ?\())
1133 (list start
1134 (save-excursion
1135 (goto-char start)
1136 (forward-sexp 1)
1137 (point))))
1138 ((and (= mode 1)
1139 (= start end)
1140 (char-after start)
1141 (= (char-syntax (char-after start)) ?\)))
1142 (list (save-excursion
1143 (goto-char (1+ start))
1144 (backward-sexp 1)
1145 (point))
1146 (1+ start)))
1147 ((and (= mode 1)
1148 (= start end)
1149 (char-after start)
1150 (= (char-syntax (char-after start)) ?\"))
1151 (let ((open (or (eq start (point-min))
1152 (save-excursion
1153 (goto-char (- start 1))
1154 (looking-at "\\s(\\|\\s \\|\\s>")))))
1155 (if open
1156 (list start
1157 (save-excursion
1158 (condition-case nil
1159 (progn
1160 (goto-char start)
1161 (forward-sexp 1)
1162 (point))
1163 (error end))))
1164 (list (save-excursion
1165 (condition-case nil
1166 (progn
1167 (goto-char (1+ start))
1168 (backward-sexp 1)
1169 (point))
1170 (error end)))
1171 (1+ start)))))
1172 ((= mode 1)
1173 (list (save-excursion
1174 (goto-char start)
1175 (mouse-skip-word -1)
1176 (point))
1177 (save-excursion
1178 (goto-char end)
1179 (mouse-skip-word 1)
1180 (point))))
1181 ((= mode 2)
1182 (list (save-excursion
1183 (goto-char start)
1184 (line-beginning-position 1))
1185 (save-excursion
1186 (goto-char end)
1187 (forward-line 1)
1188 (point))))))
1190 ;; Subroutine: set the mark where CLICK happened,
1191 ;; but don't do anything else.
1192 (defun mouse-set-mark-fast (click)
1193 (mouse-minibuffer-check click)
1194 (let ((posn (event-start click)))
1195 (select-window (posn-window posn))
1196 (if (numberp (posn-point posn))
1197 (push-mark (posn-point posn) t t))))
1199 (defun mouse-undouble-last-event (events)
1200 (let* ((index (1- (length events)))
1201 (last (nthcdr index events))
1202 (event (car last))
1203 (basic (event-basic-type event))
1204 (old-modifiers (event-modifiers event))
1205 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1206 (new
1207 (if (consp event)
1208 ;; Use reverse, not nreverse, since event-modifiers
1209 ;; does not copy the list it returns.
1210 (cons (event-convert-list (reverse (cons basic modifiers)))
1211 (cdr event))
1212 event)))
1213 (setcar last new)
1214 (if (and (not (equal modifiers old-modifiers))
1215 (key-binding (apply 'vector events)))
1217 (setcar last event)
1218 nil)))
1220 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1222 (defun mouse-set-mark (click)
1223 "Set mark at the position clicked on with the mouse.
1224 Display cursor at that position for a second.
1225 This must be bound to a mouse click."
1226 (interactive "e")
1227 (mouse-minibuffer-check click)
1228 (select-window (posn-window (event-start click)))
1229 ;; We don't use save-excursion because that preserves the mark too.
1230 (let ((point-save (point)))
1231 (unwind-protect
1232 (progn (mouse-set-point click)
1233 (push-mark nil t t)
1234 (or transient-mark-mode
1235 (sit-for 1)))
1236 (goto-char point-save))))
1238 (defun mouse-kill (click)
1239 "Kill the region between point and the mouse click.
1240 The text is saved in the kill ring, as with \\[kill-region]."
1241 (interactive "e")
1242 (mouse-minibuffer-check click)
1243 (let* ((posn (event-start click))
1244 (click-posn (posn-point posn)))
1245 (select-window (posn-window posn))
1246 (if (numberp click-posn)
1247 (kill-region (min (point) click-posn)
1248 (max (point) click-posn)))))
1250 (defun mouse-yank-at-click (click arg)
1251 "Insert the last stretch of killed text at the position clicked on.
1252 Also move point to one end of the text thus inserted (normally the end),
1253 and set mark at the beginning.
1254 Prefix arguments are interpreted as with \\[yank].
1255 If `mouse-yank-at-point' is non-nil, insert at point
1256 regardless of where you click."
1257 (interactive "e\nP")
1258 ;; Give temporary modes such as isearch a chance to turn off.
1259 (run-hooks 'mouse-leave-buffer-hook)
1260 (when select-active-regions
1261 ;; Without this, confusing things happen upon e.g. inserting into
1262 ;; the middle of an active region.
1263 (deactivate-mark))
1264 (or mouse-yank-at-point (mouse-set-point click))
1265 (setq this-command 'yank)
1266 (setq mouse-selection-click-count 0)
1267 (yank arg))
1269 (defun mouse-yank-primary (click)
1270 "Insert the primary selection at the position clicked on.
1271 Move point to the end of the inserted text.
1272 If `mouse-yank-at-point' is non-nil, insert at point
1273 regardless of where you click."
1274 (interactive "e")
1275 ;; Give temporary modes such as isearch a chance to turn off.
1276 (run-hooks 'mouse-leave-buffer-hook)
1277 ;; Without this, confusing things happen upon e.g. inserting into
1278 ;; the middle of an active region.
1279 (when select-active-regions
1280 (let (select-active-regions)
1281 (deactivate-mark)))
1282 (or mouse-yank-at-point (mouse-set-point click))
1283 (let ((primary
1284 (cond
1285 ((fboundp 'x-get-selection-value) ; MS-DOS, MS-Windows and X.
1286 (or (x-get-selection-value)
1287 (x-get-selection 'PRIMARY)))
1288 ;; FIXME: What about xterm-mouse-mode etc.?
1290 (x-get-selection 'PRIMARY)))))
1291 (if primary
1292 (insert primary)
1293 (error "No selection is available"))))
1295 (defun mouse-kill-ring-save (click)
1296 "Copy the region between point and the mouse click in the kill ring.
1297 This does not delete the region; it acts like \\[kill-ring-save]."
1298 (interactive "e")
1299 (mouse-set-mark-fast click)
1300 (let (this-command last-command)
1301 (kill-ring-save (point) (mark t))))
1303 ;; This function used to delete the text between point and the mouse
1304 ;; whenever it was equal to the front of the kill ring, but some
1305 ;; people found that confusing.
1307 ;; The position of the last invocation of `mouse-save-then-kill'.
1308 (defvar mouse-save-then-kill-posn nil)
1310 (defun mouse-save-then-kill-delete-region (beg end)
1311 ;; We must make our own undo boundaries
1312 ;; because they happen automatically only for the current buffer.
1313 (undo-boundary)
1314 (if (or (= beg end) (eq buffer-undo-list t))
1315 ;; If we have no undo list in this buffer,
1316 ;; just delete.
1317 (delete-region beg end)
1318 ;; Delete, but make the undo-list entry share with the kill ring.
1319 ;; First, delete just one char, so in case buffer is being modified
1320 ;; for the first time, the undo list records that fact.
1321 (let (before-change-functions after-change-functions)
1322 (delete-region beg
1323 (+ beg (if (> end beg) 1 -1))))
1324 (let ((buffer-undo-list buffer-undo-list))
1325 ;; Undo that deletion--but don't change the undo list!
1326 (let (before-change-functions after-change-functions)
1327 (primitive-undo 1 buffer-undo-list))
1328 ;; Now delete the rest of the specified region,
1329 ;; but don't record it.
1330 (setq buffer-undo-list t)
1331 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1332 (error "Lossage in mouse-save-then-kill-delete-region"))
1333 (delete-region beg end))
1334 (let ((tail buffer-undo-list))
1335 ;; Search back in buffer-undo-list for the string
1336 ;; that came from deleting one character.
1337 (while (and tail (not (stringp (car (car tail)))))
1338 (setq tail (cdr tail)))
1339 ;; Replace it with an entry for the entire deleted text.
1340 (and tail
1341 (setcar tail (cons (car kill-ring) (min beg end))))))
1342 (undo-boundary))
1344 (defun mouse-save-then-kill (click)
1345 "Set the region according to CLICK; the second time, kill it.
1346 CLICK should be a mouse click event.
1348 If the region is inactive, activate it temporarily. Set mark at
1349 the original point, and move point to the position of CLICK.
1351 If the region is already active, adjust it. Normally, do this by
1352 moving point or mark, whichever is closer, to CLICK. But if you
1353 have selected whole words or lines, move point or mark to the
1354 word or line boundary closest to CLICK instead.
1356 If `mouse-drag-copy-region' is non-nil, this command also saves the
1357 new region to the kill ring (replacing the previous kill if the
1358 previous region was just saved to the kill ring).
1360 If this command is called a second consecutive time with the same
1361 CLICK position, kill the region (or delete it
1362 if `mouse-drag-copy-region' is non-nil)"
1363 (interactive "e")
1364 (mouse-minibuffer-check click)
1365 (let* ((posn (event-start click))
1366 (click-pt (posn-point posn))
1367 (window (posn-window posn))
1368 (buf (window-buffer window))
1369 ;; Don't let a subsequent kill command append to this one.
1370 (this-command this-command)
1371 ;; Check if the user has multi-clicked to select words/lines.
1372 (click-count
1373 (if (and (eq mouse-selection-click-count-buffer buf)
1374 (with-current-buffer buf (mark t)))
1375 mouse-selection-click-count
1376 0)))
1377 (cond
1378 ((not (numberp click-pt)) nil)
1379 ;; If the user clicked without moving point, kill the region.
1380 ;; This also resets `mouse-selection-click-count'.
1381 ((and (eq last-command 'mouse-save-then-kill)
1382 (eq click-pt mouse-save-then-kill-posn)
1383 (eq window (selected-window)))
1384 (if mouse-drag-copy-region
1385 ;; Region already saved in the previous click;
1386 ;; don't make a duplicate entry, just delete.
1387 (delete-region (mark t) (point))
1388 (kill-region (mark t) (point)))
1389 (setq mouse-selection-click-count 0)
1390 (setq mouse-save-then-kill-posn nil))
1392 ;; Otherwise, if there is a suitable region, adjust it by moving
1393 ;; one end (whichever is closer) to CLICK-PT.
1394 ((or (with-current-buffer buf (region-active-p))
1395 (and (eq window (selected-window))
1396 (mark t)
1397 (or (and (eq last-command 'mouse-save-then-kill)
1398 mouse-save-then-kill-posn)
1399 (and (memq last-command '(mouse-drag-region
1400 mouse-set-region))
1401 (or mark-even-if-inactive
1402 (not transient-mark-mode))))))
1403 (select-window window)
1404 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1405 (if (< (abs (- click-pt (mark t)))
1406 (abs (- click-pt (point))))
1407 (set-mark (car range))
1408 (goto-char (nth 1 range)))
1409 (setq deactivate-mark nil)
1410 (mouse-set-region-1)
1411 (when mouse-drag-copy-region
1412 ;; Region already copied to kill-ring once, so replace.
1413 (kill-new (filter-buffer-substring (mark t) (point)) t))
1414 ;; Arrange for a repeated mouse-3 to kill the region.
1415 (setq mouse-save-then-kill-posn click-pt)))
1417 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1419 (select-window window)
1420 (mouse-set-mark-fast click)
1421 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1422 (if before-scroll (goto-char before-scroll)))
1423 (exchange-point-and-mark)
1424 (mouse-set-region-1)
1425 (when mouse-drag-copy-region
1426 (kill-new (filter-buffer-substring (mark t) (point))))
1427 (setq mouse-save-then-kill-posn click-pt)))))
1430 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1431 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1432 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1433 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1434 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1436 (defconst mouse-secondary-overlay
1437 (let ((ol (make-overlay (point-min) (point-min))))
1438 (delete-overlay ol)
1439 (overlay-put ol 'face 'secondary-selection)
1441 "An overlay which records the current secondary selection.
1442 It is deleted when there is no secondary selection.")
1444 (defvar mouse-secondary-click-count 0)
1446 ;; A marker which records the specified first end for a secondary selection.
1447 ;; May be nil.
1448 (defvar mouse-secondary-start nil)
1450 (defun mouse-start-secondary (click)
1451 "Set one end of the secondary selection to the position clicked on.
1452 Use \\[mouse-secondary-save-then-kill] to set the other end
1453 and complete the secondary selection."
1454 (interactive "e")
1455 (mouse-minibuffer-check click)
1456 (let ((posn (event-start click)))
1457 (with-current-buffer (window-buffer (posn-window posn))
1458 ;; Cancel any preexisting secondary selection.
1459 (delete-overlay mouse-secondary-overlay)
1460 (if (numberp (posn-point posn))
1461 (progn
1462 (or mouse-secondary-start
1463 (setq mouse-secondary-start (make-marker)))
1464 (move-marker mouse-secondary-start (posn-point posn)))))))
1466 (defun mouse-set-secondary (click)
1467 "Set the secondary selection to the text that the mouse is dragged over.
1468 This must be bound to a mouse drag event."
1469 (interactive "e")
1470 (mouse-minibuffer-check click)
1471 (let ((posn (event-start click))
1473 (end (event-end click)))
1474 (with-current-buffer (window-buffer (posn-window posn))
1475 (if (numberp (posn-point posn))
1476 (setq beg (posn-point posn)))
1477 (move-overlay mouse-secondary-overlay beg (posn-point end))
1478 (x-set-selection
1479 'SECONDARY
1480 (buffer-substring (overlay-start mouse-secondary-overlay)
1481 (overlay-end mouse-secondary-overlay))))))
1483 (defun mouse-drag-secondary (start-event)
1484 "Set the secondary selection to the text that the mouse is dragged over.
1485 Highlight the drag area as you move the mouse.
1486 This must be bound to a button-down mouse event.
1487 The function returns a non-nil value if it creates a secondary selection."
1488 (interactive "e")
1489 (mouse-minibuffer-check start-event)
1490 (let* ((echo-keystrokes 0)
1491 (start-posn (event-start start-event))
1492 (start-point (posn-point start-posn))
1493 (start-window (posn-window start-posn))
1494 (bounds (window-edges start-window))
1495 (top (nth 1 bounds))
1496 (bottom (if (window-minibuffer-p start-window)
1497 (nth 3 bounds)
1498 ;; Don't count the mode line.
1499 (1- (nth 3 bounds))))
1500 (click-count (1- (event-click-count start-event))))
1501 (with-current-buffer (window-buffer start-window)
1502 (setq mouse-secondary-click-count click-count)
1503 (if (> (mod click-count 3) 0)
1504 ;; Double or triple press: make an initial selection
1505 ;; of one word or line.
1506 (let ((range (mouse-start-end start-point start-point click-count)))
1507 (set-marker mouse-secondary-start nil)
1508 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1509 (window-buffer start-window)))
1510 ;; Single-press: cancel any preexisting secondary selection.
1511 (or mouse-secondary-start
1512 (setq mouse-secondary-start (make-marker)))
1513 (set-marker mouse-secondary-start start-point)
1514 (delete-overlay mouse-secondary-overlay))
1515 (let (event end end-point)
1516 (track-mouse
1517 (while (progn
1518 (setq event (read-event))
1519 (or (mouse-movement-p event)
1520 (memq (car-safe event) '(switch-frame select-window))))
1522 (if (memq (car-safe event) '(switch-frame select-window))
1524 (setq end (event-end event)
1525 end-point (posn-point end))
1526 (cond
1527 ;; Are we moving within the original window?
1528 ((and (eq (posn-window end) start-window)
1529 (integer-or-marker-p end-point))
1530 (let ((range (mouse-start-end start-point end-point
1531 click-count)))
1532 (if (or (/= start-point end-point)
1533 (null (marker-position mouse-secondary-start)))
1534 (progn
1535 (set-marker mouse-secondary-start nil)
1536 (move-overlay mouse-secondary-overlay
1537 (car range) (nth 1 range))))))
1539 (let ((mouse-row (cdr (cdr (mouse-position)))))
1540 (cond
1541 ((null mouse-row))
1542 ((< mouse-row top)
1543 (mouse-scroll-subr start-window (- mouse-row top)
1544 mouse-secondary-overlay start-point))
1545 ((>= mouse-row bottom)
1546 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1547 mouse-secondary-overlay start-point)))))))))
1549 (if (consp event)
1550 (if (marker-position mouse-secondary-start)
1551 (save-window-excursion
1552 (delete-overlay mouse-secondary-overlay)
1553 (x-set-selection 'SECONDARY nil)
1554 (select-window start-window)
1555 (save-excursion
1556 (goto-char mouse-secondary-start)
1557 (sit-for 1)
1558 nil))
1559 (x-set-selection
1560 'SECONDARY
1561 (buffer-substring (overlay-start mouse-secondary-overlay)
1562 (overlay-end mouse-secondary-overlay)))))))))
1564 (defun mouse-yank-secondary (click)
1565 "Insert the secondary selection at the position clicked on.
1566 Move point to the end of the inserted text.
1567 If `mouse-yank-at-point' is non-nil, insert at point
1568 regardless of where you click."
1569 (interactive "e")
1570 ;; Give temporary modes such as isearch a chance to turn off.
1571 (run-hooks 'mouse-leave-buffer-hook)
1572 (or mouse-yank-at-point (mouse-set-point click))
1573 (let ((secondary (x-get-selection 'SECONDARY)))
1574 (if secondary
1575 (insert secondary)
1576 (error "No secondary selection"))))
1578 (defun mouse-kill-secondary ()
1579 "Kill the text in the secondary selection.
1580 This is intended more as a keyboard command than as a mouse command
1581 but it can work as either one.
1583 The current buffer (in case of keyboard use), or the buffer clicked on,
1584 must be the one that the secondary selection is in. This requirement
1585 is to prevent accidents."
1586 (interactive)
1587 (let* ((keys (this-command-keys))
1588 (click (elt keys (1- (length keys)))))
1589 (or (eq (overlay-buffer mouse-secondary-overlay)
1590 (if (listp click)
1591 (window-buffer (posn-window (event-start click)))
1592 (current-buffer)))
1593 (error "Select or click on the buffer where the secondary selection is")))
1594 (let (this-command)
1595 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1596 (kill-region (overlay-start mouse-secondary-overlay)
1597 (overlay-end mouse-secondary-overlay))))
1598 (delete-overlay mouse-secondary-overlay))
1600 (defun mouse-secondary-save-then-kill (click)
1601 "Set the secondary selection and save it to the kill ring.
1602 The second time, kill it. CLICK should be a mouse click event.
1604 If you have not called `mouse-start-secondary' in the clicked
1605 buffer, activate the secondary selection and set it between point
1606 and the click position CLICK.
1608 Otherwise, adjust the bounds of the secondary selection.
1609 Normally, do this by moving its beginning or end, whichever is
1610 closer, to CLICK. But if you have selected whole words or lines,
1611 adjust to the word or line boundary closest to CLICK instead.
1613 If this command is called a second consecutive time with the same
1614 CLICK position, kill the secondary selection."
1615 (interactive "e")
1616 (mouse-minibuffer-check click)
1617 (let* ((posn (event-start click))
1618 (click-pt (posn-point posn))
1619 (window (posn-window posn))
1620 (buf (window-buffer window))
1621 ;; Don't let a subsequent kill command append to this one.
1622 (this-command this-command)
1623 ;; Check if the user has multi-clicked to select words/lines.
1624 (click-count
1625 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1626 mouse-secondary-click-count
1628 (beg (overlay-start mouse-secondary-overlay))
1629 (end (overlay-end mouse-secondary-overlay)))
1631 (cond
1632 ((not (numberp click-pt)) nil)
1634 ;; If the secondary selection is not active in BUF, activate it.
1635 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1636 (if mouse-secondary-start
1637 (marker-buffer mouse-secondary-start)))))
1638 (select-window window)
1639 (setq mouse-secondary-start (make-marker))
1640 (move-marker mouse-secondary-start (point))
1641 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1642 (kill-ring-save (point) click-pt))
1644 ;; If the user clicked without moving point, delete the secondary
1645 ;; selection. This also resets `mouse-secondary-click-count'.
1646 ((and (eq last-command 'mouse-secondary-save-then-kill)
1647 (eq click-pt mouse-save-then-kill-posn)
1648 (eq window (selected-window)))
1649 (mouse-save-then-kill-delete-region beg end)
1650 (delete-overlay mouse-secondary-overlay)
1651 (setq mouse-secondary-click-count 0)
1652 (setq mouse-save-then-kill-posn nil))
1654 ;; Otherwise, if there is a suitable secondary selection overlay,
1655 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1656 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1657 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1658 (if (< (abs (- click-pt beg))
1659 (abs (- click-pt end)))
1660 (move-overlay mouse-secondary-overlay (car range) end)
1661 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1662 (setq deactivate-mark nil)
1663 (if (eq last-command 'mouse-secondary-save-then-kill)
1664 ;; If the front of the kill ring comes from an immediately
1665 ;; previous use of this command, replace the entry.
1666 (kill-new
1667 (buffer-substring (overlay-start mouse-secondary-overlay)
1668 (overlay-end mouse-secondary-overlay))
1670 (let (deactivate-mark)
1671 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1672 (overlay-end mouse-secondary-overlay))))
1673 (setq mouse-save-then-kill-posn click-pt))
1675 ;; Otherwise, set the secondary selection overlay.
1677 (select-window window)
1678 (if mouse-secondary-start
1679 ;; All we have is one end of a selection, so put the other
1680 ;; end here.
1681 (let ((start (+ 0 mouse-secondary-start)))
1682 (kill-ring-save start click-pt)
1683 (move-overlay mouse-secondary-overlay start click-pt)))
1684 (setq mouse-save-then-kill-posn click-pt))))
1686 ;; Finally, set the window system's secondary selection.
1687 (let (str)
1688 (and (overlay-buffer mouse-secondary-overlay)
1689 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1690 (overlay-end mouse-secondary-overlay)))
1691 (> (length str) 0)
1692 (x-set-selection 'SECONDARY str))))
1695 (defcustom mouse-buffer-menu-maxlen 20
1696 "Number of buffers in one pane (submenu) of the buffer menu.
1697 If we have lots of buffers, divide them into groups of
1698 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1699 :type 'integer
1700 :group 'mouse)
1702 (defcustom mouse-buffer-menu-mode-mult 4
1703 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1704 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1705 will split the buffer menu by the major modes (see
1706 `mouse-buffer-menu-mode-groups') or just by menu length.
1707 Set to 1 (or even 0!) if you want to group by major mode always, and to
1708 a large number if you prefer a mixed multitude. The default is 4."
1709 :type 'integer
1710 :group 'mouse
1711 :version "20.3")
1713 (defvar mouse-buffer-menu-mode-groups
1714 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1715 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1716 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1717 . "Mail/News")
1718 ("\\<C\\>" . "C")
1719 ("ObjC" . "C")
1720 ("Text" . "Text")
1721 ("Outline" . "Text")
1722 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1723 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1724 ("Lisp" . "Lisp")))
1725 "How to group various major modes together in \\[mouse-buffer-menu].
1726 Each element has the form (REGEXP . GROUPNAME).
1727 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1729 (defun mouse-buffer-menu (event)
1730 "Pop up a menu of buffers for selection with the mouse.
1731 This switches buffers in the window that you clicked on,
1732 and selects that window."
1733 (interactive "e")
1734 (mouse-minibuffer-check event)
1735 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1736 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1737 (dolist (buf buffers)
1738 ;; Divide all buffers into buckets for various major modes.
1739 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1740 (with-current-buffer buf
1741 (let* ((adjusted-major-mode major-mode) elt)
1742 (dolist (group mouse-buffer-menu-mode-groups)
1743 (when (string-match (car group) (format-mode-line mode-name))
1744 (setq adjusted-major-mode (cdr group))))
1745 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1746 (unless elt
1747 (setq elt (list adjusted-major-mode
1748 (if (stringp adjusted-major-mode)
1749 adjusted-major-mode
1750 (format-mode-line mode-name nil nil buf)))
1751 split-by-major-mode (cons elt split-by-major-mode)))
1752 (or (memq buf (cdr (cdr elt)))
1753 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1754 ;; Compute the sum of squares of sizes of the major-mode buckets.
1755 (let ((tail split-by-major-mode))
1756 (setq sum-of-squares 0)
1757 (while tail
1758 (setq sum-of-squares
1759 (+ sum-of-squares
1760 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1761 (setq tail (cdr tail))))
1762 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1763 (* (length buffers) (length buffers)))
1764 ;; Subdividing by major modes really helps, so let's do it.
1765 (let (subdivided-menus (buffers-left (length buffers)))
1766 ;; Sort the list to put the most popular major modes first.
1767 (setq split-by-major-mode
1768 (sort split-by-major-mode
1769 (function (lambda (elt1 elt2)
1770 (> (length elt1) (length elt2))))))
1771 ;; Make a separate submenu for each major mode
1772 ;; that has more than one buffer,
1773 ;; unless all the remaining buffers are less than 1/10 of them.
1774 (while (and split-by-major-mode
1775 (and (> (length (car split-by-major-mode)) 3)
1776 (> (* buffers-left 10) (length buffers))))
1777 (let ((this-mode-list (mouse-buffer-menu-alist
1778 (cdr (cdr (car split-by-major-mode))))))
1779 (and this-mode-list
1780 (setq subdivided-menus
1781 (cons (cons
1782 (nth 1 (car split-by-major-mode))
1783 this-mode-list)
1784 subdivided-menus))))
1785 (setq buffers-left
1786 (- buffers-left (length (cdr (car split-by-major-mode)))))
1787 (setq split-by-major-mode (cdr split-by-major-mode)))
1788 ;; If any major modes are left over,
1789 ;; make a single submenu for them.
1790 (if split-by-major-mode
1791 (let ((others-list
1792 (mouse-buffer-menu-alist
1793 ;; we don't need split-by-major-mode any more,
1794 ;; so we can ditch it with nconc.
1795 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1796 (and others-list
1797 (setq subdivided-menus
1798 (cons (cons "Others" others-list)
1799 subdivided-menus)))))
1800 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1801 (progn
1802 (setq alist (mouse-buffer-menu-alist buffers))
1803 (setq menu (cons "Buffer Menu"
1804 (mouse-buffer-menu-split "Select Buffer" alist)))))
1805 (let ((buf (x-popup-menu event menu))
1806 (window (posn-window (event-start event))))
1807 (when buf
1808 (select-window
1809 (if (framep window) (frame-selected-window window)
1810 window))
1811 (switch-to-buffer buf)))))
1813 (defun mouse-buffer-menu-alist (buffers)
1814 (let (tail
1815 (maxlen 0)
1816 head)
1817 (setq buffers
1818 (sort buffers
1819 (function (lambda (elt1 elt2)
1820 (string< (buffer-name elt1) (buffer-name elt2))))))
1821 (setq tail buffers)
1822 (while tail
1823 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1824 (setq maxlen
1825 (max maxlen
1826 (length (buffer-name (car tail))))))
1827 (setq tail (cdr tail)))
1828 (setq tail buffers)
1829 (while tail
1830 (let ((elt (car tail)))
1831 (if (/= (aref (buffer-name elt) 0) ?\s)
1832 (setq head
1833 (cons
1834 (cons
1835 (format
1836 (format "%%-%ds %%s%%s %%s" maxlen)
1837 (buffer-name elt)
1838 (if (buffer-modified-p elt) "*" " ")
1839 (with-current-buffer elt
1840 (if buffer-read-only "%" " "))
1841 (or (buffer-file-name elt)
1842 (with-current-buffer elt
1843 (if list-buffers-directory
1844 (expand-file-name
1845 list-buffers-directory)))
1846 ""))
1847 elt)
1848 head))))
1849 (setq tail (cdr tail)))
1850 ;; Compensate for the reversal that the above loop does.
1851 (nreverse head)))
1853 (defun mouse-buffer-menu-split (title alist)
1854 ;; If we have lots of buffers, divide them into groups of 20
1855 ;; and make a pane (or submenu) for each one.
1856 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1857 (let ((alist alist) sublists next
1858 (i 1))
1859 (while alist
1860 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1861 ;; and make them the next element of sublist.
1862 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1863 (if next
1864 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1865 nil))
1866 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1867 sublists))
1868 (setq i (1+ i))
1869 (setq alist next))
1870 (nreverse sublists))
1871 ;; Few buffers--put them all in one pane.
1872 (list (cons title alist))))
1874 (define-obsolete-function-alias
1875 'mouse-choose-completion 'choose-completion "23.2")
1877 ;; Font selection.
1879 (defun font-menu-add-default ()
1880 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1881 (font-alist x-fixed-font-alist)
1882 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1883 (if (assoc "Default" elt)
1884 (delete (assoc "Default" elt) elt))
1885 (setcdr elt
1886 (cons (list "Default" default)
1887 (cdr elt)))))
1889 (defvar x-fixed-font-alist
1890 (list
1891 (purecopy "Font Menu")
1892 (cons
1893 (purecopy "Misc")
1894 (mapcar
1895 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1896 ;; For these, we specify the pixel height and width.
1897 '(("fixed" "fixed")
1898 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1899 ("6x12"
1900 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1901 ("6x13"
1902 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1903 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1904 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1905 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1906 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1907 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1908 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1909 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1910 ("")
1911 ("clean 5x8"
1912 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1913 ("clean 6x8"
1914 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1915 ("clean 8x8"
1916 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1917 ("clean 8x10"
1918 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1919 ("clean 8x14"
1920 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1921 ("clean 8x16"
1922 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1923 ("")
1924 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1925 ;; We don't seem to have these; who knows what they are.
1926 ;; ("fg-18" "fg-18")
1927 ;; ("fg-25" "fg-25")
1928 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1929 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1930 ("lucidasanstypewriter-bold-24"
1931 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
1932 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1933 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1936 (cons
1937 (purecopy "Courier")
1938 (mapcar
1939 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1940 ;; For these, we specify the point height.
1941 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1942 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1943 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1944 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1945 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1946 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1947 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1948 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1949 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1950 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1951 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1952 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1953 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1954 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1955 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1956 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1957 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1958 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1959 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1960 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1961 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1962 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1963 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1964 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1965 ))))
1966 "X fonts suitable for use in Emacs.")
1968 (declare-function generate-fontset-menu "fontset" ())
1970 (defun mouse-select-font ()
1971 "Prompt for a font name, using `x-popup-menu', and return it."
1972 (interactive)
1973 (unless (display-multi-font-p)
1974 (error "Cannot change fonts on this display"))
1975 (car
1976 (x-popup-menu
1977 (if (listp last-nonmenu-event)
1978 last-nonmenu-event
1979 (list '(0 0) (selected-window)))
1980 (append x-fixed-font-alist
1981 (list (generate-fontset-menu))))))
1983 (declare-function text-scale-mode "face-remap")
1985 (defun mouse-set-font (&rest fonts)
1986 "Set the default font for the selected frame.
1987 The argument FONTS is a list of font names; the first valid font
1988 in this list is used.
1990 When called interactively, pop up a menu and allow the user to
1991 choose a font."
1992 (interactive
1993 (progn (unless (display-multi-font-p)
1994 (error "Cannot change fonts on this display"))
1995 (x-popup-menu
1996 (if (listp last-nonmenu-event)
1997 last-nonmenu-event
1998 (list '(0 0) (selected-window)))
1999 ;; Append list of fontsets currently defined.
2000 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2001 (if fonts
2002 (let (font)
2003 (while fonts
2004 (condition-case nil
2005 (progn
2006 (set-frame-font (car fonts))
2007 (setq font (car fonts))
2008 (setq fonts nil))
2009 (error
2010 (setq fonts (cdr fonts)))))
2011 (if (null font)
2012 (error "Font not found")))))
2014 (defvar mouse-appearance-menu-map nil)
2015 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
2016 (declare-function buffer-face-mode-invoke "face-remap"
2017 (face arg &optional interactive))
2018 (declare-function font-face-attributes "font.c" (font &optional frame))
2020 (defun mouse-appearance-menu (event)
2021 "Show a menu for changing the default face in the current buffer."
2022 (interactive "@e")
2023 (require 'face-remap)
2024 (when (display-multi-font-p)
2025 (with-selected-window (car (event-start event))
2026 (if mouse-appearance-menu-map
2027 nil ; regenerate new fonts
2028 ;; Initialize mouse-appearance-menu-map
2029 (setq mouse-appearance-menu-map
2030 (make-sparse-keymap "Change Default Buffer Face"))
2031 (define-key mouse-appearance-menu-map [face-remap-reset-base]
2032 '(menu-item "Reset to Default" face-remap-reset-base))
2033 (define-key mouse-appearance-menu-map [text-scale-decrease]
2034 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
2035 (define-key mouse-appearance-menu-map [text-scale-increase]
2036 '(menu-item "Increase Buffer Text Size" text-scale-increase))
2037 ;; Font selector
2038 (if (functionp 'x-select-font)
2039 (define-key mouse-appearance-menu-map [x-select-font]
2040 '(menu-item "Change Buffer Font..." x-select-font))
2041 ;; If the select-font is unavailable, construct a menu.
2042 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
2043 (font-alist (cdr (append x-fixed-font-alist
2044 (list (generate-fontset-menu))))))
2045 (dolist (family font-alist)
2046 (let* ((submenu-name (car family))
2047 (submenu-map (make-sparse-keymap submenu-name)))
2048 (dolist (font (cdr family))
2049 (let ((font-name (car font))
2050 font-symbol)
2051 (if (string= font-name "")
2052 (define-key submenu-map [space]
2053 '("--"))
2054 (setq font-symbol (intern (cadr font)))
2055 (define-key submenu-map (vector font-symbol)
2056 (list 'menu-item (car font) font-symbol)))))
2057 (define-key font-submenu (vector (intern submenu-name))
2058 (list 'menu-item submenu-name submenu-map))))
2059 (define-key mouse-appearance-menu-map [font-submenu]
2060 (list 'menu-item "Change Text Font" font-submenu)))))
2061 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
2062 (setq choice (nth (1- (length choice)) choice))
2063 (cond ((eq choice 'text-scale-increase)
2064 (text-scale-increase 1))
2065 ((eq choice 'text-scale-decrease)
2066 (text-scale-increase -1))
2067 ((eq choice 'face-remap-reset-base)
2068 (text-scale-mode 0)
2069 (buffer-face-mode 0))
2070 (choice
2071 ;; Either choice == 'x-select-font, or choice is a
2072 ;; symbol whose name is a font.
2073 (buffer-face-mode-invoke (font-face-attributes
2074 (if (eq choice 'x-select-font)
2075 (x-select-font)
2076 (symbol-name choice)))
2078 (called-interactively-p 'interactive))))))))
2081 ;;; Bindings for mouse commands.
2083 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2084 (global-set-key [mouse-1] 'mouse-set-point)
2085 (global-set-key [drag-mouse-1] 'mouse-set-region)
2087 ;; These are tested for in mouse-drag-region.
2088 (global-set-key [double-mouse-1] 'mouse-set-point)
2089 (global-set-key [triple-mouse-1] 'mouse-set-point)
2091 ;; Clicking on the fringes causes hscrolling:
2092 (global-set-key [left-fringe mouse-1] 'mouse-set-point)
2093 (global-set-key [right-fringe mouse-1] 'mouse-set-point)
2095 (global-set-key [mouse-2] 'mouse-yank-primary)
2096 ;; Allow yanking also when the corresponding cursor is "in the fringe".
2097 (global-set-key [right-fringe mouse-2] 'mouse-yank-at-click)
2098 (global-set-key [left-fringe mouse-2] 'mouse-yank-at-click)
2099 (global-set-key [mouse-3] 'mouse-save-then-kill)
2100 (global-set-key [right-fringe mouse-3] 'mouse-save-then-kill)
2101 (global-set-key [left-fringe mouse-3] 'mouse-save-then-kill)
2103 ;; By binding these to down-going events, we let the user use the up-going
2104 ;; event to make the selection, saving a click.
2105 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2106 (if (not (eq system-type 'ms-dos))
2107 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
2108 ;; C-down-mouse-2 is bound in facemenu.el.
2109 (global-set-key [C-down-mouse-3]
2110 `(menu-item ,(purecopy "Menu Bar") ignore
2111 :filter (lambda (_)
2112 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
2113 (mouse-menu-bar-map)
2114 (mouse-menu-major-mode-map)))))
2116 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2117 ;; vertical-line prevents Emacs from signaling an error when the mouse
2118 ;; button is released after dragging these lines, on non-toolkit
2119 ;; versions.
2120 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2121 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2122 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2123 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2124 (global-set-key [header-line mouse-1] 'mouse-select-window)
2125 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2126 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2127 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2128 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2129 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2130 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2131 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2133 (provide 'mouse)
2135 ;;; mouse.el ends here