; Minor fix for last change in characters.el
[emacs.git] / lisp / mouse.el
blob699225277de3a4b7b8aa5b3f9ee6a7c1ee251a3f
1 ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1995, 1999-2016 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 ((timedout
106 (sit-for (if (numberp mouse-1-click-follows-link)
107 (/ (abs mouse-1-click-follows-link) 1000.0)
108 0))))
109 (if (if (and (numberp mouse-1-click-follows-link)
110 (>= mouse-1-click-follows-link 0))
111 timedout (not timedout))
114 (let ((event (read-key))) ;Use read-key so it works for xterm-mouse-mode!
115 (if (eq (car-safe event) (if (eq mouse-1-click-follows-link 'double)
116 'double-mouse-1 'mouse-1))
117 ;; Turn the mouse-1 into a mouse-2 to follow links.
118 (let ((newup (if (eq mouse-1-click-follows-link 'double)
119 'double-mouse-2 'mouse-2)))
120 ;; If mouse-2 has never been done by the user, it doesn't have
121 ;; the necessary property to be interpreted correctly.
122 (unless (get newup 'event-kind)
123 (put newup 'event-kind (get (car event) 'event-kind)))
124 (push (cons newup (cdr event)) unread-command-events)
125 ;; Don't change the down event, only the up-event (bug#18212).
126 nil)
127 (push event unread-command-events)
128 nil))))))
130 (define-key key-translation-map [down-mouse-1]
131 #'mouse--down-1-maybe-follows-link)
132 (define-key key-translation-map [double-down-mouse-1]
133 #'mouse--down-1-maybe-follows-link)
136 ;; Provide a mode-specific menu on a mouse button.
138 (defun minor-mode-menu-from-indicator (indicator)
139 "Show menu for minor mode specified by INDICATOR.
140 Interactively, INDICATOR is read using completion.
141 If there is no menu defined for the minor mode, then create one with
142 items `Turn Off' and `Help'."
143 (interactive
144 (list (completing-read
145 "Minor mode indicator: "
146 (describe-minor-mode-completion-table-for-indicator))))
147 (let* ((minor-mode (lookup-minor-mode-from-indicator indicator))
148 (mm-fun (or (get minor-mode :minor-mode-function) minor-mode)))
149 (unless minor-mode (error "Cannot find minor mode for `%s'" indicator))
150 (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist)))
151 (menu (and (keymapp map) (lookup-key map [menu-bar]))))
152 (setq menu
153 (if menu
154 (mouse-menu-non-singleton menu)
155 (if (fboundp mm-fun) ; bug#20201
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 (if menu
163 (popup-menu menu)
164 (message "No menu available")))))
166 (defun mouse-minor-mode-menu (event)
167 "Show minor-mode menu for EVENT on minor modes area of the mode line."
168 (interactive "@e")
169 (let ((indicator (car (nth 4 (car (cdr event))))))
170 (minor-mode-menu-from-indicator indicator)))
172 (defun mouse-menu-major-mode-map ()
173 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
174 (let* (;; Keymap from which to inherit; may be null.
175 (ancestor (mouse-menu-non-singleton
176 (and (current-local-map)
177 (local-key-binding [menu-bar]))))
178 ;; Make a keymap in which our last command leads to a menu or
179 ;; default to the edit menu.
180 (newmap (if ancestor
181 (make-sparse-keymap (concat (format-mode-line mode-name)
182 " Mode"))
183 menu-bar-edit-menu)))
184 (if ancestor
185 (set-keymap-parent newmap ancestor))
186 newmap))
188 (defun mouse-menu-non-singleton (menubar)
189 "Return menu keybar MENUBAR, or a lone submenu inside it.
190 If MENUBAR defines exactly one submenu, return just that submenu.
191 Otherwise, return MENUBAR."
192 (if menubar
193 (let (submap)
194 (map-keymap
195 (lambda (k v) (setq submap (if submap t (cons k v))))
196 (keymap-canonicalize menubar))
197 (if (eq submap t)
198 menubar
199 (lookup-key menubar (vector (car submap)))))))
201 (defun mouse-menu-bar-map ()
202 "Return a keymap equivalent to the menu bar.
203 The contents are the items that would be in the menu bar whether or
204 not it is actually displayed."
205 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
206 (let* ((local-menu (and (current-local-map)
207 (lookup-key (current-local-map) [menu-bar])))
208 (global-menu (lookup-key global-map [menu-bar]))
209 ;; If a keymap doesn't have a prompt string (a lazy
210 ;; programmer didn't bother to provide one), create it and
211 ;; insert it into the keymap; each keymap gets its own
212 ;; prompt. This is required for non-toolkit versions to
213 ;; display non-empty menu pane names.
214 (minor-mode-menus
215 (mapcar
216 (lambda (menu)
217 (let* ((minor-mode (car menu))
218 (menu (cdr menu))
219 (title-or-map (cadr menu)))
220 (or (stringp title-or-map)
221 (setq menu
222 (cons 'keymap
223 (cons (concat
224 (capitalize (subst-char-in-string
225 ?- ?\s (symbol-name
226 minor-mode)))
227 " Menu")
228 (cdr menu)))))
229 menu))
230 (minor-mode-key-binding [menu-bar])))
231 (local-title-or-map (and local-menu (cadr local-menu)))
232 (global-title-or-map (cadr global-menu)))
233 (or (null local-menu)
234 (stringp local-title-or-map)
235 (setq local-menu (cons 'keymap
236 (cons (concat (format-mode-line mode-name)
237 " Mode Menu")
238 (cdr local-menu)))))
239 (or (stringp global-title-or-map)
240 (setq global-menu (cons 'keymap
241 (cons "Global Menu"
242 (cdr global-menu)))))
243 ;; Supplying the list is faster than making a new map.
244 ;; FIXME: We have a problem here: we have to use the global/local/minor
245 ;; so they're displayed in the expected order, but later on in the command
246 ;; loop, they're actually looked up in the opposite order.
247 (apply 'append
248 global-menu
249 local-menu
250 minor-mode-menus)))
252 (defun mouse-major-mode-menu (event &optional prefix)
253 "Pop up a mode-specific menu of mouse commands.
254 Default to the Edit menu if the major mode doesn't define a menu."
255 (declare (obsolete mouse-menu-major-mode-map "23.1"))
256 (interactive "@e\nP")
257 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
258 (popup-menu (mouse-menu-major-mode-map) event prefix))
260 (defun mouse-popup-menubar (event prefix)
261 "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
262 The contents are the items that would be in the menu bar whether or
263 not it is actually displayed."
264 (declare (obsolete mouse-menu-bar-map "23.1"))
265 (interactive "@e \nP")
266 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
267 (popup-menu (mouse-menu-bar-map) (unless (integerp event) event) prefix))
269 (defun mouse-popup-menubar-stuff (event prefix)
270 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
271 Use the former if the menu bar is showing, otherwise the latter."
272 (declare (obsolete nil "23.1"))
273 (interactive "@e\nP")
274 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
275 (popup-menu
276 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
277 (mouse-menu-bar-map)
278 (mouse-menu-major-mode-map))
279 event prefix))
281 ;; Commands that operate on windows.
283 (defun mouse-minibuffer-check (event)
284 (let ((w (posn-window (event-start event))))
285 (and (window-minibuffer-p w)
286 (not (minibuffer-window-active-p w))
287 (user-error "Minibuffer window is not active")))
288 ;; Give temporary modes such as isearch a chance to turn off.
289 (run-hooks 'mouse-leave-buffer-hook))
291 (defun mouse-delete-window (click)
292 "Delete the window you click on.
293 Do nothing if the frame has just one window.
294 This command must be bound to a mouse click."
295 (interactive "e")
296 (unless (one-window-p t)
297 (mouse-minibuffer-check click)
298 (delete-window (posn-window (event-start click)))))
300 (defun mouse-select-window (click)
301 "Select the window clicked on; don't move point."
302 (interactive "e")
303 (mouse-minibuffer-check click)
304 (let ((oframe (selected-frame))
305 (frame (window-frame (posn-window (event-start click)))))
306 (select-window (posn-window (event-start click)))
307 (raise-frame frame)
308 (select-frame frame)
309 (or (eq frame oframe)
310 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
312 (define-obsolete-function-alias 'mouse-tear-off-window 'tear-off-window "24.4")
313 (defun tear-off-window (click)
314 "Delete the selected window, and create a new frame displaying its buffer."
315 (interactive "e")
316 (mouse-minibuffer-check click)
317 (let* ((window (posn-window (event-start click)))
318 (buf (window-buffer window))
319 (frame (make-frame))) ;FIXME: Use pop-to-buffer.
320 (select-frame frame)
321 (switch-to-buffer buf)
322 (delete-window window)))
324 (defun mouse-delete-other-windows ()
325 "Delete all windows except the one you click on."
326 (interactive "@")
327 (delete-other-windows))
329 (defun mouse-split-window-vertically (click)
330 "Select Emacs window mouse is on, then split it vertically in half.
331 The window is split at the line clicked on.
332 This command must be bound to a mouse click."
333 (interactive "@e")
334 (mouse-minibuffer-check click)
335 (let ((start (event-start click)))
336 (select-window (posn-window start))
337 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
338 (first-line window-min-height)
339 (last-line (- (window-height) window-min-height)))
340 (if (< last-line first-line)
341 (user-error "Window too short to split")
342 ;; Bind `window-combination-resize' to nil so we are sure to get
343 ;; the split right at the line clicked on.
344 (let (window-combination-resize)
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 (user-error "Window too narrow to split")
361 ;; Bind `window-combination-resize' to nil so we are sure to get
362 ;; the split right at the column clicked on.
363 (let (window-combination-resize)
364 (split-window-horizontally
365 (min (max new-width first-col) last-col)))))))
367 (defun mouse-drag-line (start-event line)
368 "Drag a mode line, header line, or vertical line with the mouse.
369 START-EVENT is the starting mouse-event of the drag action. LINE
370 must be one of the symbols `header', `mode', or `vertical'."
371 ;; Give temporary modes such as isearch a chance to turn off.
372 (run-hooks 'mouse-leave-buffer-hook)
373 (let* ((echo-keystrokes 0)
374 (start (event-start start-event))
375 (window (posn-window start))
376 (frame (window-frame window))
377 ;; `position' records the x- or y-coordinate of the last
378 ;; sampled position.
379 (position (if (eq line 'vertical)
380 (+ (window-pixel-left window)
381 (car (posn-x-y start)))
382 (+ (window-pixel-top window)
383 (cdr (posn-x-y start)))))
384 ;; `last-position' records the x- or y-coordinate of the
385 ;; previously sampled position. The difference of `position'
386 ;; and `last-position' determines the size change of WINDOW.
387 (last-position position)
388 (draggable t)
389 posn-window growth dragged)
390 ;; Decide on whether we are allowed to track at all and whose
391 ;; window's edge we drag.
392 (cond
393 ((eq line 'header)
394 (if (window-at-side-p window 'top)
395 ;; We can't drag the header line of a topmost window.
396 (setq draggable nil)
397 ;; Drag bottom edge of window above the header line.
398 (setq window (window-in-direction 'above window t))))
399 ((eq line 'mode)
400 (if (and (window-at-side-p window 'bottom)
401 ;; Allow resizing the minibuffer window if it's on the
402 ;; same frame as and immediately below `window', and it's
403 ;; either active or `resize-mini-windows' is nil.
404 (let ((minibuffer-window (minibuffer-window frame)))
405 (not (and (eq (window-frame minibuffer-window) frame)
406 (or (not resize-mini-windows)
407 (eq minibuffer-window
408 (active-minibuffer-window)))))))
409 (setq draggable nil)))
410 ((eq line 'vertical)
411 (let ((divider-width (frame-right-divider-width frame)))
412 (when (and (or (not (numberp divider-width))
413 (zerop divider-width))
414 (eq (cdr (assq 'vertical-scroll-bars
415 (frame-parameters frame)))
416 'left))
417 (setq window (window-in-direction 'left window t))))))
419 (let* ((exitfun nil)
420 (move
421 (lambda (event) (interactive "e")
422 (cond
423 ((not (consp event))
424 nil)
425 ((eq line 'vertical)
426 ;; Drag right edge of `window'.
427 (setq start (event-start event))
428 (setq position (car (posn-x-y start)))
429 ;; Set `posn-window' to the window where `event' was recorded.
430 ;; This can be `window' or the window on the left or right of
431 ;; `window'.
432 (when (window-live-p (setq posn-window (posn-window start)))
433 ;; Add left edge of `posn-window' to `position'.
434 (setq position (+ (window-pixel-left posn-window) position))
435 (unless (nth 1 start)
436 ;; Add width of objects on the left of the text area to
437 ;; `position'.
438 (when (eq (window-current-scroll-bars posn-window) 'left)
439 (setq position (+ (window-scroll-bar-width posn-window)
440 position)))
441 (setq position (+ (car (window-fringes posn-window))
442 (or (car (window-margins posn-window)) 0)
443 position))))
444 ;; When the cursor overshoots after shrinking a window to its
445 ;; minimum size and the dragging direction changes, have the
446 ;; cursor first catch up with the window edge.
447 (unless (or (zerop (setq growth (- position last-position)))
448 (and (> growth 0)
449 (< position (+ (window-pixel-left window)
450 (window-pixel-width window))))
451 (and (< growth 0)
452 (> position (+ (window-pixel-left window)
453 (window-pixel-width window)))))
454 (setq dragged t)
455 (adjust-window-trailing-edge window growth t t))
456 (setq last-position position))
457 (draggable
458 ;; Drag bottom edge of `window'.
459 (setq start (event-start event))
460 ;; Set `posn-window' to the window where `event' was recorded.
461 ;; This can be either `window' or the window above or below of
462 ;; `window'.
463 (setq posn-window (posn-window start))
464 (setq position (cdr (posn-x-y start)))
465 (when (window-live-p posn-window)
466 ;; Add top edge of `posn-window' to `position'.
467 (setq position (+ (window-pixel-top posn-window) position))
468 ;; If necessary, add height of header line to `position'
469 (when (memq (posn-area start)
470 '(nil left-fringe right-fringe left-margin right-margin))
471 (setq position (+ (window-header-line-height posn-window) position))))
472 ;; When the cursor overshoots after shrinking a window to its
473 ;; minimum size and the dragging direction changes, have the
474 ;; cursor first catch up with the window edge.
475 (unless (or (zerop (setq growth (- position last-position)))
476 (and (> growth 0)
477 (< position (+ (window-pixel-top window)
478 (window-pixel-height window))))
479 (and (< growth 0)
480 (> position (+ (window-pixel-top window)
481 (window-pixel-height window)))))
482 (setq dragged t)
483 (adjust-window-trailing-edge window growth nil t))
484 (setq last-position position)))))
485 (old-track-mouse track-mouse))
486 ;; Start tracking. The special value 'dragging' signals the
487 ;; display engine to freeze the mouse pointer shape for as long
488 ;; as we drag.
489 (setq track-mouse 'dragging)
490 ;; Loop reading events and sampling the position of the mouse.
491 (setq exitfun
492 (set-transient-map
493 (let ((map (make-sparse-keymap)))
494 (define-key map [switch-frame] #'ignore)
495 (define-key map [select-window] #'ignore)
496 (define-key map [scroll-bar-movement] #'ignore)
497 (define-key map [mouse-movement] move)
498 ;; Swallow drag-mouse-1 events to avoid selecting some other window.
499 (define-key map [drag-mouse-1]
500 (lambda () (interactive) (funcall exitfun)))
501 ;; For vertical line dragging swallow also a mouse-1
502 ;; event (but only if we dragged at least once to allow mouse-1
503 ;; clicks to get through).
504 (when (eq line 'vertical)
505 (define-key map [mouse-1]
506 `(menu-item "" ,(lambda () (interactive) (funcall exitfun))
507 :filter ,(lambda (cmd) (if dragged cmd)))))
508 ;; Some of the events will of course end up looked up
509 ;; with a mode-line, header-line or vertical-line prefix ...
510 (define-key map [mode-line] map)
511 (define-key map [header-line] map)
512 (define-key map [vertical-line] map)
513 ;; ... and some maybe even with a right- or bottom-divider
514 ;; prefix.
515 (define-key map [right-divider] map)
516 (define-key map [bottom-divider] map)
517 map)
518 t (lambda () (setq track-mouse old-track-mouse)))))))
520 (defun mouse-drag-mode-line (start-event)
521 "Change the height of a window by dragging on the mode line."
522 (interactive "e")
523 (mouse-drag-line start-event 'mode))
525 (defun mouse-drag-header-line (start-event)
526 "Change the height of a window by dragging on the header line."
527 (interactive "e")
528 (mouse-drag-line start-event 'header))
530 (defun mouse-drag-vertical-line (start-event)
531 "Change the width of a window by dragging on the vertical line."
532 (interactive "e")
533 (mouse-drag-line start-event 'vertical))
535 (defun mouse-set-point (event &optional promote-to-region)
536 "Move point to the position clicked on with the mouse.
537 This should be bound to a mouse click event type.
538 If PROMOTE-TO-REGION is non-nil and event is a multiple-click,
539 select the corresponding element around point."
540 (interactive "e\np")
541 (mouse-minibuffer-check event)
542 (if (and promote-to-region (> (event-click-count event) 1))
543 (mouse-set-region event)
544 ;; Use event-end in case called from mouse-drag-region.
545 ;; If EVENT is a click, event-end and event-start give same value.
546 (posn-set-point (event-end event))))
548 (defvar mouse-last-region-beg nil)
549 (defvar mouse-last-region-end nil)
550 (defvar mouse-last-region-tick nil)
552 (defun mouse-region-match ()
553 "Return non-nil if there's an active region that was set with the mouse."
554 (and (mark t) mark-active
555 (eq mouse-last-region-beg (region-beginning))
556 (eq mouse-last-region-end (region-end))
557 (eq mouse-last-region-tick (buffer-modified-tick))))
559 (defvar mouse--drag-start-event nil)
561 (defun mouse-set-region (click)
562 "Set the region to the text dragged over, and copy to kill ring.
563 This should be bound to a mouse drag event.
564 See the `mouse-drag-copy-region' variable to control whether this
565 command alters the kill ring or not."
566 (interactive "e")
567 (mouse-minibuffer-check click)
568 (select-window (posn-window (event-start click)))
569 (let ((beg (posn-point (event-start click)))
570 (end
571 (if (eq (posn-window (event-end click)) (selected-window))
572 (posn-point (event-end click))
573 ;; If the mouse ends up in any other window or on the menu
574 ;; bar, use `window-point' of selected window (Bug#23707).
575 (window-point)))
576 (click-count (event-click-count click)))
577 (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
578 (when drag-start
579 ;; Drag events don't come with a click count, sadly, so we hack
580 ;; our way around this problem by remembering the start-event in
581 ;; `mouse-drag-start' and fetching the click-count from there.
582 (when (and (<= click-count 1)
583 (equal beg (posn-point (event-start drag-start))))
584 (setq click-count (event-click-count drag-start)))
585 ;; Occasionally we get spurious drag events where the user hasn't
586 ;; dragged his mouse, but instead Emacs has dragged the text under the
587 ;; user's mouse. Try to recover those cases (bug#17562).
588 (when (and (equal (posn-x-y (event-start click))
589 (posn-x-y (event-end click)))
590 (not (eq (car drag-start) 'mouse-movement)))
591 (setq end beg))
592 (setf (terminal-parameter nil 'mouse-drag-start) nil)))
593 (when (and (integerp beg) (integerp end))
594 (let ((range (mouse-start-end beg end (1- click-count))))
595 (if (< end beg)
596 (setq end (nth 0 range) beg (nth 1 range))
597 (setq beg (nth 0 range) end (nth 1 range)))))
598 (and mouse-drag-copy-region (integerp beg) (integerp end)
599 ;; Don't set this-command to `kill-region', so a following
600 ;; C-w won't double the text in the kill ring. Ignore
601 ;; `last-command' so we don't append to a preceding kill.
602 (let (this-command last-command deactivate-mark)
603 (copy-region-as-kill beg end)))
604 (if (numberp beg) (goto-char beg))
605 ;; On a text terminal, bounce the cursor.
606 (or transient-mark-mode
607 (window-system)
608 (sit-for 1))
609 (push-mark)
610 (set-mark (point))
611 (if (numberp end) (goto-char end))
612 (mouse-set-region-1)))
614 (defun mouse-set-region-1 ()
615 ;; Set transient-mark-mode for a little while.
616 (unless (eq (car-safe transient-mark-mode) 'only)
617 (setq-local transient-mark-mode
618 (cons 'only
619 (unless (eq transient-mark-mode 'lambda)
620 transient-mark-mode))))
621 (setq mouse-last-region-beg (region-beginning))
622 (setq mouse-last-region-end (region-end))
623 (setq mouse-last-region-tick (buffer-modified-tick)))
625 (defcustom mouse-scroll-delay 0.25
626 "The pause between scroll steps caused by mouse drags, in seconds.
627 If you drag the mouse beyond the edge of a window, Emacs scrolls the
628 window to bring the text beyond that edge into view, with a delay of
629 this many seconds between scroll steps. Scrolling stops when you move
630 the mouse back into the window, or release the button.
631 This variable's value may be non-integral.
632 Setting this to zero causes Emacs to scroll as fast as it can."
633 :type 'number
634 :group 'mouse)
636 (defcustom mouse-scroll-min-lines 1
637 "The minimum number of lines scrolled by dragging mouse out of window.
638 Moving the mouse out the top or bottom edge of the window begins
639 scrolling repeatedly. The number of lines scrolled per repetition
640 is normally equal to the number of lines beyond the window edge that
641 the mouse has moved. However, it always scrolls at least the number
642 of lines specified by this variable."
643 :type 'integer
644 :group 'mouse)
646 (defun mouse-scroll-subr (window jump &optional overlay start)
647 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
648 If OVERLAY is an overlay, let it stretch from START to the far edge of
649 the newly visible text.
650 Upon exit, point is at the far edge of the newly visible text."
651 (cond
652 ((and (> jump 0) (< jump mouse-scroll-min-lines))
653 (setq jump mouse-scroll-min-lines))
654 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
655 (setq jump (- mouse-scroll-min-lines))))
656 (let ((opoint (point)))
657 (while (progn
658 (goto-char (window-start window))
659 (if (not (zerop (vertical-motion jump window)))
660 (progn
661 (set-window-start window (point))
662 (if (natnump jump)
663 (if (window-end window)
664 (progn
665 (goto-char (window-end window))
666 ;; window-end doesn't reflect the window's new
667 ;; start position until the next redisplay.
668 (vertical-motion (1- jump) window))
669 (vertical-motion (- (window-height window) 2)))
670 (goto-char (window-start window)))
671 (if overlay
672 (move-overlay overlay start (point)))
673 ;; Now that we have scrolled WINDOW properly,
674 ;; put point back where it was for the redisplay
675 ;; so that we don't mess up the selected window.
676 (or (eq window (selected-window))
677 (goto-char opoint))
678 (sit-for mouse-scroll-delay)))))
679 (or (eq window (selected-window))
680 (goto-char opoint))))
682 (defvar mouse-selection-click-count 0)
684 (defvar mouse-selection-click-count-buffer nil)
686 (defun mouse-drag-region (start-event)
687 "Set the region to the text that the mouse is dragged over.
688 Highlight the drag area as you move the mouse.
689 This must be bound to a button-down mouse event.
690 In Transient Mark mode, the highlighting remains as long as the mark
691 remains active. Otherwise, it remains until the next input event."
692 (interactive "e")
693 ;; Give temporary modes such as isearch a chance to turn off.
694 (run-hooks 'mouse-leave-buffer-hook)
695 (mouse-drag-track start-event))
698 (defun mouse-posn-property (pos property)
699 "Look for a property at click position.
700 POS may be either a buffer position or a click position like
701 those returned from `event-start'. If the click position is on
702 a string, the text property PROPERTY is examined.
703 If this is nil or the click is not on a string, then
704 the corresponding buffer position is searched for PROPERTY.
705 If PROPERTY is encountered in one of those places,
706 its value is returned."
707 (if (consp pos)
708 (let ((w (posn-window pos)) (pt (posn-point pos))
709 (str (posn-string pos)))
710 (or (and str
711 (get-text-property (cdr str) property (car str)))
712 ;; Mouse clicks in the fringe come with a position in
713 ;; (nth 5). This is useful but is not exactly where we clicked, so
714 ;; don't look up that position's properties!
715 (and pt (not (memq (posn-area pos) '(left-fringe right-fringe
716 left-margin right-margin)))
717 (get-char-property pt property w))))
718 (get-char-property pos property)))
720 (defun mouse-on-link-p (pos)
721 "Return non-nil if POS is on a link in the current buffer.
722 POS must specify a buffer position in the current buffer, as a list
723 of the form returned by the `event-start' and `event-end' functions,
724 or a mouse event location in the selected window (see `event-start').
725 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
726 POS may be a mouse event location in any window.
728 A clickable link is identified by one of the following methods:
730 - If the character at POS has a non-nil `follow-link' text or
731 overlay property, the value of that property determines what to do.
733 - If there is a local key-binding or a keybinding at position POS
734 for the `follow-link' event, the binding of that event determines
735 what to do.
737 The resulting value determine whether POS is inside a link:
739 - If the value is `mouse-face', POS is inside a link if there
740 is a non-nil `mouse-face' property at POS. Return t in this case.
742 - If the value is a function, FUNC, POS is inside a link if
743 the call \(FUNC POS) returns non-nil. Return the return value
744 from that call. Arg is \(posn-point POS) if POS is a mouse event.
746 - Otherwise, return the value itself.
748 The return value is interpreted as follows:
750 - If it is a string, the mouse-1 event is translated into the
751 first character of the string, i.e. the action of the mouse-1
752 click is the local or global binding of that character.
754 - If it is a vector, the mouse-1 event is translated into the
755 first element of that vector, i.e. the action of the mouse-1
756 click is the local or global binding of that event.
758 - Otherwise, the mouse-1 event is translated into a mouse-2 event
759 at the same position."
760 (let ((action
761 (and (or (not (consp pos))
762 mouse-1-click-in-non-selected-windows
763 (eq (selected-window) (posn-window pos)))
764 (or (mouse-posn-property pos 'follow-link)
765 (let ((area (posn-area pos)))
766 (when area
767 (key-binding (vector area 'follow-link) nil t pos)))
768 (key-binding [follow-link] nil t pos)))))
769 (cond
770 ((eq action 'mouse-face)
771 (and (mouse-posn-property pos 'mouse-face) t))
772 ((functionp action)
773 ;; FIXME: This seems questionable if the click is not in a buffer.
774 ;; Should we instead decide that `action' takes a `posn'?
775 (if (consp pos)
776 (with-current-buffer (window-buffer (posn-window pos))
777 (funcall action (posn-point pos)))
778 (funcall action pos)))
779 (t action))))
781 (defun mouse-fixup-help-message (msg)
782 "Fix help message MSG for `mouse-1-click-follows-link'."
783 (let (mp pos)
784 (if (and mouse-1-click-follows-link
785 (stringp msg)
786 (string-match-p "\\`mouse-2" msg)
787 (setq mp (mouse-pixel-position))
788 (consp (setq pos (cdr mp)))
789 (car pos) (>= (car pos) 0)
790 (cdr pos) (>= (cdr pos) 0)
791 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
792 (windowp (posn-window pos)))
793 (with-current-buffer (window-buffer (posn-window pos))
794 (if (mouse-on-link-p pos)
795 (setq msg (concat
796 (cond
797 ((eq mouse-1-click-follows-link 'double) "double-")
798 ((and (integerp mouse-1-click-follows-link)
799 (< mouse-1-click-follows-link 0)) "Long ")
800 (t ""))
801 "mouse-1" (substring msg 7)))))))
802 msg)
804 (defun mouse-drag-track (start-event)
805 "Track mouse drags by highlighting area between point and cursor.
806 The region will be defined with mark and point."
807 (mouse-minibuffer-check start-event)
808 (setq mouse-selection-click-count-buffer (current-buffer))
809 (deactivate-mark)
810 (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
811 (start-posn (event-start start-event))
812 (start-point (posn-point start-posn))
813 (start-window (posn-window start-posn))
814 (_ (with-current-buffer (window-buffer start-window)
815 (setq deactivate-mark nil)))
816 ;; We've recorded what we needed from the current buffer and
817 ;; window, now let's jump to the place of the event, where things
818 ;; are happening.
819 (_ (mouse-set-point start-event))
820 (echo-keystrokes 0)
821 (bounds (window-edges start-window))
822 (make-cursor-line-fully-visible nil)
823 (top (nth 1 bounds))
824 (bottom (if (window-minibuffer-p start-window)
825 (nth 3 bounds)
826 ;; Don't count the mode line.
827 (1- (nth 3 bounds))))
828 (click-count (1- (event-click-count start-event)))
829 ;; Suppress automatic hscrolling, because that is a nuisance
830 ;; when setting point near the right fringe (but see below).
831 (auto-hscroll-mode-saved auto-hscroll-mode)
832 (old-track-mouse track-mouse))
834 (setq mouse-selection-click-count click-count)
835 ;; In case the down click is in the middle of some intangible text,
836 ;; use the end of that text, and put it in START-POINT.
837 (if (< (point) start-point)
838 (goto-char start-point))
839 (setq start-point (point))
841 ;; Activate the region, using `mouse-start-end' to determine where
842 ;; to put point and mark (e.g., double-click will select a word).
843 (setq-local transient-mark-mode
844 (if (eq transient-mark-mode 'lambda)
845 '(only)
846 (cons 'only transient-mark-mode)))
847 (let ((range (mouse-start-end start-point start-point click-count)))
848 (push-mark (nth 0 range) t t)
849 (goto-char (nth 1 range)))
851 (setf (terminal-parameter nil 'mouse-drag-start) start-event)
852 (setq track-mouse t)
853 (setq auto-hscroll-mode nil)
855 (set-transient-map
856 (let ((map (make-sparse-keymap)))
857 (define-key map [switch-frame] #'ignore)
858 (define-key map [select-window] #'ignore)
859 (define-key map [mouse-movement]
860 (lambda (event) (interactive "e")
861 (let* ((end (event-end event))
862 (end-point (posn-point end)))
863 (unless (eq end-point start-point)
864 ;; As soon as the user moves, we can re-enable auto-hscroll.
865 (setq auto-hscroll-mode auto-hscroll-mode-saved)
866 ;; And remember that we have moved, so mouse-set-region can know
867 ;; its event is really a drag event.
868 (setcar start-event 'mouse-movement))
869 (if (and (eq (posn-window end) start-window)
870 (integer-or-marker-p end-point))
871 (mouse--drag-set-mark-and-point start-point
872 end-point click-count)
873 (let ((mouse-row (cdr (cdr (mouse-position)))))
874 (cond
875 ((null mouse-row))
876 ((< mouse-row top)
877 (mouse-scroll-subr start-window (- mouse-row top)
878 nil start-point))
879 ((>= mouse-row bottom)
880 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
881 nil start-point))))))))
882 map)
883 t (lambda ()
884 (setq track-mouse old-track-mouse)
885 (setq auto-hscroll-mode auto-hscroll-mode-saved)
886 (deactivate-mark)
887 (pop-mark)))))
889 (defun mouse--drag-set-mark-and-point (start click click-count)
890 (let* ((range (mouse-start-end start click click-count))
891 (beg (nth 0 range))
892 (end (nth 1 range)))
893 (cond ((eq (mark) beg)
894 (goto-char end))
895 ((eq (mark) end)
896 (goto-char beg))
897 ((< click (mark))
898 (set-mark end)
899 (goto-char beg))
901 (set-mark beg)
902 (goto-char end)))))
904 ;; Commands to handle xterm-style multiple clicks.
905 (defun mouse-skip-word (dir)
906 "Skip over word, over whitespace, or over identical punctuation.
907 If DIR is positive skip forward; if negative, skip backward."
908 (let* ((char (following-char))
909 (syntax (char-to-string (char-syntax char))))
910 (cond ((string= syntax "w")
911 ;; Here, we can't use skip-syntax-forward/backward because
912 ;; they don't pay attention to word-separating-categories,
913 ;; and thus they will skip over a true word boundary. So,
914 ;; we simulate the original behavior by using forward-word.
915 (if (< dir 0)
916 (if (not (looking-at "\\<"))
917 (forward-word -1))
918 (if (or (looking-at "\\<") (not (looking-at "\\>")))
919 (forward-word 1))))
920 ((string= syntax " ")
921 (if (< dir 0)
922 (skip-syntax-backward syntax)
923 (skip-syntax-forward syntax)))
924 ((string= syntax "_")
925 (if (< dir 0)
926 (skip-syntax-backward "w_")
927 (skip-syntax-forward "w_")))
928 ((< dir 0)
929 (while (and (not (bobp)) (= (preceding-char) char))
930 (forward-char -1)))
932 (while (and (not (eobp)) (= (following-char) char))
933 (forward-char 1))))))
935 (defun mouse-start-end (start end mode)
936 "Return a list of region bounds based on START and END according to MODE.
937 If MODE is 0 then set point to (min START END), mark to (max START END).
938 If MODE is 1 then set point to start of word at (min START END),
939 mark to end of word at (max START END).
940 If MODE is 2 then do the same for lines."
941 (if (> start end)
942 (let ((temp start))
943 (setq start end
944 end temp)))
945 (setq mode (mod mode 3))
946 (cond ((= mode 0)
947 (list start end))
948 ((and (= mode 1)
949 (= start end)
950 (char-after start)
951 (= (char-syntax (char-after start)) ?\())
952 (if (/= (syntax-class (syntax-after start)) 4) ; raw syntax code for ?\(
953 ;; This happens in CC Mode when unbalanced parens in CPP
954 ;; constructs are given punctuation syntax with
955 ;; syntax-table text properties. (2016-02-21).
956 (signal 'scan-error (list "Containing expression ends prematurely"
957 start start))
958 (list start
959 (save-excursion
960 (goto-char start)
961 (forward-sexp 1)
962 (point)))))
963 ((and (= mode 1)
964 (= start end)
965 (char-after start)
966 (= (char-syntax (char-after start)) ?\)))
967 (if (/= (syntax-class (syntax-after start)) 5) ; raw syntax code for ?\)
968 ;; See above comment about CC Mode.
969 (signal 'scan-error (list "Unbalanced parentheses" start start))
970 (list (save-excursion
971 (goto-char (1+ start))
972 (backward-sexp 1)
973 (point))
974 (1+ start))))
975 ((and (= mode 1)
976 (= start end)
977 (char-after start)
978 (= (char-syntax (char-after start)) ?\"))
979 (let ((open (or (eq start (point-min))
980 (save-excursion
981 (goto-char (- start 1))
982 (looking-at "\\s(\\|\\s \\|\\s>")))))
983 (if open
984 (list start
985 (save-excursion
986 (condition-case nil
987 (progn
988 (goto-char start)
989 (forward-sexp 1)
990 (point))
991 (error end))))
992 (list (save-excursion
993 (condition-case nil
994 (progn
995 (goto-char (1+ start))
996 (backward-sexp 1)
997 (point))
998 (error end)))
999 (1+ start)))))
1000 ((= mode 1)
1001 (list (save-excursion
1002 (goto-char start)
1003 (mouse-skip-word -1)
1004 (point))
1005 (save-excursion
1006 (goto-char end)
1007 (mouse-skip-word 1)
1008 (point))))
1009 ((= mode 2)
1010 (list (save-excursion
1011 (goto-char start)
1012 (line-beginning-position 1))
1013 (save-excursion
1014 (goto-char end)
1015 (forward-line 1)
1016 (point))))))
1018 ;; Subroutine: set the mark where CLICK happened,
1019 ;; but don't do anything else.
1020 (defun mouse-set-mark-fast (click)
1021 (mouse-minibuffer-check click)
1022 (let ((posn (event-start click)))
1023 (select-window (posn-window posn))
1024 (if (numberp (posn-point posn))
1025 (push-mark (posn-point posn) t t))))
1027 (defun mouse-undouble-last-event (events)
1028 (let* ((index (1- (length events)))
1029 (last (nthcdr index events))
1030 (event (car last))
1031 (basic (event-basic-type event))
1032 (old-modifiers (event-modifiers event))
1033 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1034 (new
1035 (if (consp event)
1036 ;; Use reverse, not nreverse, since event-modifiers
1037 ;; does not copy the list it returns.
1038 (cons (event-convert-list (reverse (cons basic modifiers)))
1039 (cdr event))
1040 event)))
1041 (setcar last new)
1042 (if (and (not (equal modifiers old-modifiers))
1043 (key-binding (apply 'vector events)))
1045 (setcar last event)
1046 nil)))
1048 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1050 (defun mouse-set-mark (click)
1051 "Set mark at the position clicked on with the mouse.
1052 Display cursor at that position for a second.
1053 This must be bound to a mouse click."
1054 (interactive "e")
1055 (mouse-minibuffer-check click)
1056 (select-window (posn-window (event-start click)))
1057 ;; FIXME: Use save-excursion
1058 (let ((point-save (point)))
1059 (unwind-protect
1060 (progn (mouse-set-point click)
1061 (push-mark nil t t)
1062 (or transient-mark-mode
1063 (sit-for 1)))
1064 (goto-char point-save))))
1066 (defun mouse-kill (click)
1067 "Kill the region between point and the mouse click.
1068 The text is saved in the kill ring, as with \\[kill-region]."
1069 (interactive "e")
1070 (mouse-minibuffer-check click)
1071 (let* ((posn (event-start click))
1072 (click-posn (posn-point posn)))
1073 (select-window (posn-window posn))
1074 (if (numberp click-posn)
1075 (kill-region (min (point) click-posn)
1076 (max (point) click-posn)))))
1078 (defun mouse-yank-at-click (click arg)
1079 "Insert the last stretch of killed text at the position clicked on.
1080 Also move point to one end of the text thus inserted (normally the end),
1081 and set mark at the beginning.
1082 Prefix arguments are interpreted as with \\[yank].
1083 If `mouse-yank-at-point' is non-nil, insert at point
1084 regardless of where you click."
1085 (interactive "e\nP")
1086 ;; Give temporary modes such as isearch a chance to turn off.
1087 (run-hooks 'mouse-leave-buffer-hook)
1088 (when select-active-regions
1089 ;; Without this, confusing things happen upon e.g. inserting into
1090 ;; the middle of an active region.
1091 (deactivate-mark))
1092 (or mouse-yank-at-point (mouse-set-point click))
1093 (setq this-command 'yank)
1094 (setq mouse-selection-click-count 0)
1095 (yank arg))
1097 (defun mouse-yank-primary (click)
1098 "Insert the primary selection at the position clicked on.
1099 Move point to the end of the inserted text, and set mark at
1100 beginning. If `mouse-yank-at-point' is non-nil, insert at point
1101 regardless of where you click."
1102 (interactive "e")
1103 ;; Give temporary modes such as isearch a chance to turn off.
1104 (run-hooks 'mouse-leave-buffer-hook)
1105 ;; Without this, confusing things happen upon e.g. inserting into
1106 ;; the middle of an active region.
1107 (when select-active-regions
1108 (let (select-active-regions)
1109 (deactivate-mark)))
1110 (or mouse-yank-at-point (mouse-set-point click))
1111 (let ((primary (gui-get-primary-selection)))
1112 (push-mark (point))
1113 (insert-for-yank primary)))
1115 (defun mouse-kill-ring-save (click)
1116 "Copy the region between point and the mouse click in the kill ring.
1117 This does not delete the region; it acts like \\[kill-ring-save]."
1118 (interactive "e")
1119 (mouse-set-mark-fast click)
1120 (let (this-command last-command)
1121 (kill-ring-save (point) (mark t))))
1123 ;; This function used to delete the text between point and the mouse
1124 ;; whenever it was equal to the front of the kill ring, but some
1125 ;; people found that confusing.
1127 ;; The position of the last invocation of `mouse-save-then-kill'.
1128 (defvar mouse-save-then-kill-posn nil)
1130 (defun mouse-save-then-kill-delete-region (beg end)
1131 ;; We must make our own undo boundaries
1132 ;; because they happen automatically only for the current buffer.
1133 (undo-boundary)
1134 (if (or (= beg end) (eq buffer-undo-list t))
1135 ;; If we have no undo list in this buffer,
1136 ;; just delete.
1137 (delete-region beg end)
1138 ;; Delete, but make the undo-list entry share with the kill ring.
1139 ;; First, delete just one char, so in case buffer is being modified
1140 ;; for the first time, the undo list records that fact.
1141 (let ((inhibit-modification-hooks t))
1142 (delete-region beg
1143 (+ beg (if (> end beg) 1 -1))))
1144 (let ((buffer-undo-list buffer-undo-list))
1145 ;; Undo that deletion--but don't change the undo list!
1146 (let ((inhibit-modification-hooks t))
1147 (primitive-undo 1 buffer-undo-list))
1148 ;; Now delete the rest of the specified region,
1149 ;; but don't record it.
1150 (setq buffer-undo-list t)
1151 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1152 (error "Lossage in mouse-save-then-kill-delete-region"))
1153 (delete-region beg end))
1154 (let ((tail buffer-undo-list))
1155 ;; Search back in buffer-undo-list for the string
1156 ;; that came from deleting one character.
1157 (while (and tail (not (stringp (car (car tail)))))
1158 (setq tail (cdr tail)))
1159 ;; Replace it with an entry for the entire deleted text.
1160 (and tail
1161 (setcar tail (cons (car kill-ring) (min beg end))))))
1162 (undo-boundary))
1164 (defun mouse-save-then-kill (click)
1165 "Set the region according to CLICK; the second time, kill it.
1166 CLICK should be a mouse click event.
1168 If the region is inactive, activate it temporarily. Set mark at
1169 the original point, and move point to the position of CLICK.
1171 If the region is already active, adjust it. Normally, do this by
1172 moving point or mark, whichever is closer, to CLICK. But if you
1173 have selected whole words or lines, move point or mark to the
1174 word or line boundary closest to CLICK instead.
1176 If `mouse-drag-copy-region' is non-nil, this command also saves the
1177 new region to the kill ring (replacing the previous kill if the
1178 previous region was just saved to the kill ring).
1180 If this command is called a second consecutive time with the same
1181 CLICK position, kill the region (or delete it
1182 if `mouse-drag-copy-region' is non-nil)"
1183 (interactive "e")
1184 (mouse-minibuffer-check click)
1185 (let* ((posn (event-start click))
1186 (click-pt (posn-point posn))
1187 (window (posn-window posn))
1188 (buf (window-buffer window))
1189 ;; Don't let a subsequent kill command append to this one.
1190 (this-command this-command)
1191 ;; Check if the user has multi-clicked to select words/lines.
1192 (click-count
1193 (if (and (eq mouse-selection-click-count-buffer buf)
1194 (with-current-buffer buf (mark t)))
1195 mouse-selection-click-count
1196 0)))
1197 (cond
1198 ((not (numberp click-pt)) nil)
1199 ;; If the user clicked without moving point, kill the region.
1200 ;; This also resets `mouse-selection-click-count'.
1201 ((and (eq last-command 'mouse-save-then-kill)
1202 (eq click-pt mouse-save-then-kill-posn)
1203 (eq window (selected-window)))
1204 (if mouse-drag-copy-region
1205 ;; Region already saved in the previous click;
1206 ;; don't make a duplicate entry, just delete.
1207 (delete-region (mark t) (point))
1208 (kill-region (mark t) (point)))
1209 (setq mouse-selection-click-count 0)
1210 (setq mouse-save-then-kill-posn nil))
1212 ;; Otherwise, if there is a suitable region, adjust it by moving
1213 ;; one end (whichever is closer) to CLICK-PT.
1214 ((or (with-current-buffer buf (region-active-p))
1215 (and (eq window (selected-window))
1216 (mark t)
1217 (or (and (eq last-command 'mouse-save-then-kill)
1218 mouse-save-then-kill-posn)
1219 (and (memq last-command '(mouse-drag-region
1220 mouse-set-region))
1221 (or mark-even-if-inactive
1222 (not transient-mark-mode))))))
1223 (select-window window)
1224 (let* ((range (mouse-start-end click-pt click-pt click-count)))
1225 (if (< (abs (- click-pt (mark t)))
1226 (abs (- click-pt (point))))
1227 (set-mark (car range))
1228 (goto-char (nth 1 range)))
1229 (setq deactivate-mark nil)
1230 (mouse-set-region-1)
1231 (when mouse-drag-copy-region
1232 ;; Region already copied to kill-ring once, so replace.
1233 (kill-new (filter-buffer-substring (mark t) (point)) t))
1234 ;; Arrange for a repeated mouse-3 to kill the region.
1235 (setq mouse-save-then-kill-posn click-pt)))
1237 ;; Otherwise, set the mark where point is and move to CLICK-PT.
1239 (select-window window)
1240 (mouse-set-mark-fast click)
1241 (let ((before-scroll (with-current-buffer buf point-before-scroll)))
1242 (if before-scroll (goto-char before-scroll)))
1243 (exchange-point-and-mark)
1244 (mouse-set-region-1)
1245 (when mouse-drag-copy-region
1246 (kill-new (filter-buffer-substring (mark t) (point))))
1247 (setq mouse-save-then-kill-posn click-pt)))))
1250 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1251 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1252 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1253 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1254 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1256 (defconst mouse-secondary-overlay
1257 (let ((ol (make-overlay (point-min) (point-min))))
1258 (delete-overlay ol)
1259 (overlay-put ol 'face 'secondary-selection)
1261 "An overlay which records the current secondary selection.
1262 It is deleted when there is no secondary selection.")
1264 (defvar mouse-secondary-click-count 0)
1266 ;; A marker which records the specified first end for a secondary selection.
1267 ;; May be nil.
1268 (defvar mouse-secondary-start nil)
1270 (defun mouse-start-secondary (click)
1271 "Set one end of the secondary selection to the position clicked on.
1272 Use \\[mouse-secondary-save-then-kill] to set the other end
1273 and complete the secondary selection."
1274 (interactive "e")
1275 (mouse-minibuffer-check click)
1276 (let ((posn (event-start click)))
1277 (with-current-buffer (window-buffer (posn-window posn))
1278 ;; Cancel any preexisting secondary selection.
1279 (delete-overlay mouse-secondary-overlay)
1280 (if (numberp (posn-point posn))
1281 (progn
1282 (or mouse-secondary-start
1283 (setq mouse-secondary-start (make-marker)))
1284 (move-marker mouse-secondary-start (posn-point posn)))))))
1286 (defun mouse-set-secondary (click)
1287 "Set the secondary selection to the text that the mouse is dragged over.
1288 This must be bound to a mouse drag event."
1289 (interactive "e")
1290 (mouse-minibuffer-check click)
1291 (let ((posn (event-start click))
1293 (end (event-end click)))
1294 (with-current-buffer (window-buffer (posn-window posn))
1295 (if (numberp (posn-point posn))
1296 (setq beg (posn-point posn)))
1297 (move-overlay mouse-secondary-overlay beg (posn-point end))
1298 (gui-set-selection
1299 'SECONDARY
1300 (buffer-substring (overlay-start mouse-secondary-overlay)
1301 (overlay-end mouse-secondary-overlay))))))
1303 (defun mouse-drag-secondary (start-event)
1304 "Set the secondary selection to the text that the mouse is dragged over.
1305 Highlight the drag area as you move the mouse.
1306 This must be bound to a button-down mouse event.
1307 The function returns a non-nil value if it creates a secondary selection."
1308 (interactive "e")
1309 (mouse-minibuffer-check start-event)
1310 (let* ((echo-keystrokes 0)
1311 (start-posn (event-start start-event))
1312 (start-point (posn-point start-posn))
1313 (start-window (posn-window start-posn))
1314 (bounds (window-edges start-window))
1315 (top (nth 1 bounds))
1316 (bottom (if (window-minibuffer-p start-window)
1317 (nth 3 bounds)
1318 ;; Don't count the mode line.
1319 (1- (nth 3 bounds))))
1320 (click-count (1- (event-click-count start-event))))
1321 (with-current-buffer (window-buffer start-window)
1322 (setq mouse-secondary-click-count click-count)
1323 (if (> (mod click-count 3) 0)
1324 ;; Double or triple press: make an initial selection
1325 ;; of one word or line.
1326 (let ((range (mouse-start-end start-point start-point click-count)))
1327 (set-marker mouse-secondary-start nil)
1328 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1329 (window-buffer start-window)))
1330 ;; Single-press: cancel any preexisting secondary selection.
1331 (or mouse-secondary-start
1332 (setq mouse-secondary-start (make-marker)))
1333 (set-marker mouse-secondary-start start-point)
1334 (delete-overlay mouse-secondary-overlay))
1335 ;; FIXME: Use mouse-drag-track!
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 (gui-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 (gui-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 (gui-get-selection 'SECONDARY)))
1395 (if secondary
1396 (insert-for-yank 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 (gui-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 ((buf (x-popup-menu event (mouse-buffer-menu-map)))
1559 (window (posn-window (event-start event))))
1560 (when buf
1561 (select-window
1562 (if (framep window) (frame-selected-window window)
1563 window))
1564 (switch-to-buffer buf))))
1566 (defun mouse-buffer-menu-map ()
1567 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1568 (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
1569 (dolist (buf buffers)
1570 ;; Divide all buffers into buckets for various major modes.
1571 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1572 (with-current-buffer buf
1573 (let* ((adjusted-major-mode major-mode) elt)
1574 (dolist (group mouse-buffer-menu-mode-groups)
1575 (when (string-match (car group) (format-mode-line mode-name))
1576 (setq adjusted-major-mode (cdr group))))
1577 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1578 (unless elt
1579 (setq elt (list adjusted-major-mode
1580 (if (stringp adjusted-major-mode)
1581 adjusted-major-mode
1582 (format-mode-line mode-name nil nil buf)))
1583 split-by-major-mode (cons elt split-by-major-mode)))
1584 (or (memq buf (cdr (cdr elt)))
1585 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1586 ;; Compute the sum of squares of sizes of the major-mode buckets.
1587 (let ((tail split-by-major-mode))
1588 (setq sum-of-squares 0)
1589 (while tail
1590 (setq sum-of-squares
1591 (+ sum-of-squares
1592 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1593 (setq tail (cdr tail))))
1594 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1595 (* (length buffers) (length buffers)))
1596 ;; Subdividing by major modes really helps, so let's do it.
1597 (let (subdivided-menus (buffers-left (length buffers)))
1598 ;; Sort the list to put the most popular major modes first.
1599 (setq split-by-major-mode
1600 (sort split-by-major-mode
1601 (function (lambda (elt1 elt2)
1602 (> (length elt1) (length elt2))))))
1603 ;; Make a separate submenu for each major mode
1604 ;; that has more than one buffer,
1605 ;; unless all the remaining buffers are less than 1/10 of them.
1606 (while (and split-by-major-mode
1607 (and (> (length (car split-by-major-mode)) 3)
1608 (> (* buffers-left 10) (length buffers))))
1609 (let ((this-mode-list (mouse-buffer-menu-alist
1610 (cdr (cdr (car split-by-major-mode))))))
1611 (and this-mode-list
1612 (setq subdivided-menus
1613 (cons (cons
1614 (nth 1 (car split-by-major-mode))
1615 this-mode-list)
1616 subdivided-menus))))
1617 (setq buffers-left
1618 (- buffers-left (length (cdr (car split-by-major-mode)))))
1619 (setq split-by-major-mode (cdr split-by-major-mode)))
1620 ;; If any major modes are left over,
1621 ;; make a single submenu for them.
1622 (if split-by-major-mode
1623 (let ((others-list
1624 (mouse-buffer-menu-alist
1625 ;; we don't need split-by-major-mode any more,
1626 ;; so we can ditch it with nconc.
1627 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1628 (and others-list
1629 (setq subdivided-menus
1630 (cons (cons "Others" others-list)
1631 subdivided-menus)))))
1632 (cons "Buffer Menu" (nreverse subdivided-menus)))
1633 (cons "Buffer Menu"
1634 (mouse-buffer-menu-split "Select Buffer"
1635 (mouse-buffer-menu-alist buffers))))))
1637 (defun mouse-buffer-menu-alist (buffers)
1638 (let (tail
1639 (maxlen 0)
1640 head)
1641 (setq buffers
1642 (sort buffers
1643 (function (lambda (elt1 elt2)
1644 (string< (buffer-name elt1) (buffer-name elt2))))))
1645 (setq tail buffers)
1646 (while tail
1647 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1648 (setq maxlen
1649 (max maxlen
1650 (length (buffer-name (car tail))))))
1651 (setq tail (cdr tail)))
1652 (setq tail buffers)
1653 (while tail
1654 (let ((elt (car tail)))
1655 (if (/= (aref (buffer-name elt) 0) ?\s)
1656 (setq head
1657 (cons
1658 (cons
1659 (format
1660 (format "%%-%ds %%s%%s %%s" maxlen)
1661 (buffer-name elt)
1662 (if (buffer-modified-p elt) "*" " ")
1663 (with-current-buffer elt
1664 (if buffer-read-only "%" " "))
1665 (or (buffer-file-name elt)
1666 (with-current-buffer elt
1667 (if list-buffers-directory
1668 (expand-file-name
1669 list-buffers-directory)))
1670 ""))
1671 elt)
1672 head))))
1673 (setq tail (cdr tail)))
1674 ;; Compensate for the reversal that the above loop does.
1675 (nreverse head)))
1677 (defun mouse-buffer-menu-split (title alist)
1678 ;; If we have lots of buffers, divide them into groups of 20
1679 ;; and make a pane (or submenu) for each one.
1680 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1681 (let ((alist alist) sublists next
1682 (i 1))
1683 (while alist
1684 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1685 ;; and make them the next element of sublist.
1686 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1687 (if next
1688 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1689 nil))
1690 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1691 sublists))
1692 (setq i (1+ i))
1693 (setq alist next))
1694 (nreverse sublists))
1695 ;; Few buffers--put them all in one pane.
1696 (list (cons title alist))))
1698 (define-obsolete-function-alias
1699 'mouse-choose-completion 'choose-completion "23.2")
1701 ;; Font selection.
1703 (defun font-menu-add-default ()
1704 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1705 (font-alist x-fixed-font-alist)
1706 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1707 (if (assoc "Default" elt)
1708 (delete (assoc "Default" elt) elt))
1709 (setcdr elt
1710 (cons (list "Default" default)
1711 (cdr elt)))))
1713 (defvar x-fixed-font-alist
1714 (list
1715 (purecopy "Font Menu")
1716 (cons
1717 (purecopy "Misc")
1718 (mapcar
1719 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1720 ;; For these, we specify the pixel height and width.
1721 '(("fixed" "fixed")
1722 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1723 ("6x12"
1724 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1725 ("6x13"
1726 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1727 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1728 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1729 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1730 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1731 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1732 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1733 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1734 ("")
1735 ("clean 5x8"
1736 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1737 ("clean 6x8"
1738 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1739 ("clean 8x8"
1740 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1741 ("clean 8x10"
1742 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1743 ("clean 8x14"
1744 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1745 ("clean 8x16"
1746 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1747 ("")
1748 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1749 ;; We don't seem to have these; who knows what they are.
1750 ;; ("fg-18" "fg-18")
1751 ;; ("fg-25" "fg-25")
1752 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1753 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1754 ("lucidasanstypewriter-bold-24"
1755 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
1756 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1757 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1760 (cons
1761 (purecopy "Courier")
1762 (mapcar
1763 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1764 ;; For these, we specify the point height.
1765 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1766 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1767 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1768 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1769 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1770 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1771 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1772 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1773 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1774 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1775 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1776 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1777 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1778 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1779 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1780 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1781 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1782 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1783 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1784 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1785 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1786 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1787 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1788 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
1789 ))))
1790 "X fonts suitable for use in Emacs.")
1792 (declare-function generate-fontset-menu "fontset" ())
1794 (defun mouse-select-font ()
1795 "Prompt for a font name, using `x-popup-menu', and return it."
1796 (interactive)
1797 (unless (display-multi-font-p)
1798 (error "Cannot change fonts on this display"))
1799 (car
1800 (x-popup-menu
1801 (if (listp last-nonmenu-event)
1802 last-nonmenu-event
1803 (list '(0 0) (selected-window)))
1804 (append x-fixed-font-alist
1805 (list (generate-fontset-menu))))))
1807 (declare-function text-scale-mode "face-remap")
1809 (defun mouse-set-font (&rest fonts)
1810 "Set the default font for the selected frame.
1811 The argument FONTS is a list of font names; the first valid font
1812 in this list is used.
1814 When called interactively, pop up a menu and allow the user to
1815 choose a font."
1816 (interactive
1817 (progn (unless (display-multi-font-p)
1818 (error "Cannot change fonts on this display"))
1819 (x-popup-menu
1820 (if (listp last-nonmenu-event)
1821 last-nonmenu-event
1822 (list '(0 0) (selected-window)))
1823 ;; Append list of fontsets currently defined.
1824 (append x-fixed-font-alist (list (generate-fontset-menu))))))
1825 (if fonts
1826 (let (font)
1827 (while fonts
1828 (condition-case nil
1829 (progn
1830 (set-frame-font (car fonts))
1831 (setq font (car fonts))
1832 (setq fonts nil))
1833 (error
1834 (setq fonts (cdr fonts)))))
1835 (if (null font)
1836 (error "Font not found")))))
1838 (defvar mouse-appearance-menu-map nil)
1839 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
1840 (declare-function buffer-face-mode-invoke "face-remap"
1841 (face arg &optional interactive))
1842 (declare-function font-face-attributes "font.c" (font &optional frame))
1843 (defvar w32-use-w32-font-dialog)
1844 (defvar w32-fixed-font-alist)
1846 (defun mouse-appearance-menu (event)
1847 "Show a menu for changing the default face in the current buffer."
1848 (interactive "@e")
1849 (require 'face-remap)
1850 (when (display-multi-font-p)
1851 (with-selected-window (car (event-start event))
1852 (if mouse-appearance-menu-map
1853 nil ; regenerate new fonts
1854 ;; Initialize mouse-appearance-menu-map
1855 (setq mouse-appearance-menu-map
1856 (make-sparse-keymap "Change Default Buffer Face"))
1857 (define-key mouse-appearance-menu-map [face-remap-reset-base]
1858 '(menu-item "Reset to Default" face-remap-reset-base))
1859 (define-key mouse-appearance-menu-map [text-scale-decrease]
1860 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
1861 (define-key mouse-appearance-menu-map [text-scale-increase]
1862 '(menu-item "Increase Buffer Text Size" text-scale-increase))
1863 ;; Font selector
1864 (if (and (functionp 'x-select-font)
1865 (or (not (boundp 'w32-use-w32-font-dialog))
1866 w32-use-w32-font-dialog))
1867 (define-key mouse-appearance-menu-map [x-select-font]
1868 '(menu-item "Change Buffer Font..." x-select-font))
1869 ;; If the select-font is unavailable, construct a menu.
1870 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
1871 (font-alist (cdr (append
1872 (if (eq system-type 'windows-nt)
1873 w32-fixed-font-alist
1874 x-fixed-font-alist)
1875 (list (generate-fontset-menu))))))
1876 (dolist (family font-alist)
1877 (let* ((submenu-name (car family))
1878 (submenu-map (make-sparse-keymap submenu-name)))
1879 (dolist (font (cdr family))
1880 (let ((font-name (car font))
1881 font-symbol)
1882 (if (string= font-name "")
1883 (define-key submenu-map [space]
1884 '("--"))
1885 (setq font-symbol (intern (cadr font)))
1886 (define-key submenu-map (vector font-symbol)
1887 (list 'menu-item (car font) font-symbol)))))
1888 (define-key font-submenu (vector (intern submenu-name))
1889 (list 'menu-item submenu-name submenu-map))))
1890 (define-key mouse-appearance-menu-map [font-submenu]
1891 (list 'menu-item "Change Text Font" font-submenu)))))
1892 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
1893 (setq choice (nth (1- (length choice)) choice))
1894 (cond ((eq choice 'text-scale-increase)
1895 (text-scale-increase 1))
1896 ((eq choice 'text-scale-decrease)
1897 (text-scale-increase -1))
1898 ((eq choice 'face-remap-reset-base)
1899 (text-scale-mode 0)
1900 (buffer-face-mode 0))
1901 (choice
1902 ;; Either choice == 'x-select-font, or choice is a
1903 ;; symbol whose name is a font.
1904 (let ((font (if (eq choice 'x-select-font)
1905 (x-select-font)
1906 (symbol-name choice))))
1907 (buffer-face-mode-invoke
1908 (if (fontp font 'font-spec)
1909 (list :font font)
1910 (font-face-attributes font))
1911 t (called-interactively-p 'interactive)))))))))
1914 ;;; Bindings for mouse commands.
1916 (global-set-key [down-mouse-1] 'mouse-drag-region)
1917 (global-set-key [mouse-1] 'mouse-set-point)
1918 (global-set-key [drag-mouse-1] 'mouse-set-region)
1920 (defun mouse--strip-first-event (_prompt)
1921 (substring (this-single-command-raw-keys) 1))
1923 (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
1924 (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
1926 (global-set-key [mouse-2] 'mouse-yank-primary)
1927 ;; Allow yanking also when the corresponding cursor is "in the fringe".
1928 (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
1929 (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
1930 (global-set-key [mouse-3] 'mouse-save-then-kill)
1931 (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
1932 (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
1934 ;; By binding these to down-going events, we let the user use the up-going
1935 ;; event to make the selection, saving a click.
1936 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1937 (if (not (eq system-type 'ms-dos))
1938 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
1939 ;; C-down-mouse-2 is bound in facemenu.el.
1940 (global-set-key [C-down-mouse-3]
1941 `(menu-item ,(purecopy "Menu Bar") ignore
1942 :filter (lambda (_)
1943 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
1944 (mouse-menu-bar-map)
1945 (mouse-menu-major-mode-map)))))
1947 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
1948 ;; vertical-line prevents Emacs from signaling an error when the mouse
1949 ;; button is released after dragging these lines, on non-toolkit
1950 ;; versions.
1951 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
1952 (global-set-key [header-line mouse-1] 'mouse-select-window)
1953 ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1954 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1955 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1956 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1957 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1958 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1959 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1960 (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
1961 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1962 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1963 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1964 (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
1965 (global-set-key [right-divider mouse-1] 'ignore)
1966 (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
1967 (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
1968 (global-set-key [bottom-divider mouse-1] 'ignore)
1969 (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
1971 (provide 'mouse)
1973 ;;; mouse.el ends here