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