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