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