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