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