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