1 ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1995, 1999-2018 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: hardware, mouse
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/>.
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.
32 ;;; Utility functions.
34 ;; Indent track-mouse like progn.
35 (put 'track-mouse
'lisp-indent-function
0)
38 "Input from the mouse." ;; "Mouse support."
42 (defcustom mouse-yank-at-point nil
43 "If non-nil, mouse yank commands yank at point instead of at click."
46 (defcustom mouse-drag-copy-region nil
47 "If non-nil, copy to kill-ring upon mouse adjustments of the region.
49 This affects `mouse-save-then-kill' (\\[mouse-save-then-kill]) in
50 addition to mouse drags."
54 (defcustom mouse-1-click-follows-link
450
55 "Non-nil means that clicking Mouse-1 on a link follows the link.
57 With the default setting, an ordinary Mouse-1 click on a link
58 performs the same action as Mouse-2 on that link, while a longer
59 Mouse-1 click (hold down the Mouse-1 button for more than 450
60 milliseconds) performs the original Mouse-1 binding (which
61 typically sets point where you click the mouse).
63 If value is an integer, the time elapsed between pressing and
64 releasing the mouse button determines whether to follow the link
65 or perform the normal Mouse-1 action (typically set point).
66 The absolute numeric value specifies the maximum duration of a
67 \"short click\" in milliseconds. A positive value means that a
68 short click follows the link, and a longer click performs the
69 normal action. A negative value gives the opposite behavior.
71 If value is `double', a double click follows the link.
73 Otherwise, a single Mouse-1 click unconditionally follows the link.
75 Note that dragging the mouse never follows the link.
77 This feature only works in modes that specifically identify
78 clickable text as links, so it may not work with some external
79 packages. See `mouse-on-link-p' for details."
81 :type
'(choice (const :tag
"Disabled" nil
)
82 (const :tag
"Double click" double
)
83 (number :tag
"Single click time limit" :value
450)
84 (other :tag
"Single click" t
)))
86 (defcustom mouse-1-click-in-non-selected-windows t
87 "If non-nil, a Mouse-1 click also follows links in non-selected windows.
89 If nil, a Mouse-1 click on a link in a non-selected window performs
90 the normal mouse-1 binding, typically selects the window and sets
91 point at the click position."
95 (defvar mouse--last-down nil
)
97 (defun mouse--down-1-maybe-follows-link (&optional _prompt
)
98 (when mouse-1-click-follows-link
99 (setq mouse--last-down
(cons (car-safe last-input-event
) (float-time))))
102 (defun mouse--click-1-maybe-follows-link (&optional _prompt
)
103 "Turn `mouse-1' events into `mouse-2' events if follows-link.
104 Expects to be bound to `(double-)mouse-1' in `key-translation-map'."
105 (and mouse--last-down
106 (pcase mouse-1-click-follows-link
108 ('double
(eq 'double-mouse-1
(car-safe last-input-event
)))
109 (_ (and (eq 'mouse-1
(car-safe last-input-event
))
110 (or (not (numberp mouse-1-click-follows-link
))
111 (funcall (if (< mouse-1-click-follows-link
0) #'> #'<)
112 (- (float-time) (cdr mouse--last-down
))
113 (/ (abs mouse-1-click-follows-link
) 1000.0))))))
114 (eq (car mouse--last-down
)
115 (event-convert-list (list 'down
(car-safe last-input-event
))))
116 (let* ((action (mouse-on-link-p (event-start last-input-event
))))
118 (or mouse-1-click-in-non-selected-windows
119 (eq (selected-window)
120 (posn-window (event-start last-input-event
)))))
121 ;; Turn the mouse-1 into a mouse-2 to follow links,
122 ;; but only if ‘mouse-on-link-p’ hasn’t returned a
123 ;; string or vector (see its docstring).
125 (vector (aref action
0))
126 (let ((newup (if (eq mouse-1-click-follows-link
'double
)
127 'double-mouse-2
'mouse-2
)))
128 ;; If mouse-2 has never been done by the user, it
129 ;; doesn't have the necessary property to be
130 ;; interpreted correctly.
131 (unless (get newup
'event-kind
)
132 (put newup
'event-kind
133 (get (car last-input-event
) 'event-kind
)))
134 ;; Modify the event in-place, otherwise we can get a prefix
135 ;; added again, so a click on the header-line turns
136 ;; into a [header-line header-line mouse-2] :-(.
137 ;; See fake_prefixed_keys in src/keyboard.c's.
138 (setf (car last-input-event
) newup
)
139 (vector last-input-event
)))))))
141 (define-key key-translation-map
[down-mouse-1
]
142 #'mouse--down-1-maybe-follows-link
)
143 (define-key key-translation-map
[double-down-mouse-1
]
144 #'mouse--down-1-maybe-follows-link
)
145 (define-key key-translation-map
[mouse-1
]
146 #'mouse--click-1-maybe-follows-link
)
147 (define-key key-translation-map
[double-mouse-1
]
148 #'mouse--click-1-maybe-follows-link
)
151 ;; Provide a mode-specific menu on a mouse button.
153 (defun minor-mode-menu-from-indicator (indicator)
154 "Show menu for minor mode specified by INDICATOR.
155 Interactively, INDICATOR is read using completion.
156 If there is no menu defined for the minor mode, then create one with
157 items `Turn Off' and `Help'."
159 (list (completing-read
160 "Minor mode indicator: "
161 (describe-minor-mode-completion-table-for-indicator))))
162 (let* ((minor-mode (lookup-minor-mode-from-indicator indicator
))
163 (mm-fun (or (get minor-mode
:minor-mode-function
) minor-mode
)))
164 (unless minor-mode
(error "Cannot find minor mode for `%s'" indicator
))
165 (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist
)))
166 (menu (and (keymapp map
) (lookup-key map
[menu-bar
]))))
169 (mouse-menu-non-singleton menu
)
170 (if (fboundp mm-fun
) ; bug#20201
173 (turn-off menu-item
"Turn off minor mode" ,mm-fun
)
174 (help menu-item
"Help for minor mode"
175 (lambda () (interactive)
176 (describe-function ',mm-fun
)))))))
179 (message "No menu available")))))
181 (defun mouse-minor-mode-menu (event)
182 "Show minor-mode menu for EVENT on minor modes area of the mode line."
184 (let ((indicator (car (nth 4 (car (cdr event
))))))
185 (minor-mode-menu-from-indicator indicator
)))
187 (defun mouse-menu-major-mode-map ()
188 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
189 (let* (;; Keymap from which to inherit; may be null.
190 (ancestor (mouse-menu-non-singleton
191 (and (current-local-map)
192 (local-key-binding [menu-bar
]))))
193 ;; Make a keymap in which our last command leads to a menu or
194 ;; default to the edit menu.
196 (make-sparse-keymap (concat (format-mode-line mode-name
)
198 menu-bar-edit-menu
)))
200 (set-keymap-parent newmap ancestor
))
203 (defun mouse-menu-non-singleton (menubar)
204 "Return menu keybar MENUBAR, or a lone submenu inside it.
205 If MENUBAR defines exactly one submenu, return just that submenu.
206 Otherwise, return MENUBAR."
210 (lambda (k v
) (setq submap
(if submap t
(cons k v
))))
211 (keymap-canonicalize menubar
))
214 (lookup-key menubar
(vector (car submap
)))))))
216 (defun mouse-menu-bar-map ()
217 "Return a keymap equivalent to the menu bar.
218 The contents are the items that would be in the menu bar whether or
219 not it is actually displayed."
220 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
221 (let* ((local-menu (and (current-local-map)
222 (lookup-key (current-local-map) [menu-bar
])))
223 (global-menu (lookup-key global-map
[menu-bar
]))
224 ;; If a keymap doesn't have a prompt string (a lazy
225 ;; programmer didn't bother to provide one), create it and
226 ;; insert it into the keymap; each keymap gets its own
227 ;; prompt. This is required for non-toolkit versions to
228 ;; display non-empty menu pane names.
232 (let* ((minor-mode (car menu
))
234 (title-or-map (cadr menu
)))
235 (or (stringp title-or-map
)
239 (capitalize (subst-char-in-string
245 (minor-mode-key-binding [menu-bar
])))
246 (local-title-or-map (and local-menu
(cadr local-menu
)))
247 (global-title-or-map (cadr global-menu
)))
248 (or (null local-menu
)
249 (stringp local-title-or-map
)
250 (setq local-menu
(cons 'keymap
251 (cons (concat (format-mode-line mode-name
)
254 (or (stringp global-title-or-map
)
255 (setq global-menu
(cons 'keymap
257 (cdr global-menu
)))))
258 ;; Supplying the list is faster than making a new map.
259 ;; FIXME: We have a problem here: we have to use the global/local/minor
260 ;; so they're displayed in the expected order, but later on in the command
261 ;; loop, they're actually looked up in the opposite order.
267 (defun mouse-major-mode-menu (event &optional prefix
)
268 "Pop up a mode-specific menu of mouse commands.
269 Default to the Edit menu if the major mode doesn't define a menu."
270 (declare (obsolete mouse-menu-major-mode-map
"23.1"))
271 (interactive "@e\nP")
272 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
273 (popup-menu (mouse-menu-major-mode-map) event prefix
))
275 (defun mouse-popup-menubar (event prefix
)
276 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
277 The contents are the items that would be in the menu bar whether or
278 not it is actually displayed."
279 (declare (obsolete mouse-menu-bar-map
"23.1"))
280 (interactive "@e \nP")
281 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
282 (popup-menu (mouse-menu-bar-map) (unless (integerp event
) event
) prefix
))
284 (defun mouse-popup-menubar-stuff (event prefix
)
285 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
286 Use the former if the menu bar is showing, otherwise the latter."
287 (declare (obsolete nil
"23.1"))
288 (interactive "@e\nP")
289 (run-hooks 'activate-menubar-hook
'menu-bar-update-hook
)
291 (if (zerop (or (frame-parameter nil
'menu-bar-lines
) 0))
293 (mouse-menu-major-mode-map))
296 ;; Commands that operate on windows.
298 (defun mouse-minibuffer-check (event)
299 (let ((w (posn-window (event-start event
))))
300 (and (window-minibuffer-p w
)
301 (not (minibuffer-window-active-p w
))
302 (user-error "Minibuffer window is not active")))
303 ;; Give temporary modes such as isearch a chance to turn off.
304 (run-hooks 'mouse-leave-buffer-hook
))
306 (defun mouse-delete-window (click)
307 "Delete the window you click on.
308 Do nothing if the frame has just one window.
309 This command must be bound to a mouse click."
311 (unless (one-window-p t
)
312 (mouse-minibuffer-check click
)
313 (delete-window (posn-window (event-start click
)))))
315 (defun mouse-select-window (click)
316 "Select the window clicked on; don't move point."
318 (mouse-minibuffer-check click
)
319 (let ((oframe (selected-frame))
320 (frame (window-frame (posn-window (event-start click
)))))
321 (select-window (posn-window (event-start click
)))
324 (or (eq frame oframe
)
325 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
327 (define-obsolete-function-alias 'mouse-tear-off-window
'tear-off-window
"24.4")
328 (defun tear-off-window (click)
329 "Delete the selected window, and create a new frame displaying its buffer."
331 (mouse-minibuffer-check click
)
332 (let* ((window (posn-window (event-start click
)))
333 (buf (window-buffer window
))
334 (frame (make-frame))) ;FIXME: Use pop-to-buffer.
336 (switch-to-buffer buf
)
337 (delete-window window
)))
339 (defun mouse-delete-other-windows ()
340 "Delete all windows except the one you click on."
342 (delete-other-windows))
344 (defun mouse-split-window-vertically (click)
345 "Select Emacs window mouse is on, then split it vertically in half.
346 The window is split at the line clicked on.
347 This command must be bound to a mouse click."
349 (mouse-minibuffer-check click
)
350 (let ((start (event-start click
)))
351 (select-window (posn-window start
))
352 (let ((new-height (1+ (cdr (posn-col-row (event-end click
)))))
353 (first-line window-min-height
)
354 (last-line (- (window-height) window-min-height
)))
355 (if (< last-line first-line
)
356 (user-error "Window too short to split")
357 ;; Bind `window-combination-resize' to nil so we are sure to get
358 ;; the split right at the line clicked on.
359 (let (window-combination-resize)
360 (split-window-vertically
361 (min (max new-height first-line
) last-line
)))))))
363 (defun mouse-split-window-horizontally (click)
364 "Select Emacs window mouse is on, then split it horizontally in half.
365 The window is split at the column clicked on.
366 This command must be bound to a mouse click."
368 (mouse-minibuffer-check click
)
369 (let ((start (event-start click
)))
370 (select-window (posn-window start
))
371 (let ((new-width (1+ (car (posn-col-row (event-end click
)))))
372 (first-col window-min-width
)
373 (last-col (- (window-width) window-min-width
)))
374 (if (< last-col first-col
)
375 (user-error "Window too narrow to split")
376 ;; Bind `window-combination-resize' to nil so we are sure to get
377 ;; the split right at the column clicked on.
378 (let (window-combination-resize)
379 (split-window-horizontally
380 (min (max new-width first-col
) last-col
)))))))
382 (defun mouse-drag-line (start-event line
)
383 "Drag a mode line, header line, or vertical line with the mouse.
384 START-EVENT is the starting mouse event of the drag action. LINE
385 must be one of the symbols `header', `mode', or `vertical'."
386 ;; Give temporary modes such as isearch a chance to turn off.
387 (run-hooks 'mouse-leave-buffer-hook
)
388 (let* ((echo-keystrokes 0)
389 (start (event-start start-event
))
390 (window (posn-window start
))
391 (frame (window-frame window
))
392 ;; `position' records the x- or y-coordinate of the last
394 (position (if (eq line
'vertical
)
395 (+ (window-pixel-left window
)
396 (car (posn-x-y start
)))
397 (+ (window-pixel-top window
)
398 (cdr (posn-x-y start
)))))
399 ;; `last-position' records the x- or y-coordinate of the
400 ;; previously sampled position. The difference of `position'
401 ;; and `last-position' determines the size change of WINDOW.
402 (last-position position
)
404 posn-window growth dragged
)
405 ;; Decide on whether we are allowed to track at all and whose
406 ;; window's edge we drag.
409 ;; Drag bottom edge of window above the header line.
410 (setq window
(window-in-direction 'above window t
)))
413 (let ((divider-width (frame-right-divider-width frame
)))
414 (when (and (or (not (numberp divider-width
))
415 (zerop divider-width
))
416 (eq (frame-parameter frame
'vertical-scroll-bars
) 'left
))
417 (setq window
(window-in-direction 'left window t
))))))
420 (lambda (event) (interactive "e")
425 ;; Drag right edge of `window'.
426 (setq start
(event-start event
))
427 (setq position
(car (posn-x-y start
)))
428 ;; Set `posn-window' to the window where `event' was recorded.
429 ;; This can be `window' or the window on the left or right of
431 (when (window-live-p (setq posn-window
(posn-window start
)))
432 ;; Add left edge of `posn-window' to `position'.
433 (setq position
(+ (window-pixel-left posn-window
) position
))
434 (unless (nth 1 start
)
435 ;; Add width of objects on the left of the text area to
437 (when (eq (window-current-scroll-bars posn-window
) 'left
)
438 (setq position
(+ (window-scroll-bar-width posn-window
)
440 (setq position
(+ (car (window-fringes posn-window
))
441 (or (car (window-margins posn-window
)) 0)
443 ;; When the cursor overshoots after shrinking a window to its
444 ;; minimum size and the dragging direction changes, have the
445 ;; cursor first catch up with the window edge.
446 (unless (or (zerop (setq growth
(- position last-position
)))
448 (< position
(+ (window-pixel-left window
)
449 (window-pixel-width window
))))
451 (> position
(+ (window-pixel-left window
)
452 (window-pixel-width window
)))))
454 (adjust-window-trailing-edge window growth t t
))
455 (setq last-position position
))
457 ;; Drag bottom edge of `window'.
458 (setq start
(event-start event
))
459 ;; Set `posn-window' to the window where `event' was recorded.
460 ;; This can be either `window' or the window above or below of
462 (setq posn-window
(posn-window start
))
463 (setq position
(cdr (posn-x-y start
)))
464 (when (window-live-p posn-window
)
465 ;; Add top edge of `posn-window' to `position'.
466 (setq position
(+ (window-pixel-top posn-window
) position
))
467 ;; If necessary, add height of header line to `position'
468 (when (memq (posn-area start
)
469 '(nil left-fringe right-fringe left-margin right-margin
))
470 (setq position
(+ (window-header-line-height posn-window
) position
))))
471 ;; When the cursor overshoots after shrinking a window to its
472 ;; minimum size and the dragging direction changes, have the
473 ;; cursor first catch up with the window edge.
474 (unless (or (zerop (setq growth
(- position last-position
)))
476 (< position
(+ (window-pixel-top window
)
477 (window-pixel-height window
))))
479 (> position
(+ (window-pixel-top window
)
480 (window-pixel-height window
)))))
482 (adjust-window-trailing-edge window growth nil t
))
483 (setq last-position position
)))))
484 (old-track-mouse track-mouse
))
485 ;; Start tracking. The special value 'dragging' signals the
486 ;; display engine to freeze the mouse pointer shape for as long
488 (setq track-mouse
'dragging
)
489 ;; Loop reading events and sampling the position of the mouse.
492 (let ((map (make-sparse-keymap)))
493 (define-key map
[switch-frame
] #'ignore
)
494 (define-key map
[select-window
] #'ignore
)
495 (define-key map
[scroll-bar-movement
] #'ignore
)
496 (define-key map
[mouse-movement
] move
)
497 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
498 (define-key map
[drag-mouse-1
]
499 (lambda () (interactive) (funcall exitfun
)))
500 ;; For vertical line dragging swallow also a mouse-1
501 ;; event (but only if we dragged at least once to allow mouse-1
502 ;; clicks to get through).
503 (when (eq line
'vertical
)
504 (define-key map
[mouse-1
]
505 `(menu-item "" ,(lambda () (interactive) (funcall exitfun
))
506 :filter
,(lambda (cmd) (if dragged cmd
)))))
507 ;; Some of the events will of course end up looked up
508 ;; with a mode-line, header-line or vertical-line prefix ...
509 (define-key map
[mode-line
] map
)
510 (define-key map
[header-line
] map
)
511 (define-key map
[vertical-line
] map
)
512 ;; ... and some maybe even with a right- or bottom-divider
514 (define-key map
[right-divider
] map
)
515 (define-key map
[bottom-divider
] map
)
517 t
(lambda () (setq track-mouse old-track-mouse
)))))))
519 (defun mouse-drag-mode-line (start-event)
520 "Change the height of a window by dragging on its mode line.
521 START-EVENT is the starting mouse event of the drag action.
523 If the drag happens in a mode line on the bottom of a frame and
524 that frame's `drag-with-mode-line' parameter is non-nil, drag the
527 (let* ((start (event-start start-event
))
528 (window (posn-window start
))
529 (frame (window-frame window
)))
531 ((not (window-live-p window
)))
532 ((or (not (window-at-side-p window
'bottom
))
533 ;; Allow resizing the minibuffer window if it's on the
534 ;; same frame as and immediately below `window', and it's
535 ;; either active or `resize-mini-windows' is nil.
536 (let ((minibuffer-window (minibuffer-window frame
)))
537 (and (eq (window-frame minibuffer-window
) frame
)
538 (or (not resize-mini-windows
)
539 (eq minibuffer-window
540 (active-minibuffer-window))))))
541 (mouse-drag-line start-event
'mode
))
542 ((and (frame-parameter frame
'drag-with-mode-line
)
543 (window-at-side-p window
'bottom
)
544 (let ((minibuffer-window (minibuffer-window frame
)))
545 (not (eq (window-frame minibuffer-window
) frame
))))
546 ;; Drag frame when the window is on the bottom of its frame and
547 ;; there is no minibuffer window below.
548 (mouse-drag-frame start-event
'move
)))))
550 (defun mouse-drag-header-line (start-event)
551 "Change the height of a window by dragging on its header line.
552 START-EVENT is the starting mouse event of the drag action.
554 If the drag happens in a header line on the top of a frame and
555 that frame's `drag-with-header-line' parameter is non-nil, drag
558 (let* ((start (event-start start-event
))
559 (window (posn-window start
)))
560 (if (and (window-live-p window
)
561 (not (window-at-side-p window
'top
)))
562 (mouse-drag-line start-event
'header
)
563 (let ((frame (window-frame window
)))
564 (when (frame-parameter frame
'drag-with-header-line
)
565 (mouse-drag-frame start-event
'move
))))))
567 (defun mouse-drag-vertical-line (start-event)
568 "Change the width of a window by dragging on a vertical line.
569 START-EVENT is the starting mouse event of the drag action."
571 (mouse-drag-line start-event
'vertical
))
573 (defun mouse-resize-frame (frame x-diff y-diff
&optional x-move y-move
)
574 "Helper function for `mouse-drag-frame'."
575 (let* ((frame-x-y (frame-position frame
))
576 (frame-x (car frame-x-y
))
577 (frame-y (cdr frame-x-y
))
581 (setq x-diff
(min x-diff frame-x
))
582 (setq x-move
(- frame-x x-diff
)))
583 (let* ((min-width (frame-windows-min-size frame t nil t
))
584 (min-diff (max 0 (- (frame-inner-width frame
) min-width
))))
585 (setq x-diff
(max x-diff
(- min-diff
)))
587 (setq x-move
(+ frame-x
(- x-diff
))))))
591 (setq y-diff
(min y-diff frame-y
))
592 (setq y-move
(- frame-y y-diff
)))
593 (let* ((min-height (frame-windows-min-size frame nil nil t
))
594 (min-diff (max 0 (- (frame-inner-height frame
) min-height
))))
595 (setq y-diff
(max y-diff
(- min-diff
)))
597 (setq y-move
(+ frame-y
(- y-diff
))))))
599 (unless (zerop x-diff
)
601 (push `(left .
,x-move
) alist
))
602 (push `(width .
(text-pixels .
,(+ (frame-text-width frame
) x-diff
)))
604 (unless (zerop y-diff
)
606 (push `(top .
,y-move
) alist
))
607 (push `(height .
(text-pixels .
,(+ (frame-text-height frame
) y-diff
)))
610 (modify-frame-parameters frame alist
))))
612 (defun mouse-drag-frame (start-event part
)
613 "Drag a frame or one of its edges with the mouse.
614 START-EVENT is the starting mouse event of the drag action. Its
615 position window denotes the frame that will be dragged.
617 PART specifies the part that has been dragged and must be one of
618 the symbols 'left', 'top', 'right', 'bottom', 'top-left',
619 'top-right', 'bottom-left', 'bottom-right' to drag an internal
620 border or edge. If PART equals 'move', this means to move the
621 frame with the mouse."
622 ;; Give temporary modes such as isearch a chance to turn off.
623 (run-hooks 'mouse-leave-buffer-hook
)
624 (let* ((echo-keystrokes 0)
625 (start (event-start start-event
))
626 (window (posn-window start
))
627 ;; FRAME is the frame to drag.
628 (frame (if (window-live-p window
)
629 (window-frame window
)
631 (width (frame-native-width frame
))
632 (height (frame-native-height frame
))
633 ;; PARENT is the parent frame of FRAME or, if FRAME is a
634 ;; top-level frame, FRAME's workarea.
635 (parent (frame-parent frame
))
638 `(0 0 ,(frame-native-width parent
) ,(frame-native-height parent
))
640 (car (display-monitor-attributes-list)))
641 (workarea (assq 'workarea attributes
)))
643 `(,(nth 1 workarea
) ,(nth 2 workarea
)
644 ,(+ (nth 1 workarea
) (nth 3 workarea
))
645 ,(+ (nth 2 workarea
) (nth 4 workarea
)))))))
646 (parent-left (and parent-edges
(nth 0 parent-edges
)))
647 (parent-top (and parent-edges
(nth 1 parent-edges
)))
648 (parent-right (and parent-edges
(nth 2 parent-edges
)))
649 (parent-bottom (and parent-edges
(nth 3 parent-edges
)))
650 ;; `pos-x' and `pos-y' record the x- and y-coordinates of the
651 ;; last sampled mouse position. Note that we sample absolute
652 ;; mouse positions to avoid that moving the mouse from one
653 ;; frame into another gets into our way. `last-x' and `last-y'
654 ;; records the x- and y-coordinates of the previously sampled
655 ;; position. The differences between `last-x' and `pos-x' as
656 ;; well as `last-y' and `pos-y' determine the amount the mouse
657 ;; has been dragged between the last two samples.
659 (last-x-y (mouse-absolute-pixel-position))
660 (last-x (car last-x-y
))
661 (last-y (cdr last-x-y
))
662 ;; `snap-x' and `snap-y' record the x- and y-coordinates of the
663 ;; mouse position when FRAME snapped. As soon as the
664 ;; difference between `pos-x' and `snap-x' (or `pos-y' and
665 ;; `snap-y') exceeds the value of FRAME's `snap-width'
666 ;; parameter, unsnap FRAME (at the respective side). `snap-x'
667 ;; and `snap-y' nil mean FRAME is currently not snapped.
674 (setq pos-x-y
(mouse-absolute-pixel-position))
675 (setq pos-x
(car pos-x-y
))
676 (setq pos-y
(cdr pos-x-y
))
679 (mouse-resize-frame frame
(- last-x pos-x
) 0 t
))
681 (mouse-resize-frame frame
0 (- last-y pos-y
) nil t
))
683 (mouse-resize-frame frame
(- pos-x last-x
) 0))
685 (mouse-resize-frame frame
0 (- pos-y last-y
)))
688 frame
(- last-x pos-x
) (- last-y pos-y
) t t
))
689 ((eq part
'top-right
)
691 frame
(- pos-x last-x
) (- last-y pos-y
) nil t
))
692 ((eq part
'bottom-left
)
694 frame
(- last-x pos-x
) (- pos-y last-y
) t
))
695 ((eq part
'bottom-right
)
697 frame
(- pos-x last-x
) (- pos-y last-y
)))
699 (let* ((old-position (frame-position frame
))
700 (old-left (car old-position
))
701 (old-top (cdr old-position
))
702 (left (+ old-left
(- pos-x last-x
)))
703 (top (+ old-top
(- pos-y last-y
)))
705 ;; `snap-width' (maybe also a yet to be provided
706 ;; `snap-height') could become floats to handle
707 ;; proportionality wrt PARENT. We don't do any
708 ;; checks on this parameter so far.
709 (snap-width (frame-parameter frame
'snap-width
)))
710 ;; Docking and constraining.
711 (when (and (numberp snap-width
) parent-edges
)
713 ;; Docking at the left parent edge.
716 ((and (> left parent-left
)
717 (<= (- left parent-left
) snap-width
))
718 ;; Snap when the mouse moved leftward and
719 ;; FRAME's left edge would end up within
720 ;; `snap-width' pixels from PARENT's left edge.
722 (setq left parent-left
))
723 ((and (<= left parent-left
)
724 (<= (- parent-left left
) snap-width
)
725 snap-x
(<= (- snap-x pos-x
) snap-width
))
726 ;; Stay snapped when the mouse moved leftward
727 ;; but not more than `snap-width' pixels from
728 ;; the time FRAME snapped.
729 (setq left parent-left
))
731 ;; Unsnap when the mouse moved more than
732 ;; `snap-width' pixels leftward from the time
736 (setq right
(+ left width
))
738 ((and (< right parent-right
)
739 (<= (- parent-right right
) snap-width
))
740 ;; Snap when the mouse moved rightward and
741 ;; FRAME's right edge would end up within
742 ;; `snap-width' pixels from PARENT's right edge.
744 (setq left
(- parent-right width
)))
745 ((and (>= right parent-right
)
746 (<= (- right parent-right
) snap-width
)
747 snap-x
(<= (- pos-x snap-x
) snap-width
))
748 ;; Stay snapped when the mouse moved rightward
749 ;; but not more more than `snap-width' pixels
750 ;; from the time FRAME snapped.
751 (setq left
(- parent-right width
)))
753 ;; Unsnap when the mouse moved rightward more
754 ;; than `snap-width' pixels from the time FRAME
756 (setq snap-x nil
)))))
761 ((and (> top parent-top
)
762 (<= (- top parent-top
) snap-width
))
763 ;; Snap when the mouse moved upward and FRAME's
764 ;; top edge would end up within `snap-width'
765 ;; pixels from PARENT's top edge.
767 (setq top parent-top
))
768 ((and (<= top parent-top
)
769 (<= (- parent-top top
) snap-width
)
770 snap-y
(<= (- snap-y pos-y
) snap-width
))
771 ;; Stay snapped when the mouse moved upward but
772 ;; not more more than `snap-width' pixels from
773 ;; the time FRAME snapped.
774 (setq top parent-top
))
776 ;; Unsnap when the mouse moved upward more than
777 ;; `snap-width' pixels from the time FRAME
781 (setq bottom
(+ top height
))
783 ((and (< bottom parent-bottom
)
784 (<= (- parent-bottom bottom
) snap-width
))
785 ;; Snap when the mouse moved downward and
786 ;; FRAME's bottom edge would end up within
787 ;; `snap-width' pixels from PARENT's bottom
790 (setq top
(- parent-bottom height
)))
791 ((and (>= bottom parent-bottom
)
792 (<= (- bottom parent-bottom
) snap-width
)
793 snap-y
(<= (- pos-y snap-y
) snap-width
))
794 ;; Stay snapped when the mouse moved downward
795 ;; but not more more than `snap-width' pixels
796 ;; from the time FRAME snapped.
797 (setq top
(- parent-bottom height
)))
799 ;; Unsnap when the mouse moved downward more
800 ;; than `snap-width' pixels from the time FRAME
802 (setq snap-y nil
))))))
804 ;; If requested, constrain FRAME's draggable areas to
805 ;; PARENT's edges. The `top-visible' parameter should
806 ;; be set when FRAME has a draggable header-line. If
807 ;; set to a number, it ascertains that the top of
808 ;; FRAME is always constrained to the top of PARENT
809 ;; and that at least as many pixels of FRAME as
810 ;; specified by that number are visible on each of the
811 ;; three remaining sides of PARENT.
813 ;; The `bottom-visible' parameter should be set when
814 ;; FRAME has a draggable mode-line. If set to a
815 ;; number, it ascertains that the bottom of FRAME is
816 ;; always constrained to the bottom of PARENT and that
817 ;; at least as many pixels of FRAME as specified by
818 ;; that number are visible on each of the three
819 ;; remaining sides of PARENT.
820 (let ((par (frame-parameter frame
'top-visible
))
823 (setq par
(frame-parameter frame
'bottom-visible
))
824 (setq bottom-visible t
))
825 (when (and (numberp par
) parent-edges
)
827 (max (min (- parent-right par
) left
)
828 (+ (- parent-left width
) par
)))
831 (min (max top
(- parent-top
(- height par
)))
832 (- parent-bottom height
))
833 (min (max top parent-top
)
834 (- parent-bottom par
))))))
836 ;; Use `modify-frame-parameters' since `left' and
837 ;; `top' may want to move FRAME out of its PARENT.
838 (modify-frame-parameters
840 `((left .
(+ ,left
)) (top .
(+ ,top
)))))))
842 (setq last-y pos-y
))))
843 (old-track-mouse track-mouse
))
844 ;; Start tracking. The special value 'dragging' signals the
845 ;; display engine to freeze the mouse pointer shape for as long
847 (setq track-mouse
'dragging
)
848 ;; Loop reading events and sampling the position of the mouse.
851 (let ((map (make-sparse-keymap)))
852 (define-key map
[switch-frame
] #'ignore
)
853 (define-key map
[select-window
] #'ignore
)
854 (define-key map
[scroll-bar-movement
] #'ignore
)
855 (define-key map
[mouse-movement
] move
)
856 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
857 (define-key map
[drag-mouse-1
]
858 (lambda () (interactive) (funcall exitfun
)))
859 ;; Some of the events will of course end up looked up
860 ;; with a mode-line, header-line or vertical-line prefix ...
861 (define-key map
[mode-line
] map
)
862 (define-key map
[header-line
] map
)
863 (define-key map
[vertical-line
] map
)
864 ;; ... and some maybe even with a right- or bottom-divider
866 (define-key map
[right-divider
] map
)
867 (define-key map
[bottom-divider
] map
)
869 t
(lambda () (setq track-mouse old-track-mouse
))))))
871 (defun mouse-drag-left-edge (start-event)
872 "Drag left edge of a frame with the mouse.
873 START-EVENT is the starting mouse event of the drag action."
875 (mouse-drag-frame start-event
'left
))
877 (defun mouse-drag-top-left-corner (start-event)
878 "Drag top left corner of a frame with the mouse.
879 START-EVENT is the starting mouse event of the drag action."
881 (mouse-drag-frame start-event
'top-left
))
883 (defun mouse-drag-top-edge (start-event)
884 "Drag top edge of a frame with the mouse.
885 START-EVENT is the starting mouse event of the drag action."
887 (mouse-drag-frame start-event
'top
))
889 (defun mouse-drag-top-right-corner (start-event)
890 "Drag top right corner of a frame with the mouse.
891 START-EVENT is the starting mouse event of the drag action."
893 (mouse-drag-frame start-event
'top-right
))
895 (defun mouse-drag-right-edge (start-event)
896 "Drag right edge of a frame with the mouse.
897 START-EVENT is the starting mouse event of the drag action."
899 (mouse-drag-frame start-event
'right
))
901 (defun mouse-drag-bottom-right-corner (start-event)
902 "Drag bottom right corner of a frame with the mouse.
903 START-EVENT is the starting mouse event of the drag action."
905 (mouse-drag-frame start-event
'bottom-right
))
907 (defun mouse-drag-bottom-edge (start-event)
908 "Drag bottom edge of a frame with the mouse.
909 START-EVENT is the starting mouse event of the drag action."
911 (mouse-drag-frame start-event
'bottom
))
913 (defun mouse-drag-bottom-left-corner (start-event)
914 "Drag bottom left corner of a frame with the mouse.
915 START-EVENT is the starting mouse event of the drag action."
917 (mouse-drag-frame start-event
'bottom-left
))
919 (defcustom mouse-select-region-move-to-beginning nil
920 "Effect of selecting a region extending backward from double click.
921 Nil means keep point at the position clicked (region end);
922 non-nil means move point to beginning of region."
923 :type
'(choice (const :tag
"Don't move point" nil
)
924 (const :tag
"Move point to beginning of region" t
))
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'."
934 (mouse-minibuffer-check event
)
935 (if (and promote-to-region
(> (event-click-count event
) 1))
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."
964 (mouse-minibuffer-check click
)
965 (select-window (posn-window (event-start click
)))
966 (let ((beg (posn-point (event-start click
)))
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).
973 (click-count (event-click-count click
)))
974 (let ((drag-start (terminal-parameter nil
'mouse-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
)))
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
))))
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
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
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."
1032 (defcustom mouse-scroll-min-lines
1
1033 "The minimum number of lines scrolled by dragging mouse out of window.
1034 Moving the mouse out the top or bottom edge of the window begins
1035 scrolling repeatedly. The number of lines scrolled per repetition
1036 is normally equal to the number of lines beyond the window edge that
1037 the mouse has moved. However, it always scrolls at least the number
1038 of lines specified by this variable."
1041 (defun mouse-scroll-subr (window jump
&optional overlay start
)
1042 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
1043 If OVERLAY is an overlay, let it stretch from START to the far edge of
1044 the newly visible text.
1045 Upon exit, point is at the far edge of the newly visible text."
1047 ((and (> jump
0) (< jump mouse-scroll-min-lines
))
1048 (setq jump mouse-scroll-min-lines
))
1049 ((and (< jump
0) (< (- jump
) mouse-scroll-min-lines
))
1050 (setq jump
(- mouse-scroll-min-lines
))))
1051 (let ((opoint (point)))
1053 (goto-char (window-start window
))
1054 (if (not (zerop (vertical-motion jump window
)))
1056 (set-window-start window
(point))
1058 (if (window-end window
)
1060 (goto-char (window-end window
))
1061 ;; window-end doesn't reflect the window's new
1062 ;; start position until the next redisplay.
1063 (vertical-motion (1- jump
) window
))
1064 (vertical-motion (- (window-height window
) 2)))
1065 (goto-char (window-start window
)))
1067 (move-overlay overlay start
(point)))
1068 ;; Now that we have scrolled WINDOW properly,
1069 ;; put point back where it was for the redisplay
1070 ;; so that we don't mess up the selected window.
1071 (or (eq window
(selected-window))
1073 (sit-for mouse-scroll-delay
)))))
1074 (or (eq window
(selected-window))
1075 (goto-char opoint
))))
1077 (defvar mouse-selection-click-count
0)
1079 (defvar mouse-selection-click-count-buffer nil
)
1081 (defun mouse-drag-region (start-event)
1082 "Set the region to the text that the mouse is dragged over.
1083 Highlight the drag area as you move the mouse.
1084 This must be bound to a button-down mouse event.
1085 In Transient Mark mode, the highlighting remains as long as the mark
1086 remains active. Otherwise, it remains until the next input event.
1088 When the region already exists and `mouse-drag-and-drop-region'
1089 is non-nil, this moves the entire region of text to where mouse
1090 is dragged over to."
1092 (if (and mouse-drag-and-drop-region
1093 (not (member 'triple
(event-modifiers start-event
)))
1094 (equal (mouse-posn-property (event-start start-event
) 'face
) 'region
))
1095 (mouse-drag-and-drop-region start-event
)
1096 ;; Give temporary modes such as isearch a chance to turn off.
1097 (run-hooks 'mouse-leave-buffer-hook
)
1098 (mouse-drag-track start-event
)))
1100 (defun mouse-posn-property (pos property
)
1101 "Look for a property at click position.
1102 POS may be either a buffer position or a click position like
1103 those returned from `event-start'. If the click position is on
1104 a string, the text property PROPERTY is examined.
1105 If this is nil or the click is not on a string, then
1106 the corresponding buffer position is searched for PROPERTY.
1107 If PROPERTY is encountered in one of those places,
1108 its value is returned."
1110 (let ((w (posn-window pos
)) (pt (posn-point pos
))
1111 (str (posn-string pos
)))
1113 (get-text-property (cdr str
) property
(car str
)))
1114 ;; Mouse clicks in the fringe come with a position in
1115 ;; (nth 5). This is useful but is not exactly where we clicked, so
1116 ;; don't look up that position's properties!
1117 (and pt
(not (memq (posn-area pos
) '(left-fringe right-fringe
1118 left-margin right-margin
)))
1119 (get-char-property pt property w
))))
1120 (get-char-property pos property
)))
1122 (defun mouse-on-link-p (pos)
1123 "Return non-nil if POS is on a link in the current buffer.
1124 POS must specify a buffer position in the current buffer, as a list
1125 of the form returned by the `event-start' and `event-end' functions,
1126 or a mouse event location in the selected window (see `event-start').
1127 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
1128 POS may be a mouse event location in any window.
1130 A clickable link is identified by one of the following methods:
1132 - If the character at POS has a non-nil `follow-link' text or
1133 overlay property, the value of that property determines what to do.
1135 - If there is a local key-binding or a keybinding at position POS
1136 for the `follow-link' event, the binding of that event determines
1139 The resulting value determine whether POS is inside a link:
1141 - If the value is `mouse-face', POS is inside a link if there
1142 is a non-nil `mouse-face' property at POS. Return t in this case.
1144 - If the value is a function, FUNC, POS is inside a link if
1145 the call (FUNC POS) returns non-nil. Return the return value
1146 from that call. Arg is (posn-point POS) if POS is a mouse event.
1148 - Otherwise, return the value itself.
1150 The return value is interpreted as follows:
1152 - If it is an array, the mouse-1 event is translated into the
1153 first element of that array, i.e. the action of the mouse-1
1154 click is the local or global binding of that event.
1156 - Otherwise, the mouse-1 event is translated into a mouse-2 event
1157 at the same position."
1159 (and (or (not (consp pos
))
1160 mouse-1-click-in-non-selected-windows
1161 (eq (selected-window) (posn-window pos
)))
1162 (or (mouse-posn-property pos
'follow-link
)
1163 (let ((area (posn-area pos
)))
1165 (key-binding (vector area
'follow-link
) nil t pos
)))
1166 (key-binding [follow-link
] nil t pos
)))))
1168 ((eq action
'mouse-face
)
1169 (and (mouse-posn-property pos
'mouse-face
) t
))
1171 ;; FIXME: This seems questionable if the click is not in a buffer.
1172 ;; Should we instead decide that `action' takes a `posn'?
1174 (with-current-buffer (window-buffer (posn-window pos
))
1175 (funcall action
(posn-point pos
)))
1176 (funcall action pos
)))
1179 (defun mouse-fixup-help-message (msg)
1180 "Fix help message MSG for `mouse-1-click-follows-link'."
1182 (if (and mouse-1-click-follows-link
1184 (string-match-p "\\`mouse-2" msg
)
1185 (setq mp
(mouse-pixel-position))
1186 (consp (setq pos
(cdr mp
)))
1187 (car pos
) (>= (car pos
) 0)
1188 (cdr pos
) (>= (cdr pos
) 0)
1189 (setq pos
(posn-at-x-y (car pos
) (cdr pos
) (car mp
)))
1190 (windowp (posn-window pos
)))
1191 (with-current-buffer (window-buffer (posn-window pos
))
1192 (if (mouse-on-link-p pos
)
1195 ((eq mouse-1-click-follows-link
'double
) "double-")
1196 ((and (integerp mouse-1-click-follows-link
)
1197 (< mouse-1-click-follows-link
0)) "Long ")
1199 "mouse-1" (substring msg
7)))))))
1202 (defun mouse-drag-track (start-event)
1203 "Track mouse drags by highlighting area between point and cursor.
1204 The region will be defined with mark and point."
1205 (mouse-minibuffer-check start-event
)
1206 (setq mouse-selection-click-count-buffer
(current-buffer))
1208 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
1209 (start-posn (event-start start-event
))
1210 (start-point (posn-point start-posn
))
1211 (start-window (posn-window start-posn
))
1212 (_ (with-current-buffer (window-buffer start-window
)
1213 (setq deactivate-mark nil
)))
1214 ;; We've recorded what we needed from the current buffer and
1215 ;; window, now let's jump to the place of the event, where things
1217 (_ (mouse-set-point start-event
))
1219 (bounds (window-edges start-window
))
1220 (make-cursor-line-fully-visible nil
)
1221 (top (nth 1 bounds
))
1222 (bottom (if (window-minibuffer-p start-window
)
1224 ;; Don't count the mode line.
1225 (1- (nth 3 bounds
))))
1226 (click-count (1- (event-click-count start-event
)))
1227 ;; Suppress automatic hscrolling, because that is a nuisance
1228 ;; when setting point near the right fringe (but see below).
1229 (auto-hscroll-mode-saved auto-hscroll-mode
)
1230 (old-track-mouse track-mouse
))
1232 (setq mouse-selection-click-count click-count
)
1233 ;; In case the down click is in the middle of some intangible text,
1234 ;; use the end of that text, and put it in START-POINT.
1235 (if (< (point) start-point
)
1236 (goto-char start-point
))
1237 (setq start-point
(point))
1239 ;; Activate the region, using `mouse-start-end' to determine where
1240 ;; to put point and mark (e.g., double-click will select a word).
1241 (setq-local transient-mark-mode
1242 (if (eq transient-mark-mode
'lambda
)
1244 (cons 'only transient-mark-mode
)))
1245 (let ((range (mouse-start-end start-point start-point click-count
)))
1246 (push-mark (nth 0 range
) t t
)
1247 (goto-char (nth 1 range
)))
1249 (setf (terminal-parameter nil
'mouse-drag-start
) start-event
)
1250 (setq track-mouse t
)
1251 (setq auto-hscroll-mode nil
)
1254 (let ((map (make-sparse-keymap)))
1255 (define-key map
[switch-frame
] #'ignore
)
1256 (define-key map
[select-window
] #'ignore
)
1257 (define-key map
[mouse-movement
]
1258 (lambda (event) (interactive "e")
1259 (let* ((end (event-end event
))
1260 (end-point (posn-point end
)))
1261 (unless (eq end-point start-point
)
1262 ;; As soon as the user moves, we can re-enable auto-hscroll.
1263 (setq auto-hscroll-mode auto-hscroll-mode-saved
)
1264 ;; And remember that we have moved, so mouse-set-region can know
1265 ;; its event is really a drag event.
1266 (setcar start-event
'mouse-movement
))
1267 (if (and (eq (posn-window end
) start-window
)
1268 (integer-or-marker-p end-point
))
1269 (mouse--drag-set-mark-and-point start-point
1270 end-point click-count
)
1271 (let ((mouse-row (cdr (cdr (mouse-position)))))
1275 (mouse-scroll-subr start-window
(- mouse-row top
)
1277 ((>= mouse-row bottom
)
1278 (mouse-scroll-subr start-window
(1+ (- mouse-row bottom
))
1279 nil start-point
))))))))
1282 (setq track-mouse old-track-mouse
)
1283 (setq auto-hscroll-mode auto-hscroll-mode-saved
)
1287 (defun mouse--drag-set-mark-and-point (start click click-count
)
1288 (let* ((range (mouse-start-end start click click-count
))
1290 (end (nth 1 range
)))
1291 (cond ((eq (mark) beg
)
1302 ;; Commands to handle xterm-style multiple clicks.
1303 (defun mouse-skip-word (dir)
1304 "Skip over word, over whitespace, or over identical punctuation.
1305 If DIR is positive skip forward; if negative, skip backward."
1306 (let* ((char (following-char))
1307 (syntax (char-to-string (char-syntax char
))))
1308 (cond ((string= syntax
"w")
1309 ;; Here, we can't use skip-syntax-forward/backward because
1310 ;; they don't pay attention to word-separating-categories,
1311 ;; and thus they will skip over a true word boundary. So,
1312 ;; we simulate the original behavior by using forward-word.
1314 (if (not (looking-at "\\<"))
1316 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1318 ((string= syntax
" ")
1320 (skip-syntax-backward syntax
)
1321 (skip-syntax-forward syntax
)))
1322 ((string= syntax
"_")
1324 (skip-syntax-backward "w_")
1325 (skip-syntax-forward "w_")))
1327 (while (and (not (bobp)) (= (preceding-char) char
))
1330 (while (and (not (eobp)) (= (following-char) char
))
1331 (forward-char 1))))))
1333 (defun mouse-start-end (start end mode
)
1334 "Return a list of region bounds based on START and END according to MODE.
1335 If MODE is 0 then set point to (min START END), mark to (max START END).
1336 If MODE is 1 then set point to start of word at (min START END),
1337 mark to end of word at (max START END).
1338 If MODE is 2 then do the same for lines."
1343 (setq mode
(mod mode
3))
1349 (= (char-syntax (char-after start
)) ?\
())
1350 (if (/= (syntax-class (syntax-after start
)) 4) ; raw syntax code for ?\(
1351 ;; This happens in CC Mode when unbalanced parens in CPP
1352 ;; constructs are given punctuation syntax with
1353 ;; syntax-table text properties. (2016-02-21).
1354 (signal 'scan-error
(list "Containing expression ends prematurely"
1364 (= (char-syntax (char-after start
)) ?\
)))
1365 (if (/= (syntax-class (syntax-after start
)) 5) ; raw syntax code for ?\)
1366 ;; See above comment about CC Mode.
1367 (signal 'scan-error
(list "Unbalanced parentheses" start start
))
1368 (list (save-excursion
1369 (goto-char (1+ start
))
1376 (= (char-syntax (char-after start
)) ?
\"))
1377 (let ((open (or (eq start
(point-min))
1379 (goto-char (- start
1))
1380 (looking-at "\\s(\\|\\s \\|\\s>")))))
1390 (list (save-excursion
1393 (goto-char (1+ start
))
1399 (list (save-excursion
1401 (mouse-skip-word -
1)
1408 (list (save-excursion
1410 (line-beginning-position 1))
1416 ;; Subroutine: set the mark where CLICK happened,
1417 ;; but don't do anything else.
1418 (defun mouse-set-mark-fast (click)
1419 (mouse-minibuffer-check click
)
1420 (let ((posn (event-start click
)))
1421 (select-window (posn-window posn
))
1422 (if (numberp (posn-point posn
))
1423 (push-mark (posn-point posn
) t t
))))
1425 (defun mouse-undouble-last-event (events)
1426 (let* ((index (1- (length events
)))
1427 (last (nthcdr index events
))
1429 (basic (event-basic-type event
))
1430 (old-modifiers (event-modifiers event
))
1431 (modifiers (delq 'double
(delq 'triple
(copy-sequence old-modifiers
))))
1434 ;; Use reverse, not nreverse, since event-modifiers
1435 ;; does not copy the list it returns.
1436 (cons (event-convert-list (reverse (cons basic modifiers
)))
1440 (if (and (not (equal modifiers old-modifiers
))
1441 (key-binding (apply 'vector events
)))
1446 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1448 (defun mouse-set-mark (click)
1449 "Set mark at the position clicked on with the mouse.
1450 Display cursor at that position for a second.
1451 This must be bound to a mouse click."
1453 (mouse-minibuffer-check click
)
1454 (select-window (posn-window (event-start click
)))
1455 ;; FIXME: Use save-excursion
1456 (let ((point-save (point)))
1458 (progn (mouse-set-point click
)
1460 (or transient-mark-mode
1462 (goto-char point-save
))))
1464 (defun mouse-kill (click)
1465 "Kill the region between point and the mouse click.
1466 The text is saved in the kill ring, as with \\[kill-region]."
1468 (mouse-minibuffer-check click
)
1469 (let* ((posn (event-start click
))
1470 (click-posn (posn-point posn
)))
1471 (select-window (posn-window posn
))
1472 (if (numberp click-posn
)
1473 (kill-region (min (point) click-posn
)
1474 (max (point) click-posn
)))))
1476 (defun mouse-yank-at-click (click arg
)
1477 "Insert the last stretch of killed text at the position clicked on.
1478 Also move point to one end of the text thus inserted (normally the end),
1479 and set mark at the beginning.
1480 Prefix arguments are interpreted as with \\[yank].
1481 If `mouse-yank-at-point' is non-nil, insert at point
1482 regardless of where you click."
1483 (interactive "e\nP")
1484 ;; Give temporary modes such as isearch a chance to turn off.
1485 (run-hooks 'mouse-leave-buffer-hook
)
1486 (when select-active-regions
1487 ;; Without this, confusing things happen upon e.g. inserting into
1488 ;; the middle of an active region.
1490 (or mouse-yank-at-point
(mouse-set-point click
))
1491 (setq this-command
'yank
)
1492 (setq mouse-selection-click-count
0)
1495 (defun mouse-yank-primary (click)
1496 "Insert the primary selection at the position clicked on.
1497 Move point to the end of the inserted text, and set mark at
1498 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1499 regardless of where you click."
1501 ;; Give temporary modes such as isearch a chance to turn off.
1502 (run-hooks 'mouse-leave-buffer-hook
)
1503 ;; Without this, confusing things happen upon e.g. inserting into
1504 ;; the middle of an active region.
1505 (when select-active-regions
1506 (let (select-active-regions)
1508 (or mouse-yank-at-point
(mouse-set-point click
))
1509 (let ((primary (gui-get-primary-selection)))
1511 (insert-for-yank primary
)))
1513 (defun mouse-kill-ring-save (click)
1514 "Copy the region between point and the mouse click in the kill ring.
1515 This does not delete the region; it acts like \\[kill-ring-save]."
1517 (mouse-set-mark-fast click
)
1518 (let (this-command last-command
)
1519 (kill-ring-save (point) (mark t
))))
1521 ;; This function used to delete the text between point and the mouse
1522 ;; whenever it was equal to the front of the kill ring, but some
1523 ;; people found that confusing.
1525 ;; The position of the last invocation of `mouse-save-then-kill'.
1526 (defvar mouse-save-then-kill-posn nil
)
1528 (defun mouse-save-then-kill-delete-region (beg end
)
1529 ;; We must make our own undo boundaries
1530 ;; because they happen automatically only for the current buffer.
1532 (if (or (= beg end
) (eq buffer-undo-list t
))
1533 ;; If we have no undo list in this buffer,
1535 (delete-region beg end
)
1536 ;; Delete, but make the undo-list entry share with the kill ring.
1537 ;; First, delete just one char, so in case buffer is being modified
1538 ;; for the first time, the undo list records that fact.
1539 (let ((inhibit-modification-hooks t
))
1541 (+ beg
(if (> end beg
) 1 -
1))))
1542 (let ((buffer-undo-list buffer-undo-list
))
1543 ;; Undo that deletion--but don't change the undo list!
1544 (let ((inhibit-modification-hooks t
))
1545 (primitive-undo 1 buffer-undo-list
))
1546 ;; Now delete the rest of the specified region,
1547 ;; but don't record it.
1548 (setq buffer-undo-list t
)
1549 (if (/= (length (car kill-ring
)) (- (max end beg
) (min end beg
)))
1550 (error "Lossage in mouse-save-then-kill-delete-region"))
1551 (delete-region beg end
))
1552 (let ((tail buffer-undo-list
))
1553 ;; Search back in buffer-undo-list for the string
1554 ;; that came from deleting one character.
1555 (while (and tail
(not (stringp (car (car tail
)))))
1556 (setq tail
(cdr tail
)))
1557 ;; Replace it with an entry for the entire deleted text.
1559 (setcar tail
(cons (car kill-ring
) (min beg end
))))))
1562 (defun mouse-save-then-kill (click)
1563 "Set the region according to CLICK; the second time, kill it.
1564 CLICK should be a mouse click event.
1566 If the region is inactive, activate it temporarily. Set mark at
1567 the original point, and move point to the position of CLICK.
1569 If the region is already active, adjust it. Normally, do this by
1570 moving point or mark, whichever is closer, to CLICK. But if you
1571 have selected whole words or lines, move point or mark to the
1572 word or line boundary closest to CLICK instead.
1574 If `mouse-drag-copy-region' is non-nil, this command also saves the
1575 new region to the kill ring (replacing the previous kill if the
1576 previous region was just saved to the kill ring).
1578 If this command is called a second consecutive time with the same
1579 CLICK position, kill the region (or delete it
1580 if `mouse-drag-copy-region' is non-nil)"
1582 (mouse-minibuffer-check click
)
1583 (let* ((posn (event-start click
))
1584 (click-pt (posn-point posn
))
1585 (window (posn-window posn
))
1586 (buf (window-buffer window
))
1587 ;; Don't let a subsequent kill command append to this one.
1588 (this-command this-command
)
1589 ;; Check if the user has multi-clicked to select words/lines.
1591 (if (and (eq mouse-selection-click-count-buffer buf
)
1592 (with-current-buffer buf
(mark t
)))
1593 mouse-selection-click-count
1596 ((not (numberp click-pt
)) nil
)
1597 ;; If the user clicked without moving point, kill the region.
1598 ;; This also resets `mouse-selection-click-count'.
1599 ((and (eq last-command
'mouse-save-then-kill
)
1600 (eq click-pt mouse-save-then-kill-posn
)
1601 (eq window
(selected-window)))
1602 (if mouse-drag-copy-region
1603 ;; Region already saved in the previous click;
1604 ;; don't make a duplicate entry, just delete.
1605 (delete-region (mark t
) (point))
1606 (kill-region (mark t
) (point)))
1607 (setq mouse-selection-click-count
0)
1608 (setq mouse-save-then-kill-posn nil
))
1610 ;; Otherwise, if there is a suitable region, adjust it by moving
1611 ;; one end (whichever is closer) to CLICK-PT.
1612 ((or (with-current-buffer buf
(region-active-p))
1613 (and (eq window
(selected-window))
1615 (or (and (eq last-command
'mouse-save-then-kill
)
1616 mouse-save-then-kill-posn
)
1617 (and (memq last-command
'(mouse-drag-region
1619 (or mark-even-if-inactive
1620 (not transient-mark-mode
))))))
1621 (select-window window
)
1622 (let* ((range (mouse-start-end click-pt click-pt click-count
)))
1623 (if (< (abs (- click-pt
(mark t
)))
1624 (abs (- click-pt
(point))))
1625 (set-mark (car range
))
1626 (goto-char (nth 1 range
)))
1627 (setq deactivate-mark nil
)
1628 (mouse-set-region-1)
1629 (when mouse-drag-copy-region
1630 ;; Region already copied to kill-ring once, so replace.
1631 (kill-new (filter-buffer-substring (mark t
) (point)) t
))
1632 ;; Arrange for a repeated mouse-3 to kill the region.
1633 (setq mouse-save-then-kill-posn click-pt
)))
1635 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1637 (select-window window
)
1638 (mouse-set-mark-fast click
)
1639 (let ((before-scroll (with-current-buffer buf point-before-scroll
)))
1640 (if before-scroll
(goto-char before-scroll
)))
1641 (exchange-point-and-mark)
1642 (mouse-set-region-1)
1643 (when mouse-drag-copy-region
1644 (kill-new (filter-buffer-substring (mark t
) (point))))
1645 (setq mouse-save-then-kill-posn click-pt
)))))
1648 (global-set-key [M-mouse-1
] 'mouse-start-secondary
)
1649 (global-set-key [M-drag-mouse-1
] 'mouse-set-secondary
)
1650 (global-set-key [M-down-mouse-1
] 'mouse-drag-secondary
)
1651 (global-set-key [M-mouse-3
] 'mouse-secondary-save-then-kill
)
1652 (global-set-key [M-mouse-2
] 'mouse-yank-secondary
)
1654 (defconst mouse-secondary-overlay
1655 (let ((ol (make-overlay (point-min) (point-min))))
1657 (overlay-put ol
'face
'secondary-selection
)
1659 "An overlay which records the current secondary selection.
1660 It is deleted when there is no secondary selection.")
1662 (defvar mouse-secondary-click-count
0)
1664 ;; A marker which records the specified first end for a secondary selection.
1666 (defvar mouse-secondary-start nil
)
1668 (defun mouse-start-secondary (click)
1669 "Set one end of the secondary selection to the position clicked on.
1670 Use \\[mouse-secondary-save-then-kill] to set the other end
1671 and complete the secondary selection."
1673 (mouse-minibuffer-check click
)
1674 (let ((posn (event-start click
)))
1675 (with-current-buffer (window-buffer (posn-window posn
))
1676 ;; Cancel any preexisting secondary selection.
1677 (delete-overlay mouse-secondary-overlay
)
1678 (if (numberp (posn-point posn
))
1680 (or mouse-secondary-start
1681 (setq mouse-secondary-start
(make-marker)))
1682 (move-marker mouse-secondary-start
(posn-point posn
)))))))
1684 (defun mouse-set-secondary (click)
1685 "Set the secondary selection to the text that the mouse is dragged over.
1686 This must be bound to a mouse drag event."
1688 (mouse-minibuffer-check click
)
1689 (let ((posn (event-start click
))
1691 (end (event-end click
)))
1692 (with-current-buffer (window-buffer (posn-window posn
))
1693 (if (numberp (posn-point posn
))
1694 (setq beg
(posn-point posn
)))
1695 (move-overlay mouse-secondary-overlay beg
(posn-point end
))
1698 (buffer-substring (overlay-start mouse-secondary-overlay
)
1699 (overlay-end mouse-secondary-overlay
))))))
1701 (defun mouse-drag-secondary (start-event)
1702 "Set the secondary selection to the text that the mouse is dragged over.
1703 Highlight the drag area as you move the mouse.
1704 This must be bound to a button-down mouse event.
1705 The function returns a non-nil value if it creates a secondary selection."
1707 (mouse-minibuffer-check start-event
)
1708 (let* ((echo-keystrokes 0)
1709 (start-posn (event-start start-event
))
1710 (start-point (posn-point start-posn
))
1711 (start-window (posn-window start-posn
))
1712 (bounds (window-edges start-window
))
1713 (top (nth 1 bounds
))
1714 (bottom (if (window-minibuffer-p start-window
)
1716 ;; Don't count the mode line.
1717 (1- (nth 3 bounds
))))
1718 (click-count (1- (event-click-count start-event
))))
1719 (with-current-buffer (window-buffer start-window
)
1720 (setq mouse-secondary-click-count click-count
)
1721 (if (> (mod click-count
3) 0)
1722 ;; Double or triple press: make an initial selection
1723 ;; of one word or line.
1724 (let ((range (mouse-start-end start-point start-point click-count
)))
1725 (set-marker mouse-secondary-start nil
)
1726 (move-overlay mouse-secondary-overlay
(car range
) (nth 1 range
)
1727 (window-buffer start-window
)))
1728 ;; Single-press: cancel any preexisting secondary selection.
1729 (or mouse-secondary-start
1730 (setq mouse-secondary-start
(make-marker)))
1731 (set-marker mouse-secondary-start start-point
)
1732 (delete-overlay mouse-secondary-overlay
))
1733 ;; FIXME: Use mouse-drag-track!
1734 (let (event end end-point
)
1737 (setq event
(read-event))
1738 (or (mouse-movement-p event
)
1739 (memq (car-safe event
) '(switch-frame select-window
))))
1741 (if (memq (car-safe event
) '(switch-frame select-window
))
1743 (setq end
(event-end event
)
1744 end-point
(posn-point end
))
1746 ;; Are we moving within the original window?
1747 ((and (eq (posn-window end
) start-window
)
1748 (integer-or-marker-p end-point
))
1749 (let ((range (mouse-start-end start-point end-point
1751 (if (or (/= start-point end-point
)
1752 (null (marker-position mouse-secondary-start
)))
1754 (set-marker mouse-secondary-start nil
)
1755 (move-overlay mouse-secondary-overlay
1756 (car range
) (nth 1 range
))))))
1758 (let ((mouse-row (cdr (cdr (mouse-position)))))
1762 (mouse-scroll-subr start-window
(- mouse-row top
)
1763 mouse-secondary-overlay start-point
))
1764 ((>= mouse-row bottom
)
1765 (mouse-scroll-subr start-window
(1+ (- mouse-row bottom
))
1766 mouse-secondary-overlay start-point
)))))))))
1769 (if (marker-position mouse-secondary-start
)
1770 (save-window-excursion
1771 (delete-overlay mouse-secondary-overlay
)
1772 (gui-set-selection 'SECONDARY nil
)
1773 (select-window start-window
)
1775 (goto-char mouse-secondary-start
)
1780 (buffer-substring (overlay-start mouse-secondary-overlay
)
1781 (overlay-end mouse-secondary-overlay
)))))))))
1783 (defun mouse-yank-secondary (click)
1784 "Insert the secondary selection at the position clicked on.
1785 Move point to the end of the inserted text.
1786 If `mouse-yank-at-point' is non-nil, insert at point
1787 regardless of where you click."
1789 ;; Give temporary modes such as isearch a chance to turn off.
1790 (run-hooks 'mouse-leave-buffer-hook
)
1791 (or mouse-yank-at-point
(mouse-set-point click
))
1792 (let ((secondary (gui-get-selection 'SECONDARY
)))
1794 (insert-for-yank secondary
)
1795 (error "No secondary selection"))))
1797 (defun mouse-kill-secondary ()
1798 "Kill the text in the secondary selection.
1799 This is intended more as a keyboard command than as a mouse command
1800 but it can work as either one.
1802 The current buffer (in case of keyboard use), or the buffer clicked on,
1803 must be the one that the secondary selection is in. This requirement
1804 is to prevent accidents."
1806 (let* ((keys (this-command-keys))
1807 (click (elt keys
(1- (length keys
)))))
1808 (or (eq (overlay-buffer mouse-secondary-overlay
)
1810 (window-buffer (posn-window (event-start click
)))
1812 (error "Select or click on the buffer where the secondary selection is")))
1814 (with-current-buffer (overlay-buffer mouse-secondary-overlay
)
1815 (kill-region (overlay-start mouse-secondary-overlay
)
1816 (overlay-end mouse-secondary-overlay
))))
1817 (delete-overlay mouse-secondary-overlay
))
1819 (defun mouse-secondary-save-then-kill (click)
1820 "Set the secondary selection and save it to the kill ring.
1821 The second time, kill it. CLICK should be a mouse click event.
1823 If you have not called `mouse-start-secondary' in the clicked
1824 buffer, activate the secondary selection and set it between point
1825 and the click position CLICK.
1827 Otherwise, adjust the bounds of the secondary selection.
1828 Normally, do this by moving its beginning or end, whichever is
1829 closer, to CLICK. But if you have selected whole words or lines,
1830 adjust to the word or line boundary closest to CLICK instead.
1832 If this command is called a second consecutive time with the same
1833 CLICK position, kill the secondary selection."
1835 (mouse-minibuffer-check click
)
1836 (let* ((posn (event-start click
))
1837 (click-pt (posn-point posn
))
1838 (window (posn-window posn
))
1839 (buf (window-buffer window
))
1840 ;; Don't let a subsequent kill command append to this one.
1841 (this-command this-command
)
1842 ;; Check if the user has multi-clicked to select words/lines.
1844 (if (eq (overlay-buffer mouse-secondary-overlay
) buf
)
1845 mouse-secondary-click-count
1847 (beg (overlay-start mouse-secondary-overlay
))
1848 (end (overlay-end mouse-secondary-overlay
)))
1851 ((not (numberp click-pt
)) nil
)
1853 ;; If the secondary selection is not active in BUF, activate it.
1854 ((not (eq buf
(or (overlay-buffer mouse-secondary-overlay
)
1855 (if mouse-secondary-start
1856 (marker-buffer mouse-secondary-start
)))))
1857 (select-window window
)
1858 (setq mouse-secondary-start
(make-marker))
1859 (move-marker mouse-secondary-start
(point))
1860 (move-overlay mouse-secondary-overlay
(point) click-pt buf
)
1861 (kill-ring-save (point) click-pt
))
1863 ;; If the user clicked without moving point, delete the secondary
1864 ;; selection. This also resets `mouse-secondary-click-count'.
1865 ((and (eq last-command
'mouse-secondary-save-then-kill
)
1866 (eq click-pt mouse-save-then-kill-posn
)
1867 (eq window
(selected-window)))
1868 (mouse-save-then-kill-delete-region beg end
)
1869 (delete-overlay mouse-secondary-overlay
)
1870 (setq mouse-secondary-click-count
0)
1871 (setq mouse-save-then-kill-posn nil
))
1873 ;; Otherwise, if there is a suitable secondary selection overlay,
1874 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1875 ((and beg
(eq buf
(overlay-buffer mouse-secondary-overlay
)))
1876 (let* ((range (mouse-start-end click-pt click-pt click-count
)))
1877 (if (< (abs (- click-pt beg
))
1878 (abs (- click-pt end
)))
1879 (move-overlay mouse-secondary-overlay
(car range
) end
)
1880 (move-overlay mouse-secondary-overlay beg
(nth 1 range
))))
1881 (setq deactivate-mark nil
)
1882 (if (eq last-command
'mouse-secondary-save-then-kill
)
1883 ;; If the front of the kill ring comes from an immediately
1884 ;; previous use of this command, replace the entry.
1886 (buffer-substring (overlay-start mouse-secondary-overlay
)
1887 (overlay-end mouse-secondary-overlay
))
1889 (let (deactivate-mark)
1890 (copy-region-as-kill (overlay-start mouse-secondary-overlay
)
1891 (overlay-end mouse-secondary-overlay
))))
1892 (setq mouse-save-then-kill-posn click-pt
))
1894 ;; Otherwise, set the secondary selection overlay.
1896 (select-window window
)
1897 (if mouse-secondary-start
1898 ;; All we have is one end of a selection, so put the other
1900 (let ((start (+ 0 mouse-secondary-start
)))
1901 (kill-ring-save start click-pt
)
1902 (move-overlay mouse-secondary-overlay start click-pt
)))
1903 (setq mouse-save-then-kill-posn click-pt
))))
1905 ;; Finally, set the window system's secondary selection.
1907 (and (overlay-buffer mouse-secondary-overlay
)
1908 (setq str
(buffer-substring (overlay-start mouse-secondary-overlay
)
1909 (overlay-end mouse-secondary-overlay
)))
1911 (gui-set-selection 'SECONDARY str
))))
1913 (defun secondary-selection-exist-p ()
1914 "Return non-nil if the secondary selection exists in the current buffer."
1915 (memq mouse-secondary-overlay
(overlays-in (point-min) (point-max))))
1917 (defun secondary-selection-to-region ()
1918 "Set beginning and end of the region to those of the secondary selection.
1919 This puts mark and point at the beginning and the end of the
1920 secondary selection, respectively. This works when the secondary
1921 selection exists and the region does not exist in current buffer;
1922 the secondary selection will be deleted afterward.
1923 If the region is active, or the secondary selection doesn't exist,
1924 this function does nothing."
1925 (when (and (not (region-active-p))
1926 (secondary-selection-exist-p))
1927 (let ((beg (overlay-start mouse-secondary-overlay
))
1928 (end (overlay-end mouse-secondary-overlay
)))
1931 ;; Delete the secondary selection on current buffer.
1932 (delete-overlay mouse-secondary-overlay
)))
1934 (defun secondary-selection-from-region ()
1935 "Set beginning and end of the secondary selection to those of the region.
1936 When there is no region, this function does nothing."
1937 (when (region-active-p) ; Create the secondary selection from the region.
1938 (delete-overlay mouse-secondary-overlay
) ; Delete the secondary selection even on a different buffer.
1939 (move-overlay mouse-secondary-overlay
(region-beginning) (region-end))))
1942 (defcustom mouse-buffer-menu-maxlen
20
1943 "Number of buffers in one pane (submenu) of the buffer menu.
1944 If we have lots of buffers, divide them into groups of
1945 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1948 (defcustom mouse-buffer-menu-mode-mult
4
1949 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1950 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1951 will split the buffer menu by the major modes (see
1952 `mouse-buffer-menu-mode-groups') or just by menu length.
1953 Set to 1 (or even 0!) if you want to group by major mode always, and to
1954 a large number if you prefer a mixed multitude. The default is 4."
1958 (defvar mouse-buffer-menu-mode-groups
1959 (mapcar (lambda (arg) (cons (purecopy (car arg
)) (purecopy (cdr arg
))))
1960 '(("Info\\|Help\\|Apropos\\|Man" .
"Help")
1961 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1966 ("Outline" .
"Text")
1967 ("\\(HT\\|SG\\|X\\|XHT\\)ML" .
"SGML")
1968 ("log\\|diff\\|vc\\|cvs\\|Annotate" .
"Version Control") ; "Change Management"?
1969 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1972 "How to group various major modes together in \\[mouse-buffer-menu].
1973 Each element has the form (REGEXP . GROUPNAME).
1974 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1976 (defun mouse-buffer-menu (event)
1977 "Pop up a menu of buffers for selection with the mouse.
1978 This switches buffers in the window that you clicked on,
1979 and selects that window."
1981 (mouse-minibuffer-check event
)
1982 (let ((buf (x-popup-menu event
(mouse-buffer-menu-map)))
1983 (window (posn-window (event-start event
))))
1986 (if (framep window
) (frame-selected-window window
)
1988 (switch-to-buffer buf
))))
1990 (defun mouse-buffer-menu-map ()
1991 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1992 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares
)
1993 (dolist (buf buffers
)
1994 ;; Divide all buffers into buckets for various major modes.
1995 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1996 (with-current-buffer buf
1997 (let* ((adjusted-major-mode major-mode
) elt
)
1998 (dolist (group mouse-buffer-menu-mode-groups
)
1999 (when (string-match (car group
) (format-mode-line mode-name
))
2000 (setq adjusted-major-mode
(cdr group
))))
2001 (setq elt
(assoc adjusted-major-mode split-by-major-mode
))
2003 (setq elt
(list adjusted-major-mode
2004 (if (stringp adjusted-major-mode
)
2006 (format-mode-line mode-name nil nil buf
)))
2007 split-by-major-mode
(cons elt split-by-major-mode
)))
2008 (or (memq buf
(cdr (cdr elt
)))
2009 (setcdr (cdr elt
) (cons buf
(cdr (cdr elt
))))))))
2010 ;; Compute the sum of squares of sizes of the major-mode buckets.
2011 (let ((tail split-by-major-mode
))
2012 (setq sum-of-squares
0)
2014 (setq sum-of-squares
2016 (let ((len (length (cdr (cdr (car tail
)))))) (* len len
))))
2017 (setq tail
(cdr tail
))))
2018 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult
)
2019 (* (length buffers
) (length buffers
)))
2020 ;; Subdividing by major modes really helps, so let's do it.
2021 (let (subdivided-menus (buffers-left (length buffers
)))
2022 ;; Sort the list to put the most popular major modes first.
2023 (setq split-by-major-mode
2024 (sort split-by-major-mode
2025 (function (lambda (elt1 elt2
)
2026 (> (length elt1
) (length elt2
))))))
2027 ;; Make a separate submenu for each major mode
2028 ;; that has more than one buffer,
2029 ;; unless all the remaining buffers are less than 1/10 of them.
2030 (while (and split-by-major-mode
2031 (and (> (length (car split-by-major-mode
)) 3)
2032 (> (* buffers-left
10) (length buffers
))))
2033 (let ((this-mode-list (mouse-buffer-menu-alist
2034 (cdr (cdr (car split-by-major-mode
))))))
2036 (setq subdivided-menus
2038 (nth 1 (car split-by-major-mode
))
2040 subdivided-menus
))))
2042 (- buffers-left
(length (cdr (car split-by-major-mode
)))))
2043 (setq split-by-major-mode
(cdr split-by-major-mode
)))
2044 ;; If any major modes are left over,
2045 ;; make a single submenu for them.
2046 (if split-by-major-mode
2048 (mouse-buffer-menu-alist
2049 ;; we don't need split-by-major-mode any more,
2050 ;; so we can ditch it with nconc (mapcan).
2051 (mapcan 'cddr split-by-major-mode
))))
2053 (setq subdivided-menus
2054 (cons (cons "Others" others-list
)
2055 subdivided-menus
)))))
2056 (cons "Buffer Menu" (nreverse subdivided-menus
)))
2058 (mouse-buffer-menu-split "Select Buffer"
2059 (mouse-buffer-menu-alist buffers
))))))
2061 (defun mouse-buffer-menu-alist (buffers)
2067 (function (lambda (elt1 elt2
)
2068 (string< (buffer-name elt1
) (buffer-name elt2
))))))
2071 (or (eq ?\s
(aref (buffer-name (car tail
)) 0))
2074 (length (buffer-name (car tail
))))))
2075 (setq tail
(cdr tail
)))
2078 (let ((elt (car tail
)))
2079 (if (/= (aref (buffer-name elt
) 0) ?\s
)
2084 (format "%%-%ds %%s%%s %%s" maxlen
)
2086 (if (buffer-modified-p elt
) "*" " ")
2087 (with-current-buffer elt
2088 (if buffer-read-only
"%" " "))
2089 (or (buffer-file-name elt
)
2090 (with-current-buffer elt
2091 (if list-buffers-directory
2093 list-buffers-directory
)))
2097 (setq tail
(cdr tail
)))
2098 ;; Compensate for the reversal that the above loop does.
2101 (defun mouse-buffer-menu-split (title alist
)
2102 ;; If we have lots of buffers, divide them into groups of 20
2103 ;; and make a pane (or submenu) for each one.
2104 (if (> (length alist
) (/ (* mouse-buffer-menu-maxlen
3) 2))
2105 (let ((alist alist
) sublists next
2108 ;; Pull off the next mouse-buffer-menu-maxlen buffers
2109 ;; and make them the next element of sublist.
2110 (setq next
(nthcdr mouse-buffer-menu-maxlen alist
))
2112 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen
) alist
)
2114 (setq sublists
(cons (cons (format "Buffers %d" i
) alist
)
2118 (nreverse sublists
))
2119 ;; Few buffers--put them all in one pane.
2120 (list (cons title alist
))))
2122 (define-obsolete-function-alias
2123 'mouse-choose-completion
'choose-completion
"23.2")
2127 (defun font-menu-add-default ()
2128 (let* ((default (frame-parameter nil
'font
))
2129 (font-alist x-fixed-font-alist
)
2130 (elt (or (assoc "Misc" font-alist
) (nth 1 font-alist
))))
2131 (if (assoc "Default" elt
)
2132 (delete (assoc "Default" elt
) elt
))
2134 (cons (list "Default" default
)
2137 (defvar x-fixed-font-alist
2139 (purecopy "Font Menu")
2143 (lambda (arg) (cons (purecopy (car arg
)) (purecopy (cdr arg
))))
2144 ;; For these, we specify the pixel height and width.
2146 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2148 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2150 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2151 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2152 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2153 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2154 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2155 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2156 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2157 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2160 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2162 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2164 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2166 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2168 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2170 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2172 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2173 ;; We don't seem to have these; who knows what they are.
2174 ;; ("fg-18" "fg-18")
2175 ;; ("fg-25" "fg-25")
2176 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2177 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2178 ("lucidasanstypewriter-bold-24"
2179 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2180 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2181 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2185 (purecopy "Courier")
2187 (lambda (arg) (cons (purecopy (car arg
)) (purecopy (cdr arg
))))
2188 ;; For these, we specify the point height.
2189 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2190 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2191 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2192 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2193 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2194 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2195 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2196 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2197 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2198 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2199 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2200 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2201 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2202 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2203 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2204 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2205 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2206 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2207 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2208 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2209 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2210 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2211 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2212 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
2214 "X fonts suitable for use in Emacs.")
2216 (declare-function generate-fontset-menu
"fontset" ())
2218 (defun mouse-select-font ()
2219 "Prompt for a font name, using `x-popup-menu', and return it."
2221 (unless (display-multi-font-p)
2222 (error "Cannot change fonts on this display"))
2225 (if (listp last-nonmenu-event
)
2227 (list '(0 0) (selected-window)))
2228 (append x-fixed-font-alist
2229 (list (generate-fontset-menu))))))
2231 (declare-function text-scale-mode
"face-remap")
2233 (defun mouse-set-font (&rest fonts
)
2234 "Set the default font for the selected frame.
2235 The argument FONTS is a list of font names; the first valid font
2236 in this list is used.
2238 When called interactively, pop up a menu and allow the user to
2241 (progn (unless (display-multi-font-p)
2242 (error "Cannot change fonts on this display"))
2244 (if (listp last-nonmenu-event
)
2246 (list '(0 0) (selected-window)))
2247 ;; Append list of fontsets currently defined.
2248 (append x-fixed-font-alist
(list (generate-fontset-menu))))))
2254 (set-frame-font (car fonts
))
2255 (setq font
(car fonts
))
2258 (setq fonts
(cdr fonts
)))))
2260 (error "Font not found")))))
2262 (defvar mouse-appearance-menu-map nil
)
2263 (declare-function x-select-font
"xfns.c" (&optional frame ignored
)) ; USE_GTK
2264 (declare-function buffer-face-mode-invoke
"face-remap"
2265 (face arg
&optional interactive
))
2266 (declare-function font-face-attributes
"font.c" (font &optional frame
))
2267 (defvar w32-use-w32-font-dialog
)
2268 (defvar w32-fixed-font-alist
)
2270 (defun mouse-appearance-menu (event)
2271 "Show a menu for changing the default face in the current buffer."
2273 (require 'face-remap
)
2274 (when (display-multi-font-p)
2275 (with-selected-window (car (event-start event
))
2276 (if mouse-appearance-menu-map
2277 nil
; regenerate new fonts
2278 ;; Initialize mouse-appearance-menu-map
2279 (setq mouse-appearance-menu-map
2280 (make-sparse-keymap "Change Default Buffer Face"))
2281 (define-key mouse-appearance-menu-map
[face-remap-reset-base
]
2282 '(menu-item "Reset to Default" face-remap-reset-base
))
2283 (define-key mouse-appearance-menu-map
[text-scale-decrease
]
2284 '(menu-item "Decrease Buffer Text Size" text-scale-decrease
))
2285 (define-key mouse-appearance-menu-map
[text-scale-increase
]
2286 '(menu-item "Increase Buffer Text Size" text-scale-increase
))
2288 (if (and (functionp 'x-select-font
)
2289 (or (not (boundp 'w32-use-w32-font-dialog
))
2290 w32-use-w32-font-dialog
))
2291 (define-key mouse-appearance-menu-map
[x-select-font
]
2292 '(menu-item "Change Buffer Font..." x-select-font
))
2293 ;; If the select-font is unavailable, construct a menu.
2294 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
2295 (font-alist (cdr (append
2296 (if (eq system-type
'windows-nt
)
2297 w32-fixed-font-alist
2299 (list (generate-fontset-menu))))))
2300 (dolist (family font-alist
)
2301 (let* ((submenu-name (car family
))
2302 (submenu-map (make-sparse-keymap submenu-name
)))
2303 (dolist (font (cdr family
))
2304 (let ((font-name (car font
))
2306 (if (string= font-name
"")
2307 (define-key submenu-map
[space]
2309 (setq font-symbol (intern (cadr font)))
2310 (define-key submenu-map (vector font-symbol)
2311 (list 'menu-item (car font) font-symbol)))))
2312 (define-key font-submenu (vector (intern submenu-name))
2313 (list 'menu-item submenu-name submenu-map))))
2314 (define-key mouse-appearance-menu-map [font-submenu]
2315 (list 'menu-item "Change Text Font" font-submenu)))))
2316 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
2317 (setq choice (nth (1- (length choice)) choice))
2318 (cond ((eq choice 'text-scale-increase)
2319 (text-scale-increase 1))
2320 ((eq choice 'text-scale-decrease)
2321 (text-scale-increase -1))
2322 ((eq choice 'face-remap-reset-base)
2324 (buffer-face-mode 0))
2326 ;; Either choice == 'x-select-font, or choice is a
2327 ;; symbol whose name is a font.
2328 (let ((font (if (eq choice 'x-select-font)
2330 (symbol-name choice))))
2331 (buffer-face-mode-invoke
2332 (if (fontp font 'font-spec)
2334 (font-face-attributes font))
2335 t (called-interactively-p 'interactive)))))))))
2338 ;; Drag and drop support.
2339 (defcustom mouse-drag-and-drop-region nil
2340 "If non-nil, dragging the mouse drags the region, if it exists.
2341 If the value is a modifier, such as `control' or `shift' or
2342 `meta', then if that modifier key is pressed when dropping the
2343 region, text is copied instead of being cut."
2345 (const :tag "Disable dragging the region" nil)
2348 `(const :tag ,(format "Enable, but copy with the %s modifier"
2351 '(alt super hyper shift control meta))
2352 (other :tag "Enable dragging the region" t))
2355 (defcustom mouse-drag-and-drop-region-cut-when-buffers-differ nil
2356 "If non-nil, cut text also when source and destination buffers differ.
2357 If this option is nil, `mouse-drag-and-drop-region' will leave
2358 the text in the source buffer alone when dropping it in a
2359 different buffer. If this is non-nil, it will cut the text just
2360 as it does when dropping text in the source buffer."
2364 (defcustom mouse-drag-and-drop-region-show-tooltip 256
2365 "If non-nil, text is shown by a tooltip in a graphic display.
2366 If this option is nil, `mouse-drag-and-drop-region' does not show
2367 tooltips. If this is t, it shows the entire text dragged in a
2368 tooltip. If this is an integer (as with the default value of
2369 256), it will show that many characters of the dragged text in
2374 (defcustom mouse-drag-and-drop-region-show-cursor t
2375 "If non-nil, move point with mouse cursor during dragging.
2376 If this is nil, `mouse-drag-and-drop-region' leaves point alone.
2377 Otherwise, it will move point together with the mouse cursor and,
2378 in addition, temporarily highlight the original region with the
2379 `mouse-drag-and-drop-region' face."
2383 (defface mouse-drag-and-drop-region '((t :inherit region))
2384 "Face to highlight original text during dragging.
2385 This face is used by `mouse-drag-and-drop-region' to temporarily
2386 highlight the original region when
2387 `mouse-drag-and-drop-region-show-cursor' is non-nil."
2390 (defun mouse-drag-and-drop-region (event)
2391 "Move text in the region to point where mouse is dragged to.
2392 The transportation of text is also referred as `drag and drop'.
2393 When text is dragged over to a different buffer, or if a
2394 modifier key was pressed when dropping, and the value of the
2395 variable `mouse-drag-and-drop-region' is that modifier, the text
2396 is copied instead of being cut."
2398 (let* ((mouse-button (event-basic-type last-input-event))
2399 (mouse-drag-and-drop-region-show-tooltip
2400 (when (and mouse-drag-and-drop-region-show-tooltip
2401 (display-multi-frame-p)
2403 mouse-drag-and-drop-region-show-tooltip))
2404 (start (region-beginning))
2407 (buffer (current-buffer))
2408 (window (selected-window))
2409 (text-from-read-only buffer-read-only)
2410 (mouse-drag-and-drop-overlay (make-overlay start end))
2412 point-to-paste-read-only
2419 value-selection ; This remains nil when event was "click".
2424 ;; STATES stores for each window on this frame its start and point
2425 ;; positions so we can restore them on all windows but for the one
2426 ;; where the drop occurs. For inter-frame drags we'll have to do
2427 ;; this for all windows on all visible frames. In addition we save
2428 ;; also the cursor type for the window's buffer so we can restore it
2429 ;; in case we modified it.
2430 ;; https://lists.gnu.org/archive/html/emacs-devel/2017-12/msg00090.html
2437 (copy-marker (window-start window))
2438 (copy-marker (window-point window))
2439 (with-current-buffer (window-buffer window)
2445 ;; When event was "click" instead of "drag", skip loop.
2447 (setq event (read-key)) ; read-event or read-key
2448 (or (mouse-movement-p event)
2449 ;; Handle `mouse-autoselect-window'.
2450 (eq (car-safe event) 'select-window)))
2451 ;; Obtain the dragged text in region. When the loop was
2452 ;; skipped, value-selection remains nil.
2453 (unless value-selection
2454 (setq value-selection (buffer-substring start end))
2455 (when mouse-drag-and-drop-region-show-tooltip
2456 (let ((text-size mouse-drag-and-drop-region-show-tooltip))
2458 (if (and (integerp text-size)
2459 (> (length value-selection) text-size))
2461 (substring value-selection 0 (/ text-size 2))
2463 (substring value-selection (- (/ text-size 2)) -1))
2466 ;; Check if selected text is read-only.
2467 (setq text-from-read-only (or text-from-read-only
2468 (get-text-property start 'read-only)
2470 (next-single-char-property-change
2471 start 'read-only nil end)
2473 (setq window-to-paste (posn-window (event-end event)))
2474 (setq point-to-paste (posn-point (event-end event)))
2475 ;; Set nil when target buffer is minibuffer.
2476 (setq buffer-to-paste (let (buf)
2477 (when (windowp window-to-paste)
2478 (setq buf (window-buffer window-to-paste))
2479 (when (not (minibufferp buf))
2481 (setq cursor-in-text-area (and window-to-paste
2485 (when cursor-in-text-area
2486 ;; Check if point under mouse is read-only.
2487 (save-window-excursion
2488 (select-window window-to-paste)
2489 (setq point-to-paste-read-only
2490 (or buffer-read-only
2491 (get-text-property point-to-paste 'read-only))))
2493 ;; Check if "drag but negligible". Operation "drag but
2494 ;; negligible" is defined as drag-and-drop the text to
2495 ;; the original region. When modifier is pressed, the
2496 ;; text will be inserted to inside of the original
2498 (setq drag-but-negligible
2499 (and (eq (overlay-buffer mouse-drag-and-drop-overlay)
2501 (< (overlay-start mouse-drag-and-drop-overlay)
2504 (overlay-end mouse-drag-and-drop-overlay)))))
2507 (if mouse-drag-and-drop-region-show-tooltip
2508 (tooltip-show text-tooltip)
2511 ;; Show cursor and highlight the original region.
2512 (when mouse-drag-and-drop-region-show-cursor
2513 ;; Modify cursor even when point is out of frame.
2514 (setq cursor-type (cond
2515 ((not cursor-in-text-area)
2517 ((or point-to-paste-read-only
2518 drag-but-negligible)
2522 (when cursor-in-text-area
2523 (overlay-put mouse-drag-and-drop-overlay
2524 'face 'mouse-drag-and-drop-region)
2525 (deactivate-mark) ; Maintain region in other window.
2526 (mouse-set-point event)))))
2529 (when mouse-drag-and-drop-region-show-tooltip (tooltip-hide))
2531 ;; Check if modifier was pressed on drop.
2532 (setq no-modifier-on-drop
2533 (not (member mouse-drag-and-drop-region (event-modifiers event))))
2535 ;; Check if event was "click".
2536 (setq clicked (not value-selection))
2538 ;; Restore status on drag to outside of text-area or non-mouse input.
2539 (when (or (not cursor-in-text-area)
2540 (not (equal (event-basic-type event) mouse-button)))
2541 (setq drag-but-negligible t
2542 no-modifier-on-drop t))
2544 ;; Do not modify any buffers when event is "click",
2545 ;; "drag but negligible", or "drag to read-only".
2546 (let* ((mouse-drag-and-drop-region-cut-when-buffers-differ
2547 (if no-modifier-on-drop
2548 mouse-drag-and-drop-region-cut-when-buffers-differ
2549 (not mouse-drag-and-drop-region-cut-when-buffers-differ)))
2550 (wanna-paste-to-same-buffer (equal buffer-to-paste buffer))
2551 (wanna-cut-on-same-buffer (and wanna-paste-to-same-buffer
2552 no-modifier-on-drop))
2553 (wanna-cut-on-other-buffer
2554 (and (not wanna-paste-to-same-buffer)
2555 mouse-drag-and-drop-region-cut-when-buffers-differ))
2556 (cannot-paste (or point-to-paste-read-only
2557 (when (or wanna-cut-on-same-buffer
2558 wanna-cut-on-other-buffer)
2559 text-from-read-only))))
2562 ;; Move point within region.
2565 (mouse-set-point event))
2566 ;; Undo operation. Set back the original text as region.
2567 ((or (and drag-but-negligible
2568 no-modifier-on-drop)
2570 ;; Inform user either source or destination buffer cannot be modified.
2571 (when (and (not drag-but-negligible)
2573 (message "Buffer is read-only"))
2575 ;; Select source window back and restore region.
2576 ;; (set-window-point window point)
2577 (select-window window)
2579 (setq deactivate-mark nil)
2583 ;; * DESTINATION BUFFER::
2584 ;; Insert the text to destination buffer under mouse.
2585 (select-window window-to-paste)
2586 (setq window-exempt window-to-paste)
2587 (goto-char point-to-paste)
2589 (insert value-selection)
2590 ;; On success, set the text as region on destination buffer.
2591 (when (not (equal (mark) (point)))
2592 (setq deactivate-mark nil)
2595 ;; * SOURCE BUFFER::
2596 ;; Set back the original text as region or delete the original
2597 ;; text, on source buffer.
2598 (if wanna-paste-to-same-buffer
2599 ;; When source buffer and destination buffer are the same,
2600 ;; remove the original text.
2601 (when no-modifier-on-drop
2602 (let (deactivate-mark)
2603 (delete-region (overlay-start mouse-drag-and-drop-overlay)
2604 (overlay-end mouse-drag-and-drop-overlay))))
2605 ;; When source buffer and destination buffer are different,
2606 ;; keep (set back the original text as region) or remove the
2608 (select-window window) ; Select window with source buffer.
2609 (goto-char point) ; Move point to the original text on source buffer.
2611 (if mouse-drag-and-drop-region-cut-when-buffers-differ
2612 ;; Remove the dragged text from source buffer like
2614 (delete-region (overlay-start mouse-drag-and-drop-overlay)
2615 (overlay-end mouse-drag-and-drop-overlay))
2616 ;; Set back the dragged text as region on source buffer
2617 ;; like operation `copy'.
2619 (select-window window-to-paste))))))
2622 (delete-overlay mouse-drag-and-drop-overlay)
2624 ;; Restore old states but for the window where the drop
2625 ;; occurred. Restore cursor types for all windows.
2626 (dolist (state states)
2627 (let ((window (car state)))
2628 (when (and window-exempt
2629 (not (eq window window-exempt)))
2630 (set-window-start window (nth 1 state) 'noforce)
2631 (set-marker (nth 1 state) nil)
2632 ;; If window is selected, the following automatically sets
2633 ;; point for that window's buffer.
2634 (set-window-point window (nth 2 state))
2635 (set-marker (nth 2 state) nil))
2636 (with-current-buffer (window-buffer window)
2637 (setq cursor-type (nth 3 state)))))))
2640 ;;; Bindings for mouse commands.
2642 (global-set-key [down-mouse-1] 'mouse-drag-region)
2643 (global-set-key [mouse-1] 'mouse-set-point)
2644 (global-set-key [drag-mouse-1] 'mouse-set-region)
2646 (defun mouse--strip-first-event (_prompt)
2647 (substring (this-single-command-raw-keys) 1))
2649 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
2650 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
2652 (global-set-key [mouse-2] 'mouse-yank-primary)
2653 ;; Allow yanking also when the corresponding cursor is "in the fringe".
2654 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
2655 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
2656 (global-set-key [mouse-3] 'mouse-save-then-kill)
2657 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
2658 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
2660 ;; By binding these to down-going events, we let the user use the up-going
2661 ;; event to make the selection, saving a click.
2662 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2663 (if (not (eq system-type 'ms-dos))
2664 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
2665 ;; C-down-mouse-2 is bound in facemenu.el.
2666 (global-set-key [C-down-mouse-3]
2667 `(menu-item ,(purecopy "Menu Bar") ignore
2669 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
2670 (mouse-menu-bar-map)
2671 (mouse-menu-major-mode-map)))))
2673 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2674 ;; vertical-line prevents Emacs from signaling an error when the mouse
2675 ;; button is released after dragging these lines, on non-toolkit
2677 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2678 (global-set-key [header-line mouse-1] 'mouse-select-window)
2679 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2680 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2681 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2682 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2683 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2684 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2685 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2686 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
2687 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2688 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2689 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2690 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
2691 (global-set-key [right-divider mouse-1] 'ignore)
2692 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
2693 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
2694 (global-set-key [bottom-divider mouse-1] 'ignore)
2695 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
2696 (global-set-key [left-edge down-mouse-1] 'mouse-drag-left-edge)
2697 (global-set-key [left-edge mouse-1] 'ignore)
2698 (global-set-key [top-left-corner down-mouse-1] 'mouse-drag-top-left-corner)
2699 (global-set-key [top-left-corner mouse-1] 'ignore)
2700 (global-set-key [top-edge down-mouse-1] 'mouse-drag-top-edge)
2701 (global-set-key [top-edge mouse-1] 'ignore)
2702 (global-set-key [top-right-corner down-mouse-1] 'mouse-drag-top-right-corner)
2703 (global-set-key [top-right-corner mouse-1] 'ignore)
2704 (global-set-key [right-edge down-mouse-1] 'mouse-drag-right-edge)
2705 (global-set-key [right-edge mouse-1] 'ignore)
2706 (global-set-key [bottom-right-corner down-mouse-1] 'mouse-drag-bottom-right-corner)
2707 (global-set-key [bottom-right-corner mouse-1] 'ignore)
2708 (global-set-key [bottom-edge down-mouse-1] 'mouse-drag-bottom-edge)
2709 (global-set-key [bottom-edge mouse-1] 'ignore)
2710 (global-set-key [bottom-left-corner down-mouse-1] 'mouse-drag-bottom-left-corner)
2711 (global-set-key [bottom-left-corner mouse-1] 'ignore)
2715 ;;; mouse.el ends here