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