]> code.delx.au - gnu-emacs/blob - lisp/mouse.el
Fix shr.el/image build problem
[gnu-emacs] / lisp / mouse.el
1 ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1993-1995, 1999-2016 Free Software Foundation, Inc.
4
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: hardware, mouse
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
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.
29
30 ;;; Code:
31
32 ;;; Utility functions.
33
34 ;; Indent track-mouse like progn.
35 (put 'track-mouse 'lisp-indent-function 0)
36
37 (defgroup mouse nil
38 "Input from the mouse." ;; "Mouse support."
39 :group 'environment
40 :group 'editing)
41
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)
46
47 (defcustom mouse-drag-copy-region nil
48 "If non-nil, copy to kill-ring upon mouse adjustments of the region.
49
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)
55
56 (defcustom mouse-1-click-follows-link 450
57 "Non-nil means that clicking Mouse-1 on a link follows the link.
58
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).
64
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.
72
73 If value is `double', a double click follows the link.
74
75 Otherwise, a single Mouse-1 click unconditionally follows the link.
76
77 Note that dragging the mouse never follows the link.
78
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)
88
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.
91
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)
98
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))
118 nil
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))))))))
143
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)
148
149 \f
150 ;; Provide a mode-specific menu on a mouse button.
151
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")))))
179
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)))
185
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))
201
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)))))))
214
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)))
265
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))
273
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))
282
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))
294 \f
295 ;; Commands that operate on windows.
296
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))
304
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)))))
313
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))))
325
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)))
337
338 (defun mouse-delete-other-windows ()
339 "Delete all windows except the one you click on."
340 (interactive "@")
341 (delete-other-windows))
342
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)))))))
361
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)))))))
380
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))))))
430
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)))))))
530
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))
535
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))
540
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))
545 \f
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")
554
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))))
572
573 (defvar mouse-last-region-beg nil)
574 (defvar mouse-last-region-end nil)
575 (defvar mouse-last-region-tick nil)
576
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))))
583
584 (defvar mouse--drag-start-event nil)
585
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)))
638
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)))
649
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)
660
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)
670
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))))
706
707 (defvar mouse-selection-click-count 0)
708
709 (defvar mouse-selection-click-count-buffer nil)
710
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))
721
722
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)))
744
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.
751
752 A clickable link is identified by one of the following methods:
753
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.
756
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.
760
761 The resulting value determine whether POS is inside a link:
762
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.
765
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.
769
770 - Otherwise, return the value itself.
771
772 The return value is interpreted as follows:
773
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.
777
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.
781
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))))
804
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)
827
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 ;; We've recorded what we needed from the current buffer and
836 ;; window, now let's jump to the place of the event, where things
837 ;; are happening.
838 (_ (mouse-set-point start-event))
839 (echo-keystrokes 0)
840 (start-posn (event-start start-event))
841 (start-point (posn-point start-posn))
842 (start-window (posn-window start-posn))
843 (bounds (window-edges start-window))
844 (make-cursor-line-fully-visible nil)
845 (top (nth 1 bounds))
846 (bottom (if (window-minibuffer-p start-window)
847 (nth 3 bounds)
848 ;; Don't count the mode line.
849 (1- (nth 3 bounds))))
850 (click-count (1- (event-click-count start-event)))
851 ;; Suppress automatic hscrolling, because that is a nuisance
852 ;; when setting point near the right fringe (but see below).
853 (auto-hscroll-mode-saved auto-hscroll-mode))
854
855 (setq mouse-selection-click-count click-count)
856 ;; In case the down click is in the middle of some intangible text,
857 ;; use the end of that text, and put it in START-POINT.
858 (if (< (point) start-point)
859 (goto-char start-point))
860 (setq start-point (point))
861
862 ;; Activate the region, using `mouse-start-end' to determine where
863 ;; to put point and mark (e.g., double-click will select a word).
864 (setq-local transient-mark-mode
865 (if (eq transient-mark-mode 'lambda)
866 '(only)
867 (cons 'only transient-mark-mode)))
868 (let ((range (mouse-start-end start-point start-point click-count)))
869 (push-mark (nth 0 range) t t)
870 (goto-char (nth 1 range)))
871
872 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
873 (setq track-mouse t)
874 (setq auto-hscroll-mode nil)
875
876 (set-transient-map
877 (let ((map (make-sparse-keymap)))
878 (define-key map [switch-frame] #'ignore)
879 (define-key map [select-window] #'ignore)
880 (define-key map [mouse-movement]
881 (lambda (event) (interactive "e")
882 (let* ((end (event-end event))
883 (end-point (posn-point end)))
884 (unless (eq end-point start-point)
885 ;; As soon as the user moves, we can re-enable auto-hscroll.
886 (setq auto-hscroll-mode auto-hscroll-mode-saved)
887 ;; And remember that we have moved, so mouse-set-region can know
888 ;; its event is really a drag event.
889 (setcar start-event 'mouse-movement))
890 (if (and (eq (posn-window end) start-window)
891 (integer-or-marker-p end-point))
892 (mouse--drag-set-mark-and-point start-point
893 end-point click-count)
894 (let ((mouse-row (cdr (cdr (mouse-position)))))
895 (cond
896 ((null mouse-row))
897 ((< mouse-row top)
898 (mouse-scroll-subr start-window (- mouse-row top)
899 nil start-point))
900 ((>= mouse-row bottom)
901 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
902 nil start-point))))))))
903 map)
904 t (lambda ()
905 (setq track-mouse nil)
906 (setq auto-hscroll-mode auto-hscroll-mode-saved)
907 (deactivate-mark)
908 (pop-mark)))))
909
910 (defun mouse--drag-set-mark-and-point (start click click-count)
911 (let* ((range (mouse-start-end start click click-count))
912 (beg (nth 0 range))
913 (end (nth 1 range)))
914 (cond ((eq (mark) beg)
915 (goto-char end))
916 ((eq (mark) end)
917 (goto-char beg))
918 ((< click (mark))
919 (set-mark end)
920 (goto-char beg))
921 (t
922 (set-mark beg)
923 (goto-char end)))))
924
925 ;; Commands to handle xterm-style multiple clicks.
926 (defun mouse-skip-word (dir)
927 "Skip over word, over whitespace, or over identical punctuation.
928 If DIR is positive skip forward; if negative, skip backward."
929 (let* ((char (following-char))
930 (syntax (char-to-string (char-syntax char))))
931 (cond ((string= syntax "w")
932 ;; Here, we can't use skip-syntax-forward/backward because
933 ;; they don't pay attention to word-separating-categories,
934 ;; and thus they will skip over a true word boundary. So,
935 ;; we simulate the original behavior by using forward-word.
936 (if (< dir 0)
937 (if (not (looking-at "\\<"))
938 (forward-word -1))
939 (if (or (looking-at "\\<") (not (looking-at "\\>")))
940 (forward-word 1))))
941 ((string= syntax " ")
942 (if (< dir 0)
943 (skip-syntax-backward syntax)
944 (skip-syntax-forward syntax)))
945 ((string= syntax "_")
946 (if (< dir 0)
947 (skip-syntax-backward "w_")
948 (skip-syntax-forward "w_")))
949 ((< dir 0)
950 (while (and (not (bobp)) (= (preceding-char) char))
951 (forward-char -1)))
952 (t
953 (while (and (not (eobp)) (= (following-char) char))
954 (forward-char 1))))))
955
956 (defun mouse-start-end (start end mode)
957 "Return a list of region bounds based on START and END according to MODE.
958 If MODE is 0 then set point to (min START END), mark to (max START END).
959 If MODE is 1 then set point to start of word at (min START END),
960 mark to end of word at (max START END).
961 If MODE is 2 then do the same for lines."
962 (if (> start end)
963 (let ((temp start))
964 (setq start end
965 end temp)))
966 (setq mode (mod mode 3))
967 (cond ((= mode 0)
968 (list start end))
969 ((and (= mode 1)
970 (= start end)
971 (char-after start)
972 (= (char-syntax (char-after start)) ?\())
973 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
974 ;; This happens in CC Mode when unbalanced parens in CPP
975 ;; constructs are given punctuation syntax with
976 ;; syntax-table text properties. (2016-02-21).
977 (signal 'scan-error (list "Containing expression ends prematurely"
978 start start))
979 (list start
980 (save-excursion
981 (goto-char start)
982 (forward-sexp 1)
983 (point)))))
984 ((and (= mode 1)
985 (= start end)
986 (char-after start)
987 (= (char-syntax (char-after start)) ?\)))
988 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
989 ;; See above comment about CC Mode.
990 (signal 'scan-error (list "Unbalanced parentheses" start start))
991 (list (save-excursion
992 (goto-char (1+ start))
993 (backward-sexp 1)
994 (point))
995 (1+ start))))
996 ((and (= mode 1)
997 (= start end)
998 (char-after start)
999 (= (char-syntax (char-after start)) ?\"))
1000 (let ((open (or (eq start (point-min))
1001 (save-excursion
1002 (goto-char (- start 1))
1003 (looking-at "\\s(\\|\\s \\|\\s>")))))
1004 (if open
1005 (list start
1006 (save-excursion
1007 (condition-case nil
1008 (progn
1009 (goto-char start)
1010 (forward-sexp 1)
1011 (point))
1012 (error end))))
1013 (list (save-excursion
1014 (condition-case nil
1015 (progn
1016 (goto-char (1+ start))
1017 (backward-sexp 1)
1018 (point))
1019 (error end)))
1020 (1+ start)))))
1021 ((= mode 1)
1022 (list (save-excursion
1023 (goto-char start)
1024 (mouse-skip-word -1)
1025 (point))
1026 (save-excursion
1027 (goto-char end)
1028 (mouse-skip-word 1)
1029 (point))))
1030 ((= mode 2)
1031 (list (save-excursion
1032 (goto-char start)
1033 (line-beginning-position 1))
1034 (save-excursion
1035 (goto-char end)
1036 (forward-line 1)
1037 (point))))))
1038 \f
1039 ;; Subroutine: set the mark where CLICK happened,
1040 ;; but don't do anything else.
1041 (defun mouse-set-mark-fast (click)
1042 (mouse-minibuffer-check click)
1043 (let ((posn (event-start click)))
1044 (select-window (posn-window posn))
1045 (if (numberp (posn-point posn))
1046 (push-mark (posn-point posn) t t))))
1047
1048 (defun mouse-undouble-last-event (events)
1049 (let* ((index (1- (length events)))
1050 (last (nthcdr index events))
1051 (event (car last))
1052 (basic (event-basic-type event))
1053 (old-modifiers (event-modifiers event))
1054 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1055 (new
1056 (if (consp event)
1057 ;; Use reverse, not nreverse, since event-modifiers
1058 ;; does not copy the list it returns.
1059 (cons (event-convert-list (reverse (cons basic modifiers)))
1060 (cdr event))
1061 event)))
1062 (setcar last new)
1063 (if (and (not (equal modifiers old-modifiers))
1064 (key-binding (apply 'vector events)))
1065 t
1066 (setcar last event)
1067 nil)))
1068
1069 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1070
1071 (defun mouse-set-mark (click)
1072 "Set mark at the position clicked on with the mouse.
1073 Display cursor at that position for a second.
1074 This must be bound to a mouse click."
1075 (interactive "e")
1076 (mouse-minibuffer-check click)
1077 (select-window (posn-window (event-start click)))
1078 ;; FIXME: Use save-excursion
1079 (let ((point-save (point)))
1080 (unwind-protect
1081 (progn (mouse-set-point click)
1082 (push-mark nil t t)
1083 (or transient-mark-mode
1084 (sit-for 1)))
1085 (goto-char point-save))))
1086
1087 (defun mouse-kill (click)
1088 "Kill the region between point and the mouse click.
1089 The text is saved in the kill ring, as with \\[kill-region]."
1090 (interactive "e")
1091 (mouse-minibuffer-check click)
1092 (let* ((posn (event-start click))
1093 (click-posn (posn-point posn)))
1094 (select-window (posn-window posn))
1095 (if (numberp click-posn)
1096 (kill-region (min (point) click-posn)
1097 (max (point) click-posn)))))
1098
1099 (defun mouse-yank-at-click (click arg)
1100 "Insert the last stretch of killed text at the position clicked on.
1101 Also move point to one end of the text thus inserted (normally the end),
1102 and set mark at the beginning.
1103 Prefix arguments are interpreted as with \\[yank].
1104 If `mouse-yank-at-point' is non-nil, insert at point
1105 regardless of where you click."
1106 (interactive "e\nP")
1107 ;; Give temporary modes such as isearch a chance to turn off.
1108 (run-hooks 'mouse-leave-buffer-hook)
1109 (when select-active-regions
1110 ;; Without this, confusing things happen upon e.g. inserting into
1111 ;; the middle of an active region.
1112 (deactivate-mark))
1113 (or mouse-yank-at-point (mouse-set-point click))
1114 (setq this-command 'yank)
1115 (setq mouse-selection-click-count 0)
1116 (yank arg))
1117
1118 (defun mouse-yank-primary (click)
1119 "Insert the primary selection at the position clicked on.
1120 Move point to the end of the inserted text, and set mark at
1121 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1122 regardless of where you click."
1123 (interactive "e")
1124 ;; Give temporary modes such as isearch a chance to turn off.
1125 (run-hooks 'mouse-leave-buffer-hook)
1126 ;; Without this, confusing things happen upon e.g. inserting into
1127 ;; the middle of an active region.
1128 (when select-active-regions
1129 (let (select-active-regions)
1130 (deactivate-mark)))
1131 (or mouse-yank-at-point (mouse-set-point click))
1132 (let ((primary (gui-get-primary-selection)))
1133 (push-mark (point))
1134 (insert-for-yank primary)))
1135
1136 (defun mouse-kill-ring-save (click)
1137 "Copy the region between point and the mouse click in the kill ring.
1138 This does not delete the region; it acts like \\[kill-ring-save]."
1139 (interactive "e")
1140 (mouse-set-mark-fast click)
1141 (let (this-command last-command)
1142 (kill-ring-save (point) (mark t))))
1143
1144 ;; This function used to delete the text between point and the mouse
1145 ;; whenever it was equal to the front of the kill ring, but some
1146 ;; people found that confusing.
1147
1148 ;; The position of the last invocation of `mouse-save-then-kill'.
1149 (defvar mouse-save-then-kill-posn nil)
1150
1151 (defun mouse-save-then-kill-delete-region (beg end)
1152 ;; We must make our own undo boundaries
1153 ;; because they happen automatically only for the current buffer.
1154 (undo-boundary)
1155 (if (or (= beg end) (eq buffer-undo-list t))
1156 ;; If we have no undo list in this buffer,
1157 ;; just delete.
1158 (delete-region beg end)
1159 ;; Delete, but make the undo-list entry share with the kill ring.
1160 ;; First, delete just one char, so in case buffer is being modified
1161 ;; for the first time, the undo list records that fact.
1162 (let ((inhibit-modification-hooks t))
1163 (delete-region beg
1164 (+ beg (if (> end beg) 1 -1))))
1165 (let ((buffer-undo-list buffer-undo-list))
1166 ;; Undo that deletion--but don't change the undo list!
1167 (let ((inhibit-modification-hooks t))
1168 (primitive-undo 1 buffer-undo-list))
1169 ;; Now delete the rest of the specified region,
1170 ;; but don't record it.
1171 (setq buffer-undo-list t)
1172 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1173 (error "Lossage in mouse-save-then-kill-delete-region"))
1174 (delete-region beg end))
1175 (let ((tail buffer-undo-list))
1176 ;; Search back in buffer-undo-list for the string
1177 ;; that came from deleting one character.
1178 (while (and tail (not (stringp (car (car tail)))))
1179 (setq tail (cdr tail)))
1180 ;; Replace it with an entry for the entire deleted text.
1181 (and tail
1182 (setcar tail (cons (car kill-ring) (min beg end))))))
1183 (undo-boundary))
1184
1185 (defun mouse-save-then-kill (click)
1186 "Set the region according to CLICK; the second time, kill it.
1187 CLICK should be a mouse click event.
1188
1189 If the region is inactive, activate it temporarily. Set mark at
1190 the original point, and move point to the position of CLICK.
1191
1192 If the region is already active, adjust it. Normally, do this by
1193 moving point or mark, whichever is closer, to CLICK. But if you
1194 have selected whole words or lines, move point or mark to the
1195 word or line boundary closest to CLICK instead.
1196
1197 If `mouse-drag-copy-region' is non-nil, this command also saves the
1198 new region to the kill ring (replacing the previous kill if the
1199 previous region was just saved to the kill ring).
1200
1201 If this command is called a second consecutive time with the same
1202 CLICK position, kill the region (or delete it
1203 if `mouse-drag-copy-region' is non-nil)"
1204 (interactive "e")
1205 (mouse-minibuffer-check click)
1206 (let* ((posn (event-start click))
1207 (click-pt (posn-point posn))
1208 (window (posn-window posn))
1209 (buf (window-buffer window))
1210 ;; Don't let a subsequent kill command append to this one.
1211 (this-command this-command)
1212 ;; Check if the user has multi-clicked to select words/lines.
1213 (click-count
1214 (if (and (eq mouse-selection-click-count-buffer buf)
1215 (with-current-buffer buf (mark t)))
1216 mouse-selection-click-count
1217 0)))
1218 (cond
1219 ((not (numberp click-pt)) nil)
1220 ;; If the user clicked without moving point, kill the region.
1221 ;; This also resets `mouse-selection-click-count'.
1222 ((and (eq last-command 'mouse-save-then-kill)
1223 (eq click-pt mouse-save-then-kill-posn)
1224 (eq window (selected-window)))
1225 (if mouse-drag-copy-region
1226 ;; Region already saved in the previous click;
1227 ;; don't make a duplicate entry, just delete.
1228 (delete-region (mark t) (point))
1229 (kill-region (mark t) (point)))
1230 (setq mouse-selection-click-count 0)
1231 (setq mouse-save-then-kill-posn nil))
1232
1233 ;; Otherwise, if there is a suitable region, adjust it by moving
1234 ;; one end (whichever is closer) to CLICK-PT.
1235 ((or (with-current-buffer buf (region-active-p))
1236 (and (eq window (selected-window))
1237 (mark t)
1238 (or (and (eq last-command 'mouse-save-then-kill)
1239 mouse-save-then-kill-posn)
1240 (and (memq last-command '(mouse-drag-region
1241 mouse-set-region))
1242 (or mark-even-if-inactive
1243 (not transient-mark-mode))))))
1244 (select-window window)
1245 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1246 (if (< (abs (- click-pt (mark t)))
1247 (abs (- click-pt (point))))
1248 (set-mark (car range))
1249 (goto-char (nth 1 range)))
1250 (setq deactivate-mark nil)
1251 (mouse-set-region-1)
1252 (when mouse-drag-copy-region
1253 ;; Region already copied to kill-ring once, so replace.
1254 (kill-new (filter-buffer-substring (mark t) (point)) t))
1255 ;; Arrange for a repeated mouse-3 to kill the region.
1256 (setq mouse-save-then-kill-posn click-pt)))
1257
1258 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1259 (t
1260 (select-window window)
1261 (mouse-set-mark-fast click)
1262 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1263 (if before-scroll (goto-char before-scroll)))
1264 (exchange-point-and-mark)
1265 (mouse-set-region-1)
1266 (when mouse-drag-copy-region
1267 (kill-new (filter-buffer-substring (mark t) (point))))
1268 (setq mouse-save-then-kill-posn click-pt)))))
1269
1270 \f
1271 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1272 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1273 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1274 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1275 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1276
1277 (defconst mouse-secondary-overlay
1278 (let ((ol (make-overlay (point-min) (point-min))))
1279 (delete-overlay ol)
1280 (overlay-put ol 'face 'secondary-selection)
1281 ol)
1282 "An overlay which records the current secondary selection.
1283 It is deleted when there is no secondary selection.")
1284
1285 (defvar mouse-secondary-click-count 0)
1286
1287 ;; A marker which records the specified first end for a secondary selection.
1288 ;; May be nil.
1289 (defvar mouse-secondary-start nil)
1290
1291 (defun mouse-start-secondary (click)
1292 "Set one end of the secondary selection to the position clicked on.
1293 Use \\[mouse-secondary-save-then-kill] to set the other end
1294 and complete the secondary selection."
1295 (interactive "e")
1296 (mouse-minibuffer-check click)
1297 (let ((posn (event-start click)))
1298 (with-current-buffer (window-buffer (posn-window posn))
1299 ;; Cancel any preexisting secondary selection.
1300 (delete-overlay mouse-secondary-overlay)
1301 (if (numberp (posn-point posn))
1302 (progn
1303 (or mouse-secondary-start
1304 (setq mouse-secondary-start (make-marker)))
1305 (move-marker mouse-secondary-start (posn-point posn)))))))
1306
1307 (defun mouse-set-secondary (click)
1308 "Set the secondary selection to the text that the mouse is dragged over.
1309 This must be bound to a mouse drag event."
1310 (interactive "e")
1311 (mouse-minibuffer-check click)
1312 (let ((posn (event-start click))
1313 beg
1314 (end (event-end click)))
1315 (with-current-buffer (window-buffer (posn-window posn))
1316 (if (numberp (posn-point posn))
1317 (setq beg (posn-point posn)))
1318 (move-overlay mouse-secondary-overlay beg (posn-point end))
1319 (gui-set-selection
1320 'SECONDARY
1321 (buffer-substring (overlay-start mouse-secondary-overlay)
1322 (overlay-end mouse-secondary-overlay))))))
1323
1324 (defun mouse-drag-secondary (start-event)
1325 "Set the secondary selection to the text that the mouse is dragged over.
1326 Highlight the drag area as you move the mouse.
1327 This must be bound to a button-down mouse event.
1328 The function returns a non-nil value if it creates a secondary selection."
1329 (interactive "e")
1330 (mouse-minibuffer-check start-event)
1331 (let* ((echo-keystrokes 0)
1332 (start-posn (event-start start-event))
1333 (start-point (posn-point start-posn))
1334 (start-window (posn-window start-posn))
1335 (bounds (window-edges start-window))
1336 (top (nth 1 bounds))
1337 (bottom (if (window-minibuffer-p start-window)
1338 (nth 3 bounds)
1339 ;; Don't count the mode line.
1340 (1- (nth 3 bounds))))
1341 (click-count (1- (event-click-count start-event))))
1342 (with-current-buffer (window-buffer start-window)
1343 (setq mouse-secondary-click-count click-count)
1344 (if (> (mod click-count 3) 0)
1345 ;; Double or triple press: make an initial selection
1346 ;; of one word or line.
1347 (let ((range (mouse-start-end start-point start-point click-count)))
1348 (set-marker mouse-secondary-start nil)
1349 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1350 (window-buffer start-window)))
1351 ;; Single-press: cancel any preexisting secondary selection.
1352 (or mouse-secondary-start
1353 (setq mouse-secondary-start (make-marker)))
1354 (set-marker mouse-secondary-start start-point)
1355 (delete-overlay mouse-secondary-overlay))
1356 ;; FIXME: Use mouse-drag-track!
1357 (let (event end end-point)
1358 (track-mouse
1359 (while (progn
1360 (setq event (read-event))
1361 (or (mouse-movement-p event)
1362 (memq (car-safe event) '(switch-frame select-window))))
1363
1364 (if (memq (car-safe event) '(switch-frame select-window))
1365 nil
1366 (setq end (event-end event)
1367 end-point (posn-point end))
1368 (cond
1369 ;; Are we moving within the original window?
1370 ((and (eq (posn-window end) start-window)
1371 (integer-or-marker-p end-point))
1372 (let ((range (mouse-start-end start-point end-point
1373 click-count)))
1374 (if (or (/= start-point end-point)
1375 (null (marker-position mouse-secondary-start)))
1376 (progn
1377 (set-marker mouse-secondary-start nil)
1378 (move-overlay mouse-secondary-overlay
1379 (car range) (nth 1 range))))))
1380 (t
1381 (let ((mouse-row (cdr (cdr (mouse-position)))))
1382 (cond
1383 ((null mouse-row))
1384 ((< mouse-row top)
1385 (mouse-scroll-subr start-window (- mouse-row top)
1386 mouse-secondary-overlay start-point))
1387 ((>= mouse-row bottom)
1388 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1389 mouse-secondary-overlay start-point)))))))))
1390
1391 (if (consp event)
1392 (if (marker-position mouse-secondary-start)
1393 (save-window-excursion
1394 (delete-overlay mouse-secondary-overlay)
1395 (gui-set-selection 'SECONDARY nil)
1396 (select-window start-window)
1397 (save-excursion
1398 (goto-char mouse-secondary-start)
1399 (sit-for 1)
1400 nil))
1401 (gui-set-selection
1402 'SECONDARY
1403 (buffer-substring (overlay-start mouse-secondary-overlay)
1404 (overlay-end mouse-secondary-overlay)))))))))
1405
1406 (defun mouse-yank-secondary (click)
1407 "Insert the secondary selection at the position clicked on.
1408 Move point to the end of the inserted text.
1409 If `mouse-yank-at-point' is non-nil, insert at point
1410 regardless of where you click."
1411 (interactive "e")
1412 ;; Give temporary modes such as isearch a chance to turn off.
1413 (run-hooks 'mouse-leave-buffer-hook)
1414 (or mouse-yank-at-point (mouse-set-point click))
1415 (let ((secondary (gui-get-selection 'SECONDARY)))
1416 (if secondary
1417 (insert-for-yank secondary)
1418 (error "No secondary selection"))))
1419
1420 (defun mouse-kill-secondary ()
1421 "Kill the text in the secondary selection.
1422 This is intended more as a keyboard command than as a mouse command
1423 but it can work as either one.
1424
1425 The current buffer (in case of keyboard use), or the buffer clicked on,
1426 must be the one that the secondary selection is in. This requirement
1427 is to prevent accidents."
1428 (interactive)
1429 (let* ((keys (this-command-keys))
1430 (click (elt keys (1- (length keys)))))
1431 (or (eq (overlay-buffer mouse-secondary-overlay)
1432 (if (listp click)
1433 (window-buffer (posn-window (event-start click)))
1434 (current-buffer)))
1435 (error "Select or click on the buffer where the secondary selection is")))
1436 (let (this-command)
1437 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1438 (kill-region (overlay-start mouse-secondary-overlay)
1439 (overlay-end mouse-secondary-overlay))))
1440 (delete-overlay mouse-secondary-overlay))
1441
1442 (defun mouse-secondary-save-then-kill (click)
1443 "Set the secondary selection and save it to the kill ring.
1444 The second time, kill it. CLICK should be a mouse click event.
1445
1446 If you have not called `mouse-start-secondary' in the clicked
1447 buffer, activate the secondary selection and set it between point
1448 and the click position CLICK.
1449
1450 Otherwise, adjust the bounds of the secondary selection.
1451 Normally, do this by moving its beginning or end, whichever is
1452 closer, to CLICK. But if you have selected whole words or lines,
1453 adjust to the word or line boundary closest to CLICK instead.
1454
1455 If this command is called a second consecutive time with the same
1456 CLICK position, kill the secondary selection."
1457 (interactive "e")
1458 (mouse-minibuffer-check click)
1459 (let* ((posn (event-start click))
1460 (click-pt (posn-point posn))
1461 (window (posn-window posn))
1462 (buf (window-buffer window))
1463 ;; Don't let a subsequent kill command append to this one.
1464 (this-command this-command)
1465 ;; Check if the user has multi-clicked to select words/lines.
1466 (click-count
1467 (if (eq (overlay-buffer mouse-secondary-overlay) buf)
1468 mouse-secondary-click-count
1469 0))
1470 (beg (overlay-start mouse-secondary-overlay))
1471 (end (overlay-end mouse-secondary-overlay)))
1472
1473 (cond
1474 ((not (numberp click-pt)) nil)
1475
1476 ;; If the secondary selection is not active in BUF, activate it.
1477 ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
1478 (if mouse-secondary-start
1479 (marker-buffer mouse-secondary-start)))))
1480 (select-window window)
1481 (setq mouse-secondary-start (make-marker))
1482 (move-marker mouse-secondary-start (point))
1483 (move-overlay mouse-secondary-overlay (point) click-pt buf)
1484 (kill-ring-save (point) click-pt))
1485
1486 ;; If the user clicked without moving point, delete the secondary
1487 ;; selection. This also resets `mouse-secondary-click-count'.
1488 ((and (eq last-command 'mouse-secondary-save-then-kill)
1489 (eq click-pt mouse-save-then-kill-posn)
1490 (eq window (selected-window)))
1491 (mouse-save-then-kill-delete-region beg end)
1492 (delete-overlay mouse-secondary-overlay)
1493 (setq mouse-secondary-click-count 0)
1494 (setq mouse-save-then-kill-posn nil))
1495
1496 ;; Otherwise, if there is a suitable secondary selection overlay,
1497 ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
1498 ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
1499 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1500 (if (< (abs (- click-pt beg))
1501 (abs (- click-pt end)))
1502 (move-overlay mouse-secondary-overlay (car range) end)
1503 (move-overlay mouse-secondary-overlay beg (nth 1 range))))
1504 (setq deactivate-mark nil)
1505 (if (eq last-command 'mouse-secondary-save-then-kill)
1506 ;; If the front of the kill ring comes from an immediately
1507 ;; previous use of this command, replace the entry.
1508 (kill-new
1509 (buffer-substring (overlay-start mouse-secondary-overlay)
1510 (overlay-end mouse-secondary-overlay))
1511 t)
1512 (let (deactivate-mark)
1513 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1514 (overlay-end mouse-secondary-overlay))))
1515 (setq mouse-save-then-kill-posn click-pt))
1516
1517 ;; Otherwise, set the secondary selection overlay.
1518 (t
1519 (select-window window)
1520 (if mouse-secondary-start
1521 ;; All we have is one end of a selection, so put the other
1522 ;; end here.
1523 (let ((start (+ 0 mouse-secondary-start)))
1524 (kill-ring-save start click-pt)
1525 (move-overlay mouse-secondary-overlay start click-pt)))
1526 (setq mouse-save-then-kill-posn click-pt))))
1527
1528 ;; Finally, set the window system's secondary selection.
1529 (let (str)
1530 (and (overlay-buffer mouse-secondary-overlay)
1531 (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
1532 (overlay-end mouse-secondary-overlay)))
1533 (> (length str) 0)
1534 (gui-set-selection 'SECONDARY str))))
1535
1536 \f
1537 (defcustom mouse-buffer-menu-maxlen 20
1538 "Number of buffers in one pane (submenu) of the buffer menu.
1539 If we have lots of buffers, divide them into groups of
1540 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1541 :type 'integer
1542 :group 'mouse)
1543
1544 (defcustom mouse-buffer-menu-mode-mult 4
1545 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1546 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1547 will split the buffer menu by the major modes (see
1548 `mouse-buffer-menu-mode-groups') or just by menu length.
1549 Set to 1 (or even 0!) if you want to group by major mode always, and to
1550 a large number if you prefer a mixed multitude. The default is 4."
1551 :type 'integer
1552 :group 'mouse
1553 :version "20.3")
1554
1555 (defvar mouse-buffer-menu-mode-groups
1556 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1557 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1558 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1559 . "Mail/News")
1560 ("\\<C\\>" . "C")
1561 ("ObjC" . "C")
1562 ("Text" . "Text")
1563 ("Outline" . "Text")
1564 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1565 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1566 ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
1567 . "GDB")
1568 ("Lisp" . "Lisp")))
1569 "How to group various major modes together in \\[mouse-buffer-menu].
1570 Each element has the form (REGEXP . GROUPNAME).
1571 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1572
1573 (defun mouse-buffer-menu (event)
1574 "Pop up a menu of buffers for selection with the mouse.
1575 This switches buffers in the window that you clicked on,
1576 and selects that window."
1577 (interactive "e")
1578 (mouse-minibuffer-check event)
1579 (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1580 (window (posn-window (event-start event))))
1581 (when buf
1582 (select-window
1583 (if (framep window) (frame-selected-window window)
1584 window))
1585 (switch-to-buffer buf))))
1586
1587 (defun mouse-buffer-menu-map ()
1588 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1589 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
1590 (dolist (buf buffers)
1591 ;; Divide all buffers into buckets for various major modes.
1592 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1593 (with-current-buffer buf
1594 (let* ((adjusted-major-mode major-mode) elt)
1595 (dolist (group mouse-buffer-menu-mode-groups)
1596 (when (string-match (car group) (format-mode-line mode-name))
1597 (setq adjusted-major-mode (cdr group))))
1598 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1599 (unless elt
1600 (setq elt (list adjusted-major-mode
1601 (if (stringp adjusted-major-mode)
1602 adjusted-major-mode
1603 (format-mode-line mode-name nil nil buf)))
1604 split-by-major-mode (cons elt split-by-major-mode)))
1605 (or (memq buf (cdr (cdr elt)))
1606 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1607 ;; Compute the sum of squares of sizes of the major-mode buckets.
1608 (let ((tail split-by-major-mode))
1609 (setq sum-of-squares 0)
1610 (while tail
1611 (setq sum-of-squares
1612 (+ sum-of-squares
1613 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1614 (setq tail (cdr tail))))
1615 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1616 (* (length buffers) (length buffers)))
1617 ;; Subdividing by major modes really helps, so let's do it.
1618 (let (subdivided-menus (buffers-left (length buffers)))
1619 ;; Sort the list to put the most popular major modes first.
1620 (setq split-by-major-mode
1621 (sort split-by-major-mode
1622 (function (lambda (elt1 elt2)
1623 (> (length elt1) (length elt2))))))
1624 ;; Make a separate submenu for each major mode
1625 ;; that has more than one buffer,
1626 ;; unless all the remaining buffers are less than 1/10 of them.
1627 (while (and split-by-major-mode
1628 (and (> (length (car split-by-major-mode)) 3)
1629 (> (* buffers-left 10) (length buffers))))
1630 (let ((this-mode-list (mouse-buffer-menu-alist
1631 (cdr (cdr (car split-by-major-mode))))))
1632 (and this-mode-list
1633 (setq subdivided-menus
1634 (cons (cons
1635 (nth 1 (car split-by-major-mode))
1636 this-mode-list)
1637 subdivided-menus))))
1638 (setq buffers-left
1639 (- buffers-left (length (cdr (car split-by-major-mode)))))
1640 (setq split-by-major-mode (cdr split-by-major-mode)))
1641 ;; If any major modes are left over,
1642 ;; make a single submenu for them.
1643 (if split-by-major-mode
1644 (let ((others-list
1645 (mouse-buffer-menu-alist
1646 ;; we don't need split-by-major-mode any more,
1647 ;; so we can ditch it with nconc (mapcan).
1648 (mapcan 'cddr split-by-major-mode))))
1649 (and others-list
1650 (setq subdivided-menus
1651 (cons (cons "Others" others-list)
1652 subdivided-menus)))))
1653 (cons "Buffer Menu" (nreverse subdivided-menus)))
1654 (cons "Buffer Menu"
1655 (mouse-buffer-menu-split "Select Buffer"
1656 (mouse-buffer-menu-alist buffers))))))
1657
1658 (defun mouse-buffer-menu-alist (buffers)
1659 (let (tail
1660 (maxlen 0)
1661 head)
1662 (setq buffers
1663 (sort buffers
1664 (function (lambda (elt1 elt2)
1665 (string< (buffer-name elt1) (buffer-name elt2))))))
1666 (setq tail buffers)
1667 (while tail
1668 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1669 (setq maxlen
1670 (max maxlen
1671 (length (buffer-name (car tail))))))
1672 (setq tail (cdr tail)))
1673 (setq tail buffers)
1674 (while tail
1675 (let ((elt (car tail)))
1676 (if (/= (aref (buffer-name elt) 0) ?\s)
1677 (setq head
1678 (cons
1679 (cons
1680 (format
1681 (format "%%-%ds %%s%%s %%s" maxlen)
1682 (buffer-name elt)
1683 (if (buffer-modified-p elt) "*" " ")
1684 (with-current-buffer elt
1685 (if buffer-read-only "%" " "))
1686 (or (buffer-file-name elt)
1687 (with-current-buffer elt
1688 (if list-buffers-directory
1689 (expand-file-name
1690 list-buffers-directory)))
1691 ""))
1692 elt)
1693 head))))
1694 (setq tail (cdr tail)))
1695 ;; Compensate for the reversal that the above loop does.
1696 (nreverse head)))
1697
1698 (defun mouse-buffer-menu-split (title alist)
1699 ;; If we have lots of buffers, divide them into groups of 20
1700 ;; and make a pane (or submenu) for each one.
1701 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1702 (let ((alist alist) sublists next
1703 (i 1))
1704 (while alist
1705 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1706 ;; and make them the next element of sublist.
1707 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1708 (if next
1709 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1710 nil))
1711 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1712 sublists))
1713 (setq i (1+ i))
1714 (setq alist next))
1715 (nreverse sublists))
1716 ;; Few buffers--put them all in one pane.
1717 (list (cons title alist))))
1718 \f
1719 (define-obsolete-function-alias
1720 'mouse-choose-completion 'choose-completion "23.2")
1721
1722 ;; Font selection.
1723
1724 (defun font-menu-add-default ()
1725 (let* ((default (frame-parameter nil 'font))
1726 (font-alist x-fixed-font-alist)
1727 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1728 (if (assoc "Default" elt)
1729 (delete (assoc "Default" elt) elt))
1730 (setcdr elt
1731 (cons (list "Default" default)
1732 (cdr elt)))))
1733
1734 (defvar x-fixed-font-alist
1735 (list
1736 (purecopy "Font Menu")
1737 (cons
1738 (purecopy "Misc")
1739 (mapcar
1740 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1741 ;; For these, we specify the pixel height and width.
1742 '(("fixed" "fixed")
1743 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1744 ("6x12"
1745 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1746 ("6x13"
1747 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1748 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1749 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1750 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1751 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1752 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1753 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1754 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1755 ("")
1756 ("clean 5x8"
1757 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1758 ("clean 6x8"
1759 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1760 ("clean 8x8"
1761 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1762 ("clean 8x10"
1763 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1764 ("clean 8x14"
1765 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1766 ("clean 8x16"
1767 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1768 ("")
1769 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1770 ;; We don't seem to have these; who knows what they are.
1771 ;; ("fg-18" "fg-18")
1772 ;; ("fg-25" "fg-25")
1773 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1774 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1775 ("lucidasanstypewriter-bold-24"
1776 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
1777 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1778 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1779 )))
1780
1781 (cons
1782 (purecopy "Courier")
1783 (mapcar
1784 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1785 ;; For these, we specify the point height.
1786 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1787 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1788 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1789 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1790 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1791 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1792 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1793 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1794 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1795 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1796 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1797 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1798 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1799 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1800 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1801 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1802 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1803 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1804 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1805 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1806 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1807 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1808 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1809 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1810 ))))
1811 "X fonts suitable for use in Emacs.")
1812
1813 (declare-function generate-fontset-menu "fontset" ())
1814
1815 (defun mouse-select-font ()
1816 "Prompt for a font name, using `x-popup-menu', and return it."
1817 (interactive)
1818 (unless (display-multi-font-p)
1819 (error "Cannot change fonts on this display"))
1820 (car
1821 (x-popup-menu
1822 (if (listp last-nonmenu-event)
1823 last-nonmenu-event
1824 (list '(0 0) (selected-window)))
1825 (append x-fixed-font-alist
1826 (list (generate-fontset-menu))))))
1827
1828 (declare-function text-scale-mode "face-remap")
1829
1830 (defun mouse-set-font (&rest fonts)
1831 "Set the default font for the selected frame.
1832 The argument FONTS is a list of font names; the first valid font
1833 in this list is used.
1834
1835 When called interactively, pop up a menu and allow the user to
1836 choose a font."
1837 (interactive
1838 (progn (unless (display-multi-font-p)
1839 (error "Cannot change fonts on this display"))
1840 (x-popup-menu
1841 (if (listp last-nonmenu-event)
1842 last-nonmenu-event
1843 (list '(0 0) (selected-window)))
1844 ;; Append list of fontsets currently defined.
1845 (append x-fixed-font-alist (list (generate-fontset-menu))))))
1846 (if fonts
1847 (let (font)
1848 (while fonts
1849 (condition-case nil
1850 (progn
1851 (set-frame-font (car fonts))
1852 (setq font (car fonts))
1853 (setq fonts nil))
1854 (error
1855 (setq fonts (cdr fonts)))))
1856 (if (null font)
1857 (error "Font not found")))))
1858
1859 (defvar mouse-appearance-menu-map nil)
1860 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
1861 (declare-function buffer-face-mode-invoke "face-remap"
1862 (face arg &optional interactive))
1863 (declare-function font-face-attributes "font.c" (font &optional frame))
1864 (defvar w32-use-w32-font-dialog)
1865 (defvar w32-fixed-font-alist)
1866
1867 (defun mouse-appearance-menu (event)
1868 "Show a menu for changing the default face in the current buffer."
1869 (interactive "@e")
1870 (require 'face-remap)
1871 (when (display-multi-font-p)
1872 (with-selected-window (car (event-start event))
1873 (if mouse-appearance-menu-map
1874 nil ; regenerate new fonts
1875 ;; Initialize mouse-appearance-menu-map
1876 (setq mouse-appearance-menu-map
1877 (make-sparse-keymap "Change Default Buffer Face"))
1878 (define-key mouse-appearance-menu-map [face-remap-reset-base]
1879 '(menu-item "Reset to Default" face-remap-reset-base))
1880 (define-key mouse-appearance-menu-map [text-scale-decrease]
1881 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
1882 (define-key mouse-appearance-menu-map [text-scale-increase]
1883 '(menu-item "Increase Buffer Text Size" text-scale-increase))
1884 ;; Font selector
1885 (if (and (functionp 'x-select-font)
1886 (or (not (boundp 'w32-use-w32-font-dialog))
1887 w32-use-w32-font-dialog))
1888 (define-key mouse-appearance-menu-map [x-select-font]
1889 '(menu-item "Change Buffer Font..." x-select-font))
1890 ;; If the select-font is unavailable, construct a menu.
1891 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
1892 (font-alist (cdr (append
1893 (if (eq system-type 'windows-nt)
1894 w32-fixed-font-alist
1895 x-fixed-font-alist)
1896 (list (generate-fontset-menu))))))
1897 (dolist (family font-alist)
1898 (let* ((submenu-name (car family))
1899 (submenu-map (make-sparse-keymap submenu-name)))
1900 (dolist (font (cdr family))
1901 (let ((font-name (car font))
1902 font-symbol)
1903 (if (string= font-name "")
1904 (define-key submenu-map [space]
1905 '("--"))
1906 (setq font-symbol (intern (cadr font)))
1907 (define-key submenu-map (vector font-symbol)
1908 (list 'menu-item (car font) font-symbol)))))
1909 (define-key font-submenu (vector (intern submenu-name))
1910 (list 'menu-item submenu-name submenu-map))))
1911 (define-key mouse-appearance-menu-map [font-submenu]
1912 (list 'menu-item "Change Text Font" font-submenu)))))
1913 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
1914 (setq choice (nth (1- (length choice)) choice))
1915 (cond ((eq choice 'text-scale-increase)
1916 (text-scale-increase 1))
1917 ((eq choice 'text-scale-decrease)
1918 (text-scale-increase -1))
1919 ((eq choice 'face-remap-reset-base)
1920 (text-scale-mode 0)
1921 (buffer-face-mode 0))
1922 (choice
1923 ;; Either choice == 'x-select-font, or choice is a
1924 ;; symbol whose name is a font.
1925 (let ((font (if (eq choice 'x-select-font)
1926 (x-select-font)
1927 (symbol-name choice))))
1928 (buffer-face-mode-invoke
1929 (if (fontp font 'font-spec)
1930 (list :font font)
1931 (font-face-attributes font))
1932 t (called-interactively-p 'interactive)))))))))
1933
1934 \f
1935 ;;; Bindings for mouse commands.
1936
1937 (global-set-key [down-mouse-1] 'mouse-drag-region)
1938 (global-set-key [mouse-1] 'mouse-set-point)
1939 (global-set-key [drag-mouse-1] 'mouse-set-region)
1940
1941 (defun mouse--strip-first-event (_prompt)
1942 (substring (this-single-command-raw-keys) 1))
1943
1944 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
1945 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
1946
1947 (global-set-key [mouse-2] 'mouse-yank-primary)
1948 ;; Allow yanking also when the corresponding cursor is "in the fringe".
1949 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
1950 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
1951 (global-set-key [mouse-3] 'mouse-save-then-kill)
1952 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
1953 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
1954
1955 ;; By binding these to down-going events, we let the user use the up-going
1956 ;; event to make the selection, saving a click.
1957 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1958 (if (not (eq system-type 'ms-dos))
1959 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
1960 ;; C-down-mouse-2 is bound in facemenu.el.
1961 (global-set-key [C-down-mouse-3]
1962 `(menu-item ,(purecopy "Menu Bar") ignore
1963 :filter (lambda (_)
1964 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
1965 (mouse-menu-bar-map)
1966 (mouse-menu-major-mode-map)))))
1967
1968 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
1969 ;; vertical-line prevents Emacs from signaling an error when the mouse
1970 ;; button is released after dragging these lines, on non-toolkit
1971 ;; versions.
1972 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
1973 (global-set-key [header-line mouse-1] 'mouse-select-window)
1974 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1975 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1976 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1977 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1978 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1979 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1980 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1981 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
1982 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1983 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1984 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1985 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
1986 (global-set-key [right-divider mouse-1] 'ignore)
1987 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
1988 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
1989 (global-set-key [bottom-divider mouse-1] 'ignore)
1990 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
1991
1992 (provide 'mouse)
1993
1994 ;;; mouse.el ends here