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