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