merge from trunk
[bpt/emacs.git] / lisp / mouse.el
1 ;;; mouse.el --- window system-independent mouse support
2
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: hardware, mouse
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
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.
29 ;;
30 ;; The code is rather X-dependent.
31
32 ;;; Code:
33
34 ;;; Utility functions.
35
36 ;; Indent track-mouse like progn.
37 (put 'track-mouse 'lisp-indent-function 0)
38
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)
43
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)
49
50 (defcustom mouse-1-click-follows-link 450
51 "Non-nil means that clicking Mouse-1 on a link follows the link.
52
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).
58
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.
66
67 If value is `double', a double click follows the link.
68
69 Otherwise, a single Mouse-1 click unconditionally follows the link.
70
71 Note that dragging the mouse never follows the link.
72
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)
82
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.
85
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)
92
93
94 \f
95 ;; Provide a mode-specific menu on a mouse button.
96
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))))
151
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))))
176
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)))
182
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))
198
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)))))))
211
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)))
261
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")
269
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")
278
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")
290 \f
291 ;; Commands that operate on windows.
292
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))
300
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)))))
309
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))))
321
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)))
332
333 (defun mouse-delete-other-windows ()
334 "Delete all windows except the one you click on."
335 (interactive "@")
336 (delete-other-windows))
337
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))))))
353
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))))))
369
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))
390
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)))
399
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)))))
410
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)))))
439
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))
445
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))
458
459 ((memq (car event) '(switch-frame select-window))
460 nil)
461
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))
470
471 ((not (eq (car mouse) start-event-frame))
472 nil)
473
474 ((null (car (cdr mouse)))
475 nil)
476
477 (t
478 (setq y (cdr (cdr mouse))
479 edges (window-edges start-event-window)
480 top (nth 1 edges)
481 bot (nth 3 edges))
482
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))
492
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"))
498
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))
503
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)))
513
514 ;; if this window's growth caused another
515 ;; window to be deleted because it was too
516 ;; short, rescind the change.
517 ;;
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)))))
533
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))))))
540
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))
545
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))))
562
563 \f
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))
581
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)
637 (t
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))))))))))
659 \f
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)))
668
669 (defvar mouse-last-region-beg nil)
670 (defvar mouse-last-region-end nil)
671 (defvar mouse-last-region-tick nil)
672
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))))
679
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 (set-mark (point))
701 (if (numberp end) (goto-char end))
702 (mouse-set-region-1)))
703
704 (defun mouse-set-region-1 ()
705 ;; Set transient-mark-mode for a little while.
706 (unless (eq (car-safe transient-mark-mode) 'only)
707 (setq transient-mark-mode
708 (cons 'only
709 (unless (eq transient-mark-mode 'lambda)
710 transient-mark-mode))))
711 (setq mouse-last-region-beg (region-beginning))
712 (setq mouse-last-region-end (region-end))
713 (setq mouse-last-region-tick (buffer-modified-tick)))
714
715 (defcustom mouse-scroll-delay 0.25
716 "The pause between scroll steps caused by mouse drags, in seconds.
717 If you drag the mouse beyond the edge of a window, Emacs scrolls the
718 window to bring the text beyond that edge into view, with a delay of
719 this many seconds between scroll steps. Scrolling stops when you move
720 the mouse back into the window, or release the button.
721 This variable's value may be non-integral.
722 Setting this to zero causes Emacs to scroll as fast as it can."
723 :type 'number
724 :group 'mouse)
725
726 (defcustom mouse-scroll-min-lines 1
727 "The minimum number of lines scrolled by dragging mouse out of window.
728 Moving the mouse out the top or bottom edge of the window begins
729 scrolling repeatedly. The number of lines scrolled per repetition
730 is normally equal to the number of lines beyond the window edge that
731 the mouse has moved. However, it always scrolls at least the number
732 of lines specified by this variable."
733 :type 'integer
734 :group 'mouse)
735
736 (defun mouse-scroll-subr (window jump &optional overlay start)
737 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
738 If OVERLAY is an overlay, let it stretch from START to the far edge of
739 the newly visible text.
740 Upon exit, point is at the far edge of the newly visible text."
741 (cond
742 ((and (> jump 0) (< jump mouse-scroll-min-lines))
743 (setq jump mouse-scroll-min-lines))
744 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
745 (setq jump (- mouse-scroll-min-lines))))
746 (let ((opoint (point)))
747 (while (progn
748 (goto-char (window-start window))
749 (if (not (zerop (vertical-motion jump window)))
750 (progn
751 (set-window-start window (point))
752 (if (natnump jump)
753 (if (window-end window)
754 (progn
755 (goto-char (window-end window))
756 ;; window-end doesn't reflect the window's new
757 ;; start position until the next redisplay.
758 (vertical-motion (1- jump) window))
759 (vertical-motion (- (window-height window) 2)))
760 (goto-char (window-start window)))
761 (if overlay
762 (move-overlay overlay start (point)))
763 ;; Now that we have scrolled WINDOW properly,
764 ;; put point back where it was for the redisplay
765 ;; so that we don't mess up the selected window.
766 (or (eq window (selected-window))
767 (goto-char opoint))
768 (sit-for mouse-scroll-delay)))))
769 (or (eq window (selected-window))
770 (goto-char opoint))))
771
772 (defvar mouse-selection-click-count 0)
773
774 (defvar mouse-selection-click-count-buffer nil)
775
776 (defun mouse-drag-region (start-event)
777 "Set the region to the text that the mouse is dragged over.
778 Highlight the drag area as you move the mouse.
779 This must be bound to a button-down mouse event.
780 In Transient Mark mode, the highlighting remains as long as the mark
781 remains active. Otherwise, it remains until the next input event.
782
783 If the click is in the echo area, display the `*Messages*' buffer."
784 (interactive "e")
785 (let ((w (posn-window (event-start start-event))))
786 (if (and (window-minibuffer-p w)
787 (not (minibuffer-window-active-p w)))
788 (save-excursion
789 ;; Swallow the up-event.
790 (read-event)
791 (set-buffer (get-buffer-create "*Messages*"))
792 (goto-char (point-max))
793 (display-buffer (current-buffer)))
794 ;; Give temporary modes such as isearch a chance to turn off.
795 (run-hooks 'mouse-leave-buffer-hook)
796 (mouse-drag-track start-event t))))
797
798
799 (defun mouse-posn-property (pos property)
800 "Look for a property at click position.
801 POS may be either a buffer position or a click position like
802 those returned from `event-start'. If the click position is on
803 a string, the text property PROPERTY is examined.
804 If this is nil or the click is not on a string, then
805 the corresponding buffer position is searched for PROPERTY.
806 If PROPERTY is encountered in one of those places,
807 its value is returned."
808 (if (consp pos)
809 (let ((w (posn-window pos)) (pt (posn-point pos))
810 (str (posn-string pos)))
811 (or (and str
812 (get-text-property (cdr str) property (car str)))
813 (and pt
814 (get-char-property pt property w))))
815 (get-char-property pos property)))
816
817 (defun mouse-on-link-p (pos)
818 "Return non-nil if POS is on a link in the current buffer.
819 POS must be a buffer position in the current buffer or a mouse
820 event location in the selected window (see `event-start').
821 However, if `mouse-1-click-in-non-selected-windows' is non-nil,
822 POS may be a mouse event location in any window.
823
824 A clickable link is identified by one of the following methods:
825
826 - If the character at POS has a non-nil `follow-link' text or
827 overlay property, the value of that property determines what to do.
828
829 - If there is a local key-binding or a keybinding at position POS
830 for the `follow-link' event, the binding of that event determines
831 what to do.
832
833 The resulting value determine whether POS is inside a link:
834
835 - If the value is `mouse-face', POS is inside a link if there
836 is a non-nil `mouse-face' property at POS. Return t in this case.
837
838 - If the value is a function, FUNC, POS is inside a link if
839 the call \(FUNC POS) returns non-nil. Return the return value
840 from that call. Arg is \(posn-point POS) if POS is a mouse event.
841
842 - Otherwise, return the value itself.
843
844 The return value is interpreted as follows:
845
846 - If it is a string, the mouse-1 event is translated into the
847 first character of the string, i.e. the action of the mouse-1
848 click is the local or global binding of that character.
849
850 - If it is a vector, the mouse-1 event is translated into the
851 first element of that vector, i.e. the action of the mouse-1
852 click is the local or global binding of that event.
853
854 - Otherwise, the mouse-1 event is translated into a mouse-2 event
855 at the same position."
856 (let ((action
857 (and (or (not (consp pos))
858 mouse-1-click-in-non-selected-windows
859 (eq (selected-window) (posn-window pos)))
860 (or (mouse-posn-property pos 'follow-link)
861 (key-binding [follow-link] nil t pos)))))
862 (cond
863 ((eq action 'mouse-face)
864 (and (mouse-posn-property pos 'mouse-face) t))
865 ((functionp action)
866 ;; FIXME: This seems questionable if the click is not in a buffer.
867 ;; Should we instead decide that `action' takes a `posn'?
868 (if (consp pos)
869 (with-current-buffer (window-buffer (posn-window pos))
870 (funcall action (posn-point pos)))
871 (funcall action pos)))
872 (t action))))
873
874 (defun mouse-fixup-help-message (msg)
875 "Fix help message MSG for `mouse-1-click-follows-link'."
876 (let (mp pos)
877 (if (and mouse-1-click-follows-link
878 (stringp msg)
879 (string-match-p "\\`mouse-2" msg)
880 (setq mp (mouse-pixel-position))
881 (consp (setq pos (cdr mp)))
882 (car pos) (>= (car pos) 0)
883 (cdr pos) (>= (cdr pos) 0)
884 (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
885 (windowp (posn-window pos)))
886 (with-current-buffer (window-buffer (posn-window pos))
887 (if (mouse-on-link-p pos)
888 (setq msg (concat
889 (cond
890 ((eq mouse-1-click-follows-link 'double) "double-")
891 ((and (integerp mouse-1-click-follows-link)
892 (< mouse-1-click-follows-link 0)) "Long ")
893 (t ""))
894 "mouse-1" (substring msg 7)))))))
895 msg)
896
897 (defun mouse-drag-track (start-event &optional
898 do-mouse-drag-region-post-process)
899 "Track mouse drags by highlighting area between point and cursor.
900 The region will be defined with mark and point.
901 DO-MOUSE-DRAG-REGION-POST-PROCESS should only be used by
902 `mouse-drag-region'."
903 (mouse-minibuffer-check start-event)
904 (setq mouse-selection-click-count-buffer (current-buffer))
905 (deactivate-mark)
906 (let* ((original-window (selected-window))
907 ;; We've recorded what we needed from the current buffer and
908 ;; window, now let's jump to the place of the event, where things
909 ;; are happening.
910 (_ (mouse-set-point start-event))
911 (echo-keystrokes 0)
912 (start-posn (event-start start-event))
913 (start-point (posn-point start-posn))
914 (start-window (posn-window start-posn))
915 (start-window-start (window-start start-window))
916 (start-hscroll (window-hscroll start-window))
917 (bounds (window-edges start-window))
918 (make-cursor-line-fully-visible nil)
919 (top (nth 1 bounds))
920 (bottom (if (window-minibuffer-p start-window)
921 (nth 3 bounds)
922 ;; Don't count the mode line.
923 (1- (nth 3 bounds))))
924 (on-link (and mouse-1-click-follows-link
925 (or mouse-1-click-in-non-selected-windows
926 (eq start-window original-window))
927 ;; Use start-point before the intangibility
928 ;; treatment, in case we click on a link inside an
929 ;; intangible text.
930 (mouse-on-link-p start-posn)))
931 (click-count (1- (event-click-count start-event)))
932 (remap-double-click (and on-link
933 (eq mouse-1-click-follows-link 'double)
934 (= click-count 1)))
935 ;; Suppress automatic hscrolling, because that is a nuisance
936 ;; when setting point near the right fringe (but see below).
937 (automatic-hscrolling-saved automatic-hscrolling)
938 (automatic-hscrolling nil)
939 event end end-point)
940
941 (setq mouse-selection-click-count click-count)
942 ;; In case the down click is in the middle of some intangible text,
943 ;; use the end of that text, and put it in START-POINT.
944 (if (< (point) start-point)
945 (goto-char start-point))
946 (setq start-point (point))
947 (if remap-double-click
948 (setq click-count 0))
949
950 ;; Activate the region, using `mouse-start-end' to determine where
951 ;; to put point and mark (e.g., double-click will select a word).
952 (setq transient-mark-mode
953 (if (eq transient-mark-mode 'lambda)
954 '(only)
955 (cons 'only transient-mark-mode)))
956 (let ((range (mouse-start-end start-point start-point click-count)))
957 (push-mark (nth 0 range) t t)
958 (goto-char (nth 1 range)))
959
960 ;; Track the mouse until we get a non-movement event.
961 (track-mouse
962 (while (progn
963 (setq event (read-event))
964 (or (mouse-movement-p event)
965 (memq (car-safe event) '(switch-frame select-window))))
966 (unless (memq (car-safe event) '(switch-frame select-window))
967 ;; Automatic hscrolling did not occur during the call to
968 ;; `read-event'; but if the user subsequently drags the
969 ;; mouse, go ahead and hscroll.
970 (let ((automatic-hscrolling automatic-hscrolling-saved))
971 (redisplay))
972 (setq end (event-end event)
973 end-point (posn-point end))
974 (if (and (eq (posn-window end) start-window)
975 (integer-or-marker-p end-point))
976 (mouse--drag-set-mark-and-point start-point
977 end-point click-count)
978 (let ((mouse-row (cdr (cdr (mouse-position)))))
979 (cond
980 ((null mouse-row))
981 ((< mouse-row top)
982 (mouse-scroll-subr start-window (- mouse-row top)
983 nil start-point))
984 ((>= mouse-row bottom)
985 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
986 nil start-point))))))))
987
988 ;; Handle the terminating event if possible.
989 (when (consp event)
990 ;; Ensure that point is on the end of the last event.
991 (when (and (setq end-point (posn-point (event-end event)))
992 (eq (posn-window end) start-window)
993 (integer-or-marker-p end-point)
994 (/= start-point end-point))
995 (mouse--drag-set-mark-and-point start-point
996 end-point click-count))
997
998 ;; Find its binding.
999 (let* ((fun (key-binding (vector (car event))))
1000 (do-multi-click (and (> (event-click-count event) 0)
1001 (functionp fun)
1002 (not (memq fun '(mouse-set-point
1003 mouse-set-region))))))
1004 (if (and (/= (mark) (point))
1005 (not do-multi-click))
1006
1007 ;; If point has moved, finish the drag.
1008 (let* (last-command this-command)
1009 (and mouse-drag-copy-region
1010 do-mouse-drag-region-post-process
1011 (let (deactivate-mark)
1012 (copy-region-as-kill (mark) (point)))))
1013
1014 ;; If point hasn't moved, run the binding of the
1015 ;; terminating up-event.
1016 (if do-multi-click
1017 (goto-char start-point)
1018 (deactivate-mark))
1019 (when (and (functionp fun)
1020 (= start-hscroll (window-hscroll start-window))
1021 ;; Don't run the up-event handler if the window
1022 ;; start changed in a redisplay after the
1023 ;; mouse-set-point for the down-mouse event at
1024 ;; the beginning of this function. When the
1025 ;; window start has changed, the up-mouse event
1026 ;; contains a different position due to the new
1027 ;; window contents, and point is set again.
1028 (or end-point
1029 (= (window-start start-window)
1030 start-window-start)))
1031 (when (and on-link
1032 (= start-point (point))
1033 (mouse--remap-link-click-p start-event event))
1034 ;; If we rebind to mouse-2, reselect previous selected
1035 ;; window, so that the mouse-2 event runs in the same
1036 ;; situation as if user had clicked it directly. Fixes
1037 ;; the bug reported by juri@jurta.org on 2005-12-27.
1038 (if (or (vectorp on-link) (stringp on-link))
1039 (setq event (aref on-link 0))
1040 (select-window original-window)
1041 (setcar event 'mouse-2)
1042 ;; If this mouse click has never been done by the
1043 ;; user, it doesn't have the necessary property to be
1044 ;; interpreted correctly.
1045 (put 'mouse-2 'event-kind 'mouse-click)))
1046 (push event unread-command-events)))))))
1047
1048 (defun mouse--drag-set-mark-and-point (start click click-count)
1049 (let* ((range (mouse-start-end start click click-count))
1050 (beg (nth 0 range))
1051 (end (nth 1 range)))
1052 (cond ((eq (mark) beg)
1053 (goto-char end))
1054 ((eq (mark) end)
1055 (goto-char beg))
1056 ((< click (mark))
1057 (set-mark end)
1058 (goto-char beg))
1059 (t
1060 (set-mark beg)
1061 (goto-char end)))))
1062
1063 (defun mouse--remap-link-click-p (start-event end-event)
1064 (or (and (eq mouse-1-click-follows-link 'double)
1065 (= (event-click-count start-event) 2))
1066 (and
1067 (not (eq mouse-1-click-follows-link 'double))
1068 (= (event-click-count start-event) 1)
1069 (= (event-click-count end-event) 1)
1070 (or (not (integerp mouse-1-click-follows-link))
1071 (let ((t0 (posn-timestamp (event-start start-event)))
1072 (t1 (posn-timestamp (event-end end-event))))
1073 (and (integerp t0) (integerp t1)
1074 (if (> mouse-1-click-follows-link 0)
1075 (<= (- t1 t0) mouse-1-click-follows-link)
1076 (< (- t0 t1) mouse-1-click-follows-link))))))))
1077
1078 \f
1079 ;; Commands to handle xterm-style multiple clicks.
1080 (defun mouse-skip-word (dir)
1081 "Skip over word, over whitespace, or over identical punctuation.
1082 If DIR is positive skip forward; if negative, skip backward."
1083 (let* ((char (following-char))
1084 (syntax (char-to-string (char-syntax char))))
1085 (cond ((string= syntax "w")
1086 ;; Here, we can't use skip-syntax-forward/backward because
1087 ;; they don't pay attention to word-separating-categories,
1088 ;; and thus they will skip over a true word boundary. So,
1089 ;; we simulate the original behavior by using forward-word.
1090 (if (< dir 0)
1091 (if (not (looking-at "\\<"))
1092 (forward-word -1))
1093 (if (or (looking-at "\\<") (not (looking-at "\\>")))
1094 (forward-word 1))))
1095 ((string= syntax " ")
1096 (if (< dir 0)
1097 (skip-syntax-backward syntax)
1098 (skip-syntax-forward syntax)))
1099 ((string= syntax "_")
1100 (if (< dir 0)
1101 (skip-syntax-backward "w_")
1102 (skip-syntax-forward "w_")))
1103 ((< dir 0)
1104 (while (and (not (bobp)) (= (preceding-char) char))
1105 (forward-char -1)))
1106 (t
1107 (while (and (not (eobp)) (= (following-char) char))
1108 (forward-char 1))))))
1109
1110 (defun mouse-start-end (start end mode)
1111 "Return a list of region bounds based on START and END according to MODE.
1112 If MODE is 0 then set point to (min START END), mark to (max START END).
1113 If MODE is 1 then set point to start of word at (min START END),
1114 mark to end of word at (max START END).
1115 If MODE is 2 then do the same for lines."
1116 (if (> start end)
1117 (let ((temp start))
1118 (setq start end
1119 end temp)))
1120 (setq mode (mod mode 3))
1121 (cond ((= mode 0)
1122 (list start end))
1123 ((and (= mode 1)
1124 (= start end)
1125 (char-after start)
1126 (= (char-syntax (char-after start)) ?\())
1127 (list start
1128 (save-excursion
1129 (goto-char start)
1130 (forward-sexp 1)
1131 (point))))
1132 ((and (= mode 1)
1133 (= start end)
1134 (char-after start)
1135 (= (char-syntax (char-after start)) ?\)))
1136 (list (save-excursion
1137 (goto-char (1+ start))
1138 (backward-sexp 1)
1139 (point))
1140 (1+ start)))
1141 ((and (= mode 1)
1142 (= start end)
1143 (char-after start)
1144 (= (char-syntax (char-after start)) ?\"))
1145 (let ((open (or (eq start (point-min))
1146 (save-excursion
1147 (goto-char (- start 1))
1148 (looking-at "\\s(\\|\\s \\|\\s>")))))
1149 (if open
1150 (list start
1151 (save-excursion
1152 (condition-case nil
1153 (progn
1154 (goto-char start)
1155 (forward-sexp 1)
1156 (point))
1157 (error end))))
1158 (list (save-excursion
1159 (condition-case nil
1160 (progn
1161 (goto-char (1+ start))
1162 (backward-sexp 1)
1163 (point))
1164 (error end)))
1165 (1+ start)))))
1166 ((= mode 1)
1167 (list (save-excursion
1168 (goto-char start)
1169 (mouse-skip-word -1)
1170 (point))
1171 (save-excursion
1172 (goto-char end)
1173 (mouse-skip-word 1)
1174 (point))))
1175 ((= mode 2)
1176 (list (save-excursion
1177 (goto-char start)
1178 (line-beginning-position 1))
1179 (save-excursion
1180 (goto-char end)
1181 (forward-line 1)
1182 (point))))))
1183 \f
1184 ;; Subroutine: set the mark where CLICK happened,
1185 ;; but don't do anything else.
1186 (defun mouse-set-mark-fast (click)
1187 (mouse-minibuffer-check click)
1188 (let ((posn (event-start click)))
1189 (select-window (posn-window posn))
1190 (if (numberp (posn-point posn))
1191 (push-mark (posn-point posn) t t))))
1192
1193 (defun mouse-undouble-last-event (events)
1194 (let* ((index (1- (length events)))
1195 (last (nthcdr index events))
1196 (event (car last))
1197 (basic (event-basic-type event))
1198 (old-modifiers (event-modifiers event))
1199 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
1200 (new
1201 (if (consp event)
1202 ;; Use reverse, not nreverse, since event-modifiers
1203 ;; does not copy the list it returns.
1204 (cons (event-convert-list (reverse (cons basic modifiers)))
1205 (cdr event))
1206 event)))
1207 (setcar last new)
1208 (if (and (not (equal modifiers old-modifiers))
1209 (key-binding (apply 'vector events)))
1210 t
1211 (setcar last event)
1212 nil)))
1213
1214 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1215
1216 (defun mouse-set-mark (click)
1217 "Set mark at the position clicked on with the mouse.
1218 Display cursor at that position for a second.
1219 This must be bound to a mouse click."
1220 (interactive "e")
1221 (mouse-minibuffer-check click)
1222 (select-window (posn-window (event-start click)))
1223 ;; We don't use save-excursion because that preserves the mark too.
1224 (let ((point-save (point)))
1225 (unwind-protect
1226 (progn (mouse-set-point click)
1227 (push-mark nil t t)
1228 (or transient-mark-mode
1229 (sit-for 1)))
1230 (goto-char point-save))))
1231
1232 (defun mouse-kill (click)
1233 "Kill the region between point and the mouse click.
1234 The text is saved in the kill ring, as with \\[kill-region]."
1235 (interactive "e")
1236 (mouse-minibuffer-check click)
1237 (let* ((posn (event-start click))
1238 (click-posn (posn-point posn)))
1239 (select-window (posn-window posn))
1240 (if (numberp click-posn)
1241 (kill-region (min (point) click-posn)
1242 (max (point) click-posn)))))
1243
1244 (defun mouse-yank-at-click (click arg)
1245 "Insert the last stretch of killed text at the position clicked on.
1246 Also move point to one end of the text thus inserted (normally the end),
1247 and set mark at the beginning.
1248 Prefix arguments are interpreted as with \\[yank].
1249 If `mouse-yank-at-point' is non-nil, insert at point
1250 regardless of where you click."
1251 (interactive "e\nP")
1252 ;; Give temporary modes such as isearch a chance to turn off.
1253 (run-hooks 'mouse-leave-buffer-hook)
1254 (when select-active-regions
1255 ;; Without this, confusing things happen upon e.g. inserting into
1256 ;; the middle of an active region.
1257 (deactivate-mark))
1258 (or mouse-yank-at-point (mouse-set-point click))
1259 (setq this-command 'yank)
1260 (setq mouse-selection-click-count 0)
1261 (yank arg))
1262
1263 (defun mouse-yank-primary (click)
1264 "Insert the primary selection at the position clicked on.
1265 Move point to the end of the inserted text.
1266 If `mouse-yank-at-point' is non-nil, insert at point
1267 regardless of where you click."
1268 (interactive "e")
1269 ;; Give temporary modes such as isearch a chance to turn off.
1270 (run-hooks 'mouse-leave-buffer-hook)
1271 (when select-active-regions
1272 ;; Without this, confusing things happen upon e.g. inserting into
1273 ;; the middle of an active region.
1274 (deactivate-mark))
1275 (or mouse-yank-at-point (mouse-set-point click))
1276 (let ((primary
1277 (cond
1278 ((fboundp 'x-get-selection-value) ; MS-DOS and MS-Windows
1279 (or (x-get-selection-value)
1280 (x-get-selection 'PRIMARY)))
1281 ;; FIXME: What about xterm-mouse-mode etc.?
1282 (t
1283 (x-get-selection 'PRIMARY)))))
1284 (if primary
1285 (insert primary)
1286 (error "No selection is available"))))
1287
1288 (defun mouse-kill-ring-save (click)
1289 "Copy the region between point and the mouse click in the kill ring.
1290 This does not delete the region; it acts like \\[kill-ring-save]."
1291 (interactive "e")
1292 (mouse-set-mark-fast click)
1293 (let (this-command last-command)
1294 (kill-ring-save (point) (mark t))))
1295
1296 ;; This function used to delete the text between point and the mouse
1297 ;; whenever it was equal to the front of the kill ring, but some
1298 ;; people found that confusing.
1299
1300 ;; A list (TEXT START END), describing the text and position of the last
1301 ;; invocation of mouse-save-then-kill.
1302 (defvar mouse-save-then-kill-posn nil)
1303
1304 (defun mouse-save-then-kill-delete-region (beg end)
1305 ;; We must make our own undo boundaries
1306 ;; because they happen automatically only for the current buffer.
1307 (undo-boundary)
1308 (if (or (= beg end) (eq buffer-undo-list t))
1309 ;; If we have no undo list in this buffer,
1310 ;; just delete.
1311 (delete-region beg end)
1312 ;; Delete, but make the undo-list entry share with the kill ring.
1313 ;; First, delete just one char, so in case buffer is being modified
1314 ;; for the first time, the undo list records that fact.
1315 (let (before-change-functions after-change-functions)
1316 (delete-region beg
1317 (+ beg (if (> end beg) 1 -1))))
1318 (let ((buffer-undo-list buffer-undo-list))
1319 ;; Undo that deletion--but don't change the undo list!
1320 (let (before-change-functions after-change-functions)
1321 (primitive-undo 1 buffer-undo-list))
1322 ;; Now delete the rest of the specified region,
1323 ;; but don't record it.
1324 (setq buffer-undo-list t)
1325 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1326 (error "Lossage in mouse-save-then-kill-delete-region"))
1327 (delete-region beg end))
1328 (let ((tail buffer-undo-list))
1329 ;; Search back in buffer-undo-list for the string
1330 ;; that came from deleting one character.
1331 (while (and tail (not (stringp (car (car tail)))))
1332 (setq tail (cdr tail)))
1333 ;; Replace it with an entry for the entire deleted text.
1334 (and tail
1335 (setcar tail (cons (car kill-ring) (min beg end))))))
1336 (undo-boundary))
1337
1338 (defun mouse-save-then-kill (click)
1339 "Set the region according to CLICK; the second time, kill the region.
1340 Assuming this command is bound to a mouse button, CLICK is the
1341 corresponding input event.
1342
1343 If the region is already active, adjust it. Normally, this
1344 happens by moving either point or mark, whichever is closer, to
1345 the position of CLICK. But if you have selected words or lines,
1346 the region is adjusted by moving point or mark to the word or
1347 line boundary closest to CLICK.
1348
1349 If the region is inactive, activate it temporarily; set mark at
1350 the original point, and move click to the position of CLICK.
1351
1352 However, if this command is being called a second time (i.e. the
1353 value of `last-command' is `mouse-save-then-kill'), kill the
1354 region instead. If the text in the region is the same as the
1355 text in the front of the kill ring, just delete it."
1356 (interactive "e")
1357 (let ((before-scroll
1358 (with-current-buffer (window-buffer (posn-window (event-start click)))
1359 point-before-scroll)))
1360 (mouse-minibuffer-check click)
1361 (let ((click-posn (posn-point (event-start click)))
1362 ;; Don't let a subsequent kill command append to this one:
1363 ;; prevent setting this-command to kill-region.
1364 (this-command this-command))
1365 (if (and (with-current-buffer
1366 (window-buffer (posn-window (event-start click)))
1367 (and (mark t)
1368 (> (mod mouse-selection-click-count 3) 0)
1369 ;; Don't be fooled by a recent click in some other buffer.
1370 (eq mouse-selection-click-count-buffer
1371 (current-buffer)))))
1372 (if (and (eq last-command 'mouse-save-then-kill)
1373 (equal click-posn (nth 2 mouse-save-then-kill-posn)))
1374 ;; If we click this button again without moving it, kill.
1375 (progn
1376 ;; Call `deactivate-mark' to save the primary selection.
1377 (deactivate-mark)
1378 (mouse-save-then-kill-delete-region (mark) (point))
1379 (setq mouse-selection-click-count 0)
1380 (setq mouse-save-then-kill-posn nil))
1381 ;; Find both ends of the object selected by this click.
1382 (let* ((range
1383 (mouse-start-end click-posn click-posn
1384 mouse-selection-click-count)))
1385 ;; Move whichever end is closer to the click.
1386 ;; That's what xterm does, and it seems reasonable.
1387 (if (< (abs (- click-posn (mark t)))
1388 (abs (- click-posn (point))))
1389 (set-mark (car range))
1390 (goto-char (nth 1 range)))
1391 ;; We have already put the old region in the kill ring.
1392 ;; Replace it with the extended region.
1393 ;; (It would be annoying to make a separate entry.)
1394 (kill-new (buffer-substring (point) (mark t)) t)
1395 (mouse-set-region-1)
1396 ;; Arrange for a repeated mouse-3 to kill this region.
1397 (setq mouse-save-then-kill-posn
1398 (list (car kill-ring) (point) click-posn))))
1399
1400 (if (and (eq last-command 'mouse-save-then-kill)
1401 mouse-save-then-kill-posn
1402 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1403 (equal (cdr mouse-save-then-kill-posn)
1404 (list (point) click-posn)))
1405 ;; If this is the second time we've called
1406 ;; mouse-save-then-kill, delete the text from the buffer.
1407 (progn
1408 ;; Call `deactivate-mark' to save the primary selection.
1409 (deactivate-mark)
1410 (mouse-save-then-kill-delete-region (point) (mark t))
1411 ;; After we kill, another click counts as "the first time".
1412 (setq mouse-save-then-kill-posn nil))
1413 ;; This is not a repetition.
1414 ;; We are adjusting an old selection or creating a new one.
1415 (if (or (and (eq last-command 'mouse-save-then-kill)
1416 mouse-save-then-kill-posn)
1417 (and mark-active transient-mark-mode)
1418 (and (memq last-command
1419 '(mouse-drag-region mouse-set-region))
1420 (or mark-even-if-inactive
1421 (not transient-mark-mode))))
1422 ;; We have a selection or suitable region, so adjust it.
1423 (let* ((posn (event-start click))
1424 (new (posn-point posn)))
1425 (select-window (posn-window posn))
1426 (if (numberp new)
1427 (progn
1428 ;; Move whichever end of the region is closer to the click.
1429 ;; That is what xterm does, and it seems reasonable.
1430 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1431 (goto-char new)
1432 (set-mark new))
1433 (setq deactivate-mark nil)))
1434 (kill-new (buffer-substring (point) (mark t)) t))
1435 ;; Set the mark where point is, then move where clicked.
1436 (mouse-set-mark-fast click)
1437 (if before-scroll
1438 (goto-char before-scroll))
1439 (exchange-point-and-mark) ;Why??? --Stef
1440 (kill-new (buffer-substring (point) (mark t))))
1441 (mouse-set-region-1)
1442 (setq mouse-save-then-kill-posn
1443 (list (car kill-ring) (point) click-posn)))))))
1444 \f
1445 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1446 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1447 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1448 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1449 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1450
1451 (defconst mouse-secondary-overlay
1452 (let ((ol (make-overlay (point-min) (point-min))))
1453 (delete-overlay ol)
1454 (overlay-put ol 'face 'secondary-selection)
1455 ol)
1456 "An overlay which records the current secondary selection.
1457 It is deleted when there is no secondary selection.")
1458
1459 (defvar mouse-secondary-click-count 0)
1460
1461 ;; A marker which records the specified first end for a secondary selection.
1462 ;; May be nil.
1463 (defvar mouse-secondary-start nil)
1464
1465 (defun mouse-start-secondary (click)
1466 "Set one end of the secondary selection to the position clicked on.
1467 Use \\[mouse-secondary-save-then-kill] to set the other end
1468 and complete the secondary selection."
1469 (interactive "e")
1470 (mouse-minibuffer-check click)
1471 (let ((posn (event-start click)))
1472 (with-current-buffer (window-buffer (posn-window posn))
1473 ;; Cancel any preexisting secondary selection.
1474 (delete-overlay mouse-secondary-overlay)
1475 (if (numberp (posn-point posn))
1476 (progn
1477 (or mouse-secondary-start
1478 (setq mouse-secondary-start (make-marker)))
1479 (move-marker mouse-secondary-start (posn-point posn)))))))
1480
1481 (defun mouse-set-secondary (click)
1482 "Set the secondary selection to the text that the mouse is dragged over.
1483 This must be bound to a mouse drag event."
1484 (interactive "e")
1485 (mouse-minibuffer-check click)
1486 (let ((posn (event-start click))
1487 beg
1488 (end (event-end click)))
1489 (with-current-buffer (window-buffer (posn-window posn))
1490 (if (numberp (posn-point posn))
1491 (setq beg (posn-point posn)))
1492 (move-overlay mouse-secondary-overlay beg (posn-point end))
1493 (x-set-selection
1494 'SECONDARY
1495 (buffer-substring (overlay-start mouse-secondary-overlay)
1496 (overlay-end mouse-secondary-overlay))))))
1497
1498 (defun mouse-drag-secondary (start-event)
1499 "Set the secondary selection to the text that the mouse is dragged over.
1500 Highlight the drag area as you move the mouse.
1501 This must be bound to a button-down mouse event.
1502 The function returns a non-nil value if it creates a secondary selection."
1503 (interactive "e")
1504 (mouse-minibuffer-check start-event)
1505 (let* ((echo-keystrokes 0)
1506 (start-posn (event-start start-event))
1507 (start-point (posn-point start-posn))
1508 (start-window (posn-window start-posn))
1509 (bounds (window-edges start-window))
1510 (top (nth 1 bounds))
1511 (bottom (if (window-minibuffer-p start-window)
1512 (nth 3 bounds)
1513 ;; Don't count the mode line.
1514 (1- (nth 3 bounds))))
1515 (click-count (1- (event-click-count start-event))))
1516 (with-current-buffer (window-buffer start-window)
1517 (setq mouse-secondary-click-count click-count)
1518 (if (> (mod click-count 3) 0)
1519 ;; Double or triple press: make an initial selection
1520 ;; of one word or line.
1521 (let ((range (mouse-start-end start-point start-point click-count)))
1522 (set-marker mouse-secondary-start nil)
1523 ;; Why the double move? --Stef
1524 ;; (move-overlay mouse-secondary-overlay 1 1
1525 ;; (window-buffer start-window))
1526 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1527 (window-buffer start-window)))
1528 ;; Single-press: cancel any preexisting secondary selection.
1529 (or mouse-secondary-start
1530 (setq mouse-secondary-start (make-marker)))
1531 (set-marker mouse-secondary-start start-point)
1532 (delete-overlay mouse-secondary-overlay))
1533 (let (event end end-point)
1534 (track-mouse
1535 (while (progn
1536 (setq event (read-event))
1537 (or (mouse-movement-p event)
1538 (memq (car-safe event) '(switch-frame select-window))))
1539
1540 (if (memq (car-safe event) '(switch-frame select-window))
1541 nil
1542 (setq end (event-end event)
1543 end-point (posn-point end))
1544 (cond
1545 ;; Are we moving within the original window?
1546 ((and (eq (posn-window end) start-window)
1547 (integer-or-marker-p end-point))
1548 (let ((range (mouse-start-end start-point end-point
1549 click-count)))
1550 (if (or (/= start-point end-point)
1551 (null (marker-position mouse-secondary-start)))
1552 (progn
1553 (set-marker mouse-secondary-start nil)
1554 (move-overlay mouse-secondary-overlay
1555 (car range) (nth 1 range))))))
1556 (t
1557 (let ((mouse-row (cdr (cdr (mouse-position)))))
1558 (cond
1559 ((null mouse-row))
1560 ((< mouse-row top)
1561 (mouse-scroll-subr start-window (- mouse-row top)
1562 mouse-secondary-overlay start-point))
1563 ((>= mouse-row bottom)
1564 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1565 mouse-secondary-overlay start-point)))))))))
1566
1567 (if (consp event)
1568 (if (marker-position mouse-secondary-start)
1569 (save-window-excursion
1570 (delete-overlay mouse-secondary-overlay)
1571 (x-set-selection 'SECONDARY nil)
1572 (select-window start-window)
1573 (save-excursion
1574 (goto-char mouse-secondary-start)
1575 (sit-for 1)
1576 nil))
1577 (x-set-selection
1578 'SECONDARY
1579 (buffer-substring (overlay-start mouse-secondary-overlay)
1580 (overlay-end mouse-secondary-overlay)))))))))
1581
1582 (defun mouse-yank-secondary (click)
1583 "Insert the secondary selection at the position clicked on.
1584 Move point to the end of the inserted text.
1585 If `mouse-yank-at-point' is non-nil, insert at point
1586 regardless of where you click."
1587 (interactive "e")
1588 ;; Give temporary modes such as isearch a chance to turn off.
1589 (run-hooks 'mouse-leave-buffer-hook)
1590 (or mouse-yank-at-point (mouse-set-point click))
1591 (let ((secondary (x-get-selection 'SECONDARY)))
1592 (if secondary
1593 (insert secondary)
1594 (error "No secondary selection"))))
1595
1596 (defun mouse-kill-secondary ()
1597 "Kill the text in the secondary selection.
1598 This is intended more as a keyboard command than as a mouse command
1599 but it can work as either one.
1600
1601 The current buffer (in case of keyboard use), or the buffer clicked on,
1602 must be the one that the secondary selection is in. This requirement
1603 is to prevent accidents."
1604 (interactive)
1605 (let* ((keys (this-command-keys))
1606 (click (elt keys (1- (length keys)))))
1607 (or (eq (overlay-buffer mouse-secondary-overlay)
1608 (if (listp click)
1609 (window-buffer (posn-window (event-start click)))
1610 (current-buffer)))
1611 (error "Select or click on the buffer where the secondary selection is")))
1612 (let (this-command)
1613 (with-current-buffer (overlay-buffer mouse-secondary-overlay)
1614 (kill-region (overlay-start mouse-secondary-overlay)
1615 (overlay-end mouse-secondary-overlay))))
1616 (delete-overlay mouse-secondary-overlay))
1617
1618 (defun mouse-secondary-save-then-kill (click)
1619 "Save text to point in kill ring; the second time, kill the text.
1620 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1621 If the text between where you did \\[mouse-start-secondary] and where
1622 you use this command matches the text at the front of the kill ring,
1623 this command deletes the text.
1624 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1625 which prepares for a second click with this command to delete the text.
1626
1627 If you have already made a secondary selection in that buffer,
1628 this command extends or retracts the selection to where you click.
1629 If you do this again in a different position, it extends or retracts
1630 again. If you do this twice in the same position, it kills the selection."
1631 (interactive "e")
1632 (mouse-minibuffer-check click)
1633 (let ((posn (event-start click))
1634 (click-posn (posn-point (event-start click)))
1635 ;; Don't let a subsequent kill command append to this one:
1636 ;; prevent setting this-command to kill-region.
1637 (this-command this-command))
1638 (or (eq (window-buffer (posn-window posn))
1639 (or (overlay-buffer mouse-secondary-overlay)
1640 (if mouse-secondary-start
1641 (marker-buffer mouse-secondary-start))))
1642 (error "Wrong buffer"))
1643 (with-current-buffer (window-buffer (posn-window posn))
1644 (if (> (mod mouse-secondary-click-count 3) 0)
1645 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1646 (equal click-posn
1647 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1648 ;; Find both ends of the object selected by this click.
1649 (let* ((range
1650 (mouse-start-end click-posn click-posn
1651 mouse-secondary-click-count)))
1652 ;; Move whichever end is closer to the click.
1653 ;; That's what xterm does, and it seems reasonable.
1654 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1655 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1656 (move-overlay mouse-secondary-overlay (car range)
1657 (overlay-end mouse-secondary-overlay))
1658 (move-overlay mouse-secondary-overlay
1659 (overlay-start mouse-secondary-overlay)
1660 (nth 1 range)))
1661 ;; We have already put the old region in the kill ring.
1662 ;; Replace it with the extended region.
1663 ;; (It would be annoying to make a separate entry.)
1664 (kill-new (buffer-substring
1665 (overlay-start mouse-secondary-overlay)
1666 (overlay-end mouse-secondary-overlay)) t)
1667 ;; Arrange for a repeated mouse-3 to kill this region.
1668 (setq mouse-save-then-kill-posn
1669 (list (car kill-ring) (point) click-posn)))
1670 ;; If we click this button again without moving it,
1671 ;; that time kill.
1672 (progn
1673 (mouse-save-then-kill-delete-region
1674 (overlay-start mouse-secondary-overlay)
1675 (overlay-end mouse-secondary-overlay))
1676 (setq mouse-save-then-kill-posn nil)
1677 (setq mouse-secondary-click-count 0)
1678 (delete-overlay mouse-secondary-overlay)))
1679 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1680 mouse-save-then-kill-posn
1681 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1682 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1683 ;; If this is the second time we've called
1684 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1685 (progn
1686 (mouse-save-then-kill-delete-region
1687 (overlay-start mouse-secondary-overlay)
1688 (overlay-end mouse-secondary-overlay))
1689 (setq mouse-save-then-kill-posn nil)
1690 (delete-overlay mouse-secondary-overlay))
1691 (if (overlay-start mouse-secondary-overlay)
1692 ;; We have a selection, so adjust it.
1693 (progn
1694 (if (numberp click-posn)
1695 (progn
1696 ;; Move whichever end of the region is closer to the click.
1697 ;; That is what xterm does, and it seems reasonable.
1698 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1699 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1700 (move-overlay mouse-secondary-overlay click-posn
1701 (overlay-end mouse-secondary-overlay))
1702 (move-overlay mouse-secondary-overlay
1703 (overlay-start mouse-secondary-overlay)
1704 click-posn))
1705 (setq deactivate-mark nil)))
1706 (if (eq last-command 'mouse-secondary-save-then-kill)
1707 ;; If the front of the kill ring comes from
1708 ;; an immediately previous use of this command,
1709 ;; replace it with the extended region.
1710 ;; (It would be annoying to make a separate entry.)
1711 (kill-new (buffer-substring
1712 (overlay-start mouse-secondary-overlay)
1713 (overlay-end mouse-secondary-overlay)) t)
1714 (let (deactivate-mark)
1715 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1716 (overlay-end mouse-secondary-overlay)))))
1717 (if mouse-secondary-start
1718 ;; All we have is one end of a selection,
1719 ;; so put the other end here.
1720 (let ((start (+ 0 mouse-secondary-start)))
1721 (kill-ring-save start click-posn)
1722 (move-overlay mouse-secondary-overlay start click-posn))))
1723 (setq mouse-save-then-kill-posn
1724 (list (car kill-ring) (point) click-posn))))
1725 (if (overlay-buffer mouse-secondary-overlay)
1726 (x-set-selection 'SECONDARY
1727 (buffer-substring
1728 (overlay-start mouse-secondary-overlay)
1729 (overlay-end mouse-secondary-overlay)))))))
1730 \f
1731 (defcustom mouse-buffer-menu-maxlen 20
1732 "Number of buffers in one pane (submenu) of the buffer menu.
1733 If we have lots of buffers, divide them into groups of
1734 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1735 :type 'integer
1736 :group 'mouse)
1737
1738 (defcustom mouse-buffer-menu-mode-mult 4
1739 "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1740 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1741 will split the buffer menu by the major modes (see
1742 `mouse-buffer-menu-mode-groups') or just by menu length.
1743 Set to 1 (or even 0!) if you want to group by major mode always, and to
1744 a large number if you prefer a mixed multitude. The default is 4."
1745 :type 'integer
1746 :group 'mouse
1747 :version "20.3")
1748
1749 (defvar mouse-buffer-menu-mode-groups
1750 (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
1751 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1752 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1753 . "Mail/News")
1754 ("\\<C\\>" . "C")
1755 ("ObjC" . "C")
1756 ("Text" . "Text")
1757 ("Outline" . "Text")
1758 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1759 ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
1760 ("Lisp" . "Lisp")))
1761 "How to group various major modes together in \\[mouse-buffer-menu].
1762 Each element has the form (REGEXP . GROUPNAME).
1763 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1764
1765 (defun mouse-buffer-menu (event)
1766 "Pop up a menu of buffers for selection with the mouse.
1767 This switches buffers in the window that you clicked on,
1768 and selects that window."
1769 (interactive "e")
1770 (mouse-minibuffer-check event)
1771 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1772 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1773 (dolist (buf buffers)
1774 ;; Divide all buffers into buckets for various major modes.
1775 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1776 (with-current-buffer buf
1777 (let* ((adjusted-major-mode major-mode) elt)
1778 (dolist (group mouse-buffer-menu-mode-groups)
1779 (when (string-match (car group) (format-mode-line mode-name))
1780 (setq adjusted-major-mode (cdr group))))
1781 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1782 (unless elt
1783 (setq elt (list adjusted-major-mode
1784 (if (stringp adjusted-major-mode)
1785 adjusted-major-mode
1786 (format-mode-line mode-name nil nil buf)))
1787 split-by-major-mode (cons elt split-by-major-mode)))
1788 (or (memq buf (cdr (cdr elt)))
1789 (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
1790 ;; Compute the sum of squares of sizes of the major-mode buckets.
1791 (let ((tail split-by-major-mode))
1792 (setq sum-of-squares 0)
1793 (while tail
1794 (setq sum-of-squares
1795 (+ sum-of-squares
1796 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1797 (setq tail (cdr tail))))
1798 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1799 (* (length buffers) (length buffers)))
1800 ;; Subdividing by major modes really helps, so let's do it.
1801 (let (subdivided-menus (buffers-left (length buffers)))
1802 ;; Sort the list to put the most popular major modes first.
1803 (setq split-by-major-mode
1804 (sort split-by-major-mode
1805 (function (lambda (elt1 elt2)
1806 (> (length elt1) (length elt2))))))
1807 ;; Make a separate submenu for each major mode
1808 ;; that has more than one buffer,
1809 ;; unless all the remaining buffers are less than 1/10 of them.
1810 (while (and split-by-major-mode
1811 (and (> (length (car split-by-major-mode)) 3)
1812 (> (* buffers-left 10) (length buffers))))
1813 (let ((this-mode-list (mouse-buffer-menu-alist
1814 (cdr (cdr (car split-by-major-mode))))))
1815 (and this-mode-list
1816 (setq subdivided-menus
1817 (cons (cons
1818 (nth 1 (car split-by-major-mode))
1819 this-mode-list)
1820 subdivided-menus))))
1821 (setq buffers-left
1822 (- buffers-left (length (cdr (car split-by-major-mode)))))
1823 (setq split-by-major-mode (cdr split-by-major-mode)))
1824 ;; If any major modes are left over,
1825 ;; make a single submenu for them.
1826 (if split-by-major-mode
1827 (let ((others-list
1828 (mouse-buffer-menu-alist
1829 ;; we don't need split-by-major-mode any more,
1830 ;; so we can ditch it with nconc.
1831 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1832 (and others-list
1833 (setq subdivided-menus
1834 (cons (cons "Others" others-list)
1835 subdivided-menus)))))
1836 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1837 (progn
1838 (setq alist (mouse-buffer-menu-alist buffers))
1839 (setq menu (cons "Buffer Menu"
1840 (mouse-buffer-menu-split "Select Buffer" alist)))))
1841 (let ((buf (x-popup-menu event menu))
1842 (window (posn-window (event-start event))))
1843 (when buf
1844 (select-window
1845 (if (framep window) (frame-selected-window window)
1846 window))
1847 (switch-to-buffer buf)))))
1848
1849 (defun mouse-buffer-menu-alist (buffers)
1850 (let (tail
1851 (maxlen 0)
1852 head)
1853 (setq buffers
1854 (sort buffers
1855 (function (lambda (elt1 elt2)
1856 (string< (buffer-name elt1) (buffer-name elt2))))))
1857 (setq tail buffers)
1858 (while tail
1859 (or (eq ?\s (aref (buffer-name (car tail)) 0))
1860 (setq maxlen
1861 (max maxlen
1862 (length (buffer-name (car tail))))))
1863 (setq tail (cdr tail)))
1864 (setq tail buffers)
1865 (while tail
1866 (let ((elt (car tail)))
1867 (if (/= (aref (buffer-name elt) 0) ?\s)
1868 (setq head
1869 (cons
1870 (cons
1871 (format
1872 (format "%%-%ds %%s%%s %%s" maxlen)
1873 (buffer-name elt)
1874 (if (buffer-modified-p elt) "*" " ")
1875 (with-current-buffer elt
1876 (if buffer-read-only "%" " "))
1877 (or (buffer-file-name elt)
1878 (with-current-buffer elt
1879 (if list-buffers-directory
1880 (expand-file-name
1881 list-buffers-directory)))
1882 ""))
1883 elt)
1884 head))))
1885 (setq tail (cdr tail)))
1886 ;; Compensate for the reversal that the above loop does.
1887 (nreverse head)))
1888
1889 (defun mouse-buffer-menu-split (title alist)
1890 ;; If we have lots of buffers, divide them into groups of 20
1891 ;; and make a pane (or submenu) for each one.
1892 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1893 (let ((alist alist) sublists next
1894 (i 1))
1895 (while alist
1896 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1897 ;; and make them the next element of sublist.
1898 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1899 (if next
1900 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1901 nil))
1902 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1903 sublists))
1904 (setq i (1+ i))
1905 (setq alist next))
1906 (nreverse sublists))
1907 ;; Few buffers--put them all in one pane.
1908 (list (cons title alist))))
1909 \f
1910 ;; These need to be rewritten for the new scroll bar implementation.
1911
1912 ;;!! ;; Commands for the scroll bar.
1913 ;;!!
1914 ;;!! (defun mouse-scroll-down (click)
1915 ;;!! (interactive "@e")
1916 ;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1917 ;;!!
1918 ;;!! (defun mouse-scroll-up (click)
1919 ;;!! (interactive "@e")
1920 ;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1921 ;;!!
1922 ;;!! (defun mouse-scroll-down-full ()
1923 ;;!! (interactive "@")
1924 ;;!! (scroll-down nil))
1925 ;;!!
1926 ;;!! (defun mouse-scroll-up-full ()
1927 ;;!! (interactive "@")
1928 ;;!! (scroll-up nil))
1929 ;;!!
1930 ;;!! (defun mouse-scroll-move-cursor (click)
1931 ;;!! (interactive "@e")
1932 ;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1933 ;;!!
1934 ;;!! (defun mouse-scroll-absolute (event)
1935 ;;!! (interactive "@e")
1936 ;;!! (let* ((pos (car event))
1937 ;;!! (position (car pos))
1938 ;;!! (length (car (cdr pos))))
1939 ;;!! (if (<= length 0) (setq length 1))
1940 ;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1941 ;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1942 ;;!! position)
1943 ;;!! length)
1944 ;;!! scale-factor)))
1945 ;;!! (goto-char newpos)
1946 ;;!! (recenter '(4)))))
1947 ;;!!
1948 ;;!! (defun mouse-scroll-left (click)
1949 ;;!! (interactive "@e")
1950 ;;!! (scroll-left (1+ (car (mouse-coords click)))))
1951 ;;!!
1952 ;;!! (defun mouse-scroll-right (click)
1953 ;;!! (interactive "@e")
1954 ;;!! (scroll-right (1+ (car (mouse-coords click)))))
1955 ;;!!
1956 ;;!! (defun mouse-scroll-left-full ()
1957 ;;!! (interactive "@")
1958 ;;!! (scroll-left nil))
1959 ;;!!
1960 ;;!! (defun mouse-scroll-right-full ()
1961 ;;!! (interactive "@")
1962 ;;!! (scroll-right nil))
1963 ;;!!
1964 ;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1965 ;;!! (interactive "@e")
1966 ;;!! (move-to-column (1+ (car (mouse-coords click)))))
1967 ;;!!
1968 ;;!! (defun mouse-scroll-absolute-horizontally (event)
1969 ;;!! (interactive "@e")
1970 ;;!! (let* ((pos (car event))
1971 ;;!! (position (car pos))
1972 ;;!! (length (car (cdr pos))))
1973 ;;!! (set-window-hscroll (selected-window) 33)))
1974 ;;!!
1975 ;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1976 ;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1977 ;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1978 ;;!!
1979 ;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1980 ;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1981 ;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1982 ;;!!
1983 ;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1984 ;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1985 ;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1986 ;;!!
1987 ;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1988 ;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1989 ;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1990 ;;!!
1991 ;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1992 ;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1993 ;;!! 'mouse-scroll-absolute-horizontally)
1994 ;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1995 ;;!!
1996 ;;!! (global-set-key [horizontal-slider mouse-1]
1997 ;;!! 'mouse-scroll-move-cursor-horizontally)
1998 ;;!! (global-set-key [horizontal-slider mouse-2]
1999 ;;!! 'mouse-scroll-move-cursor-horizontally)
2000 ;;!! (global-set-key [horizontal-slider mouse-3]
2001 ;;!! 'mouse-scroll-move-cursor-horizontally)
2002 ;;!!
2003 ;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
2004 ;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
2005 ;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
2006 ;;!!
2007 ;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
2008 ;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
2009 ;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
2010 ;;!!
2011 ;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
2012 ;;!! 'mouse-split-window-horizontally)
2013 ;;!! (global-set-key [mode-line S-mouse-2]
2014 ;;!! 'mouse-split-window-horizontally)
2015 ;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
2016 ;;!! 'mouse-split-window)
2017 \f
2018 ;;!! ;;;;
2019 ;;!! ;;;; Here are experimental things being tested. Mouse events
2020 ;;!! ;;;; are of the form:
2021 ;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
2022 ;;!! ;;
2023 ;;!! ;;;;
2024 ;;!! ;;;; Dynamically track mouse coordinates
2025 ;;!! ;;;;
2026 ;;!! ;;
2027 ;;!! ;;(defun track-mouse (event)
2028 ;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
2029 ;;!! ;; (interactive "@e")
2030 ;;!! ;; (while mouse-grabbed
2031 ;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
2032 ;;!! ;; (abs-x (car pos))
2033 ;;!! ;; (abs-y (cdr pos))
2034 ;;!! ;; (relative-coordinate (coordinates-in-window-p
2035 ;;!! ;; (list (car pos) (cdr pos))
2036 ;;!! ;; (selected-window))))
2037 ;;!! ;; (if (consp relative-coordinate)
2038 ;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
2039 ;;!! ;; (car relative-coordinate)
2040 ;;!! ;; (car (cdr relative-coordinate)))
2041 ;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
2042 ;;!!
2043 ;;!! ;;
2044 ;;!! ;; Dynamically put a box around the line indicated by point
2045 ;;!! ;;
2046 ;;!! ;;
2047 ;;!! ;;(require 'backquote)
2048 ;;!! ;;
2049 ;;!! ;;(defun mouse-select-buffer-line (event)
2050 ;;!! ;; (interactive "@e")
2051 ;;!! ;; (let ((relative-coordinate
2052 ;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
2053 ;;!! ;; (abs-y (car (cdr (car event)))))
2054 ;;!! ;; (if (consp relative-coordinate)
2055 ;;!! ;; (progn
2056 ;;!! ;; (save-excursion
2057 ;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2058 ;;!! ;; (x-draw-rectangle
2059 ;;!! ;; (selected-screen)
2060 ;;!! ;; abs-y 0
2061 ;;!! ;; (save-excursion
2062 ;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2063 ;;!! ;; (end-of-line)
2064 ;;!! ;; (push-mark nil t)
2065 ;;!! ;; (beginning-of-line)
2066 ;;!! ;; (- (region-end) (region-beginning))) 1))
2067 ;;!! ;; (sit-for 1)
2068 ;;!! ;; (x-erase-rectangle (selected-screen))))))
2069 ;;!! ;;
2070 ;;!! ;;(defvar last-line-drawn nil)
2071 ;;!! ;;(defvar begin-delim "[^ \t]")
2072 ;;!! ;;(defvar end-delim "[^ \t]")
2073 ;;!! ;;
2074 ;;!! ;;(defun mouse-boxing (event)
2075 ;;!! ;; (interactive "@e")
2076 ;;!! ;; (save-excursion
2077 ;;!! ;; (let ((screen (selected-screen)))
2078 ;;!! ;; (while (= (x-mouse-events) 0)
2079 ;;!! ;; (let* ((pos (read-mouse-position screen))
2080 ;;!! ;; (abs-x (car pos))
2081 ;;!! ;; (abs-y (cdr pos))
2082 ;;!! ;; (relative-coordinate
2083 ;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
2084 ;;!! ;; (selected-window)))
2085 ;;!! ;; (begin-reg nil)
2086 ;;!! ;; (end-reg nil)
2087 ;;!! ;; (end-column nil)
2088 ;;!! ;; (begin-column nil))
2089 ;;!! ;; (if (and (consp relative-coordinate)
2090 ;;!! ;; (or (not last-line-drawn)
2091 ;;!! ;; (not (= last-line-drawn abs-y))))
2092 ;;!! ;; (progn
2093 ;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
2094 ;;!! ;; (if (= (following-char) 10)
2095 ;;!! ;; ()
2096 ;;!! ;; (progn
2097 ;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
2098 ;;!! ;; (setq begin-column (1- (current-column)))
2099 ;;!! ;; (end-of-line)
2100 ;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
2101 ;;!! ;; (setq end-column (1+ (current-column)))
2102 ;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
2103 ;;!! ;; (x-draw-rectangle screen
2104 ;;!! ;; (setq last-line-drawn abs-y)
2105 ;;!! ;; begin-column
2106 ;;!! ;; (- end-column begin-column) 1))))))))))
2107 ;;!! ;;
2108 ;;!! ;;(defun mouse-erase-box ()
2109 ;;!! ;; (interactive)
2110 ;;!! ;; (if last-line-drawn
2111 ;;!! ;; (progn
2112 ;;!! ;; (x-erase-rectangle (selected-screen))
2113 ;;!! ;; (setq last-line-drawn nil))))
2114 ;;!!
2115 ;;!! ;;; (defun test-x-rectangle ()
2116 ;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
2117 ;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
2118 ;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
2119 ;;!!
2120 ;;!! ;;
2121 ;;!! ;; Here is how to do double clicking in lisp. About to change.
2122 ;;!! ;;
2123 ;;!!
2124 ;;!! (defvar double-start nil)
2125 ;;!! (defconst double-click-interval 300
2126 ;;!! "Max ticks between clicks")
2127 ;;!!
2128 ;;!! (defun double-down (event)
2129 ;;!! (interactive "@e")
2130 ;;!! (if double-start
2131 ;;!! (let ((interval (- (nth 4 event) double-start)))
2132 ;;!! (if (< interval double-click-interval)
2133 ;;!! (progn
2134 ;;!! (backward-up-list 1)
2135 ;;!! ;; (message "Interval %d" interval)
2136 ;;!! (sleep-for 1)))
2137 ;;!! (setq double-start nil))
2138 ;;!! (setq double-start (nth 4 event))))
2139 ;;!!
2140 ;;!! (defun double-up (event)
2141 ;;!! (interactive "@e")
2142 ;;!! (and double-start
2143 ;;!! (> (- (nth 4 event ) double-start) double-click-interval)
2144 ;;!! (setq double-start nil)))
2145 ;;!!
2146 ;;!! ;;; (defun x-test-doubleclick ()
2147 ;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
2148 ;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
2149 ;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
2150 ;;!!
2151 ;;!! ;;
2152 ;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
2153 ;;!! ;;
2154 ;;!!
2155 ;;!! (defvar scrolled-lines 0)
2156 ;;!! (defconst scroll-speed 1)
2157 ;;!!
2158 ;;!! (defun incr-scroll-down (event)
2159 ;;!! (interactive "@e")
2160 ;;!! (setq scrolled-lines 0)
2161 ;;!! (incremental-scroll scroll-speed))
2162 ;;!!
2163 ;;!! (defun incr-scroll-up (event)
2164 ;;!! (interactive "@e")
2165 ;;!! (setq scrolled-lines 0)
2166 ;;!! (incremental-scroll (- scroll-speed)))
2167 ;;!!
2168 ;;!! (defun incremental-scroll (n)
2169 ;;!! (while (= (x-mouse-events) 0)
2170 ;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
2171 ;;!! (scroll-down n)
2172 ;;!! (sit-for 300 t)))
2173 ;;!!
2174 ;;!! (defun incr-scroll-stop (event)
2175 ;;!! (interactive "@e")
2176 ;;!! (message "Scrolled %d lines" scrolled-lines)
2177 ;;!! (setq scrolled-lines 0)
2178 ;;!! (sleep-for 1))
2179 ;;!!
2180 ;;!! ;;; (defun x-testing-scroll ()
2181 ;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
2182 ;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
2183 ;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
2184 ;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
2185 ;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
2186 ;;!!
2187 ;;!! ;;
2188 ;;!! ;; Some playthings suitable for picture mode? They need work.
2189 ;;!! ;;
2190 ;;!!
2191 ;;!! (defun mouse-kill-rectangle (event)
2192 ;;!! "Kill the rectangle between point and the mouse cursor."
2193 ;;!! (interactive "@e")
2194 ;;!! (let ((point-save (point)))
2195 ;;!! (save-excursion
2196 ;;!! (mouse-set-point event)
2197 ;;!! (push-mark nil t)
2198 ;;!! (if (> point-save (point))
2199 ;;!! (kill-rectangle (point) point-save)
2200 ;;!! (kill-rectangle point-save (point))))))
2201 ;;!!
2202 ;;!! (defun mouse-open-rectangle (event)
2203 ;;!! "Kill the rectangle between point and the mouse cursor."
2204 ;;!! (interactive "@e")
2205 ;;!! (let ((point-save (point)))
2206 ;;!! (save-excursion
2207 ;;!! (mouse-set-point event)
2208 ;;!! (push-mark nil t)
2209 ;;!! (if (> point-save (point))
2210 ;;!! (open-rectangle (point) point-save)
2211 ;;!! (open-rectangle point-save (point))))))
2212 ;;!!
2213 ;;!! ;; Must be a better way to do this.
2214 ;;!!
2215 ;;!! (defun mouse-multiple-insert (n char)
2216 ;;!! (while (> n 0)
2217 ;;!! (insert char)
2218 ;;!! (setq n (1- n))))
2219 ;;!!
2220 ;;!! ;; What this could do is not finalize until button was released.
2221 ;;!!
2222 ;;!! (defun mouse-move-text (event)
2223 ;;!! "Move text from point to cursor position, inserting spaces."
2224 ;;!! (interactive "@e")
2225 ;;!! (let* ((relative-coordinate
2226 ;;!! (coordinates-in-window-p (car event) (selected-window))))
2227 ;;!! (if (consp relative-coordinate)
2228 ;;!! (cond ((> (current-column) (car relative-coordinate))
2229 ;;!! (delete-char
2230 ;;!! (- (car relative-coordinate) (current-column))))
2231 ;;!! ((< (current-column) (car relative-coordinate))
2232 ;;!! (mouse-multiple-insert
2233 ;;!! (- (car relative-coordinate) (current-column)) " "))
2234 ;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
2235 \f
2236 (define-obsolete-function-alias
2237 'mouse-choose-completion 'choose-completion "23.2")
2238
2239 ;; Font selection.
2240
2241 (defun font-menu-add-default ()
2242 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2243 (font-alist x-fixed-font-alist)
2244 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2245 (if (assoc "Default" elt)
2246 (delete (assoc "Default" elt) elt))
2247 (setcdr elt
2248 (cons (list "Default" default)
2249 (cdr elt)))))
2250
2251 (defvar x-fixed-font-alist
2252 (list
2253 (purecopy "Font Menu")
2254 (cons
2255 (purecopy "Misc")
2256 (mapcar
2257 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2258 ;; For these, we specify the pixel height and width.
2259 '(("fixed" "fixed")
2260 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2261 ("6x12"
2262 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2263 ("6x13"
2264 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2265 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2266 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2267 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2268 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2269 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2270 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2271 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2272 ("")
2273 ("clean 5x8"
2274 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2275 ("clean 6x8"
2276 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2277 ("clean 8x8"
2278 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2279 ("clean 8x10"
2280 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2281 ("clean 8x14"
2282 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2283 ("clean 8x16"
2284 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2285 ("")
2286 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2287 ;; We don't seem to have these; who knows what they are.
2288 ;; ("fg-18" "fg-18")
2289 ;; ("fg-25" "fg-25")
2290 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2291 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2292 ("lucidasanstypewriter-bold-24"
2293 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2294 ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2295 ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2296 )))
2297
2298 (cons
2299 (purecopy "Courier")
2300 (mapcar
2301 (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
2302 ;; For these, we specify the point height.
2303 '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2304 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2305 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2306 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2307 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2308 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2309 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2310 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2311 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2312 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2313 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2314 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2315 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2316 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2317 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2318 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2319 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2320 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2321 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2322 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2323 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2324 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2325 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2326 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
2327 ))))
2328 "X fonts suitable for use in Emacs.")
2329
2330 (declare-function generate-fontset-menu "fontset" ())
2331
2332 (defun mouse-select-font ()
2333 "Prompt for a font name, using `x-popup-menu', and return it."
2334 (interactive)
2335 (unless (display-multi-font-p)
2336 (error "Cannot change fonts on this display"))
2337 (car
2338 (x-popup-menu
2339 (if (listp last-nonmenu-event)
2340 last-nonmenu-event
2341 (list '(0 0) (selected-window)))
2342 (append x-fixed-font-alist
2343 (list (generate-fontset-menu))))))
2344
2345 (declare-function text-scale-mode "face-remap")
2346
2347 (defun mouse-set-font (&rest fonts)
2348 "Set the default font for the selected frame.
2349 The argument FONTS is a list of font names; the first valid font
2350 in this list is used.
2351
2352 When called interactively, pop up a menu and allow the user to
2353 choose a font."
2354 (interactive
2355 (progn (unless (display-multi-font-p)
2356 (error "Cannot change fonts on this display"))
2357 (x-popup-menu
2358 (if (listp last-nonmenu-event)
2359 last-nonmenu-event
2360 (list '(0 0) (selected-window)))
2361 ;; Append list of fontsets currently defined.
2362 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2363 (if fonts
2364 (let (font)
2365 (while fonts
2366 (condition-case nil
2367 (progn
2368 (set-frame-font (car fonts))
2369 (setq font (car fonts))
2370 (setq fonts nil))
2371 (error
2372 (setq fonts (cdr fonts)))))
2373 (if (null font)
2374 (error "Font not found")))))
2375
2376 (defvar mouse-appearance-menu-map nil)
2377 (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
2378 (declare-function buffer-face-mode-invoke "face-remap"
2379 (face arg &optional interactive))
2380 (declare-function font-face-attributes "font.c" (font &optional frame))
2381
2382 (defun mouse-appearance-menu (event)
2383 "Show a menu for changing the default face in the current buffer."
2384 (interactive "@e")
2385 (require 'face-remap)
2386 (when (display-multi-font-p)
2387 (with-selected-window (car (event-start event))
2388 (if mouse-appearance-menu-map
2389 nil ; regenerate new fonts
2390 ;; Initialize mouse-appearance-menu-map
2391 (setq mouse-appearance-menu-map
2392 (make-sparse-keymap "Change Default Buffer Face"))
2393 (define-key mouse-appearance-menu-map [face-remap-reset-base]
2394 '(menu-item "Reset to Default" face-remap-reset-base))
2395 (define-key mouse-appearance-menu-map [text-scale-decrease]
2396 '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
2397 (define-key mouse-appearance-menu-map [text-scale-increase]
2398 '(menu-item "Increase Buffer Text Size" text-scale-increase))
2399 ;; Font selector
2400 (if (functionp 'x-select-font)
2401 (define-key mouse-appearance-menu-map [x-select-font]
2402 '(menu-item "Change Buffer Font..." x-select-font))
2403 ;; If the select-font is unavailable, construct a menu.
2404 (let ((font-submenu (make-sparse-keymap "Change Text Font"))
2405 (font-alist (cdr (append x-fixed-font-alist
2406 (list (generate-fontset-menu))))))
2407 (dolist (family font-alist)
2408 (let* ((submenu-name (car family))
2409 (submenu-map (make-sparse-keymap submenu-name)))
2410 (dolist (font (cdr family))
2411 (let ((font-name (car font))
2412 font-symbol)
2413 (if (string= font-name "")
2414 (define-key submenu-map [space]
2415 '("--"))
2416 (setq font-symbol (intern (cadr font)))
2417 (define-key submenu-map (vector font-symbol)
2418 (list 'menu-item (car font) font-symbol)))))
2419 (define-key font-submenu (vector (intern submenu-name))
2420 (list 'menu-item submenu-name submenu-map))))
2421 (define-key mouse-appearance-menu-map [font-submenu]
2422 (list 'menu-item "Change Text Font" font-submenu)))))
2423 (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
2424 (setq choice (nth (1- (length choice)) choice))
2425 (cond ((eq choice 'text-scale-increase)
2426 (text-scale-increase 1))
2427 ((eq choice 'text-scale-decrease)
2428 (text-scale-increase -1))
2429 ((eq choice 'face-remap-reset-base)
2430 (text-scale-mode 0)
2431 (buffer-face-mode 0))
2432 (choice
2433 ;; Either choice == 'x-select-font, or choice is a
2434 ;; symbol whose name is a font.
2435 (buffer-face-mode-invoke (font-face-attributes
2436 (if (eq choice 'x-select-font)
2437 (x-select-font)
2438 (symbol-name choice)))
2439 t
2440 (called-interactively-p 'interactive))))))))
2441
2442 \f
2443 ;;; Bindings for mouse commands.
2444
2445 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2446 (global-set-key [mouse-1] 'mouse-set-point)
2447 (global-set-key [drag-mouse-1] 'mouse-set-region)
2448
2449 ;; These are tested for in mouse-drag-region.
2450 (global-set-key [double-mouse-1] 'mouse-set-point)
2451 (global-set-key [triple-mouse-1] 'mouse-set-point)
2452
2453 ;; Clicking on the fringes causes hscrolling:
2454 (global-set-key [left-fringe mouse-1] 'mouse-set-point)
2455 (global-set-key [right-fringe mouse-1] 'mouse-set-point)
2456
2457 (global-set-key [mouse-2] 'mouse-yank-primary)
2458 ;; Allow yanking also when the corresponding cursor is "in the fringe".
2459 (global-set-key [right-fringe mouse-2] 'mouse-yank-at-click)
2460 (global-set-key [left-fringe mouse-2] 'mouse-yank-at-click)
2461 (global-set-key [mouse-3] 'mouse-save-then-kill)
2462 (global-set-key [right-fringe mouse-3] 'mouse-save-then-kill)
2463 (global-set-key [left-fringe mouse-3] 'mouse-save-then-kill)
2464
2465 ;; By binding these to down-going events, we let the user use the up-going
2466 ;; event to make the selection, saving a click.
2467 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2468 (if (not (eq system-type 'ms-dos))
2469 (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
2470 ;; C-down-mouse-2 is bound in facemenu.el.
2471 (global-set-key [C-down-mouse-3]
2472 `(menu-item ,(purecopy "Menu Bar") ignore
2473 :filter (lambda (_)
2474 (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
2475 (mouse-menu-bar-map)
2476 (mouse-menu-major-mode-map)))))
2477
2478
2479 ;; Replaced with dragging mouse-1
2480 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2481
2482 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2483 ;; vertical-line prevents Emacs from signaling an error when the mouse
2484 ;; button is released after dragging these lines, on non-toolkit
2485 ;; versions.
2486 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2487 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2488 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2489 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2490 (global-set-key [header-line mouse-1] 'mouse-select-window)
2491 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2492 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2493 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2494 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2495 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2496 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2497 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2498
2499 (provide 'mouse)
2500
2501 ;; This file contains the functionality of the old mldrag.el.
2502 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2503 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2504 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2505 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2506 (provide 'mldrag)
2507
2508 ;; arch-tag: 9a710ce1-914a-4923-9b81-697f7bf82ab3
2509 ;;; mouse.el ends here