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