Implement quoting the local part of a remote file name
[emacs.git] / lisp / mouse.el
blobdb9f13b2e6c5a119411d1292673a0252942f3cf2
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 (old-track-mouse track-mouse))
498 ;; Start tracking. The special value 'dragging' signals the
499 ;; display engine to freeze the mouse pointer shape for as long
500 ;; as we drag.
501 (setq track-mouse 'dragging)
502 ;; Loop reading events and sampling the position of the mouse.
503 (setq exitfun
504 (set-transient-map
505 (let ((map (make-sparse-keymap)))
506 (define-key map [switch-frame] #'ignore)
507 (define-key map [select-window] #'ignore)
508 (define-key map [scroll-bar-movement] #'ignore)
509 (define-key map [mouse-movement] move)
510 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
511 (define-key map [drag-mouse-1]
512 (lambda () (interactive) (funcall exitfun)))
513 ;; For vertical line dragging swallow also a mouse-1
514 ;; event (but only if we dragged at least once to allow mouse-1
515 ;; clicks to get through).
516 (when (eq line 'vertical)
517 (define-key map [mouse-1]
518 `(menu-item "" ,(lambda () (interactive) (funcall exitfun))
519 :filter ,(lambda (cmd) (if dragged cmd)))))
520 ;; Some of the events will of course end up looked up
521 ;; with a mode-line, header-line or vertical-line prefix ...
522 (define-key map [mode-line] map)
523 (define-key map [header-line] map)
524 (define-key map [vertical-line] map)
525 ;; ... and some maybe even with a right- or bottom-divider
526 ;; prefix.
527 (define-key map [right-divider] map)
528 (define-key map [bottom-divider] map)
529 map)
530 t (lambda () (setq track-mouse old-track-mouse)))))))
532 (defun mouse-drag-mode-line (start-event)
533 "Change the height of a window by dragging on the mode line."
534 (interactive "e")
535 (mouse-drag-line start-event 'mode))
537 (defun mouse-drag-header-line (start-event)
538 "Change the height of a window by dragging on the header line."
539 (interactive "e")
540 (mouse-drag-line start-event 'header))
542 (defun mouse-drag-vertical-line (start-event)
543 "Change the width of a window by dragging on the vertical line."
544 (interactive "e")
545 (mouse-drag-line start-event 'vertical))
547 (defcustom mouse-select-region-move-to-beginning nil
548 "Effect of selecting a region extending backward from double click.
549 Nil means keep point at the position clicked (region end);
550 non-nil means move point to beginning of region."
551 :type '(choice (const :tag "Don't move point" nil)
552 (const :tag "Move point to beginning of region" t))
553 :group 'mouse
554 :version "26.1")
556 (defun mouse-set-point (event &optional promote-to-region)
557 "Move point to the position clicked on with the mouse.
558 This should be bound to a mouse click event type.
559 If PROMOTE-TO-REGION is non-nil and event is a multiple-click, select
560 the corresponding element around point, with the resulting position of
561 point determined by `mouse-select-region-move-to-beginning'."
562 (interactive "e\np")
563 (mouse-minibuffer-check event)
564 (if (and promote-to-region (> (event-click-count event) 1))
565 (progn
566 (mouse-set-region event)
567 (when mouse-select-region-move-to-beginning
568 (when (> (posn-point (event-start event)) (region-beginning))
569 (exchange-point-and-mark))))
570 ;; Use event-end in case called from mouse-drag-region.
571 ;; If EVENT is a click, event-end and event-start give same value.
572 (posn-set-point (event-end event))))
574 (defvar mouse-last-region-beg nil)
575 (defvar mouse-last-region-end nil)
576 (defvar mouse-last-region-tick nil)
578 (defun mouse-region-match ()
579 "Return non-nil if there's an active region that was set with the mouse."
580 (and (mark t) mark-active
581 (eq mouse-last-region-beg (region-beginning))
582 (eq mouse-last-region-end (region-end))
583 (eq mouse-last-region-tick (buffer-modified-tick))))
585 (defvar mouse--drag-start-event nil)
587 (defun mouse-set-region (click)
588 "Set the region to the text dragged over, and copy to kill ring.
589 This should be bound to a mouse drag event.
590 See the `mouse-drag-copy-region' variable to control whether this
591 command alters the kill ring or not."
592 (interactive "e")
593 (mouse-minibuffer-check click)
594 (select-window (posn-window (event-start click)))
595 (let ((beg (posn-point (event-start click)))
596 (end
597 (if (eq (posn-window (event-end click)) (selected-window))
598 (posn-point (event-end click))
599 ;; If the mouse ends up in any other window or on the menu
600 ;; bar, use `window-point' of selected window (Bug#23707).
601 (window-point)))
602 (click-count (event-click-count click)))
603 (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
604 (when drag-start
605 ;; Drag events don't come with a click count, sadly, so we hack
606 ;; our way around this problem by remembering the start-event in
607 ;; `mouse-drag-start' and fetching the click-count from there.
608 (when (and (<= click-count 1)
609 (equal beg (posn-point (event-start drag-start))))
610 (setq click-count (event-click-count drag-start)))
611 ;; Occasionally we get spurious drag events where the user hasn't
612 ;; dragged his mouse, but instead Emacs has dragged the text under the
613 ;; user's mouse. Try to recover those cases (bug#17562).
614 (when (and (equal (posn-x-y (event-start click))
615 (posn-x-y (event-end click)))
616 (not (eq (car drag-start) 'mouse-movement)))
617 (setq end beg))
618 (setf (terminal-parameter nil 'mouse-drag-start) nil)))
619 (when (and (integerp beg) (integerp end))
620 (let ((range (mouse-start-end beg end (1- click-count))))
621 (if (< end beg)
622 (setq end (nth 0 range) beg (nth 1 range))
623 (setq beg (nth 0 range) end (nth 1 range)))))
624 (and mouse-drag-copy-region (integerp beg) (integerp end)
625 ;; Don't set this-command to `kill-region', so a following
626 ;; C-w won't double the text in the kill ring. Ignore
627 ;; `last-command' so we don't append to a preceding kill.
628 (let (this-command last-command deactivate-mark)
629 (copy-region-as-kill beg end)))
630 (if (numberp beg) (goto-char beg))
631 ;; On a text terminal, bounce the cursor.
632 (or transient-mark-mode
633 (window-system)
634 (sit-for 1))
635 (push-mark)
636 (set-mark (point))
637 (if (numberp end) (goto-char end))
638 (mouse-set-region-1)))
640 (defun mouse-set-region-1 ()
641 ;; Set transient-mark-mode for a little while.
642 (unless (eq (car-safe transient-mark-mode) 'only)
643 (setq-local transient-mark-mode
644 (cons 'only
645 (unless (eq transient-mark-mode 'lambda)
646 transient-mark-mode))))
647 (setq mouse-last-region-beg (region-beginning))
648 (setq mouse-last-region-end (region-end))
649 (setq mouse-last-region-tick (buffer-modified-tick)))
651 (defcustom mouse-scroll-delay 0.25
652 "The pause between scroll steps caused by mouse drags, in seconds.
653 If you drag the mouse beyond the edge of a window, Emacs scrolls the
654 window to bring the text beyond that edge into view, with a delay of
655 this many seconds between scroll steps. Scrolling stops when you move
656 the mouse back into the window, or release the button.
657 This variable's value may be non-integral.
658 Setting this to zero causes Emacs to scroll as fast as it can."
659 :type 'number
660 :group 'mouse)
662 (defcustom mouse-scroll-min-lines 1
663 "The minimum number of lines scrolled by dragging mouse out of window.
664 Moving the mouse out the top or bottom edge of the window begins
665 scrolling repeatedly. The number of lines scrolled per repetition
666 is normally equal to the number of lines beyond the window edge that
667 the mouse has moved. However, it always scrolls at least the number
668 of lines specified by this variable."
669 :type 'integer
670 :group 'mouse)
672 (defun mouse-scroll-subr (window jump &optional overlay start)
673 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
674 If OVERLAY is an overlay, let it stretch from START to the far edge of
675 the newly visible text.
676 Upon exit, point is at the far edge of the newly visible text."
677 (cond
678 ((and (> jump 0) (< jump mouse-scroll-min-lines))
679 (setq jump mouse-scroll-min-lines))
680 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
681 (setq jump (- mouse-scroll-min-lines))))
682 (let ((opoint (point)))
683 (while (progn
684 (goto-char (window-start window))
685 (if (not (zerop (vertical-motion jump window)))
686 (progn
687 (set-window-start window (point))
688 (if (natnump jump)
689 (if (window-end window)
690 (progn
691 (goto-char (window-end window))
692 ;; window-end doesn't reflect the window's new
693 ;; start position until the next redisplay.
694 (vertical-motion (1- jump) window))
695 (vertical-motion (- (window-height window) 2)))
696 (goto-char (window-start window)))
697 (if overlay
698 (move-overlay overlay start (point)))
699 ;; Now that we have scrolled WINDOW properly,
700 ;; put point back where it was for the redisplay
701 ;; so that we don't mess up the selected window.
702 (or (eq window (selected-window))
703 (goto-char opoint))
704 (sit-for mouse-scroll-delay)))))
705 (or (eq window (selected-window))
706 (goto-char opoint))))
708 (defvar mouse-selection-click-count 0)
710 (defvar mouse-selection-click-count-buffer nil)
712 (defun mouse-drag-region (start-event)
713 "Set the region to the text that the mouse is dragged over.
714 Highlight the drag area as you move the mouse.
715 This must be bound to a button-down mouse event.
716 In Transient Mark mode, the highlighting remains as long as the mark
717 remains active. Otherwise, it remains until the next input event."
718 (interactive "e")
719 ;; Give temporary modes such as isearch a chance to turn off.
720 (run-hooks 'mouse-leave-buffer-hook)
721 (mouse-drag-track start-event))
724 (defun mouse-posn-property (pos property)
725 "Look for a property at click position.
726 POS may be either a buffer position or a click position like
727 those returned from `event-start'. If the click position is on
728 a string, the text property PROPERTY is examined.
729 If this is nil or the click is not on a string, then
730 the corresponding buffer position is searched for PROPERTY.
731 If PROPERTY is encountered in one of those places,
732 its value is returned."
733 (if (consp pos)
734 (let ((w (posn-window pos)) (pt (posn-point pos))
735 (str (posn-string pos)))
736 (or (and str
737 (get-text-property (cdr str) property (car str)))
738 ;; Mouse clicks in the fringe come with a position in
739 ;; (nth 5). This is useful but is not exactly where we clicked, so
740 ;; don't look up that position's properties!
741 (and pt (not (memq (posn-area pos) '(left-fringe right-fringe
742 left-margin right-margin)))
743 (get-char-property pt property w))))
744 (get-char-property pos property)))
746 (defun mouse-on-link-p (pos)
747 "Return non-nil if POS is on a link in the current buffer.
748 POS must specify a buffer position in the current buffer, as a list
749 of the form returned by the `event-start' and `event-end' functions,
750 or a mouse event location in the selected window (see `event-start').
751 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
752 POS may be a mouse event location in any window.
754 A clickable link is identified by one of the following methods:
756 - If the character at POS has a non-nil `follow-link' text or
757 overlay property, the value of that property determines what to do.
759 - If there is a local key-binding or a keybinding at position POS
760 for the `follow-link' event, the binding of that event determines
761 what to do.
763 The resulting value determine whether POS is inside a link:
765 - If the value is `mouse-face', POS is inside a link if there
766 is a non-nil `mouse-face' property at POS. Return t in this case.
768 - If the value is a function, FUNC, POS is inside a link if
769 the call \(FUNC POS) returns non-nil. Return the return value
770 from that call. Arg is \(posn-point POS) if POS is a mouse event.
772 - Otherwise, return the value itself.
774 The return value is interpreted as follows:
776 - If it is a string, the mouse-1 event is translated into the
777 first character of the string, i.e. the action of the mouse-1
778 click is the local or global binding of that character.
780 - If it is a vector, the mouse-1 event is translated into the
781 first element of that vector, i.e. the action of the mouse-1
782 click is the local or global binding of that event.
784 - Otherwise, the mouse-1 event is translated into a mouse-2 event
785 at the same position."
786 (let ((action
787 (and (or (not (consp pos))
788 mouse-1-click-in-non-selected-windows
789 (eq (selected-window) (posn-window pos)))
790 (or (mouse-posn-property pos 'follow-link)
791 (let ((area (posn-area pos)))
792 (when area
793 (key-binding (vector area 'follow-link) nil t pos)))
794 (key-binding [follow-link] nil t pos)))))
795 (cond
796 ((eq action 'mouse-face)
797 (and (mouse-posn-property pos 'mouse-face) t))
798 ((functionp action)
799 ;; FIXME: This seems questionable if the click is not in a buffer.
800 ;; Should we instead decide that `action' takes a `posn'?
801 (if (consp pos)
802 (with-current-buffer (window-buffer (posn-window pos))
803 (funcall action (posn-point pos)))
804 (funcall action pos)))
805 (t action))))
807 (defun mouse-fixup-help-message (msg)
808 "Fix help message MSG for `mouse-1-click-follows-link'."
809 (let (mp pos)
810 (if (and mouse-1-click-follows-link
811 (stringp msg)
812 (string-match-p "\\`mouse-2" msg)
813 (setq mp (mouse-pixel-position))
814 (consp (setq pos (cdr mp)))
815 (car pos) (>= (car pos) 0)
816 (cdr pos) (>= (cdr pos) 0)
817 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
818 (windowp (posn-window pos)))
819 (with-current-buffer (window-buffer (posn-window pos))
820 (if (mouse-on-link-p pos)
821 (setq msg (concat
822 (cond
823 ((eq mouse-1-click-follows-link 'double) "double-")
824 ((and (integerp mouse-1-click-follows-link)
825 (< mouse-1-click-follows-link 0)) "Long ")
826 (t ""))
827 "mouse-1" (substring msg 7)))))))
828 msg)
830 (defun mouse-drag-track (start-event)
831 "Track mouse drags by highlighting area between point and cursor.
832 The region will be defined with mark and point."
833 (mouse-minibuffer-check start-event)
834 (setq mouse-selection-click-count-buffer (current-buffer))
835 (deactivate-mark)
836 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
837 (start-posn (event-start start-event))
838 (start-point (posn-point start-posn))
839 (start-window (posn-window start-posn))
840 (_ (with-current-buffer (window-buffer start-window)
841 (setq deactivate-mark nil)))
842 ;; We've recorded what we needed from the current buffer and
843 ;; window, now let's jump to the place of the event, where things
844 ;; are happening.
845 (_ (mouse-set-point start-event))
846 (echo-keystrokes 0)
847 (bounds (window-edges start-window))
848 (make-cursor-line-fully-visible nil)
849 (top (nth 1 bounds))
850 (bottom (if (window-minibuffer-p start-window)
851 (nth 3 bounds)
852 ;; Don't count the mode line.
853 (1- (nth 3 bounds))))
854 (click-count (1- (event-click-count start-event)))
855 ;; Suppress automatic hscrolling, because that is a nuisance
856 ;; when setting point near the right fringe (but see below).
857 (auto-hscroll-mode-saved auto-hscroll-mode)
858 (old-track-mouse track-mouse))
860 (setq mouse-selection-click-count click-count)
861 ;; In case the down click is in the middle of some intangible text,
862 ;; use the end of that text, and put it in START-POINT.
863 (if (< (point) start-point)
864 (goto-char start-point))
865 (setq start-point (point))
867 ;; Activate the region, using `mouse-start-end' to determine where
868 ;; to put point and mark (e.g., double-click will select a word).
869 (setq-local transient-mark-mode
870 (if (eq transient-mark-mode 'lambda)
871 '(only)
872 (cons 'only transient-mark-mode)))
873 (let ((range (mouse-start-end start-point start-point click-count)))
874 (push-mark (nth 0 range) t t)
875 (goto-char (nth 1 range)))
877 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
878 (setq track-mouse t)
879 (setq auto-hscroll-mode nil)
881 (set-transient-map
882 (let ((map (make-sparse-keymap)))
883 (define-key map [switch-frame] #'ignore)
884 (define-key map [select-window] #'ignore)
885 (define-key map [mouse-movement]
886 (lambda (event) (interactive "e")
887 (let* ((end (event-end event))
888 (end-point (posn-point end)))
889 (unless (eq end-point start-point)
890 ;; As soon as the user moves, we can re-enable auto-hscroll.
891 (setq auto-hscroll-mode auto-hscroll-mode-saved)
892 ;; And remember that we have moved, so mouse-set-region can know
893 ;; its event is really a drag event.
894 (setcar start-event 'mouse-movement))
895 (if (and (eq (posn-window end) start-window)
896 (integer-or-marker-p end-point))
897 (mouse--drag-set-mark-and-point start-point
898 end-point click-count)
899 (let ((mouse-row (cdr (cdr (mouse-position)))))
900 (cond
901 ((null mouse-row))
902 ((< mouse-row top)
903 (mouse-scroll-subr start-window (- mouse-row top)
904 nil start-point))
905 ((>= mouse-row bottom)
906 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
907 nil start-point))))))))
908 map)
909 t (lambda ()
910 (setq track-mouse old-track-mouse)
911 (setq auto-hscroll-mode auto-hscroll-mode-saved)
912 (deactivate-mark)
913 (pop-mark)))))
915 (defun mouse--drag-set-mark-and-point (start click click-count)
916 (let* ((range (mouse-start-end start click click-count))
917 (beg (nth 0 range))
918 (end (nth 1 range)))
919 (cond ((eq (mark) beg)
920 (goto-char end))
921 ((eq (mark) end)
922 (goto-char beg))
923 ((< click (mark))
924 (set-mark end)
925 (goto-char beg))
927 (set-mark beg)
928 (goto-char end)))))
930 ;; Commands to handle xterm-style multiple clicks.
931 (defun mouse-skip-word (dir)
932 "Skip over word, over whitespace, or over identical punctuation.
933 If DIR is positive skip forward; if negative, skip backward."
934 (let* ((char (following-char))
935 (syntax (char-to-string (char-syntax char))))
936 (cond ((string= syntax "w")
937 ;; Here, we can't use skip-syntax-forward/backward because
938 ;; they don't pay attention to word-separating-categories,
939 ;; and thus they will skip over a true word boundary. So,
940 ;; we simulate the original behavior by using forward-word.
941 (if (< dir 0)
942 (if (not (looking-at "\\<"))
943 (forward-word -1))
944 (if (or (looking-at "\\<") (not (looking-at "\\>")))
945 (forward-word 1))))
946 ((string= syntax " ")
947 (if (< dir 0)
948 (skip-syntax-backward syntax)
949 (skip-syntax-forward syntax)))
950 ((string= syntax "_")
951 (if (< dir 0)
952 (skip-syntax-backward "w_")
953 (skip-syntax-forward "w_")))
954 ((< dir 0)
955 (while (and (not (bobp)) (= (preceding-char) char))
956 (forward-char -1)))
958 (while (and (not (eobp)) (= (following-char) char))
959 (forward-char 1))))))
961 (defun mouse-start-end (start end mode)
962 "Return a list of region bounds based on START and END according to MODE.
963 If MODE is 0 then set point to (min START END), mark to (max START END).
964 If MODE is 1 then set point to start of word at (min START END),
965 mark to end of word at (max START END).
966 If MODE is 2 then do the same for lines."
967 (if (> start end)
968 (let ((temp start))
969 (setq start end
970 end temp)))
971 (setq mode (mod mode 3))
972 (cond ((= mode 0)
973 (list start end))
974 ((and (= mode 1)
975 (= start end)
976 (char-after start)
977 (= (char-syntax (char-after start)) ?\())
978 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
979 ;; This happens in CC Mode when unbalanced parens in CPP
980 ;; constructs are given punctuation syntax with
981 ;; syntax-table text properties. (2016-02-21).
982 (signal 'scan-error (list "Containing expression ends prematurely"
983 start start))
984 (list start
985 (save-excursion
986 (goto-char start)
987 (forward-sexp 1)
988 (point)))))
989 ((and (= mode 1)
990 (= start end)
991 (char-after start)
992 (= (char-syntax (char-after start)) ?\)))
993 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
994 ;; See above comment about CC Mode.
995 (signal 'scan-error (list "Unbalanced parentheses" start start))
996 (list (save-excursion
997 (goto-char (1+ start))
998 (backward-sexp 1)
999 (point))
1000 (1+ start))))
1001 ((and (= mode 1)
1002 (= start end)
1003 (char-after start)
1004 (= (char-syntax (char-after start)) ?\"))
1005 (let ((open (or (eq start (point-min))
1006 (save-excursion
1007 (goto-char (- start 1))
1008 (looking-at "\\s(\\|\\s \\|\\s>")))))
1009 (if open
1010 (list start
1011 (save-excursion
1012 (condition-case nil
1013 (progn
1014 (goto-char start)
1015 (forward-sexp 1)
1016 (point))
1017 (error end))))
1018 (list (save-excursion
1019 (condition-case nil
1020 (progn
1021 (goto-char (1+ start))
1022 (backward-sexp 1)
1023 (point))
1024 (error end)))
1025 (1+ start)))))
1026 ((= mode 1)
1027 (list (save-excursion
1028 (goto-char start)
1029 (mouse-skip-word -1)
1030 (point))
1031 (save-excursion
1032 (goto-char end)
1033 (mouse-skip-word 1)
1034 (point))))
1035 ((= mode 2)
1036 (list (save-excursion
1037 (goto-char start)
1038 (line-beginning-position 1))
1039 (save-excursion
1040 (goto-char end)
1041 (forward-line 1)
1042 (point))))))
1044 ;; Subroutine: set the mark where CLICK happened,
1045 ;; but don't do anything else.
1046 (defun mouse-set-mark-fast (click)
1047 (mouse-minibuffer-check click)
1048 (let ((posn (event-start click)))
1049 (select-window (posn-window posn))
1050 (if (numberp (posn-point posn))
1051 (push-mark (posn-point posn) t t))))
1053 (defun mouse-undouble-last-event (events)
1054 (let* ((index (1- (length events)))
1055 (last (nthcdr index events))
1056 (event (car last))
1057 (basic (event-basic-type event))
1058 (old-modifiers (event-modifiers event))
1059 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1060 (new
1061 (if (consp event)
1062 ;; Use reverse, not nreverse, since event-modifiers
1063 ;; does not copy the list it returns.
1064 (cons (event-convert-list (reverse (cons basic modifiers)))
1065 (cdr event))
1066 event)))
1067 (setcar last new)
1068 (if (and (not (equal modifiers old-modifiers))
1069 (key-binding (apply 'vector events)))
1071 (setcar last event)
1072 nil)))
1074 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1076 (defun mouse-set-mark (click)
1077 "Set mark at the position clicked on with the mouse.
1078 Display cursor at that position for a second.
1079 This must be bound to a mouse click."
1080 (interactive "e")
1081 (mouse-minibuffer-check click)
1082 (select-window (posn-window (event-start click)))
1083 ;; FIXME: Use save-excursion
1084 (let ((point-save (point)))
1085 (unwind-protect
1086 (progn (mouse-set-point click)
1087 (push-mark nil t t)
1088 (or transient-mark-mode
1089 (sit-for 1)))
1090 (goto-char point-save))))
1092 (defun mouse-kill (click)
1093 "Kill the region between point and the mouse click.
1094 The text is saved in the kill ring, as with \\[kill-region]."
1095 (interactive "e")
1096 (mouse-minibuffer-check click)
1097 (let* ((posn (event-start click))
1098 (click-posn (posn-point posn)))
1099 (select-window (posn-window posn))
1100 (if (numberp click-posn)
1101 (kill-region (min (point) click-posn)
1102 (max (point) click-posn)))))
1104 (defun mouse-yank-at-click (click arg)
1105 "Insert the last stretch of killed text at the position clicked on.
1106 Also move point to one end of the text thus inserted (normally the end),
1107 and set mark at the beginning.
1108 Prefix arguments are interpreted as with \\[yank].
1109 If `mouse-yank-at-point' is non-nil, insert at point
1110 regardless of where you click."
1111 (interactive "e\nP")
1112 ;; Give temporary modes such as isearch a chance to turn off.
1113 (run-hooks 'mouse-leave-buffer-hook)
1114 (when select-active-regions
1115 ;; Without this, confusing things happen upon e.g. inserting into
1116 ;; the middle of an active region.
1117 (deactivate-mark))
1118 (or mouse-yank-at-point (mouse-set-point click))
1119 (setq this-command 'yank)
1120 (setq mouse-selection-click-count 0)
1121 (yank arg))
1123 (defun mouse-yank-primary (click)
1124 "Insert the primary selection at the position clicked on.
1125 Move point to the end of the inserted text, and set mark at
1126 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1127 regardless of where you click."
1128 (interactive "e")
1129 ;; Give temporary modes such as isearch a chance to turn off.
1130 (run-hooks 'mouse-leave-buffer-hook)
1131 ;; Without this, confusing things happen upon e.g. inserting into
1132 ;; the middle of an active region.
1133 (when select-active-regions
1134 (let (select-active-regions)
1135 (deactivate-mark)))
1136 (or mouse-yank-at-point (mouse-set-point click))
1137 (let ((primary (gui-get-primary-selection)))
1138 (push-mark (point))
1139 (insert-for-yank primary)))
1141 (defun mouse-kill-ring-save (click)
1142 "Copy the region between point and the mouse click in the kill ring.
1143 This does not delete the region; it acts like \\[kill-ring-save]."
1144 (interactive "e")
1145 (mouse-set-mark-fast click)
1146 (let (this-command last-command)
1147 (kill-ring-save (point) (mark t))))
1149 ;; This function used to delete the text between point and the mouse
1150 ;; whenever it was equal to the front of the kill ring, but some
1151 ;; people found that confusing.
1153 ;; The position of the last invocation of `mouse-save-then-kill'.
1154 (defvar mouse-save-then-kill-posn nil)
1156 (defun mouse-save-then-kill-delete-region (beg end)
1157 ;; We must make our own undo boundaries
1158 ;; because they happen automatically only for the current buffer.
1159 (undo-boundary)
1160 (if (or (= beg end) (eq buffer-undo-list t))
1161 ;; If we have no undo list in this buffer,
1162 ;; just delete.
1163 (delete-region beg end)
1164 ;; Delete, but make the undo-list entry share with the kill ring.
1165 ;; First, delete just one char, so in case buffer is being modified
1166 ;; for the first time, the undo list records that fact.
1167 (let ((inhibit-modification-hooks t))
1168 (delete-region beg
1169 (+ beg (if (> end beg) 1 -1))))
1170 (let ((buffer-undo-list buffer-undo-list))
1171 ;; Undo that deletion--but don't change the undo list!
1172 (let ((inhibit-modification-hooks t))
1173 (primitive-undo 1 buffer-undo-list))
1174 ;; Now delete the rest of the specified region,
1175 ;; but don't record it.
1176 (setq buffer-undo-list t)
1177 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1178 (error "Lossage in mouse-save-then-kill-delete-region"))
1179 (delete-region beg end))
1180 (let ((tail buffer-undo-list))
1181 ;; Search back in buffer-undo-list for the string
1182 ;; that came from deleting one character.
1183 (while (and tail (not (stringp (car (car tail)))))
1184 (setq tail (cdr tail)))
1185 ;; Replace it with an entry for the entire deleted text.
1186 (and tail
1187 (setcar tail (cons (car kill-ring) (min beg end))))))
1188 (undo-boundary))
1190 (defun mouse-save-then-kill (click)
1191 "Set the region according to CLICK; the second time, kill it.
1192 CLICK should be a mouse click event.
1194 If the region is inactive, activate it temporarily. Set mark at
1195 the original point, and move point to the position of CLICK.
1197 If the region is already active, adjust it. Normally, do this by
1198 moving point or mark, whichever is closer, to CLICK. But if you
1199 have selected whole words or lines, move point or mark to the
1200 word or line boundary closest to CLICK instead.
1202 If `mouse-drag-copy-region' is non-nil, this command also saves the
1203 new region to the kill ring (replacing the previous kill if the
1204 previous region was just saved to the kill ring).
1206 If this command is called a second consecutive time with the same
1207 CLICK position, kill the region (or delete it
1208 if `mouse-drag-copy-region' is non-nil)"
1209 (interactive "e")
1210 (mouse-minibuffer-check click)
1211 (let* ((posn (event-start click))
1212 (click-pt (posn-point posn))
1213 (window (posn-window posn))
1214 (buf (window-buffer window))
1215 ;; Don't let a subsequent kill command append to this one.
1216 (this-command this-command)
1217 ;; Check if the user has multi-clicked to select words/lines.
1218 (click-count
1219 (if (and (eq mouse-selection-click-count-buffer buf)
1220 (with-current-buffer buf (mark t)))
1221 mouse-selection-click-count
1222 0)))
1223 (cond
1224 ((not (numberp click-pt)) nil)
1225 ;; If the user clicked without moving point, kill the region.
1226 ;; This also resets `mouse-selection-click-count'.
1227 ((and (eq last-command 'mouse-save-then-kill)
1228 (eq click-pt mouse-save-then-kill-posn)
1229 (eq window (selected-window)))
1230 (if mouse-drag-copy-region
1231 ;; Region already saved in the previous click;
1232 ;; don't make a duplicate entry, just delete.
1233 (delete-region (mark t) (point))
1234 (kill-region (mark t) (point)))
1235 (setq mouse-selection-click-count 0)
1236 (setq mouse-save-then-kill-posn nil))
1238 ;; Otherwise, if there is a suitable region, adjust it by moving
1239 ;; one end (whichever is closer) to CLICK-PT.
1240 ((or (with-current-buffer buf (region-active-p))
1241 (and (eq window (selected-window))
1242 (mark t)
1243 (or (and (eq last-command 'mouse-save-then-kill)
1244 mouse-save-then-kill-posn)
1245 (and (memq last-command '(mouse-drag-region
1246 mouse-set-region))
1247 (or mark-even-if-inactive
1248 (not transient-mark-mode))))))
1249 (select-window window)
1250 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1251 (if (< (abs (- click-pt (mark t)))
1252 (abs (- click-pt (point))))
1253 (set-mark (car range))
1254 (goto-char (nth 1 range)))
1255 (setq deactivate-mark nil)
1256 (mouse-set-region-1)
1257 (when mouse-drag-copy-region
1258 ;; Region already copied to kill-ring once, so replace.
1259 (kill-new (filter-buffer-substring (mark t) (point)) t))
1260 ;; Arrange for a repeated mouse-3 to kill the region.
1261 (setq mouse-save-then-kill-posn click-pt)))
1263 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1265 (select-window window)
1266 (mouse-set-mark-fast click)
1267 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1268 (if before-scroll (goto-char before-scroll)))
1269 (exchange-point-and-mark)
1270 (mouse-set-region-1)
1271 (when mouse-drag-copy-region
1272 (kill-new (filter-buffer-substring (mark t) (point))))
1273 (setq mouse-save-then-kill-posn click-pt)))))
1276 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1277 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1278 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1279 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1280 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1282 (defconst mouse-secondary-overlay
1283 (let ((ol (make-overlay (point-min) (point-min))))
1284 (delete-overlay ol)
1285 (overlay-put ol 'face 'secondary-selection)
1287 "An overlay which records the current secondary selection.
1288 It is deleted when there is no secondary selection.")
1290 (defvar mouse-secondary-click-count 0)
1292 ;; A marker which records the specified first end for a secondary selection.
1293 ;; May be nil.
1294 (defvar mouse-secondary-start nil)
1296 (defun mouse-start-secondary (click)
1297 "Set one end of the secondary selection to the position clicked on.
1298 Use \\[mouse-secondary-save-then-kill] to set the other end
1299 and complete the secondary selection."
1300 (interactive "e")
1301 (mouse-minibuffer-check click)
1302 (let ((posn (event-start click)))
1303 (with-current-buffer (window-buffer (posn-window posn))
1304 ;; Cancel any preexisting secondary selection.
1305 (delete-overlay mouse-secondary-overlay)
1306 (if (numberp (posn-point posn))
1307 (progn
1308 (or mouse-secondary-start
1309 (setq mouse-secondary-start (make-marker)))
1310 (move-marker mouse-secondary-start (posn-point posn)))))))
1312 (defun mouse-set-secondary (click)
1313 "Set the secondary selection to the text that the mouse is dragged over.
1314 This must be bound to a mouse drag event."
1315 (interactive "e")
1316 (mouse-minibuffer-check click)
1317 (let ((posn (event-start click))
1319 (end (event-end click)))
1320 (with-current-buffer (window-buffer (posn-window posn))
1321 (if (numberp (posn-point posn))
1322 (setq beg (posn-point posn)))
1323 (move-overlay mouse-secondary-overlay beg (posn-point end))
1324 (gui-set-selection
1325 'SECONDARY
1326 (buffer-substring (overlay-start mouse-secondary-overlay)
1327 (overlay-end mouse-secondary-overlay))))))
1329 (defun mouse-drag-secondary (start-event)
1330 "Set the secondary selection to the text that the mouse is dragged over.
1331 Highlight the drag area as you move the mouse.
1332 This must be bound to a button-down mouse event.
1333 The function returns a non-nil value if it creates a secondary selection."
1334 (interactive "e")
1335 (mouse-minibuffer-check start-event)
1336 (let* ((echo-keystrokes 0)
1337 (start-posn (event-start start-event))
1338 (start-point (posn-point start-posn))
1339 (start-window (posn-window start-posn))
1340 (bounds (window-edges start-window))
1341 (top (nth 1 bounds))
1342 (bottom (if (window-minibuffer-p start-window)
1343 (nth 3 bounds)
1344 ;; Don't count the mode line.
1345 (1- (nth 3 bounds))))
1346 (click-count (1- (event-click-count start-event))))
1347 (with-current-buffer (window-buffer start-window)
1348 (setq mouse-secondary-click-count click-count)
1349 (if (> (mod click-count 3) 0)
1350 ;; Double or triple press: make an initial selection
1351 ;; of one word or line.
1352 (let ((range (mouse-start-end start-point start-point click-count)))
1353 (set-marker mouse-secondary-start nil)
1354 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1355 (window-buffer start-window)))
1356 ;; Single-press: cancel any preexisting secondary selection.
1357 (or mouse-secondary-start
1358 (setq mouse-secondary-start (make-marker)))
1359 (set-marker mouse-secondary-start start-point)
1360 (delete-overlay mouse-secondary-overlay))
1361 ;; FIXME: Use mouse-drag-track!
1362 (let (event end end-point)
1363 (track-mouse
1364 (while (progn
1365 (setq event (read-event))
1366 (or (mouse-movement-p event)
1367 (memq (car-safe event) '(switch-frame select-window))))
1369 (if (memq (car-safe event) '(switch-frame select-window))
1371 (setq end (event-end event)
1372 end-point (posn-point end))
1373 (cond
1374 ;; Are we moving within the original window?
1375 ((and (eq (posn-window end) start-window)
1376 (integer-or-marker-p end-point))
1377 (let ((range (mouse-start-end start-point end-point
1378 click-count)))
1379 (if (or (/= start-point end-point)
1380 (null (marker-position mouse-secondary-start)))
1381 (progn
1382 (set-marker mouse-secondary-start nil)
1383 (move-overlay mouse-secondary-overlay
1384 (car range) (nth 1 range))))))
1386 (let ((mouse-row (cdr (cdr (mouse-position)))))
1387 (cond
1388 ((null mouse-row))
1389 ((< mouse-row top)
1390 (mouse-scroll-subr start-window (- mouse-row top)
1391 mouse-secondary-overlay start-point))
1392 ((>= mouse-row bottom)
1393 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1394 mouse-secondary-overlay start-point)))))))))
1396 (if (consp event)
1397 (if (marker-position mouse-secondary-start)
1398 (save-window-excursion
1399 (delete-overlay mouse-secondary-overlay)
1400 (gui-set-selection 'SECONDARY nil)
1401 (select-window start-window)
1402 (save-excursion
1403 (goto-char mouse-secondary-start)
1404 (sit-for 1)
1405 nil))
1406 (gui-set-selection
1407 'SECONDARY
1408 (buffer-substring (overlay-start mouse-secondary-overlay)
1409 (overlay-end mouse-secondary-overlay)))))))))
1411 (defun mouse-yank-secondary (click)
1412 "Insert the secondary selection at the position clicked on.
1413 Move point to the end of the inserted text.
1414 If `mouse-yank-at-point' is non-nil, insert at point
1415 regardless of where you click."
1416 (interactive "e")
1417 ;; Give temporary modes such as isearch a chance to turn off.
1418 (run-hooks 'mouse-leave-buffer-hook)
1419 (or mouse-yank-at-point (mouse-set-point click))
1420 (let ((secondary (gui-get-selection 'SECONDARY)))
1421 (if secondary
1422 (insert-for-yank secondary)
1423 (error "No secondary selection"))))
1425 (defun mouse-kill-secondary ()
1426 "Kill the text in the secondary selection.
1427 This is intended more as a keyboard command than as a mouse command
1428 but it can work as either one.
1430 The current buffer (in case of keyboard use), or the buffer clicked on,
1431 must be the one that the secondary selection is in. This requirement
1432 is to prevent accidents."
1433 (interactive)
1434 (let* ((keys (this-command-keys))
1435 (click (elt keys (1- (length keys)))))
1436 (or (eq (overlay-buffer mouse-secondary-overlay)
1437 (if (listp click)
1438 (window-buffer (posn-window (event-start click)))
1439 (current-buffer)))
1440 (error "Select or click on the buffer where the secondary selection is")))
1441 (let (this-command)
1442 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1443 (kill-region (overlay-start mouse-secondary-overlay)
1444 (overlay-end mouse-secondary-overlay))))
1445 (delete-overlay mouse-secondary-overlay))
1447 (defun mouse-secondary-save-then-kill (click)
1448 "Set the secondary selection and save it to the kill ring.
1449 The second time, kill it. CLICK should be a mouse click event.
1451 If you have not called `mouse-start-secondary' in the clicked
1452 buffer, activate the secondary selection and set it between point
1453 and the click position CLICK.
1455 Otherwise, adjust the bounds of the secondary selection.
1456 Normally, do this by moving its beginning or end, whichever is
1457 closer, to CLICK. But if you have selected whole words or lines,
1458 adjust to the word or line boundary closest to CLICK instead.
1460 If this command is called a second consecutive time with the same
1461 CLICK position, kill the secondary selection."
1462 (interactive "e")
1463 (mouse-minibuffer-check click)
1464 (let* ((posn (event-start click))
1465 (click-pt (posn-point posn))
1466 (window (posn-window posn))
1467 (buf (window-buffer window))
1468 ;; Don't let a subsequent kill command append to this one.
1469 (this-command this-command)
1470 ;; Check if the user has multi-clicked to select words/lines.
1471 (click-count
1472 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1473 mouse-secondary-click-count
1475 (beg (overlay-start mouse-secondary-overlay))
1476 (end (overlay-end mouse-secondary-overlay)))
1478 (cond
1479 ((not (numberp click-pt)) nil)
1481 ;; If the secondary selection is not active in BUF, activate it.
1482 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1483 (if mouse-secondary-start
1484 (marker-buffer mouse-secondary-start)))))
1485 (select-window window)
1486 (setq mouse-secondary-start (make-marker))
1487 (move-marker mouse-secondary-start (point))
1488 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1489 (kill-ring-save (point) click-pt))
1491 ;; If the user clicked without moving point, delete the secondary
1492 ;; selection. This also resets `mouse-secondary-click-count'.
1493 ((and (eq last-command 'mouse-secondary-save-then-kill)
1494 (eq click-pt mouse-save-then-kill-posn)
1495 (eq window (selected-window)))
1496 (mouse-save-then-kill-delete-region beg end)
1497 (delete-overlay mouse-secondary-overlay)
1498 (setq mouse-secondary-click-count 0)
1499 (setq mouse-save-then-kill-posn nil))
1501 ;; Otherwise, if there is a suitable secondary selection overlay,
1502 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1503 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1504 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1505 (if (< (abs (- click-pt beg))
1506 (abs (- click-pt end)))
1507 (move-overlay mouse-secondary-overlay (car range) end)
1508 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1509 (setq deactivate-mark nil)
1510 (if (eq last-command 'mouse-secondary-save-then-kill)
1511 ;; If the front of the kill ring comes from an immediately
1512 ;; previous use of this command, replace the entry.
1513 (kill-new
1514 (buffer-substring (overlay-start mouse-secondary-overlay)
1515 (overlay-end mouse-secondary-overlay))
1517 (let (deactivate-mark)
1518 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1519 (overlay-end mouse-secondary-overlay))))
1520 (setq mouse-save-then-kill-posn click-pt))
1522 ;; Otherwise, set the secondary selection overlay.
1524 (select-window window)
1525 (if mouse-secondary-start
1526 ;; All we have is one end of a selection, so put the other
1527 ;; end here.
1528 (let ((start (+ 0 mouse-secondary-start)))
1529 (kill-ring-save start click-pt)
1530 (move-overlay mouse-secondary-overlay start click-pt)))
1531 (setq mouse-save-then-kill-posn click-pt))))
1533 ;; Finally, set the window system's secondary selection.
1534 (let (str)
1535 (and (overlay-buffer mouse-secondary-overlay)
1536 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1537 (overlay-end mouse-secondary-overlay)))
1538 (> (length str) 0)
1539 (gui-set-selection 'SECONDARY str))))
1542 (defcustom mouse-buffer-menu-maxlen 20
1543 "Number of buffers in one pane (submenu) of the buffer menu.
1544 If we have lots of buffers, divide them into groups of
1545 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1546 :type 'integer
1547 :group 'mouse)
1549 (defcustom mouse-buffer-menu-mode-mult 4
1550 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1551 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1552 will split the buffer menu by the major modes (see
1553 `mouse-buffer-menu-mode-groups') or just by menu length.
1554 Set to 1 (or even 0!) if you want to group by major mode always, and to
1555 a large number if you prefer a mixed multitude. The default is 4."
1556 :type 'integer
1557 :group 'mouse
1558 :version "20.3")
1560 (defvar mouse-buffer-menu-mode-groups
1561 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1562 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1563 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1564 . "Mail/News")
1565 ("\\<C\\>" . "C")
1566 ("ObjC" . "C")
1567 ("Text" . "Text")
1568 ("Outline" . "Text")
1569 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1570 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1571 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1572 . "GDB")
1573 ("Lisp" . "Lisp")))
1574 "How to group various major modes together in \\[mouse-buffer-menu].
1575 Each element has the form (REGEXP . GROUPNAME).
1576 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1578 (defun mouse-buffer-menu (event)
1579 "Pop up a menu of buffers for selection with the mouse.
1580 This switches buffers in the window that you clicked on,
1581 and selects that window."
1582 (interactive "e")
1583 (mouse-minibuffer-check event)
1584 (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1585 (window (posn-window (event-start event))))
1586 (when buf
1587 (select-window
1588 (if (framep window) (frame-selected-window window)
1589 window))
1590 (switch-to-buffer buf))))
1592 (defun mouse-buffer-menu-map ()
1593 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1594 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
1595 (dolist (buf buffers)
1596 ;; Divide all buffers into buckets for various major modes.
1597 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1598 (with-current-buffer buf
1599 (let* ((adjusted-major-mode major-mode) elt)
1600 (dolist (group mouse-buffer-menu-mode-groups)
1601 (when (string-match (car group) (format-mode-line mode-name))
1602 (setq adjusted-major-mode (cdr group))))
1603 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1604 (unless elt
1605 (setq elt (list adjusted-major-mode
1606 (if (stringp adjusted-major-mode)
1607 adjusted-major-mode
1608 (format-mode-line mode-name nil nil buf)))
1609 split-by-major-mode (cons elt split-by-major-mode)))
1610 (or (memq buf (cdr (cdr elt)))
1611 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1612 ;; Compute the sum of squares of sizes of the major-mode buckets.
1613 (let ((tail split-by-major-mode))
1614 (setq sum-of-squares 0)
1615 (while tail
1616 (setq sum-of-squares
1617 (+ sum-of-squares
1618 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1619 (setq tail (cdr tail))))
1620 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1621 (* (length buffers) (length buffers)))
1622 ;; Subdividing by major modes really helps, so let's do it.
1623 (let (subdivided-menus (buffers-left (length buffers)))
1624 ;; Sort the list to put the most popular major modes first.
1625 (setq split-by-major-mode
1626 (sort split-by-major-mode
1627 (function (lambda (elt1 elt2)
1628 (> (length elt1) (length elt2))))))
1629 ;; Make a separate submenu for each major mode
1630 ;; that has more than one buffer,
1631 ;; unless all the remaining buffers are less than 1/10 of them.
1632 (while (and split-by-major-mode
1633 (and (> (length (car split-by-major-mode)) 3)
1634 (> (* buffers-left 10) (length buffers))))
1635 (let ((this-mode-list (mouse-buffer-menu-alist
1636 (cdr (cdr (car split-by-major-mode))))))
1637 (and this-mode-list
1638 (setq subdivided-menus
1639 (cons (cons
1640 (nth 1 (car split-by-major-mode))
1641 this-mode-list)
1642 subdivided-menus))))
1643 (setq buffers-left
1644 (- buffers-left (length (cdr (car split-by-major-mode)))))
1645 (setq split-by-major-mode (cdr split-by-major-mode)))
1646 ;; If any major modes are left over,
1647 ;; make a single submenu for them.
1648 (if split-by-major-mode
1649 (let ((others-list
1650 (mouse-buffer-menu-alist
1651 ;; we don't need split-by-major-mode any more,
1652 ;; so we can ditch it with nconc (mapcan).
1653 (mapcan 'cddr split-by-major-mode))))
1654 (and others-list
1655 (setq subdivided-menus
1656 (cons (cons "Others" others-list)
1657 subdivided-menus)))))
1658 (cons "Buffer Menu" (nreverse subdivided-menus)))
1659 (cons "Buffer Menu"
1660 (mouse-buffer-menu-split "Select Buffer"
1661 (mouse-buffer-menu-alist buffers))))))
1663 (defun mouse-buffer-menu-alist (buffers)
1664 (let (tail
1665 (maxlen 0)
1666 head)
1667 (setq buffers
1668 (sort buffers
1669 (function (lambda (elt1 elt2)
1670 (string< (buffer-name elt1) (buffer-name elt2))))))
1671 (setq tail buffers)
1672 (while tail
1673 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1674 (setq maxlen
1675 (max maxlen
1676 (length (buffer-name (car tail))))))
1677 (setq tail (cdr tail)))
1678 (setq tail buffers)
1679 (while tail
1680 (let ((elt (car tail)))
1681 (if (/= (aref (buffer-name elt) 0) ?\s)
1682 (setq head
1683 (cons
1684 (cons
1685 (format
1686 (format "%%-%ds %%s%%s %%s" maxlen)
1687 (buffer-name elt)
1688 (if (buffer-modified-p elt) "*" " ")
1689 (with-current-buffer elt
1690 (if buffer-read-only "%" " "))
1691 (or (buffer-file-name elt)
1692 (with-current-buffer elt
1693 (if list-buffers-directory
1694 (expand-file-name
1695 list-buffers-directory)))
1696 ""))
1697 elt)
1698 head))))
1699 (setq tail (cdr tail)))
1700 ;; Compensate for the reversal that the above loop does.
1701 (nreverse head)))
1703 (defun mouse-buffer-menu-split (title alist)
1704 ;; If we have lots of buffers, divide them into groups of 20
1705 ;; and make a pane (or submenu) for each one.
1706 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1707 (let ((alist alist) sublists next
1708 (i 1))
1709 (while alist
1710 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1711 ;; and make them the next element of sublist.
1712 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1713 (if next
1714 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1715 nil))
1716 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1717 sublists))
1718 (setq i (1+ i))
1719 (setq alist next))
1720 (nreverse sublists))
1721 ;; Few buffers--put them all in one pane.
1722 (list (cons title alist))))
1724 (define-obsolete-function-alias
1725 'mouse-choose-completion 'choose-completion "23.2")
1727 ;; Font selection.
1729 (defun font-menu-add-default ()
1730 (let* ((default (frame-parameter nil 'font))
1731 (font-alist x-fixed-font-alist)
1732 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1733 (if (assoc "Default" elt)
1734 (delete (assoc "Default" elt) elt))
1735 (setcdr elt
1736 (cons (list "Default" default)
1737 (cdr elt)))))
1739 (defvar x-fixed-font-alist
1740 (list
1741 (purecopy "Font Menu")
1742 (cons
1743 (purecopy "Misc")
1744 (mapcar
1745 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1746 ;; For these, we specify the pixel height and width.
1747 '(("fixed" "fixed")
1748 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1749 ("6x12"
1750 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1751 ("6x13"
1752 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1753 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1754 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1755 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1756 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1757 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1758 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1759 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1760 ("")
1761 ("clean 5x8"
1762 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1763 ("clean 6x8"
1764 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1765 ("clean 8x8"
1766 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1767 ("clean 8x10"
1768 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1769 ("clean 8x14"
1770 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1771 ("clean 8x16"
1772 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1773 ("")
1774 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1775 ;; We don't seem to have these; who knows what they are.
1776 ;; ("fg-18" "fg-18")
1777 ;; ("fg-25" "fg-25")
1778 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1779 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1780 ("lucidasanstypewriter-bold-24"
1781 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
1782 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1783 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1786 (cons
1787 (purecopy "Courier")
1788 (mapcar
1789 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1790 ;; For these, we specify the point height.
1791 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1792 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1793 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1794 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1795 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1796 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1797 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1798 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1799 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1800 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1801 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1802 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1803 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1804 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1805 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1806 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1807 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1808 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1809 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1810 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1811 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1812 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1813 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1814 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1815 ))))
1816 "X fonts suitable for use in Emacs.")
1818 (declare-function generate-fontset-menu "fontset" ())
1820 (defun mouse-select-font ()
1821 "Prompt for a font name, using `x-popup-menu', and return it."
1822 (interactive)
1823 (unless (display-multi-font-p)
1824 (error "Cannot change fonts on this display"))
1825 (car
1826 (x-popup-menu
1827 (if (listp last-nonmenu-event)
1828 last-nonmenu-event
1829 (list '(0 0) (selected-window)))
1830 (append x-fixed-font-alist
1831 (list (generate-fontset-menu))))))
1833 (declare-function text-scale-mode "face-remap")
1835 (defun mouse-set-font (&rest fonts)
1836 "Set the default font for the selected frame.
1837 The argument FONTS is a list of font names; the first valid font
1838 in this list is used.
1840 When called interactively, pop up a menu and allow the user to
1841 choose a font."
1842 (interactive
1843 (progn (unless (display-multi-font-p)
1844 (error "Cannot change fonts on this display"))
1845 (x-popup-menu
1846 (if (listp last-nonmenu-event)
1847 last-nonmenu-event
1848 (list '(0 0) (selected-window)))
1849 ;; Append list of fontsets currently defined.
1850 (append x-fixed-font-alist (list (generate-fontset-menu))))))
1851 (if fonts
1852 (let (font)
1853 (while fonts
1854 (condition-case nil
1855 (progn
1856 (set-frame-font (car fonts))
1857 (setq font (car fonts))
1858 (setq fonts nil))
1859 (error
1860 (setq fonts (cdr fonts)))))
1861 (if (null font)
1862 (error "Font not found")))))
1864 (defvar mouse-appearance-menu-map nil)
1865 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
1866 (declare-function buffer-face-mode-invoke "face-remap"
1867 (face arg &optional interactive))
1868 (declare-function font-face-attributes "font.c" (font &optional frame))
1869 (defvar w32-use-w32-font-dialog)
1870 (defvar w32-fixed-font-alist)
1872 (defun mouse-appearance-menu (event)
1873 "Show a menu for changing the default face in the current buffer."
1874 (interactive "@e")
1875 (require 'face-remap)
1876 (when (display-multi-font-p)
1877 (with-selected-window (car (event-start event))
1878 (if mouse-appearance-menu-map
1879 nil ; regenerate new fonts
1880 ;; Initialize mouse-appearance-menu-map
1881 (setq mouse-appearance-menu-map
1882 (make-sparse-keymap "Change Default Buffer Face"))
1883 (define-key mouse-appearance-menu-map [face-remap-reset-base]
1884 '(menu-item "Reset to Default" face-remap-reset-base))
1885 (define-key mouse-appearance-menu-map [text-scale-decrease]
1886 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
1887 (define-key mouse-appearance-menu-map [text-scale-increase]
1888 '(menu-item "Increase Buffer Text Size" text-scale-increase))
1889 ;; Font selector
1890 (if (and (functionp 'x-select-font)
1891 (or (not (boundp 'w32-use-w32-font-dialog))
1892 w32-use-w32-font-dialog))
1893 (define-key mouse-appearance-menu-map [x-select-font]
1894 '(menu-item "Change Buffer Font..." x-select-font))
1895 ;; If the select-font is unavailable, construct a menu.
1896 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
1897 (font-alist (cdr (append
1898 (if (eq system-type 'windows-nt)
1899 w32-fixed-font-alist
1900 x-fixed-font-alist)
1901 (list (generate-fontset-menu))))))
1902 (dolist (family font-alist)
1903 (let* ((submenu-name (car family))
1904 (submenu-map (make-sparse-keymap submenu-name)))
1905 (dolist (font (cdr family))
1906 (let ((font-name (car font))
1907 font-symbol)
1908 (if (string= font-name "")
1909 (define-key submenu-map [space]
1910 '("--"))
1911 (setq font-symbol (intern (cadr font)))
1912 (define-key submenu-map (vector font-symbol)
1913 (list 'menu-item (car font) font-symbol)))))
1914 (define-key font-submenu (vector (intern submenu-name))
1915 (list 'menu-item submenu-name submenu-map))))
1916 (define-key mouse-appearance-menu-map [font-submenu]
1917 (list 'menu-item "Change Text Font" font-submenu)))))
1918 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
1919 (setq choice (nth (1- (length choice)) choice))
1920 (cond ((eq choice 'text-scale-increase)
1921 (text-scale-increase 1))
1922 ((eq choice 'text-scale-decrease)
1923 (text-scale-increase -1))
1924 ((eq choice 'face-remap-reset-base)
1925 (text-scale-mode 0)
1926 (buffer-face-mode 0))
1927 (choice
1928 ;; Either choice == 'x-select-font, or choice is a
1929 ;; symbol whose name is a font.
1930 (let ((font (if (eq choice 'x-select-font)
1931 (x-select-font)
1932 (symbol-name choice))))
1933 (buffer-face-mode-invoke
1934 (if (fontp font 'font-spec)
1935 (list :font font)
1936 (font-face-attributes font))
1937 t (called-interactively-p 'interactive)))))))))
1940 ;;; Bindings for mouse commands.
1942 (global-set-key [down-mouse-1] 'mouse-drag-region)
1943 (global-set-key [mouse-1] 'mouse-set-point)
1944 (global-set-key [drag-mouse-1] 'mouse-set-region)
1946 (defun mouse--strip-first-event (_prompt)
1947 (substring (this-single-command-raw-keys) 1))
1949 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
1950 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
1952 (global-set-key [mouse-2] 'mouse-yank-primary)
1953 ;; Allow yanking also when the corresponding cursor is "in the fringe".
1954 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
1955 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
1956 (global-set-key [mouse-3] 'mouse-save-then-kill)
1957 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
1958 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
1960 ;; By binding these to down-going events, we let the user use the up-going
1961 ;; event to make the selection, saving a click.
1962 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1963 (if (not (eq system-type 'ms-dos))
1964 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
1965 ;; C-down-mouse-2 is bound in facemenu.el.
1966 (global-set-key [C-down-mouse-3]
1967 `(menu-item ,(purecopy "Menu Bar") ignore
1968 :filter (lambda (_)
1969 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
1970 (mouse-menu-bar-map)
1971 (mouse-menu-major-mode-map)))))
1973 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
1974 ;; vertical-line prevents Emacs from signaling an error when the mouse
1975 ;; button is released after dragging these lines, on non-toolkit
1976 ;; versions.
1977 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
1978 (global-set-key [header-line mouse-1] 'mouse-select-window)
1979 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1980 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1981 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1982 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1983 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1984 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1985 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1986 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
1987 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1988 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1989 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1990 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
1991 (global-set-key [right-divider mouse-1] 'ignore)
1992 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
1993 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
1994 (global-set-key [bottom-divider mouse-1] 'ignore)
1995 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
1997 (provide 'mouse)
1999 ;;; mouse.el ends here