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