* lisp/url/url-cookie.el: Fix warning and miscompilation
[emacs.git] / lisp / mouse.el
blob169d2632f4f9640a79a76c7fc83c7c927ff0617a
1 ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1995, 1999-2017 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: hardware, mouse
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package provides various useful commands (including help
27 ;; system access) through the mouse. All this code assumes that mouse
28 ;; interpretation has been abstracted into Emacs input events.
30 ;;; Code:
32 ;;; Utility functions.
34 ;; Indent track-mouse like progn.
35 (put 'track-mouse 'lisp-indent-function 0)
37 (defgroup mouse nil
38 "Input from the mouse." ;; "Mouse support."
39 :group 'environment
40 :group 'editing)
42 (defcustom mouse-yank-at-point nil
43 "If non-nil, mouse yank commands yank at point instead of at click."
44 :type 'boolean
45 :group 'mouse)
47 (defcustom mouse-drag-copy-region nil
48 "If non-nil, copy to kill-ring upon mouse adjustments of the region.
50 This affects `mouse-save-then-kill' (\\[mouse-save-then-kill]) in
51 addition to mouse drags."
52 :type 'boolean
53 :version "24.1"
54 :group 'mouse)
56 (defcustom mouse-1-click-follows-link 450
57 "Non-nil means that clicking Mouse-1 on a link follows the link.
59 With the default setting, an ordinary Mouse-1 click on a link
60 performs the same action as Mouse-2 on that link, while a longer
61 Mouse-1 click \(hold down the Mouse-1 button for more than 450
62 milliseconds) performs the original Mouse-1 binding \(which
63 typically sets point where you click the mouse).
65 If value is an integer, the time elapsed between pressing and
66 releasing the mouse button determines whether to follow the link
67 or perform the normal Mouse-1 action (typically set point).
68 The absolute numeric value specifies the maximum duration of a
69 \"short click\" in milliseconds. A positive value means that a
70 short click follows the link, and a longer click performs the
71 normal action. A negative value gives the opposite behavior.
73 If value is `double', a double click follows the link.
75 Otherwise, a single Mouse-1 click unconditionally follows the link.
77 Note that dragging the mouse never follows the link.
79 This feature only works in modes that specifically identify
80 clickable text as links, so it may not work with some external
81 packages. See `mouse-on-link-p' for details."
82 :version "22.1"
83 :type '(choice (const :tag "Disabled" nil)
84 (const :tag "Double click" double)
85 (number :tag "Single click time limit" :value 450)
86 (other :tag "Single click" t))
87 :group 'mouse)
89 (defcustom mouse-1-click-in-non-selected-windows t
90 "If non-nil, a Mouse-1 click also follows links in non-selected windows.
92 If nil, a Mouse-1 click on a link in a non-selected window performs
93 the normal mouse-1 binding, typically selects the window and sets
94 point at the click position."
95 :type 'boolean
96 :version "22.1"
97 :group 'mouse)
99 (defun mouse--down-1-maybe-follows-link (&optional _prompt)
100 "Turn `mouse-1' events into `mouse-2' events if follows-link.
101 Expects to be bound to `down-mouse-1' in `key-translation-map'."
102 (when (and mouse-1-click-follows-link
103 (eq (if (eq mouse-1-click-follows-link 'double)
104 'double-down-mouse-1 'down-mouse-1)
105 (car-safe last-input-event)))
106 (let ((action (mouse-on-link-p (event-start last-input-event))))
107 (when (and action
108 (or mouse-1-click-in-non-selected-windows
109 (eq (selected-window)
110 (posn-window (event-start last-input-event)))))
111 (let ((timedout
112 (sit-for (if (numberp mouse-1-click-follows-link)
113 (/ (abs mouse-1-click-follows-link) 1000.0)
114 0))))
115 (if (if (and (numberp mouse-1-click-follows-link)
116 (>= mouse-1-click-follows-link 0))
117 timedout (not timedout))
119 ;; Use read-key so it works for xterm-mouse-mode!
120 (let ((event (read-key)))
121 (if (eq (car-safe event)
122 (if (eq mouse-1-click-follows-link 'double)
123 'double-mouse-1 'mouse-1))
124 (progn
125 ;; Turn the mouse-1 into a mouse-2 to follow links,
126 ;; but only if ‘mouse-on-link-p’ hasn’t returned a
127 ;; string or vector (see its docstring).
128 (if (or (stringp action) (vectorp action))
129 (push (aref action 0) unread-command-events)
130 (let ((newup (if (eq mouse-1-click-follows-link 'double)
131 'double-mouse-2 'mouse-2)))
132 ;; If mouse-2 has never been done by the user, it
133 ;; doesn't have the necessary property to be
134 ;; interpreted correctly.
135 (unless (get newup 'event-kind)
136 (put newup 'event-kind (get (car event) 'event-kind)))
137 (push (cons newup (cdr event)) unread-command-events)))
138 ;; Don't change the down event, only the up-event
139 ;; (bug#18212).
140 nil)
141 (push event unread-command-events)
142 nil))))))))
144 (define-key key-translation-map [down-mouse-1]
145 #'mouse--down-1-maybe-follows-link)
146 (define-key key-translation-map [double-down-mouse-1]
147 #'mouse--down-1-maybe-follows-link)
150 ;; Provide a mode-specific menu on a mouse button.
152 (defun minor-mode-menu-from-indicator (indicator)
153 "Show menu for minor mode specified by INDICATOR.
154 Interactively, INDICATOR is read using completion.
155 If there is no menu defined for the minor mode, then create one with
156 items `Turn Off' and `Help'."
157 (interactive
158 (list (completing-read
159 "Minor mode indicator: "
160 (describe-minor-mode-completion-table-for-indicator))))
161 (let* ((minor-mode (lookup-minor-mode-from-indicator indicator))
162 (mm-fun (or (get minor-mode :minor-mode-function) minor-mode)))
163 (unless minor-mode (error "Cannot find minor mode for `%s'" indicator))
164 (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist)))
165 (menu (and (keymapp map) (lookup-key map [menu-bar]))))
166 (setq menu
167 (if menu
168 (mouse-menu-non-singleton menu)
169 (if (fboundp mm-fun) ; bug#20201
170 `(keymap
171 ,indicator
172 (turn-off menu-item "Turn off minor mode" ,mm-fun)
173 (help menu-item "Help for minor mode"
174 (lambda () (interactive)
175 (describe-function ',mm-fun)))))))
176 (if menu
177 (popup-menu menu)
178 (message "No menu available")))))
180 (defun mouse-minor-mode-menu (event)
181 "Show minor-mode menu for EVENT on minor modes area of the mode line."
182 (interactive "@e")
183 (let ((indicator (car (nth 4 (car (cdr event))))))
184 (minor-mode-menu-from-indicator indicator)))
186 (defun mouse-menu-major-mode-map ()
187 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
188 (let* (;; Keymap from which to inherit; may be null.
189 (ancestor (mouse-menu-non-singleton
190 (and (current-local-map)
191 (local-key-binding [menu-bar]))))
192 ;; Make a keymap in which our last command leads to a menu or
193 ;; default to the edit menu.
194 (newmap (if ancestor
195 (make-sparse-keymap (concat (format-mode-line mode-name)
196 " Mode"))
197 menu-bar-edit-menu)))
198 (if ancestor
199 (set-keymap-parent newmap ancestor))
200 newmap))
202 (defun mouse-menu-non-singleton (menubar)
203 "Return menu keybar MENUBAR, or a lone submenu inside it.
204 If MENUBAR defines exactly one submenu, return just that submenu.
205 Otherwise, return MENUBAR."
206 (if menubar
207 (let (submap)
208 (map-keymap
209 (lambda (k v) (setq submap (if submap t (cons k v))))
210 (keymap-canonicalize menubar))
211 (if (eq submap t)
212 menubar
213 (lookup-key menubar (vector (car submap)))))))
215 (defun mouse-menu-bar-map ()
216 "Return a keymap equivalent to the menu bar.
217 The contents are the items that would be in the menu bar whether or
218 not it is actually displayed."
219 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
220 (let* ((local-menu (and (current-local-map)
221 (lookup-key (current-local-map) [menu-bar])))
222 (global-menu (lookup-key global-map [menu-bar]))
223 ;; If a keymap doesn't have a prompt string (a lazy
224 ;; programmer didn't bother to provide one), create it and
225 ;; insert it into the keymap; each keymap gets its own
226 ;; prompt. This is required for non-toolkit versions to
227 ;; display non-empty menu pane names.
228 (minor-mode-menus
229 (mapcar
230 (lambda (menu)
231 (let* ((minor-mode (car menu))
232 (menu (cdr menu))
233 (title-or-map (cadr menu)))
234 (or (stringp title-or-map)
235 (setq menu
236 (cons 'keymap
237 (cons (concat
238 (capitalize (subst-char-in-string
239 ?- ?\s (symbol-name
240 minor-mode)))
241 " Menu")
242 (cdr menu)))))
243 menu))
244 (minor-mode-key-binding [menu-bar])))
245 (local-title-or-map (and local-menu (cadr local-menu)))
246 (global-title-or-map (cadr global-menu)))
247 (or (null local-menu)
248 (stringp local-title-or-map)
249 (setq local-menu (cons 'keymap
250 (cons (concat (format-mode-line mode-name)
251 " Mode Menu")
252 (cdr local-menu)))))
253 (or (stringp global-title-or-map)
254 (setq global-menu (cons 'keymap
255 (cons "Global Menu"
256 (cdr global-menu)))))
257 ;; Supplying the list is faster than making a new map.
258 ;; FIXME: We have a problem here: we have to use the global/local/minor
259 ;; so they're displayed in the expected order, but later on in the command
260 ;; loop, they're actually looked up in the opposite order.
261 (apply 'append
262 global-menu
263 local-menu
264 minor-mode-menus)))
266 (defun mouse-major-mode-menu (event &optional prefix)
267 "Pop up a mode-specific menu of mouse commands.
268 Default to the Edit menu if the major mode doesn't define a menu."
269 (declare (obsolete mouse-menu-major-mode-map "23.1"))
270 (interactive "@e\nP")
271 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
272 (popup-menu (mouse-menu-major-mode-map) event prefix))
274 (defun mouse-popup-menubar (event prefix)
275 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
276 The contents are the items that would be in the menu bar whether or
277 not it is actually displayed."
278 (declare (obsolete mouse-menu-bar-map "23.1"))
279 (interactive "@e \nP")
280 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
281 (popup-menu (mouse-menu-bar-map) (unless (integerp event) event) prefix))
283 (defun mouse-popup-menubar-stuff (event prefix)
284 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
285 Use the former if the menu bar is showing, otherwise the latter."
286 (declare (obsolete nil "23.1"))
287 (interactive "@e\nP")
288 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
289 (popup-menu
290 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
291 (mouse-menu-bar-map)
292 (mouse-menu-major-mode-map))
293 event prefix))
295 ;; Commands that operate on windows.
297 (defun mouse-minibuffer-check (event)
298 (let ((w (posn-window (event-start event))))
299 (and (window-minibuffer-p w)
300 (not (minibuffer-window-active-p w))
301 (user-error "Minibuffer window is not active")))
302 ;; Give temporary modes such as isearch a chance to turn off.
303 (run-hooks 'mouse-leave-buffer-hook))
305 (defun mouse-delete-window (click)
306 "Delete the window you click on.
307 Do nothing if the frame has just one window.
308 This command must be bound to a mouse click."
309 (interactive "e")
310 (unless (one-window-p t)
311 (mouse-minibuffer-check click)
312 (delete-window (posn-window (event-start click)))))
314 (defun mouse-select-window (click)
315 "Select the window clicked on; don't move point."
316 (interactive "e")
317 (mouse-minibuffer-check click)
318 (let ((oframe (selected-frame))
319 (frame (window-frame (posn-window (event-start click)))))
320 (select-window (posn-window (event-start click)))
321 (raise-frame frame)
322 (select-frame frame)
323 (or (eq frame oframe)
324 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
326 (define-obsolete-function-alias 'mouse-tear-off-window 'tear-off-window "24.4")
327 (defun tear-off-window (click)
328 "Delete the selected window, and create a new frame displaying its buffer."
329 (interactive "e")
330 (mouse-minibuffer-check click)
331 (let* ((window (posn-window (event-start click)))
332 (buf (window-buffer window))
333 (frame (make-frame))) ;FIXME: Use pop-to-buffer.
334 (select-frame frame)
335 (switch-to-buffer buf)
336 (delete-window window)))
338 (defun mouse-delete-other-windows ()
339 "Delete all windows except the one you click on."
340 (interactive "@")
341 (delete-other-windows))
343 (defun mouse-split-window-vertically (click)
344 "Select Emacs window mouse is on, then split it vertically in half.
345 The window is split at the line clicked on.
346 This command must be bound to a mouse click."
347 (interactive "@e")
348 (mouse-minibuffer-check click)
349 (let ((start (event-start click)))
350 (select-window (posn-window start))
351 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
352 (first-line window-min-height)
353 (last-line (- (window-height) window-min-height)))
354 (if (< last-line first-line)
355 (user-error "Window too short to split")
356 ;; Bind `window-combination-resize' to nil so we are sure to get
357 ;; the split right at the line clicked on.
358 (let (window-combination-resize)
359 (split-window-vertically
360 (min (max new-height first-line) last-line)))))))
362 (defun mouse-split-window-horizontally (click)
363 "Select Emacs window mouse is on, then split it horizontally in half.
364 The window is split at the column clicked on.
365 This command must be bound to a mouse click."
366 (interactive "@e")
367 (mouse-minibuffer-check click)
368 (let ((start (event-start click)))
369 (select-window (posn-window start))
370 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
371 (first-col window-min-width)
372 (last-col (- (window-width) window-min-width)))
373 (if (< last-col first-col)
374 (user-error "Window too narrow to split")
375 ;; Bind `window-combination-resize' to nil so we are sure to get
376 ;; the split right at the column clicked on.
377 (let (window-combination-resize)
378 (split-window-horizontally
379 (min (max new-width first-col) last-col)))))))
381 (defun mouse-drag-line (start-event line)
382 "Drag a mode line, header line, or vertical line with the mouse.
383 START-EVENT is the starting mouse event of the drag action. LINE
384 must be one of the symbols `header', `mode', or `vertical'."
385 ;; Give temporary modes such as isearch a chance to turn off.
386 (run-hooks 'mouse-leave-buffer-hook)
387 (let* ((echo-keystrokes 0)
388 (start (event-start start-event))
389 (window (posn-window start))
390 (frame (window-frame window))
391 ;; `position' records the x- or y-coordinate of the last
392 ;; sampled position.
393 (position (if (eq line 'vertical)
394 (+ (window-pixel-left window)
395 (car (posn-x-y start)))
396 (+ (window-pixel-top window)
397 (cdr (posn-x-y start)))))
398 ;; `last-position' records the x- or y-coordinate of the
399 ;; previously sampled position. The difference of `position'
400 ;; and `last-position' determines the size change of WINDOW.
401 (last-position position)
402 (draggable t)
403 posn-window growth dragged)
404 ;; Decide on whether we are allowed to track at all and whose
405 ;; window's edge we drag.
406 (cond
407 ((eq line 'header)
408 ;; Drag bottom edge of window above the header line.
409 (setq window (window-in-direction 'above window t)))
410 ((eq line 'mode))
411 ((eq line 'vertical)
412 (let ((divider-width (frame-right-divider-width frame)))
413 (when (and (or (not (numberp divider-width))
414 (zerop divider-width))
415 (eq (frame-parameter frame 'vertical-scroll-bars) 'left))
416 (setq window (window-in-direction 'left window t))))))
417 (let* ((exitfun nil)
418 (move
419 (lambda (event) (interactive "e")
420 (cond
421 ((not (consp event))
422 nil)
423 ((eq line 'vertical)
424 ;; Drag right edge of `window'.
425 (setq start (event-start event))
426 (setq position (car (posn-x-y start)))
427 ;; Set `posn-window' to the window where `event' was recorded.
428 ;; This can be `window' or the window on the left or right of
429 ;; `window'.
430 (when (window-live-p (setq posn-window (posn-window start)))
431 ;; Add left edge of `posn-window' to `position'.
432 (setq position (+ (window-pixel-left posn-window) position))
433 (unless (nth 1 start)
434 ;; Add width of objects on the left of the text area to
435 ;; `position'.
436 (when (eq (window-current-scroll-bars posn-window) 'left)
437 (setq position (+ (window-scroll-bar-width posn-window)
438 position)))
439 (setq position (+ (car (window-fringes posn-window))
440 (or (car (window-margins posn-window)) 0)
441 position))))
442 ;; When the cursor overshoots after shrinking a window to its
443 ;; minimum size and the dragging direction changes, have the
444 ;; cursor first catch up with the window edge.
445 (unless (or (zerop (setq growth (- position last-position)))
446 (and (> growth 0)
447 (< position (+ (window-pixel-left window)
448 (window-pixel-width window))))
449 (and (< growth 0)
450 (> position (+ (window-pixel-left window)
451 (window-pixel-width window)))))
452 (setq dragged t)
453 (adjust-window-trailing-edge window growth t t))
454 (setq last-position position))
455 (draggable
456 ;; Drag bottom edge of `window'.
457 (setq start (event-start event))
458 ;; Set `posn-window' to the window where `event' was recorded.
459 ;; This can be either `window' or the window above or below of
460 ;; `window'.
461 (setq posn-window (posn-window start))
462 (setq position (cdr (posn-x-y start)))
463 (when (window-live-p posn-window)
464 ;; Add top edge of `posn-window' to `position'.
465 (setq position (+ (window-pixel-top posn-window) position))
466 ;; If necessary, add height of header line to `position'
467 (when (memq (posn-area start)
468 '(nil left-fringe right-fringe left-margin right-margin))
469 (setq position (+ (window-header-line-height posn-window) position))))
470 ;; When the cursor overshoots after shrinking a window to its
471 ;; minimum size and the dragging direction changes, have the
472 ;; cursor first catch up with the window edge.
473 (unless (or (zerop (setq growth (- position last-position)))
474 (and (> growth 0)
475 (< position (+ (window-pixel-top window)
476 (window-pixel-height window))))
477 (and (< growth 0)
478 (> position (+ (window-pixel-top window)
479 (window-pixel-height window)))))
480 (setq dragged t)
481 (adjust-window-trailing-edge window growth nil t))
482 (setq last-position position)))))
483 (old-track-mouse track-mouse))
484 ;; Start tracking. The special value 'dragging' signals the
485 ;; display engine to freeze the mouse pointer shape for as long
486 ;; as we drag.
487 (setq track-mouse 'dragging)
488 ;; Loop reading events and sampling the position of the mouse.
489 (setq exitfun
490 (set-transient-map
491 (let ((map (make-sparse-keymap)))
492 (define-key map [switch-frame] #'ignore)
493 (define-key map [select-window] #'ignore)
494 (define-key map [scroll-bar-movement] #'ignore)
495 (define-key map [mouse-movement] move)
496 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
497 (define-key map [drag-mouse-1]
498 (lambda () (interactive) (funcall exitfun)))
499 ;; For vertical line dragging swallow also a mouse-1
500 ;; event (but only if we dragged at least once to allow mouse-1
501 ;; clicks to get through).
502 (when (eq line 'vertical)
503 (define-key map [mouse-1]
504 `(menu-item "" ,(lambda () (interactive) (funcall exitfun))
505 :filter ,(lambda (cmd) (if dragged cmd)))))
506 ;; Some of the events will of course end up looked up
507 ;; with a mode-line, header-line or vertical-line prefix ...
508 (define-key map [mode-line] map)
509 (define-key map [header-line] map)
510 (define-key map [vertical-line] map)
511 ;; ... and some maybe even with a right- or bottom-divider
512 ;; prefix.
513 (define-key map [right-divider] map)
514 (define-key map [bottom-divider] map)
515 map)
516 t (lambda () (setq track-mouse old-track-mouse)))))))
518 (defun mouse-drag-mode-line (start-event)
519 "Change the height of a window by dragging on its mode line.
520 START-EVENT is the starting mouse event of the drag action.
522 If the drag happens in a mode line on the bottom of a frame and
523 that frame's `drag-with-mode-line' parameter is non-nil, drag the
524 frame instead."
525 (interactive "e")
526 (let* ((start (event-start start-event))
527 (window (posn-window start))
528 (frame (window-frame window)))
529 (cond
530 ((not (window-live-p window)))
531 ((or (not (window-at-side-p window 'bottom))
532 ;; Allow resizing the minibuffer window if it's on the
533 ;; same frame as and immediately below `window', and it's
534 ;; either active or `resize-mini-windows' is nil.
535 (let ((minibuffer-window (minibuffer-window frame)))
536 (and (eq (window-frame minibuffer-window) frame)
537 (or (not resize-mini-windows)
538 (eq minibuffer-window
539 (active-minibuffer-window))))))
540 (mouse-drag-line start-event 'mode))
541 ((and (frame-parameter frame 'drag-with-mode-line)
542 (window-at-side-p window 'bottom)
543 (let ((minibuffer-window (minibuffer-window frame)))
544 (not (eq (window-frame minibuffer-window) frame))))
545 ;; Drag frame when the window is on the bottom of its frame and
546 ;; there is no minibuffer window below.
547 (mouse-drag-frame start-event 'move)))))
549 (defun mouse-drag-header-line (start-event)
550 "Change the height of a window by dragging on its header line.
551 START-EVENT is the starting mouse event of the drag action.
553 If the drag happens in a header line on the top of a frame and
554 that frame's `drag-with-header-line' parameter is non-nil, drag
555 the frame instead."
556 (interactive "e")
557 (let* ((start (event-start start-event))
558 (window (posn-window start)))
559 (if (and (window-live-p window)
560 (not (window-at-side-p window 'top)))
561 (mouse-drag-line start-event 'header)
562 (let ((frame (window-frame window)))
563 (when (frame-parameter frame 'drag-with-header-line)
564 (mouse-drag-frame start-event 'move))))))
566 (defun mouse-drag-vertical-line (start-event)
567 "Change the width of a window by dragging on a vertical line.
568 START-EVENT is the starting mouse event of the drag action."
569 (interactive "e")
570 (mouse-drag-line start-event 'vertical))
572 (defun mouse-resize-frame (frame x-diff y-diff &optional x-move y-move)
573 "Helper function for `mouse-drag-frame'."
574 (let* ((frame-x-y (frame-position frame))
575 (frame-x (car frame-x-y))
576 (frame-y (cdr frame-x-y))
577 alist)
578 (if (> x-diff 0)
579 (when x-move
580 (setq x-diff (min x-diff frame-x))
581 (setq x-move (- frame-x x-diff)))
582 (let* ((min-width (frame-windows-min-size frame t nil t))
583 (min-diff (max 0 (- (frame-inner-width frame) min-width))))
584 (setq x-diff (max x-diff (- min-diff)))
585 (when x-move
586 (setq x-move (+ frame-x (- x-diff))))))
588 (if (> y-diff 0)
589 (when y-move
590 (setq y-diff (min y-diff frame-y))
591 (setq y-move (- frame-y y-diff)))
592 (let* ((min-height (frame-windows-min-size frame nil nil t))
593 (min-diff (max 0 (- (frame-inner-height frame) min-height))))
594 (setq y-diff (max y-diff (- min-diff)))
595 (when y-move
596 (setq y-move (+ frame-y (- y-diff))))))
598 (unless (zerop x-diff)
599 (when x-move
600 (push `(left . ,x-move) alist))
601 (push `(width . (text-pixels . ,(+ (frame-text-width frame) x-diff)))
602 alist))
603 (unless (zerop y-diff)
604 (when y-move
605 (push `(top . ,y-move) alist))
606 (push `(height . (text-pixels . ,(+ (frame-text-height frame) y-diff)))
607 alist))
608 (when alist
609 (modify-frame-parameters frame alist))))
611 (defun mouse-drag-frame (start-event part)
612 "Drag a frame or one of its edges with the mouse.
613 START-EVENT is the starting mouse event of the drag action. Its
614 position window denotes the frame that will be dragged.
616 PART specifies the part that has been dragged and must be one of
617 the symbols 'left', 'top', 'right', 'bottom', 'top-left',
618 'top-right', 'bottom-left', 'bottom-right' to drag an internal
619 border or edge. If PART equals 'move', this means to move the
620 frame with the mouse."
621 ;; Give temporary modes such as isearch a chance to turn off.
622 (run-hooks 'mouse-leave-buffer-hook)
623 (let* ((echo-keystrokes 0)
624 (start (event-start start-event))
625 (window (posn-window start))
626 ;; FRAME is the frame to drag.
627 (frame (if (window-live-p window)
628 (window-frame window)
629 window))
630 (width (frame-native-width frame))
631 (height (frame-native-height frame))
632 ;; PARENT is the parent frame of FRAME or, if FRAME is a
633 ;; top-level frame, FRAME's workarea.
634 (parent (frame-parent frame))
635 (parent-edges
636 (if parent
637 `(0 0 ,(frame-native-width parent) ,(frame-native-height parent))
638 (let* ((attributes
639 (car (display-monitor-attributes-list)))
640 (workarea (assq 'workarea attributes)))
641 (and workarea
642 `(,(nth 1 workarea) ,(nth 2 workarea)
643 ,(+ (nth 1 workarea) (nth 3 workarea))
644 ,(+ (nth 2 workarea) (nth 4 workarea)))))))
645 (parent-left (and parent-edges (nth 0 parent-edges)))
646 (parent-top (and parent-edges (nth 1 parent-edges)))
647 (parent-right (and parent-edges (nth 2 parent-edges)))
648 (parent-bottom (and parent-edges (nth 3 parent-edges)))
649 ;; `pos-x' and `pos-y' record the x- and y-coordinates of the
650 ;; last sampled mouse position. Note that we sample absolute
651 ;; mouse positions to avoid that moving the mouse from one
652 ;; frame into another gets into our way. `last-x' and `last-y'
653 ;; records the x- and y-coordinates of the previously sampled
654 ;; position. The differences between `last-x' and `pos-x' as
655 ;; well as `last-y' and `pos-y' determine the amount the mouse
656 ;; has been dragged between the last two samples.
657 pos-x-y pos-x pos-y
658 (last-x-y (mouse-absolute-pixel-position))
659 (last-x (car last-x-y))
660 (last-y (cdr last-x-y))
661 ;; `snap-x' and `snap-y' record the x- and y-coordinates of the
662 ;; mouse position when FRAME snapped. As soon as the
663 ;; difference between `pos-x' and `snap-x' (or `pos-y' and
664 ;; `snap-y') exceeds the value of FRAME's `snap-width'
665 ;; parameter, unsnap FRAME (at the respective side). `snap-x'
666 ;; and `snap-y' nil mean FRAME is currently not snapped.
667 snap-x snap-y
668 (exitfun nil)
669 (move
670 (lambda (event)
671 (interactive "e")
672 (when (consp event)
673 (setq pos-x-y (mouse-absolute-pixel-position))
674 (setq pos-x (car pos-x-y))
675 (setq pos-y (cdr pos-x-y))
676 (cond
677 ((eq part 'left)
678 (mouse-resize-frame frame (- last-x pos-x) 0 t))
679 ((eq part 'top)
680 (mouse-resize-frame frame 0 (- last-y pos-y) nil t))
681 ((eq part 'right)
682 (mouse-resize-frame frame (- pos-x last-x) 0))
683 ((eq part 'bottom)
684 (mouse-resize-frame frame 0 (- pos-y last-y)))
685 ((eq part 'top-left)
686 (mouse-resize-frame
687 frame (- last-x pos-x) (- last-y pos-y) t t))
688 ((eq part 'top-right)
689 (mouse-resize-frame
690 frame (- pos-x last-x) (- last-y pos-y) nil t))
691 ((eq part 'bottom-left)
692 (mouse-resize-frame
693 frame (- last-x pos-x) (- pos-y last-y) t))
694 ((eq part 'bottom-right)
695 (mouse-resize-frame
696 frame (- pos-x last-x) (- pos-y last-y)))
697 ((eq part 'move)
698 (let* ((old-position (frame-position frame))
699 (old-left (car old-position))
700 (old-top (cdr old-position))
701 (left (+ old-left (- pos-x last-x)))
702 (top (+ old-top (- pos-y last-y)))
703 right bottom
704 ;; `snap-width' (maybe also a yet to be provided
705 ;; `snap-height') could become floats to handle
706 ;; proportionality wrt PARENT. We don't do any
707 ;; checks on this parameter so far.
708 (snap-width (frame-parameter frame 'snap-width)))
709 ;; Docking and constraining.
710 (when (and (numberp snap-width) parent-edges)
711 (cond
712 ;; Docking at the left parent edge.
713 ((< pos-x last-x)
714 (cond
715 ((and (> left parent-left)
716 (<= (- left parent-left) snap-width))
717 ;; Snap when the mouse moved leftward and
718 ;; FRAME's left edge would end up within
719 ;; `snap-width' pixels from PARENT's left edge.
720 (setq snap-x pos-x)
721 (setq left parent-left))
722 ((and (<= left parent-left)
723 (<= (- parent-left left) snap-width)
724 snap-x (<= (- snap-x pos-x) snap-width))
725 ;; Stay snapped when the mouse moved leftward
726 ;; but not more than `snap-width' pixels from
727 ;; the time FRAME snapped.
728 (setq left parent-left))
730 ;; Unsnap when the mouse moved more than
731 ;; `snap-width' pixels leftward from the time
732 ;; FRAME snapped.
733 (setq snap-x nil))))
734 ((> pos-x last-x)
735 (setq right (+ left width))
736 (cond
737 ((and (< right parent-right)
738 (<= (- parent-right right) snap-width))
739 ;; Snap when the mouse moved rightward and
740 ;; FRAME's right edge would end up within
741 ;; `snap-width' pixels from PARENT's right edge.
742 (setq snap-x pos-x)
743 (setq left (- parent-right width)))
744 ((and (>= right parent-right)
745 (<= (- right parent-right) snap-width)
746 snap-x (<= (- pos-x snap-x) snap-width))
747 ;; Stay snapped when the mouse moved rightward
748 ;; but not more more than `snap-width' pixels
749 ;; from the time FRAME snapped.
750 (setq left (- parent-right width)))
752 ;; Unsnap when the mouse moved rightward more
753 ;; than `snap-width' pixels from the time FRAME
754 ;; snapped.
755 (setq snap-x nil)))))
757 (cond
758 ((< pos-y last-y)
759 (cond
760 ((and (> top parent-top)
761 (<= (- top parent-top) snap-width))
762 ;; Snap when the mouse moved upward and FRAME's
763 ;; top edge would end up within `snap-width'
764 ;; pixels from PARENT's top edge.
765 (setq snap-y pos-y)
766 (setq top parent-top))
767 ((and (<= top parent-top)
768 (<= (- parent-top top) snap-width)
769 snap-y (<= (- snap-y pos-y) snap-width))
770 ;; Stay snapped when the mouse moved upward but
771 ;; not more more than `snap-width' pixels from
772 ;; the time FRAME snapped.
773 (setq top parent-top))
775 ;; Unsnap when the mouse moved upward more than
776 ;; `snap-width' pixels from the time FRAME
777 ;; snapped.
778 (setq snap-y nil))))
779 ((> pos-y last-y)
780 (setq bottom (+ top height))
781 (cond
782 ((and (< bottom parent-bottom)
783 (<= (- parent-bottom bottom) snap-width))
784 ;; Snap when the mouse moved downward and
785 ;; FRAME's bottom edge would end up within
786 ;; `snap-width' pixels from PARENT's bottom
787 ;; edge.
788 (setq snap-y pos-y)
789 (setq top (- parent-bottom height)))
790 ((and (>= bottom parent-bottom)
791 (<= (- bottom parent-bottom) snap-width)
792 snap-y (<= (- pos-y snap-y) snap-width))
793 ;; Stay snapped when the mouse moved downward
794 ;; but not more more than `snap-width' pixels
795 ;; from the time FRAME snapped.
796 (setq top (- parent-bottom height)))
798 ;; Unsnap when the mouse moved downward more
799 ;; than `snap-width' pixels from the time FRAME
800 ;; snapped.
801 (setq snap-y nil))))))
803 ;; If requested, constrain FRAME's draggable areas to
804 ;; PARENT's edges. The `top-visible' parameter should
805 ;; be set when FRAME has a draggable header-line. If
806 ;; set to a number, it ascertains that the top of
807 ;; FRAME is always constrained to the top of PARENT
808 ;; and that at least as many pixels of FRAME as
809 ;; specified by that number are visible on each of the
810 ;; three remaining sides of PARENT.
812 ;; The `bottom-visible' parameter should be set when
813 ;; FRAME has a draggable mode-line. If set to a
814 ;; number, it ascertains that the bottom of FRAME is
815 ;; always constrained to the bottom of PARENT and that
816 ;; at least as many pixels of FRAME as specified by
817 ;; that number are visible on each of the three
818 ;; remaining sides of PARENT.
819 (let ((par (frame-parameter frame 'top-visible))
820 bottom-visible)
821 (unless par
822 (setq par (frame-parameter frame 'bottom-visible))
823 (setq bottom-visible t))
824 (when (and (numberp par) parent-edges)
825 (setq left
826 (max (min (- parent-right par) left)
827 (+ (- parent-left width) par)))
828 (setq top
829 (if bottom-visible
830 (min (max top (- parent-top (- height par)))
831 (- parent-bottom height))
832 (min (max top parent-top)
833 (- parent-bottom par))))))
835 ;; Use `modify-frame-parameters' since `left' and
836 ;; `top' may want to move FRAME out of its PARENT.
837 (modify-frame-parameters
838 frame
839 `((left . (+ ,left)) (top . (+ ,top)))))))
840 (setq last-x pos-x)
841 (setq last-y pos-y))))
842 (old-track-mouse track-mouse))
843 ;; Start tracking. The special value 'dragging' signals the
844 ;; display engine to freeze the mouse pointer shape for as long
845 ;; as we drag.
846 (setq track-mouse 'dragging)
847 ;; Loop reading events and sampling the position of the mouse.
848 (setq exitfun
849 (set-transient-map
850 (let ((map (make-sparse-keymap)))
851 (define-key map [switch-frame] #'ignore)
852 (define-key map [select-window] #'ignore)
853 (define-key map [scroll-bar-movement] #'ignore)
854 (define-key map [mouse-movement] move)
855 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
856 (define-key map [drag-mouse-1]
857 (lambda () (interactive) (funcall exitfun)))
858 ;; Some of the events will of course end up looked up
859 ;; with a mode-line, header-line or vertical-line prefix ...
860 (define-key map [mode-line] map)
861 (define-key map [header-line] map)
862 (define-key map [vertical-line] map)
863 ;; ... and some maybe even with a right- or bottom-divider
864 ;; prefix.
865 (define-key map [right-divider] map)
866 (define-key map [bottom-divider] map)
867 map)
868 t (lambda () (setq track-mouse old-track-mouse))))))
870 (defun mouse-drag-left-edge (start-event)
871 "Drag left edge of a frame with the mouse.
872 START-EVENT is the starting mouse event of the drag action."
873 (interactive "e")
874 (mouse-drag-frame start-event 'left))
876 (defun mouse-drag-top-left-corner (start-event)
877 "Drag top left corner of a frame with the mouse.
878 START-EVENT is the starting mouse event of the drag action."
879 (interactive "e")
880 (mouse-drag-frame start-event 'top-left))
882 (defun mouse-drag-top-edge (start-event)
883 "Drag top edge of a frame with the mouse.
884 START-EVENT is the starting mouse event of the drag action."
885 (interactive "e")
886 (mouse-drag-frame start-event 'top))
888 (defun mouse-drag-top-right-corner (start-event)
889 "Drag top right corner of a frame with the mouse.
890 START-EVENT is the starting mouse event of the drag action."
891 (interactive "e")
892 (mouse-drag-frame start-event 'top-right))
894 (defun mouse-drag-right-edge (start-event)
895 "Drag right edge of a frame with the mouse.
896 START-EVENT is the starting mouse event of the drag action."
897 (interactive "e")
898 (mouse-drag-frame start-event 'right))
900 (defun mouse-drag-bottom-right-corner (start-event)
901 "Drag bottom right corner of a frame with the mouse.
902 START-EVENT is the starting mouse event of the drag action."
903 (interactive "e")
904 (mouse-drag-frame start-event 'bottom-right))
906 (defun mouse-drag-bottom-edge (start-event)
907 "Drag bottom edge of a frame with the mouse.
908 START-EVENT is the starting mouse event of the drag action."
909 (interactive "e")
910 (mouse-drag-frame start-event 'bottom))
912 (defun mouse-drag-bottom-left-corner (start-event)
913 "Drag bottom left corner of a frame with the mouse.
914 START-EVENT is the starting mouse event of the drag action."
915 (interactive "e")
916 (mouse-drag-frame start-event 'bottom-left))
918 (defcustom mouse-select-region-move-to-beginning nil
919 "Effect of selecting a region extending backward from double click.
920 Nil means keep point at the position clicked (region end);
921 non-nil means move point to beginning of region."
922 :type '(choice (const :tag "Don't move point" nil)
923 (const :tag "Move point to beginning of region" t))
924 :group 'mouse
925 :version "26.1")
927 (defun mouse-set-point (event &optional promote-to-region)
928 "Move point to the position clicked on with the mouse.
929 This should be bound to a mouse click event type.
930 If PROMOTE-TO-REGION is non-nil and event is a multiple-click, select
931 the corresponding element around point, with the resulting position of
932 point determined by `mouse-select-region-move-to-beginning'."
933 (interactive "e\np")
934 (mouse-minibuffer-check event)
935 (if (and promote-to-region (> (event-click-count event) 1))
936 (progn
937 (mouse-set-region event)
938 (when mouse-select-region-move-to-beginning
939 (when (> (posn-point (event-start event)) (region-beginning))
940 (exchange-point-and-mark))))
941 ;; Use event-end in case called from mouse-drag-region.
942 ;; If EVENT is a click, event-end and event-start give same value.
943 (posn-set-point (event-end event))))
945 (defvar mouse-last-region-beg nil)
946 (defvar mouse-last-region-end nil)
947 (defvar mouse-last-region-tick nil)
949 (defun mouse-region-match ()
950 "Return non-nil if there's an active region that was set with the mouse."
951 (and (mark t) mark-active
952 (eq mouse-last-region-beg (region-beginning))
953 (eq mouse-last-region-end (region-end))
954 (eq mouse-last-region-tick (buffer-modified-tick))))
956 (defvar mouse--drag-start-event nil)
958 (defun mouse-set-region (click)
959 "Set the region to the text dragged over, and copy to kill ring.
960 This should be bound to a mouse drag event.
961 See the `mouse-drag-copy-region' variable to control whether this
962 command alters the kill ring or not."
963 (interactive "e")
964 (mouse-minibuffer-check click)
965 (select-window (posn-window (event-start click)))
966 (let ((beg (posn-point (event-start click)))
967 (end
968 (if (eq (posn-window (event-end click)) (selected-window))
969 (posn-point (event-end click))
970 ;; If the mouse ends up in any other window or on the menu
971 ;; bar, use `window-point' of selected window (Bug#23707).
972 (window-point)))
973 (click-count (event-click-count click)))
974 (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
975 (when drag-start
976 ;; Drag events don't come with a click count, sadly, so we hack
977 ;; our way around this problem by remembering the start-event in
978 ;; `mouse-drag-start' and fetching the click-count from there.
979 (when (and (<= click-count 1)
980 (equal beg (posn-point (event-start drag-start))))
981 (setq click-count (event-click-count drag-start)))
982 ;; Occasionally we get spurious drag events where the user hasn't
983 ;; dragged his mouse, but instead Emacs has dragged the text under the
984 ;; user's mouse. Try to recover those cases (bug#17562).
985 (when (and (equal (posn-x-y (event-start click))
986 (posn-x-y (event-end click)))
987 (not (eq (car drag-start) 'mouse-movement)))
988 (setq end beg))
989 (setf (terminal-parameter nil 'mouse-drag-start) nil)))
990 (when (and (integerp beg) (integerp end))
991 (let ((range (mouse-start-end beg end (1- click-count))))
992 (if (< end beg)
993 (setq end (nth 0 range) beg (nth 1 range))
994 (setq beg (nth 0 range) end (nth 1 range)))))
995 (and mouse-drag-copy-region (integerp beg) (integerp end)
996 ;; Don't set this-command to `kill-region', so a following
997 ;; C-w won't double the text in the kill ring. Ignore
998 ;; `last-command' so we don't append to a preceding kill.
999 (let (this-command last-command deactivate-mark)
1000 (copy-region-as-kill beg end)))
1001 (if (numberp beg) (goto-char beg))
1002 ;; On a text terminal, bounce the cursor.
1003 (or transient-mark-mode
1004 (window-system)
1005 (sit-for 1))
1006 (push-mark)
1007 (set-mark (point))
1008 (if (numberp end) (goto-char end))
1009 (mouse-set-region-1)))
1011 (defun mouse-set-region-1 ()
1012 ;; Set transient-mark-mode for a little while.
1013 (unless (eq (car-safe transient-mark-mode) 'only)
1014 (setq-local transient-mark-mode
1015 (cons 'only
1016 (unless (eq transient-mark-mode 'lambda)
1017 transient-mark-mode))))
1018 (setq mouse-last-region-beg (region-beginning))
1019 (setq mouse-last-region-end (region-end))
1020 (setq mouse-last-region-tick (buffer-modified-tick)))
1022 (defcustom mouse-scroll-delay 0.25
1023 "The pause between scroll steps caused by mouse drags, in seconds.
1024 If you drag the mouse beyond the edge of a window, Emacs scrolls the
1025 window to bring the text beyond that edge into view, with a delay of
1026 this many seconds between scroll steps. Scrolling stops when you move
1027 the mouse back into the window, or release the button.
1028 This variable's value may be non-integral.
1029 Setting this to zero causes Emacs to scroll as fast as it can."
1030 :type 'number
1031 :group 'mouse)
1033 (defcustom mouse-scroll-min-lines 1
1034 "The minimum number of lines scrolled by dragging mouse out of window.
1035 Moving the mouse out the top or bottom edge of the window begins
1036 scrolling repeatedly. The number of lines scrolled per repetition
1037 is normally equal to the number of lines beyond the window edge that
1038 the mouse has moved. However, it always scrolls at least the number
1039 of lines specified by this variable."
1040 :type 'integer
1041 :group 'mouse)
1043 (defun mouse-scroll-subr (window jump &optional overlay start)
1044 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
1045 If OVERLAY is an overlay, let it stretch from START to the far edge of
1046 the newly visible text.
1047 Upon exit, point is at the far edge of the newly visible text."
1048 (cond
1049 ((and (> jump 0) (< jump mouse-scroll-min-lines))
1050 (setq jump mouse-scroll-min-lines))
1051 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
1052 (setq jump (- mouse-scroll-min-lines))))
1053 (let ((opoint (point)))
1054 (while (progn
1055 (goto-char (window-start window))
1056 (if (not (zerop (vertical-motion jump window)))
1057 (progn
1058 (set-window-start window (point))
1059 (if (natnump jump)
1060 (if (window-end window)
1061 (progn
1062 (goto-char (window-end window))
1063 ;; window-end doesn't reflect the window's new
1064 ;; start position until the next redisplay.
1065 (vertical-motion (1- jump) window))
1066 (vertical-motion (- (window-height window) 2)))
1067 (goto-char (window-start window)))
1068 (if overlay
1069 (move-overlay overlay start (point)))
1070 ;; Now that we have scrolled WINDOW properly,
1071 ;; put point back where it was for the redisplay
1072 ;; so that we don't mess up the selected window.
1073 (or (eq window (selected-window))
1074 (goto-char opoint))
1075 (sit-for mouse-scroll-delay)))))
1076 (or (eq window (selected-window))
1077 (goto-char opoint))))
1079 (defvar mouse-selection-click-count 0)
1081 (defvar mouse-selection-click-count-buffer nil)
1083 (defun mouse-drag-region (start-event)
1084 "Set the region to the text that the mouse is dragged over.
1085 Highlight the drag area as you move the mouse.
1086 This must be bound to a button-down mouse event.
1087 In Transient Mark mode, the highlighting remains as long as the mark
1088 remains active. Otherwise, it remains until the next input event.
1090 When the region already exists and `mouse-drag-and-drop-region'
1091 is non-nil, this moves the entire region of text to where mouse
1092 is dragged over to."
1093 (interactive "e")
1094 (if (and mouse-drag-and-drop-region
1095 (not (member 'triple (event-modifiers start-event)))
1096 (equal (mouse-posn-property (event-start start-event) 'face) 'region))
1097 (mouse-drag-and-drop-region start-event)
1098 ;; Give temporary modes such as isearch a chance to turn off.
1099 (run-hooks 'mouse-leave-buffer-hook)
1100 (mouse-drag-track start-event)))
1102 (defun mouse-posn-property (pos property)
1103 "Look for a property at click position.
1104 POS may be either a buffer position or a click position like
1105 those returned from `event-start'. If the click position is on
1106 a string, the text property PROPERTY is examined.
1107 If this is nil or the click is not on a string, then
1108 the corresponding buffer position is searched for PROPERTY.
1109 If PROPERTY is encountered in one of those places,
1110 its value is returned."
1111 (if (consp pos)
1112 (let ((w (posn-window pos)) (pt (posn-point pos))
1113 (str (posn-string pos)))
1114 (or (and str
1115 (get-text-property (cdr str) property (car str)))
1116 ;; Mouse clicks in the fringe come with a position in
1117 ;; (nth 5). This is useful but is not exactly where we clicked, so
1118 ;; don't look up that position's properties!
1119 (and pt (not (memq (posn-area pos) '(left-fringe right-fringe
1120 left-margin right-margin)))
1121 (get-char-property pt property w))))
1122 (get-char-property pos property)))
1124 (defun mouse-on-link-p (pos)
1125 "Return non-nil if POS is on a link in the current buffer.
1126 POS must specify a buffer position in the current buffer, as a list
1127 of the form returned by the `event-start' and `event-end' functions,
1128 or a mouse event location in the selected window (see `event-start').
1129 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
1130 POS may be a mouse event location in any window.
1132 A clickable link is identified by one of the following methods:
1134 - If the character at POS has a non-nil `follow-link' text or
1135 overlay property, the value of that property determines what to do.
1137 - If there is a local key-binding or a keybinding at position POS
1138 for the `follow-link' event, the binding of that event determines
1139 what to do.
1141 The resulting value determine whether POS is inside a link:
1143 - If the value is `mouse-face', POS is inside a link if there
1144 is a non-nil `mouse-face' property at POS. Return t in this case.
1146 - If the value is a function, FUNC, POS is inside a link if
1147 the call \(FUNC POS) returns non-nil. Return the return value
1148 from that call. Arg is \(posn-point POS) if POS is a mouse event.
1150 - Otherwise, return the value itself.
1152 The return value is interpreted as follows:
1154 - If it is a string, the mouse-1 event is translated into the
1155 first character of the string, i.e. the action of the mouse-1
1156 click is the local or global binding of that character.
1158 - If it is a vector, the mouse-1 event is translated into the
1159 first element of that vector, i.e. the action of the mouse-1
1160 click is the local or global binding of that event.
1162 - Otherwise, the mouse-1 event is translated into a mouse-2 event
1163 at the same position."
1164 (let ((action
1165 (and (or (not (consp pos))
1166 mouse-1-click-in-non-selected-windows
1167 (eq (selected-window) (posn-window pos)))
1168 (or (mouse-posn-property pos 'follow-link)
1169 (let ((area (posn-area pos)))
1170 (when area
1171 (key-binding (vector area 'follow-link) nil t pos)))
1172 (key-binding [follow-link] nil t pos)))))
1173 (cond
1174 ((eq action 'mouse-face)
1175 (and (mouse-posn-property pos 'mouse-face) t))
1176 ((functionp action)
1177 ;; FIXME: This seems questionable if the click is not in a buffer.
1178 ;; Should we instead decide that `action' takes a `posn'?
1179 (if (consp pos)
1180 (with-current-buffer (window-buffer (posn-window pos))
1181 (funcall action (posn-point pos)))
1182 (funcall action pos)))
1183 (t action))))
1185 (defun mouse-fixup-help-message (msg)
1186 "Fix help message MSG for `mouse-1-click-follows-link'."
1187 (let (mp pos)
1188 (if (and mouse-1-click-follows-link
1189 (stringp msg)
1190 (string-match-p "\\`mouse-2" msg)
1191 (setq mp (mouse-pixel-position))
1192 (consp (setq pos (cdr mp)))
1193 (car pos) (>= (car pos) 0)
1194 (cdr pos) (>= (cdr pos) 0)
1195 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
1196 (windowp (posn-window pos)))
1197 (with-current-buffer (window-buffer (posn-window pos))
1198 (if (mouse-on-link-p pos)
1199 (setq msg (concat
1200 (cond
1201 ((eq mouse-1-click-follows-link 'double) "double-")
1202 ((and (integerp mouse-1-click-follows-link)
1203 (< mouse-1-click-follows-link 0)) "Long ")
1204 (t ""))
1205 "mouse-1" (substring msg 7)))))))
1206 msg)
1208 (defun mouse-drag-track (start-event)
1209 "Track mouse drags by highlighting area between point and cursor.
1210 The region will be defined with mark and point."
1211 (mouse-minibuffer-check start-event)
1212 (setq mouse-selection-click-count-buffer (current-buffer))
1213 (deactivate-mark)
1214 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
1215 (start-posn (event-start start-event))
1216 (start-point (posn-point start-posn))
1217 (start-window (posn-window start-posn))
1218 (_ (with-current-buffer (window-buffer start-window)
1219 (setq deactivate-mark nil)))
1220 ;; We've recorded what we needed from the current buffer and
1221 ;; window, now let's jump to the place of the event, where things
1222 ;; are happening.
1223 (_ (mouse-set-point start-event))
1224 (echo-keystrokes 0)
1225 (bounds (window-edges start-window))
1226 (make-cursor-line-fully-visible nil)
1227 (top (nth 1 bounds))
1228 (bottom (if (window-minibuffer-p start-window)
1229 (nth 3 bounds)
1230 ;; Don't count the mode line.
1231 (1- (nth 3 bounds))))
1232 (click-count (1- (event-click-count start-event)))
1233 ;; Suppress automatic hscrolling, because that is a nuisance
1234 ;; when setting point near the right fringe (but see below).
1235 (auto-hscroll-mode-saved auto-hscroll-mode)
1236 (old-track-mouse track-mouse))
1238 (setq mouse-selection-click-count click-count)
1239 ;; In case the down click is in the middle of some intangible text,
1240 ;; use the end of that text, and put it in START-POINT.
1241 (if (< (point) start-point)
1242 (goto-char start-point))
1243 (setq start-point (point))
1245 ;; Activate the region, using `mouse-start-end' to determine where
1246 ;; to put point and mark (e.g., double-click will select a word).
1247 (setq-local transient-mark-mode
1248 (if (eq transient-mark-mode 'lambda)
1249 '(only)
1250 (cons 'only transient-mark-mode)))
1251 (let ((range (mouse-start-end start-point start-point click-count)))
1252 (push-mark (nth 0 range) t t)
1253 (goto-char (nth 1 range)))
1255 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
1256 (setq track-mouse t)
1257 (setq auto-hscroll-mode nil)
1259 (set-transient-map
1260 (let ((map (make-sparse-keymap)))
1261 (define-key map [switch-frame] #'ignore)
1262 (define-key map [select-window] #'ignore)
1263 (define-key map [mouse-movement]
1264 (lambda (event) (interactive "e")
1265 (let* ((end (event-end event))
1266 (end-point (posn-point end)))
1267 (unless (eq end-point start-point)
1268 ;; As soon as the user moves, we can re-enable auto-hscroll.
1269 (setq auto-hscroll-mode auto-hscroll-mode-saved)
1270 ;; And remember that we have moved, so mouse-set-region can know
1271 ;; its event is really a drag event.
1272 (setcar start-event 'mouse-movement))
1273 (if (and (eq (posn-window end) start-window)
1274 (integer-or-marker-p end-point))
1275 (mouse--drag-set-mark-and-point start-point
1276 end-point click-count)
1277 (let ((mouse-row (cdr (cdr (mouse-position)))))
1278 (cond
1279 ((null mouse-row))
1280 ((< mouse-row top)
1281 (mouse-scroll-subr start-window (- mouse-row top)
1282 nil start-point))
1283 ((>= mouse-row bottom)
1284 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1285 nil start-point))))))))
1286 map)
1287 t (lambda ()
1288 (setq track-mouse old-track-mouse)
1289 (setq auto-hscroll-mode auto-hscroll-mode-saved)
1290 (deactivate-mark)
1291 (pop-mark)))))
1293 (defun mouse--drag-set-mark-and-point (start click click-count)
1294 (let* ((range (mouse-start-end start click click-count))
1295 (beg (nth 0 range))
1296 (end (nth 1 range)))
1297 (cond ((eq (mark) beg)
1298 (goto-char end))
1299 ((eq (mark) end)
1300 (goto-char beg))
1301 ((< click (mark))
1302 (set-mark end)
1303 (goto-char beg))
1305 (set-mark beg)
1306 (goto-char end)))))
1308 ;; Commands to handle xterm-style multiple clicks.
1309 (defun mouse-skip-word (dir)
1310 "Skip over word, over whitespace, or over identical punctuation.
1311 If DIR is positive skip forward; if negative, skip backward."
1312 (let* ((char (following-char))
1313 (syntax (char-to-string (char-syntax char))))
1314 (cond ((string= syntax "w")
1315 ;; Here, we can't use skip-syntax-forward/backward because
1316 ;; they don't pay attention to word-separating-categories,
1317 ;; and thus they will skip over a true word boundary. So,
1318 ;; we simulate the original behavior by using forward-word.
1319 (if (< dir 0)
1320 (if (not (looking-at "\\<"))
1321 (forward-word -1))
1322 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1323 (forward-word 1))))
1324 ((string= syntax " ")
1325 (if (< dir 0)
1326 (skip-syntax-backward syntax)
1327 (skip-syntax-forward syntax)))
1328 ((string= syntax "_")
1329 (if (< dir 0)
1330 (skip-syntax-backward "w_")
1331 (skip-syntax-forward "w_")))
1332 ((< dir 0)
1333 (while (and (not (bobp)) (= (preceding-char) char))
1334 (forward-char -1)))
1336 (while (and (not (eobp)) (= (following-char) char))
1337 (forward-char 1))))))
1339 (defun mouse-start-end (start end mode)
1340 "Return a list of region bounds based on START and END according to MODE.
1341 If MODE is 0 then set point to (min START END), mark to (max START END).
1342 If MODE is 1 then set point to start of word at (min START END),
1343 mark to end of word at (max START END).
1344 If MODE is 2 then do the same for lines."
1345 (if (> start end)
1346 (let ((temp start))
1347 (setq start end
1348 end temp)))
1349 (setq mode (mod mode 3))
1350 (cond ((= mode 0)
1351 (list start end))
1352 ((and (= mode 1)
1353 (= start end)
1354 (char-after start)
1355 (= (char-syntax (char-after start)) ?\())
1356 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
1357 ;; This happens in CC Mode when unbalanced parens in CPP
1358 ;; constructs are given punctuation syntax with
1359 ;; syntax-table text properties. (2016-02-21).
1360 (signal 'scan-error (list "Containing expression ends prematurely"
1361 start start))
1362 (list start
1363 (save-excursion
1364 (goto-char start)
1365 (forward-sexp 1)
1366 (point)))))
1367 ((and (= mode 1)
1368 (= start end)
1369 (char-after start)
1370 (= (char-syntax (char-after start)) ?\)))
1371 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
1372 ;; See above comment about CC Mode.
1373 (signal 'scan-error (list "Unbalanced parentheses" start start))
1374 (list (save-excursion
1375 (goto-char (1+ start))
1376 (backward-sexp 1)
1377 (point))
1378 (1+ start))))
1379 ((and (= mode 1)
1380 (= start end)
1381 (char-after start)
1382 (= (char-syntax (char-after start)) ?\"))
1383 (let ((open (or (eq start (point-min))
1384 (save-excursion
1385 (goto-char (- start 1))
1386 (looking-at "\\s(\\|\\s \\|\\s>")))))
1387 (if open
1388 (list start
1389 (save-excursion
1390 (condition-case nil
1391 (progn
1392 (goto-char start)
1393 (forward-sexp 1)
1394 (point))
1395 (error end))))
1396 (list (save-excursion
1397 (condition-case nil
1398 (progn
1399 (goto-char (1+ start))
1400 (backward-sexp 1)
1401 (point))
1402 (error end)))
1403 (1+ start)))))
1404 ((= mode 1)
1405 (list (save-excursion
1406 (goto-char start)
1407 (mouse-skip-word -1)
1408 (point))
1409 (save-excursion
1410 (goto-char end)
1411 (mouse-skip-word 1)
1412 (point))))
1413 ((= mode 2)
1414 (list (save-excursion
1415 (goto-char start)
1416 (line-beginning-position 1))
1417 (save-excursion
1418 (goto-char end)
1419 (forward-line 1)
1420 (point))))))
1422 ;; Subroutine: set the mark where CLICK happened,
1423 ;; but don't do anything else.
1424 (defun mouse-set-mark-fast (click)
1425 (mouse-minibuffer-check click)
1426 (let ((posn (event-start click)))
1427 (select-window (posn-window posn))
1428 (if (numberp (posn-point posn))
1429 (push-mark (posn-point posn) t t))))
1431 (defun mouse-undouble-last-event (events)
1432 (let* ((index (1- (length events)))
1433 (last (nthcdr index events))
1434 (event (car last))
1435 (basic (event-basic-type event))
1436 (old-modifiers (event-modifiers event))
1437 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1438 (new
1439 (if (consp event)
1440 ;; Use reverse, not nreverse, since event-modifiers
1441 ;; does not copy the list it returns.
1442 (cons (event-convert-list (reverse (cons basic modifiers)))
1443 (cdr event))
1444 event)))
1445 (setcar last new)
1446 (if (and (not (equal modifiers old-modifiers))
1447 (key-binding (apply 'vector events)))
1449 (setcar last event)
1450 nil)))
1452 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1454 (defun mouse-set-mark (click)
1455 "Set mark at the position clicked on with the mouse.
1456 Display cursor at that position for a second.
1457 This must be bound to a mouse click."
1458 (interactive "e")
1459 (mouse-minibuffer-check click)
1460 (select-window (posn-window (event-start click)))
1461 ;; FIXME: Use save-excursion
1462 (let ((point-save (point)))
1463 (unwind-protect
1464 (progn (mouse-set-point click)
1465 (push-mark nil t t)
1466 (or transient-mark-mode
1467 (sit-for 1)))
1468 (goto-char point-save))))
1470 (defun mouse-kill (click)
1471 "Kill the region between point and the mouse click.
1472 The text is saved in the kill ring, as with \\[kill-region]."
1473 (interactive "e")
1474 (mouse-minibuffer-check click)
1475 (let* ((posn (event-start click))
1476 (click-posn (posn-point posn)))
1477 (select-window (posn-window posn))
1478 (if (numberp click-posn)
1479 (kill-region (min (point) click-posn)
1480 (max (point) click-posn)))))
1482 (defun mouse-yank-at-click (click arg)
1483 "Insert the last stretch of killed text at the position clicked on.
1484 Also move point to one end of the text thus inserted (normally the end),
1485 and set mark at the beginning.
1486 Prefix arguments are interpreted as with \\[yank].
1487 If `mouse-yank-at-point' is non-nil, insert at point
1488 regardless of where you click."
1489 (interactive "e\nP")
1490 ;; Give temporary modes such as isearch a chance to turn off.
1491 (run-hooks 'mouse-leave-buffer-hook)
1492 (when select-active-regions
1493 ;; Without this, confusing things happen upon e.g. inserting into
1494 ;; the middle of an active region.
1495 (deactivate-mark))
1496 (or mouse-yank-at-point (mouse-set-point click))
1497 (setq this-command 'yank)
1498 (setq mouse-selection-click-count 0)
1499 (yank arg))
1501 (defun mouse-yank-primary (click)
1502 "Insert the primary selection at the position clicked on.
1503 Move point to the end of the inserted text, and set mark at
1504 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1505 regardless of where you click."
1506 (interactive "e")
1507 ;; Give temporary modes such as isearch a chance to turn off.
1508 (run-hooks 'mouse-leave-buffer-hook)
1509 ;; Without this, confusing things happen upon e.g. inserting into
1510 ;; the middle of an active region.
1511 (when select-active-regions
1512 (let (select-active-regions)
1513 (deactivate-mark)))
1514 (or mouse-yank-at-point (mouse-set-point click))
1515 (let ((primary (gui-get-primary-selection)))
1516 (push-mark)
1517 (insert-for-yank primary)))
1519 (defun mouse-kill-ring-save (click)
1520 "Copy the region between point and the mouse click in the kill ring.
1521 This does not delete the region; it acts like \\[kill-ring-save]."
1522 (interactive "e")
1523 (mouse-set-mark-fast click)
1524 (let (this-command last-command)
1525 (kill-ring-save (point) (mark t))))
1527 ;; This function used to delete the text between point and the mouse
1528 ;; whenever it was equal to the front of the kill ring, but some
1529 ;; people found that confusing.
1531 ;; The position of the last invocation of `mouse-save-then-kill'.
1532 (defvar mouse-save-then-kill-posn nil)
1534 (defun mouse-save-then-kill-delete-region (beg end)
1535 ;; We must make our own undo boundaries
1536 ;; because they happen automatically only for the current buffer.
1537 (undo-boundary)
1538 (if (or (= beg end) (eq buffer-undo-list t))
1539 ;; If we have no undo list in this buffer,
1540 ;; just delete.
1541 (delete-region beg end)
1542 ;; Delete, but make the undo-list entry share with the kill ring.
1543 ;; First, delete just one char, so in case buffer is being modified
1544 ;; for the first time, the undo list records that fact.
1545 (let ((inhibit-modification-hooks t))
1546 (delete-region beg
1547 (+ beg (if (> end beg) 1 -1))))
1548 (let ((buffer-undo-list buffer-undo-list))
1549 ;; Undo that deletion--but don't change the undo list!
1550 (let ((inhibit-modification-hooks t))
1551 (primitive-undo 1 buffer-undo-list))
1552 ;; Now delete the rest of the specified region,
1553 ;; but don't record it.
1554 (setq buffer-undo-list t)
1555 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1556 (error "Lossage in mouse-save-then-kill-delete-region"))
1557 (delete-region beg end))
1558 (let ((tail buffer-undo-list))
1559 ;; Search back in buffer-undo-list for the string
1560 ;; that came from deleting one character.
1561 (while (and tail (not (stringp (car (car tail)))))
1562 (setq tail (cdr tail)))
1563 ;; Replace it with an entry for the entire deleted text.
1564 (and tail
1565 (setcar tail (cons (car kill-ring) (min beg end))))))
1566 (undo-boundary))
1568 (defun mouse-save-then-kill (click)
1569 "Set the region according to CLICK; the second time, kill it.
1570 CLICK should be a mouse click event.
1572 If the region is inactive, activate it temporarily. Set mark at
1573 the original point, and move point to the position of CLICK.
1575 If the region is already active, adjust it. Normally, do this by
1576 moving point or mark, whichever is closer, to CLICK. But if you
1577 have selected whole words or lines, move point or mark to the
1578 word or line boundary closest to CLICK instead.
1580 If `mouse-drag-copy-region' is non-nil, this command also saves the
1581 new region to the kill ring (replacing the previous kill if the
1582 previous region was just saved to the kill ring).
1584 If this command is called a second consecutive time with the same
1585 CLICK position, kill the region (or delete it
1586 if `mouse-drag-copy-region' is non-nil)"
1587 (interactive "e")
1588 (mouse-minibuffer-check click)
1589 (let* ((posn (event-start click))
1590 (click-pt (posn-point posn))
1591 (window (posn-window posn))
1592 (buf (window-buffer window))
1593 ;; Don't let a subsequent kill command append to this one.
1594 (this-command this-command)
1595 ;; Check if the user has multi-clicked to select words/lines.
1596 (click-count
1597 (if (and (eq mouse-selection-click-count-buffer buf)
1598 (with-current-buffer buf (mark t)))
1599 mouse-selection-click-count
1600 0)))
1601 (cond
1602 ((not (numberp click-pt)) nil)
1603 ;; If the user clicked without moving point, kill the region.
1604 ;; This also resets `mouse-selection-click-count'.
1605 ((and (eq last-command 'mouse-save-then-kill)
1606 (eq click-pt mouse-save-then-kill-posn)
1607 (eq window (selected-window)))
1608 (if mouse-drag-copy-region
1609 ;; Region already saved in the previous click;
1610 ;; don't make a duplicate entry, just delete.
1611 (delete-region (mark t) (point))
1612 (kill-region (mark t) (point)))
1613 (setq mouse-selection-click-count 0)
1614 (setq mouse-save-then-kill-posn nil))
1616 ;; Otherwise, if there is a suitable region, adjust it by moving
1617 ;; one end (whichever is closer) to CLICK-PT.
1618 ((or (with-current-buffer buf (region-active-p))
1619 (and (eq window (selected-window))
1620 (mark t)
1621 (or (and (eq last-command 'mouse-save-then-kill)
1622 mouse-save-then-kill-posn)
1623 (and (memq last-command '(mouse-drag-region
1624 mouse-set-region))
1625 (or mark-even-if-inactive
1626 (not transient-mark-mode))))))
1627 (select-window window)
1628 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1629 (if (< (abs (- click-pt (mark t)))
1630 (abs (- click-pt (point))))
1631 (set-mark (car range))
1632 (goto-char (nth 1 range)))
1633 (setq deactivate-mark nil)
1634 (mouse-set-region-1)
1635 (when mouse-drag-copy-region
1636 ;; Region already copied to kill-ring once, so replace.
1637 (kill-new (filter-buffer-substring (mark t) (point)) t))
1638 ;; Arrange for a repeated mouse-3 to kill the region.
1639 (setq mouse-save-then-kill-posn click-pt)))
1641 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1643 (select-window window)
1644 (mouse-set-mark-fast click)
1645 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1646 (if before-scroll (goto-char before-scroll)))
1647 (exchange-point-and-mark)
1648 (mouse-set-region-1)
1649 (when mouse-drag-copy-region
1650 (kill-new (filter-buffer-substring (mark t) (point))))
1651 (setq mouse-save-then-kill-posn click-pt)))))
1654 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1655 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1656 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1657 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1658 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1660 (defconst mouse-secondary-overlay
1661 (let ((ol (make-overlay (point-min) (point-min))))
1662 (delete-overlay ol)
1663 (overlay-put ol 'face 'secondary-selection)
1665 "An overlay which records the current secondary selection.
1666 It is deleted when there is no secondary selection.")
1668 (defvar mouse-secondary-click-count 0)
1670 ;; A marker which records the specified first end for a secondary selection.
1671 ;; May be nil.
1672 (defvar mouse-secondary-start nil)
1674 (defun mouse-start-secondary (click)
1675 "Set one end of the secondary selection to the position clicked on.
1676 Use \\[mouse-secondary-save-then-kill] to set the other end
1677 and complete the secondary selection."
1678 (interactive "e")
1679 (mouse-minibuffer-check click)
1680 (let ((posn (event-start click)))
1681 (with-current-buffer (window-buffer (posn-window posn))
1682 ;; Cancel any preexisting secondary selection.
1683 (delete-overlay mouse-secondary-overlay)
1684 (if (numberp (posn-point posn))
1685 (progn
1686 (or mouse-secondary-start
1687 (setq mouse-secondary-start (make-marker)))
1688 (move-marker mouse-secondary-start (posn-point posn)))))))
1690 (defun mouse-set-secondary (click)
1691 "Set the secondary selection to the text that the mouse is dragged over.
1692 This must be bound to a mouse drag event."
1693 (interactive "e")
1694 (mouse-minibuffer-check click)
1695 (let ((posn (event-start click))
1697 (end (event-end click)))
1698 (with-current-buffer (window-buffer (posn-window posn))
1699 (if (numberp (posn-point posn))
1700 (setq beg (posn-point posn)))
1701 (move-overlay mouse-secondary-overlay beg (posn-point end))
1702 (gui-set-selection
1703 'SECONDARY
1704 (buffer-substring (overlay-start mouse-secondary-overlay)
1705 (overlay-end mouse-secondary-overlay))))))
1707 (defun mouse-drag-secondary (start-event)
1708 "Set the secondary selection to the text that the mouse is dragged over.
1709 Highlight the drag area as you move the mouse.
1710 This must be bound to a button-down mouse event.
1711 The function returns a non-nil value if it creates a secondary selection."
1712 (interactive "e")
1713 (mouse-minibuffer-check start-event)
1714 (let* ((echo-keystrokes 0)
1715 (start-posn (event-start start-event))
1716 (start-point (posn-point start-posn))
1717 (start-window (posn-window start-posn))
1718 (bounds (window-edges start-window))
1719 (top (nth 1 bounds))
1720 (bottom (if (window-minibuffer-p start-window)
1721 (nth 3 bounds)
1722 ;; Don't count the mode line.
1723 (1- (nth 3 bounds))))
1724 (click-count (1- (event-click-count start-event))))
1725 (with-current-buffer (window-buffer start-window)
1726 (setq mouse-secondary-click-count click-count)
1727 (if (> (mod click-count 3) 0)
1728 ;; Double or triple press: make an initial selection
1729 ;; of one word or line.
1730 (let ((range (mouse-start-end start-point start-point click-count)))
1731 (set-marker mouse-secondary-start nil)
1732 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1733 (window-buffer start-window)))
1734 ;; Single-press: cancel any preexisting secondary selection.
1735 (or mouse-secondary-start
1736 (setq mouse-secondary-start (make-marker)))
1737 (set-marker mouse-secondary-start start-point)
1738 (delete-overlay mouse-secondary-overlay))
1739 ;; FIXME: Use mouse-drag-track!
1740 (let (event end end-point)
1741 (track-mouse
1742 (while (progn
1743 (setq event (read-event))
1744 (or (mouse-movement-p event)
1745 (memq (car-safe event) '(switch-frame select-window))))
1747 (if (memq (car-safe event) '(switch-frame select-window))
1749 (setq end (event-end event)
1750 end-point (posn-point end))
1751 (cond
1752 ;; Are we moving within the original window?
1753 ((and (eq (posn-window end) start-window)
1754 (integer-or-marker-p end-point))
1755 (let ((range (mouse-start-end start-point end-point
1756 click-count)))
1757 (if (or (/= start-point end-point)
1758 (null (marker-position mouse-secondary-start)))
1759 (progn
1760 (set-marker mouse-secondary-start nil)
1761 (move-overlay mouse-secondary-overlay
1762 (car range) (nth 1 range))))))
1764 (let ((mouse-row (cdr (cdr (mouse-position)))))
1765 (cond
1766 ((null mouse-row))
1767 ((< mouse-row top)
1768 (mouse-scroll-subr start-window (- mouse-row top)
1769 mouse-secondary-overlay start-point))
1770 ((>= mouse-row bottom)
1771 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1772 mouse-secondary-overlay start-point)))))))))
1774 (if (consp event)
1775 (if (marker-position mouse-secondary-start)
1776 (save-window-excursion
1777 (delete-overlay mouse-secondary-overlay)
1778 (gui-set-selection 'SECONDARY nil)
1779 (select-window start-window)
1780 (save-excursion
1781 (goto-char mouse-secondary-start)
1782 (sit-for 1)
1783 nil))
1784 (gui-set-selection
1785 'SECONDARY
1786 (buffer-substring (overlay-start mouse-secondary-overlay)
1787 (overlay-end mouse-secondary-overlay)))))))))
1789 (defun mouse-yank-secondary (click)
1790 "Insert the secondary selection at the position clicked on.
1791 Move point to the end of the inserted text.
1792 If `mouse-yank-at-point' is non-nil, insert at point
1793 regardless of where you click."
1794 (interactive "e")
1795 ;; Give temporary modes such as isearch a chance to turn off.
1796 (run-hooks 'mouse-leave-buffer-hook)
1797 (or mouse-yank-at-point (mouse-set-point click))
1798 (let ((secondary (gui-get-selection 'SECONDARY)))
1799 (if secondary
1800 (insert-for-yank secondary)
1801 (error "No secondary selection"))))
1803 (defun mouse-kill-secondary ()
1804 "Kill the text in the secondary selection.
1805 This is intended more as a keyboard command than as a mouse command
1806 but it can work as either one.
1808 The current buffer (in case of keyboard use), or the buffer clicked on,
1809 must be the one that the secondary selection is in. This requirement
1810 is to prevent accidents."
1811 (interactive)
1812 (let* ((keys (this-command-keys))
1813 (click (elt keys (1- (length keys)))))
1814 (or (eq (overlay-buffer mouse-secondary-overlay)
1815 (if (listp click)
1816 (window-buffer (posn-window (event-start click)))
1817 (current-buffer)))
1818 (error "Select or click on the buffer where the secondary selection is")))
1819 (let (this-command)
1820 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1821 (kill-region (overlay-start mouse-secondary-overlay)
1822 (overlay-end mouse-secondary-overlay))))
1823 (delete-overlay mouse-secondary-overlay))
1825 (defun mouse-secondary-save-then-kill (click)
1826 "Set the secondary selection and save it to the kill ring.
1827 The second time, kill it. CLICK should be a mouse click event.
1829 If you have not called `mouse-start-secondary' in the clicked
1830 buffer, activate the secondary selection and set it between point
1831 and the click position CLICK.
1833 Otherwise, adjust the bounds of the secondary selection.
1834 Normally, do this by moving its beginning or end, whichever is
1835 closer, to CLICK. But if you have selected whole words or lines,
1836 adjust to the word or line boundary closest to CLICK instead.
1838 If this command is called a second consecutive time with the same
1839 CLICK position, kill the secondary selection."
1840 (interactive "e")
1841 (mouse-minibuffer-check click)
1842 (let* ((posn (event-start click))
1843 (click-pt (posn-point posn))
1844 (window (posn-window posn))
1845 (buf (window-buffer window))
1846 ;; Don't let a subsequent kill command append to this one.
1847 (this-command this-command)
1848 ;; Check if the user has multi-clicked to select words/lines.
1849 (click-count
1850 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1851 mouse-secondary-click-count
1853 (beg (overlay-start mouse-secondary-overlay))
1854 (end (overlay-end mouse-secondary-overlay)))
1856 (cond
1857 ((not (numberp click-pt)) nil)
1859 ;; If the secondary selection is not active in BUF, activate it.
1860 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1861 (if mouse-secondary-start
1862 (marker-buffer mouse-secondary-start)))))
1863 (select-window window)
1864 (setq mouse-secondary-start (make-marker))
1865 (move-marker mouse-secondary-start (point))
1866 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1867 (kill-ring-save (point) click-pt))
1869 ;; If the user clicked without moving point, delete the secondary
1870 ;; selection. This also resets `mouse-secondary-click-count'.
1871 ((and (eq last-command 'mouse-secondary-save-then-kill)
1872 (eq click-pt mouse-save-then-kill-posn)
1873 (eq window (selected-window)))
1874 (mouse-save-then-kill-delete-region beg end)
1875 (delete-overlay mouse-secondary-overlay)
1876 (setq mouse-secondary-click-count 0)
1877 (setq mouse-save-then-kill-posn nil))
1879 ;; Otherwise, if there is a suitable secondary selection overlay,
1880 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1881 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1882 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1883 (if (< (abs (- click-pt beg))
1884 (abs (- click-pt end)))
1885 (move-overlay mouse-secondary-overlay (car range) end)
1886 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1887 (setq deactivate-mark nil)
1888 (if (eq last-command 'mouse-secondary-save-then-kill)
1889 ;; If the front of the kill ring comes from an immediately
1890 ;; previous use of this command, replace the entry.
1891 (kill-new
1892 (buffer-substring (overlay-start mouse-secondary-overlay)
1893 (overlay-end mouse-secondary-overlay))
1895 (let (deactivate-mark)
1896 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1897 (overlay-end mouse-secondary-overlay))))
1898 (setq mouse-save-then-kill-posn click-pt))
1900 ;; Otherwise, set the secondary selection overlay.
1902 (select-window window)
1903 (if mouse-secondary-start
1904 ;; All we have is one end of a selection, so put the other
1905 ;; end here.
1906 (let ((start (+ 0 mouse-secondary-start)))
1907 (kill-ring-save start click-pt)
1908 (move-overlay mouse-secondary-overlay start click-pt)))
1909 (setq mouse-save-then-kill-posn click-pt))))
1911 ;; Finally, set the window system's secondary selection.
1912 (let (str)
1913 (and (overlay-buffer mouse-secondary-overlay)
1914 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1915 (overlay-end mouse-secondary-overlay)))
1916 (> (length str) 0)
1917 (gui-set-selection 'SECONDARY str))))
1919 (defun secondary-selection-exist-p ()
1920 "Return non-nil if the secondary selection exists in the current buffer."
1921 (memq mouse-secondary-overlay (overlays-in (point-min) (point-max))))
1923 (defun secondary-selection-to-region ()
1924 "Set beginning and end of the region to those of the secondary selection.
1925 This puts mark and point at the beginning and the end of the
1926 secondary selection, respectively. This works when the secondary
1927 selection exists and the region does not exist in current buffer;
1928 the secondary selection will be deleted afterward.
1929 If the region is active, or the secondary selection doesn't exist,
1930 this function does nothing."
1931 (when (and (not (region-active-p))
1932 (secondary-selection-exist-p))
1933 (let ((beg (overlay-start mouse-secondary-overlay))
1934 (end (overlay-end mouse-secondary-overlay)))
1935 (push-mark beg t t)
1936 (goto-char end))
1937 ;; Delete the secondary selection on current buffer.
1938 (delete-overlay mouse-secondary-overlay)))
1940 (defun secondary-selection-from-region ()
1941 "Set beginning and end of the secondary selection to those of the region.
1942 When there is no region, this function does nothing."
1943 (when (region-active-p) ; Create the secondary selection from the region.
1944 (delete-overlay mouse-secondary-overlay) ; Delete the secondary selection even on a different buffer.
1945 (move-overlay mouse-secondary-overlay (region-beginning) (region-end))))
1948 (defcustom mouse-buffer-menu-maxlen 20
1949 "Number of buffers in one pane (submenu) of the buffer menu.
1950 If we have lots of buffers, divide them into groups of
1951 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1952 :type 'integer
1953 :group 'mouse)
1955 (defcustom mouse-buffer-menu-mode-mult 4
1956 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1957 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1958 will split the buffer menu by the major modes (see
1959 `mouse-buffer-menu-mode-groups') or just by menu length.
1960 Set to 1 (or even 0!) if you want to group by major mode always, and to
1961 a large number if you prefer a mixed multitude. The default is 4."
1962 :type 'integer
1963 :group 'mouse
1964 :version "20.3")
1966 (defvar mouse-buffer-menu-mode-groups
1967 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1968 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1969 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1970 . "Mail/News")
1971 ("\\<C\\>" . "C")
1972 ("ObjC" . "C")
1973 ("Text" . "Text")
1974 ("Outline" . "Text")
1975 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1976 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1977 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1978 . "GDB")
1979 ("Lisp" . "Lisp")))
1980 "How to group various major modes together in \\[mouse-buffer-menu].
1981 Each element has the form (REGEXP . GROUPNAME).
1982 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1984 (defun mouse-buffer-menu (event)
1985 "Pop up a menu of buffers for selection with the mouse.
1986 This switches buffers in the window that you clicked on,
1987 and selects that window."
1988 (interactive "e")
1989 (mouse-minibuffer-check event)
1990 (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1991 (window (posn-window (event-start event))))
1992 (when buf
1993 (select-window
1994 (if (framep window) (frame-selected-window window)
1995 window))
1996 (switch-to-buffer buf))))
1998 (defun mouse-buffer-menu-map ()
1999 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
2000 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
2001 (dolist (buf buffers)
2002 ;; Divide all buffers into buckets for various major modes.
2003 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
2004 (with-current-buffer buf
2005 (let* ((adjusted-major-mode major-mode) elt)
2006 (dolist (group mouse-buffer-menu-mode-groups)
2007 (when (string-match (car group) (format-mode-line mode-name))
2008 (setq adjusted-major-mode (cdr group))))
2009 (setq elt (assoc adjusted-major-mode split-by-major-mode))
2010 (unless elt
2011 (setq elt (list adjusted-major-mode
2012 (if (stringp adjusted-major-mode)
2013 adjusted-major-mode
2014 (format-mode-line mode-name nil nil buf)))
2015 split-by-major-mode (cons elt split-by-major-mode)))
2016 (or (memq buf (cdr (cdr elt)))
2017 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
2018 ;; Compute the sum of squares of sizes of the major-mode buckets.
2019 (let ((tail split-by-major-mode))
2020 (setq sum-of-squares 0)
2021 (while tail
2022 (setq sum-of-squares
2023 (+ sum-of-squares
2024 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
2025 (setq tail (cdr tail))))
2026 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
2027 (* (length buffers) (length buffers)))
2028 ;; Subdividing by major modes really helps, so let's do it.
2029 (let (subdivided-menus (buffers-left (length buffers)))
2030 ;; Sort the list to put the most popular major modes first.
2031 (setq split-by-major-mode
2032 (sort split-by-major-mode
2033 (function (lambda (elt1 elt2)
2034 (> (length elt1) (length elt2))))))
2035 ;; Make a separate submenu for each major mode
2036 ;; that has more than one buffer,
2037 ;; unless all the remaining buffers are less than 1/10 of them.
2038 (while (and split-by-major-mode
2039 (and (> (length (car split-by-major-mode)) 3)
2040 (> (* buffers-left 10) (length buffers))))
2041 (let ((this-mode-list (mouse-buffer-menu-alist
2042 (cdr (cdr (car split-by-major-mode))))))
2043 (and this-mode-list
2044 (setq subdivided-menus
2045 (cons (cons
2046 (nth 1 (car split-by-major-mode))
2047 this-mode-list)
2048 subdivided-menus))))
2049 (setq buffers-left
2050 (- buffers-left (length (cdr (car split-by-major-mode)))))
2051 (setq split-by-major-mode (cdr split-by-major-mode)))
2052 ;; If any major modes are left over,
2053 ;; make a single submenu for them.
2054 (if split-by-major-mode
2055 (let ((others-list
2056 (mouse-buffer-menu-alist
2057 ;; we don't need split-by-major-mode any more,
2058 ;; so we can ditch it with nconc (mapcan).
2059 (mapcan 'cddr split-by-major-mode))))
2060 (and others-list
2061 (setq subdivided-menus
2062 (cons (cons "Others" others-list)
2063 subdivided-menus)))))
2064 (cons "Buffer Menu" (nreverse subdivided-menus)))
2065 (cons "Buffer Menu"
2066 (mouse-buffer-menu-split "Select Buffer"
2067 (mouse-buffer-menu-alist buffers))))))
2069 (defun mouse-buffer-menu-alist (buffers)
2070 (let (tail
2071 (maxlen 0)
2072 head)
2073 (setq buffers
2074 (sort buffers
2075 (function (lambda (elt1 elt2)
2076 (string< (buffer-name elt1) (buffer-name elt2))))))
2077 (setq tail buffers)
2078 (while tail
2079 (or (eq ?\s (aref (buffer-name (car tail)) 0))
2080 (setq maxlen
2081 (max maxlen
2082 (length (buffer-name (car tail))))))
2083 (setq tail (cdr tail)))
2084 (setq tail buffers)
2085 (while tail
2086 (let ((elt (car tail)))
2087 (if (/= (aref (buffer-name elt) 0) ?\s)
2088 (setq head
2089 (cons
2090 (cons
2091 (format
2092 (format "%%-%ds %%s%%s %%s" maxlen)
2093 (buffer-name elt)
2094 (if (buffer-modified-p elt) "*" " ")
2095 (with-current-buffer elt
2096 (if buffer-read-only "%" " "))
2097 (or (buffer-file-name elt)
2098 (with-current-buffer elt
2099 (if list-buffers-directory
2100 (expand-file-name
2101 list-buffers-directory)))
2102 ""))
2103 elt)
2104 head))))
2105 (setq tail (cdr tail)))
2106 ;; Compensate for the reversal that the above loop does.
2107 (nreverse head)))
2109 (defun mouse-buffer-menu-split (title alist)
2110 ;; If we have lots of buffers, divide them into groups of 20
2111 ;; and make a pane (or submenu) for each one.
2112 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
2113 (let ((alist alist) sublists next
2114 (i 1))
2115 (while alist
2116 ;; Pull off the next mouse-buffer-menu-maxlen buffers
2117 ;; and make them the next element of sublist.
2118 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
2119 (if next
2120 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
2121 nil))
2122 (setq sublists (cons (cons (format "Buffers %d" i) alist)
2123 sublists))
2124 (setq i (1+ i))
2125 (setq alist next))
2126 (nreverse sublists))
2127 ;; Few buffers--put them all in one pane.
2128 (list (cons title alist))))
2130 (define-obsolete-function-alias
2131 'mouse-choose-completion 'choose-completion "23.2")
2133 ;; Font selection.
2135 (defun font-menu-add-default ()
2136 (let* ((default (frame-parameter nil 'font))
2137 (font-alist x-fixed-font-alist)
2138 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2139 (if (assoc "Default" elt)
2140 (delete (assoc "Default" elt) elt))
2141 (setcdr elt
2142 (cons (list "Default" default)
2143 (cdr elt)))))
2145 (defvar x-fixed-font-alist
2146 (list
2147 (purecopy "Font Menu")
2148 (cons
2149 (purecopy "Misc")
2150 (mapcar
2151 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2152 ;; For these, we specify the pixel height and width.
2153 '(("fixed" "fixed")
2154 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2155 ("6x12"
2156 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2157 ("6x13"
2158 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2159 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2160 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2161 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2162 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2163 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2164 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2165 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2166 ("")
2167 ("clean 5x8"
2168 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2169 ("clean 6x8"
2170 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2171 ("clean 8x8"
2172 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2173 ("clean 8x10"
2174 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2175 ("clean 8x14"
2176 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2177 ("clean 8x16"
2178 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2179 ("")
2180 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2181 ;; We don't seem to have these; who knows what they are.
2182 ;; ("fg-18" "fg-18")
2183 ;; ("fg-25" "fg-25")
2184 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2185 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2186 ("lucidasanstypewriter-bold-24"
2187 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2188 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2189 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2192 (cons
2193 (purecopy "Courier")
2194 (mapcar
2195 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2196 ;; For these, we specify the point height.
2197 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2198 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2199 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2200 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2201 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2202 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2203 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2204 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2205 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2206 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2207 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2208 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2209 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2210 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2211 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2212 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2213 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2214 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2215 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2216 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2217 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2218 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2219 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2220 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
2221 ))))
2222 "X fonts suitable for use in Emacs.")
2224 (declare-function generate-fontset-menu "fontset" ())
2226 (defun mouse-select-font ()
2227 "Prompt for a font name, using `x-popup-menu', and return it."
2228 (interactive)
2229 (unless (display-multi-font-p)
2230 (error "Cannot change fonts on this display"))
2231 (car
2232 (x-popup-menu
2233 (if (listp last-nonmenu-event)
2234 last-nonmenu-event
2235 (list '(0 0) (selected-window)))
2236 (append x-fixed-font-alist
2237 (list (generate-fontset-menu))))))
2239 (declare-function text-scale-mode "face-remap")
2241 (defun mouse-set-font (&rest fonts)
2242 "Set the default font for the selected frame.
2243 The argument FONTS is a list of font names; the first valid font
2244 in this list is used.
2246 When called interactively, pop up a menu and allow the user to
2247 choose a font."
2248 (interactive
2249 (progn (unless (display-multi-font-p)
2250 (error "Cannot change fonts on this display"))
2251 (x-popup-menu
2252 (if (listp last-nonmenu-event)
2253 last-nonmenu-event
2254 (list '(0 0) (selected-window)))
2255 ;; Append list of fontsets currently defined.
2256 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2257 (if fonts
2258 (let (font)
2259 (while fonts
2260 (condition-case nil
2261 (progn
2262 (set-frame-font (car fonts))
2263 (setq font (car fonts))
2264 (setq fonts nil))
2265 (error
2266 (setq fonts (cdr fonts)))))
2267 (if (null font)
2268 (error "Font not found")))))
2270 (defvar mouse-appearance-menu-map nil)
2271 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
2272 (declare-function buffer-face-mode-invoke "face-remap"
2273 (face arg &optional interactive))
2274 (declare-function font-face-attributes "font.c" (font &optional frame))
2275 (defvar w32-use-w32-font-dialog)
2276 (defvar w32-fixed-font-alist)
2278 (defun mouse-appearance-menu (event)
2279 "Show a menu for changing the default face in the current buffer."
2280 (interactive "@e")
2281 (require 'face-remap)
2282 (when (display-multi-font-p)
2283 (with-selected-window (car (event-start event))
2284 (if mouse-appearance-menu-map
2285 nil ; regenerate new fonts
2286 ;; Initialize mouse-appearance-menu-map
2287 (setq mouse-appearance-menu-map
2288 (make-sparse-keymap "Change Default Buffer Face"))
2289 (define-key mouse-appearance-menu-map [face-remap-reset-base]
2290 '(menu-item "Reset to Default" face-remap-reset-base))
2291 (define-key mouse-appearance-menu-map [text-scale-decrease]
2292 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
2293 (define-key mouse-appearance-menu-map [text-scale-increase]
2294 '(menu-item "Increase Buffer Text Size" text-scale-increase))
2295 ;; Font selector
2296 (if (and (functionp 'x-select-font)
2297 (or (not (boundp 'w32-use-w32-font-dialog))
2298 w32-use-w32-font-dialog))
2299 (define-key mouse-appearance-menu-map [x-select-font]
2300 '(menu-item "Change Buffer Font..." x-select-font))
2301 ;; If the select-font is unavailable, construct a menu.
2302 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
2303 (font-alist (cdr (append
2304 (if (eq system-type 'windows-nt)
2305 w32-fixed-font-alist
2306 x-fixed-font-alist)
2307 (list (generate-fontset-menu))))))
2308 (dolist (family font-alist)
2309 (let* ((submenu-name (car family))
2310 (submenu-map (make-sparse-keymap submenu-name)))
2311 (dolist (font (cdr family))
2312 (let ((font-name (car font))
2313 font-symbol)
2314 (if (string= font-name "")
2315 (define-key submenu-map [space]
2316 '("--"))
2317 (setq font-symbol (intern (cadr font)))
2318 (define-key submenu-map (vector font-symbol)
2319 (list 'menu-item (car font) font-symbol)))))
2320 (define-key font-submenu (vector (intern submenu-name))
2321 (list 'menu-item submenu-name submenu-map))))
2322 (define-key mouse-appearance-menu-map [font-submenu]
2323 (list 'menu-item "Change Text Font" font-submenu)))))
2324 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
2325 (setq choice (nth (1- (length choice)) choice))
2326 (cond ((eq choice 'text-scale-increase)
2327 (text-scale-increase 1))
2328 ((eq choice 'text-scale-decrease)
2329 (text-scale-increase -1))
2330 ((eq choice 'face-remap-reset-base)
2331 (text-scale-mode 0)
2332 (buffer-face-mode 0))
2333 (choice
2334 ;; Either choice == 'x-select-font, or choice is a
2335 ;; symbol whose name is a font.
2336 (let ((font (if (eq choice 'x-select-font)
2337 (x-select-font)
2338 (symbol-name choice))))
2339 (buffer-face-mode-invoke
2340 (if (fontp font 'font-spec)
2341 (list :font font)
2342 (font-face-attributes font))
2343 t (called-interactively-p 'interactive)))))))))
2346 ;; Drag and drop support.
2347 (defcustom mouse-drag-and-drop-region nil
2348 "If non-nil, dragging the mouse drags the region, if that exists.
2349 If the value is a modifier, such as `control' or `shift' or `meta',
2350 then if that modifier key is pressed when dropping the region, region
2351 text is copied instead of being cut."
2352 :type 'symbol
2353 :version "26.1"
2354 :group 'mouse)
2356 (defun mouse-drag-and-drop-region (event)
2357 "Move text in the region to point where mouse is dragged to.
2358 The transportation of text is also referred as `drag and drop'.
2359 When text is dragged over to a different buffer, or if a
2360 modifier key was pressed when dropping, and the value of the
2361 variable `mouse-drag-and-drop-region' is that modifier, the text
2362 is copied instead of being cut."
2363 (interactive "e")
2364 (require 'tooltip)
2365 (let ((start (region-beginning))
2366 (end (region-end))
2367 (point (point))
2368 (buffer (current-buffer))
2369 (window (selected-window))
2370 value-selection)
2371 (track-mouse
2372 ;; When event was click instead of drag, skip loop
2373 (while (progn
2374 (setq event (read-event))
2375 (mouse-movement-p event))
2376 (unless value-selection ; initialization
2377 (delete-overlay mouse-secondary-overlay)
2378 (setq value-selection (buffer-substring start end))
2379 (move-overlay mouse-secondary-overlay start end)) ; (deactivate-mark)
2380 (ignore-errors (deactivate-mark) ; care existing region in other window
2381 (mouse-set-point event)
2382 (tooltip-show value-selection)))
2383 (tooltip-hide))
2384 ;; Do not modify buffer under mouse when "event was click",
2385 ;; "drag negligible", or
2386 ;; "drag to read-only".
2387 (if (or (equal (mouse-posn-property (event-end event) 'face) 'region) ; "event was click"
2388 (member 'secondary-selection ; "drag negligible"
2389 (mapcar (lambda (xxx) (overlay-get xxx 'face))
2390 (overlays-at (posn-point (event-end event)))))
2391 buffer-read-only)
2392 ;; Do not modify buffer under mouse.
2393 (cond
2394 ;; "drag negligible" or "drag to read-only", restore region.
2395 (value-selection
2396 (select-window window) ; In case miss drag to other window
2397 (goto-char point)
2398 (setq deactivate-mark nil)
2399 (activate-mark))
2400 ;; "event was click"
2402 (deactivate-mark)
2403 (mouse-set-point event)))
2404 ;; Modify buffer under mouse by inserting text.
2405 (push-mark)
2406 (insert value-selection)
2407 (when (not (equal (mark) (point))) ; on success insert
2408 (setq deactivate-mark nil)
2409 (activate-mark)) ; have region on destination
2410 ;; Take care of initial region on source.
2411 (if (equal (current-buffer) buffer) ; when same buffer
2412 (let (deactivate-mark) ; remove text
2413 (unless (member mouse-drag-and-drop-region (event-modifiers event))
2414 (kill-region (overlay-start mouse-secondary-overlay)
2415 (overlay-end mouse-secondary-overlay))))
2416 (let ((window1 (selected-window))) ; when beyond buffer
2417 (select-window window)
2418 (goto-char point) ; restore point on source window
2419 (activate-mark) ; restore region
2420 (select-window window1))))
2421 (delete-overlay mouse-secondary-overlay)))
2424 ;;; Bindings for mouse commands.
2426 (global-set-key [down-mouse-1] 'mouse-drag-region)
2427 (global-set-key [mouse-1] 'mouse-set-point)
2428 (global-set-key [drag-mouse-1] 'mouse-set-region)
2430 (defun mouse--strip-first-event (_prompt)
2431 (substring (this-single-command-raw-keys) 1))
2433 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
2434 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
2436 (global-set-key [mouse-2] 'mouse-yank-primary)
2437 ;; Allow yanking also when the corresponding cursor is "in the fringe".
2438 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
2439 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
2440 (global-set-key [mouse-3] 'mouse-save-then-kill)
2441 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
2442 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
2444 ;; By binding these to down-going events, we let the user use the up-going
2445 ;; event to make the selection, saving a click.
2446 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2447 (if (not (eq system-type 'ms-dos))
2448 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
2449 ;; C-down-mouse-2 is bound in facemenu.el.
2450 (global-set-key [C-down-mouse-3]
2451 `(menu-item ,(purecopy "Menu Bar") ignore
2452 :filter (lambda (_)
2453 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
2454 (mouse-menu-bar-map)
2455 (mouse-menu-major-mode-map)))))
2457 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2458 ;; vertical-line prevents Emacs from signaling an error when the mouse
2459 ;; button is released after dragging these lines, on non-toolkit
2460 ;; versions.
2461 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2462 (global-set-key [header-line mouse-1] 'mouse-select-window)
2463 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2464 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2465 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2466 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2467 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2468 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2469 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2470 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
2471 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2472 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2473 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2474 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
2475 (global-set-key [right-divider mouse-1] 'ignore)
2476 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
2477 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
2478 (global-set-key [bottom-divider mouse-1] 'ignore)
2479 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
2480 (global-set-key [left-edge down-mouse-1] 'mouse-drag-left-edge)
2481 (global-set-key [left-edge mouse-1] 'ignore)
2482 (global-set-key [top-left-corner down-mouse-1] 'mouse-drag-top-left-corner)
2483 (global-set-key [top-left-corner mouse-1] 'ignore)
2484 (global-set-key [top-edge down-mouse-1] 'mouse-drag-top-edge)
2485 (global-set-key [top-edge mouse-1] 'ignore)
2486 (global-set-key [top-right-corner down-mouse-1] 'mouse-drag-top-right-corner)
2487 (global-set-key [top-right-corner mouse-1] 'ignore)
2488 (global-set-key [right-edge down-mouse-1] 'mouse-drag-right-edge)
2489 (global-set-key [right-edge mouse-1] 'ignore)
2490 (global-set-key [bottom-right-corner down-mouse-1] 'mouse-drag-bottom-right-corner)
2491 (global-set-key [bottom-right-corner mouse-1] 'ignore)
2492 (global-set-key [bottom-edge down-mouse-1] 'mouse-drag-bottom-edge)
2493 (global-set-key [bottom-edge mouse-1] 'ignore)
2494 (global-set-key [bottom-left-corner down-mouse-1] 'mouse-drag-bottom-left-corner)
2495 (global-set-key [bottom-left-corner mouse-1] 'ignore)
2497 (provide 'mouse)
2499 ;;; mouse.el ends here