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