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