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