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