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