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