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