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