Add isearch-yank-symbol-or-char
[emacs.git] / lisp / mouse.el
blob6a98ee7353f4971dd072fbd9f537125f88389044
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 (defvar mouse--last-down nil)
101 (defun mouse--down-1-maybe-follows-link (&optional _prompt)
102 (when mouse-1-click-follows-link
103 (setq mouse--last-down (cons (car-safe last-input-event) (float-time))))
104 nil)
106 (defun mouse--click-1-maybe-follows-link (&optional _prompt)
107 "Turn `mouse-1' events into `mouse-2' events if follows-link.
108 Expects to be bound to `(double-)mouse-1' in `key-translation-map'."
109 (and mouse--last-down
110 (pcase mouse-1-click-follows-link
111 ('nil nil)
112 ('double (eq 'double-mouse-1 (car-safe last-input-event)))
113 (_ (and (eq 'mouse-1 (car-safe last-input-event))
114 (or (not (numberp mouse-1-click-follows-link))
115 (funcall (if (< mouse-1-click-follows-link 0) #'> #'<)
116 (- (float-time) (cdr mouse--last-down))
117 (/ (abs mouse-1-click-follows-link) 1000.0))))))
118 (eq (car mouse--last-down)
119 (event-convert-list (list 'down (car-safe last-input-event))))
120 (let* ((action (mouse-on-link-p (event-start last-input-event))))
121 (when (and action
122 (or mouse-1-click-in-non-selected-windows
123 (eq (selected-window)
124 (posn-window (event-start last-input-event)))))
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 (arrayp action)
129 (vector (aref action 0))
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
137 (get (car last-input-event) 'event-kind)))
138 (vector (cons newup (cdr last-input-event)))))))))
140 (define-key key-translation-map [down-mouse-1]
141 #'mouse--down-1-maybe-follows-link)
142 (define-key key-translation-map [double-down-mouse-1]
143 #'mouse--down-1-maybe-follows-link)
144 (define-key key-translation-map [mouse-1]
145 #'mouse--click-1-maybe-follows-link)
146 (define-key key-translation-map [double-mouse-1]
147 #'mouse--click-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 an array, the mouse-1 event is translated into the
1155 first element of that array, i.e. the action of the mouse-1
1156 click is the local or global binding of that event.
1158 - Otherwise, the mouse-1 event is translated into a mouse-2 event
1159 at the same position."
1160 (let ((action
1161 (and (or (not (consp pos))
1162 mouse-1-click-in-non-selected-windows
1163 (eq (selected-window) (posn-window pos)))
1164 (or (mouse-posn-property pos 'follow-link)
1165 (let ((area (posn-area pos)))
1166 (when area
1167 (key-binding (vector area 'follow-link) nil t pos)))
1168 (key-binding [follow-link] nil t pos)))))
1169 (cond
1170 ((eq action 'mouse-face)
1171 (and (mouse-posn-property pos 'mouse-face) t))
1172 ((functionp action)
1173 ;; FIXME: This seems questionable if the click is not in a buffer.
1174 ;; Should we instead decide that `action' takes a `posn'?
1175 (if (consp pos)
1176 (with-current-buffer (window-buffer (posn-window pos))
1177 (funcall action (posn-point pos)))
1178 (funcall action pos)))
1179 (t action))))
1181 (defun mouse-fixup-help-message (msg)
1182 "Fix help message MSG for `mouse-1-click-follows-link'."
1183 (let (mp pos)
1184 (if (and mouse-1-click-follows-link
1185 (stringp msg)
1186 (string-match-p "\\`mouse-2" msg)
1187 (setq mp (mouse-pixel-position))
1188 (consp (setq pos (cdr mp)))
1189 (car pos) (>= (car pos) 0)
1190 (cdr pos) (>= (cdr pos) 0)
1191 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
1192 (windowp (posn-window pos)))
1193 (with-current-buffer (window-buffer (posn-window pos))
1194 (if (mouse-on-link-p pos)
1195 (setq msg (concat
1196 (cond
1197 ((eq mouse-1-click-follows-link 'double) "double-")
1198 ((and (integerp mouse-1-click-follows-link)
1199 (< mouse-1-click-follows-link 0)) "Long ")
1200 (t ""))
1201 "mouse-1" (substring msg 7)))))))
1202 msg)
1204 (defun mouse-drag-track (start-event)
1205 "Track mouse drags by highlighting area between point and cursor.
1206 The region will be defined with mark and point."
1207 (mouse-minibuffer-check start-event)
1208 (setq mouse-selection-click-count-buffer (current-buffer))
1209 (deactivate-mark)
1210 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
1211 (start-posn (event-start start-event))
1212 (start-point (posn-point start-posn))
1213 (start-window (posn-window start-posn))
1214 (_ (with-current-buffer (window-buffer start-window)
1215 (setq deactivate-mark nil)))
1216 ;; We've recorded what we needed from the current buffer and
1217 ;; window, now let's jump to the place of the event, where things
1218 ;; are happening.
1219 (_ (mouse-set-point start-event))
1220 (echo-keystrokes 0)
1221 (bounds (window-edges start-window))
1222 (make-cursor-line-fully-visible nil)
1223 (top (nth 1 bounds))
1224 (bottom (if (window-minibuffer-p start-window)
1225 (nth 3 bounds)
1226 ;; Don't count the mode line.
1227 (1- (nth 3 bounds))))
1228 (click-count (1- (event-click-count start-event)))
1229 ;; Suppress automatic hscrolling, because that is a nuisance
1230 ;; when setting point near the right fringe (but see below).
1231 (auto-hscroll-mode-saved auto-hscroll-mode)
1232 (old-track-mouse track-mouse))
1234 (setq mouse-selection-click-count click-count)
1235 ;; In case the down click is in the middle of some intangible text,
1236 ;; use the end of that text, and put it in START-POINT.
1237 (if (< (point) start-point)
1238 (goto-char start-point))
1239 (setq start-point (point))
1241 ;; Activate the region, using `mouse-start-end' to determine where
1242 ;; to put point and mark (e.g., double-click will select a word).
1243 (setq-local transient-mark-mode
1244 (if (eq transient-mark-mode 'lambda)
1245 '(only)
1246 (cons 'only transient-mark-mode)))
1247 (let ((range (mouse-start-end start-point start-point click-count)))
1248 (push-mark (nth 0 range) t t)
1249 (goto-char (nth 1 range)))
1251 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
1252 (setq track-mouse t)
1253 (setq auto-hscroll-mode nil)
1255 (set-transient-map
1256 (let ((map (make-sparse-keymap)))
1257 (define-key map [switch-frame] #'ignore)
1258 (define-key map [select-window] #'ignore)
1259 (define-key map [mouse-movement]
1260 (lambda (event) (interactive "e")
1261 (let* ((end (event-end event))
1262 (end-point (posn-point end)))
1263 (unless (eq end-point start-point)
1264 ;; As soon as the user moves, we can re-enable auto-hscroll.
1265 (setq auto-hscroll-mode auto-hscroll-mode-saved)
1266 ;; And remember that we have moved, so mouse-set-region can know
1267 ;; its event is really a drag event.
1268 (setcar start-event 'mouse-movement))
1269 (if (and (eq (posn-window end) start-window)
1270 (integer-or-marker-p end-point))
1271 (mouse--drag-set-mark-and-point start-point
1272 end-point click-count)
1273 (let ((mouse-row (cdr (cdr (mouse-position)))))
1274 (cond
1275 ((null mouse-row))
1276 ((< mouse-row top)
1277 (mouse-scroll-subr start-window (- mouse-row top)
1278 nil start-point))
1279 ((>= mouse-row bottom)
1280 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1281 nil start-point))))))))
1282 map)
1283 t (lambda ()
1284 (setq track-mouse old-track-mouse)
1285 (setq auto-hscroll-mode auto-hscroll-mode-saved)
1286 (deactivate-mark)
1287 (pop-mark)))))
1289 (defun mouse--drag-set-mark-and-point (start click click-count)
1290 (let* ((range (mouse-start-end start click click-count))
1291 (beg (nth 0 range))
1292 (end (nth 1 range)))
1293 (cond ((eq (mark) beg)
1294 (goto-char end))
1295 ((eq (mark) end)
1296 (goto-char beg))
1297 ((< click (mark))
1298 (set-mark end)
1299 (goto-char beg))
1301 (set-mark beg)
1302 (goto-char end)))))
1304 ;; Commands to handle xterm-style multiple clicks.
1305 (defun mouse-skip-word (dir)
1306 "Skip over word, over whitespace, or over identical punctuation.
1307 If DIR is positive skip forward; if negative, skip backward."
1308 (let* ((char (following-char))
1309 (syntax (char-to-string (char-syntax char))))
1310 (cond ((string= syntax "w")
1311 ;; Here, we can't use skip-syntax-forward/backward because
1312 ;; they don't pay attention to word-separating-categories,
1313 ;; and thus they will skip over a true word boundary. So,
1314 ;; we simulate the original behavior by using forward-word.
1315 (if (< dir 0)
1316 (if (not (looking-at "\\<"))
1317 (forward-word -1))
1318 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1319 (forward-word 1))))
1320 ((string= syntax " ")
1321 (if (< dir 0)
1322 (skip-syntax-backward syntax)
1323 (skip-syntax-forward syntax)))
1324 ((string= syntax "_")
1325 (if (< dir 0)
1326 (skip-syntax-backward "w_")
1327 (skip-syntax-forward "w_")))
1328 ((< dir 0)
1329 (while (and (not (bobp)) (= (preceding-char) char))
1330 (forward-char -1)))
1332 (while (and (not (eobp)) (= (following-char) char))
1333 (forward-char 1))))))
1335 (defun mouse-start-end (start end mode)
1336 "Return a list of region bounds based on START and END according to MODE.
1337 If MODE is 0 then set point to (min START END), mark to (max START END).
1338 If MODE is 1 then set point to start of word at (min START END),
1339 mark to end of word at (max START END).
1340 If MODE is 2 then do the same for lines."
1341 (if (> start end)
1342 (let ((temp start))
1343 (setq start end
1344 end temp)))
1345 (setq mode (mod mode 3))
1346 (cond ((= mode 0)
1347 (list start end))
1348 ((and (= mode 1)
1349 (= start end)
1350 (char-after start)
1351 (= (char-syntax (char-after start)) ?\())
1352 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
1353 ;; This happens in CC Mode when unbalanced parens in CPP
1354 ;; constructs are given punctuation syntax with
1355 ;; syntax-table text properties. (2016-02-21).
1356 (signal 'scan-error (list "Containing expression ends prematurely"
1357 start start))
1358 (list start
1359 (save-excursion
1360 (goto-char start)
1361 (forward-sexp 1)
1362 (point)))))
1363 ((and (= mode 1)
1364 (= start end)
1365 (char-after start)
1366 (= (char-syntax (char-after start)) ?\)))
1367 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
1368 ;; See above comment about CC Mode.
1369 (signal 'scan-error (list "Unbalanced parentheses" start start))
1370 (list (save-excursion
1371 (goto-char (1+ start))
1372 (backward-sexp 1)
1373 (point))
1374 (1+ start))))
1375 ((and (= mode 1)
1376 (= start end)
1377 (char-after start)
1378 (= (char-syntax (char-after start)) ?\"))
1379 (let ((open (or (eq start (point-min))
1380 (save-excursion
1381 (goto-char (- start 1))
1382 (looking-at "\\s(\\|\\s \\|\\s>")))))
1383 (if open
1384 (list start
1385 (save-excursion
1386 (condition-case nil
1387 (progn
1388 (goto-char start)
1389 (forward-sexp 1)
1390 (point))
1391 (error end))))
1392 (list (save-excursion
1393 (condition-case nil
1394 (progn
1395 (goto-char (1+ start))
1396 (backward-sexp 1)
1397 (point))
1398 (error end)))
1399 (1+ start)))))
1400 ((= mode 1)
1401 (list (save-excursion
1402 (goto-char start)
1403 (mouse-skip-word -1)
1404 (point))
1405 (save-excursion
1406 (goto-char end)
1407 (mouse-skip-word 1)
1408 (point))))
1409 ((= mode 2)
1410 (list (save-excursion
1411 (goto-char start)
1412 (line-beginning-position 1))
1413 (save-excursion
1414 (goto-char end)
1415 (forward-line 1)
1416 (point))))))
1418 ;; Subroutine: set the mark where CLICK happened,
1419 ;; but don't do anything else.
1420 (defun mouse-set-mark-fast (click)
1421 (mouse-minibuffer-check click)
1422 (let ((posn (event-start click)))
1423 (select-window (posn-window posn))
1424 (if (numberp (posn-point posn))
1425 (push-mark (posn-point posn) t t))))
1427 (defun mouse-undouble-last-event (events)
1428 (let* ((index (1- (length events)))
1429 (last (nthcdr index events))
1430 (event (car last))
1431 (basic (event-basic-type event))
1432 (old-modifiers (event-modifiers event))
1433 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1434 (new
1435 (if (consp event)
1436 ;; Use reverse, not nreverse, since event-modifiers
1437 ;; does not copy the list it returns.
1438 (cons (event-convert-list (reverse (cons basic modifiers)))
1439 (cdr event))
1440 event)))
1441 (setcar last new)
1442 (if (and (not (equal modifiers old-modifiers))
1443 (key-binding (apply 'vector events)))
1445 (setcar last event)
1446 nil)))
1448 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1450 (defun mouse-set-mark (click)
1451 "Set mark at the position clicked on with the mouse.
1452 Display cursor at that position for a second.
1453 This must be bound to a mouse click."
1454 (interactive "e")
1455 (mouse-minibuffer-check click)
1456 (select-window (posn-window (event-start click)))
1457 ;; FIXME: Use save-excursion
1458 (let ((point-save (point)))
1459 (unwind-protect
1460 (progn (mouse-set-point click)
1461 (push-mark nil t t)
1462 (or transient-mark-mode
1463 (sit-for 1)))
1464 (goto-char point-save))))
1466 (defun mouse-kill (click)
1467 "Kill the region between point and the mouse click.
1468 The text is saved in the kill ring, as with \\[kill-region]."
1469 (interactive "e")
1470 (mouse-minibuffer-check click)
1471 (let* ((posn (event-start click))
1472 (click-posn (posn-point posn)))
1473 (select-window (posn-window posn))
1474 (if (numberp click-posn)
1475 (kill-region (min (point) click-posn)
1476 (max (point) click-posn)))))
1478 (defun mouse-yank-at-click (click arg)
1479 "Insert the last stretch of killed text at the position clicked on.
1480 Also move point to one end of the text thus inserted (normally the end),
1481 and set mark at the beginning.
1482 Prefix arguments are interpreted as with \\[yank].
1483 If `mouse-yank-at-point' is non-nil, insert at point
1484 regardless of where you click."
1485 (interactive "e\nP")
1486 ;; Give temporary modes such as isearch a chance to turn off.
1487 (run-hooks 'mouse-leave-buffer-hook)
1488 (when select-active-regions
1489 ;; Without this, confusing things happen upon e.g. inserting into
1490 ;; the middle of an active region.
1491 (deactivate-mark))
1492 (or mouse-yank-at-point (mouse-set-point click))
1493 (setq this-command 'yank)
1494 (setq mouse-selection-click-count 0)
1495 (yank arg))
1497 (defun mouse-yank-primary (click)
1498 "Insert the primary selection at the position clicked on.
1499 Move point to the end of the inserted text, and set mark at
1500 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1501 regardless of where you click."
1502 (interactive "e")
1503 ;; Give temporary modes such as isearch a chance to turn off.
1504 (run-hooks 'mouse-leave-buffer-hook)
1505 ;; Without this, confusing things happen upon e.g. inserting into
1506 ;; the middle of an active region.
1507 (when select-active-regions
1508 (let (select-active-regions)
1509 (deactivate-mark)))
1510 (or mouse-yank-at-point (mouse-set-point click))
1511 (let ((primary (gui-get-primary-selection)))
1512 (push-mark)
1513 (insert-for-yank primary)))
1515 (defun mouse-kill-ring-save (click)
1516 "Copy the region between point and the mouse click in the kill ring.
1517 This does not delete the region; it acts like \\[kill-ring-save]."
1518 (interactive "e")
1519 (mouse-set-mark-fast click)
1520 (let (this-command last-command)
1521 (kill-ring-save (point) (mark t))))
1523 ;; This function used to delete the text between point and the mouse
1524 ;; whenever it was equal to the front of the kill ring, but some
1525 ;; people found that confusing.
1527 ;; The position of the last invocation of `mouse-save-then-kill'.
1528 (defvar mouse-save-then-kill-posn nil)
1530 (defun mouse-save-then-kill-delete-region (beg end)
1531 ;; We must make our own undo boundaries
1532 ;; because they happen automatically only for the current buffer.
1533 (undo-boundary)
1534 (if (or (= beg end) (eq buffer-undo-list t))
1535 ;; If we have no undo list in this buffer,
1536 ;; just delete.
1537 (delete-region beg end)
1538 ;; Delete, but make the undo-list entry share with the kill ring.
1539 ;; First, delete just one char, so in case buffer is being modified
1540 ;; for the first time, the undo list records that fact.
1541 (let ((inhibit-modification-hooks t))
1542 (delete-region beg
1543 (+ beg (if (> end beg) 1 -1))))
1544 (let ((buffer-undo-list buffer-undo-list))
1545 ;; Undo that deletion--but don't change the undo list!
1546 (let ((inhibit-modification-hooks t))
1547 (primitive-undo 1 buffer-undo-list))
1548 ;; Now delete the rest of the specified region,
1549 ;; but don't record it.
1550 (setq buffer-undo-list t)
1551 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1552 (error "Lossage in mouse-save-then-kill-delete-region"))
1553 (delete-region beg end))
1554 (let ((tail buffer-undo-list))
1555 ;; Search back in buffer-undo-list for the string
1556 ;; that came from deleting one character.
1557 (while (and tail (not (stringp (car (car tail)))))
1558 (setq tail (cdr tail)))
1559 ;; Replace it with an entry for the entire deleted text.
1560 (and tail
1561 (setcar tail (cons (car kill-ring) (min beg end))))))
1562 (undo-boundary))
1564 (defun mouse-save-then-kill (click)
1565 "Set the region according to CLICK; the second time, kill it.
1566 CLICK should be a mouse click event.
1568 If the region is inactive, activate it temporarily. Set mark at
1569 the original point, and move point to the position of CLICK.
1571 If the region is already active, adjust it. Normally, do this by
1572 moving point or mark, whichever is closer, to CLICK. But if you
1573 have selected whole words or lines, move point or mark to the
1574 word or line boundary closest to CLICK instead.
1576 If `mouse-drag-copy-region' is non-nil, this command also saves the
1577 new region to the kill ring (replacing the previous kill if the
1578 previous region was just saved to the kill ring).
1580 If this command is called a second consecutive time with the same
1581 CLICK position, kill the region (or delete it
1582 if `mouse-drag-copy-region' is non-nil)"
1583 (interactive "e")
1584 (mouse-minibuffer-check click)
1585 (let* ((posn (event-start click))
1586 (click-pt (posn-point posn))
1587 (window (posn-window posn))
1588 (buf (window-buffer window))
1589 ;; Don't let a subsequent kill command append to this one.
1590 (this-command this-command)
1591 ;; Check if the user has multi-clicked to select words/lines.
1592 (click-count
1593 (if (and (eq mouse-selection-click-count-buffer buf)
1594 (with-current-buffer buf (mark t)))
1595 mouse-selection-click-count
1596 0)))
1597 (cond
1598 ((not (numberp click-pt)) nil)
1599 ;; If the user clicked without moving point, kill the region.
1600 ;; This also resets `mouse-selection-click-count'.
1601 ((and (eq last-command 'mouse-save-then-kill)
1602 (eq click-pt mouse-save-then-kill-posn)
1603 (eq window (selected-window)))
1604 (if mouse-drag-copy-region
1605 ;; Region already saved in the previous click;
1606 ;; don't make a duplicate entry, just delete.
1607 (delete-region (mark t) (point))
1608 (kill-region (mark t) (point)))
1609 (setq mouse-selection-click-count 0)
1610 (setq mouse-save-then-kill-posn nil))
1612 ;; Otherwise, if there is a suitable region, adjust it by moving
1613 ;; one end (whichever is closer) to CLICK-PT.
1614 ((or (with-current-buffer buf (region-active-p))
1615 (and (eq window (selected-window))
1616 (mark t)
1617 (or (and (eq last-command 'mouse-save-then-kill)
1618 mouse-save-then-kill-posn)
1619 (and (memq last-command '(mouse-drag-region
1620 mouse-set-region))
1621 (or mark-even-if-inactive
1622 (not transient-mark-mode))))))
1623 (select-window window)
1624 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1625 (if (< (abs (- click-pt (mark t)))
1626 (abs (- click-pt (point))))
1627 (set-mark (car range))
1628 (goto-char (nth 1 range)))
1629 (setq deactivate-mark nil)
1630 (mouse-set-region-1)
1631 (when mouse-drag-copy-region
1632 ;; Region already copied to kill-ring once, so replace.
1633 (kill-new (filter-buffer-substring (mark t) (point)) t))
1634 ;; Arrange for a repeated mouse-3 to kill the region.
1635 (setq mouse-save-then-kill-posn click-pt)))
1637 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1639 (select-window window)
1640 (mouse-set-mark-fast click)
1641 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1642 (if before-scroll (goto-char before-scroll)))
1643 (exchange-point-and-mark)
1644 (mouse-set-region-1)
1645 (when mouse-drag-copy-region
1646 (kill-new (filter-buffer-substring (mark t) (point))))
1647 (setq mouse-save-then-kill-posn click-pt)))))
1650 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1651 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1652 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1653 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1654 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1656 (defconst mouse-secondary-overlay
1657 (let ((ol (make-overlay (point-min) (point-min))))
1658 (delete-overlay ol)
1659 (overlay-put ol 'face 'secondary-selection)
1661 "An overlay which records the current secondary selection.
1662 It is deleted when there is no secondary selection.")
1664 (defvar mouse-secondary-click-count 0)
1666 ;; A marker which records the specified first end for a secondary selection.
1667 ;; May be nil.
1668 (defvar mouse-secondary-start nil)
1670 (defun mouse-start-secondary (click)
1671 "Set one end of the secondary selection to the position clicked on.
1672 Use \\[mouse-secondary-save-then-kill] to set the other end
1673 and complete the secondary selection."
1674 (interactive "e")
1675 (mouse-minibuffer-check click)
1676 (let ((posn (event-start click)))
1677 (with-current-buffer (window-buffer (posn-window posn))
1678 ;; Cancel any preexisting secondary selection.
1679 (delete-overlay mouse-secondary-overlay)
1680 (if (numberp (posn-point posn))
1681 (progn
1682 (or mouse-secondary-start
1683 (setq mouse-secondary-start (make-marker)))
1684 (move-marker mouse-secondary-start (posn-point posn)))))))
1686 (defun mouse-set-secondary (click)
1687 "Set the secondary selection to the text that the mouse is dragged over.
1688 This must be bound to a mouse drag event."
1689 (interactive "e")
1690 (mouse-minibuffer-check click)
1691 (let ((posn (event-start click))
1693 (end (event-end click)))
1694 (with-current-buffer (window-buffer (posn-window posn))
1695 (if (numberp (posn-point posn))
1696 (setq beg (posn-point posn)))
1697 (move-overlay mouse-secondary-overlay beg (posn-point end))
1698 (gui-set-selection
1699 'SECONDARY
1700 (buffer-substring (overlay-start mouse-secondary-overlay)
1701 (overlay-end mouse-secondary-overlay))))))
1703 (defun mouse-drag-secondary (start-event)
1704 "Set the secondary selection to the text that the mouse is dragged over.
1705 Highlight the drag area as you move the mouse.
1706 This must be bound to a button-down mouse event.
1707 The function returns a non-nil value if it creates a secondary selection."
1708 (interactive "e")
1709 (mouse-minibuffer-check start-event)
1710 (let* ((echo-keystrokes 0)
1711 (start-posn (event-start start-event))
1712 (start-point (posn-point start-posn))
1713 (start-window (posn-window start-posn))
1714 (bounds (window-edges start-window))
1715 (top (nth 1 bounds))
1716 (bottom (if (window-minibuffer-p start-window)
1717 (nth 3 bounds)
1718 ;; Don't count the mode line.
1719 (1- (nth 3 bounds))))
1720 (click-count (1- (event-click-count start-event))))
1721 (with-current-buffer (window-buffer start-window)
1722 (setq mouse-secondary-click-count click-count)
1723 (if (> (mod click-count 3) 0)
1724 ;; Double or triple press: make an initial selection
1725 ;; of one word or line.
1726 (let ((range (mouse-start-end start-point start-point click-count)))
1727 (set-marker mouse-secondary-start nil)
1728 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1729 (window-buffer start-window)))
1730 ;; Single-press: cancel any preexisting secondary selection.
1731 (or mouse-secondary-start
1732 (setq mouse-secondary-start (make-marker)))
1733 (set-marker mouse-secondary-start start-point)
1734 (delete-overlay mouse-secondary-overlay))
1735 ;; FIXME: Use mouse-drag-track!
1736 (let (event end end-point)
1737 (track-mouse
1738 (while (progn
1739 (setq event (read-event))
1740 (or (mouse-movement-p event)
1741 (memq (car-safe event) '(switch-frame select-window))))
1743 (if (memq (car-safe event) '(switch-frame select-window))
1745 (setq end (event-end event)
1746 end-point (posn-point end))
1747 (cond
1748 ;; Are we moving within the original window?
1749 ((and (eq (posn-window end) start-window)
1750 (integer-or-marker-p end-point))
1751 (let ((range (mouse-start-end start-point end-point
1752 click-count)))
1753 (if (or (/= start-point end-point)
1754 (null (marker-position mouse-secondary-start)))
1755 (progn
1756 (set-marker mouse-secondary-start nil)
1757 (move-overlay mouse-secondary-overlay
1758 (car range) (nth 1 range))))))
1760 (let ((mouse-row (cdr (cdr (mouse-position)))))
1761 (cond
1762 ((null mouse-row))
1763 ((< mouse-row top)
1764 (mouse-scroll-subr start-window (- mouse-row top)
1765 mouse-secondary-overlay start-point))
1766 ((>= mouse-row bottom)
1767 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1768 mouse-secondary-overlay start-point)))))))))
1770 (if (consp event)
1771 (if (marker-position mouse-secondary-start)
1772 (save-window-excursion
1773 (delete-overlay mouse-secondary-overlay)
1774 (gui-set-selection 'SECONDARY nil)
1775 (select-window start-window)
1776 (save-excursion
1777 (goto-char mouse-secondary-start)
1778 (sit-for 1)
1779 nil))
1780 (gui-set-selection
1781 'SECONDARY
1782 (buffer-substring (overlay-start mouse-secondary-overlay)
1783 (overlay-end mouse-secondary-overlay)))))))))
1785 (defun mouse-yank-secondary (click)
1786 "Insert the secondary selection at the position clicked on.
1787 Move point to the end of the inserted text.
1788 If `mouse-yank-at-point' is non-nil, insert at point
1789 regardless of where you click."
1790 (interactive "e")
1791 ;; Give temporary modes such as isearch a chance to turn off.
1792 (run-hooks 'mouse-leave-buffer-hook)
1793 (or mouse-yank-at-point (mouse-set-point click))
1794 (let ((secondary (gui-get-selection 'SECONDARY)))
1795 (if secondary
1796 (insert-for-yank secondary)
1797 (error "No secondary selection"))))
1799 (defun mouse-kill-secondary ()
1800 "Kill the text in the secondary selection.
1801 This is intended more as a keyboard command than as a mouse command
1802 but it can work as either one.
1804 The current buffer (in case of keyboard use), or the buffer clicked on,
1805 must be the one that the secondary selection is in. This requirement
1806 is to prevent accidents."
1807 (interactive)
1808 (let* ((keys (this-command-keys))
1809 (click (elt keys (1- (length keys)))))
1810 (or (eq (overlay-buffer mouse-secondary-overlay)
1811 (if (listp click)
1812 (window-buffer (posn-window (event-start click)))
1813 (current-buffer)))
1814 (error "Select or click on the buffer where the secondary selection is")))
1815 (let (this-command)
1816 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1817 (kill-region (overlay-start mouse-secondary-overlay)
1818 (overlay-end mouse-secondary-overlay))))
1819 (delete-overlay mouse-secondary-overlay))
1821 (defun mouse-secondary-save-then-kill (click)
1822 "Set the secondary selection and save it to the kill ring.
1823 The second time, kill it. CLICK should be a mouse click event.
1825 If you have not called `mouse-start-secondary' in the clicked
1826 buffer, activate the secondary selection and set it between point
1827 and the click position CLICK.
1829 Otherwise, adjust the bounds of the secondary selection.
1830 Normally, do this by moving its beginning or end, whichever is
1831 closer, to CLICK. But if you have selected whole words or lines,
1832 adjust to the word or line boundary closest to CLICK instead.
1834 If this command is called a second consecutive time with the same
1835 CLICK position, kill the secondary selection."
1836 (interactive "e")
1837 (mouse-minibuffer-check click)
1838 (let* ((posn (event-start click))
1839 (click-pt (posn-point posn))
1840 (window (posn-window posn))
1841 (buf (window-buffer window))
1842 ;; Don't let a subsequent kill command append to this one.
1843 (this-command this-command)
1844 ;; Check if the user has multi-clicked to select words/lines.
1845 (click-count
1846 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1847 mouse-secondary-click-count
1849 (beg (overlay-start mouse-secondary-overlay))
1850 (end (overlay-end mouse-secondary-overlay)))
1852 (cond
1853 ((not (numberp click-pt)) nil)
1855 ;; If the secondary selection is not active in BUF, activate it.
1856 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1857 (if mouse-secondary-start
1858 (marker-buffer mouse-secondary-start)))))
1859 (select-window window)
1860 (setq mouse-secondary-start (make-marker))
1861 (move-marker mouse-secondary-start (point))
1862 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1863 (kill-ring-save (point) click-pt))
1865 ;; If the user clicked without moving point, delete the secondary
1866 ;; selection. This also resets `mouse-secondary-click-count'.
1867 ((and (eq last-command 'mouse-secondary-save-then-kill)
1868 (eq click-pt mouse-save-then-kill-posn)
1869 (eq window (selected-window)))
1870 (mouse-save-then-kill-delete-region beg end)
1871 (delete-overlay mouse-secondary-overlay)
1872 (setq mouse-secondary-click-count 0)
1873 (setq mouse-save-then-kill-posn nil))
1875 ;; Otherwise, if there is a suitable secondary selection overlay,
1876 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1877 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1878 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1879 (if (< (abs (- click-pt beg))
1880 (abs (- click-pt end)))
1881 (move-overlay mouse-secondary-overlay (car range) end)
1882 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1883 (setq deactivate-mark nil)
1884 (if (eq last-command 'mouse-secondary-save-then-kill)
1885 ;; If the front of the kill ring comes from an immediately
1886 ;; previous use of this command, replace the entry.
1887 (kill-new
1888 (buffer-substring (overlay-start mouse-secondary-overlay)
1889 (overlay-end mouse-secondary-overlay))
1891 (let (deactivate-mark)
1892 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1893 (overlay-end mouse-secondary-overlay))))
1894 (setq mouse-save-then-kill-posn click-pt))
1896 ;; Otherwise, set the secondary selection overlay.
1898 (select-window window)
1899 (if mouse-secondary-start
1900 ;; All we have is one end of a selection, so put the other
1901 ;; end here.
1902 (let ((start (+ 0 mouse-secondary-start)))
1903 (kill-ring-save start click-pt)
1904 (move-overlay mouse-secondary-overlay start click-pt)))
1905 (setq mouse-save-then-kill-posn click-pt))))
1907 ;; Finally, set the window system's secondary selection.
1908 (let (str)
1909 (and (overlay-buffer mouse-secondary-overlay)
1910 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1911 (overlay-end mouse-secondary-overlay)))
1912 (> (length str) 0)
1913 (gui-set-selection 'SECONDARY str))))
1915 (defun secondary-selection-exist-p ()
1916 "Return non-nil if the secondary selection exists in the current buffer."
1917 (memq mouse-secondary-overlay (overlays-in (point-min) (point-max))))
1919 (defun secondary-selection-to-region ()
1920 "Set beginning and end of the region to those of the secondary selection.
1921 This puts mark and point at the beginning and the end of the
1922 secondary selection, respectively. This works when the secondary
1923 selection exists and the region does not exist in current buffer;
1924 the secondary selection will be deleted afterward.
1925 If the region is active, or the secondary selection doesn't exist,
1926 this function does nothing."
1927 (when (and (not (region-active-p))
1928 (secondary-selection-exist-p))
1929 (let ((beg (overlay-start mouse-secondary-overlay))
1930 (end (overlay-end mouse-secondary-overlay)))
1931 (push-mark beg t t)
1932 (goto-char end))
1933 ;; Delete the secondary selection on current buffer.
1934 (delete-overlay mouse-secondary-overlay)))
1936 (defun secondary-selection-from-region ()
1937 "Set beginning and end of the secondary selection to those of the region.
1938 When there is no region, this function does nothing."
1939 (when (region-active-p) ; Create the secondary selection from the region.
1940 (delete-overlay mouse-secondary-overlay) ; Delete the secondary selection even on a different buffer.
1941 (move-overlay mouse-secondary-overlay (region-beginning) (region-end))))
1944 (defcustom mouse-buffer-menu-maxlen 20
1945 "Number of buffers in one pane (submenu) of the buffer menu.
1946 If we have lots of buffers, divide them into groups of
1947 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1948 :type 'integer
1949 :group 'mouse)
1951 (defcustom mouse-buffer-menu-mode-mult 4
1952 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1953 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1954 will split the buffer menu by the major modes (see
1955 `mouse-buffer-menu-mode-groups') or just by menu length.
1956 Set to 1 (or even 0!) if you want to group by major mode always, and to
1957 a large number if you prefer a mixed multitude. The default is 4."
1958 :type 'integer
1959 :group 'mouse
1960 :version "20.3")
1962 (defvar mouse-buffer-menu-mode-groups
1963 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1964 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1965 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1966 . "Mail/News")
1967 ("\\<C\\>" . "C")
1968 ("ObjC" . "C")
1969 ("Text" . "Text")
1970 ("Outline" . "Text")
1971 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1972 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1973 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1974 . "GDB")
1975 ("Lisp" . "Lisp")))
1976 "How to group various major modes together in \\[mouse-buffer-menu].
1977 Each element has the form (REGEXP . GROUPNAME).
1978 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1980 (defun mouse-buffer-menu (event)
1981 "Pop up a menu of buffers for selection with the mouse.
1982 This switches buffers in the window that you clicked on,
1983 and selects that window."
1984 (interactive "e")
1985 (mouse-minibuffer-check event)
1986 (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1987 (window (posn-window (event-start event))))
1988 (when buf
1989 (select-window
1990 (if (framep window) (frame-selected-window window)
1991 window))
1992 (switch-to-buffer buf))))
1994 (defun mouse-buffer-menu-map ()
1995 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1996 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
1997 (dolist (buf buffers)
1998 ;; Divide all buffers into buckets for various major modes.
1999 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
2000 (with-current-buffer buf
2001 (let* ((adjusted-major-mode major-mode) elt)
2002 (dolist (group mouse-buffer-menu-mode-groups)
2003 (when (string-match (car group) (format-mode-line mode-name))
2004 (setq adjusted-major-mode (cdr group))))
2005 (setq elt (assoc adjusted-major-mode split-by-major-mode))
2006 (unless elt
2007 (setq elt (list adjusted-major-mode
2008 (if (stringp adjusted-major-mode)
2009 adjusted-major-mode
2010 (format-mode-line mode-name nil nil buf)))
2011 split-by-major-mode (cons elt split-by-major-mode)))
2012 (or (memq buf (cdr (cdr elt)))
2013 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
2014 ;; Compute the sum of squares of sizes of the major-mode buckets.
2015 (let ((tail split-by-major-mode))
2016 (setq sum-of-squares 0)
2017 (while tail
2018 (setq sum-of-squares
2019 (+ sum-of-squares
2020 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
2021 (setq tail (cdr tail))))
2022 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
2023 (* (length buffers) (length buffers)))
2024 ;; Subdividing by major modes really helps, so let's do it.
2025 (let (subdivided-menus (buffers-left (length buffers)))
2026 ;; Sort the list to put the most popular major modes first.
2027 (setq split-by-major-mode
2028 (sort split-by-major-mode
2029 (function (lambda (elt1 elt2)
2030 (> (length elt1) (length elt2))))))
2031 ;; Make a separate submenu for each major mode
2032 ;; that has more than one buffer,
2033 ;; unless all the remaining buffers are less than 1/10 of them.
2034 (while (and split-by-major-mode
2035 (and (> (length (car split-by-major-mode)) 3)
2036 (> (* buffers-left 10) (length buffers))))
2037 (let ((this-mode-list (mouse-buffer-menu-alist
2038 (cdr (cdr (car split-by-major-mode))))))
2039 (and this-mode-list
2040 (setq subdivided-menus
2041 (cons (cons
2042 (nth 1 (car split-by-major-mode))
2043 this-mode-list)
2044 subdivided-menus))))
2045 (setq buffers-left
2046 (- buffers-left (length (cdr (car split-by-major-mode)))))
2047 (setq split-by-major-mode (cdr split-by-major-mode)))
2048 ;; If any major modes are left over,
2049 ;; make a single submenu for them.
2050 (if split-by-major-mode
2051 (let ((others-list
2052 (mouse-buffer-menu-alist
2053 ;; we don't need split-by-major-mode any more,
2054 ;; so we can ditch it with nconc (mapcan).
2055 (mapcan 'cddr split-by-major-mode))))
2056 (and others-list
2057 (setq subdivided-menus
2058 (cons (cons "Others" others-list)
2059 subdivided-menus)))))
2060 (cons "Buffer Menu" (nreverse subdivided-menus)))
2061 (cons "Buffer Menu"
2062 (mouse-buffer-menu-split "Select Buffer"
2063 (mouse-buffer-menu-alist buffers))))))
2065 (defun mouse-buffer-menu-alist (buffers)
2066 (let (tail
2067 (maxlen 0)
2068 head)
2069 (setq buffers
2070 (sort buffers
2071 (function (lambda (elt1 elt2)
2072 (string< (buffer-name elt1) (buffer-name elt2))))))
2073 (setq tail buffers)
2074 (while tail
2075 (or (eq ?\s (aref (buffer-name (car tail)) 0))
2076 (setq maxlen
2077 (max maxlen
2078 (length (buffer-name (car tail))))))
2079 (setq tail (cdr tail)))
2080 (setq tail buffers)
2081 (while tail
2082 (let ((elt (car tail)))
2083 (if (/= (aref (buffer-name elt) 0) ?\s)
2084 (setq head
2085 (cons
2086 (cons
2087 (format
2088 (format "%%-%ds %%s%%s %%s" maxlen)
2089 (buffer-name elt)
2090 (if (buffer-modified-p elt) "*" " ")
2091 (with-current-buffer elt
2092 (if buffer-read-only "%" " "))
2093 (or (buffer-file-name elt)
2094 (with-current-buffer elt
2095 (if list-buffers-directory
2096 (expand-file-name
2097 list-buffers-directory)))
2098 ""))
2099 elt)
2100 head))))
2101 (setq tail (cdr tail)))
2102 ;; Compensate for the reversal that the above loop does.
2103 (nreverse head)))
2105 (defun mouse-buffer-menu-split (title alist)
2106 ;; If we have lots of buffers, divide them into groups of 20
2107 ;; and make a pane (or submenu) for each one.
2108 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
2109 (let ((alist alist) sublists next
2110 (i 1))
2111 (while alist
2112 ;; Pull off the next mouse-buffer-menu-maxlen buffers
2113 ;; and make them the next element of sublist.
2114 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
2115 (if next
2116 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
2117 nil))
2118 (setq sublists (cons (cons (format "Buffers %d" i) alist)
2119 sublists))
2120 (setq i (1+ i))
2121 (setq alist next))
2122 (nreverse sublists))
2123 ;; Few buffers--put them all in one pane.
2124 (list (cons title alist))))
2126 (define-obsolete-function-alias
2127 'mouse-choose-completion 'choose-completion "23.2")
2129 ;; Font selection.
2131 (defun font-menu-add-default ()
2132 (let* ((default (frame-parameter nil 'font))
2133 (font-alist x-fixed-font-alist)
2134 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2135 (if (assoc "Default" elt)
2136 (delete (assoc "Default" elt) elt))
2137 (setcdr elt
2138 (cons (list "Default" default)
2139 (cdr elt)))))
2141 (defvar x-fixed-font-alist
2142 (list
2143 (purecopy "Font Menu")
2144 (cons
2145 (purecopy "Misc")
2146 (mapcar
2147 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2148 ;; For these, we specify the pixel height and width.
2149 '(("fixed" "fixed")
2150 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2151 ("6x12"
2152 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2153 ("6x13"
2154 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2155 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2156 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2157 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2158 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2159 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2160 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2161 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2162 ("")
2163 ("clean 5x8"
2164 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2165 ("clean 6x8"
2166 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2167 ("clean 8x8"
2168 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2169 ("clean 8x10"
2170 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2171 ("clean 8x14"
2172 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2173 ("clean 8x16"
2174 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2175 ("")
2176 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2177 ;; We don't seem to have these; who knows what they are.
2178 ;; ("fg-18" "fg-18")
2179 ;; ("fg-25" "fg-25")
2180 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2181 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2182 ("lucidasanstypewriter-bold-24"
2183 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2184 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2185 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2188 (cons
2189 (purecopy "Courier")
2190 (mapcar
2191 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2192 ;; For these, we specify the point height.
2193 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2194 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2195 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2196 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2197 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2198 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2199 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2200 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2201 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2202 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2203 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2204 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2205 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2206 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2207 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2208 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2209 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2210 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2211 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2212 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2213 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2214 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2215 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2216 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
2217 ))))
2218 "X fonts suitable for use in Emacs.")
2220 (declare-function generate-fontset-menu "fontset" ())
2222 (defun mouse-select-font ()
2223 "Prompt for a font name, using `x-popup-menu', and return it."
2224 (interactive)
2225 (unless (display-multi-font-p)
2226 (error "Cannot change fonts on this display"))
2227 (car
2228 (x-popup-menu
2229 (if (listp last-nonmenu-event)
2230 last-nonmenu-event
2231 (list '(0 0) (selected-window)))
2232 (append x-fixed-font-alist
2233 (list (generate-fontset-menu))))))
2235 (declare-function text-scale-mode "face-remap")
2237 (defun mouse-set-font (&rest fonts)
2238 "Set the default font for the selected frame.
2239 The argument FONTS is a list of font names; the first valid font
2240 in this list is used.
2242 When called interactively, pop up a menu and allow the user to
2243 choose a font."
2244 (interactive
2245 (progn (unless (display-multi-font-p)
2246 (error "Cannot change fonts on this display"))
2247 (x-popup-menu
2248 (if (listp last-nonmenu-event)
2249 last-nonmenu-event
2250 (list '(0 0) (selected-window)))
2251 ;; Append list of fontsets currently defined.
2252 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2253 (if fonts
2254 (let (font)
2255 (while fonts
2256 (condition-case nil
2257 (progn
2258 (set-frame-font (car fonts))
2259 (setq font (car fonts))
2260 (setq fonts nil))
2261 (error
2262 (setq fonts (cdr fonts)))))
2263 (if (null font)
2264 (error "Font not found")))))
2266 (defvar mouse-appearance-menu-map nil)
2267 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
2268 (declare-function buffer-face-mode-invoke "face-remap"
2269 (face arg &optional interactive))
2270 (declare-function font-face-attributes "font.c" (font &optional frame))
2271 (defvar w32-use-w32-font-dialog)
2272 (defvar w32-fixed-font-alist)
2274 (defun mouse-appearance-menu (event)
2275 "Show a menu for changing the default face in the current buffer."
2276 (interactive "@e")
2277 (require 'face-remap)
2278 (when (display-multi-font-p)
2279 (with-selected-window (car (event-start event))
2280 (if mouse-appearance-menu-map
2281 nil ; regenerate new fonts
2282 ;; Initialize mouse-appearance-menu-map
2283 (setq mouse-appearance-menu-map
2284 (make-sparse-keymap "Change Default Buffer Face"))
2285 (define-key mouse-appearance-menu-map [face-remap-reset-base]
2286 '(menu-item "Reset to Default" face-remap-reset-base))
2287 (define-key mouse-appearance-menu-map [text-scale-decrease]
2288 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
2289 (define-key mouse-appearance-menu-map [text-scale-increase]
2290 '(menu-item "Increase Buffer Text Size" text-scale-increase))
2291 ;; Font selector
2292 (if (and (functionp 'x-select-font)
2293 (or (not (boundp 'w32-use-w32-font-dialog))
2294 w32-use-w32-font-dialog))
2295 (define-key mouse-appearance-menu-map [x-select-font]
2296 '(menu-item "Change Buffer Font..." x-select-font))
2297 ;; If the select-font is unavailable, construct a menu.
2298 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
2299 (font-alist (cdr (append
2300 (if (eq system-type 'windows-nt)
2301 w32-fixed-font-alist
2302 x-fixed-font-alist)
2303 (list (generate-fontset-menu))))))
2304 (dolist (family font-alist)
2305 (let* ((submenu-name (car family))
2306 (submenu-map (make-sparse-keymap submenu-name)))
2307 (dolist (font (cdr family))
2308 (let ((font-name (car font))
2309 font-symbol)
2310 (if (string= font-name "")
2311 (define-key submenu-map [space]
2312 '("--"))
2313 (setq font-symbol (intern (cadr font)))
2314 (define-key submenu-map (vector font-symbol)
2315 (list 'menu-item (car font) font-symbol)))))
2316 (define-key font-submenu (vector (intern submenu-name))
2317 (list 'menu-item submenu-name submenu-map))))
2318 (define-key mouse-appearance-menu-map [font-submenu]
2319 (list 'menu-item "Change Text Font" font-submenu)))))
2320 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
2321 (setq choice (nth (1- (length choice)) choice))
2322 (cond ((eq choice 'text-scale-increase)
2323 (text-scale-increase 1))
2324 ((eq choice 'text-scale-decrease)
2325 (text-scale-increase -1))
2326 ((eq choice 'face-remap-reset-base)
2327 (text-scale-mode 0)
2328 (buffer-face-mode 0))
2329 (choice
2330 ;; Either choice == 'x-select-font, or choice is a
2331 ;; symbol whose name is a font.
2332 (let ((font (if (eq choice 'x-select-font)
2333 (x-select-font)
2334 (symbol-name choice))))
2335 (buffer-face-mode-invoke
2336 (if (fontp font 'font-spec)
2337 (list :font font)
2338 (font-face-attributes font))
2339 t (called-interactively-p 'interactive)))))))))
2342 ;; Drag and drop support.
2343 (defcustom mouse-drag-and-drop-region nil
2344 "If non-nil, dragging the mouse drags the region, if it exists.
2345 If the value is a modifier, such as `control' or `shift' or
2346 `meta', then if that modifier key is pressed when dropping the
2347 region, text is copied instead of being cut."
2348 :type `(choice
2349 (const :tag "Disable dragging the region" nil)
2350 ,@(mapcar
2351 (lambda (modifier)
2352 `(const :tag ,(format "Enable, but copy with the %s modifier"
2353 modifier)
2354 modifier))
2355 '(alt super hyper shift control meta))
2356 (other :tag "Enable dragging the region" t))
2357 :version "26.1"
2358 :group 'mouse)
2360 (defcustom mouse-drag-and-drop-region-cut-when-buffers-differ nil
2361 "If non-nil, cut text also when source and destination buffers differ.
2362 If this option is nil, `mouse-drag-and-drop-region' will leave
2363 the text in the source buffer alone when dropping it in a
2364 different buffer. If this is non-nil, it will cut the text just
2365 as it does when dropping text in the source buffer."
2366 :type 'boolean
2367 :version "26.1"
2368 :group 'mouse)
2370 (defcustom mouse-drag-and-drop-region-show-tooltip 256
2371 "If non-nil, text is shown by a tooltip in a graphic display.
2372 If this option is nil, `mouse-drag-and-drop-region' does not show
2373 tooltips. If this is t, it shows the entire text dragged in a
2374 tooltip. If this is an integer (as with the default value of
2375 256), it will show that many characters of the dragged text in
2376 a tooltip."
2377 :type 'integer
2378 :version "26.1"
2379 :group 'mouse)
2381 (defcustom mouse-drag-and-drop-region-show-cursor t
2382 "If non-nil, move point with mouse cursor during dragging.
2383 If this is nil, `mouse-drag-and-drop-region' leaves point alone.
2384 Otherwise, it will move point together with the mouse cursor and,
2385 in addition, temporarily highlight the original region with the
2386 `mouse-drag-and-drop-region' face."
2387 :type 'boolean
2388 :version "26.1"
2389 :group 'mouse)
2391 (defface mouse-drag-and-drop-region '((t :inherit region))
2392 "Face to highlight original text during dragging.
2393 This face is used by `mouse-drag-and-drop-region' to temporarily
2394 highlight the original region when
2395 `mouse-drag-and-drop-region-show-cursor' is non-nil."
2396 :version "26.1"
2397 :group 'mouse)
2399 (defun mouse-drag-and-drop-region (event)
2400 "Move text in the region to point where mouse is dragged to.
2401 The transportation of text is also referred as `drag and drop'.
2402 When text is dragged over to a different buffer, or if a
2403 modifier key was pressed when dropping, and the value of the
2404 variable `mouse-drag-and-drop-region' is that modifier, the text
2405 is copied instead of being cut."
2406 (interactive "e")
2407 (let* ((mouse-button (event-basic-type last-input-event))
2408 (mouse-drag-and-drop-region-show-tooltip
2409 (when (and mouse-drag-and-drop-region-show-tooltip
2410 (display-multi-frame-p)
2411 (require 'tooltip))
2412 mouse-drag-and-drop-region-show-tooltip))
2413 (start (region-beginning))
2414 (end (region-end))
2415 (point (point))
2416 (buffer (current-buffer))
2417 (window (selected-window))
2418 (text-from-read-only buffer-read-only)
2419 (mouse-drag-and-drop-overlay (make-overlay start end))
2420 point-to-paste
2421 point-to-paste-read-only
2422 window-to-paste
2423 buffer-to-paste
2424 cursor-in-text-area
2425 no-modifier-on-drop
2426 drag-but-negligible
2427 clicked
2428 value-selection ; This remains nil when event was "click".
2429 text-tooltip
2430 states
2431 window-exempt)
2433 ;; STATES stores for each window on this frame its start and point
2434 ;; positions so we can restore them on all windows but for the one
2435 ;; where the drop occurs. For inter-frame drags we'll have to do
2436 ;; this for all windows on all visible frames. In addition we save
2437 ;; also the cursor type for the window's buffer so we can restore it
2438 ;; in case we modified it.
2439 ;; https://lists.gnu.org/archive/html/emacs-devel/2017-12/msg00090.html
2440 (walk-window-tree
2441 (lambda (window)
2442 (setq states
2443 (cons
2444 (list
2445 window
2446 (copy-marker (window-start window))
2447 (copy-marker (window-point window))
2448 (with-current-buffer (window-buffer window)
2449 cursor-type))
2450 states))))
2452 (ignore-errors
2453 (track-mouse
2454 ;; When event was "click" instead of "drag", skip loop.
2455 (while (progn
2456 (setq event (read-key)) ; read-event or read-key
2457 (or (mouse-movement-p event)
2458 ;; Handle `mouse-autoselect-window'.
2459 (eq (car-safe event) 'select-window)))
2460 ;; Obtain the dragged text in region. When the loop was
2461 ;; skipped, value-selection remains nil.
2462 (unless value-selection
2463 (setq value-selection (buffer-substring start end))
2464 (when mouse-drag-and-drop-region-show-tooltip
2465 (let ((text-size mouse-drag-and-drop-region-show-tooltip))
2466 (setq text-tooltip
2467 (if (and (integerp text-size)
2468 (> (length value-selection) text-size))
2469 (concat
2470 (substring value-selection 0 (/ text-size 2))
2471 "\n...\n"
2472 (substring value-selection (- (/ text-size 2)) -1))
2473 value-selection))))
2475 ;; Check if selected text is read-only.
2476 (setq text-from-read-only (or text-from-read-only
2477 (get-text-property start 'read-only)
2478 (not (equal
2479 (next-single-char-property-change
2480 start 'read-only nil end)
2481 end)))))
2482 (setq window-to-paste (posn-window (event-end event)))
2483 (setq point-to-paste (posn-point (event-end event)))
2484 ;; Set nil when target buffer is minibuffer.
2485 (setq buffer-to-paste (let (buf)
2486 (when (windowp window-to-paste)
2487 (setq buf (window-buffer window-to-paste))
2488 (when (not (minibufferp buf))
2489 buf))))
2490 (setq cursor-in-text-area (and window-to-paste
2491 point-to-paste
2492 buffer-to-paste))
2494 (when cursor-in-text-area
2495 ;; Check if point under mouse is read-only.
2496 (save-window-excursion
2497 (select-window window-to-paste)
2498 (setq point-to-paste-read-only
2499 (or buffer-read-only
2500 (get-text-property point-to-paste 'read-only))))
2502 ;; Check if "drag but negligible". Operation "drag but
2503 ;; negligible" is defined as drag-and-drop the text to
2504 ;; the original region. When modifier is pressed, the
2505 ;; text will be inserted to inside of the original
2506 ;; region.
2507 (setq drag-but-negligible
2508 (and (eq (overlay-buffer mouse-drag-and-drop-overlay)
2509 buffer-to-paste)
2510 (< (overlay-start mouse-drag-and-drop-overlay)
2511 point-to-paste)
2512 (< point-to-paste
2513 (overlay-end mouse-drag-and-drop-overlay)))))
2515 ;; Show a tooltip.
2516 (if mouse-drag-and-drop-region-show-tooltip
2517 (tooltip-show text-tooltip)
2518 (tooltip-hide))
2520 ;; Show cursor and highlight the original region.
2521 (when mouse-drag-and-drop-region-show-cursor
2522 ;; Modify cursor even when point is out of frame.
2523 (setq cursor-type (cond
2524 ((not cursor-in-text-area)
2525 nil)
2526 ((or point-to-paste-read-only
2527 drag-but-negligible)
2528 'hollow)
2530 'bar)))
2531 (when cursor-in-text-area
2532 (overlay-put mouse-drag-and-drop-overlay
2533 'face 'mouse-drag-and-drop-region)
2534 (deactivate-mark) ; Maintain region in other window.
2535 (mouse-set-point event)))))
2537 ;; Hide a tooltip.
2538 (when mouse-drag-and-drop-region-show-tooltip (tooltip-hide))
2540 ;; Check if modifier was pressed on drop.
2541 (setq no-modifier-on-drop
2542 (not (member mouse-drag-and-drop-region (event-modifiers event))))
2544 ;; Check if event was "click".
2545 (setq clicked (not value-selection))
2547 ;; Restore status on drag to outside of text-area or non-mouse input.
2548 (when (or (not cursor-in-text-area)
2549 (not (equal (event-basic-type event) mouse-button)))
2550 (setq drag-but-negligible t
2551 no-modifier-on-drop t))
2553 ;; Do not modify any buffers when event is "click",
2554 ;; "drag but negligible", or "drag to read-only".
2555 (let* ((mouse-drag-and-drop-region-cut-when-buffers-differ
2556 (if no-modifier-on-drop
2557 mouse-drag-and-drop-region-cut-when-buffers-differ
2558 (not mouse-drag-and-drop-region-cut-when-buffers-differ)))
2559 (wanna-paste-to-same-buffer (equal buffer-to-paste buffer))
2560 (wanna-cut-on-same-buffer (and wanna-paste-to-same-buffer
2561 no-modifier-on-drop))
2562 (wanna-cut-on-other-buffer
2563 (and (not wanna-paste-to-same-buffer)
2564 mouse-drag-and-drop-region-cut-when-buffers-differ))
2565 (cannot-paste (or point-to-paste-read-only
2566 (when (or wanna-cut-on-same-buffer
2567 wanna-cut-on-other-buffer)
2568 text-from-read-only))))
2570 (cond
2571 ;; Move point within region.
2572 (clicked
2573 (deactivate-mark)
2574 (mouse-set-point event))
2575 ;; Undo operation. Set back the original text as region.
2576 ((or (and drag-but-negligible
2577 no-modifier-on-drop)
2578 cannot-paste)
2579 ;; Inform user either source or destination buffer cannot be modified.
2580 (when (and (not drag-but-negligible)
2581 cannot-paste)
2582 (message "Buffer is read-only"))
2584 ;; Select source window back and restore region.
2585 ;; (set-window-point window point)
2586 (select-window window)
2587 (goto-char point)
2588 (setq deactivate-mark nil)
2589 (activate-mark))
2590 ;; Modify buffers.
2592 ;; * DESTINATION BUFFER::
2593 ;; Insert the text to destination buffer under mouse.
2594 (select-window window-to-paste)
2595 (setq window-exempt window-to-paste)
2596 (goto-char point-to-paste)
2597 (push-mark)
2598 (insert value-selection)
2599 ;; On success, set the text as region on destination buffer.
2600 (when (not (equal (mark) (point)))
2601 (setq deactivate-mark nil)
2602 (activate-mark))
2604 ;; * SOURCE BUFFER::
2605 ;; Set back the original text as region or delete the original
2606 ;; text, on source buffer.
2607 (if wanna-paste-to-same-buffer
2608 ;; When source buffer and destination buffer are the same,
2609 ;; remove the original text.
2610 (when no-modifier-on-drop
2611 (let (deactivate-mark)
2612 (delete-region (overlay-start mouse-drag-and-drop-overlay)
2613 (overlay-end mouse-drag-and-drop-overlay))))
2614 ;; When source buffer and destination buffer are different,
2615 ;; keep (set back the original text as region) or remove the
2616 ;; original text.
2617 (select-window window) ; Select window with source buffer.
2618 (goto-char point) ; Move point to the original text on source buffer.
2620 (if mouse-drag-and-drop-region-cut-when-buffers-differ
2621 ;; Remove the dragged text from source buffer like
2622 ;; operation `cut'.
2623 (delete-region (overlay-start mouse-drag-and-drop-overlay)
2624 (overlay-end mouse-drag-and-drop-overlay))
2625 ;; Set back the dragged text as region on source buffer
2626 ;; like operation `copy'.
2627 (activate-mark))
2628 (select-window window-to-paste))))))
2630 ;; Clean up.
2631 (delete-overlay mouse-drag-and-drop-overlay)
2633 ;; Restore old states but for the window where the drop
2634 ;; occurred. Restore cursor types for all windows.
2635 (dolist (state states)
2636 (let ((window (car state)))
2637 (when (and window-exempt
2638 (not (eq window window-exempt)))
2639 (set-window-start window (nth 1 state) 'noforce)
2640 (set-marker (nth 1 state) nil)
2641 ;; If window is selected, the following automatically sets
2642 ;; point for that window's buffer.
2643 (set-window-point window (nth 2 state))
2644 (set-marker (nth 2 state) nil))
2645 (with-current-buffer (window-buffer window)
2646 (setq cursor-type (nth 3 state)))))))
2649 ;;; Bindings for mouse commands.
2651 (global-set-key [down-mouse-1] 'mouse-drag-region)
2652 (global-set-key [mouse-1] 'mouse-set-point)
2653 (global-set-key [drag-mouse-1] 'mouse-set-region)
2655 (defun mouse--strip-first-event (_prompt)
2656 (substring (this-single-command-raw-keys) 1))
2658 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
2659 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
2661 (global-set-key [mouse-2] 'mouse-yank-primary)
2662 ;; Allow yanking also when the corresponding cursor is "in the fringe".
2663 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
2664 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
2665 (global-set-key [mouse-3] 'mouse-save-then-kill)
2666 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
2667 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
2669 ;; By binding these to down-going events, we let the user use the up-going
2670 ;; event to make the selection, saving a click.
2671 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2672 (if (not (eq system-type 'ms-dos))
2673 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
2674 ;; C-down-mouse-2 is bound in facemenu.el.
2675 (global-set-key [C-down-mouse-3]
2676 `(menu-item ,(purecopy "Menu Bar") ignore
2677 :filter (lambda (_)
2678 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
2679 (mouse-menu-bar-map)
2680 (mouse-menu-major-mode-map)))))
2682 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2683 ;; vertical-line prevents Emacs from signaling an error when the mouse
2684 ;; button is released after dragging these lines, on non-toolkit
2685 ;; versions.
2686 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2687 (global-set-key [header-line mouse-1] 'mouse-select-window)
2688 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2689 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2690 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2691 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2692 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2693 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2694 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2695 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
2696 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2697 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2698 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2699 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
2700 (global-set-key [right-divider mouse-1] 'ignore)
2701 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
2702 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
2703 (global-set-key [bottom-divider mouse-1] 'ignore)
2704 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
2705 (global-set-key [left-edge down-mouse-1] 'mouse-drag-left-edge)
2706 (global-set-key [left-edge mouse-1] 'ignore)
2707 (global-set-key [top-left-corner down-mouse-1] 'mouse-drag-top-left-corner)
2708 (global-set-key [top-left-corner mouse-1] 'ignore)
2709 (global-set-key [top-edge down-mouse-1] 'mouse-drag-top-edge)
2710 (global-set-key [top-edge mouse-1] 'ignore)
2711 (global-set-key [top-right-corner down-mouse-1] 'mouse-drag-top-right-corner)
2712 (global-set-key [top-right-corner mouse-1] 'ignore)
2713 (global-set-key [right-edge down-mouse-1] 'mouse-drag-right-edge)
2714 (global-set-key [right-edge mouse-1] 'ignore)
2715 (global-set-key [bottom-right-corner down-mouse-1] 'mouse-drag-bottom-right-corner)
2716 (global-set-key [bottom-right-corner mouse-1] 'ignore)
2717 (global-set-key [bottom-edge down-mouse-1] 'mouse-drag-bottom-edge)
2718 (global-set-key [bottom-edge mouse-1] 'ignore)
2719 (global-set-key [bottom-left-corner down-mouse-1] 'mouse-drag-bottom-left-corner)
2720 (global-set-key [bottom-left-corner mouse-1] 'ignore)
2722 (provide 'mouse)
2724 ;;; mouse.el ends here