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