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