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