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