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