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