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