Rename option to shell-command-dont-erase-buffer
[emacs.git] / lisp / mouse.el
blob2a9a62feae8a46d3f15b94d03ac12500a3869def
1 ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1995, 1999-2016 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
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 ;;; Code:
32 ;;; Utility functions.
34 ;; Indent track-mouse like progn.
35 (put 'track-mouse 'lisp-indent-function 0)
37 (defgroup mouse nil
38 "Input from the mouse." ;; "Mouse support."
39 :group 'environment
40 :group 'editing)
42 (defcustom mouse-yank-at-point nil
43 "If non-nil, mouse yank commands yank at point instead of at click."
44 :type 'boolean
45 :group 'mouse)
47 (defcustom mouse-drag-copy-region nil
48 "If non-nil, copy to kill-ring upon mouse adjustments of the region.
50 This affects `mouse-save-then-kill' (\\[mouse-save-then-kill]) in
51 addition to mouse drags."
52 :type 'boolean
53 :version "24.1"
54 :group 'mouse)
56 (defcustom mouse-1-click-follows-link 450
57 "Non-nil means that clicking Mouse-1 on a link follows the link.
59 With the default setting, an ordinary Mouse-1 click on a link
60 performs the same action as Mouse-2 on that link, while a longer
61 Mouse-1 click \(hold down the Mouse-1 button for more than 450
62 milliseconds) performs the original Mouse-1 binding \(which
63 typically sets point where you click the mouse).
65 If value is an integer, the time elapsed between pressing and
66 releasing the mouse button determines whether to follow the link
67 or perform the normal Mouse-1 action (typically set point).
68 The absolute numeric value specifies the maximum duration of a
69 \"short click\" in milliseconds. A positive value means that a
70 short click follows the link, and a longer click performs the
71 normal action. A negative value gives the opposite behavior.
73 If value is `double', a double click follows the link.
75 Otherwise, a single Mouse-1 click unconditionally follows the link.
77 Note that dragging the mouse never follows the link.
79 This feature only works in modes that specifically identify
80 clickable text as links, so it may not work with some external
81 packages. See `mouse-on-link-p' for details."
82 :version "22.1"
83 :type '(choice (const :tag "Disabled" nil)
84 (const :tag "Double click" double)
85 (number :tag "Single click time limit" :value 450)
86 (other :tag "Single click" t))
87 :group 'mouse)
89 (defcustom mouse-1-click-in-non-selected-windows t
90 "If non-nil, a Mouse-1 click also follows links in non-selected windows.
92 If nil, a Mouse-1 click on a link in a non-selected window performs
93 the normal mouse-1 binding, typically selects the window and sets
94 point at the click position."
95 :type 'boolean
96 :version "22.1"
97 :group 'mouse)
99 (defun mouse--down-1-maybe-follows-link (&optional _prompt)
100 "Turn `mouse-1' events into `mouse-2' events if follows-link.
101 Expects to be bound to `down-mouse-1' in `key-translation-map'."
102 (when (and mouse-1-click-follows-link
103 (eq (if (eq mouse-1-click-follows-link 'double)
104 'double-down-mouse-1 'down-mouse-1)
105 (car-safe last-input-event)))
106 (let ((action (mouse-on-link-p (event-start last-input-event))))
107 (when (and action
108 (or mouse-1-click-in-non-selected-windows
109 (eq (selected-window)
110 (posn-window (event-start last-input-event)))))
111 (let ((timedout
112 (sit-for (if (numberp mouse-1-click-follows-link)
113 (/ (abs mouse-1-click-follows-link) 1000.0)
114 0))))
115 (if (if (and (numberp mouse-1-click-follows-link)
116 (>= mouse-1-click-follows-link 0))
117 timedout (not timedout))
119 ;; Use read-key so it works for xterm-mouse-mode!
120 (let ((event (read-key)))
121 (if (eq (car-safe event)
122 (if (eq mouse-1-click-follows-link 'double)
123 'double-mouse-1 'mouse-1))
124 (progn
125 ;; Turn the mouse-1 into a mouse-2 to follow links,
126 ;; but only if ‘mouse-on-link-p’ hasn’t returned a
127 ;; string or vector (see its docstring).
128 (if (or (stringp action) (vectorp action))
129 (push (aref action 0) unread-command-events)
130 (let ((newup (if (eq mouse-1-click-follows-link 'double)
131 'double-mouse-2 'mouse-2)))
132 ;; If mouse-2 has never been done by the user, it
133 ;; doesn't have the necessary property to be
134 ;; interpreted correctly.
135 (unless (get newup 'event-kind)
136 (put newup 'event-kind (get (car event) 'event-kind)))
137 (push (cons newup (cdr event)) unread-command-events)))
138 ;; Don't change the down event, only the up-event
139 ;; (bug#18212).
140 nil)
141 (push event unread-command-events)
142 nil))))))))
144 (define-key key-translation-map [down-mouse-1]
145 #'mouse--down-1-maybe-follows-link)
146 (define-key key-translation-map [double-down-mouse-1]
147 #'mouse--down-1-maybe-follows-link)
150 ;; Provide a mode-specific menu on a mouse button.
152 (defun minor-mode-menu-from-indicator (indicator)
153 "Show menu for minor mode specified by INDICATOR.
154 Interactively, INDICATOR is read using completion.
155 If there is no menu defined for the minor mode, then create one with
156 items `Turn Off' and `Help'."
157 (interactive
158 (list (completing-read
159 "Minor mode indicator: "
160 (describe-minor-mode-completion-table-for-indicator))))
161 (let* ((minor-mode (lookup-minor-mode-from-indicator indicator))
162 (mm-fun (or (get minor-mode :minor-mode-function) minor-mode)))
163 (unless minor-mode (error "Cannot find minor mode for `%s'" indicator))
164 (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist)))
165 (menu (and (keymapp map) (lookup-key map [menu-bar]))))
166 (setq menu
167 (if menu
168 (mouse-menu-non-singleton menu)
169 (if (fboundp mm-fun) ; bug#20201
170 `(keymap
171 ,indicator
172 (turn-off menu-item "Turn off minor mode" ,mm-fun)
173 (help menu-item "Help for minor mode"
174 (lambda () (interactive)
175 (describe-function ',mm-fun)))))))
176 (if menu
177 (popup-menu menu)
178 (message "No menu available")))))
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 (declare (obsolete mouse-menu-major-mode-map "23.1"))
270 (interactive "@e\nP")
271 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
272 (popup-menu (mouse-menu-major-mode-map) event prefix))
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 (declare (obsolete mouse-menu-bar-map "23.1"))
279 (interactive "@e \nP")
280 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
281 (popup-menu (mouse-menu-bar-map) (unless (integerp event) event) prefix))
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 (declare (obsolete nil "23.1"))
287 (interactive "@e\nP")
288 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
289 (popup-menu
290 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
291 (mouse-menu-bar-map)
292 (mouse-menu-major-mode-map))
293 event prefix))
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 (define-obsolete-function-alias 'mouse-tear-off-window 'tear-off-window "24.4")
327 (defun tear-off-window (click)
328 "Delete the selected window, and create a new frame displaying its buffer."
329 (interactive "e")
330 (mouse-minibuffer-check click)
331 (let* ((window (posn-window (event-start click)))
332 (buf (window-buffer window))
333 (frame (make-frame))) ;FIXME: Use pop-to-buffer.
334 (select-frame frame)
335 (switch-to-buffer buf)
336 (delete-window window)))
338 (defun mouse-delete-other-windows ()
339 "Delete all windows except the one you click on."
340 (interactive "@")
341 (delete-other-windows))
343 (defun mouse-split-window-vertically (click)
344 "Select Emacs window mouse is on, then split it vertically in half.
345 The window is split at the line clicked on.
346 This command must be bound to a mouse click."
347 (interactive "@e")
348 (mouse-minibuffer-check click)
349 (let ((start (event-start click)))
350 (select-window (posn-window start))
351 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
352 (first-line window-min-height)
353 (last-line (- (window-height) window-min-height)))
354 (if (< last-line first-line)
355 (user-error "Window too short to split")
356 ;; Bind `window-combination-resize' to nil so we are sure to get
357 ;; the split right at the line clicked on.
358 (let (window-combination-resize)
359 (split-window-vertically
360 (min (max new-height first-line) last-line)))))))
362 (defun mouse-split-window-horizontally (click)
363 "Select Emacs window mouse is on, then split it horizontally in half.
364 The window is split at the column clicked on.
365 This command must be bound to a mouse click."
366 (interactive "@e")
367 (mouse-minibuffer-check click)
368 (let ((start (event-start click)))
369 (select-window (posn-window start))
370 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
371 (first-col window-min-width)
372 (last-col (- (window-width) window-min-width)))
373 (if (< last-col first-col)
374 (user-error "Window too narrow to split")
375 ;; Bind `window-combination-resize' to nil so we are sure to get
376 ;; the split right at the column clicked on.
377 (let (window-combination-resize)
378 (split-window-horizontally
379 (min (max new-width first-col) last-col)))))))
381 (defun mouse-drag-line (start-event line)
382 "Drag a mode line, header line, or vertical line with the mouse.
383 START-EVENT is the starting mouse-event of the drag action. LINE
384 must be one of the symbols `header', `mode', or `vertical'."
385 ;; Give temporary modes such as isearch a chance to turn off.
386 (run-hooks 'mouse-leave-buffer-hook)
387 (let* ((echo-keystrokes 0)
388 (start (event-start start-event))
389 (window (posn-window start))
390 (frame (window-frame window))
391 ;; `position' records the x- or y-coordinate of the last
392 ;; sampled position.
393 (position (if (eq line 'vertical)
394 (+ (window-pixel-left window)
395 (car (posn-x-y start)))
396 (+ (window-pixel-top window)
397 (cdr (posn-x-y start)))))
398 ;; `last-position' records the x- or y-coordinate of the
399 ;; previously sampled position. The difference of `position'
400 ;; and `last-position' determines the size change of WINDOW.
401 (last-position position)
402 (draggable t)
403 posn-window growth dragged)
404 ;; Decide on whether we are allowed to track at all and whose
405 ;; window's edge we drag.
406 (cond
407 ((eq line 'header)
408 (if (window-at-side-p window 'top)
409 ;; We can't drag the header line of a topmost window.
410 (setq draggable nil)
411 ;; Drag bottom edge of window above the header line.
412 (setq window (window-in-direction 'above window t))))
413 ((eq line 'mode)
414 (if (and (window-at-side-p window 'bottom)
415 ;; Allow resizing the minibuffer window if it's on the
416 ;; same frame as and immediately below `window', and it's
417 ;; either active or `resize-mini-windows' is nil.
418 (let ((minibuffer-window (minibuffer-window frame)))
419 (not (and (eq (window-frame minibuffer-window) frame)
420 (or (not resize-mini-windows)
421 (eq minibuffer-window
422 (active-minibuffer-window)))))))
423 (setq draggable nil)))
424 ((eq line 'vertical)
425 (let ((divider-width (frame-right-divider-width frame)))
426 (when (and (or (not (numberp divider-width))
427 (zerop divider-width))
428 (eq (frame-parameter frame 'vertical-scroll-bars) 'left))
429 (setq window (window-in-direction 'left window t))))))
431 (let* ((exitfun nil)
432 (move
433 (lambda (event) (interactive "e")
434 (cond
435 ((not (consp event))
436 nil)
437 ((eq line 'vertical)
438 ;; Drag right edge of `window'.
439 (setq start (event-start event))
440 (setq position (car (posn-x-y start)))
441 ;; Set `posn-window' to the window where `event' was recorded.
442 ;; This can be `window' or the window on the left or right of
443 ;; `window'.
444 (when (window-live-p (setq posn-window (posn-window start)))
445 ;; Add left edge of `posn-window' to `position'.
446 (setq position (+ (window-pixel-left posn-window) position))
447 (unless (nth 1 start)
448 ;; Add width of objects on the left of the text area to
449 ;; `position'.
450 (when (eq (window-current-scroll-bars posn-window) 'left)
451 (setq position (+ (window-scroll-bar-width posn-window)
452 position)))
453 (setq position (+ (car (window-fringes posn-window))
454 (or (car (window-margins posn-window)) 0)
455 position))))
456 ;; When the cursor overshoots after shrinking a window to its
457 ;; minimum size and the dragging direction changes, have the
458 ;; cursor first catch up with the window edge.
459 (unless (or (zerop (setq growth (- position last-position)))
460 (and (> growth 0)
461 (< position (+ (window-pixel-left window)
462 (window-pixel-width window))))
463 (and (< growth 0)
464 (> position (+ (window-pixel-left window)
465 (window-pixel-width window)))))
466 (setq dragged t)
467 (adjust-window-trailing-edge window growth t t))
468 (setq last-position position))
469 (draggable
470 ;; Drag bottom edge of `window'.
471 (setq start (event-start event))
472 ;; Set `posn-window' to the window where `event' was recorded.
473 ;; This can be either `window' or the window above or below of
474 ;; `window'.
475 (setq posn-window (posn-window start))
476 (setq position (cdr (posn-x-y start)))
477 (when (window-live-p posn-window)
478 ;; Add top edge of `posn-window' to `position'.
479 (setq position (+ (window-pixel-top posn-window) position))
480 ;; If necessary, add height of header line to `position'
481 (when (memq (posn-area start)
482 '(nil left-fringe right-fringe left-margin right-margin))
483 (setq position (+ (window-header-line-height posn-window) position))))
484 ;; When the cursor overshoots after shrinking a window to its
485 ;; minimum size and the dragging direction changes, have the
486 ;; cursor first catch up with the window edge.
487 (unless (or (zerop (setq growth (- position last-position)))
488 (and (> growth 0)
489 (< position (+ (window-pixel-top window)
490 (window-pixel-height window))))
491 (and (< growth 0)
492 (> position (+ (window-pixel-top window)
493 (window-pixel-height window)))))
494 (setq dragged t)
495 (adjust-window-trailing-edge window growth nil t))
496 (setq last-position position))))))
497 ;; Start tracking. The special value 'dragging' signals the
498 ;; display engine to freeze the mouse pointer shape for as long
499 ;; as we drag.
500 (setq track-mouse 'dragging)
501 ;; Loop reading events and sampling the position of the mouse.
502 (setq exitfun
503 (set-transient-map
504 (let ((map (make-sparse-keymap)))
505 (define-key map [switch-frame] #'ignore)
506 (define-key map [select-window] #'ignore)
507 (define-key map [scroll-bar-movement] #'ignore)
508 (define-key map [mouse-movement] move)
509 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
510 (define-key map [drag-mouse-1]
511 (lambda () (interactive) (funcall exitfun)))
512 ;; For vertical line dragging swallow also a mouse-1
513 ;; event (but only if we dragged at least once to allow mouse-1
514 ;; clicks to get through).
515 (when (eq line 'vertical)
516 (define-key map [mouse-1]
517 `(menu-item "" ,(lambda () (interactive) (funcall exitfun))
518 :filter ,(lambda (cmd) (if dragged cmd)))))
519 ;; Some of the events will of course end up looked up
520 ;; with a mode-line, header-line or vertical-line prefix ...
521 (define-key map [mode-line] map)
522 (define-key map [header-line] map)
523 (define-key map [vertical-line] map)
524 ;; ... and some maybe even with a right- or bottom-divider
525 ;; prefix.
526 (define-key map [right-divider] map)
527 (define-key map [bottom-divider] map)
528 map)
529 t (lambda () (setq track-mouse nil)))))))
531 (defun mouse-drag-mode-line (start-event)
532 "Change the height of a window by dragging on the mode line."
533 (interactive "e")
534 (mouse-drag-line start-event 'mode))
536 (defun mouse-drag-header-line (start-event)
537 "Change the height of a window by dragging on the header line."
538 (interactive "e")
539 (mouse-drag-line start-event 'header))
541 (defun mouse-drag-vertical-line (start-event)
542 "Change the width of a window by dragging on the vertical line."
543 (interactive "e")
544 (mouse-drag-line start-event 'vertical))
546 (defcustom mouse-select-region-move-to-beginning nil
547 "Effect of selecting a region extending backward from double click.
548 Nil means keep point at the position clicked (region end);
549 non-nil means move point to beginning of region."
550 :type '(choice (const :tag "Don't move point" nil)
551 (const :tag "Move point to beginning of region" t))
552 :group 'mouse
553 :version "25.2")
555 (defun mouse-set-point (event &optional promote-to-region)
556 "Move point to the position clicked on with the mouse.
557 This should be bound to a mouse click event type.
558 If PROMOTE-TO-REGION is non-nil and event is a multiple-click, select
559 the corresponding element around point, with the resulting position of
560 point determined by `mouse-select-region-move-to-beginning'."
561 (interactive "e\np")
562 (mouse-minibuffer-check event)
563 (if (and promote-to-region (> (event-click-count event) 1))
564 (progn
565 (mouse-set-region event)
566 (when mouse-select-region-move-to-beginning
567 (when (> (posn-point (event-start event)) (region-beginning))
568 (exchange-point-and-mark))))
569 ;; Use event-end in case called from mouse-drag-region.
570 ;; If EVENT is a click, event-end and event-start give same value.
571 (posn-set-point (event-end event))))
573 (defvar mouse-last-region-beg nil)
574 (defvar mouse-last-region-end nil)
575 (defvar mouse-last-region-tick nil)
577 (defun mouse-region-match ()
578 "Return non-nil if there's an active region that was set with the mouse."
579 (and (mark t) mark-active
580 (eq mouse-last-region-beg (region-beginning))
581 (eq mouse-last-region-end (region-end))
582 (eq mouse-last-region-tick (buffer-modified-tick))))
584 (defvar mouse--drag-start-event nil)
586 (defun mouse-set-region (click)
587 "Set the region to the text dragged over, and copy to kill ring.
588 This should be bound to a mouse drag event.
589 See the `mouse-drag-copy-region' variable to control whether this
590 command alters the kill ring or not."
591 (interactive "e")
592 (mouse-minibuffer-check click)
593 (select-window (posn-window (event-start click)))
594 (let ((beg (posn-point (event-start click)))
595 (end
596 (if (eq (posn-window (event-end click)) (selected-window))
597 (posn-point (event-end click))
598 ;; If the mouse ends up in any other window or on the menu
599 ;; bar, use `window-point' of selected window (Bug#23707).
600 (window-point)))
601 (click-count (event-click-count click)))
602 (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
603 (when drag-start
604 ;; Drag events don't come with a click count, sadly, so we hack
605 ;; our way around this problem by remembering the start-event in
606 ;; `mouse-drag-start' and fetching the click-count from there.
607 (when (and (<= click-count 1)
608 (equal beg (posn-point (event-start drag-start))))
609 (setq click-count (event-click-count drag-start)))
610 ;; Occasionally we get spurious drag events where the user hasn't
611 ;; dragged his mouse, but instead Emacs has dragged the text under the
612 ;; user's mouse. Try to recover those cases (bug#17562).
613 (when (and (equal (posn-x-y (event-start click))
614 (posn-x-y (event-end click)))
615 (not (eq (car drag-start) 'mouse-movement)))
616 (setq end beg))
617 (setf (terminal-parameter nil 'mouse-drag-start) nil)))
618 (when (and (integerp beg) (integerp end))
619 (let ((range (mouse-start-end beg end (1- click-count))))
620 (if (< end beg)
621 (setq end (nth 0 range) beg (nth 1 range))
622 (setq beg (nth 0 range) end (nth 1 range)))))
623 (and mouse-drag-copy-region (integerp beg) (integerp end)
624 ;; Don't set this-command to `kill-region', so a following
625 ;; C-w won't double the text in the kill ring. Ignore
626 ;; `last-command' so we don't append to a preceding kill.
627 (let (this-command last-command deactivate-mark)
628 (copy-region-as-kill beg end)))
629 (if (numberp beg) (goto-char beg))
630 ;; On a text terminal, bounce the cursor.
631 (or transient-mark-mode
632 (window-system)
633 (sit-for 1))
634 (push-mark)
635 (set-mark (point))
636 (if (numberp end) (goto-char end))
637 (mouse-set-region-1)))
639 (defun mouse-set-region-1 ()
640 ;; Set transient-mark-mode for a little while.
641 (unless (eq (car-safe transient-mark-mode) 'only)
642 (setq-local transient-mark-mode
643 (cons 'only
644 (unless (eq transient-mark-mode 'lambda)
645 transient-mark-mode))))
646 (setq mouse-last-region-beg (region-beginning))
647 (setq mouse-last-region-end (region-end))
648 (setq mouse-last-region-tick (buffer-modified-tick)))
650 (defcustom mouse-scroll-delay 0.25
651 "The pause between scroll steps caused by mouse drags, in seconds.
652 If you drag the mouse beyond the edge of a window, Emacs scrolls the
653 window to bring the text beyond that edge into view, with a delay of
654 this many seconds between scroll steps. Scrolling stops when you move
655 the mouse back into the window, or release the button.
656 This variable's value may be non-integral.
657 Setting this to zero causes Emacs to scroll as fast as it can."
658 :type 'number
659 :group 'mouse)
661 (defcustom mouse-scroll-min-lines 1
662 "The minimum number of lines scrolled by dragging mouse out of window.
663 Moving the mouse out the top or bottom edge of the window begins
664 scrolling repeatedly. The number of lines scrolled per repetition
665 is normally equal to the number of lines beyond the window edge that
666 the mouse has moved. However, it always scrolls at least the number
667 of lines specified by this variable."
668 :type 'integer
669 :group 'mouse)
671 (defun mouse-scroll-subr (window jump &optional overlay start)
672 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
673 If OVERLAY is an overlay, let it stretch from START to the far edge of
674 the newly visible text.
675 Upon exit, point is at the far edge of the newly visible text."
676 (cond
677 ((and (> jump 0) (< jump mouse-scroll-min-lines))
678 (setq jump mouse-scroll-min-lines))
679 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
680 (setq jump (- mouse-scroll-min-lines))))
681 (let ((opoint (point)))
682 (while (progn
683 (goto-char (window-start window))
684 (if (not (zerop (vertical-motion jump window)))
685 (progn
686 (set-window-start window (point))
687 (if (natnump jump)
688 (if (window-end window)
689 (progn
690 (goto-char (window-end window))
691 ;; window-end doesn't reflect the window's new
692 ;; start position until the next redisplay.
693 (vertical-motion (1- jump) window))
694 (vertical-motion (- (window-height window) 2)))
695 (goto-char (window-start window)))
696 (if overlay
697 (move-overlay overlay start (point)))
698 ;; Now that we have scrolled WINDOW properly,
699 ;; put point back where it was for the redisplay
700 ;; so that we don't mess up the selected window.
701 (or (eq window (selected-window))
702 (goto-char opoint))
703 (sit-for mouse-scroll-delay)))))
704 (or (eq window (selected-window))
705 (goto-char opoint))))
707 (defvar mouse-selection-click-count 0)
709 (defvar mouse-selection-click-count-buffer nil)
711 (defun mouse-drag-region (start-event)
712 "Set the region to the text that the mouse is dragged over.
713 Highlight the drag area as you move the mouse.
714 This must be bound to a button-down mouse event.
715 In Transient Mark mode, the highlighting remains as long as the mark
716 remains active. Otherwise, it remains until the next input event."
717 (interactive "e")
718 ;; Give temporary modes such as isearch a chance to turn off.
719 (run-hooks 'mouse-leave-buffer-hook)
720 (mouse-drag-track start-event))
723 (defun mouse-posn-property (pos property)
724 "Look for a property at click position.
725 POS may be either a buffer position or a click position like
726 those returned from `event-start'. If the click position is on
727 a string, the text property PROPERTY is examined.
728 If this is nil or the click is not on a string, then
729 the corresponding buffer position is searched for PROPERTY.
730 If PROPERTY is encountered in one of those places,
731 its value is returned."
732 (if (consp pos)
733 (let ((w (posn-window pos)) (pt (posn-point pos))
734 (str (posn-string pos)))
735 (or (and str
736 (get-text-property (cdr str) property (car str)))
737 ;; Mouse clicks in the fringe come with a position in
738 ;; (nth 5). This is useful but is not exactly where we clicked, so
739 ;; don't look up that position's properties!
740 (and pt (not (memq (posn-area pos) '(left-fringe right-fringe
741 left-margin right-margin)))
742 (get-char-property pt property w))))
743 (get-char-property pos property)))
745 (defun mouse-on-link-p (pos)
746 "Return non-nil if POS is on a link in the current buffer.
747 POS must specify a buffer position in the current buffer, as a list
748 of the form returned by the `event-start' and `event-end' functions,
749 or a mouse event location in the selected window (see `event-start').
750 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
751 POS may be a mouse event location in any window.
753 A clickable link is identified by one of the following methods:
755 - If the character at POS has a non-nil `follow-link' text or
756 overlay property, the value of that property determines what to do.
758 - If there is a local key-binding or a keybinding at position POS
759 for the `follow-link' event, the binding of that event determines
760 what to do.
762 The resulting value determine whether POS is inside a link:
764 - If the value is `mouse-face', POS is inside a link if there
765 is a non-nil `mouse-face' property at POS. Return t in this case.
767 - If the value is a function, FUNC, POS is inside a link if
768 the call \(FUNC POS) returns non-nil. Return the return value
769 from that call. Arg is \(posn-point POS) if POS is a mouse event.
771 - Otherwise, return the value itself.
773 The return value is interpreted as follows:
775 - If it is a string, the mouse-1 event is translated into the
776 first character of the string, i.e. the action of the mouse-1
777 click is the local or global binding of that character.
779 - If it is a vector, the mouse-1 event is translated into the
780 first element of that vector, i.e. the action of the mouse-1
781 click is the local or global binding of that event.
783 - Otherwise, the mouse-1 event is translated into a mouse-2 event
784 at the same position."
785 (let ((action
786 (and (or (not (consp pos))
787 mouse-1-click-in-non-selected-windows
788 (eq (selected-window) (posn-window pos)))
789 (or (mouse-posn-property pos 'follow-link)
790 (let ((area (posn-area pos)))
791 (when area
792 (key-binding (vector area 'follow-link) nil t pos)))
793 (key-binding [follow-link] nil t pos)))))
794 (cond
795 ((eq action 'mouse-face)
796 (and (mouse-posn-property pos 'mouse-face) t))
797 ((functionp action)
798 ;; FIXME: This seems questionable if the click is not in a buffer.
799 ;; Should we instead decide that `action' takes a `posn'?
800 (if (consp pos)
801 (with-current-buffer (window-buffer (posn-window pos))
802 (funcall action (posn-point pos)))
803 (funcall action pos)))
804 (t action))))
806 (defun mouse-fixup-help-message (msg)
807 "Fix help message MSG for `mouse-1-click-follows-link'."
808 (let (mp pos)
809 (if (and mouse-1-click-follows-link
810 (stringp msg)
811 (string-match-p "\\`mouse-2" msg)
812 (setq mp (mouse-pixel-position))
813 (consp (setq pos (cdr mp)))
814 (car pos) (>= (car pos) 0)
815 (cdr pos) (>= (cdr pos) 0)
816 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
817 (windowp (posn-window pos)))
818 (with-current-buffer (window-buffer (posn-window pos))
819 (if (mouse-on-link-p pos)
820 (setq msg (concat
821 (cond
822 ((eq mouse-1-click-follows-link 'double) "double-")
823 ((and (integerp mouse-1-click-follows-link)
824 (< mouse-1-click-follows-link 0)) "Long ")
825 (t ""))
826 "mouse-1" (substring msg 7)))))))
827 msg)
829 (defun mouse-drag-track (start-event)
830 "Track mouse drags by highlighting area between point and cursor.
831 The region will be defined with mark and point."
832 (mouse-minibuffer-check start-event)
833 (setq mouse-selection-click-count-buffer (current-buffer))
834 (deactivate-mark)
835 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
836 (start-posn (event-start start-event))
837 (start-point (posn-point start-posn))
838 (start-window (posn-window start-posn))
839 (_ (with-current-buffer (window-buffer start-window)
840 (setq deactivate-mark nil)))
841 ;; We've recorded what we needed from the current buffer and
842 ;; window, now let's jump to the place of the event, where things
843 ;; are happening.
844 (_ (mouse-set-point start-event))
845 (echo-keystrokes 0)
846 (bounds (window-edges start-window))
847 (make-cursor-line-fully-visible nil)
848 (top (nth 1 bounds))
849 (bottom (if (window-minibuffer-p start-window)
850 (nth 3 bounds)
851 ;; Don't count the mode line.
852 (1- (nth 3 bounds))))
853 (click-count (1- (event-click-count start-event)))
854 ;; Suppress automatic hscrolling, because that is a nuisance
855 ;; when setting point near the right fringe (but see below).
856 (auto-hscroll-mode-saved auto-hscroll-mode))
858 (setq mouse-selection-click-count click-count)
859 ;; In case the down click is in the middle of some intangible text,
860 ;; use the end of that text, and put it in START-POINT.
861 (if (< (point) start-point)
862 (goto-char start-point))
863 (setq start-point (point))
865 ;; Activate the region, using `mouse-start-end' to determine where
866 ;; to put point and mark (e.g., double-click will select a word).
867 (setq-local transient-mark-mode
868 (if (eq transient-mark-mode 'lambda)
869 '(only)
870 (cons 'only transient-mark-mode)))
871 (let ((range (mouse-start-end start-point start-point click-count)))
872 (push-mark (nth 0 range) t t)
873 (goto-char (nth 1 range)))
875 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
876 (setq track-mouse t)
877 (setq auto-hscroll-mode nil)
879 (set-transient-map
880 (let ((map (make-sparse-keymap)))
881 (define-key map [switch-frame] #'ignore)
882 (define-key map [select-window] #'ignore)
883 (define-key map [mouse-movement]
884 (lambda (event) (interactive "e")
885 (let* ((end (event-end event))
886 (end-point (posn-point end)))
887 (unless (eq end-point start-point)
888 ;; As soon as the user moves, we can re-enable auto-hscroll.
889 (setq auto-hscroll-mode auto-hscroll-mode-saved)
890 ;; And remember that we have moved, so mouse-set-region can know
891 ;; its event is really a drag event.
892 (setcar start-event 'mouse-movement))
893 (if (and (eq (posn-window end) start-window)
894 (integer-or-marker-p end-point))
895 (mouse--drag-set-mark-and-point start-point
896 end-point click-count)
897 (let ((mouse-row (cdr (cdr (mouse-position)))))
898 (cond
899 ((null mouse-row))
900 ((< mouse-row top)
901 (mouse-scroll-subr start-window (- mouse-row top)
902 nil start-point))
903 ((>= mouse-row bottom)
904 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
905 nil start-point))))))))
906 map)
907 t (lambda ()
908 (setq track-mouse nil)
909 (setq auto-hscroll-mode auto-hscroll-mode-saved)
910 (deactivate-mark)
911 (pop-mark)))))
913 (defun mouse--drag-set-mark-and-point (start click click-count)
914 (let* ((range (mouse-start-end start click click-count))
915 (beg (nth 0 range))
916 (end (nth 1 range)))
917 (cond ((eq (mark) beg)
918 (goto-char end))
919 ((eq (mark) end)
920 (goto-char beg))
921 ((< click (mark))
922 (set-mark end)
923 (goto-char beg))
925 (set-mark beg)
926 (goto-char end)))))
928 ;; Commands to handle xterm-style multiple clicks.
929 (defun mouse-skip-word (dir)
930 "Skip over word, over whitespace, or over identical punctuation.
931 If DIR is positive skip forward; if negative, skip backward."
932 (let* ((char (following-char))
933 (syntax (char-to-string (char-syntax char))))
934 (cond ((string= syntax "w")
935 ;; Here, we can't use skip-syntax-forward/backward because
936 ;; they don't pay attention to word-separating-categories,
937 ;; and thus they will skip over a true word boundary. So,
938 ;; we simulate the original behavior by using forward-word.
939 (if (< dir 0)
940 (if (not (looking-at "\\<"))
941 (forward-word -1))
942 (if (or (looking-at "\\<") (not (looking-at "\\>")))
943 (forward-word 1))))
944 ((string= syntax " ")
945 (if (< dir 0)
946 (skip-syntax-backward syntax)
947 (skip-syntax-forward syntax)))
948 ((string= syntax "_")
949 (if (< dir 0)
950 (skip-syntax-backward "w_")
951 (skip-syntax-forward "w_")))
952 ((< dir 0)
953 (while (and (not (bobp)) (= (preceding-char) char))
954 (forward-char -1)))
956 (while (and (not (eobp)) (= (following-char) char))
957 (forward-char 1))))))
959 (defun mouse-start-end (start end mode)
960 "Return a list of region bounds based on START and END according to MODE.
961 If MODE is 0 then set point to (min START END), mark to (max START END).
962 If MODE is 1 then set point to start of word at (min START END),
963 mark to end of word at (max START END).
964 If MODE is 2 then do the same for lines."
965 (if (> start end)
966 (let ((temp start))
967 (setq start end
968 end temp)))
969 (setq mode (mod mode 3))
970 (cond ((= mode 0)
971 (list start end))
972 ((and (= mode 1)
973 (= start end)
974 (char-after start)
975 (= (char-syntax (char-after start)) ?\())
976 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
977 ;; This happens in CC Mode when unbalanced parens in CPP
978 ;; constructs are given punctuation syntax with
979 ;; syntax-table text properties. (2016-02-21).
980 (signal 'scan-error (list "Containing expression ends prematurely"
981 start start))
982 (list start
983 (save-excursion
984 (goto-char start)
985 (forward-sexp 1)
986 (point)))))
987 ((and (= mode 1)
988 (= start end)
989 (char-after start)
990 (= (char-syntax (char-after start)) ?\)))
991 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
992 ;; See above comment about CC Mode.
993 (signal 'scan-error (list "Unbalanced parentheses" start start))
994 (list (save-excursion
995 (goto-char (1+ start))
996 (backward-sexp 1)
997 (point))
998 (1+ start))))
999 ((and (= mode 1)
1000 (= start end)
1001 (char-after start)
1002 (= (char-syntax (char-after start)) ?\"))
1003 (let ((open (or (eq start (point-min))
1004 (save-excursion
1005 (goto-char (- start 1))
1006 (looking-at "\\s(\\|\\s \\|\\s>")))))
1007 (if open
1008 (list start
1009 (save-excursion
1010 (condition-case nil
1011 (progn
1012 (goto-char start)
1013 (forward-sexp 1)
1014 (point))
1015 (error end))))
1016 (list (save-excursion
1017 (condition-case nil
1018 (progn
1019 (goto-char (1+ start))
1020 (backward-sexp 1)
1021 (point))
1022 (error end)))
1023 (1+ start)))))
1024 ((= mode 1)
1025 (list (save-excursion
1026 (goto-char start)
1027 (mouse-skip-word -1)
1028 (point))
1029 (save-excursion
1030 (goto-char end)
1031 (mouse-skip-word 1)
1032 (point))))
1033 ((= mode 2)
1034 (list (save-excursion
1035 (goto-char start)
1036 (line-beginning-position 1))
1037 (save-excursion
1038 (goto-char end)
1039 (forward-line 1)
1040 (point))))))
1042 ;; Subroutine: set the mark where CLICK happened,
1043 ;; but don't do anything else.
1044 (defun mouse-set-mark-fast (click)
1045 (mouse-minibuffer-check click)
1046 (let ((posn (event-start click)))
1047 (select-window (posn-window posn))
1048 (if (numberp (posn-point posn))
1049 (push-mark (posn-point posn) t t))))
1051 (defun mouse-undouble-last-event (events)
1052 (let* ((index (1- (length events)))
1053 (last (nthcdr index events))
1054 (event (car last))
1055 (basic (event-basic-type event))
1056 (old-modifiers (event-modifiers event))
1057 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1058 (new
1059 (if (consp event)
1060 ;; Use reverse, not nreverse, since event-modifiers
1061 ;; does not copy the list it returns.
1062 (cons (event-convert-list (reverse (cons basic modifiers)))
1063 (cdr event))
1064 event)))
1065 (setcar last new)
1066 (if (and (not (equal modifiers old-modifiers))
1067 (key-binding (apply 'vector events)))
1069 (setcar last event)
1070 nil)))
1072 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1074 (defun mouse-set-mark (click)
1075 "Set mark at the position clicked on with the mouse.
1076 Display cursor at that position for a second.
1077 This must be bound to a mouse click."
1078 (interactive "e")
1079 (mouse-minibuffer-check click)
1080 (select-window (posn-window (event-start click)))
1081 ;; FIXME: Use save-excursion
1082 (let ((point-save (point)))
1083 (unwind-protect
1084 (progn (mouse-set-point click)
1085 (push-mark nil t t)
1086 (or transient-mark-mode
1087 (sit-for 1)))
1088 (goto-char point-save))))
1090 (defun mouse-kill (click)
1091 "Kill the region between point and the mouse click.
1092 The text is saved in the kill ring, as with \\[kill-region]."
1093 (interactive "e")
1094 (mouse-minibuffer-check click)
1095 (let* ((posn (event-start click))
1096 (click-posn (posn-point posn)))
1097 (select-window (posn-window posn))
1098 (if (numberp click-posn)
1099 (kill-region (min (point) click-posn)
1100 (max (point) click-posn)))))
1102 (defun mouse-yank-at-click (click arg)
1103 "Insert the last stretch of killed text at the position clicked on.
1104 Also move point to one end of the text thus inserted (normally the end),
1105 and set mark at the beginning.
1106 Prefix arguments are interpreted as with \\[yank].
1107 If `mouse-yank-at-point' is non-nil, insert at point
1108 regardless of where you click."
1109 (interactive "e\nP")
1110 ;; Give temporary modes such as isearch a chance to turn off.
1111 (run-hooks 'mouse-leave-buffer-hook)
1112 (when select-active-regions
1113 ;; Without this, confusing things happen upon e.g. inserting into
1114 ;; the middle of an active region.
1115 (deactivate-mark))
1116 (or mouse-yank-at-point (mouse-set-point click))
1117 (setq this-command 'yank)
1118 (setq mouse-selection-click-count 0)
1119 (yank arg))
1121 (defun mouse-yank-primary (click)
1122 "Insert the primary selection at the position clicked on.
1123 Move point to the end of the inserted text, and set mark at
1124 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1125 regardless of where you click."
1126 (interactive "e")
1127 ;; Give temporary modes such as isearch a chance to turn off.
1128 (run-hooks 'mouse-leave-buffer-hook)
1129 ;; Without this, confusing things happen upon e.g. inserting into
1130 ;; the middle of an active region.
1131 (when select-active-regions
1132 (let (select-active-regions)
1133 (deactivate-mark)))
1134 (or mouse-yank-at-point (mouse-set-point click))
1135 (let ((primary (gui-get-primary-selection)))
1136 (push-mark (point))
1137 (insert-for-yank primary)))
1139 (defun mouse-kill-ring-save (click)
1140 "Copy the region between point and the mouse click in the kill ring.
1141 This does not delete the region; it acts like \\[kill-ring-save]."
1142 (interactive "e")
1143 (mouse-set-mark-fast click)
1144 (let (this-command last-command)
1145 (kill-ring-save (point) (mark t))))
1147 ;; This function used to delete the text between point and the mouse
1148 ;; whenever it was equal to the front of the kill ring, but some
1149 ;; people found that confusing.
1151 ;; The position of the last invocation of `mouse-save-then-kill'.
1152 (defvar mouse-save-then-kill-posn nil)
1154 (defun mouse-save-then-kill-delete-region (beg end)
1155 ;; We must make our own undo boundaries
1156 ;; because they happen automatically only for the current buffer.
1157 (undo-boundary)
1158 (if (or (= beg end) (eq buffer-undo-list t))
1159 ;; If we have no undo list in this buffer,
1160 ;; just delete.
1161 (delete-region beg end)
1162 ;; Delete, but make the undo-list entry share with the kill ring.
1163 ;; First, delete just one char, so in case buffer is being modified
1164 ;; for the first time, the undo list records that fact.
1165 (let ((inhibit-modification-hooks t))
1166 (delete-region beg
1167 (+ beg (if (> end beg) 1 -1))))
1168 (let ((buffer-undo-list buffer-undo-list))
1169 ;; Undo that deletion--but don't change the undo list!
1170 (let ((inhibit-modification-hooks t))
1171 (primitive-undo 1 buffer-undo-list))
1172 ;; Now delete the rest of the specified region,
1173 ;; but don't record it.
1174 (setq buffer-undo-list t)
1175 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1176 (error "Lossage in mouse-save-then-kill-delete-region"))
1177 (delete-region beg end))
1178 (let ((tail buffer-undo-list))
1179 ;; Search back in buffer-undo-list for the string
1180 ;; that came from deleting one character.
1181 (while (and tail (not (stringp (car (car tail)))))
1182 (setq tail (cdr tail)))
1183 ;; Replace it with an entry for the entire deleted text.
1184 (and tail
1185 (setcar tail (cons (car kill-ring) (min beg end))))))
1186 (undo-boundary))
1188 (defun mouse-save-then-kill (click)
1189 "Set the region according to CLICK; the second time, kill it.
1190 CLICK should be a mouse click event.
1192 If the region is inactive, activate it temporarily. Set mark at
1193 the original point, and move point to the position of CLICK.
1195 If the region is already active, adjust it. Normally, do this by
1196 moving point or mark, whichever is closer, to CLICK. But if you
1197 have selected whole words or lines, move point or mark to the
1198 word or line boundary closest to CLICK instead.
1200 If `mouse-drag-copy-region' is non-nil, this command also saves the
1201 new region to the kill ring (replacing the previous kill if the
1202 previous region was just saved to the kill ring).
1204 If this command is called a second consecutive time with the same
1205 CLICK position, kill the region (or delete it
1206 if `mouse-drag-copy-region' is non-nil)"
1207 (interactive "e")
1208 (mouse-minibuffer-check click)
1209 (let* ((posn (event-start click))
1210 (click-pt (posn-point posn))
1211 (window (posn-window posn))
1212 (buf (window-buffer window))
1213 ;; Don't let a subsequent kill command append to this one.
1214 (this-command this-command)
1215 ;; Check if the user has multi-clicked to select words/lines.
1216 (click-count
1217 (if (and (eq mouse-selection-click-count-buffer buf)
1218 (with-current-buffer buf (mark t)))
1219 mouse-selection-click-count
1220 0)))
1221 (cond
1222 ((not (numberp click-pt)) nil)
1223 ;; If the user clicked without moving point, kill the region.
1224 ;; This also resets `mouse-selection-click-count'.
1225 ((and (eq last-command 'mouse-save-then-kill)
1226 (eq click-pt mouse-save-then-kill-posn)
1227 (eq window (selected-window)))
1228 (if mouse-drag-copy-region
1229 ;; Region already saved in the previous click;
1230 ;; don't make a duplicate entry, just delete.
1231 (delete-region (mark t) (point))
1232 (kill-region (mark t) (point)))
1233 (setq mouse-selection-click-count 0)
1234 (setq mouse-save-then-kill-posn nil))
1236 ;; Otherwise, if there is a suitable region, adjust it by moving
1237 ;; one end (whichever is closer) to CLICK-PT.
1238 ((or (with-current-buffer buf (region-active-p))
1239 (and (eq window (selected-window))
1240 (mark t)
1241 (or (and (eq last-command 'mouse-save-then-kill)
1242 mouse-save-then-kill-posn)
1243 (and (memq last-command '(mouse-drag-region
1244 mouse-set-region))
1245 (or mark-even-if-inactive
1246 (not transient-mark-mode))))))
1247 (select-window window)
1248 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1249 (if (< (abs (- click-pt (mark t)))
1250 (abs (- click-pt (point))))
1251 (set-mark (car range))
1252 (goto-char (nth 1 range)))
1253 (setq deactivate-mark nil)
1254 (mouse-set-region-1)
1255 (when mouse-drag-copy-region
1256 ;; Region already copied to kill-ring once, so replace.
1257 (kill-new (filter-buffer-substring (mark t) (point)) t))
1258 ;; Arrange for a repeated mouse-3 to kill the region.
1259 (setq mouse-save-then-kill-posn click-pt)))
1261 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1263 (select-window window)
1264 (mouse-set-mark-fast click)
1265 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1266 (if before-scroll (goto-char before-scroll)))
1267 (exchange-point-and-mark)
1268 (mouse-set-region-1)
1269 (when mouse-drag-copy-region
1270 (kill-new (filter-buffer-substring (mark t) (point))))
1271 (setq mouse-save-then-kill-posn click-pt)))))
1274 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1275 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1276 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1277 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1278 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1280 (defconst mouse-secondary-overlay
1281 (let ((ol (make-overlay (point-min) (point-min))))
1282 (delete-overlay ol)
1283 (overlay-put ol 'face 'secondary-selection)
1285 "An overlay which records the current secondary selection.
1286 It is deleted when there is no secondary selection.")
1288 (defvar mouse-secondary-click-count 0)
1290 ;; A marker which records the specified first end for a secondary selection.
1291 ;; May be nil.
1292 (defvar mouse-secondary-start nil)
1294 (defun mouse-start-secondary (click)
1295 "Set one end of the secondary selection to the position clicked on.
1296 Use \\[mouse-secondary-save-then-kill] to set the other end
1297 and complete the secondary selection."
1298 (interactive "e")
1299 (mouse-minibuffer-check click)
1300 (let ((posn (event-start click)))
1301 (with-current-buffer (window-buffer (posn-window posn))
1302 ;; Cancel any preexisting secondary selection.
1303 (delete-overlay mouse-secondary-overlay)
1304 (if (numberp (posn-point posn))
1305 (progn
1306 (or mouse-secondary-start
1307 (setq mouse-secondary-start (make-marker)))
1308 (move-marker mouse-secondary-start (posn-point posn)))))))
1310 (defun mouse-set-secondary (click)
1311 "Set the secondary selection to the text that the mouse is dragged over.
1312 This must be bound to a mouse drag event."
1313 (interactive "e")
1314 (mouse-minibuffer-check click)
1315 (let ((posn (event-start click))
1317 (end (event-end click)))
1318 (with-current-buffer (window-buffer (posn-window posn))
1319 (if (numberp (posn-point posn))
1320 (setq beg (posn-point posn)))
1321 (move-overlay mouse-secondary-overlay beg (posn-point end))
1322 (gui-set-selection
1323 'SECONDARY
1324 (buffer-substring (overlay-start mouse-secondary-overlay)
1325 (overlay-end mouse-secondary-overlay))))))
1327 (defun mouse-drag-secondary (start-event)
1328 "Set the secondary selection to the text that the mouse is dragged over.
1329 Highlight the drag area as you move the mouse.
1330 This must be bound to a button-down mouse event.
1331 The function returns a non-nil value if it creates a secondary selection."
1332 (interactive "e")
1333 (mouse-minibuffer-check start-event)
1334 (let* ((echo-keystrokes 0)
1335 (start-posn (event-start start-event))
1336 (start-point (posn-point start-posn))
1337 (start-window (posn-window start-posn))
1338 (bounds (window-edges start-window))
1339 (top (nth 1 bounds))
1340 (bottom (if (window-minibuffer-p start-window)
1341 (nth 3 bounds)
1342 ;; Don't count the mode line.
1343 (1- (nth 3 bounds))))
1344 (click-count (1- (event-click-count start-event))))
1345 (with-current-buffer (window-buffer start-window)
1346 (setq mouse-secondary-click-count click-count)
1347 (if (> (mod click-count 3) 0)
1348 ;; Double or triple press: make an initial selection
1349 ;; of one word or line.
1350 (let ((range (mouse-start-end start-point start-point click-count)))
1351 (set-marker mouse-secondary-start nil)
1352 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1353 (window-buffer start-window)))
1354 ;; Single-press: cancel any preexisting secondary selection.
1355 (or mouse-secondary-start
1356 (setq mouse-secondary-start (make-marker)))
1357 (set-marker mouse-secondary-start start-point)
1358 (delete-overlay mouse-secondary-overlay))
1359 ;; FIXME: Use mouse-drag-track!
1360 (let (event end end-point)
1361 (track-mouse
1362 (while (progn
1363 (setq event (read-event))
1364 (or (mouse-movement-p event)
1365 (memq (car-safe event) '(switch-frame select-window))))
1367 (if (memq (car-safe event) '(switch-frame select-window))
1369 (setq end (event-end event)
1370 end-point (posn-point end))
1371 (cond
1372 ;; Are we moving within the original window?
1373 ((and (eq (posn-window end) start-window)
1374 (integer-or-marker-p end-point))
1375 (let ((range (mouse-start-end start-point end-point
1376 click-count)))
1377 (if (or (/= start-point end-point)
1378 (null (marker-position mouse-secondary-start)))
1379 (progn
1380 (set-marker mouse-secondary-start nil)
1381 (move-overlay mouse-secondary-overlay
1382 (car range) (nth 1 range))))))
1384 (let ((mouse-row (cdr (cdr (mouse-position)))))
1385 (cond
1386 ((null mouse-row))
1387 ((< mouse-row top)
1388 (mouse-scroll-subr start-window (- mouse-row top)
1389 mouse-secondary-overlay start-point))
1390 ((>= mouse-row bottom)
1391 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1392 mouse-secondary-overlay start-point)))))))))
1394 (if (consp event)
1395 (if (marker-position mouse-secondary-start)
1396 (save-window-excursion
1397 (delete-overlay mouse-secondary-overlay)
1398 (gui-set-selection 'SECONDARY nil)
1399 (select-window start-window)
1400 (save-excursion
1401 (goto-char mouse-secondary-start)
1402 (sit-for 1)
1403 nil))
1404 (gui-set-selection
1405 'SECONDARY
1406 (buffer-substring (overlay-start mouse-secondary-overlay)
1407 (overlay-end mouse-secondary-overlay)))))))))
1409 (defun mouse-yank-secondary (click)
1410 "Insert the secondary selection at the position clicked on.
1411 Move point to the end of the inserted text.
1412 If `mouse-yank-at-point' is non-nil, insert at point
1413 regardless of where you click."
1414 (interactive "e")
1415 ;; Give temporary modes such as isearch a chance to turn off.
1416 (run-hooks 'mouse-leave-buffer-hook)
1417 (or mouse-yank-at-point (mouse-set-point click))
1418 (let ((secondary (gui-get-selection 'SECONDARY)))
1419 (if secondary
1420 (insert-for-yank secondary)
1421 (error "No secondary selection"))))
1423 (defun mouse-kill-secondary ()
1424 "Kill the text in the secondary selection.
1425 This is intended more as a keyboard command than as a mouse command
1426 but it can work as either one.
1428 The current buffer (in case of keyboard use), or the buffer clicked on,
1429 must be the one that the secondary selection is in. This requirement
1430 is to prevent accidents."
1431 (interactive)
1432 (let* ((keys (this-command-keys))
1433 (click (elt keys (1- (length keys)))))
1434 (or (eq (overlay-buffer mouse-secondary-overlay)
1435 (if (listp click)
1436 (window-buffer (posn-window (event-start click)))
1437 (current-buffer)))
1438 (error "Select or click on the buffer where the secondary selection is")))
1439 (let (this-command)
1440 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1441 (kill-region (overlay-start mouse-secondary-overlay)
1442 (overlay-end mouse-secondary-overlay))))
1443 (delete-overlay mouse-secondary-overlay))
1445 (defun mouse-secondary-save-then-kill (click)
1446 "Set the secondary selection and save it to the kill ring.
1447 The second time, kill it. CLICK should be a mouse click event.
1449 If you have not called `mouse-start-secondary' in the clicked
1450 buffer, activate the secondary selection and set it between point
1451 and the click position CLICK.
1453 Otherwise, adjust the bounds of the secondary selection.
1454 Normally, do this by moving its beginning or end, whichever is
1455 closer, to CLICK. But if you have selected whole words or lines,
1456 adjust to the word or line boundary closest to CLICK instead.
1458 If this command is called a second consecutive time with the same
1459 CLICK position, kill the secondary selection."
1460 (interactive "e")
1461 (mouse-minibuffer-check click)
1462 (let* ((posn (event-start click))
1463 (click-pt (posn-point posn))
1464 (window (posn-window posn))
1465 (buf (window-buffer window))
1466 ;; Don't let a subsequent kill command append to this one.
1467 (this-command this-command)
1468 ;; Check if the user has multi-clicked to select words/lines.
1469 (click-count
1470 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1471 mouse-secondary-click-count
1473 (beg (overlay-start mouse-secondary-overlay))
1474 (end (overlay-end mouse-secondary-overlay)))
1476 (cond
1477 ((not (numberp click-pt)) nil)
1479 ;; If the secondary selection is not active in BUF, activate it.
1480 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1481 (if mouse-secondary-start
1482 (marker-buffer mouse-secondary-start)))))
1483 (select-window window)
1484 (setq mouse-secondary-start (make-marker))
1485 (move-marker mouse-secondary-start (point))
1486 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1487 (kill-ring-save (point) click-pt))
1489 ;; If the user clicked without moving point, delete the secondary
1490 ;; selection. This also resets `mouse-secondary-click-count'.
1491 ((and (eq last-command 'mouse-secondary-save-then-kill)
1492 (eq click-pt mouse-save-then-kill-posn)
1493 (eq window (selected-window)))
1494 (mouse-save-then-kill-delete-region beg end)
1495 (delete-overlay mouse-secondary-overlay)
1496 (setq mouse-secondary-click-count 0)
1497 (setq mouse-save-then-kill-posn nil))
1499 ;; Otherwise, if there is a suitable secondary selection overlay,
1500 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1501 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1502 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1503 (if (< (abs (- click-pt beg))
1504 (abs (- click-pt end)))
1505 (move-overlay mouse-secondary-overlay (car range) end)
1506 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1507 (setq deactivate-mark nil)
1508 (if (eq last-command 'mouse-secondary-save-then-kill)
1509 ;; If the front of the kill ring comes from an immediately
1510 ;; previous use of this command, replace the entry.
1511 (kill-new
1512 (buffer-substring (overlay-start mouse-secondary-overlay)
1513 (overlay-end mouse-secondary-overlay))
1515 (let (deactivate-mark)
1516 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1517 (overlay-end mouse-secondary-overlay))))
1518 (setq mouse-save-then-kill-posn click-pt))
1520 ;; Otherwise, set the secondary selection overlay.
1522 (select-window window)
1523 (if mouse-secondary-start
1524 ;; All we have is one end of a selection, so put the other
1525 ;; end here.
1526 (let ((start (+ 0 mouse-secondary-start)))
1527 (kill-ring-save start click-pt)
1528 (move-overlay mouse-secondary-overlay start click-pt)))
1529 (setq mouse-save-then-kill-posn click-pt))))
1531 ;; Finally, set the window system's secondary selection.
1532 (let (str)
1533 (and (overlay-buffer mouse-secondary-overlay)
1534 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1535 (overlay-end mouse-secondary-overlay)))
1536 (> (length str) 0)
1537 (gui-set-selection 'SECONDARY str))))
1540 (defcustom mouse-buffer-menu-maxlen 20
1541 "Number of buffers in one pane (submenu) of the buffer menu.
1542 If we have lots of buffers, divide them into groups of
1543 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1544 :type 'integer
1545 :group 'mouse)
1547 (defcustom mouse-buffer-menu-mode-mult 4
1548 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1549 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1550 will split the buffer menu by the major modes (see
1551 `mouse-buffer-menu-mode-groups') or just by menu length.
1552 Set to 1 (or even 0!) if you want to group by major mode always, and to
1553 a large number if you prefer a mixed multitude. The default is 4."
1554 :type 'integer
1555 :group 'mouse
1556 :version "20.3")
1558 (defvar mouse-buffer-menu-mode-groups
1559 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1560 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1561 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1562 . "Mail/News")
1563 ("\\<C\\>" . "C")
1564 ("ObjC" . "C")
1565 ("Text" . "Text")
1566 ("Outline" . "Text")
1567 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1568 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1569 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1570 . "GDB")
1571 ("Lisp" . "Lisp")))
1572 "How to group various major modes together in \\[mouse-buffer-menu].
1573 Each element has the form (REGEXP . GROUPNAME).
1574 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1576 (defun mouse-buffer-menu (event)
1577 "Pop up a menu of buffers for selection with the mouse.
1578 This switches buffers in the window that you clicked on,
1579 and selects that window."
1580 (interactive "e")
1581 (mouse-minibuffer-check event)
1582 (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1583 (window (posn-window (event-start event))))
1584 (when buf
1585 (select-window
1586 (if (framep window) (frame-selected-window window)
1587 window))
1588 (switch-to-buffer buf))))
1590 (defun mouse-buffer-menu-map ()
1591 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1592 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
1593 (dolist (buf buffers)
1594 ;; Divide all buffers into buckets for various major modes.
1595 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1596 (with-current-buffer buf
1597 (let* ((adjusted-major-mode major-mode) elt)
1598 (dolist (group mouse-buffer-menu-mode-groups)
1599 (when (string-match (car group) (format-mode-line mode-name))
1600 (setq adjusted-major-mode (cdr group))))
1601 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1602 (unless elt
1603 (setq elt (list adjusted-major-mode
1604 (if (stringp adjusted-major-mode)
1605 adjusted-major-mode
1606 (format-mode-line mode-name nil nil buf)))
1607 split-by-major-mode (cons elt split-by-major-mode)))
1608 (or (memq buf (cdr (cdr elt)))
1609 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1610 ;; Compute the sum of squares of sizes of the major-mode buckets.
1611 (let ((tail split-by-major-mode))
1612 (setq sum-of-squares 0)
1613 (while tail
1614 (setq sum-of-squares
1615 (+ sum-of-squares
1616 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1617 (setq tail (cdr tail))))
1618 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1619 (* (length buffers) (length buffers)))
1620 ;; Subdividing by major modes really helps, so let's do it.
1621 (let (subdivided-menus (buffers-left (length buffers)))
1622 ;; Sort the list to put the most popular major modes first.
1623 (setq split-by-major-mode
1624 (sort split-by-major-mode
1625 (function (lambda (elt1 elt2)
1626 (> (length elt1) (length elt2))))))
1627 ;; Make a separate submenu for each major mode
1628 ;; that has more than one buffer,
1629 ;; unless all the remaining buffers are less than 1/10 of them.
1630 (while (and split-by-major-mode
1631 (and (> (length (car split-by-major-mode)) 3)
1632 (> (* buffers-left 10) (length buffers))))
1633 (let ((this-mode-list (mouse-buffer-menu-alist
1634 (cdr (cdr (car split-by-major-mode))))))
1635 (and this-mode-list
1636 (setq subdivided-menus
1637 (cons (cons
1638 (nth 1 (car split-by-major-mode))
1639 this-mode-list)
1640 subdivided-menus))))
1641 (setq buffers-left
1642 (- buffers-left (length (cdr (car split-by-major-mode)))))
1643 (setq split-by-major-mode (cdr split-by-major-mode)))
1644 ;; If any major modes are left over,
1645 ;; make a single submenu for them.
1646 (if split-by-major-mode
1647 (let ((others-list
1648 (mouse-buffer-menu-alist
1649 ;; we don't need split-by-major-mode any more,
1650 ;; so we can ditch it with nconc (mapcan).
1651 (mapcan 'cddr split-by-major-mode))))
1652 (and others-list
1653 (setq subdivided-menus
1654 (cons (cons "Others" others-list)
1655 subdivided-menus)))))
1656 (cons "Buffer Menu" (nreverse subdivided-menus)))
1657 (cons "Buffer Menu"
1658 (mouse-buffer-menu-split "Select Buffer"
1659 (mouse-buffer-menu-alist buffers))))))
1661 (defun mouse-buffer-menu-alist (buffers)
1662 (let (tail
1663 (maxlen 0)
1664 head)
1665 (setq buffers
1666 (sort buffers
1667 (function (lambda (elt1 elt2)
1668 (string< (buffer-name elt1) (buffer-name elt2))))))
1669 (setq tail buffers)
1670 (while tail
1671 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1672 (setq maxlen
1673 (max maxlen
1674 (length (buffer-name (car tail))))))
1675 (setq tail (cdr tail)))
1676 (setq tail buffers)
1677 (while tail
1678 (let ((elt (car tail)))
1679 (if (/= (aref (buffer-name elt) 0) ?\s)
1680 (setq head
1681 (cons
1682 (cons
1683 (format
1684 (format "%%-%ds %%s%%s %%s" maxlen)
1685 (buffer-name elt)
1686 (if (buffer-modified-p elt) "*" " ")
1687 (with-current-buffer elt
1688 (if buffer-read-only "%" " "))
1689 (or (buffer-file-name elt)
1690 (with-current-buffer elt
1691 (if list-buffers-directory
1692 (expand-file-name
1693 list-buffers-directory)))
1694 ""))
1695 elt)
1696 head))))
1697 (setq tail (cdr tail)))
1698 ;; Compensate for the reversal that the above loop does.
1699 (nreverse head)))
1701 (defun mouse-buffer-menu-split (title alist)
1702 ;; If we have lots of buffers, divide them into groups of 20
1703 ;; and make a pane (or submenu) for each one.
1704 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1705 (let ((alist alist) sublists next
1706 (i 1))
1707 (while alist
1708 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1709 ;; and make them the next element of sublist.
1710 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1711 (if next
1712 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1713 nil))
1714 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1715 sublists))
1716 (setq i (1+ i))
1717 (setq alist next))
1718 (nreverse sublists))
1719 ;; Few buffers--put them all in one pane.
1720 (list (cons title alist))))
1722 (define-obsolete-function-alias
1723 'mouse-choose-completion 'choose-completion "23.2")
1725 ;; Font selection.
1727 (defun font-menu-add-default ()
1728 (let* ((default (frame-parameter nil 'font))
1729 (font-alist x-fixed-font-alist)
1730 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1731 (if (assoc "Default" elt)
1732 (delete (assoc "Default" elt) elt))
1733 (setcdr elt
1734 (cons (list "Default" default)
1735 (cdr elt)))))
1737 (defvar x-fixed-font-alist
1738 (list
1739 (purecopy "Font Menu")
1740 (cons
1741 (purecopy "Misc")
1742 (mapcar
1743 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1744 ;; For these, we specify the pixel height and width.
1745 '(("fixed" "fixed")
1746 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1747 ("6x12"
1748 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1749 ("6x13"
1750 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1751 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1752 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1753 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1754 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1755 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1756 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1757 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1758 ("")
1759 ("clean 5x8"
1760 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1761 ("clean 6x8"
1762 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1763 ("clean 8x8"
1764 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1765 ("clean 8x10"
1766 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1767 ("clean 8x14"
1768 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1769 ("clean 8x16"
1770 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1771 ("")
1772 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1773 ;; We don't seem to have these; who knows what they are.
1774 ;; ("fg-18" "fg-18")
1775 ;; ("fg-25" "fg-25")
1776 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1777 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1778 ("lucidasanstypewriter-bold-24"
1779 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
1780 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1781 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1784 (cons
1785 (purecopy "Courier")
1786 (mapcar
1787 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1788 ;; For these, we specify the point height.
1789 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1790 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1791 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1792 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1793 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1794 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1795 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1796 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1797 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1798 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1799 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1800 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1801 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1802 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1803 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1804 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1805 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1806 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1807 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1808 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1809 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1810 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1811 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1812 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1813 ))))
1814 "X fonts suitable for use in Emacs.")
1816 (declare-function generate-fontset-menu "fontset" ())
1818 (defun mouse-select-font ()
1819 "Prompt for a font name, using `x-popup-menu', and return it."
1820 (interactive)
1821 (unless (display-multi-font-p)
1822 (error "Cannot change fonts on this display"))
1823 (car
1824 (x-popup-menu
1825 (if (listp last-nonmenu-event)
1826 last-nonmenu-event
1827 (list '(0 0) (selected-window)))
1828 (append x-fixed-font-alist
1829 (list (generate-fontset-menu))))))
1831 (declare-function text-scale-mode "face-remap")
1833 (defun mouse-set-font (&rest fonts)
1834 "Set the default font for the selected frame.
1835 The argument FONTS is a list of font names; the first valid font
1836 in this list is used.
1838 When called interactively, pop up a menu and allow the user to
1839 choose a font."
1840 (interactive
1841 (progn (unless (display-multi-font-p)
1842 (error "Cannot change fonts on this display"))
1843 (x-popup-menu
1844 (if (listp last-nonmenu-event)
1845 last-nonmenu-event
1846 (list '(0 0) (selected-window)))
1847 ;; Append list of fontsets currently defined.
1848 (append x-fixed-font-alist (list (generate-fontset-menu))))))
1849 (if fonts
1850 (let (font)
1851 (while fonts
1852 (condition-case nil
1853 (progn
1854 (set-frame-font (car fonts))
1855 (setq font (car fonts))
1856 (setq fonts nil))
1857 (error
1858 (setq fonts (cdr fonts)))))
1859 (if (null font)
1860 (error "Font not found")))))
1862 (defvar mouse-appearance-menu-map nil)
1863 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
1864 (declare-function buffer-face-mode-invoke "face-remap"
1865 (face arg &optional interactive))
1866 (declare-function font-face-attributes "font.c" (font &optional frame))
1867 (defvar w32-use-w32-font-dialog)
1868 (defvar w32-fixed-font-alist)
1870 (defun mouse-appearance-menu (event)
1871 "Show a menu for changing the default face in the current buffer."
1872 (interactive "@e")
1873 (require 'face-remap)
1874 (when (display-multi-font-p)
1875 (with-selected-window (car (event-start event))
1876 (if mouse-appearance-menu-map
1877 nil ; regenerate new fonts
1878 ;; Initialize mouse-appearance-menu-map
1879 (setq mouse-appearance-menu-map
1880 (make-sparse-keymap "Change Default Buffer Face"))
1881 (define-key mouse-appearance-menu-map [face-remap-reset-base]
1882 '(menu-item "Reset to Default" face-remap-reset-base))
1883 (define-key mouse-appearance-menu-map [text-scale-decrease]
1884 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
1885 (define-key mouse-appearance-menu-map [text-scale-increase]
1886 '(menu-item "Increase Buffer Text Size" text-scale-increase))
1887 ;; Font selector
1888 (if (and (functionp 'x-select-font)
1889 (or (not (boundp 'w32-use-w32-font-dialog))
1890 w32-use-w32-font-dialog))
1891 (define-key mouse-appearance-menu-map [x-select-font]
1892 '(menu-item "Change Buffer Font..." x-select-font))
1893 ;; If the select-font is unavailable, construct a menu.
1894 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
1895 (font-alist (cdr (append
1896 (if (eq system-type 'windows-nt)
1897 w32-fixed-font-alist
1898 x-fixed-font-alist)
1899 (list (generate-fontset-menu))))))
1900 (dolist (family font-alist)
1901 (let* ((submenu-name (car family))
1902 (submenu-map (make-sparse-keymap submenu-name)))
1903 (dolist (font (cdr family))
1904 (let ((font-name (car font))
1905 font-symbol)
1906 (if (string= font-name "")
1907 (define-key submenu-map [space]
1908 '("--"))
1909 (setq font-symbol (intern (cadr font)))
1910 (define-key submenu-map (vector font-symbol)
1911 (list 'menu-item (car font) font-symbol)))))
1912 (define-key font-submenu (vector (intern submenu-name))
1913 (list 'menu-item submenu-name submenu-map))))
1914 (define-key mouse-appearance-menu-map [font-submenu]
1915 (list 'menu-item "Change Text Font" font-submenu)))))
1916 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
1917 (setq choice (nth (1- (length choice)) choice))
1918 (cond ((eq choice 'text-scale-increase)
1919 (text-scale-increase 1))
1920 ((eq choice 'text-scale-decrease)
1921 (text-scale-increase -1))
1922 ((eq choice 'face-remap-reset-base)
1923 (text-scale-mode 0)
1924 (buffer-face-mode 0))
1925 (choice
1926 ;; Either choice == 'x-select-font, or choice is a
1927 ;; symbol whose name is a font.
1928 (let ((font (if (eq choice 'x-select-font)
1929 (x-select-font)
1930 (symbol-name choice))))
1931 (buffer-face-mode-invoke
1932 (if (fontp font 'font-spec)
1933 (list :font font)
1934 (font-face-attributes font))
1935 t (called-interactively-p 'interactive)))))))))
1938 ;;; Bindings for mouse commands.
1940 (global-set-key [down-mouse-1] 'mouse-drag-region)
1941 (global-set-key [mouse-1] 'mouse-set-point)
1942 (global-set-key [drag-mouse-1] 'mouse-set-region)
1944 (defun mouse--strip-first-event (_prompt)
1945 (substring (this-single-command-raw-keys) 1))
1947 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
1948 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
1950 (global-set-key [mouse-2] 'mouse-yank-primary)
1951 ;; Allow yanking also when the corresponding cursor is "in the fringe".
1952 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
1953 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
1954 (global-set-key [mouse-3] 'mouse-save-then-kill)
1955 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
1956 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
1958 ;; By binding these to down-going events, we let the user use the up-going
1959 ;; event to make the selection, saving a click.
1960 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1961 (if (not (eq system-type 'ms-dos))
1962 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
1963 ;; C-down-mouse-2 is bound in facemenu.el.
1964 (global-set-key [C-down-mouse-3]
1965 `(menu-item ,(purecopy "Menu Bar") ignore
1966 :filter (lambda (_)
1967 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
1968 (mouse-menu-bar-map)
1969 (mouse-menu-major-mode-map)))))
1971 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
1972 ;; vertical-line prevents Emacs from signaling an error when the mouse
1973 ;; button is released after dragging these lines, on non-toolkit
1974 ;; versions.
1975 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
1976 (global-set-key [header-line mouse-1] 'mouse-select-window)
1977 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1978 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1979 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1980 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1981 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1982 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1983 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1984 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
1985 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1986 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1987 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1988 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
1989 (global-set-key [right-divider mouse-1] 'ignore)
1990 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
1991 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
1992 (global-set-key [bottom-divider mouse-1] 'ignore)
1993 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
1995 (provide 'mouse)
1997 ;;; mouse.el ends here