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