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