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