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