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