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