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