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