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