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