aclocal.m4: put this file back into repository
[emacs.git] / lisp / mouse.el
blob87b2270d8c394a0c782d05fcdc52e0e50c798b28
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, 2011 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 ((eq system-type 'windows-nt)
1286 ;; MS-Windows emulates PRIMARY in x-get-selection, but not
1287 ;; in x-get-selection-value (the latter only accesses the
1288 ;; clipboard). So try PRIMARY first, in case they selected
1289 ;; something with the mouse in the current Emacs session.
1290 (or (x-get-selection 'PRIMARY)
1291 (x-get-selection-value)))
1292 ((fboundp 'x-get-selection-value) ; MS-DOS and X.
1293 ;; On X, x-get-selection-value supports more formats and
1294 ;; encodings, so use it in preference to x-get-selection.
1295 (or (x-get-selection-value)
1296 (x-get-selection 'PRIMARY)))
1297 ;; FIXME: What about xterm-mouse-mode etc.?
1299 (x-get-selection 'PRIMARY)))))
1300 (if primary
1301 (insert primary)
1302 (error "No selection is available"))))
1304 (defun mouse-kill-ring-save (click)
1305 "Copy the region between point and the mouse click in the kill ring.
1306 This does not delete the region; it acts like \\[kill-ring-save]."
1307 (interactive "e")
1308 (mouse-set-mark-fast click)
1309 (let (this-command last-command)
1310 (kill-ring-save (point) (mark t))))
1312 ;; This function used to delete the text between point and the mouse
1313 ;; whenever it was equal to the front of the kill ring, but some
1314 ;; people found that confusing.
1316 ;; The position of the last invocation of `mouse-save-then-kill'.
1317 (defvar mouse-save-then-kill-posn nil)
1319 (defun mouse-save-then-kill-delete-region (beg end)
1320 ;; We must make our own undo boundaries
1321 ;; because they happen automatically only for the current buffer.
1322 (undo-boundary)
1323 (if (or (= beg end) (eq buffer-undo-list t))
1324 ;; If we have no undo list in this buffer,
1325 ;; just delete.
1326 (delete-region beg end)
1327 ;; Delete, but make the undo-list entry share with the kill ring.
1328 ;; First, delete just one char, so in case buffer is being modified
1329 ;; for the first time, the undo list records that fact.
1330 (let (before-change-functions after-change-functions)
1331 (delete-region beg
1332 (+ beg (if (> end beg) 1 -1))))
1333 (let ((buffer-undo-list buffer-undo-list))
1334 ;; Undo that deletion--but don't change the undo list!
1335 (let (before-change-functions after-change-functions)
1336 (primitive-undo 1 buffer-undo-list))
1337 ;; Now delete the rest of the specified region,
1338 ;; but don't record it.
1339 (setq buffer-undo-list t)
1340 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1341 (error "Lossage in mouse-save-then-kill-delete-region"))
1342 (delete-region beg end))
1343 (let ((tail buffer-undo-list))
1344 ;; Search back in buffer-undo-list for the string
1345 ;; that came from deleting one character.
1346 (while (and tail (not (stringp (car (car tail)))))
1347 (setq tail (cdr tail)))
1348 ;; Replace it with an entry for the entire deleted text.
1349 (and tail
1350 (setcar tail (cons (car kill-ring) (min beg end))))))
1351 (undo-boundary))
1353 (defun mouse-save-then-kill (click)
1354 "Set the region according to CLICK; the second time, kill it.
1355 CLICK should be a mouse click event.
1357 If the region is inactive, activate it temporarily. Set mark at
1358 the original point, and move point to the position of CLICK.
1360 If the region is already active, adjust it. Normally, do this by
1361 moving point or mark, whichever is closer, to CLICK. But if you
1362 have selected whole words or lines, move point or mark to the
1363 word or line boundary closest to CLICK instead.
1365 If `mouse-drag-copy-region' is non-nil, this command also saves the
1366 new region to the kill ring (replacing the previous kill if the
1367 previous region was just saved to the kill ring).
1369 If this command is called a second consecutive time with the same
1370 CLICK position, kill the region (or delete it
1371 if `mouse-drag-copy-region' is non-nil)"
1372 (interactive "e")
1373 (mouse-minibuffer-check click)
1374 (let* ((posn (event-start click))
1375 (click-pt (posn-point posn))
1376 (window (posn-window posn))
1377 (buf (window-buffer window))
1378 ;; Don't let a subsequent kill command append to this one.
1379 (this-command this-command)
1380 ;; Check if the user has multi-clicked to select words/lines.
1381 (click-count
1382 (if (and (eq mouse-selection-click-count-buffer buf)
1383 (with-current-buffer buf (mark t)))
1384 mouse-selection-click-count
1385 0)))
1386 (cond
1387 ((not (numberp click-pt)) nil)
1388 ;; If the user clicked without moving point, kill the region.
1389 ;; This also resets `mouse-selection-click-count'.
1390 ((and (eq last-command 'mouse-save-then-kill)
1391 (eq click-pt mouse-save-then-kill-posn)
1392 (eq window (selected-window)))
1393 (if mouse-drag-copy-region
1394 ;; Region already saved in the previous click;
1395 ;; don't make a duplicate entry, just delete.
1396 (delete-region (mark t) (point))
1397 (kill-region (mark t) (point)))
1398 (setq mouse-selection-click-count 0)
1399 (setq mouse-save-then-kill-posn nil))
1401 ;; Otherwise, if there is a suitable region, adjust it by moving
1402 ;; one end (whichever is closer) to CLICK-PT.
1403 ((or (with-current-buffer buf (region-active-p))
1404 (and (eq window (selected-window))
1405 (mark t)
1406 (or (and (eq last-command 'mouse-save-then-kill)
1407 mouse-save-then-kill-posn)
1408 (and (memq last-command '(mouse-drag-region
1409 mouse-set-region))
1410 (or mark-even-if-inactive
1411 (not transient-mark-mode))))))
1412 (select-window window)
1413 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1414 (if (< (abs (- click-pt (mark t)))
1415 (abs (- click-pt (point))))
1416 (set-mark (car range))
1417 (goto-char (nth 1 range)))
1418 (setq deactivate-mark nil)
1419 (mouse-set-region-1)
1420 (when mouse-drag-copy-region
1421 ;; Region already copied to kill-ring once, so replace.
1422 (kill-new (filter-buffer-substring (mark t) (point)) t))
1423 ;; Arrange for a repeated mouse-3 to kill the region.
1424 (setq mouse-save-then-kill-posn click-pt)))
1426 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1428 (select-window window)
1429 (mouse-set-mark-fast click)
1430 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1431 (if before-scroll (goto-char before-scroll)))
1432 (exchange-point-and-mark)
1433 (mouse-set-region-1)
1434 (when mouse-drag-copy-region
1435 (kill-new (filter-buffer-substring (mark t) (point))))
1436 (setq mouse-save-then-kill-posn click-pt)))))
1439 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1440 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1441 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1442 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1443 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1445 (defconst mouse-secondary-overlay
1446 (let ((ol (make-overlay (point-min) (point-min))))
1447 (delete-overlay ol)
1448 (overlay-put ol 'face 'secondary-selection)
1450 "An overlay which records the current secondary selection.
1451 It is deleted when there is no secondary selection.")
1453 (defvar mouse-secondary-click-count 0)
1455 ;; A marker which records the specified first end for a secondary selection.
1456 ;; May be nil.
1457 (defvar mouse-secondary-start nil)
1459 (defun mouse-start-secondary (click)
1460 "Set one end of the secondary selection to the position clicked on.
1461 Use \\[mouse-secondary-save-then-kill] to set the other end
1462 and complete the secondary selection."
1463 (interactive "e")
1464 (mouse-minibuffer-check click)
1465 (let ((posn (event-start click)))
1466 (with-current-buffer (window-buffer (posn-window posn))
1467 ;; Cancel any preexisting secondary selection.
1468 (delete-overlay mouse-secondary-overlay)
1469 (if (numberp (posn-point posn))
1470 (progn
1471 (or mouse-secondary-start
1472 (setq mouse-secondary-start (make-marker)))
1473 (move-marker mouse-secondary-start (posn-point posn)))))))
1475 (defun mouse-set-secondary (click)
1476 "Set the secondary selection to the text that the mouse is dragged over.
1477 This must be bound to a mouse drag event."
1478 (interactive "e")
1479 (mouse-minibuffer-check click)
1480 (let ((posn (event-start click))
1482 (end (event-end click)))
1483 (with-current-buffer (window-buffer (posn-window posn))
1484 (if (numberp (posn-point posn))
1485 (setq beg (posn-point posn)))
1486 (move-overlay mouse-secondary-overlay beg (posn-point end))
1487 (x-set-selection
1488 'SECONDARY
1489 (buffer-substring (overlay-start mouse-secondary-overlay)
1490 (overlay-end mouse-secondary-overlay))))))
1492 (defun mouse-drag-secondary (start-event)
1493 "Set the secondary selection to the text that the mouse is dragged over.
1494 Highlight the drag area as you move the mouse.
1495 This must be bound to a button-down mouse event.
1496 The function returns a non-nil value if it creates a secondary selection."
1497 (interactive "e")
1498 (mouse-minibuffer-check start-event)
1499 (let* ((echo-keystrokes 0)
1500 (start-posn (event-start start-event))
1501 (start-point (posn-point start-posn))
1502 (start-window (posn-window start-posn))
1503 (bounds (window-edges start-window))
1504 (top (nth 1 bounds))
1505 (bottom (if (window-minibuffer-p start-window)
1506 (nth 3 bounds)
1507 ;; Don't count the mode line.
1508 (1- (nth 3 bounds))))
1509 (click-count (1- (event-click-count start-event))))
1510 (with-current-buffer (window-buffer start-window)
1511 (setq mouse-secondary-click-count click-count)
1512 (if (> (mod click-count 3) 0)
1513 ;; Double or triple press: make an initial selection
1514 ;; of one word or line.
1515 (let ((range (mouse-start-end start-point start-point click-count)))
1516 (set-marker mouse-secondary-start nil)
1517 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1518 (window-buffer start-window)))
1519 ;; Single-press: cancel any preexisting secondary selection.
1520 (or mouse-secondary-start
1521 (setq mouse-secondary-start (make-marker)))
1522 (set-marker mouse-secondary-start start-point)
1523 (delete-overlay mouse-secondary-overlay))
1524 (let (event end end-point)
1525 (track-mouse
1526 (while (progn
1527 (setq event (read-event))
1528 (or (mouse-movement-p event)
1529 (memq (car-safe event) '(switch-frame select-window))))
1531 (if (memq (car-safe event) '(switch-frame select-window))
1533 (setq end (event-end event)
1534 end-point (posn-point end))
1535 (cond
1536 ;; Are we moving within the original window?
1537 ((and (eq (posn-window end) start-window)
1538 (integer-or-marker-p end-point))
1539 (let ((range (mouse-start-end start-point end-point
1540 click-count)))
1541 (if (or (/= start-point end-point)
1542 (null (marker-position mouse-secondary-start)))
1543 (progn
1544 (set-marker mouse-secondary-start nil)
1545 (move-overlay mouse-secondary-overlay
1546 (car range) (nth 1 range))))))
1548 (let ((mouse-row (cdr (cdr (mouse-position)))))
1549 (cond
1550 ((null mouse-row))
1551 ((< mouse-row top)
1552 (mouse-scroll-subr start-window (- mouse-row top)
1553 mouse-secondary-overlay start-point))
1554 ((>= mouse-row bottom)
1555 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1556 mouse-secondary-overlay start-point)))))))))
1558 (if (consp event)
1559 (if (marker-position mouse-secondary-start)
1560 (save-window-excursion
1561 (delete-overlay mouse-secondary-overlay)
1562 (x-set-selection 'SECONDARY nil)
1563 (select-window start-window)
1564 (save-excursion
1565 (goto-char mouse-secondary-start)
1566 (sit-for 1)
1567 nil))
1568 (x-set-selection
1569 'SECONDARY
1570 (buffer-substring (overlay-start mouse-secondary-overlay)
1571 (overlay-end mouse-secondary-overlay)))))))))
1573 (defun mouse-yank-secondary (click)
1574 "Insert the secondary selection at the position clicked on.
1575 Move point to the end of the inserted text.
1576 If `mouse-yank-at-point' is non-nil, insert at point
1577 regardless of where you click."
1578 (interactive "e")
1579 ;; Give temporary modes such as isearch a chance to turn off.
1580 (run-hooks 'mouse-leave-buffer-hook)
1581 (or mouse-yank-at-point (mouse-set-point click))
1582 (let ((secondary (x-get-selection 'SECONDARY)))
1583 (if secondary
1584 (insert secondary)
1585 (error "No secondary selection"))))
1587 (defun mouse-kill-secondary ()
1588 "Kill the text in the secondary selection.
1589 This is intended more as a keyboard command than as a mouse command
1590 but it can work as either one.
1592 The current buffer (in case of keyboard use), or the buffer clicked on,
1593 must be the one that the secondary selection is in. This requirement
1594 is to prevent accidents."
1595 (interactive)
1596 (let* ((keys (this-command-keys))
1597 (click (elt keys (1- (length keys)))))
1598 (or (eq (overlay-buffer mouse-secondary-overlay)
1599 (if (listp click)
1600 (window-buffer (posn-window (event-start click)))
1601 (current-buffer)))
1602 (error "Select or click on the buffer where the secondary selection is")))
1603 (let (this-command)
1604 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1605 (kill-region (overlay-start mouse-secondary-overlay)
1606 (overlay-end mouse-secondary-overlay))))
1607 (delete-overlay mouse-secondary-overlay))
1609 (defun mouse-secondary-save-then-kill (click)
1610 "Set the secondary selection and save it to the kill ring.
1611 The second time, kill it. CLICK should be a mouse click event.
1613 If you have not called `mouse-start-secondary' in the clicked
1614 buffer, activate the secondary selection and set it between point
1615 and the click position CLICK.
1617 Otherwise, adjust the bounds of the secondary selection.
1618 Normally, do this by moving its beginning or end, whichever is
1619 closer, to CLICK. But if you have selected whole words or lines,
1620 adjust to the word or line boundary closest to CLICK instead.
1622 If this command is called a second consecutive time with the same
1623 CLICK position, kill the secondary selection."
1624 (interactive "e")
1625 (mouse-minibuffer-check click)
1626 (let* ((posn (event-start click))
1627 (click-pt (posn-point posn))
1628 (window (posn-window posn))
1629 (buf (window-buffer window))
1630 ;; Don't let a subsequent kill command append to this one.
1631 (this-command this-command)
1632 ;; Check if the user has multi-clicked to select words/lines.
1633 (click-count
1634 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1635 mouse-secondary-click-count
1637 (beg (overlay-start mouse-secondary-overlay))
1638 (end (overlay-end mouse-secondary-overlay)))
1640 (cond
1641 ((not (numberp click-pt)) nil)
1643 ;; If the secondary selection is not active in BUF, activate it.
1644 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1645 (if mouse-secondary-start
1646 (marker-buffer mouse-secondary-start)))))
1647 (select-window window)
1648 (setq mouse-secondary-start (make-marker))
1649 (move-marker mouse-secondary-start (point))
1650 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1651 (kill-ring-save (point) click-pt))
1653 ;; If the user clicked without moving point, delete the secondary
1654 ;; selection. This also resets `mouse-secondary-click-count'.
1655 ((and (eq last-command 'mouse-secondary-save-then-kill)
1656 (eq click-pt mouse-save-then-kill-posn)
1657 (eq window (selected-window)))
1658 (mouse-save-then-kill-delete-region beg end)
1659 (delete-overlay mouse-secondary-overlay)
1660 (setq mouse-secondary-click-count 0)
1661 (setq mouse-save-then-kill-posn nil))
1663 ;; Otherwise, if there is a suitable secondary selection overlay,
1664 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1665 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1666 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1667 (if (< (abs (- click-pt beg))
1668 (abs (- click-pt end)))
1669 (move-overlay mouse-secondary-overlay (car range) end)
1670 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1671 (setq deactivate-mark nil)
1672 (if (eq last-command 'mouse-secondary-save-then-kill)
1673 ;; If the front of the kill ring comes from an immediately
1674 ;; previous use of this command, replace the entry.
1675 (kill-new
1676 (buffer-substring (overlay-start mouse-secondary-overlay)
1677 (overlay-end mouse-secondary-overlay))
1679 (let (deactivate-mark)
1680 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1681 (overlay-end mouse-secondary-overlay))))
1682 (setq mouse-save-then-kill-posn click-pt))
1684 ;; Otherwise, set the secondary selection overlay.
1686 (select-window window)
1687 (if mouse-secondary-start
1688 ;; All we have is one end of a selection, so put the other
1689 ;; end here.
1690 (let ((start (+ 0 mouse-secondary-start)))
1691 (kill-ring-save start click-pt)
1692 (move-overlay mouse-secondary-overlay start click-pt)))
1693 (setq mouse-save-then-kill-posn click-pt))))
1695 ;; Finally, set the window system's secondary selection.
1696 (let (str)
1697 (and (overlay-buffer mouse-secondary-overlay)
1698 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1699 (overlay-end mouse-secondary-overlay)))
1700 (> (length str) 0)
1701 (x-set-selection 'SECONDARY str))))
1704 (defcustom mouse-buffer-menu-maxlen 20
1705 "Number of buffers in one pane (submenu) of the buffer menu.
1706 If we have lots of buffers, divide them into groups of
1707 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1708 :type 'integer
1709 :group 'mouse)
1711 (defcustom mouse-buffer-menu-mode-mult 4
1712 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1713 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1714 will split the buffer menu by the major modes (see
1715 `mouse-buffer-menu-mode-groups') or just by menu length.
1716 Set to 1 (or even 0!) if you want to group by major mode always, and to
1717 a large number if you prefer a mixed multitude. The default is 4."
1718 :type 'integer
1719 :group 'mouse
1720 :version "20.3")
1722 (defvar mouse-buffer-menu-mode-groups
1723 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1724 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1725 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1726 . "Mail/News")
1727 ("\\<C\\>" . "C")
1728 ("ObjC" . "C")
1729 ("Text" . "Text")
1730 ("Outline" . "Text")
1731 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1732 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1733 ("Lisp" . "Lisp")))
1734 "How to group various major modes together in \\[mouse-buffer-menu].
1735 Each element has the form (REGEXP . GROUPNAME).
1736 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1738 (defun mouse-buffer-menu (event)
1739 "Pop up a menu of buffers for selection with the mouse.
1740 This switches buffers in the window that you clicked on,
1741 and selects that window."
1742 (interactive "e")
1743 (mouse-minibuffer-check event)
1744 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1745 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1746 (dolist (buf buffers)
1747 ;; Divide all buffers into buckets for various major modes.
1748 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1749 (with-current-buffer buf
1750 (let* ((adjusted-major-mode major-mode) elt)
1751 (dolist (group mouse-buffer-menu-mode-groups)
1752 (when (string-match (car group) (format-mode-line mode-name))
1753 (setq adjusted-major-mode (cdr group))))
1754 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1755 (unless elt
1756 (setq elt (list adjusted-major-mode
1757 (if (stringp adjusted-major-mode)
1758 adjusted-major-mode
1759 (format-mode-line mode-name nil nil buf)))
1760 split-by-major-mode (cons elt split-by-major-mode)))
1761 (or (memq buf (cdr (cdr elt)))
1762 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1763 ;; Compute the sum of squares of sizes of the major-mode buckets.
1764 (let ((tail split-by-major-mode))
1765 (setq sum-of-squares 0)
1766 (while tail
1767 (setq sum-of-squares
1768 (+ sum-of-squares
1769 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1770 (setq tail (cdr tail))))
1771 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1772 (* (length buffers) (length buffers)))
1773 ;; Subdividing by major modes really helps, so let's do it.
1774 (let (subdivided-menus (buffers-left (length buffers)))
1775 ;; Sort the list to put the most popular major modes first.
1776 (setq split-by-major-mode
1777 (sort split-by-major-mode
1778 (function (lambda (elt1 elt2)
1779 (> (length elt1) (length elt2))))))
1780 ;; Make a separate submenu for each major mode
1781 ;; that has more than one buffer,
1782 ;; unless all the remaining buffers are less than 1/10 of them.
1783 (while (and split-by-major-mode
1784 (and (> (length (car split-by-major-mode)) 3)
1785 (> (* buffers-left 10) (length buffers))))
1786 (let ((this-mode-list (mouse-buffer-menu-alist
1787 (cdr (cdr (car split-by-major-mode))))))
1788 (and this-mode-list
1789 (setq subdivided-menus
1790 (cons (cons
1791 (nth 1 (car split-by-major-mode))
1792 this-mode-list)
1793 subdivided-menus))))
1794 (setq buffers-left
1795 (- buffers-left (length (cdr (car split-by-major-mode)))))
1796 (setq split-by-major-mode (cdr split-by-major-mode)))
1797 ;; If any major modes are left over,
1798 ;; make a single submenu for them.
1799 (if split-by-major-mode
1800 (let ((others-list
1801 (mouse-buffer-menu-alist
1802 ;; we don't need split-by-major-mode any more,
1803 ;; so we can ditch it with nconc.
1804 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1805 (and others-list
1806 (setq subdivided-menus
1807 (cons (cons "Others" others-list)
1808 subdivided-menus)))))
1809 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1810 (progn
1811 (setq alist (mouse-buffer-menu-alist buffers))
1812 (setq menu (cons "Buffer Menu"
1813 (mouse-buffer-menu-split "Select Buffer" alist)))))
1814 (let ((buf (x-popup-menu event menu))
1815 (window (posn-window (event-start event))))
1816 (when buf
1817 (select-window
1818 (if (framep window) (frame-selected-window window)
1819 window))
1820 (switch-to-buffer buf)))))
1822 (defun mouse-buffer-menu-alist (buffers)
1823 (let (tail
1824 (maxlen 0)
1825 head)
1826 (setq buffers
1827 (sort buffers
1828 (function (lambda (elt1 elt2)
1829 (string< (buffer-name elt1) (buffer-name elt2))))))
1830 (setq tail buffers)
1831 (while tail
1832 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1833 (setq maxlen
1834 (max maxlen
1835 (length (buffer-name (car tail))))))
1836 (setq tail (cdr tail)))
1837 (setq tail buffers)
1838 (while tail
1839 (let ((elt (car tail)))
1840 (if (/= (aref (buffer-name elt) 0) ?\s)
1841 (setq head
1842 (cons
1843 (cons
1844 (format
1845 (format "%%-%ds %%s%%s %%s" maxlen)
1846 (buffer-name elt)
1847 (if (buffer-modified-p elt) "*" " ")
1848 (with-current-buffer elt
1849 (if buffer-read-only "%" " "))
1850 (or (buffer-file-name elt)
1851 (with-current-buffer elt
1852 (if list-buffers-directory
1853 (expand-file-name
1854 list-buffers-directory)))
1855 ""))
1856 elt)
1857 head))))
1858 (setq tail (cdr tail)))
1859 ;; Compensate for the reversal that the above loop does.
1860 (nreverse head)))
1862 (defun mouse-buffer-menu-split (title alist)
1863 ;; If we have lots of buffers, divide them into groups of 20
1864 ;; and make a pane (or submenu) for each one.
1865 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1866 (let ((alist alist) sublists next
1867 (i 1))
1868 (while alist
1869 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1870 ;; and make them the next element of sublist.
1871 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1872 (if next
1873 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1874 nil))
1875 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1876 sublists))
1877 (setq i (1+ i))
1878 (setq alist next))
1879 (nreverse sublists))
1880 ;; Few buffers--put them all in one pane.
1881 (list (cons title alist))))
1883 (define-obsolete-function-alias
1884 'mouse-choose-completion 'choose-completion "23.2")
1886 ;; Font selection.
1888 (defun font-menu-add-default ()
1889 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1890 (font-alist x-fixed-font-alist)
1891 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1892 (if (assoc "Default" elt)
1893 (delete (assoc "Default" elt) elt))
1894 (setcdr elt
1895 (cons (list "Default" default)
1896 (cdr elt)))))
1898 (defvar x-fixed-font-alist
1899 (list
1900 (purecopy "Font Menu")
1901 (cons
1902 (purecopy "Misc")
1903 (mapcar
1904 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1905 ;; For these, we specify the pixel height and width.
1906 '(("fixed" "fixed")
1907 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1908 ("6x12"
1909 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1910 ("6x13"
1911 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1912 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1913 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1914 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1915 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1916 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1917 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1918 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1919 ("")
1920 ("clean 5x8"
1921 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1922 ("clean 6x8"
1923 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1924 ("clean 8x8"
1925 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1926 ("clean 8x10"
1927 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1928 ("clean 8x14"
1929 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1930 ("clean 8x16"
1931 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1932 ("")
1933 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1934 ;; We don't seem to have these; who knows what they are.
1935 ;; ("fg-18" "fg-18")
1936 ;; ("fg-25" "fg-25")
1937 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1938 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1939 ("lucidasanstypewriter-bold-24"
1940 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
1941 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1942 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1945 (cons
1946 (purecopy "Courier")
1947 (mapcar
1948 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1949 ;; For these, we specify the point height.
1950 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1951 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1952 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1953 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1954 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1955 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1956 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1957 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1958 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1959 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1960 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1961 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1962 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1963 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1964 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1965 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1966 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1967 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1968 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1969 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1970 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1971 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1972 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1973 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1974 ))))
1975 "X fonts suitable for use in Emacs.")
1977 (declare-function generate-fontset-menu "fontset" ())
1979 (defun mouse-select-font ()
1980 "Prompt for a font name, using `x-popup-menu', and return it."
1981 (interactive)
1982 (unless (display-multi-font-p)
1983 (error "Cannot change fonts on this display"))
1984 (car
1985 (x-popup-menu
1986 (if (listp last-nonmenu-event)
1987 last-nonmenu-event
1988 (list '(0 0) (selected-window)))
1989 (append x-fixed-font-alist
1990 (list (generate-fontset-menu))))))
1992 (declare-function text-scale-mode "face-remap")
1994 (defun mouse-set-font (&rest fonts)
1995 "Set the default font for the selected frame.
1996 The argument FONTS is a list of font names; the first valid font
1997 in this list is used.
1999 When called interactively, pop up a menu and allow the user to
2000 choose a font."
2001 (interactive
2002 (progn (unless (display-multi-font-p)
2003 (error "Cannot change fonts on this display"))
2004 (x-popup-menu
2005 (if (listp last-nonmenu-event)
2006 last-nonmenu-event
2007 (list '(0 0) (selected-window)))
2008 ;; Append list of fontsets currently defined.
2009 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2010 (if fonts
2011 (let (font)
2012 (while fonts
2013 (condition-case nil
2014 (progn
2015 (set-frame-font (car fonts))
2016 (setq font (car fonts))
2017 (setq fonts nil))
2018 (error
2019 (setq fonts (cdr fonts)))))
2020 (if (null font)
2021 (error "Font not found")))))
2023 (defvar mouse-appearance-menu-map nil)
2024 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
2025 (declare-function buffer-face-mode-invoke "face-remap"
2026 (face arg &optional interactive))
2027 (declare-function font-face-attributes "font.c" (font &optional frame))
2029 (defun mouse-appearance-menu (event)
2030 "Show a menu for changing the default face in the current buffer."
2031 (interactive "@e")
2032 (require 'face-remap)
2033 (when (display-multi-font-p)
2034 (with-selected-window (car (event-start event))
2035 (if mouse-appearance-menu-map
2036 nil ; regenerate new fonts
2037 ;; Initialize mouse-appearance-menu-map
2038 (setq mouse-appearance-menu-map
2039 (make-sparse-keymap "Change Default Buffer Face"))
2040 (define-key mouse-appearance-menu-map [face-remap-reset-base]
2041 '(menu-item "Reset to Default" face-remap-reset-base))
2042 (define-key mouse-appearance-menu-map [text-scale-decrease]
2043 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
2044 (define-key mouse-appearance-menu-map [text-scale-increase]
2045 '(menu-item "Increase Buffer Text Size" text-scale-increase))
2046 ;; Font selector
2047 (if (functionp 'x-select-font)
2048 (define-key mouse-appearance-menu-map [x-select-font]
2049 '(menu-item "Change Buffer Font..." x-select-font))
2050 ;; If the select-font is unavailable, construct a menu.
2051 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
2052 (font-alist (cdr (append x-fixed-font-alist
2053 (list (generate-fontset-menu))))))
2054 (dolist (family font-alist)
2055 (let* ((submenu-name (car family))
2056 (submenu-map (make-sparse-keymap submenu-name)))
2057 (dolist (font (cdr family))
2058 (let ((font-name (car font))
2059 font-symbol)
2060 (if (string= font-name "")
2061 (define-key submenu-map [space]
2062 '("--"))
2063 (setq font-symbol (intern (cadr font)))
2064 (define-key submenu-map (vector font-symbol)
2065 (list 'menu-item (car font) font-symbol)))))
2066 (define-key font-submenu (vector (intern submenu-name))
2067 (list 'menu-item submenu-name submenu-map))))
2068 (define-key mouse-appearance-menu-map [font-submenu]
2069 (list 'menu-item "Change Text Font" font-submenu)))))
2070 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
2071 (setq choice (nth (1- (length choice)) choice))
2072 (cond ((eq choice 'text-scale-increase)
2073 (text-scale-increase 1))
2074 ((eq choice 'text-scale-decrease)
2075 (text-scale-increase -1))
2076 ((eq choice 'face-remap-reset-base)
2077 (text-scale-mode 0)
2078 (buffer-face-mode 0))
2079 (choice
2080 ;; Either choice == 'x-select-font, or choice is a
2081 ;; symbol whose name is a font.
2082 (buffer-face-mode-invoke (font-face-attributes
2083 (if (eq choice 'x-select-font)
2084 (x-select-font)
2085 (symbol-name choice)))
2087 (called-interactively-p 'interactive))))))))
2090 ;;; Bindings for mouse commands.
2092 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2093 (global-set-key [mouse-1] 'mouse-set-point)
2094 (global-set-key [drag-mouse-1] 'mouse-set-region)
2096 ;; These are tested for in mouse-drag-region.
2097 (global-set-key [double-mouse-1] 'mouse-set-point)
2098 (global-set-key [triple-mouse-1] 'mouse-set-point)
2100 ;; Clicking on the fringes causes hscrolling:
2101 (global-set-key [left-fringe mouse-1] 'mouse-set-point)
2102 (global-set-key [right-fringe mouse-1] 'mouse-set-point)
2104 (global-set-key [mouse-2] 'mouse-yank-primary)
2105 ;; Allow yanking also when the corresponding cursor is "in the fringe".
2106 (global-set-key [right-fringe mouse-2] 'mouse-yank-at-click)
2107 (global-set-key [left-fringe mouse-2] 'mouse-yank-at-click)
2108 (global-set-key [mouse-3] 'mouse-save-then-kill)
2109 (global-set-key [right-fringe mouse-3] 'mouse-save-then-kill)
2110 (global-set-key [left-fringe mouse-3] 'mouse-save-then-kill)
2112 ;; By binding these to down-going events, we let the user use the up-going
2113 ;; event to make the selection, saving a click.
2114 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2115 (if (not (eq system-type 'ms-dos))
2116 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
2117 ;; C-down-mouse-2 is bound in facemenu.el.
2118 (global-set-key [C-down-mouse-3]
2119 `(menu-item ,(purecopy "Menu Bar") ignore
2120 :filter (lambda (_)
2121 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
2122 (mouse-menu-bar-map)
2123 (mouse-menu-major-mode-map)))))
2125 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2126 ;; vertical-line prevents Emacs from signaling an error when the mouse
2127 ;; button is released after dragging these lines, on non-toolkit
2128 ;; versions.
2129 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2130 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2131 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2132 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2133 (global-set-key [header-line mouse-1] 'mouse-select-window)
2134 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2135 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2136 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2137 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2138 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2139 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2140 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2142 (provide 'mouse)
2144 ;;; mouse.el ends here