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