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