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