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