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