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