Revert previous patch; comment was OK after all.
[emacs.git] / lisp / mouse.el
blobf749d12054b36675b1cbffcf1ded8c4e3c56fa29
1 ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1995, 1999-2018 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 <https://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 ;; Drag bottom edge of window above the header line.
409 (setq window (window-in-direction 'above window t)))
410 ((eq line 'mode))
411 ((eq line 'vertical)
412 (let ((divider-width (frame-right-divider-width frame)))
413 (when (and (or (not (numberp divider-width))
414 (zerop divider-width))
415 (eq (frame-parameter frame 'vertical-scroll-bars) 'left))
416 (setq window (window-in-direction 'left window t))))))
417 (let* ((exitfun nil)
418 (move
419 (lambda (event) (interactive "e")
420 (cond
421 ((not (consp event))
422 nil)
423 ((eq line 'vertical)
424 ;; Drag right edge of `window'.
425 (setq start (event-start event))
426 (setq position (car (posn-x-y start)))
427 ;; Set `posn-window' to the window where `event' was recorded.
428 ;; This can be `window' or the window on the left or right of
429 ;; `window'.
430 (when (window-live-p (setq posn-window (posn-window start)))
431 ;; Add left edge of `posn-window' to `position'.
432 (setq position (+ (window-pixel-left posn-window) position))
433 (unless (nth 1 start)
434 ;; Add width of objects on the left of the text area to
435 ;; `position'.
436 (when (eq (window-current-scroll-bars posn-window) 'left)
437 (setq position (+ (window-scroll-bar-width posn-window)
438 position)))
439 (setq position (+ (car (window-fringes posn-window))
440 (or (car (window-margins posn-window)) 0)
441 position))))
442 ;; When the cursor overshoots after shrinking a window to its
443 ;; minimum size and the dragging direction changes, have the
444 ;; cursor first catch up with the window edge.
445 (unless (or (zerop (setq growth (- position last-position)))
446 (and (> growth 0)
447 (< position (+ (window-pixel-left window)
448 (window-pixel-width window))))
449 (and (< growth 0)
450 (> position (+ (window-pixel-left window)
451 (window-pixel-width window)))))
452 (setq dragged t)
453 (adjust-window-trailing-edge window growth t t))
454 (setq last-position position))
455 (draggable
456 ;; Drag bottom edge of `window'.
457 (setq start (event-start event))
458 ;; Set `posn-window' to the window where `event' was recorded.
459 ;; This can be either `window' or the window above or below of
460 ;; `window'.
461 (setq posn-window (posn-window start))
462 (setq position (cdr (posn-x-y start)))
463 (when (window-live-p posn-window)
464 ;; Add top edge of `posn-window' to `position'.
465 (setq position (+ (window-pixel-top posn-window) position))
466 ;; If necessary, add height of header line to `position'
467 (when (memq (posn-area start)
468 '(nil left-fringe right-fringe left-margin right-margin))
469 (setq position (+ (window-header-line-height posn-window) position))))
470 ;; When the cursor overshoots after shrinking a window to its
471 ;; minimum size and the dragging direction changes, have the
472 ;; cursor first catch up with the window edge.
473 (unless (or (zerop (setq growth (- position last-position)))
474 (and (> growth 0)
475 (< position (+ (window-pixel-top window)
476 (window-pixel-height window))))
477 (and (< growth 0)
478 (> position (+ (window-pixel-top window)
479 (window-pixel-height window)))))
480 (setq dragged t)
481 (adjust-window-trailing-edge window growth nil t))
482 (setq last-position position)))))
483 (old-track-mouse track-mouse))
484 ;; Start tracking. The special value 'dragging' signals the
485 ;; display engine to freeze the mouse pointer shape for as long
486 ;; as we drag.
487 (setq track-mouse 'dragging)
488 ;; Loop reading events and sampling the position of the mouse.
489 (setq exitfun
490 (set-transient-map
491 (let ((map (make-sparse-keymap)))
492 (define-key map [switch-frame] #'ignore)
493 (define-key map [select-window] #'ignore)
494 (define-key map [scroll-bar-movement] #'ignore)
495 (define-key map [mouse-movement] move)
496 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
497 (define-key map [drag-mouse-1]
498 (lambda () (interactive) (funcall exitfun)))
499 ;; For vertical line dragging swallow also a mouse-1
500 ;; event (but only if we dragged at least once to allow mouse-1
501 ;; clicks to get through).
502 (when (eq line 'vertical)
503 (define-key map [mouse-1]
504 `(menu-item "" ,(lambda () (interactive) (funcall exitfun))
505 :filter ,(lambda (cmd) (if dragged cmd)))))
506 ;; Some of the events will of course end up looked up
507 ;; with a mode-line, header-line or vertical-line prefix ...
508 (define-key map [mode-line] map)
509 (define-key map [header-line] map)
510 (define-key map [vertical-line] map)
511 ;; ... and some maybe even with a right- or bottom-divider
512 ;; prefix.
513 (define-key map [right-divider] map)
514 (define-key map [bottom-divider] map)
515 map)
516 t (lambda () (setq track-mouse old-track-mouse)))))))
518 (defun mouse-drag-mode-line (start-event)
519 "Change the height of a window by dragging on its mode line.
520 START-EVENT is the starting mouse event of the drag action.
522 If the drag happens in a mode line on the bottom of a frame and
523 that frame's `drag-with-mode-line' parameter is non-nil, drag the
524 frame instead."
525 (interactive "e")
526 (let* ((start (event-start start-event))
527 (window (posn-window start))
528 (frame (window-frame window)))
529 (cond
530 ((not (window-live-p window)))
531 ((or (not (window-at-side-p window 'bottom))
532 ;; Allow resizing the minibuffer window if it's on the
533 ;; same frame as and immediately below `window', and it's
534 ;; either active or `resize-mini-windows' is nil.
535 (let ((minibuffer-window (minibuffer-window frame)))
536 (and (eq (window-frame minibuffer-window) frame)
537 (or (not resize-mini-windows)
538 (eq minibuffer-window
539 (active-minibuffer-window))))))
540 (mouse-drag-line start-event 'mode))
541 ((and (frame-parameter frame 'drag-with-mode-line)
542 (window-at-side-p window 'bottom)
543 (let ((minibuffer-window (minibuffer-window frame)))
544 (not (eq (window-frame minibuffer-window) frame))))
545 ;; Drag frame when the window is on the bottom of its frame and
546 ;; there is no minibuffer window below.
547 (mouse-drag-frame start-event 'move)))))
549 (defun mouse-drag-header-line (start-event)
550 "Change the height of a window by dragging on its header line.
551 START-EVENT is the starting mouse event of the drag action.
553 If the drag happens in a header line on the top of a frame and
554 that frame's `drag-with-header-line' parameter is non-nil, drag
555 the frame instead."
556 (interactive "e")
557 (let* ((start (event-start start-event))
558 (window (posn-window start)))
559 (if (and (window-live-p window)
560 (not (window-at-side-p window 'top)))
561 (mouse-drag-line start-event 'header)
562 (let ((frame (window-frame window)))
563 (when (frame-parameter frame 'drag-with-header-line)
564 (mouse-drag-frame start-event 'move))))))
566 (defun mouse-drag-vertical-line (start-event)
567 "Change the width of a window by dragging on a vertical line.
568 START-EVENT is the starting mouse event of the drag action."
569 (interactive "e")
570 (mouse-drag-line start-event 'vertical))
572 (defun mouse-resize-frame (frame x-diff y-diff &optional x-move y-move)
573 "Helper function for `mouse-drag-frame'."
574 (let* ((frame-x-y (frame-position frame))
575 (frame-x (car frame-x-y))
576 (frame-y (cdr frame-x-y))
577 alist)
578 (if (> x-diff 0)
579 (when x-move
580 (setq x-diff (min x-diff frame-x))
581 (setq x-move (- frame-x x-diff)))
582 (let* ((min-width (frame-windows-min-size frame t nil t))
583 (min-diff (max 0 (- (frame-inner-width frame) min-width))))
584 (setq x-diff (max x-diff (- min-diff)))
585 (when x-move
586 (setq x-move (+ frame-x (- x-diff))))))
588 (if (> y-diff 0)
589 (when y-move
590 (setq y-diff (min y-diff frame-y))
591 (setq y-move (- frame-y y-diff)))
592 (let* ((min-height (frame-windows-min-size frame nil nil t))
593 (min-diff (max 0 (- (frame-inner-height frame) min-height))))
594 (setq y-diff (max y-diff (- min-diff)))
595 (when y-move
596 (setq y-move (+ frame-y (- y-diff))))))
598 (unless (zerop x-diff)
599 (when x-move
600 (push `(left . ,x-move) alist))
601 (push `(width . (text-pixels . ,(+ (frame-text-width frame) x-diff)))
602 alist))
603 (unless (zerop y-diff)
604 (when y-move
605 (push `(top . ,y-move) alist))
606 (push `(height . (text-pixels . ,(+ (frame-text-height frame) y-diff)))
607 alist))
608 (when alist
609 (modify-frame-parameters frame alist))))
611 (defun mouse-drag-frame (start-event part)
612 "Drag a frame or one of its edges with the mouse.
613 START-EVENT is the starting mouse event of the drag action. Its
614 position window denotes the frame that will be dragged.
616 PART specifies the part that has been dragged and must be one of
617 the symbols 'left', 'top', 'right', 'bottom', 'top-left',
618 'top-right', 'bottom-left', 'bottom-right' to drag an internal
619 border or edge. If PART equals 'move', this means to move the
620 frame with the mouse."
621 ;; Give temporary modes such as isearch a chance to turn off.
622 (run-hooks 'mouse-leave-buffer-hook)
623 (let* ((echo-keystrokes 0)
624 (start (event-start start-event))
625 (window (posn-window start))
626 ;; FRAME is the frame to drag.
627 (frame (if (window-live-p window)
628 (window-frame window)
629 window))
630 (width (frame-native-width frame))
631 (height (frame-native-height frame))
632 ;; PARENT is the parent frame of FRAME or, if FRAME is a
633 ;; top-level frame, FRAME's workarea.
634 (parent (frame-parent frame))
635 (parent-edges
636 (if parent
637 `(0 0 ,(frame-native-width parent) ,(frame-native-height parent))
638 (let* ((attributes
639 (car (display-monitor-attributes-list)))
640 (workarea (assq 'workarea attributes)))
641 (and workarea
642 `(,(nth 1 workarea) ,(nth 2 workarea)
643 ,(+ (nth 1 workarea) (nth 3 workarea))
644 ,(+ (nth 2 workarea) (nth 4 workarea)))))))
645 (parent-left (and parent-edges (nth 0 parent-edges)))
646 (parent-top (and parent-edges (nth 1 parent-edges)))
647 (parent-right (and parent-edges (nth 2 parent-edges)))
648 (parent-bottom (and parent-edges (nth 3 parent-edges)))
649 ;; `pos-x' and `pos-y' record the x- and y-coordinates of the
650 ;; last sampled mouse position. Note that we sample absolute
651 ;; mouse positions to avoid that moving the mouse from one
652 ;; frame into another gets into our way. `last-x' and `last-y'
653 ;; records the x- and y-coordinates of the previously sampled
654 ;; position. The differences between `last-x' and `pos-x' as
655 ;; well as `last-y' and `pos-y' determine the amount the mouse
656 ;; has been dragged between the last two samples.
657 pos-x-y pos-x pos-y
658 (last-x-y (mouse-absolute-pixel-position))
659 (last-x (car last-x-y))
660 (last-y (cdr last-x-y))
661 ;; `snap-x' and `snap-y' record the x- and y-coordinates of the
662 ;; mouse position when FRAME snapped. As soon as the
663 ;; difference between `pos-x' and `snap-x' (or `pos-y' and
664 ;; `snap-y') exceeds the value of FRAME's `snap-width'
665 ;; parameter, unsnap FRAME (at the respective side). `snap-x'
666 ;; and `snap-y' nil mean FRAME is currently not snapped.
667 snap-x snap-y
668 (exitfun nil)
669 (move
670 (lambda (event)
671 (interactive "e")
672 (when (consp event)
673 (setq pos-x-y (mouse-absolute-pixel-position))
674 (setq pos-x (car pos-x-y))
675 (setq pos-y (cdr pos-x-y))
676 (cond
677 ((eq part 'left)
678 (mouse-resize-frame frame (- last-x pos-x) 0 t))
679 ((eq part 'top)
680 (mouse-resize-frame frame 0 (- last-y pos-y) nil t))
681 ((eq part 'right)
682 (mouse-resize-frame frame (- pos-x last-x) 0))
683 ((eq part 'bottom)
684 (mouse-resize-frame frame 0 (- pos-y last-y)))
685 ((eq part 'top-left)
686 (mouse-resize-frame
687 frame (- last-x pos-x) (- last-y pos-y) t t))
688 ((eq part 'top-right)
689 (mouse-resize-frame
690 frame (- pos-x last-x) (- last-y pos-y) nil t))
691 ((eq part 'bottom-left)
692 (mouse-resize-frame
693 frame (- last-x pos-x) (- pos-y last-y) t))
694 ((eq part 'bottom-right)
695 (mouse-resize-frame
696 frame (- pos-x last-x) (- pos-y last-y)))
697 ((eq part 'move)
698 (let* ((old-position (frame-position frame))
699 (old-left (car old-position))
700 (old-top (cdr old-position))
701 (left (+ old-left (- pos-x last-x)))
702 (top (+ old-top (- pos-y last-y)))
703 right bottom
704 ;; `snap-width' (maybe also a yet to be provided
705 ;; `snap-height') could become floats to handle
706 ;; proportionality wrt PARENT. We don't do any
707 ;; checks on this parameter so far.
708 (snap-width (frame-parameter frame 'snap-width)))
709 ;; Docking and constraining.
710 (when (and (numberp snap-width) parent-edges)
711 (cond
712 ;; Docking at the left parent edge.
713 ((< pos-x last-x)
714 (cond
715 ((and (> left parent-left)
716 (<= (- left parent-left) snap-width))
717 ;; Snap when the mouse moved leftward and
718 ;; FRAME's left edge would end up within
719 ;; `snap-width' pixels from PARENT's left edge.
720 (setq snap-x pos-x)
721 (setq left parent-left))
722 ((and (<= left parent-left)
723 (<= (- parent-left left) snap-width)
724 snap-x (<= (- snap-x pos-x) snap-width))
725 ;; Stay snapped when the mouse moved leftward
726 ;; but not more than `snap-width' pixels from
727 ;; the time FRAME snapped.
728 (setq left parent-left))
730 ;; Unsnap when the mouse moved more than
731 ;; `snap-width' pixels leftward from the time
732 ;; FRAME snapped.
733 (setq snap-x nil))))
734 ((> pos-x last-x)
735 (setq right (+ left width))
736 (cond
737 ((and (< right parent-right)
738 (<= (- parent-right right) snap-width))
739 ;; Snap when the mouse moved rightward and
740 ;; FRAME's right edge would end up within
741 ;; `snap-width' pixels from PARENT's right edge.
742 (setq snap-x pos-x)
743 (setq left (- parent-right width)))
744 ((and (>= right parent-right)
745 (<= (- right parent-right) snap-width)
746 snap-x (<= (- pos-x snap-x) snap-width))
747 ;; Stay snapped when the mouse moved rightward
748 ;; but not more more than `snap-width' pixels
749 ;; from the time FRAME snapped.
750 (setq left (- parent-right width)))
752 ;; Unsnap when the mouse moved rightward more
753 ;; than `snap-width' pixels from the time FRAME
754 ;; snapped.
755 (setq snap-x nil)))))
757 (cond
758 ((< pos-y last-y)
759 (cond
760 ((and (> top parent-top)
761 (<= (- top parent-top) snap-width))
762 ;; Snap when the mouse moved upward and FRAME's
763 ;; top edge would end up within `snap-width'
764 ;; pixels from PARENT's top edge.
765 (setq snap-y pos-y)
766 (setq top parent-top))
767 ((and (<= top parent-top)
768 (<= (- parent-top top) snap-width)
769 snap-y (<= (- snap-y pos-y) snap-width))
770 ;; Stay snapped when the mouse moved upward but
771 ;; not more more than `snap-width' pixels from
772 ;; the time FRAME snapped.
773 (setq top parent-top))
775 ;; Unsnap when the mouse moved upward more than
776 ;; `snap-width' pixels from the time FRAME
777 ;; snapped.
778 (setq snap-y nil))))
779 ((> pos-y last-y)
780 (setq bottom (+ top height))
781 (cond
782 ((and (< bottom parent-bottom)
783 (<= (- parent-bottom bottom) snap-width))
784 ;; Snap when the mouse moved downward and
785 ;; FRAME's bottom edge would end up within
786 ;; `snap-width' pixels from PARENT's bottom
787 ;; edge.
788 (setq snap-y pos-y)
789 (setq top (- parent-bottom height)))
790 ((and (>= bottom parent-bottom)
791 (<= (- bottom parent-bottom) snap-width)
792 snap-y (<= (- pos-y snap-y) snap-width))
793 ;; Stay snapped when the mouse moved downward
794 ;; but not more more than `snap-width' pixels
795 ;; from the time FRAME snapped.
796 (setq top (- parent-bottom height)))
798 ;; Unsnap when the mouse moved downward more
799 ;; than `snap-width' pixels from the time FRAME
800 ;; snapped.
801 (setq snap-y nil))))))
803 ;; If requested, constrain FRAME's draggable areas to
804 ;; PARENT's edges. The `top-visible' parameter should
805 ;; be set when FRAME has a draggable header-line. If
806 ;; set to a number, it ascertains that the top of
807 ;; FRAME is always constrained to the top of PARENT
808 ;; and that at least as many pixels of FRAME as
809 ;; specified by that number are visible on each of the
810 ;; three remaining sides of PARENT.
812 ;; The `bottom-visible' parameter should be set when
813 ;; FRAME has a draggable mode-line. If set to a
814 ;; number, it ascertains that the bottom of FRAME is
815 ;; always constrained to the bottom of PARENT and that
816 ;; at least as many pixels of FRAME as specified by
817 ;; that number are visible on each of the three
818 ;; remaining sides of PARENT.
819 (let ((par (frame-parameter frame 'top-visible))
820 bottom-visible)
821 (unless par
822 (setq par (frame-parameter frame 'bottom-visible))
823 (setq bottom-visible t))
824 (when (and (numberp par) parent-edges)
825 (setq left
826 (max (min (- parent-right par) left)
827 (+ (- parent-left width) par)))
828 (setq top
829 (if bottom-visible
830 (min (max top (- parent-top (- height par)))
831 (- parent-bottom height))
832 (min (max top parent-top)
833 (- parent-bottom par))))))
835 ;; Use `modify-frame-parameters' since `left' and
836 ;; `top' may want to move FRAME out of its PARENT.
837 (modify-frame-parameters
838 frame
839 `((left . (+ ,left)) (top . (+ ,top)))))))
840 (setq last-x pos-x)
841 (setq last-y pos-y))))
842 (old-track-mouse track-mouse))
843 ;; Start tracking. The special value 'dragging' signals the
844 ;; display engine to freeze the mouse pointer shape for as long
845 ;; as we drag.
846 (setq track-mouse 'dragging)
847 ;; Loop reading events and sampling the position of the mouse.
848 (setq exitfun
849 (set-transient-map
850 (let ((map (make-sparse-keymap)))
851 (define-key map [switch-frame] #'ignore)
852 (define-key map [select-window] #'ignore)
853 (define-key map [scroll-bar-movement] #'ignore)
854 (define-key map [mouse-movement] move)
855 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
856 (define-key map [drag-mouse-1]
857 (lambda () (interactive) (funcall exitfun)))
858 ;; Some of the events will of course end up looked up
859 ;; with a mode-line, header-line or vertical-line prefix ...
860 (define-key map [mode-line] map)
861 (define-key map [header-line] map)
862 (define-key map [vertical-line] map)
863 ;; ... and some maybe even with a right- or bottom-divider
864 ;; prefix.
865 (define-key map [right-divider] map)
866 (define-key map [bottom-divider] map)
867 map)
868 t (lambda () (setq track-mouse old-track-mouse))))))
870 (defun mouse-drag-left-edge (start-event)
871 "Drag left edge of a frame with the mouse.
872 START-EVENT is the starting mouse event of the drag action."
873 (interactive "e")
874 (mouse-drag-frame start-event 'left))
876 (defun mouse-drag-top-left-corner (start-event)
877 "Drag top left corner of a frame with the mouse.
878 START-EVENT is the starting mouse event of the drag action."
879 (interactive "e")
880 (mouse-drag-frame start-event 'top-left))
882 (defun mouse-drag-top-edge (start-event)
883 "Drag top edge of a frame with the mouse.
884 START-EVENT is the starting mouse event of the drag action."
885 (interactive "e")
886 (mouse-drag-frame start-event 'top))
888 (defun mouse-drag-top-right-corner (start-event)
889 "Drag top right corner of a frame with the mouse.
890 START-EVENT is the starting mouse event of the drag action."
891 (interactive "e")
892 (mouse-drag-frame start-event 'top-right))
894 (defun mouse-drag-right-edge (start-event)
895 "Drag right edge of a frame with the mouse.
896 START-EVENT is the starting mouse event of the drag action."
897 (interactive "e")
898 (mouse-drag-frame start-event 'right))
900 (defun mouse-drag-bottom-right-corner (start-event)
901 "Drag bottom right corner of a frame with the mouse.
902 START-EVENT is the starting mouse event of the drag action."
903 (interactive "e")
904 (mouse-drag-frame start-event 'bottom-right))
906 (defun mouse-drag-bottom-edge (start-event)
907 "Drag bottom edge of a frame with the mouse.
908 START-EVENT is the starting mouse event of the drag action."
909 (interactive "e")
910 (mouse-drag-frame start-event 'bottom))
912 (defun mouse-drag-bottom-left-corner (start-event)
913 "Drag bottom left corner of a frame with the mouse.
914 START-EVENT is the starting mouse event of the drag action."
915 (interactive "e")
916 (mouse-drag-frame start-event 'bottom-left))
918 (defcustom mouse-select-region-move-to-beginning nil
919 "Effect of selecting a region extending backward from double click.
920 Nil means keep point at the position clicked (region end);
921 non-nil means move point to beginning of region."
922 :type '(choice (const :tag "Don't move point" nil)
923 (const :tag "Move point to beginning of region" t))
924 :group 'mouse
925 :version "26.1")
927 (defun mouse-set-point (event &optional promote-to-region)
928 "Move point to the position clicked on with the mouse.
929 This should be bound to a mouse click event type.
930 If PROMOTE-TO-REGION is non-nil and event is a multiple-click, select
931 the corresponding element around point, with the resulting position of
932 point determined by `mouse-select-region-move-to-beginning'."
933 (interactive "e\np")
934 (mouse-minibuffer-check event)
935 (if (and promote-to-region (> (event-click-count event) 1))
936 (progn
937 (mouse-set-region event)
938 (when mouse-select-region-move-to-beginning
939 (when (> (posn-point (event-start event)) (region-beginning))
940 (exchange-point-and-mark))))
941 ;; Use event-end in case called from mouse-drag-region.
942 ;; If EVENT is a click, event-end and event-start give same value.
943 (posn-set-point (event-end event))))
945 (defvar mouse-last-region-beg nil)
946 (defvar mouse-last-region-end nil)
947 (defvar mouse-last-region-tick nil)
949 (defun mouse-region-match ()
950 "Return non-nil if there's an active region that was set with the mouse."
951 (and (mark t) mark-active
952 (eq mouse-last-region-beg (region-beginning))
953 (eq mouse-last-region-end (region-end))
954 (eq mouse-last-region-tick (buffer-modified-tick))))
956 (defvar mouse--drag-start-event nil)
958 (defun mouse-set-region (click)
959 "Set the region to the text dragged over, and copy to kill ring.
960 This should be bound to a mouse drag event.
961 See the `mouse-drag-copy-region' variable to control whether this
962 command alters the kill ring or not."
963 (interactive "e")
964 (mouse-minibuffer-check click)
965 (select-window (posn-window (event-start click)))
966 (let ((beg (posn-point (event-start click)))
967 (end
968 (if (eq (posn-window (event-end click)) (selected-window))
969 (posn-point (event-end click))
970 ;; If the mouse ends up in any other window or on the menu
971 ;; bar, use `window-point' of selected window (Bug#23707).
972 (window-point)))
973 (click-count (event-click-count click)))
974 (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
975 (when drag-start
976 ;; Drag events don't come with a click count, sadly, so we hack
977 ;; our way around this problem by remembering the start-event in
978 ;; `mouse-drag-start' and fetching the click-count from there.
979 (when (and (<= click-count 1)
980 (equal beg (posn-point (event-start drag-start))))
981 (setq click-count (event-click-count drag-start)))
982 ;; Occasionally we get spurious drag events where the user hasn't
983 ;; dragged his mouse, but instead Emacs has dragged the text under the
984 ;; user's mouse. Try to recover those cases (bug#17562).
985 (when (and (equal (posn-x-y (event-start click))
986 (posn-x-y (event-end click)))
987 (not (eq (car drag-start) 'mouse-movement)))
988 (setq end beg))
989 (setf (terminal-parameter nil 'mouse-drag-start) nil)))
990 (when (and (integerp beg) (integerp end))
991 (let ((range (mouse-start-end beg end (1- click-count))))
992 (if (< end beg)
993 (setq end (nth 0 range) beg (nth 1 range))
994 (setq beg (nth 0 range) end (nth 1 range)))))
995 (and mouse-drag-copy-region (integerp beg) (integerp end)
996 ;; Don't set this-command to `kill-region', so a following
997 ;; C-w won't double the text in the kill ring. Ignore
998 ;; `last-command' so we don't append to a preceding kill.
999 (let (this-command last-command deactivate-mark)
1000 (copy-region-as-kill beg end)))
1001 (if (numberp beg) (goto-char beg))
1002 ;; On a text terminal, bounce the cursor.
1003 (or transient-mark-mode
1004 (window-system)
1005 (sit-for 1))
1006 (push-mark)
1007 (set-mark (point))
1008 (if (numberp end) (goto-char end))
1009 (mouse-set-region-1)))
1011 (defun mouse-set-region-1 ()
1012 ;; Set transient-mark-mode for a little while.
1013 (unless (eq (car-safe transient-mark-mode) 'only)
1014 (setq-local transient-mark-mode
1015 (cons 'only
1016 (unless (eq transient-mark-mode 'lambda)
1017 transient-mark-mode))))
1018 (setq mouse-last-region-beg (region-beginning))
1019 (setq mouse-last-region-end (region-end))
1020 (setq mouse-last-region-tick (buffer-modified-tick)))
1022 (defcustom mouse-scroll-delay 0.25
1023 "The pause between scroll steps caused by mouse drags, in seconds.
1024 If you drag the mouse beyond the edge of a window, Emacs scrolls the
1025 window to bring the text beyond that edge into view, with a delay of
1026 this many seconds between scroll steps. Scrolling stops when you move
1027 the mouse back into the window, or release the button.
1028 This variable's value may be non-integral.
1029 Setting this to zero causes Emacs to scroll as fast as it can."
1030 :type 'number
1031 :group 'mouse)
1033 (defcustom mouse-scroll-min-lines 1
1034 "The minimum number of lines scrolled by dragging mouse out of window.
1035 Moving the mouse out the top or bottom edge of the window begins
1036 scrolling repeatedly. The number of lines scrolled per repetition
1037 is normally equal to the number of lines beyond the window edge that
1038 the mouse has moved. However, it always scrolls at least the number
1039 of lines specified by this variable."
1040 :type 'integer
1041 :group 'mouse)
1043 (defun mouse-scroll-subr (window jump &optional overlay start)
1044 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
1045 If OVERLAY is an overlay, let it stretch from START to the far edge of
1046 the newly visible text.
1047 Upon exit, point is at the far edge of the newly visible text."
1048 (cond
1049 ((and (> jump 0) (< jump mouse-scroll-min-lines))
1050 (setq jump mouse-scroll-min-lines))
1051 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
1052 (setq jump (- mouse-scroll-min-lines))))
1053 (let ((opoint (point)))
1054 (while (progn
1055 (goto-char (window-start window))
1056 (if (not (zerop (vertical-motion jump window)))
1057 (progn
1058 (set-window-start window (point))
1059 (if (natnump jump)
1060 (if (window-end window)
1061 (progn
1062 (goto-char (window-end window))
1063 ;; window-end doesn't reflect the window's new
1064 ;; start position until the next redisplay.
1065 (vertical-motion (1- jump) window))
1066 (vertical-motion (- (window-height window) 2)))
1067 (goto-char (window-start window)))
1068 (if overlay
1069 (move-overlay overlay start (point)))
1070 ;; Now that we have scrolled WINDOW properly,
1071 ;; put point back where it was for the redisplay
1072 ;; so that we don't mess up the selected window.
1073 (or (eq window (selected-window))
1074 (goto-char opoint))
1075 (sit-for mouse-scroll-delay)))))
1076 (or (eq window (selected-window))
1077 (goto-char opoint))))
1079 (defvar mouse-selection-click-count 0)
1081 (defvar mouse-selection-click-count-buffer nil)
1083 (defun mouse-drag-region (start-event)
1084 "Set the region to the text that the mouse is dragged over.
1085 Highlight the drag area as you move the mouse.
1086 This must be bound to a button-down mouse event.
1087 In Transient Mark mode, the highlighting remains as long as the mark
1088 remains active. Otherwise, it remains until the next input event.
1090 When the region already exists and `mouse-drag-and-drop-region'
1091 is non-nil, this moves the entire region of text to where mouse
1092 is dragged over to."
1093 (interactive "e")
1094 (if (and mouse-drag-and-drop-region
1095 (not (member 'triple (event-modifiers start-event)))
1096 (equal (mouse-posn-property (event-start start-event) 'face) 'region))
1097 (mouse-drag-and-drop-region start-event)
1098 ;; Give temporary modes such as isearch a chance to turn off.
1099 (run-hooks 'mouse-leave-buffer-hook)
1100 (mouse-drag-track start-event)))
1102 (defun mouse-posn-property (pos property)
1103 "Look for a property at click position.
1104 POS may be either a buffer position or a click position like
1105 those returned from `event-start'. If the click position is on
1106 a string, the text property PROPERTY is examined.
1107 If this is nil or the click is not on a string, then
1108 the corresponding buffer position is searched for PROPERTY.
1109 If PROPERTY is encountered in one of those places,
1110 its value is returned."
1111 (if (consp pos)
1112 (let ((w (posn-window pos)) (pt (posn-point pos))
1113 (str (posn-string pos)))
1114 (or (and str
1115 (get-text-property (cdr str) property (car str)))
1116 ;; Mouse clicks in the fringe come with a position in
1117 ;; (nth 5). This is useful but is not exactly where we clicked, so
1118 ;; don't look up that position's properties!
1119 (and pt (not (memq (posn-area pos) '(left-fringe right-fringe
1120 left-margin right-margin)))
1121 (get-char-property pt property w))))
1122 (get-char-property pos property)))
1124 (defun mouse-on-link-p (pos)
1125 "Return non-nil if POS is on a link in the current buffer.
1126 POS must specify a buffer position in the current buffer, as a list
1127 of the form returned by the `event-start' and `event-end' functions,
1128 or a mouse event location in the selected window (see `event-start').
1129 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
1130 POS may be a mouse event location in any window.
1132 A clickable link is identified by one of the following methods:
1134 - If the character at POS has a non-nil `follow-link' text or
1135 overlay property, the value of that property determines what to do.
1137 - If there is a local key-binding or a keybinding at position POS
1138 for the `follow-link' event, the binding of that event determines
1139 what to do.
1141 The resulting value determine whether POS is inside a link:
1143 - If the value is `mouse-face', POS is inside a link if there
1144 is a non-nil `mouse-face' property at POS. Return t in this case.
1146 - If the value is a function, FUNC, POS is inside a link if
1147 the call \(FUNC POS) returns non-nil. Return the return value
1148 from that call. Arg is \(posn-point POS) if POS is a mouse event.
1150 - Otherwise, return the value itself.
1152 The return value is interpreted as follows:
1154 - If it is a string, the mouse-1 event is translated into the
1155 first character of the string, i.e. the action of the mouse-1
1156 click is the local or global binding of that character.
1158 - If it is a vector, the mouse-1 event is translated into the
1159 first element of that vector, i.e. the action of the mouse-1
1160 click is the local or global binding of that event.
1162 - Otherwise, the mouse-1 event is translated into a mouse-2 event
1163 at the same position."
1164 (let ((action
1165 (and (or (not (consp pos))
1166 mouse-1-click-in-non-selected-windows
1167 (eq (selected-window) (posn-window pos)))
1168 (or (mouse-posn-property pos 'follow-link)
1169 (let ((area (posn-area pos)))
1170 (when area
1171 (key-binding (vector area 'follow-link) nil t pos)))
1172 (key-binding [follow-link] nil t pos)))))
1173 (cond
1174 ((eq action 'mouse-face)
1175 (and (mouse-posn-property pos 'mouse-face) t))
1176 ((functionp action)
1177 ;; FIXME: This seems questionable if the click is not in a buffer.
1178 ;; Should we instead decide that `action' takes a `posn'?
1179 (if (consp pos)
1180 (with-current-buffer (window-buffer (posn-window pos))
1181 (funcall action (posn-point pos)))
1182 (funcall action pos)))
1183 (t action))))
1185 (defun mouse-fixup-help-message (msg)
1186 "Fix help message MSG for `mouse-1-click-follows-link'."
1187 (let (mp pos)
1188 (if (and mouse-1-click-follows-link
1189 (stringp msg)
1190 (string-match-p "\\`mouse-2" msg)
1191 (setq mp (mouse-pixel-position))
1192 (consp (setq pos (cdr mp)))
1193 (car pos) (>= (car pos) 0)
1194 (cdr pos) (>= (cdr pos) 0)
1195 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
1196 (windowp (posn-window pos)))
1197 (with-current-buffer (window-buffer (posn-window pos))
1198 (if (mouse-on-link-p pos)
1199 (setq msg (concat
1200 (cond
1201 ((eq mouse-1-click-follows-link 'double) "double-")
1202 ((and (integerp mouse-1-click-follows-link)
1203 (< mouse-1-click-follows-link 0)) "Long ")
1204 (t ""))
1205 "mouse-1" (substring msg 7)))))))
1206 msg)
1208 (defun mouse-drag-track (start-event)
1209 "Track mouse drags by highlighting area between point and cursor.
1210 The region will be defined with mark and point."
1211 (mouse-minibuffer-check start-event)
1212 (setq mouse-selection-click-count-buffer (current-buffer))
1213 (deactivate-mark)
1214 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
1215 (start-posn (event-start start-event))
1216 (start-point (posn-point start-posn))
1217 (start-window (posn-window start-posn))
1218 (_ (with-current-buffer (window-buffer start-window)
1219 (setq deactivate-mark nil)))
1220 ;; We've recorded what we needed from the current buffer and
1221 ;; window, now let's jump to the place of the event, where things
1222 ;; are happening.
1223 (_ (mouse-set-point start-event))
1224 (echo-keystrokes 0)
1225 (bounds (window-edges start-window))
1226 (make-cursor-line-fully-visible nil)
1227 (top (nth 1 bounds))
1228 (bottom (if (or (window-minibuffer-p start-window)
1229 ;; Do not account for the mode line if there
1230 ;; is no mode line, which is common for child
1231 ;; frames.
1232 (not mode-line-format))
1233 (nth 3 bounds)
1234 ;; Don't count the mode line.
1235 (1- (nth 3 bounds))))
1236 (click-count (1- (event-click-count start-event)))
1237 ;; Suppress automatic hscrolling, because that is a nuisance
1238 ;; when setting point near the right fringe (but see below).
1239 (auto-hscroll-mode-saved auto-hscroll-mode)
1240 (old-track-mouse track-mouse))
1242 (setq mouse-selection-click-count click-count)
1243 ;; In case the down click is in the middle of some intangible text,
1244 ;; use the end of that text, and put it in START-POINT.
1245 (if (< (point) start-point)
1246 (goto-char start-point))
1247 (setq start-point (point))
1249 ;; Activate the region, using `mouse-start-end' to determine where
1250 ;; to put point and mark (e.g., double-click will select a word).
1251 (setq-local transient-mark-mode
1252 (if (eq transient-mark-mode 'lambda)
1253 '(only)
1254 (cons 'only transient-mark-mode)))
1255 (let ((range (mouse-start-end start-point start-point click-count)))
1256 (push-mark (nth 0 range) t t)
1257 (goto-char (nth 1 range)))
1259 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
1260 (setq track-mouse t)
1261 (setq auto-hscroll-mode nil)
1263 (set-transient-map
1264 (let ((map (make-sparse-keymap)))
1265 (define-key map [switch-frame] #'ignore)
1266 (define-key map [select-window] #'ignore)
1267 (define-key map [mouse-movement]
1268 (lambda (event) (interactive "e")
1269 (let* ((end (event-end event))
1270 (end-point (posn-point end)))
1271 (unless (eq end-point start-point)
1272 ;; As soon as the user moves, we can re-enable auto-hscroll.
1273 (setq auto-hscroll-mode auto-hscroll-mode-saved)
1274 ;; And remember that we have moved, so mouse-set-region can know
1275 ;; its event is really a drag event.
1276 (setcar start-event 'mouse-movement))
1277 (if (and (eq (posn-window end) start-window)
1278 (integer-or-marker-p end-point))
1279 (mouse--drag-set-mark-and-point start-point
1280 end-point click-count)
1281 (let ((mouse-row (cdr (cdr (mouse-position)))))
1282 (cond
1283 ((null mouse-row))
1284 ((< mouse-row top)
1285 (mouse-scroll-subr start-window (- mouse-row top)
1286 nil start-point))
1287 ((>= mouse-row bottom)
1288 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1289 nil start-point))))))))
1290 map)
1291 t (lambda ()
1292 (setq track-mouse old-track-mouse)
1293 (setq auto-hscroll-mode auto-hscroll-mode-saved)
1294 (deactivate-mark)
1295 (pop-mark)))))
1297 (defun mouse--drag-set-mark-and-point (start click click-count)
1298 (let* ((range (mouse-start-end start click click-count))
1299 (beg (nth 0 range))
1300 (end (nth 1 range)))
1301 (cond ((eq (mark) beg)
1302 (goto-char end))
1303 ((eq (mark) end)
1304 (goto-char beg))
1305 ((< click (mark))
1306 (set-mark end)
1307 (goto-char beg))
1309 (set-mark beg)
1310 (goto-char end)))))
1312 ;; Commands to handle xterm-style multiple clicks.
1313 (defun mouse-skip-word (dir)
1314 "Skip over word, over whitespace, or over identical punctuation.
1315 If DIR is positive skip forward; if negative, skip backward."
1316 (let* ((char (following-char))
1317 (syntax (char-to-string (char-syntax char))))
1318 (cond ((string= syntax "w")
1319 ;; Here, we can't use skip-syntax-forward/backward because
1320 ;; they don't pay attention to word-separating-categories,
1321 ;; and thus they will skip over a true word boundary. So,
1322 ;; we simulate the original behavior by using forward-word.
1323 (if (< dir 0)
1324 (if (not (looking-at "\\<"))
1325 (forward-word -1))
1326 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1327 (forward-word 1))))
1328 ((string= syntax " ")
1329 (if (< dir 0)
1330 (skip-syntax-backward syntax)
1331 (skip-syntax-forward syntax)))
1332 ((string= syntax "_")
1333 (if (< dir 0)
1334 (skip-syntax-backward "w_")
1335 (skip-syntax-forward "w_")))
1336 ((< dir 0)
1337 (while (and (not (bobp)) (= (preceding-char) char))
1338 (forward-char -1)))
1340 (while (and (not (eobp)) (= (following-char) char))
1341 (forward-char 1))))))
1343 (defun mouse-start-end (start end mode)
1344 "Return a list of region bounds based on START and END according to MODE.
1345 If MODE is 0 then set point to (min START END), mark to (max START END).
1346 If MODE is 1 then set point to start of word at (min START END),
1347 mark to end of word at (max START END).
1348 If MODE is 2 then do the same for lines."
1349 (if (> start end)
1350 (let ((temp start))
1351 (setq start end
1352 end temp)))
1353 (setq mode (mod mode 3))
1354 (cond ((= mode 0)
1355 (list start end))
1356 ((and (= mode 1)
1357 (= start end)
1358 (char-after start)
1359 (= (char-syntax (char-after start)) ?\())
1360 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
1361 ;; This happens in CC Mode when unbalanced parens in CPP
1362 ;; constructs are given punctuation syntax with
1363 ;; syntax-table text properties. (2016-02-21).
1364 (signal 'scan-error (list "Containing expression ends prematurely"
1365 start start))
1366 (list start
1367 (save-excursion
1368 (goto-char start)
1369 (forward-sexp 1)
1370 (point)))))
1371 ((and (= mode 1)
1372 (= start end)
1373 (char-after start)
1374 (= (char-syntax (char-after start)) ?\)))
1375 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
1376 ;; See above comment about CC Mode.
1377 (signal 'scan-error (list "Unbalanced parentheses" start start))
1378 (list (save-excursion
1379 (goto-char (1+ start))
1380 (backward-sexp 1)
1381 (point))
1382 (1+ start))))
1383 ((and (= mode 1)
1384 (= start end)
1385 (char-after start)
1386 (= (char-syntax (char-after start)) ?\"))
1387 (let ((open (or (eq start (point-min))
1388 (save-excursion
1389 (goto-char (- start 1))
1390 (looking-at "\\s(\\|\\s \\|\\s>")))))
1391 (if open
1392 (list start
1393 (save-excursion
1394 (condition-case nil
1395 (progn
1396 (goto-char start)
1397 (forward-sexp 1)
1398 (point))
1399 (error end))))
1400 (list (save-excursion
1401 (condition-case nil
1402 (progn
1403 (goto-char (1+ start))
1404 (backward-sexp 1)
1405 (point))
1406 (error end)))
1407 (1+ start)))))
1408 ((= mode 1)
1409 (list (save-excursion
1410 (goto-char start)
1411 (mouse-skip-word -1)
1412 (point))
1413 (save-excursion
1414 (goto-char end)
1415 (mouse-skip-word 1)
1416 (point))))
1417 ((= mode 2)
1418 (list (save-excursion
1419 (goto-char start)
1420 (line-beginning-position 1))
1421 (save-excursion
1422 (goto-char end)
1423 (forward-line 1)
1424 (point))))))
1426 ;; Subroutine: set the mark where CLICK happened,
1427 ;; but don't do anything else.
1428 (defun mouse-set-mark-fast (click)
1429 (mouse-minibuffer-check click)
1430 (let ((posn (event-start click)))
1431 (select-window (posn-window posn))
1432 (if (numberp (posn-point posn))
1433 (push-mark (posn-point posn) t t))))
1435 (defun mouse-undouble-last-event (events)
1436 (let* ((index (1- (length events)))
1437 (last (nthcdr index events))
1438 (event (car last))
1439 (basic (event-basic-type event))
1440 (old-modifiers (event-modifiers event))
1441 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1442 (new
1443 (if (consp event)
1444 ;; Use reverse, not nreverse, since event-modifiers
1445 ;; does not copy the list it returns.
1446 (cons (event-convert-list (reverse (cons basic modifiers)))
1447 (cdr event))
1448 event)))
1449 (setcar last new)
1450 (if (and (not (equal modifiers old-modifiers))
1451 (key-binding (apply 'vector events)))
1453 (setcar last event)
1454 nil)))
1456 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1458 (defun mouse-set-mark (click)
1459 "Set mark at the position clicked on with the mouse.
1460 Display cursor at that position for a second.
1461 This must be bound to a mouse click."
1462 (interactive "e")
1463 (mouse-minibuffer-check click)
1464 (select-window (posn-window (event-start click)))
1465 ;; FIXME: Use save-excursion
1466 (let ((point-save (point)))
1467 (unwind-protect
1468 (progn (mouse-set-point click)
1469 (push-mark nil t t)
1470 (or transient-mark-mode
1471 (sit-for 1)))
1472 (goto-char point-save))))
1474 (defun mouse-kill (click)
1475 "Kill the region between point and the mouse click.
1476 The text is saved in the kill ring, as with \\[kill-region]."
1477 (interactive "e")
1478 (mouse-minibuffer-check click)
1479 (let* ((posn (event-start click))
1480 (click-posn (posn-point posn)))
1481 (select-window (posn-window posn))
1482 (if (numberp click-posn)
1483 (kill-region (min (point) click-posn)
1484 (max (point) click-posn)))))
1486 (defun mouse-yank-at-click (click arg)
1487 "Insert the last stretch of killed text at the position clicked on.
1488 Also move point to one end of the text thus inserted (normally the end),
1489 and set mark at the beginning.
1490 Prefix arguments are interpreted as with \\[yank].
1491 If `mouse-yank-at-point' is non-nil, insert at point
1492 regardless of where you click."
1493 (interactive "e\nP")
1494 ;; Give temporary modes such as isearch a chance to turn off.
1495 (run-hooks 'mouse-leave-buffer-hook)
1496 (when select-active-regions
1497 ;; Without this, confusing things happen upon e.g. inserting into
1498 ;; the middle of an active region.
1499 (deactivate-mark))
1500 (or mouse-yank-at-point (mouse-set-point click))
1501 (setq this-command 'yank)
1502 (setq mouse-selection-click-count 0)
1503 (yank arg))
1505 (defun mouse-yank-primary (click)
1506 "Insert the primary selection at the position clicked on.
1507 Move point to the end of the inserted text, and set mark at
1508 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1509 regardless of where you click."
1510 (interactive "e")
1511 ;; Give temporary modes such as isearch a chance to turn off.
1512 (run-hooks 'mouse-leave-buffer-hook)
1513 ;; Without this, confusing things happen upon e.g. inserting into
1514 ;; the middle of an active region.
1515 (when select-active-regions
1516 (let (select-active-regions)
1517 (deactivate-mark)))
1518 (or mouse-yank-at-point (mouse-set-point click))
1519 (let ((primary (gui-get-primary-selection)))
1520 (push-mark)
1521 (insert-for-yank primary)))
1523 (defun mouse-kill-ring-save (click)
1524 "Copy the region between point and the mouse click in the kill ring.
1525 This does not delete the region; it acts like \\[kill-ring-save]."
1526 (interactive "e")
1527 (mouse-set-mark-fast click)
1528 (let (this-command last-command)
1529 (kill-ring-save (point) (mark t))))
1531 ;; This function used to delete the text between point and the mouse
1532 ;; whenever it was equal to the front of the kill ring, but some
1533 ;; people found that confusing.
1535 ;; The position of the last invocation of `mouse-save-then-kill'.
1536 (defvar mouse-save-then-kill-posn nil)
1538 (defun mouse-save-then-kill-delete-region (beg end)
1539 ;; We must make our own undo boundaries
1540 ;; because they happen automatically only for the current buffer.
1541 (undo-boundary)
1542 (if (or (= beg end) (eq buffer-undo-list t))
1543 ;; If we have no undo list in this buffer,
1544 ;; just delete.
1545 (delete-region beg end)
1546 ;; Delete, but make the undo-list entry share with the kill ring.
1547 ;; First, delete just one char, so in case buffer is being modified
1548 ;; for the first time, the undo list records that fact.
1549 (let ((inhibit-modification-hooks t))
1550 (delete-region beg
1551 (+ beg (if (> end beg) 1 -1))))
1552 (let ((buffer-undo-list buffer-undo-list))
1553 ;; Undo that deletion--but don't change the undo list!
1554 (let ((inhibit-modification-hooks t))
1555 (primitive-undo 1 buffer-undo-list))
1556 ;; Now delete the rest of the specified region,
1557 ;; but don't record it.
1558 (setq buffer-undo-list t)
1559 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1560 (error "Lossage in mouse-save-then-kill-delete-region"))
1561 (delete-region beg end))
1562 (let ((tail buffer-undo-list))
1563 ;; Search back in buffer-undo-list for the string
1564 ;; that came from deleting one character.
1565 (while (and tail (not (stringp (car (car tail)))))
1566 (setq tail (cdr tail)))
1567 ;; Replace it with an entry for the entire deleted text.
1568 (and tail
1569 (setcar tail (cons (car kill-ring) (min beg end))))))
1570 (undo-boundary))
1572 (defun mouse-save-then-kill (click)
1573 "Set the region according to CLICK; the second time, kill it.
1574 CLICK should be a mouse click event.
1576 If the region is inactive, activate it temporarily. Set mark at
1577 the original point, and move point to the position of CLICK.
1579 If the region is already active, adjust it. Normally, do this by
1580 moving point or mark, whichever is closer, to CLICK. But if you
1581 have selected whole words or lines, move point or mark to the
1582 word or line boundary closest to CLICK instead.
1584 If `mouse-drag-copy-region' is non-nil, this command also saves the
1585 new region to the kill ring (replacing the previous kill if the
1586 previous region was just saved to the kill ring).
1588 If this command is called a second consecutive time with the same
1589 CLICK position, kill the region (or delete it
1590 if `mouse-drag-copy-region' is non-nil)"
1591 (interactive "e")
1592 (mouse-minibuffer-check click)
1593 (let* ((posn (event-start click))
1594 (click-pt (posn-point posn))
1595 (window (posn-window posn))
1596 (buf (window-buffer window))
1597 ;; Don't let a subsequent kill command append to this one.
1598 (this-command this-command)
1599 ;; Check if the user has multi-clicked to select words/lines.
1600 (click-count
1601 (if (and (eq mouse-selection-click-count-buffer buf)
1602 (with-current-buffer buf (mark t)))
1603 mouse-selection-click-count
1604 0)))
1605 (cond
1606 ((not (numberp click-pt)) nil)
1607 ;; If the user clicked without moving point, kill the region.
1608 ;; This also resets `mouse-selection-click-count'.
1609 ((and (eq last-command 'mouse-save-then-kill)
1610 (eq click-pt mouse-save-then-kill-posn)
1611 (eq window (selected-window)))
1612 (if mouse-drag-copy-region
1613 ;; Region already saved in the previous click;
1614 ;; don't make a duplicate entry, just delete.
1615 (delete-region (mark t) (point))
1616 (kill-region (mark t) (point)))
1617 (setq mouse-selection-click-count 0)
1618 (setq mouse-save-then-kill-posn nil))
1620 ;; Otherwise, if there is a suitable region, adjust it by moving
1621 ;; one end (whichever is closer) to CLICK-PT.
1622 ((or (with-current-buffer buf (region-active-p))
1623 (and (eq window (selected-window))
1624 (mark t)
1625 (or (and (eq last-command 'mouse-save-then-kill)
1626 mouse-save-then-kill-posn)
1627 (and (memq last-command '(mouse-drag-region
1628 mouse-set-region))
1629 (or mark-even-if-inactive
1630 (not transient-mark-mode))))))
1631 (select-window window)
1632 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1633 (if (< (abs (- click-pt (mark t)))
1634 (abs (- click-pt (point))))
1635 (set-mark (car range))
1636 (goto-char (nth 1 range)))
1637 (setq deactivate-mark nil)
1638 (mouse-set-region-1)
1639 (when mouse-drag-copy-region
1640 ;; Region already copied to kill-ring once, so replace.
1641 (kill-new (filter-buffer-substring (mark t) (point)) t))
1642 ;; Arrange for a repeated mouse-3 to kill the region.
1643 (setq mouse-save-then-kill-posn click-pt)))
1645 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1647 (select-window window)
1648 (mouse-set-mark-fast click)
1649 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1650 (if before-scroll (goto-char before-scroll)))
1651 (exchange-point-and-mark)
1652 (mouse-set-region-1)
1653 (when mouse-drag-copy-region
1654 (kill-new (filter-buffer-substring (mark t) (point))))
1655 (setq mouse-save-then-kill-posn click-pt)))))
1658 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1659 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1660 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1661 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1662 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1664 (defconst mouse-secondary-overlay
1665 (let ((ol (make-overlay (point-min) (point-min))))
1666 (delete-overlay ol)
1667 (overlay-put ol 'face 'secondary-selection)
1669 "An overlay which records the current secondary selection.
1670 It is deleted when there is no secondary selection.")
1672 (defvar mouse-secondary-click-count 0)
1674 ;; A marker which records the specified first end for a secondary selection.
1675 ;; May be nil.
1676 (defvar mouse-secondary-start nil)
1678 (defun mouse-start-secondary (click)
1679 "Set one end of the secondary selection to the position clicked on.
1680 Use \\[mouse-secondary-save-then-kill] to set the other end
1681 and complete the secondary selection."
1682 (interactive "e")
1683 (mouse-minibuffer-check click)
1684 (let ((posn (event-start click)))
1685 (with-current-buffer (window-buffer (posn-window posn))
1686 ;; Cancel any preexisting secondary selection.
1687 (delete-overlay mouse-secondary-overlay)
1688 (if (numberp (posn-point posn))
1689 (progn
1690 (or mouse-secondary-start
1691 (setq mouse-secondary-start (make-marker)))
1692 (move-marker mouse-secondary-start (posn-point posn)))))))
1694 (defun mouse-set-secondary (click)
1695 "Set the secondary selection to the text that the mouse is dragged over.
1696 This must be bound to a mouse drag event."
1697 (interactive "e")
1698 (mouse-minibuffer-check click)
1699 (let ((posn (event-start click))
1701 (end (event-end click)))
1702 (with-current-buffer (window-buffer (posn-window posn))
1703 (if (numberp (posn-point posn))
1704 (setq beg (posn-point posn)))
1705 (move-overlay mouse-secondary-overlay beg (posn-point end))
1706 (gui-set-selection
1707 'SECONDARY
1708 (buffer-substring (overlay-start mouse-secondary-overlay)
1709 (overlay-end mouse-secondary-overlay))))))
1711 (defun mouse-drag-secondary (start-event)
1712 "Set the secondary selection to the text that the mouse is dragged over.
1713 Highlight the drag area as you move the mouse.
1714 This must be bound to a button-down mouse event.
1715 The function returns a non-nil value if it creates a secondary selection."
1716 (interactive "e")
1717 (mouse-minibuffer-check start-event)
1718 (let* ((echo-keystrokes 0)
1719 (start-posn (event-start start-event))
1720 (start-point (posn-point start-posn))
1721 (start-window (posn-window start-posn))
1722 (bounds (window-edges start-window))
1723 (top (nth 1 bounds))
1724 (bottom (if (window-minibuffer-p start-window)
1725 (nth 3 bounds)
1726 ;; Don't count the mode line.
1727 (1- (nth 3 bounds))))
1728 (click-count (1- (event-click-count start-event))))
1729 (with-current-buffer (window-buffer start-window)
1730 (setq mouse-secondary-click-count click-count)
1731 (if (> (mod click-count 3) 0)
1732 ;; Double or triple press: make an initial selection
1733 ;; of one word or line.
1734 (let ((range (mouse-start-end start-point start-point click-count)))
1735 (set-marker mouse-secondary-start nil)
1736 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1737 (window-buffer start-window)))
1738 ;; Single-press: cancel any preexisting secondary selection.
1739 (or mouse-secondary-start
1740 (setq mouse-secondary-start (make-marker)))
1741 (set-marker mouse-secondary-start start-point)
1742 (delete-overlay mouse-secondary-overlay))
1743 ;; FIXME: Use mouse-drag-track!
1744 (let (event end end-point)
1745 (track-mouse
1746 (while (progn
1747 (setq event (read-event))
1748 (or (mouse-movement-p event)
1749 (memq (car-safe event) '(switch-frame select-window))))
1751 (if (memq (car-safe event) '(switch-frame select-window))
1753 (setq end (event-end event)
1754 end-point (posn-point end))
1755 (cond
1756 ;; Are we moving within the original window?
1757 ((and (eq (posn-window end) start-window)
1758 (integer-or-marker-p end-point))
1759 (let ((range (mouse-start-end start-point end-point
1760 click-count)))
1761 (if (or (/= start-point end-point)
1762 (null (marker-position mouse-secondary-start)))
1763 (progn
1764 (set-marker mouse-secondary-start nil)
1765 (move-overlay mouse-secondary-overlay
1766 (car range) (nth 1 range))))))
1768 (let ((mouse-row (cdr (cdr (mouse-position)))))
1769 (cond
1770 ((null mouse-row))
1771 ((< mouse-row top)
1772 (mouse-scroll-subr start-window (- mouse-row top)
1773 mouse-secondary-overlay start-point))
1774 ((>= mouse-row bottom)
1775 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1776 mouse-secondary-overlay start-point)))))))))
1778 (if (consp event)
1779 (if (marker-position mouse-secondary-start)
1780 (save-window-excursion
1781 (delete-overlay mouse-secondary-overlay)
1782 (gui-set-selection 'SECONDARY nil)
1783 (select-window start-window)
1784 (save-excursion
1785 (goto-char mouse-secondary-start)
1786 (sit-for 1)
1787 nil))
1788 (gui-set-selection
1789 'SECONDARY
1790 (buffer-substring (overlay-start mouse-secondary-overlay)
1791 (overlay-end mouse-secondary-overlay)))))))))
1793 (defun mouse-yank-secondary (click)
1794 "Insert the secondary selection at the position clicked on.
1795 Move point to the end of the inserted text.
1796 If `mouse-yank-at-point' is non-nil, insert at point
1797 regardless of where you click."
1798 (interactive "e")
1799 ;; Give temporary modes such as isearch a chance to turn off.
1800 (run-hooks 'mouse-leave-buffer-hook)
1801 (or mouse-yank-at-point (mouse-set-point click))
1802 (let ((secondary (gui-get-selection 'SECONDARY)))
1803 (if secondary
1804 (insert-for-yank secondary)
1805 (error "No secondary selection"))))
1807 (defun mouse-kill-secondary ()
1808 "Kill the text in the secondary selection.
1809 This is intended more as a keyboard command than as a mouse command
1810 but it can work as either one.
1812 The current buffer (in case of keyboard use), or the buffer clicked on,
1813 must be the one that the secondary selection is in. This requirement
1814 is to prevent accidents."
1815 (interactive)
1816 (let* ((keys (this-command-keys))
1817 (click (elt keys (1- (length keys)))))
1818 (or (eq (overlay-buffer mouse-secondary-overlay)
1819 (if (listp click)
1820 (window-buffer (posn-window (event-start click)))
1821 (current-buffer)))
1822 (error "Select or click on the buffer where the secondary selection is")))
1823 (let (this-command)
1824 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1825 (kill-region (overlay-start mouse-secondary-overlay)
1826 (overlay-end mouse-secondary-overlay))))
1827 (delete-overlay mouse-secondary-overlay))
1829 (defun mouse-secondary-save-then-kill (click)
1830 "Set the secondary selection and save it to the kill ring.
1831 The second time, kill it. CLICK should be a mouse click event.
1833 If you have not called `mouse-start-secondary' in the clicked
1834 buffer, activate the secondary selection and set it between point
1835 and the click position CLICK.
1837 Otherwise, adjust the bounds of the secondary selection.
1838 Normally, do this by moving its beginning or end, whichever is
1839 closer, to CLICK. But if you have selected whole words or lines,
1840 adjust to the word or line boundary closest to CLICK instead.
1842 If this command is called a second consecutive time with the same
1843 CLICK position, kill the secondary selection."
1844 (interactive "e")
1845 (mouse-minibuffer-check click)
1846 (let* ((posn (event-start click))
1847 (click-pt (posn-point posn))
1848 (window (posn-window posn))
1849 (buf (window-buffer window))
1850 ;; Don't let a subsequent kill command append to this one.
1851 (this-command this-command)
1852 ;; Check if the user has multi-clicked to select words/lines.
1853 (click-count
1854 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1855 mouse-secondary-click-count
1857 (beg (overlay-start mouse-secondary-overlay))
1858 (end (overlay-end mouse-secondary-overlay)))
1860 (cond
1861 ((not (numberp click-pt)) nil)
1863 ;; If the secondary selection is not active in BUF, activate it.
1864 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1865 (if mouse-secondary-start
1866 (marker-buffer mouse-secondary-start)))))
1867 (select-window window)
1868 (setq mouse-secondary-start (make-marker))
1869 (move-marker mouse-secondary-start (point))
1870 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1871 (kill-ring-save (point) click-pt))
1873 ;; If the user clicked without moving point, delete the secondary
1874 ;; selection. This also resets `mouse-secondary-click-count'.
1875 ((and (eq last-command 'mouse-secondary-save-then-kill)
1876 (eq click-pt mouse-save-then-kill-posn)
1877 (eq window (selected-window)))
1878 (mouse-save-then-kill-delete-region beg end)
1879 (delete-overlay mouse-secondary-overlay)
1880 (setq mouse-secondary-click-count 0)
1881 (setq mouse-save-then-kill-posn nil))
1883 ;; Otherwise, if there is a suitable secondary selection overlay,
1884 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1885 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1886 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1887 (if (< (abs (- click-pt beg))
1888 (abs (- click-pt end)))
1889 (move-overlay mouse-secondary-overlay (car range) end)
1890 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1891 (setq deactivate-mark nil)
1892 (if (eq last-command 'mouse-secondary-save-then-kill)
1893 ;; If the front of the kill ring comes from an immediately
1894 ;; previous use of this command, replace the entry.
1895 (kill-new
1896 (buffer-substring (overlay-start mouse-secondary-overlay)
1897 (overlay-end mouse-secondary-overlay))
1899 (let (deactivate-mark)
1900 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1901 (overlay-end mouse-secondary-overlay))))
1902 (setq mouse-save-then-kill-posn click-pt))
1904 ;; Otherwise, set the secondary selection overlay.
1906 (select-window window)
1907 (if mouse-secondary-start
1908 ;; All we have is one end of a selection, so put the other
1909 ;; end here.
1910 (let ((start (+ 0 mouse-secondary-start)))
1911 (kill-ring-save start click-pt)
1912 (move-overlay mouse-secondary-overlay start click-pt)))
1913 (setq mouse-save-then-kill-posn click-pt))))
1915 ;; Finally, set the window system's secondary selection.
1916 (let (str)
1917 (and (overlay-buffer mouse-secondary-overlay)
1918 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1919 (overlay-end mouse-secondary-overlay)))
1920 (> (length str) 0)
1921 (gui-set-selection 'SECONDARY str))))
1923 (defun secondary-selection-exist-p ()
1924 "Return non-nil if the secondary selection exists in the current buffer."
1925 (memq mouse-secondary-overlay (overlays-in (point-min) (point-max))))
1927 (defun secondary-selection-to-region ()
1928 "Set beginning and end of the region to those of the secondary selection.
1929 This puts mark and point at the beginning and the end of the
1930 secondary selection, respectively. This works when the secondary
1931 selection exists and the region does not exist in current buffer;
1932 the secondary selection will be deleted afterward.
1933 If the region is active, or the secondary selection doesn't exist,
1934 this function does nothing."
1935 (when (and (not (region-active-p))
1936 (secondary-selection-exist-p))
1937 (let ((beg (overlay-start mouse-secondary-overlay))
1938 (end (overlay-end mouse-secondary-overlay)))
1939 (push-mark beg t t)
1940 (goto-char end))
1941 ;; Delete the secondary selection on current buffer.
1942 (delete-overlay mouse-secondary-overlay)))
1944 (defun secondary-selection-from-region ()
1945 "Set beginning and end of the secondary selection to those of the region.
1946 When there is no region, this function does nothing."
1947 (when (region-active-p) ; Create the secondary selection from the region.
1948 (delete-overlay mouse-secondary-overlay) ; Delete the secondary selection even on a different buffer.
1949 (move-overlay mouse-secondary-overlay (region-beginning) (region-end))))
1952 (defcustom mouse-buffer-menu-maxlen 20
1953 "Number of buffers in one pane (submenu) of the buffer menu.
1954 If we have lots of buffers, divide them into groups of
1955 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1956 :type 'integer
1957 :group 'mouse)
1959 (defcustom mouse-buffer-menu-mode-mult 4
1960 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1961 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1962 will split the buffer menu by the major modes (see
1963 `mouse-buffer-menu-mode-groups') or just by menu length.
1964 Set to 1 (or even 0!) if you want to group by major mode always, and to
1965 a large number if you prefer a mixed multitude. The default is 4."
1966 :type 'integer
1967 :group 'mouse
1968 :version "20.3")
1970 (defvar mouse-buffer-menu-mode-groups
1971 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1972 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1973 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1974 . "Mail/News")
1975 ("\\<C\\>" . "C")
1976 ("ObjC" . "C")
1977 ("Text" . "Text")
1978 ("Outline" . "Text")
1979 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1980 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1981 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1982 . "GDB")
1983 ("Lisp" . "Lisp")))
1984 "How to group various major modes together in \\[mouse-buffer-menu].
1985 Each element has the form (REGEXP . GROUPNAME).
1986 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1988 (defun mouse-buffer-menu (event)
1989 "Pop up a menu of buffers for selection with the mouse.
1990 This switches buffers in the window that you clicked on,
1991 and selects that window."
1992 (interactive "e")
1993 (mouse-minibuffer-check event)
1994 (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1995 (window (posn-window (event-start event))))
1996 (when buf
1997 (select-window
1998 (if (framep window) (frame-selected-window window)
1999 window))
2000 (switch-to-buffer buf))))
2002 (defun mouse-buffer-menu-map ()
2003 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
2004 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
2005 (dolist (buf buffers)
2006 ;; Divide all buffers into buckets for various major modes.
2007 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
2008 (with-current-buffer buf
2009 (let* ((adjusted-major-mode major-mode) elt)
2010 (dolist (group mouse-buffer-menu-mode-groups)
2011 (when (string-match (car group) (format-mode-line mode-name))
2012 (setq adjusted-major-mode (cdr group))))
2013 (setq elt (assoc adjusted-major-mode split-by-major-mode))
2014 (unless elt
2015 (setq elt (list adjusted-major-mode
2016 (if (stringp adjusted-major-mode)
2017 adjusted-major-mode
2018 (format-mode-line mode-name nil nil buf)))
2019 split-by-major-mode (cons elt split-by-major-mode)))
2020 (or (memq buf (cdr (cdr elt)))
2021 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
2022 ;; Compute the sum of squares of sizes of the major-mode buckets.
2023 (let ((tail split-by-major-mode))
2024 (setq sum-of-squares 0)
2025 (while tail
2026 (setq sum-of-squares
2027 (+ sum-of-squares
2028 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
2029 (setq tail (cdr tail))))
2030 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
2031 (* (length buffers) (length buffers)))
2032 ;; Subdividing by major modes really helps, so let's do it.
2033 (let (subdivided-menus (buffers-left (length buffers)))
2034 ;; Sort the list to put the most popular major modes first.
2035 (setq split-by-major-mode
2036 (sort split-by-major-mode
2037 (function (lambda (elt1 elt2)
2038 (> (length elt1) (length elt2))))))
2039 ;; Make a separate submenu for each major mode
2040 ;; that has more than one buffer,
2041 ;; unless all the remaining buffers are less than 1/10 of them.
2042 (while (and split-by-major-mode
2043 (and (> (length (car split-by-major-mode)) 3)
2044 (> (* buffers-left 10) (length buffers))))
2045 (let ((this-mode-list (mouse-buffer-menu-alist
2046 (cdr (cdr (car split-by-major-mode))))))
2047 (and this-mode-list
2048 (setq subdivided-menus
2049 (cons (cons
2050 (nth 1 (car split-by-major-mode))
2051 this-mode-list)
2052 subdivided-menus))))
2053 (setq buffers-left
2054 (- buffers-left (length (cdr (car split-by-major-mode)))))
2055 (setq split-by-major-mode (cdr split-by-major-mode)))
2056 ;; If any major modes are left over,
2057 ;; make a single submenu for them.
2058 (if split-by-major-mode
2059 (let ((others-list
2060 (mouse-buffer-menu-alist
2061 ;; we don't need split-by-major-mode any more,
2062 ;; so we can ditch it with nconc (mapcan).
2063 (mapcan 'cddr split-by-major-mode))))
2064 (and others-list
2065 (setq subdivided-menus
2066 (cons (cons "Others" others-list)
2067 subdivided-menus)))))
2068 (cons "Buffer Menu" (nreverse subdivided-menus)))
2069 (cons "Buffer Menu"
2070 (mouse-buffer-menu-split "Select Buffer"
2071 (mouse-buffer-menu-alist buffers))))))
2073 (defun mouse-buffer-menu-alist (buffers)
2074 (let (tail
2075 (maxlen 0)
2076 head)
2077 (setq buffers
2078 (sort buffers
2079 (function (lambda (elt1 elt2)
2080 (string< (buffer-name elt1) (buffer-name elt2))))))
2081 (setq tail buffers)
2082 (while tail
2083 (or (eq ?\s (aref (buffer-name (car tail)) 0))
2084 (setq maxlen
2085 (max maxlen
2086 (length (buffer-name (car tail))))))
2087 (setq tail (cdr tail)))
2088 (setq tail buffers)
2089 (while tail
2090 (let ((elt (car tail)))
2091 (if (/= (aref (buffer-name elt) 0) ?\s)
2092 (setq head
2093 (cons
2094 (cons
2095 (format
2096 (format "%%-%ds %%s%%s %%s" maxlen)
2097 (buffer-name elt)
2098 (if (buffer-modified-p elt) "*" " ")
2099 (with-current-buffer elt
2100 (if buffer-read-only "%" " "))
2101 (or (buffer-file-name elt)
2102 (with-current-buffer elt
2103 (if list-buffers-directory
2104 (expand-file-name
2105 list-buffers-directory)))
2106 ""))
2107 elt)
2108 head))))
2109 (setq tail (cdr tail)))
2110 ;; Compensate for the reversal that the above loop does.
2111 (nreverse head)))
2113 (defun mouse-buffer-menu-split (title alist)
2114 ;; If we have lots of buffers, divide them into groups of 20
2115 ;; and make a pane (or submenu) for each one.
2116 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
2117 (let ((alist alist) sublists next
2118 (i 1))
2119 (while alist
2120 ;; Pull off the next mouse-buffer-menu-maxlen buffers
2121 ;; and make them the next element of sublist.
2122 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
2123 (if next
2124 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
2125 nil))
2126 (setq sublists (cons (cons (format "Buffers %d" i) alist)
2127 sublists))
2128 (setq i (1+ i))
2129 (setq alist next))
2130 (nreverse sublists))
2131 ;; Few buffers--put them all in one pane.
2132 (list (cons title alist))))
2134 (define-obsolete-function-alias
2135 'mouse-choose-completion 'choose-completion "23.2")
2137 ;; Font selection.
2139 (defun font-menu-add-default ()
2140 (let* ((default (frame-parameter nil 'font))
2141 (font-alist x-fixed-font-alist)
2142 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2143 (if (assoc "Default" elt)
2144 (delete (assoc "Default" elt) elt))
2145 (setcdr elt
2146 (cons (list "Default" default)
2147 (cdr elt)))))
2149 (defvar x-fixed-font-alist
2150 (list
2151 (purecopy "Font Menu")
2152 (cons
2153 (purecopy "Misc")
2154 (mapcar
2155 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2156 ;; For these, we specify the pixel height and width.
2157 '(("fixed" "fixed")
2158 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2159 ("6x12"
2160 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2161 ("6x13"
2162 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2163 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2164 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2165 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2166 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2167 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2168 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2169 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2170 ("")
2171 ("clean 5x8"
2172 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2173 ("clean 6x8"
2174 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2175 ("clean 8x8"
2176 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2177 ("clean 8x10"
2178 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2179 ("clean 8x14"
2180 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2181 ("clean 8x16"
2182 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2183 ("")
2184 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2185 ;; We don't seem to have these; who knows what they are.
2186 ;; ("fg-18" "fg-18")
2187 ;; ("fg-25" "fg-25")
2188 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2189 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2190 ("lucidasanstypewriter-bold-24"
2191 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2192 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2193 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2196 (cons
2197 (purecopy "Courier")
2198 (mapcar
2199 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2200 ;; For these, we specify the point height.
2201 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2202 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2203 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2204 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2205 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2206 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2207 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2208 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2209 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2210 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2211 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2212 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2213 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2214 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2215 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2216 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2217 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2218 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2219 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2220 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2221 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2222 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2223 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2224 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
2225 ))))
2226 "X fonts suitable for use in Emacs.")
2228 (declare-function generate-fontset-menu "fontset" ())
2230 (defun mouse-select-font ()
2231 "Prompt for a font name, using `x-popup-menu', and return it."
2232 (interactive)
2233 (unless (display-multi-font-p)
2234 (error "Cannot change fonts on this display"))
2235 (car
2236 (x-popup-menu
2237 (if (listp last-nonmenu-event)
2238 last-nonmenu-event
2239 (list '(0 0) (selected-window)))
2240 (append x-fixed-font-alist
2241 (list (generate-fontset-menu))))))
2243 (declare-function text-scale-mode "face-remap")
2245 (defun mouse-set-font (&rest fonts)
2246 "Set the default font for the selected frame.
2247 The argument FONTS is a list of font names; the first valid font
2248 in this list is used.
2250 When called interactively, pop up a menu and allow the user to
2251 choose a font."
2252 (interactive
2253 (progn (unless (display-multi-font-p)
2254 (error "Cannot change fonts on this display"))
2255 (x-popup-menu
2256 (if (listp last-nonmenu-event)
2257 last-nonmenu-event
2258 (list '(0 0) (selected-window)))
2259 ;; Append list of fontsets currently defined.
2260 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2261 (if fonts
2262 (let (font)
2263 (while fonts
2264 (condition-case nil
2265 (progn
2266 (set-frame-font (car fonts))
2267 (setq font (car fonts))
2268 (setq fonts nil))
2269 (error
2270 (setq fonts (cdr fonts)))))
2271 (if (null font)
2272 (error "Font not found")))))
2274 (defvar mouse-appearance-menu-map nil)
2275 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
2276 (declare-function buffer-face-mode-invoke "face-remap"
2277 (face arg &optional interactive))
2278 (declare-function font-face-attributes "font.c" (font &optional frame))
2279 (defvar w32-use-w32-font-dialog)
2280 (defvar w32-fixed-font-alist)
2282 (defun mouse-appearance-menu (event)
2283 "Show a menu for changing the default face in the current buffer."
2284 (interactive "@e")
2285 (require 'face-remap)
2286 (when (display-multi-font-p)
2287 (with-selected-window (car (event-start event))
2288 (if mouse-appearance-menu-map
2289 nil ; regenerate new fonts
2290 ;; Initialize mouse-appearance-menu-map
2291 (setq mouse-appearance-menu-map
2292 (make-sparse-keymap "Change Default Buffer Face"))
2293 (define-key mouse-appearance-menu-map [face-remap-reset-base]
2294 '(menu-item "Reset to Default" face-remap-reset-base))
2295 (define-key mouse-appearance-menu-map [text-scale-decrease]
2296 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
2297 (define-key mouse-appearance-menu-map [text-scale-increase]
2298 '(menu-item "Increase Buffer Text Size" text-scale-increase))
2299 ;; Font selector
2300 (if (and (functionp 'x-select-font)
2301 (or (not (boundp 'w32-use-w32-font-dialog))
2302 w32-use-w32-font-dialog))
2303 (define-key mouse-appearance-menu-map [x-select-font]
2304 '(menu-item "Change Buffer Font..." x-select-font))
2305 ;; If the select-font is unavailable, construct a menu.
2306 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
2307 (font-alist (cdr (append
2308 (if (eq system-type 'windows-nt)
2309 w32-fixed-font-alist
2310 x-fixed-font-alist)
2311 (list (generate-fontset-menu))))))
2312 (dolist (family font-alist)
2313 (let* ((submenu-name (car family))
2314 (submenu-map (make-sparse-keymap submenu-name)))
2315 (dolist (font (cdr family))
2316 (let ((font-name (car font))
2317 font-symbol)
2318 (if (string= font-name "")
2319 (define-key submenu-map [space]
2320 '("--"))
2321 (setq font-symbol (intern (cadr font)))
2322 (define-key submenu-map (vector font-symbol)
2323 (list 'menu-item (car font) font-symbol)))))
2324 (define-key font-submenu (vector (intern submenu-name))
2325 (list 'menu-item submenu-name submenu-map))))
2326 (define-key mouse-appearance-menu-map [font-submenu]
2327 (list 'menu-item "Change Text Font" font-submenu)))))
2328 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
2329 (setq choice (nth (1- (length choice)) choice))
2330 (cond ((eq choice 'text-scale-increase)
2331 (text-scale-increase 1))
2332 ((eq choice 'text-scale-decrease)
2333 (text-scale-increase -1))
2334 ((eq choice 'face-remap-reset-base)
2335 (text-scale-mode 0)
2336 (buffer-face-mode 0))
2337 (choice
2338 ;; Either choice == 'x-select-font, or choice is a
2339 ;; symbol whose name is a font.
2340 (let ((font (if (eq choice 'x-select-font)
2341 (x-select-font)
2342 (symbol-name choice))))
2343 (buffer-face-mode-invoke
2344 (if (fontp font 'font-spec)
2345 (list :font font)
2346 (font-face-attributes font))
2347 t (called-interactively-p 'interactive)))))))))
2350 ;; Drag and drop support.
2351 (defcustom mouse-drag-and-drop-region nil
2352 "If non-nil, dragging the mouse drags the region, if it exists.
2353 If the value is a modifier, such as `control' or `shift' or
2354 `meta', then if that modifier key is pressed when dropping the
2355 region, text is copied instead of being cut."
2356 :type `(choice
2357 (const :tag "Disable dragging the region" nil)
2358 ,@(mapcar
2359 (lambda (modifier)
2360 `(const :tag ,(format "Enable, but copy with the %s modifier"
2361 modifier)
2362 modifier))
2363 '(alt super hyper shift control meta))
2364 (other :tag "Enable dragging the region" t))
2365 :version "26.1"
2366 :group 'mouse)
2368 (defcustom mouse-drag-and-drop-region-cut-when-buffers-differ nil
2369 "If non-nil, cut text also when source and destination buffers differ.
2370 If this option is nil, `mouse-drag-and-drop-region' will leave
2371 the text in the source buffer alone when dropping it in a
2372 different buffer. If this is non-nil, it will cut the text just
2373 as it does when dropping text in the source buffer."
2374 :type 'boolean
2375 :version "26.1"
2376 :group 'mouse)
2378 (defcustom mouse-drag-and-drop-region-show-tooltip 256
2379 "If non-nil, text is shown by a tooltip in a graphic display.
2380 If this option is nil, `mouse-drag-and-drop-region' does not show
2381 tooltips. If this is t, it shows the entire text dragged in a
2382 tooltip. If this is an integer (as with the default value of
2383 256), it will show that many characters of the dragged text in
2384 a tooltip."
2385 :type 'integer
2386 :version "26.1"
2387 :group 'mouse)
2389 (defcustom mouse-drag-and-drop-region-show-cursor t
2390 "If non-nil, move point with mouse cursor during dragging.
2391 If this is nil, `mouse-drag-and-drop-region' leaves point alone.
2392 Otherwise, it will move point together with the mouse cursor and,
2393 in addition, temporarily highlight the original region with the
2394 `mouse-drag-and-drop-region' face."
2395 :type 'boolean
2396 :version "26.1"
2397 :group 'mouse)
2399 (defface mouse-drag-and-drop-region '((t :inherit region))
2400 "Face to highlight original text during dragging.
2401 This face is used by `mouse-drag-and-drop-region' to temporarily
2402 highlight the original region when
2403 `mouse-drag-and-drop-region-show-cursor' is non-nil."
2404 :version "26.1"
2405 :group 'mouse)
2407 (defun mouse-drag-and-drop-region (event)
2408 "Move text in the region to point where mouse is dragged to.
2409 The transportation of text is also referred as `drag and drop'.
2410 When text is dragged over to a different buffer, or if a
2411 modifier key was pressed when dropping, and the value of the
2412 variable `mouse-drag-and-drop-region' is that modifier, the text
2413 is copied instead of being cut."
2414 (interactive "e")
2415 (let* ((mouse-button (event-basic-type last-input-event))
2416 (mouse-drag-and-drop-region-show-tooltip
2417 (when (and mouse-drag-and-drop-region-show-tooltip
2418 (display-multi-frame-p)
2419 (require 'tooltip))
2420 mouse-drag-and-drop-region-show-tooltip))
2421 (start (region-beginning))
2422 (end (region-end))
2423 (point (point))
2424 (buffer (current-buffer))
2425 (window (selected-window))
2426 (text-from-read-only buffer-read-only)
2427 (mouse-drag-and-drop-overlay (make-overlay start end))
2428 point-to-paste
2429 point-to-paste-read-only
2430 window-to-paste
2431 buffer-to-paste
2432 cursor-in-text-area
2433 no-modifier-on-drop
2434 drag-but-negligible
2435 clicked
2436 value-selection ; This remains nil when event was "click".
2437 text-tooltip
2438 states
2439 window-exempt)
2441 ;; STATES stores for each window on this frame its start and point
2442 ;; positions so we can restore them on all windows but for the one
2443 ;; where the drop occurs. For inter-frame drags we'll have to do
2444 ;; this for all windows on all visible frames. In addition we save
2445 ;; also the cursor type for the window's buffer so we can restore it
2446 ;; in case we modified it.
2447 ;; https://lists.gnu.org/archive/html/emacs-devel/2017-12/msg00090.html
2448 (walk-window-tree
2449 (lambda (window)
2450 (setq states
2451 (cons
2452 (list
2453 window
2454 (copy-marker (window-start window))
2455 (copy-marker (window-point window))
2456 (with-current-buffer (window-buffer window)
2457 cursor-type))
2458 states))))
2460 (ignore-errors
2461 (track-mouse
2462 ;; When event was "click" instead of "drag", skip loop.
2463 (while (progn
2464 (setq event (read-key)) ; read-event or read-key
2465 (or (mouse-movement-p event)
2466 ;; Handle `mouse-autoselect-window'.
2467 (eq (car-safe event) 'select-window)))
2468 ;; Obtain the dragged text in region. When the loop was
2469 ;; skipped, value-selection remains nil.
2470 (unless value-selection
2471 (setq value-selection (buffer-substring start end))
2472 (when mouse-drag-and-drop-region-show-tooltip
2473 (let ((text-size mouse-drag-and-drop-region-show-tooltip))
2474 (setq text-tooltip
2475 (if (and (integerp text-size)
2476 (> (length value-selection) text-size))
2477 (concat
2478 (substring value-selection 0 (/ text-size 2))
2479 "\n...\n"
2480 (substring value-selection (- (/ text-size 2)) -1))
2481 value-selection))))
2483 ;; Check if selected text is read-only.
2484 (setq text-from-read-only (or text-from-read-only
2485 (get-text-property start 'read-only)
2486 (not (equal
2487 (next-single-char-property-change
2488 start 'read-only nil end)
2489 end)))))
2490 (setq window-to-paste (posn-window (event-end event)))
2491 (setq point-to-paste (posn-point (event-end event)))
2492 ;; Set nil when target buffer is minibuffer.
2493 (setq buffer-to-paste (let (buf)
2494 (when (windowp window-to-paste)
2495 (setq buf (window-buffer window-to-paste))
2496 (when (not (minibufferp buf))
2497 buf))))
2498 (setq cursor-in-text-area (and window-to-paste
2499 point-to-paste
2500 buffer-to-paste))
2502 (when cursor-in-text-area
2503 ;; Check if point under mouse is read-only.
2504 (save-window-excursion
2505 (select-window window-to-paste)
2506 (setq point-to-paste-read-only
2507 (or buffer-read-only
2508 (get-text-property point-to-paste 'read-only))))
2510 ;; Check if "drag but negligible". Operation "drag but
2511 ;; negligible" is defined as drag-and-drop the text to
2512 ;; the original region. When modifier is pressed, the
2513 ;; text will be inserted to inside of the original
2514 ;; region.
2515 (setq drag-but-negligible
2516 (and (eq (overlay-buffer mouse-drag-and-drop-overlay)
2517 buffer-to-paste)
2518 (<= (overlay-start mouse-drag-and-drop-overlay)
2519 point-to-paste)
2520 (<= point-to-paste
2521 (overlay-end mouse-drag-and-drop-overlay)))))
2523 ;; Show a tooltip.
2524 (if mouse-drag-and-drop-region-show-tooltip
2525 (tooltip-show text-tooltip)
2526 (tooltip-hide))
2528 ;; Show cursor and highlight the original region.
2529 (when mouse-drag-and-drop-region-show-cursor
2530 ;; Modify cursor even when point is out of frame.
2531 (setq cursor-type (cond
2532 ((not cursor-in-text-area)
2533 nil)
2534 ((or point-to-paste-read-only
2535 drag-but-negligible)
2536 'hollow)
2538 'bar)))
2539 (when cursor-in-text-area
2540 (overlay-put mouse-drag-and-drop-overlay
2541 'face 'mouse-drag-and-drop-region)
2542 (deactivate-mark) ; Maintain region in other window.
2543 (mouse-set-point event)))))
2545 ;; Hide a tooltip.
2546 (when mouse-drag-and-drop-region-show-tooltip (tooltip-hide))
2548 ;; Check if modifier was pressed on drop.
2549 (setq no-modifier-on-drop
2550 (not (member mouse-drag-and-drop-region (event-modifiers event))))
2552 ;; Check if event was "click".
2553 (setq clicked (not value-selection))
2555 ;; Restore status on drag to outside of text-area or non-mouse input.
2556 (when (or (not cursor-in-text-area)
2557 (not (equal (event-basic-type event) mouse-button)))
2558 (setq drag-but-negligible t
2559 no-modifier-on-drop t))
2561 ;; Do not modify any buffers when event is "click",
2562 ;; "drag but negligible", or "drag to read-only".
2563 (let* ((mouse-drag-and-drop-region-cut-when-buffers-differ
2564 (if no-modifier-on-drop
2565 mouse-drag-and-drop-region-cut-when-buffers-differ
2566 (not mouse-drag-and-drop-region-cut-when-buffers-differ)))
2567 (wanna-paste-to-same-buffer (equal buffer-to-paste buffer))
2568 (wanna-cut-on-same-buffer (and wanna-paste-to-same-buffer
2569 no-modifier-on-drop))
2570 (wanna-cut-on-other-buffer
2571 (and (not wanna-paste-to-same-buffer)
2572 mouse-drag-and-drop-region-cut-when-buffers-differ))
2573 (cannot-paste (or point-to-paste-read-only
2574 (when (or wanna-cut-on-same-buffer
2575 wanna-cut-on-other-buffer)
2576 text-from-read-only))))
2578 (cond
2579 ;; Move point within region.
2580 (clicked
2581 (deactivate-mark)
2582 (mouse-set-point event))
2583 ;; Undo operation. Set back the original text as region.
2584 ((or (and drag-but-negligible
2585 no-modifier-on-drop)
2586 cannot-paste)
2587 ;; Inform user either source or destination buffer cannot be modified.
2588 (when (and (not drag-but-negligible)
2589 cannot-paste)
2590 (message "Buffer is read-only"))
2592 ;; Select source window back and restore region.
2593 ;; (set-window-point window point)
2594 (select-window window)
2595 (goto-char point)
2596 (setq deactivate-mark nil)
2597 (activate-mark))
2598 ;; Modify buffers.
2600 ;; * DESTINATION BUFFER::
2601 ;; Insert the text to destination buffer under mouse.
2602 (select-window window-to-paste)
2603 (setq window-exempt window-to-paste)
2604 (goto-char point-to-paste)
2605 (push-mark)
2606 (insert value-selection)
2607 ;; On success, set the text as region on destination buffer.
2608 (when (not (equal (mark) (point)))
2609 (setq deactivate-mark nil)
2610 (activate-mark))
2612 ;; * SOURCE BUFFER::
2613 ;; Set back the original text as region or delete the original
2614 ;; text, on source buffer.
2615 (if wanna-paste-to-same-buffer
2616 ;; When source buffer and destination buffer are the same,
2617 ;; remove the original text.
2618 (when no-modifier-on-drop
2619 (let (deactivate-mark)
2620 (delete-region (overlay-start mouse-drag-and-drop-overlay)
2621 (overlay-end mouse-drag-and-drop-overlay))))
2622 ;; When source buffer and destination buffer are different,
2623 ;; keep (set back the original text as region) or remove the
2624 ;; original text.
2625 (select-window window) ; Select window with source buffer.
2626 (goto-char point) ; Move point to the original text on source buffer.
2628 (if mouse-drag-and-drop-region-cut-when-buffers-differ
2629 ;; Remove the dragged text from source buffer like
2630 ;; operation `cut'.
2631 (delete-region (overlay-start mouse-drag-and-drop-overlay)
2632 (overlay-end mouse-drag-and-drop-overlay))
2633 ;; Set back the dragged text as region on source buffer
2634 ;; like operation `copy'.
2635 (activate-mark))
2636 (select-window window-to-paste))))))
2638 ;; Clean up.
2639 (delete-overlay mouse-drag-and-drop-overlay)
2641 ;; Restore old states but for the window where the drop
2642 ;; occurred. Restore cursor types for all windows.
2643 (dolist (state states)
2644 (let ((window (car state)))
2645 (when (and window-exempt
2646 (not (eq window window-exempt)))
2647 (set-window-start window (nth 1 state) 'noforce)
2648 (set-marker (nth 1 state) nil)
2649 ;; If window is selected, the following automatically sets
2650 ;; point for that window's buffer.
2651 (set-window-point window (nth 2 state))
2652 (set-marker (nth 2 state) nil))
2653 (with-current-buffer (window-buffer window)
2654 (setq cursor-type (nth 3 state)))))))
2657 ;;; Bindings for mouse commands.
2659 (global-set-key [down-mouse-1] 'mouse-drag-region)
2660 (global-set-key [mouse-1] 'mouse-set-point)
2661 (global-set-key [drag-mouse-1] 'mouse-set-region)
2663 (defun mouse--strip-first-event (_prompt)
2664 (substring (this-single-command-raw-keys) 1))
2666 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
2667 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
2669 (global-set-key [mouse-2] 'mouse-yank-primary)
2670 ;; Allow yanking also when the corresponding cursor is "in the fringe".
2671 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
2672 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
2673 (global-set-key [mouse-3] 'mouse-save-then-kill)
2674 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
2675 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
2677 ;; By binding these to down-going events, we let the user use the up-going
2678 ;; event to make the selection, saving a click.
2679 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2680 (if (not (eq system-type 'ms-dos))
2681 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
2682 ;; C-down-mouse-2 is bound in facemenu.el.
2683 (global-set-key [C-down-mouse-3]
2684 `(menu-item ,(purecopy "Menu Bar") ignore
2685 :filter (lambda (_)
2686 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
2687 (mouse-menu-bar-map)
2688 (mouse-menu-major-mode-map)))))
2690 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2691 ;; vertical-line prevents Emacs from signaling an error when the mouse
2692 ;; button is released after dragging these lines, on non-toolkit
2693 ;; versions.
2694 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2695 (global-set-key [header-line mouse-1] 'mouse-select-window)
2696 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2697 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2698 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2699 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2700 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2701 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2702 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2703 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
2704 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2705 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2706 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2707 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
2708 (global-set-key [right-divider mouse-1] 'ignore)
2709 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
2710 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
2711 (global-set-key [bottom-divider mouse-1] 'ignore)
2712 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
2713 (global-set-key [left-edge down-mouse-1] 'mouse-drag-left-edge)
2714 (global-set-key [left-edge mouse-1] 'ignore)
2715 (global-set-key [top-left-corner down-mouse-1] 'mouse-drag-top-left-corner)
2716 (global-set-key [top-left-corner mouse-1] 'ignore)
2717 (global-set-key [top-edge down-mouse-1] 'mouse-drag-top-edge)
2718 (global-set-key [top-edge mouse-1] 'ignore)
2719 (global-set-key [top-right-corner down-mouse-1] 'mouse-drag-top-right-corner)
2720 (global-set-key [top-right-corner mouse-1] 'ignore)
2721 (global-set-key [right-edge down-mouse-1] 'mouse-drag-right-edge)
2722 (global-set-key [right-edge mouse-1] 'ignore)
2723 (global-set-key [bottom-right-corner down-mouse-1] 'mouse-drag-bottom-right-corner)
2724 (global-set-key [bottom-right-corner mouse-1] 'ignore)
2725 (global-set-key [bottom-edge down-mouse-1] 'mouse-drag-bottom-edge)
2726 (global-set-key [bottom-edge mouse-1] 'ignore)
2727 (global-set-key [bottom-left-corner down-mouse-1] 'mouse-drag-bottom-left-corner)
2728 (global-set-key [bottom-left-corner mouse-1] 'ignore)
2730 (provide 'mouse)
2732 ;;; mouse.el ends here