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