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