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