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