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