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