Update from gnulib
[emacs.git] / lisp / mouse.el
blob4b4749bb45d8600b7a5c74b9400cfa476cb08e82
1 ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1995, 1999-2016 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 ;; Start tracking. The special value 'dragging' signals the
498 ;; display engine to freeze the mouse pointer shape for as long
499 ;; as we drag.
500 (setq track-mouse 'dragging)
501 ;; Loop reading events and sampling the position of the mouse.
502 (setq exitfun
503 (set-transient-map
504 (let ((map (make-sparse-keymap)))
505 (define-key map [switch-frame] #'ignore)
506 (define-key map [select-window] #'ignore)
507 (define-key map [scroll-bar-movement] #'ignore)
508 (define-key map [mouse-movement] move)
509 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
510 (define-key map [drag-mouse-1]
511 (lambda () (interactive) (funcall exitfun)))
512 ;; For vertical line dragging swallow also a mouse-1
513 ;; event (but only if we dragged at least once to allow mouse-1
514 ;; clicks to get through).
515 (when (eq line 'vertical)
516 (define-key map [mouse-1]
517 `(menu-item "" ,(lambda () (interactive) (funcall exitfun))
518 :filter ,(lambda (cmd) (if dragged cmd)))))
519 ;; Some of the events will of course end up looked up
520 ;; with a mode-line, header-line or vertical-line prefix ...
521 (define-key map [mode-line] map)
522 (define-key map [header-line] map)
523 (define-key map [vertical-line] map)
524 ;; ... and some maybe even with a right- or bottom-divider
525 ;; prefix.
526 (define-key map [right-divider] map)
527 (define-key map [bottom-divider] map)
528 map)
529 t (lambda () (setq track-mouse nil)))))))
531 (defun mouse-drag-mode-line (start-event)
532 "Change the height of a window by dragging on the mode line."
533 (interactive "e")
534 (mouse-drag-line start-event 'mode))
536 (defun mouse-drag-header-line (start-event)
537 "Change the height of a window by dragging on the header line."
538 (interactive "e")
539 (mouse-drag-line start-event 'header))
541 (defun mouse-drag-vertical-line (start-event)
542 "Change the width of a window by dragging on the vertical line."
543 (interactive "e")
544 (mouse-drag-line start-event 'vertical))
546 (defcustom mouse-select-region-move-to-beginning nil
547 "Effect of selecting a region extending backward from double click.
548 Nil means keep point at the position clicked (region end);
549 non-nil means move point to beginning of region."
550 :type '(choice (const :tag "Don't move point" nil)
551 (const :tag "Move point to beginning of region" t))
552 :group 'mouse
553 :version "25.2")
555 (defun mouse-set-point (event &optional promote-to-region)
556 "Move point to the position clicked on with the mouse.
557 This should be bound to a mouse click event type.
558 If PROMOTE-TO-REGION is non-nil and event is a multiple-click, select
559 the corresponding element around point, with the resulting position of
560 point determined by `mouse-select-region-move-to-beginning'."
561 (interactive "e\np")
562 (mouse-minibuffer-check event)
563 (if (and promote-to-region (> (event-click-count event) 1))
564 (progn
565 (mouse-set-region event)
566 (when mouse-select-region-move-to-beginning
567 (when (> (posn-point (event-start event)) (region-beginning))
568 (exchange-point-and-mark))))
569 ;; Use event-end in case called from mouse-drag-region.
570 ;; If EVENT is a click, event-end and event-start give same value.
571 (posn-set-point (event-end event))))
573 (defvar mouse-last-region-beg nil)
574 (defvar mouse-last-region-end nil)
575 (defvar mouse-last-region-tick nil)
577 (defun mouse-region-match ()
578 "Return non-nil if there's an active region that was set with the mouse."
579 (and (mark t) mark-active
580 (eq mouse-last-region-beg (region-beginning))
581 (eq mouse-last-region-end (region-end))
582 (eq mouse-last-region-tick (buffer-modified-tick))))
584 (defvar mouse--drag-start-event nil)
586 (defun mouse-set-region (click)
587 "Set the region to the text dragged over, and copy to kill ring.
588 This should be bound to a mouse drag event.
589 See the `mouse-drag-copy-region' variable to control whether this
590 command alters the kill ring or not."
591 (interactive "e")
592 (mouse-minibuffer-check click)
593 (select-window (posn-window (event-start click)))
594 (let ((beg (posn-point (event-start click)))
595 (end
596 (if (eq (posn-window (event-end click)) (selected-window))
597 (posn-point (event-end click))
598 ;; If the mouse ends up in any other window or on the menu
599 ;; bar, use `window-point' of selected window (Bug#23707).
600 (window-point)))
601 (click-count (event-click-count click)))
602 (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
603 (when drag-start
604 ;; Drag events don't come with a click count, sadly, so we hack
605 ;; our way around this problem by remembering the start-event in
606 ;; `mouse-drag-start' and fetching the click-count from there.
607 (when (and (<= click-count 1)
608 (equal beg (posn-point (event-start drag-start))))
609 (setq click-count (event-click-count drag-start)))
610 ;; Occasionally we get spurious drag events where the user hasn't
611 ;; dragged his mouse, but instead Emacs has dragged the text under the
612 ;; user's mouse. Try to recover those cases (bug#17562).
613 (when (and (equal (posn-x-y (event-start click))
614 (posn-x-y (event-end click)))
615 (not (eq (car drag-start) 'mouse-movement)))
616 (setq end beg))
617 (setf (terminal-parameter nil 'mouse-drag-start) nil)))
618 (when (and (integerp beg) (integerp end))
619 (let ((range (mouse-start-end beg end (1- click-count))))
620 (if (< end beg)
621 (setq end (nth 0 range) beg (nth 1 range))
622 (setq beg (nth 0 range) end (nth 1 range)))))
623 (and mouse-drag-copy-region (integerp beg) (integerp end)
624 ;; Don't set this-command to `kill-region', so a following
625 ;; C-w won't double the text in the kill ring. Ignore
626 ;; `last-command' so we don't append to a preceding kill.
627 (let (this-command last-command deactivate-mark)
628 (copy-region-as-kill beg end)))
629 (if (numberp beg) (goto-char beg))
630 ;; On a text terminal, bounce the cursor.
631 (or transient-mark-mode
632 (window-system)
633 (sit-for 1))
634 (push-mark)
635 (set-mark (point))
636 (if (numberp end) (goto-char end))
637 (mouse-set-region-1)))
639 (defun mouse-set-region-1 ()
640 ;; Set transient-mark-mode for a little while.
641 (unless (eq (car-safe transient-mark-mode) 'only)
642 (setq-local transient-mark-mode
643 (cons 'only
644 (unless (eq transient-mark-mode 'lambda)
645 transient-mark-mode))))
646 (setq mouse-last-region-beg (region-beginning))
647 (setq mouse-last-region-end (region-end))
648 (setq mouse-last-region-tick (buffer-modified-tick)))
650 (defcustom mouse-scroll-delay 0.25
651 "The pause between scroll steps caused by mouse drags, in seconds.
652 If you drag the mouse beyond the edge of a window, Emacs scrolls the
653 window to bring the text beyond that edge into view, with a delay of
654 this many seconds between scroll steps. Scrolling stops when you move
655 the mouse back into the window, or release the button.
656 This variable's value may be non-integral.
657 Setting this to zero causes Emacs to scroll as fast as it can."
658 :type 'number
659 :group 'mouse)
661 (defcustom mouse-scroll-min-lines 1
662 "The minimum number of lines scrolled by dragging mouse out of window.
663 Moving the mouse out the top or bottom edge of the window begins
664 scrolling repeatedly. The number of lines scrolled per repetition
665 is normally equal to the number of lines beyond the window edge that
666 the mouse has moved. However, it always scrolls at least the number
667 of lines specified by this variable."
668 :type 'integer
669 :group 'mouse)
671 (defun mouse-scroll-subr (window jump &optional overlay start)
672 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
673 If OVERLAY is an overlay, let it stretch from START to the far edge of
674 the newly visible text.
675 Upon exit, point is at the far edge of the newly visible text."
676 (cond
677 ((and (> jump 0) (< jump mouse-scroll-min-lines))
678 (setq jump mouse-scroll-min-lines))
679 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
680 (setq jump (- mouse-scroll-min-lines))))
681 (let ((opoint (point)))
682 (while (progn
683 (goto-char (window-start window))
684 (if (not (zerop (vertical-motion jump window)))
685 (progn
686 (set-window-start window (point))
687 (if (natnump jump)
688 (if (window-end window)
689 (progn
690 (goto-char (window-end window))
691 ;; window-end doesn't reflect the window's new
692 ;; start position until the next redisplay.
693 (vertical-motion (1- jump) window))
694 (vertical-motion (- (window-height window) 2)))
695 (goto-char (window-start window)))
696 (if overlay
697 (move-overlay overlay start (point)))
698 ;; Now that we have scrolled WINDOW properly,
699 ;; put point back where it was for the redisplay
700 ;; so that we don't mess up the selected window.
701 (or (eq window (selected-window))
702 (goto-char opoint))
703 (sit-for mouse-scroll-delay)))))
704 (or (eq window (selected-window))
705 (goto-char opoint))))
707 (defvar mouse-selection-click-count 0)
709 (defvar mouse-selection-click-count-buffer nil)
711 (defun mouse-drag-region (start-event)
712 "Set the region to the text that the mouse is dragged over.
713 Highlight the drag area as you move the mouse.
714 This must be bound to a button-down mouse event.
715 In Transient Mark mode, the highlighting remains as long as the mark
716 remains active. Otherwise, it remains until the next input event."
717 (interactive "e")
718 ;; Give temporary modes such as isearch a chance to turn off.
719 (run-hooks 'mouse-leave-buffer-hook)
720 (mouse-drag-track start-event))
723 (defun mouse-posn-property (pos property)
724 "Look for a property at click position.
725 POS may be either a buffer position or a click position like
726 those returned from `event-start'. If the click position is on
727 a string, the text property PROPERTY is examined.
728 If this is nil or the click is not on a string, then
729 the corresponding buffer position is searched for PROPERTY.
730 If PROPERTY is encountered in one of those places,
731 its value is returned."
732 (if (consp pos)
733 (let ((w (posn-window pos)) (pt (posn-point pos))
734 (str (posn-string pos)))
735 (or (and str
736 (get-text-property (cdr str) property (car str)))
737 ;; Mouse clicks in the fringe come with a position in
738 ;; (nth 5). This is useful but is not exactly where we clicked, so
739 ;; don't look up that position's properties!
740 (and pt (not (memq (posn-area pos) '(left-fringe right-fringe
741 left-margin right-margin)))
742 (get-char-property pt property w))))
743 (get-char-property pos property)))
745 (defun mouse-on-link-p (pos)
746 "Return non-nil if POS is on a link in the current buffer.
747 POS must be a buffer position in the current buffer or a mouse
748 event location in the selected window (see `event-start').
749 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
750 POS may be a mouse event location in any window.
752 A clickable link is identified by one of the following methods:
754 - If the character at POS has a non-nil `follow-link' text or
755 overlay property, the value of that property determines what to do.
757 - If there is a local key-binding or a keybinding at position POS
758 for the `follow-link' event, the binding of that event determines
759 what to do.
761 The resulting value determine whether POS is inside a link:
763 - If the value is `mouse-face', POS is inside a link if there
764 is a non-nil `mouse-face' property at POS. Return t in this case.
766 - If the value is a function, FUNC, POS is inside a link if
767 the call \(FUNC POS) returns non-nil. Return the return value
768 from that call. Arg is \(posn-point POS) if POS is a mouse event.
770 - Otherwise, return the value itself.
772 The return value is interpreted as follows:
774 - If it is a string, the mouse-1 event is translated into the
775 first character of the string, i.e. the action of the mouse-1
776 click is the local or global binding of that character.
778 - If it is a vector, the mouse-1 event is translated into the
779 first element of that vector, i.e. the action of the mouse-1
780 click is the local or global binding of that event.
782 - Otherwise, the mouse-1 event is translated into a mouse-2 event
783 at the same position."
784 (let ((action
785 (and (or (not (consp pos))
786 mouse-1-click-in-non-selected-windows
787 (eq (selected-window) (posn-window pos)))
788 (or (mouse-posn-property pos 'follow-link)
789 (let ((area (posn-area pos)))
790 (when area
791 (key-binding (vector area 'follow-link) nil t pos)))
792 (key-binding [follow-link] nil t pos)))))
793 (cond
794 ((eq action 'mouse-face)
795 (and (mouse-posn-property pos 'mouse-face) t))
796 ((functionp action)
797 ;; FIXME: This seems questionable if the click is not in a buffer.
798 ;; Should we instead decide that `action' takes a `posn'?
799 (if (consp pos)
800 (with-current-buffer (window-buffer (posn-window pos))
801 (funcall action (posn-point pos)))
802 (funcall action pos)))
803 (t action))))
805 (defun mouse-fixup-help-message (msg)
806 "Fix help message MSG for `mouse-1-click-follows-link'."
807 (let (mp pos)
808 (if (and mouse-1-click-follows-link
809 (stringp msg)
810 (string-match-p "\\`mouse-2" msg)
811 (setq mp (mouse-pixel-position))
812 (consp (setq pos (cdr mp)))
813 (car pos) (>= (car pos) 0)
814 (cdr pos) (>= (cdr pos) 0)
815 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
816 (windowp (posn-window pos)))
817 (with-current-buffer (window-buffer (posn-window pos))
818 (if (mouse-on-link-p pos)
819 (setq msg (concat
820 (cond
821 ((eq mouse-1-click-follows-link 'double) "double-")
822 ((and (integerp mouse-1-click-follows-link)
823 (< mouse-1-click-follows-link 0)) "Long ")
824 (t ""))
825 "mouse-1" (substring msg 7)))))))
826 msg)
828 (defun mouse-drag-track (start-event)
829 "Track mouse drags by highlighting area between point and cursor.
830 The region will be defined with mark and point."
831 (mouse-minibuffer-check start-event)
832 (setq mouse-selection-click-count-buffer (current-buffer))
833 (deactivate-mark)
834 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
835 (start-posn (event-start start-event))
836 (start-point (posn-point start-posn))
837 (start-window (posn-window start-posn))
838 (_ (with-current-buffer (window-buffer start-window)
839 (setq deactivate-mark nil)))
840 ;; We've recorded what we needed from the current buffer and
841 ;; window, now let's jump to the place of the event, where things
842 ;; are happening.
843 (_ (mouse-set-point start-event))
844 (echo-keystrokes 0)
845 (bounds (window-edges start-window))
846 (make-cursor-line-fully-visible nil)
847 (top (nth 1 bounds))
848 (bottom (if (window-minibuffer-p start-window)
849 (nth 3 bounds)
850 ;; Don't count the mode line.
851 (1- (nth 3 bounds))))
852 (click-count (1- (event-click-count start-event)))
853 ;; Suppress automatic hscrolling, because that is a nuisance
854 ;; when setting point near the right fringe (but see below).
855 (auto-hscroll-mode-saved auto-hscroll-mode))
857 (setq mouse-selection-click-count click-count)
858 ;; In case the down click is in the middle of some intangible text,
859 ;; use the end of that text, and put it in START-POINT.
860 (if (< (point) start-point)
861 (goto-char start-point))
862 (setq start-point (point))
864 ;; Activate the region, using `mouse-start-end' to determine where
865 ;; to put point and mark (e.g., double-click will select a word).
866 (setq-local transient-mark-mode
867 (if (eq transient-mark-mode 'lambda)
868 '(only)
869 (cons 'only transient-mark-mode)))
870 (let ((range (mouse-start-end start-point start-point click-count)))
871 (push-mark (nth 0 range) t t)
872 (goto-char (nth 1 range)))
874 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
875 (setq track-mouse t)
876 (setq auto-hscroll-mode nil)
878 (set-transient-map
879 (let ((map (make-sparse-keymap)))
880 (define-key map [switch-frame] #'ignore)
881 (define-key map [select-window] #'ignore)
882 (define-key map [mouse-movement]
883 (lambda (event) (interactive "e")
884 (let* ((end (event-end event))
885 (end-point (posn-point end)))
886 (unless (eq end-point start-point)
887 ;; As soon as the user moves, we can re-enable auto-hscroll.
888 (setq auto-hscroll-mode auto-hscroll-mode-saved)
889 ;; And remember that we have moved, so mouse-set-region can know
890 ;; its event is really a drag event.
891 (setcar start-event 'mouse-movement))
892 (if (and (eq (posn-window end) start-window)
893 (integer-or-marker-p end-point))
894 (mouse--drag-set-mark-and-point start-point
895 end-point click-count)
896 (let ((mouse-row (cdr (cdr (mouse-position)))))
897 (cond
898 ((null mouse-row))
899 ((< mouse-row top)
900 (mouse-scroll-subr start-window (- mouse-row top)
901 nil start-point))
902 ((>= mouse-row bottom)
903 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
904 nil start-point))))))))
905 map)
906 t (lambda ()
907 (setq track-mouse nil)
908 (setq auto-hscroll-mode auto-hscroll-mode-saved)
909 (deactivate-mark)
910 (pop-mark)))))
912 (defun mouse--drag-set-mark-and-point (start click click-count)
913 (let* ((range (mouse-start-end start click click-count))
914 (beg (nth 0 range))
915 (end (nth 1 range)))
916 (cond ((eq (mark) beg)
917 (goto-char end))
918 ((eq (mark) end)
919 (goto-char beg))
920 ((< click (mark))
921 (set-mark end)
922 (goto-char beg))
924 (set-mark beg)
925 (goto-char end)))))
927 ;; Commands to handle xterm-style multiple clicks.
928 (defun mouse-skip-word (dir)
929 "Skip over word, over whitespace, or over identical punctuation.
930 If DIR is positive skip forward; if negative, skip backward."
931 (let* ((char (following-char))
932 (syntax (char-to-string (char-syntax char))))
933 (cond ((string= syntax "w")
934 ;; Here, we can't use skip-syntax-forward/backward because
935 ;; they don't pay attention to word-separating-categories,
936 ;; and thus they will skip over a true word boundary. So,
937 ;; we simulate the original behavior by using forward-word.
938 (if (< dir 0)
939 (if (not (looking-at "\\<"))
940 (forward-word -1))
941 (if (or (looking-at "\\<") (not (looking-at "\\>")))
942 (forward-word 1))))
943 ((string= syntax " ")
944 (if (< dir 0)
945 (skip-syntax-backward syntax)
946 (skip-syntax-forward syntax)))
947 ((string= syntax "_")
948 (if (< dir 0)
949 (skip-syntax-backward "w_")
950 (skip-syntax-forward "w_")))
951 ((< dir 0)
952 (while (and (not (bobp)) (= (preceding-char) char))
953 (forward-char -1)))
955 (while (and (not (eobp)) (= (following-char) char))
956 (forward-char 1))))))
958 (defun mouse-start-end (start end mode)
959 "Return a list of region bounds based on START and END according to MODE.
960 If MODE is 0 then set point to (min START END), mark to (max START END).
961 If MODE is 1 then set point to start of word at (min START END),
962 mark to end of word at (max START END).
963 If MODE is 2 then do the same for lines."
964 (if (> start end)
965 (let ((temp start))
966 (setq start end
967 end temp)))
968 (setq mode (mod mode 3))
969 (cond ((= mode 0)
970 (list start end))
971 ((and (= mode 1)
972 (= start end)
973 (char-after start)
974 (= (char-syntax (char-after start)) ?\())
975 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
976 ;; This happens in CC Mode when unbalanced parens in CPP
977 ;; constructs are given punctuation syntax with
978 ;; syntax-table text properties. (2016-02-21).
979 (signal 'scan-error (list "Containing expression ends prematurely"
980 start start))
981 (list start
982 (save-excursion
983 (goto-char start)
984 (forward-sexp 1)
985 (point)))))
986 ((and (= mode 1)
987 (= start end)
988 (char-after start)
989 (= (char-syntax (char-after start)) ?\)))
990 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
991 ;; See above comment about CC Mode.
992 (signal 'scan-error (list "Unbalanced parentheses" start start))
993 (list (save-excursion
994 (goto-char (1+ start))
995 (backward-sexp 1)
996 (point))
997 (1+ start))))
998 ((and (= mode 1)
999 (= start end)
1000 (char-after start)
1001 (= (char-syntax (char-after start)) ?\"))
1002 (let ((open (or (eq start (point-min))
1003 (save-excursion
1004 (goto-char (- start 1))
1005 (looking-at "\\s(\\|\\s \\|\\s>")))))
1006 (if open
1007 (list start
1008 (save-excursion
1009 (condition-case nil
1010 (progn
1011 (goto-char start)
1012 (forward-sexp 1)
1013 (point))
1014 (error end))))
1015 (list (save-excursion
1016 (condition-case nil
1017 (progn
1018 (goto-char (1+ start))
1019 (backward-sexp 1)
1020 (point))
1021 (error end)))
1022 (1+ start)))))
1023 ((= mode 1)
1024 (list (save-excursion
1025 (goto-char start)
1026 (mouse-skip-word -1)
1027 (point))
1028 (save-excursion
1029 (goto-char end)
1030 (mouse-skip-word 1)
1031 (point))))
1032 ((= mode 2)
1033 (list (save-excursion
1034 (goto-char start)
1035 (line-beginning-position 1))
1036 (save-excursion
1037 (goto-char end)
1038 (forward-line 1)
1039 (point))))))
1041 ;; Subroutine: set the mark where CLICK happened,
1042 ;; but don't do anything else.
1043 (defun mouse-set-mark-fast (click)
1044 (mouse-minibuffer-check click)
1045 (let ((posn (event-start click)))
1046 (select-window (posn-window posn))
1047 (if (numberp (posn-point posn))
1048 (push-mark (posn-point posn) t t))))
1050 (defun mouse-undouble-last-event (events)
1051 (let* ((index (1- (length events)))
1052 (last (nthcdr index events))
1053 (event (car last))
1054 (basic (event-basic-type event))
1055 (old-modifiers (event-modifiers event))
1056 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1057 (new
1058 (if (consp event)
1059 ;; Use reverse, not nreverse, since event-modifiers
1060 ;; does not copy the list it returns.
1061 (cons (event-convert-list (reverse (cons basic modifiers)))
1062 (cdr event))
1063 event)))
1064 (setcar last new)
1065 (if (and (not (equal modifiers old-modifiers))
1066 (key-binding (apply 'vector events)))
1068 (setcar last event)
1069 nil)))
1071 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1073 (defun mouse-set-mark (click)
1074 "Set mark at the position clicked on with the mouse.
1075 Display cursor at that position for a second.
1076 This must be bound to a mouse click."
1077 (interactive "e")
1078 (mouse-minibuffer-check click)
1079 (select-window (posn-window (event-start click)))
1080 ;; FIXME: Use save-excursion
1081 (let ((point-save (point)))
1082 (unwind-protect
1083 (progn (mouse-set-point click)
1084 (push-mark nil t t)
1085 (or transient-mark-mode
1086 (sit-for 1)))
1087 (goto-char point-save))))
1089 (defun mouse-kill (click)
1090 "Kill the region between point and the mouse click.
1091 The text is saved in the kill ring, as with \\[kill-region]."
1092 (interactive "e")
1093 (mouse-minibuffer-check click)
1094 (let* ((posn (event-start click))
1095 (click-posn (posn-point posn)))
1096 (select-window (posn-window posn))
1097 (if (numberp click-posn)
1098 (kill-region (min (point) click-posn)
1099 (max (point) click-posn)))))
1101 (defun mouse-yank-at-click (click arg)
1102 "Insert the last stretch of killed text at the position clicked on.
1103 Also move point to one end of the text thus inserted (normally the end),
1104 and set mark at the beginning.
1105 Prefix arguments are interpreted as with \\[yank].
1106 If `mouse-yank-at-point' is non-nil, insert at point
1107 regardless of where you click."
1108 (interactive "e\nP")
1109 ;; Give temporary modes such as isearch a chance to turn off.
1110 (run-hooks 'mouse-leave-buffer-hook)
1111 (when select-active-regions
1112 ;; Without this, confusing things happen upon e.g. inserting into
1113 ;; the middle of an active region.
1114 (deactivate-mark))
1115 (or mouse-yank-at-point (mouse-set-point click))
1116 (setq this-command 'yank)
1117 (setq mouse-selection-click-count 0)
1118 (yank arg))
1120 (defun mouse-yank-primary (click)
1121 "Insert the primary selection at the position clicked on.
1122 Move point to the end of the inserted text, and set mark at
1123 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1124 regardless of where you click."
1125 (interactive "e")
1126 ;; Give temporary modes such as isearch a chance to turn off.
1127 (run-hooks 'mouse-leave-buffer-hook)
1128 ;; Without this, confusing things happen upon e.g. inserting into
1129 ;; the middle of an active region.
1130 (when select-active-regions
1131 (let (select-active-regions)
1132 (deactivate-mark)))
1133 (or mouse-yank-at-point (mouse-set-point click))
1134 (let ((primary (gui-get-primary-selection)))
1135 (push-mark (point))
1136 (insert-for-yank primary)))
1138 (defun mouse-kill-ring-save (click)
1139 "Copy the region between point and the mouse click in the kill ring.
1140 This does not delete the region; it acts like \\[kill-ring-save]."
1141 (interactive "e")
1142 (mouse-set-mark-fast click)
1143 (let (this-command last-command)
1144 (kill-ring-save (point) (mark t))))
1146 ;; This function used to delete the text between point and the mouse
1147 ;; whenever it was equal to the front of the kill ring, but some
1148 ;; people found that confusing.
1150 ;; The position of the last invocation of `mouse-save-then-kill'.
1151 (defvar mouse-save-then-kill-posn nil)
1153 (defun mouse-save-then-kill-delete-region (beg end)
1154 ;; We must make our own undo boundaries
1155 ;; because they happen automatically only for the current buffer.
1156 (undo-boundary)
1157 (if (or (= beg end) (eq buffer-undo-list t))
1158 ;; If we have no undo list in this buffer,
1159 ;; just delete.
1160 (delete-region beg end)
1161 ;; Delete, but make the undo-list entry share with the kill ring.
1162 ;; First, delete just one char, so in case buffer is being modified
1163 ;; for the first time, the undo list records that fact.
1164 (let ((inhibit-modification-hooks t))
1165 (delete-region beg
1166 (+ beg (if (> end beg) 1 -1))))
1167 (let ((buffer-undo-list buffer-undo-list))
1168 ;; Undo that deletion--but don't change the undo list!
1169 (let ((inhibit-modification-hooks t))
1170 (primitive-undo 1 buffer-undo-list))
1171 ;; Now delete the rest of the specified region,
1172 ;; but don't record it.
1173 (setq buffer-undo-list t)
1174 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1175 (error "Lossage in mouse-save-then-kill-delete-region"))
1176 (delete-region beg end))
1177 (let ((tail buffer-undo-list))
1178 ;; Search back in buffer-undo-list for the string
1179 ;; that came from deleting one character.
1180 (while (and tail (not (stringp (car (car tail)))))
1181 (setq tail (cdr tail)))
1182 ;; Replace it with an entry for the entire deleted text.
1183 (and tail
1184 (setcar tail (cons (car kill-ring) (min beg end))))))
1185 (undo-boundary))
1187 (defun mouse-save-then-kill (click)
1188 "Set the region according to CLICK; the second time, kill it.
1189 CLICK should be a mouse click event.
1191 If the region is inactive, activate it temporarily. Set mark at
1192 the original point, and move point to the position of CLICK.
1194 If the region is already active, adjust it. Normally, do this by
1195 moving point or mark, whichever is closer, to CLICK. But if you
1196 have selected whole words or lines, move point or mark to the
1197 word or line boundary closest to CLICK instead.
1199 If `mouse-drag-copy-region' is non-nil, this command also saves the
1200 new region to the kill ring (replacing the previous kill if the
1201 previous region was just saved to the kill ring).
1203 If this command is called a second consecutive time with the same
1204 CLICK position, kill the region (or delete it
1205 if `mouse-drag-copy-region' is non-nil)"
1206 (interactive "e")
1207 (mouse-minibuffer-check click)
1208 (let* ((posn (event-start click))
1209 (click-pt (posn-point posn))
1210 (window (posn-window posn))
1211 (buf (window-buffer window))
1212 ;; Don't let a subsequent kill command append to this one.
1213 (this-command this-command)
1214 ;; Check if the user has multi-clicked to select words/lines.
1215 (click-count
1216 (if (and (eq mouse-selection-click-count-buffer buf)
1217 (with-current-buffer buf (mark t)))
1218 mouse-selection-click-count
1219 0)))
1220 (cond
1221 ((not (numberp click-pt)) nil)
1222 ;; If the user clicked without moving point, kill the region.
1223 ;; This also resets `mouse-selection-click-count'.
1224 ((and (eq last-command 'mouse-save-then-kill)
1225 (eq click-pt mouse-save-then-kill-posn)
1226 (eq window (selected-window)))
1227 (if mouse-drag-copy-region
1228 ;; Region already saved in the previous click;
1229 ;; don't make a duplicate entry, just delete.
1230 (delete-region (mark t) (point))
1231 (kill-region (mark t) (point)))
1232 (setq mouse-selection-click-count 0)
1233 (setq mouse-save-then-kill-posn nil))
1235 ;; Otherwise, if there is a suitable region, adjust it by moving
1236 ;; one end (whichever is closer) to CLICK-PT.
1237 ((or (with-current-buffer buf (region-active-p))
1238 (and (eq window (selected-window))
1239 (mark t)
1240 (or (and (eq last-command 'mouse-save-then-kill)
1241 mouse-save-then-kill-posn)
1242 (and (memq last-command '(mouse-drag-region
1243 mouse-set-region))
1244 (or mark-even-if-inactive
1245 (not transient-mark-mode))))))
1246 (select-window window)
1247 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1248 (if (< (abs (- click-pt (mark t)))
1249 (abs (- click-pt (point))))
1250 (set-mark (car range))
1251 (goto-char (nth 1 range)))
1252 (setq deactivate-mark nil)
1253 (mouse-set-region-1)
1254 (when mouse-drag-copy-region
1255 ;; Region already copied to kill-ring once, so replace.
1256 (kill-new (filter-buffer-substring (mark t) (point)) t))
1257 ;; Arrange for a repeated mouse-3 to kill the region.
1258 (setq mouse-save-then-kill-posn click-pt)))
1260 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1262 (select-window window)
1263 (mouse-set-mark-fast click)
1264 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1265 (if before-scroll (goto-char before-scroll)))
1266 (exchange-point-and-mark)
1267 (mouse-set-region-1)
1268 (when mouse-drag-copy-region
1269 (kill-new (filter-buffer-substring (mark t) (point))))
1270 (setq mouse-save-then-kill-posn click-pt)))))
1273 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1274 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1275 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1276 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1277 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1279 (defconst mouse-secondary-overlay
1280 (let ((ol (make-overlay (point-min) (point-min))))
1281 (delete-overlay ol)
1282 (overlay-put ol 'face 'secondary-selection)
1284 "An overlay which records the current secondary selection.
1285 It is deleted when there is no secondary selection.")
1287 (defvar mouse-secondary-click-count 0)
1289 ;; A marker which records the specified first end for a secondary selection.
1290 ;; May be nil.
1291 (defvar mouse-secondary-start nil)
1293 (defun mouse-start-secondary (click)
1294 "Set one end of the secondary selection to the position clicked on.
1295 Use \\[mouse-secondary-save-then-kill] to set the other end
1296 and complete the secondary selection."
1297 (interactive "e")
1298 (mouse-minibuffer-check click)
1299 (let ((posn (event-start click)))
1300 (with-current-buffer (window-buffer (posn-window posn))
1301 ;; Cancel any preexisting secondary selection.
1302 (delete-overlay mouse-secondary-overlay)
1303 (if (numberp (posn-point posn))
1304 (progn
1305 (or mouse-secondary-start
1306 (setq mouse-secondary-start (make-marker)))
1307 (move-marker mouse-secondary-start (posn-point posn)))))))
1309 (defun mouse-set-secondary (click)
1310 "Set the secondary selection to the text that the mouse is dragged over.
1311 This must be bound to a mouse drag event."
1312 (interactive "e")
1313 (mouse-minibuffer-check click)
1314 (let ((posn (event-start click))
1316 (end (event-end click)))
1317 (with-current-buffer (window-buffer (posn-window posn))
1318 (if (numberp (posn-point posn))
1319 (setq beg (posn-point posn)))
1320 (move-overlay mouse-secondary-overlay beg (posn-point end))
1321 (gui-set-selection
1322 'SECONDARY
1323 (buffer-substring (overlay-start mouse-secondary-overlay)
1324 (overlay-end mouse-secondary-overlay))))))
1326 (defun mouse-drag-secondary (start-event)
1327 "Set the secondary selection to the text that the mouse is dragged over.
1328 Highlight the drag area as you move the mouse.
1329 This must be bound to a button-down mouse event.
1330 The function returns a non-nil value if it creates a secondary selection."
1331 (interactive "e")
1332 (mouse-minibuffer-check start-event)
1333 (let* ((echo-keystrokes 0)
1334 (start-posn (event-start start-event))
1335 (start-point (posn-point start-posn))
1336 (start-window (posn-window start-posn))
1337 (bounds (window-edges start-window))
1338 (top (nth 1 bounds))
1339 (bottom (if (window-minibuffer-p start-window)
1340 (nth 3 bounds)
1341 ;; Don't count the mode line.
1342 (1- (nth 3 bounds))))
1343 (click-count (1- (event-click-count start-event))))
1344 (with-current-buffer (window-buffer start-window)
1345 (setq mouse-secondary-click-count click-count)
1346 (if (> (mod click-count 3) 0)
1347 ;; Double or triple press: make an initial selection
1348 ;; of one word or line.
1349 (let ((range (mouse-start-end start-point start-point click-count)))
1350 (set-marker mouse-secondary-start nil)
1351 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1352 (window-buffer start-window)))
1353 ;; Single-press: cancel any preexisting secondary selection.
1354 (or mouse-secondary-start
1355 (setq mouse-secondary-start (make-marker)))
1356 (set-marker mouse-secondary-start start-point)
1357 (delete-overlay mouse-secondary-overlay))
1358 ;; FIXME: Use mouse-drag-track!
1359 (let (event end end-point)
1360 (track-mouse
1361 (while (progn
1362 (setq event (read-event))
1363 (or (mouse-movement-p event)
1364 (memq (car-safe event) '(switch-frame select-window))))
1366 (if (memq (car-safe event) '(switch-frame select-window))
1368 (setq end (event-end event)
1369 end-point (posn-point end))
1370 (cond
1371 ;; Are we moving within the original window?
1372 ((and (eq (posn-window end) start-window)
1373 (integer-or-marker-p end-point))
1374 (let ((range (mouse-start-end start-point end-point
1375 click-count)))
1376 (if (or (/= start-point end-point)
1377 (null (marker-position mouse-secondary-start)))
1378 (progn
1379 (set-marker mouse-secondary-start nil)
1380 (move-overlay mouse-secondary-overlay
1381 (car range) (nth 1 range))))))
1383 (let ((mouse-row (cdr (cdr (mouse-position)))))
1384 (cond
1385 ((null mouse-row))
1386 ((< mouse-row top)
1387 (mouse-scroll-subr start-window (- mouse-row top)
1388 mouse-secondary-overlay start-point))
1389 ((>= mouse-row bottom)
1390 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1391 mouse-secondary-overlay start-point)))))))))
1393 (if (consp event)
1394 (if (marker-position mouse-secondary-start)
1395 (save-window-excursion
1396 (delete-overlay mouse-secondary-overlay)
1397 (gui-set-selection 'SECONDARY nil)
1398 (select-window start-window)
1399 (save-excursion
1400 (goto-char mouse-secondary-start)
1401 (sit-for 1)
1402 nil))
1403 (gui-set-selection
1404 'SECONDARY
1405 (buffer-substring (overlay-start mouse-secondary-overlay)
1406 (overlay-end mouse-secondary-overlay)))))))))
1408 (defun mouse-yank-secondary (click)
1409 "Insert the secondary selection at the position clicked on.
1410 Move point to the end of the inserted text.
1411 If `mouse-yank-at-point' is non-nil, insert at point
1412 regardless of where you click."
1413 (interactive "e")
1414 ;; Give temporary modes such as isearch a chance to turn off.
1415 (run-hooks 'mouse-leave-buffer-hook)
1416 (or mouse-yank-at-point (mouse-set-point click))
1417 (let ((secondary (gui-get-selection 'SECONDARY)))
1418 (if secondary
1419 (insert-for-yank secondary)
1420 (error "No secondary selection"))))
1422 (defun mouse-kill-secondary ()
1423 "Kill the text in the secondary selection.
1424 This is intended more as a keyboard command than as a mouse command
1425 but it can work as either one.
1427 The current buffer (in case of keyboard use), or the buffer clicked on,
1428 must be the one that the secondary selection is in. This requirement
1429 is to prevent accidents."
1430 (interactive)
1431 (let* ((keys (this-command-keys))
1432 (click (elt keys (1- (length keys)))))
1433 (or (eq (overlay-buffer mouse-secondary-overlay)
1434 (if (listp click)
1435 (window-buffer (posn-window (event-start click)))
1436 (current-buffer)))
1437 (error "Select or click on the buffer where the secondary selection is")))
1438 (let (this-command)
1439 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1440 (kill-region (overlay-start mouse-secondary-overlay)
1441 (overlay-end mouse-secondary-overlay))))
1442 (delete-overlay mouse-secondary-overlay))
1444 (defun mouse-secondary-save-then-kill (click)
1445 "Set the secondary selection and save it to the kill ring.
1446 The second time, kill it. CLICK should be a mouse click event.
1448 If you have not called `mouse-start-secondary' in the clicked
1449 buffer, activate the secondary selection and set it between point
1450 and the click position CLICK.
1452 Otherwise, adjust the bounds of the secondary selection.
1453 Normally, do this by moving its beginning or end, whichever is
1454 closer, to CLICK. But if you have selected whole words or lines,
1455 adjust to the word or line boundary closest to CLICK instead.
1457 If this command is called a second consecutive time with the same
1458 CLICK position, kill the secondary selection."
1459 (interactive "e")
1460 (mouse-minibuffer-check click)
1461 (let* ((posn (event-start click))
1462 (click-pt (posn-point posn))
1463 (window (posn-window posn))
1464 (buf (window-buffer window))
1465 ;; Don't let a subsequent kill command append to this one.
1466 (this-command this-command)
1467 ;; Check if the user has multi-clicked to select words/lines.
1468 (click-count
1469 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1470 mouse-secondary-click-count
1472 (beg (overlay-start mouse-secondary-overlay))
1473 (end (overlay-end mouse-secondary-overlay)))
1475 (cond
1476 ((not (numberp click-pt)) nil)
1478 ;; If the secondary selection is not active in BUF, activate it.
1479 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1480 (if mouse-secondary-start
1481 (marker-buffer mouse-secondary-start)))))
1482 (select-window window)
1483 (setq mouse-secondary-start (make-marker))
1484 (move-marker mouse-secondary-start (point))
1485 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1486 (kill-ring-save (point) click-pt))
1488 ;; If the user clicked without moving point, delete the secondary
1489 ;; selection. This also resets `mouse-secondary-click-count'.
1490 ((and (eq last-command 'mouse-secondary-save-then-kill)
1491 (eq click-pt mouse-save-then-kill-posn)
1492 (eq window (selected-window)))
1493 (mouse-save-then-kill-delete-region beg end)
1494 (delete-overlay mouse-secondary-overlay)
1495 (setq mouse-secondary-click-count 0)
1496 (setq mouse-save-then-kill-posn nil))
1498 ;; Otherwise, if there is a suitable secondary selection overlay,
1499 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1500 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1501 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1502 (if (< (abs (- click-pt beg))
1503 (abs (- click-pt end)))
1504 (move-overlay mouse-secondary-overlay (car range) end)
1505 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1506 (setq deactivate-mark nil)
1507 (if (eq last-command 'mouse-secondary-save-then-kill)
1508 ;; If the front of the kill ring comes from an immediately
1509 ;; previous use of this command, replace the entry.
1510 (kill-new
1511 (buffer-substring (overlay-start mouse-secondary-overlay)
1512 (overlay-end mouse-secondary-overlay))
1514 (let (deactivate-mark)
1515 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1516 (overlay-end mouse-secondary-overlay))))
1517 (setq mouse-save-then-kill-posn click-pt))
1519 ;; Otherwise, set the secondary selection overlay.
1521 (select-window window)
1522 (if mouse-secondary-start
1523 ;; All we have is one end of a selection, so put the other
1524 ;; end here.
1525 (let ((start (+ 0 mouse-secondary-start)))
1526 (kill-ring-save start click-pt)
1527 (move-overlay mouse-secondary-overlay start click-pt)))
1528 (setq mouse-save-then-kill-posn click-pt))))
1530 ;; Finally, set the window system's secondary selection.
1531 (let (str)
1532 (and (overlay-buffer mouse-secondary-overlay)
1533 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1534 (overlay-end mouse-secondary-overlay)))
1535 (> (length str) 0)
1536 (gui-set-selection 'SECONDARY str))))
1539 (defcustom mouse-buffer-menu-maxlen 20
1540 "Number of buffers in one pane (submenu) of the buffer menu.
1541 If we have lots of buffers, divide them into groups of
1542 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1543 :type 'integer
1544 :group 'mouse)
1546 (defcustom mouse-buffer-menu-mode-mult 4
1547 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1548 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1549 will split the buffer menu by the major modes (see
1550 `mouse-buffer-menu-mode-groups') or just by menu length.
1551 Set to 1 (or even 0!) if you want to group by major mode always, and to
1552 a large number if you prefer a mixed multitude. The default is 4."
1553 :type 'integer
1554 :group 'mouse
1555 :version "20.3")
1557 (defvar mouse-buffer-menu-mode-groups
1558 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1559 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1560 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1561 . "Mail/News")
1562 ("\\<C\\>" . "C")
1563 ("ObjC" . "C")
1564 ("Text" . "Text")
1565 ("Outline" . "Text")
1566 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1567 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1568 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1569 . "GDB")
1570 ("Lisp" . "Lisp")))
1571 "How to group various major modes together in \\[mouse-buffer-menu].
1572 Each element has the form (REGEXP . GROUPNAME).
1573 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1575 (defun mouse-buffer-menu (event)
1576 "Pop up a menu of buffers for selection with the mouse.
1577 This switches buffers in the window that you clicked on,
1578 and selects that window."
1579 (interactive "e")
1580 (mouse-minibuffer-check event)
1581 (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1582 (window (posn-window (event-start event))))
1583 (when buf
1584 (select-window
1585 (if (framep window) (frame-selected-window window)
1586 window))
1587 (switch-to-buffer buf))))
1589 (defun mouse-buffer-menu-map ()
1590 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1591 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
1592 (dolist (buf buffers)
1593 ;; Divide all buffers into buckets for various major modes.
1594 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1595 (with-current-buffer buf
1596 (let* ((adjusted-major-mode major-mode) elt)
1597 (dolist (group mouse-buffer-menu-mode-groups)
1598 (when (string-match (car group) (format-mode-line mode-name))
1599 (setq adjusted-major-mode (cdr group))))
1600 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1601 (unless elt
1602 (setq elt (list adjusted-major-mode
1603 (if (stringp adjusted-major-mode)
1604 adjusted-major-mode
1605 (format-mode-line mode-name nil nil buf)))
1606 split-by-major-mode (cons elt split-by-major-mode)))
1607 (or (memq buf (cdr (cdr elt)))
1608 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1609 ;; Compute the sum of squares of sizes of the major-mode buckets.
1610 (let ((tail split-by-major-mode))
1611 (setq sum-of-squares 0)
1612 (while tail
1613 (setq sum-of-squares
1614 (+ sum-of-squares
1615 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1616 (setq tail (cdr tail))))
1617 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1618 (* (length buffers) (length buffers)))
1619 ;; Subdividing by major modes really helps, so let's do it.
1620 (let (subdivided-menus (buffers-left (length buffers)))
1621 ;; Sort the list to put the most popular major modes first.
1622 (setq split-by-major-mode
1623 (sort split-by-major-mode
1624 (function (lambda (elt1 elt2)
1625 (> (length elt1) (length elt2))))))
1626 ;; Make a separate submenu for each major mode
1627 ;; that has more than one buffer,
1628 ;; unless all the remaining buffers are less than 1/10 of them.
1629 (while (and split-by-major-mode
1630 (and (> (length (car split-by-major-mode)) 3)
1631 (> (* buffers-left 10) (length buffers))))
1632 (let ((this-mode-list (mouse-buffer-menu-alist
1633 (cdr (cdr (car split-by-major-mode))))))
1634 (and this-mode-list
1635 (setq subdivided-menus
1636 (cons (cons
1637 (nth 1 (car split-by-major-mode))
1638 this-mode-list)
1639 subdivided-menus))))
1640 (setq buffers-left
1641 (- buffers-left (length (cdr (car split-by-major-mode)))))
1642 (setq split-by-major-mode (cdr split-by-major-mode)))
1643 ;; If any major modes are left over,
1644 ;; make a single submenu for them.
1645 (if split-by-major-mode
1646 (let ((others-list
1647 (mouse-buffer-menu-alist
1648 ;; we don't need split-by-major-mode any more,
1649 ;; so we can ditch it with nconc (mapcan).
1650 (mapcan 'cddr split-by-major-mode))))
1651 (and others-list
1652 (setq subdivided-menus
1653 (cons (cons "Others" others-list)
1654 subdivided-menus)))))
1655 (cons "Buffer Menu" (nreverse subdivided-menus)))
1656 (cons "Buffer Menu"
1657 (mouse-buffer-menu-split "Select Buffer"
1658 (mouse-buffer-menu-alist buffers))))))
1660 (defun mouse-buffer-menu-alist (buffers)
1661 (let (tail
1662 (maxlen 0)
1663 head)
1664 (setq buffers
1665 (sort buffers
1666 (function (lambda (elt1 elt2)
1667 (string< (buffer-name elt1) (buffer-name elt2))))))
1668 (setq tail buffers)
1669 (while tail
1670 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1671 (setq maxlen
1672 (max maxlen
1673 (length (buffer-name (car tail))))))
1674 (setq tail (cdr tail)))
1675 (setq tail buffers)
1676 (while tail
1677 (let ((elt (car tail)))
1678 (if (/= (aref (buffer-name elt) 0) ?\s)
1679 (setq head
1680 (cons
1681 (cons
1682 (format
1683 (format "%%-%ds %%s%%s %%s" maxlen)
1684 (buffer-name elt)
1685 (if (buffer-modified-p elt) "*" " ")
1686 (with-current-buffer elt
1687 (if buffer-read-only "%" " "))
1688 (or (buffer-file-name elt)
1689 (with-current-buffer elt
1690 (if list-buffers-directory
1691 (expand-file-name
1692 list-buffers-directory)))
1693 ""))
1694 elt)
1695 head))))
1696 (setq tail (cdr tail)))
1697 ;; Compensate for the reversal that the above loop does.
1698 (nreverse head)))
1700 (defun mouse-buffer-menu-split (title alist)
1701 ;; If we have lots of buffers, divide them into groups of 20
1702 ;; and make a pane (or submenu) for each one.
1703 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1704 (let ((alist alist) sublists next
1705 (i 1))
1706 (while alist
1707 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1708 ;; and make them the next element of sublist.
1709 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1710 (if next
1711 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1712 nil))
1713 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1714 sublists))
1715 (setq i (1+ i))
1716 (setq alist next))
1717 (nreverse sublists))
1718 ;; Few buffers--put them all in one pane.
1719 (list (cons title alist))))
1721 (define-obsolete-function-alias
1722 'mouse-choose-completion 'choose-completion "23.2")
1724 ;; Font selection.
1726 (defun font-menu-add-default ()
1727 (let* ((default (frame-parameter nil 'font))
1728 (font-alist x-fixed-font-alist)
1729 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1730 (if (assoc "Default" elt)
1731 (delete (assoc "Default" elt) elt))
1732 (setcdr elt
1733 (cons (list "Default" default)
1734 (cdr elt)))))
1736 (defvar x-fixed-font-alist
1737 (list
1738 (purecopy "Font Menu")
1739 (cons
1740 (purecopy "Misc")
1741 (mapcar
1742 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1743 ;; For these, we specify the pixel height and width.
1744 '(("fixed" "fixed")
1745 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1746 ("6x12"
1747 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1748 ("6x13"
1749 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1750 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1751 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1752 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1753 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1754 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1755 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1756 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1757 ("")
1758 ("clean 5x8"
1759 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1760 ("clean 6x8"
1761 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1762 ("clean 8x8"
1763 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1764 ("clean 8x10"
1765 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1766 ("clean 8x14"
1767 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1768 ("clean 8x16"
1769 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1770 ("")
1771 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1772 ;; We don't seem to have these; who knows what they are.
1773 ;; ("fg-18" "fg-18")
1774 ;; ("fg-25" "fg-25")
1775 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1776 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1777 ("lucidasanstypewriter-bold-24"
1778 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
1779 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1780 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1783 (cons
1784 (purecopy "Courier")
1785 (mapcar
1786 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1787 ;; For these, we specify the point height.
1788 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1789 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1790 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1791 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1792 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1793 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1794 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1795 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1796 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1797 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1798 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1799 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1800 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1801 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1802 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1803 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1804 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1805 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1806 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1807 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1808 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1809 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1810 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1811 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1812 ))))
1813 "X fonts suitable for use in Emacs.")
1815 (declare-function generate-fontset-menu "fontset" ())
1817 (defun mouse-select-font ()
1818 "Prompt for a font name, using `x-popup-menu', and return it."
1819 (interactive)
1820 (unless (display-multi-font-p)
1821 (error "Cannot change fonts on this display"))
1822 (car
1823 (x-popup-menu
1824 (if (listp last-nonmenu-event)
1825 last-nonmenu-event
1826 (list '(0 0) (selected-window)))
1827 (append x-fixed-font-alist
1828 (list (generate-fontset-menu))))))
1830 (declare-function text-scale-mode "face-remap")
1832 (defun mouse-set-font (&rest fonts)
1833 "Set the default font for the selected frame.
1834 The argument FONTS is a list of font names; the first valid font
1835 in this list is used.
1837 When called interactively, pop up a menu and allow the user to
1838 choose a font."
1839 (interactive
1840 (progn (unless (display-multi-font-p)
1841 (error "Cannot change fonts on this display"))
1842 (x-popup-menu
1843 (if (listp last-nonmenu-event)
1844 last-nonmenu-event
1845 (list '(0 0) (selected-window)))
1846 ;; Append list of fontsets currently defined.
1847 (append x-fixed-font-alist (list (generate-fontset-menu))))))
1848 (if fonts
1849 (let (font)
1850 (while fonts
1851 (condition-case nil
1852 (progn
1853 (set-frame-font (car fonts))
1854 (setq font (car fonts))
1855 (setq fonts nil))
1856 (error
1857 (setq fonts (cdr fonts)))))
1858 (if (null font)
1859 (error "Font not found")))))
1861 (defvar mouse-appearance-menu-map nil)
1862 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
1863 (declare-function buffer-face-mode-invoke "face-remap"
1864 (face arg &optional interactive))
1865 (declare-function font-face-attributes "font.c" (font &optional frame))
1866 (defvar w32-use-w32-font-dialog)
1867 (defvar w32-fixed-font-alist)
1869 (defun mouse-appearance-menu (event)
1870 "Show a menu for changing the default face in the current buffer."
1871 (interactive "@e")
1872 (require 'face-remap)
1873 (when (display-multi-font-p)
1874 (with-selected-window (car (event-start event))
1875 (if mouse-appearance-menu-map
1876 nil ; regenerate new fonts
1877 ;; Initialize mouse-appearance-menu-map
1878 (setq mouse-appearance-menu-map
1879 (make-sparse-keymap "Change Default Buffer Face"))
1880 (define-key mouse-appearance-menu-map [face-remap-reset-base]
1881 '(menu-item "Reset to Default" face-remap-reset-base))
1882 (define-key mouse-appearance-menu-map [text-scale-decrease]
1883 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
1884 (define-key mouse-appearance-menu-map [text-scale-increase]
1885 '(menu-item "Increase Buffer Text Size" text-scale-increase))
1886 ;; Font selector
1887 (if (and (functionp 'x-select-font)
1888 (or (not (boundp 'w32-use-w32-font-dialog))
1889 w32-use-w32-font-dialog))
1890 (define-key mouse-appearance-menu-map [x-select-font]
1891 '(menu-item "Change Buffer Font..." x-select-font))
1892 ;; If the select-font is unavailable, construct a menu.
1893 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
1894 (font-alist (cdr (append
1895 (if (eq system-type 'windows-nt)
1896 w32-fixed-font-alist
1897 x-fixed-font-alist)
1898 (list (generate-fontset-menu))))))
1899 (dolist (family font-alist)
1900 (let* ((submenu-name (car family))
1901 (submenu-map (make-sparse-keymap submenu-name)))
1902 (dolist (font (cdr family))
1903 (let ((font-name (car font))
1904 font-symbol)
1905 (if (string= font-name "")
1906 (define-key submenu-map [space]
1907 '("--"))
1908 (setq font-symbol (intern (cadr font)))
1909 (define-key submenu-map (vector font-symbol)
1910 (list 'menu-item (car font) font-symbol)))))
1911 (define-key font-submenu (vector (intern submenu-name))
1912 (list 'menu-item submenu-name submenu-map))))
1913 (define-key mouse-appearance-menu-map [font-submenu]
1914 (list 'menu-item "Change Text Font" font-submenu)))))
1915 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
1916 (setq choice (nth (1- (length choice)) choice))
1917 (cond ((eq choice 'text-scale-increase)
1918 (text-scale-increase 1))
1919 ((eq choice 'text-scale-decrease)
1920 (text-scale-increase -1))
1921 ((eq choice 'face-remap-reset-base)
1922 (text-scale-mode 0)
1923 (buffer-face-mode 0))
1924 (choice
1925 ;; Either choice == 'x-select-font, or choice is a
1926 ;; symbol whose name is a font.
1927 (let ((font (if (eq choice 'x-select-font)
1928 (x-select-font)
1929 (symbol-name choice))))
1930 (buffer-face-mode-invoke
1931 (if (fontp font 'font-spec)
1932 (list :font font)
1933 (font-face-attributes font))
1934 t (called-interactively-p 'interactive)))))))))
1937 ;;; Bindings for mouse commands.
1939 (global-set-key [down-mouse-1] 'mouse-drag-region)
1940 (global-set-key [mouse-1] 'mouse-set-point)
1941 (global-set-key [drag-mouse-1] 'mouse-set-region)
1943 (defun mouse--strip-first-event (_prompt)
1944 (substring (this-single-command-raw-keys) 1))
1946 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
1947 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
1949 (global-set-key [mouse-2] 'mouse-yank-primary)
1950 ;; Allow yanking also when the corresponding cursor is "in the fringe".
1951 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
1952 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
1953 (global-set-key [mouse-3] 'mouse-save-then-kill)
1954 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
1955 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
1957 ;; By binding these to down-going events, we let the user use the up-going
1958 ;; event to make the selection, saving a click.
1959 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1960 (if (not (eq system-type 'ms-dos))
1961 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
1962 ;; C-down-mouse-2 is bound in facemenu.el.
1963 (global-set-key [C-down-mouse-3]
1964 `(menu-item ,(purecopy "Menu Bar") ignore
1965 :filter (lambda (_)
1966 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
1967 (mouse-menu-bar-map)
1968 (mouse-menu-major-mode-map)))))
1970 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
1971 ;; vertical-line prevents Emacs from signaling an error when the mouse
1972 ;; button is released after dragging these lines, on non-toolkit
1973 ;; versions.
1974 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
1975 (global-set-key [header-line mouse-1] 'mouse-select-window)
1976 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1977 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1978 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1979 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1980 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1981 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1982 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1983 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
1984 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1985 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1986 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1987 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
1988 (global-set-key [right-divider mouse-1] 'ignore)
1989 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
1990 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
1991 (global-set-key [bottom-divider mouse-1] 'ignore)
1992 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
1994 (provide 'mouse)
1996 ;;; mouse.el ends here