(mouse-major-mode-menu): Use local-key-binding.
[emacs.git] / lisp / mouse.el
blob8cc878d2f90ed9c952a1da707bec3e14dc27d8f0
1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: hardware, mouse
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package provides various useful commands (including help
28 ;; system access) through the mouse. All this code assumes that mouse
29 ;; interpretation has been abstracted into Emacs input events.
31 ;; The code is rather X-dependent.
33 ;;; Code:
35 ;;; Utility functions.
37 ;;; Indent track-mouse like progn.
38 (put 'track-mouse 'lisp-indent-function 0)
40 (defcustom mouse-yank-at-point nil
41 "*If non-nil, mouse yank commands yank at point instead of at click."
42 :type 'boolean
43 :group 'mouse)
45 ;; Provide a mode-specific menu on a mouse button.
47 (defun popup-menu (menu &optional position prefix)
48 "Popup the given menu and call the selected option.
49 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
50 `x-popup-menu'.
51 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
52 the current mouse position.
53 PREFIX is the prefix argument (if any) to pass to the command."
54 (let* ((map (cond
55 ((keymapp menu) menu)
56 ((and (listp menu) (keymapp (car menu))) menu)
57 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
58 (filter (when (symbolp map)
59 (plist-get (get map 'menu-pro) :filter))))
60 (if filter (funcall filter (symbol-function map)) map)))))
61 event)
62 ;; The looping behavior was taken from lmenu's popup-menu-popup
63 (while (and map (setq event
64 ;; map could be a prefix key, in which case
65 ;; we need to get its function cell
66 ;; definition.
67 (x-popup-menu position (indirect-function map))))
68 ;; Strangely x-popup-menu returns a list.
69 ;; mouse-major-mode-menu was using a weird:
70 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
71 (let ((cmd
72 (if (and (not (keymapp map)) (listp map))
73 ;; We were given a list of keymaps. Search them all
74 ;; in sequence until a first binding is found.
75 (let ((mouse-click (apply 'vector event))
76 binding)
77 (while (and map (null binding))
78 (setq binding (lookup-key (car map) mouse-click))
79 (if (numberp binding) ; `too long'
80 (setq binding nil))
81 (setq map (cdr map)))
82 binding)
83 ;; We were given a single keymap.
84 (lookup-key map (apply 'vector event)))))
85 (setq map nil)
86 ;; Clear out echoing, which perhaps shows a prefix arg.
87 (message "")
88 (when cmd
89 (if (keymapp cmd)
90 ;; Try again but with the submap.
91 (setq map cmd)
92 (setq prefix-arg prefix)
93 ;; `setup-specified-language-environment', for instance,
94 ;; expects this to be set from a menu keymap.
95 (setq last-command-event (car (last event)))
96 ;; mouse-major-mode-menu was using `command-execute' instead.
97 (call-interactively cmd)))))))
99 (defvar mouse-major-mode-menu-prefix) ; dynamically bound
101 (defun mouse-major-mode-menu (event prefix)
102 "Pop up a mode-specific menu of mouse commands.
103 Default to the Edit menu if the major mode doesn't define a menu."
104 ;; Switch to the window clicked on, because otherwise
105 ;; the mode's commands may not make sense.
106 (interactive "@e\nP")
107 ;; Let the mode update its menus first.
108 (run-hooks 'activate-menubar-hook)
109 (let* (;; This is where mouse-major-mode-menu-prefix
110 ;; returns the prefix we should use (after menu-bar).
111 ;; It is either nil or (SOME-SYMBOL).
112 (mouse-major-mode-menu-prefix nil)
113 ;; Keymap from which to inherit; may be null.
114 (ancestor (mouse-major-mode-menu-1
115 (and (current-local-map)
116 (local-key-binding [menu-bar]))))
117 ;; Make a keymap in which our last command leads to a menu or
118 ;; default to the edit menu.
119 (newmap (if ancestor
120 (make-sparse-keymap (concat mode-name " Mode"))
121 menu-bar-edit-menu))
122 result)
123 (if ancestor
124 ;; Make our menu inherit from the desired keymap which we want
125 ;; to display as the menu now.
126 (set-keymap-parent newmap ancestor))
127 (popup-menu newmap event prefix)))
130 ;; Compute and cache the equivalent keys in MENU and all its submenus.
131 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
132 ;;; (and (eq (car menu) 'keymap)
133 ;;; (x-popup-menu nil menu))
134 ;;; (while menu
135 ;;; (and (consp (car menu))
136 ;;; (consp (cdr (car menu)))
137 ;;; (let ((tail (cdr (car menu))))
138 ;;; (while (and (consp tail)
139 ;;; (not (eq (car tail) 'keymap)))
140 ;;; (setq tail (cdr tail)))
141 ;;; (if (consp tail)
142 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
143 ;;; (setq menu (cdr menu))))
145 ;; Given a mode's menu bar keymap,
146 ;; if it defines exactly one menu bar menu,
147 ;; return just that menu.
148 ;; Otherwise return a menu for all of them.
149 (defun mouse-major-mode-menu-1 (menubar)
150 (if menubar
151 (let ((tail menubar)
152 submap)
153 (while tail
154 (if (consp (car tail))
155 (if submap
156 (setq submap t)
157 (setq submap (car tail))))
158 (setq tail (cdr tail)))
159 (if (eq submap t)
160 menubar
161 (setq mouse-major-mode-menu-prefix (list (car submap)))
162 (lookup-key menubar (vector (car submap)))))))
164 (defun mouse-popup-menubar (event prefix)
165 "Pops up a menu equiavlent to the menu bar a keyboard EVENT with PREFIX.
166 The contents are the items that would be in the menu bar whether or
167 not it is actually displayed."
168 (interactive "@e \nP")
169 (run-hooks 'activate-menubar-hook)
170 (let* ((local-menu (and (current-local-map)
171 (lookup-key (current-local-map) [menu-bar])))
172 (global-menu (lookup-key global-map [menu-bar]))
173 (local-title-or-map (and local-menu (cadr local-menu)))
174 (minor-mode-menus (mapcar #'cdr (minor-mode-key-binding [menu-bar])))
175 (global-title-or-map (cadr global-menu)))
176 ;; If the keymaps don't have prompt string (a lazy programmer
177 ;; didn't bother to provide one), create it and insert it into the
178 ;; keymaps; each keymap gets its own prompt. This is required for
179 ;; non-toolkit versions to display non-empty menu pane names.
180 (or (null local-menu)
181 (stringp local-title-or-map)
182 (setq local-menu (cons 'keymap
183 (cons (concat mode-name " Mode Menu")
184 (cdr local-menu)))))
185 (or (stringp global-title-or-map)
186 (setq global-menu (cons 'keymap
187 (cons "Global Menu"
188 (cdr global-menu)))))
189 ;; Supplying the list is faster than making a new map.
190 (popup-menu (append (list global-menu)
191 (if local-menu
192 (list local-menu))
193 minor-mode-menus)
194 event prefix)))
196 (defun mouse-popup-menubar-stuff (event prefix)
197 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
198 Use the former if the menu bar is showing, otherwise the latter."
199 (interactive "@e \nP")
200 (if (zerop (assoc-default 'menu-bar-lines (frame-parameters) 'eq 0))
201 (mouse-popup-menubar event prefix)
202 (mouse-major-mode-menu event prefix)))
204 ;; Commands that operate on windows.
206 (defun mouse-minibuffer-check (event)
207 (let ((w (posn-window (event-start event))))
208 (and (window-minibuffer-p w)
209 (not (minibuffer-window-active-p w))
210 (error "Minibuffer window is not active")))
211 ;; Give temporary modes such as isearch a chance to turn off.
212 (run-hooks 'mouse-leave-buffer-hook))
214 (defun mouse-delete-window (click)
215 "Delete the window you click on.
216 If the frame has just one window, bury the current buffer instead.
217 This command must be bound to a mouse click."
218 (interactive "e")
219 (if (one-window-p t)
220 (bury-buffer)
221 (mouse-minibuffer-check click)
222 (delete-window (posn-window (event-start click)))))
224 (defun mouse-select-window (click)
225 "Select the window clicked on; don't move point."
226 (interactive "e")
227 (mouse-minibuffer-check click)
228 (let ((oframe (selected-frame))
229 (frame (window-frame (posn-window (event-start click)))))
230 (select-window (posn-window (event-start click)))
231 (raise-frame frame)
232 (select-frame frame)
233 (or (eq frame oframe)
234 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
236 (defun mouse-tear-off-window (click)
237 "Delete the window clicked on, and create a new frame displaying its buffer."
238 (interactive "e")
239 (mouse-minibuffer-check click)
240 (let* ((window (posn-window (event-start click)))
241 (buf (window-buffer window))
242 (frame (make-frame)))
243 (select-frame frame)
244 (switch-to-buffer buf)
245 (delete-window window)))
247 (defun mouse-delete-other-windows ()
248 "Delete all window except the one you click on."
249 (interactive "@")
250 (delete-other-windows))
252 (defun mouse-split-window-vertically (click)
253 "Select Emacs window mouse is on, then split it vertically in half.
254 The window is split at the line clicked on.
255 This command must be bound to a mouse click."
256 (interactive "@e")
257 (mouse-minibuffer-check click)
258 (let ((start (event-start click)))
259 (select-window (posn-window start))
260 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
261 (first-line window-min-height)
262 (last-line (- (window-height) window-min-height)))
263 (if (< last-line first-line)
264 (error "Window too short to split")
265 (split-window-vertically
266 (min (max new-height first-line) last-line))))))
268 (defun mouse-split-window-horizontally (click)
269 "Select Emacs window mouse is on, then split it horizontally in half.
270 The window is split at the column clicked on.
271 This command must be bound to a mouse click."
272 (interactive "@e")
273 (mouse-minibuffer-check click)
274 (let ((start (event-start click)))
275 (select-window (posn-window start))
276 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
277 (first-col window-min-width)
278 (last-col (- (window-width) window-min-width)))
279 (if (< last-col first-col)
280 (error "Window too narrow to split")
281 (split-window-horizontally
282 (min (max new-width first-col) last-col))))))
284 (defun mouse-drag-mode-line-1 (start-event mode-line-p)
285 "Change the height of a window by dragging on the mode or header line.
286 START-EVENT is the starting mouse-event of the drag action.
287 MODE-LINE-P non-nil means a mode line is dragged."
288 ;; Give temporary modes such as isearch a chance to turn off.
289 (run-hooks 'mouse-leave-buffer-hook)
290 (let ((done nil)
291 (echo-keystrokes 0)
292 (start-event-frame (window-frame (car (car (cdr start-event)))))
293 (start-event-window (car (car (cdr start-event))))
294 (start-nwindows (count-windows t))
295 (old-selected-window (selected-window))
296 should-enlarge-minibuffer
297 event mouse minibuffer y top bot edges wconfig params growth)
298 (setq params (frame-parameters))
299 (setq minibuffer (cdr (assq 'minibuffer params)))
300 (track-mouse
301 (progn
302 ;; enlarge-window only works on the selected window, so
303 ;; we must select the window where the start event originated.
304 ;; unwind-protect will restore the old selected window later.
305 (select-window start-event-window)
306 ;; if this is the bottommost ordinary window, then to
307 ;; move its modeline the minibuffer must be enlarged.
308 (setq should-enlarge-minibuffer
309 (and minibuffer
310 mode-line-p
311 (not (one-window-p t))
312 (= (nth 1 (window-edges minibuffer))
313 (nth 3 (window-edges)))))
314 ;; loop reading events and sampling the position of
315 ;; the mouse.
316 (while (not done)
317 (setq event (read-event)
318 mouse (mouse-position))
319 ;; do nothing if
320 ;; - there is a switch-frame event.
321 ;; - the mouse isn't in the frame that we started in
322 ;; - the mouse isn't in any Emacs frame
323 ;; drag if
324 ;; - there is a mouse-movement event
325 ;; - there is a scroll-bar-movement event
326 ;; (same as mouse movement for our purposes)
327 ;; quit if
328 ;; - there is a keyboard event or some other unknown event
329 ;; unknown event.
330 (cond ((integerp event)
331 (setq done t))
332 ((eq (car event) 'switch-frame)
333 nil)
334 ((not (memq (car event)
335 '(mouse-movement scroll-bar-movement)))
336 (if (consp event)
337 (setq unread-command-events
338 (cons event unread-command-events)))
339 (setq done t))
340 ((not (eq (car mouse) start-event-frame))
341 nil)
342 ((null (car (cdr mouse)))
343 nil)
345 (setq y (cdr (cdr mouse))
346 edges (window-edges)
347 top (nth 1 edges)
348 bot (nth 3 edges))
350 ;; compute size change needed
351 (cond (mode-line-p
352 ;; Scale back a move that would make the
353 ;; window too short.
354 (when (< (- y top -1) window-min-height)
355 (setq y (+ top window-min-height -1)))
356 (setq growth (- y bot -1)))
358 (when (< (- bot y -1) window-min-height)
359 (setq y (- bot window-min-height -1)))
360 (setq growth (- top y -1))))
361 (setq wconfig (current-window-configuration))
363 ;; Check for an error case.
364 (if (and (/= growth 0)
365 (not minibuffer)
366 (one-window-p t))
367 (error "Attempt to resize sole window"))
369 ;; grow/shrink minibuffer?
370 (if should-enlarge-minibuffer
371 (progn
372 ;; yes. briefly select minibuffer so
373 ;; enlarge-window will affect the
374 ;; correct window.
375 (select-window minibuffer)
376 ;; scale back shrinkage if it would
377 ;; make the minibuffer less than 1
378 ;; line tall.
379 (if (and (> growth 0)
380 (< (- (window-height minibuffer)
381 growth)
383 (setq growth (1- (window-height minibuffer))))
384 (enlarge-window (- growth))
385 (select-window start-event-window))
386 ;; no. grow/shrink the selected window
387 ;; (message "growth = %d" growth)
388 (enlarge-window growth))
390 ;; if this window's growth caused another
391 ;; window to be deleted because it was too
392 ;; short, rescind the change.
394 ;; if size change caused space to be stolen
395 ;; from a window above this one, rescind the
396 ;; change, but only if we didn't grow/shrink
397 ;; the minibuffer. minibuffer size changes
398 ;; can cause all windows to shrink... no way
399 ;; around it.
400 (if (or (/= start-nwindows (count-windows t))
401 (and (not should-enlarge-minibuffer)
402 mode-line-p
403 (/= top (nth 1 (window-edges)))))
404 (set-window-configuration wconfig)))))))))
406 (defun mouse-drag-mode-line (start-event)
407 "Change the height of a window by dragging on the mode line."
408 (interactive "e")
409 (mouse-drag-mode-line-1 start-event t))
411 (defun mouse-drag-header-line (start-event)
412 "Change the height of a window by dragging on the header line."
413 (interactive "e")
414 (mouse-drag-mode-line-1 start-event nil))
417 (defun mouse-drag-vertical-line (start-event)
418 "Change the width of a window by dragging on the vertical line."
419 (interactive "e")
420 ;; Give temporary modes such as isearch a chance to turn off.
421 (run-hooks 'mouse-leave-buffer-hook)
422 (let* ((done nil)
423 (echo-keystrokes 0)
424 (start-event-frame (window-frame (car (car (cdr start-event)))))
425 (start-event-window (car (car (cdr start-event))))
426 (start-nwindows (count-windows t))
427 (old-selected-window (selected-window))
428 event mouse x left right edges wconfig growth
429 (which-side
430 (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
431 'right)))
432 (if (one-window-p t)
433 (error "Attempt to resize sole ordinary window"))
434 (if (eq which-side 'right)
435 (if (= (nth 2 (window-edges start-event-window))
436 (frame-width start-event-frame))
437 (error "Attempt to drag rightmost scrollbar"))
438 (if (= (nth 0 (window-edges start-event-window)) 0)
439 (error "Attempt to drag leftmost scrollbar")))
440 (track-mouse
441 (progn
442 ;; enlarge-window only works on the selected window, so
443 ;; we must select the window where the start event originated.
444 ;; unwind-protect will restore the old selected window later.
445 (select-window start-event-window)
446 ;; loop reading events and sampling the position of
447 ;; the mouse.
448 (while (not done)
449 (setq event (read-event)
450 mouse (mouse-position))
451 ;; do nothing if
452 ;; - there is a switch-frame event.
453 ;; - the mouse isn't in the frame that we started in
454 ;; - the mouse isn't in any Emacs frame
455 ;; drag if
456 ;; - there is a mouse-movement event
457 ;; - there is a scroll-bar-movement event
458 ;; (same as mouse movement for our purposes)
459 ;; quit if
460 ;; - there is a keyboard event or some other unknown event
461 ;; unknown event.
462 (cond ((integerp event)
463 (setq done t))
464 ((eq (car event) 'switch-frame)
465 nil)
466 ((not (memq (car event)
467 '(mouse-movement scroll-bar-movement)))
468 (if (consp event)
469 (setq unread-command-events
470 (cons event unread-command-events)))
471 (setq done t))
472 ((not (eq (car mouse) start-event-frame))
473 nil)
474 ((null (car (cdr mouse)))
475 nil)
477 (save-selected-window
478 ;; If the scroll bar is on the window's left,
479 ;; adjust the window on the left.
480 (unless (eq which-side 'right)
481 (select-window (previous-window)))
482 (setq x (- (car (cdr mouse))
483 (if (eq which-side 'right) 0 2))
484 edges (window-edges)
485 left (nth 0 edges)
486 right (nth 2 edges))
487 ;; scale back a move that would make the
488 ;; window too thin.
489 (if (< (- x left -1) window-min-width)
490 (setq x (+ left window-min-width -1)))
491 ;; compute size change needed
492 (setq growth (- x right -1)
493 wconfig (current-window-configuration))
494 (enlarge-window growth t)
495 ;; if this window's growth caused another
496 ;; window to be deleted because it was too
497 ;; thin, rescind the change.
499 ;; if size change caused space to be stolen
500 ;; from a window to the left of this one,
501 ;; rescind the change.
502 (if (or (/= start-nwindows (count-windows t))
503 (/= left (nth 0 (window-edges))))
504 (set-window-configuration wconfig))))))))))
506 (defun mouse-set-point (event)
507 "Move point to the position clicked on with the mouse.
508 This should be bound to a mouse click event type."
509 (interactive "e")
510 (mouse-minibuffer-check event)
511 ;; Use event-end in case called from mouse-drag-region.
512 ;; If EVENT is a click, event-end and event-start give same value.
513 (let ((posn (event-end event)))
514 (if (not (windowp (posn-window posn)))
515 (error "Cursor not in text area of window"))
516 (select-window (posn-window posn))
517 (if (numberp (posn-point posn))
518 (goto-char (posn-point posn)))))
520 (defvar mouse-last-region-beg nil)
521 (defvar mouse-last-region-end nil)
522 (defvar mouse-last-region-tick nil)
524 (defun mouse-region-match ()
525 "Return non-nil if there's an active region that was set with the mouse."
526 (and (mark t) mark-active
527 (eq mouse-last-region-beg (region-beginning))
528 (eq mouse-last-region-end (region-end))
529 (eq mouse-last-region-tick (buffer-modified-tick))))
531 (defun mouse-set-region (click)
532 "Set the region to the text dragged over, and copy to kill ring.
533 This should be bound to a mouse drag event."
534 (interactive "e")
535 (mouse-minibuffer-check click)
536 (let ((posn (event-start click))
537 (end (event-end click)))
538 (select-window (posn-window posn))
539 (if (numberp (posn-point posn))
540 (goto-char (posn-point posn)))
541 ;; If mark is highlighted, no need to bounce the cursor.
542 ;; On X, we highlight while dragging, thus once again no need to bounce.
543 (or transient-mark-mode
544 (memq (framep (selected-frame)) '(x pc w32))
545 (sit-for 1))
546 (push-mark)
547 (set-mark (point))
548 (if (numberp (posn-point end))
549 (goto-char (posn-point end)))
550 ;; Don't set this-command to kill-region, so that a following
551 ;; C-w will not double the text in the kill ring.
552 ;; Ignore last-command so we don't append to a preceding kill.
553 (let (this-command last-command deactivate-mark)
554 (copy-region-as-kill (mark) (point)))
555 (mouse-set-region-1)))
557 (defun mouse-set-region-1 ()
558 (setq mouse-last-region-beg (region-beginning))
559 (setq mouse-last-region-end (region-end))
560 (setq mouse-last-region-tick (buffer-modified-tick)))
562 (defcustom mouse-scroll-delay 0.25
563 "*The pause between scroll steps caused by mouse drags, in seconds.
564 If you drag the mouse beyond the edge of a window, Emacs scrolls the
565 window to bring the text beyond that edge into view, with a delay of
566 this many seconds between scroll steps. Scrolling stops when you move
567 the mouse back into the window, or release the button.
568 This variable's value may be non-integral.
569 Setting this to zero causes Emacs to scroll as fast as it can."
570 :type 'number
571 :group 'mouse)
573 (defcustom mouse-scroll-min-lines 1
574 "*The minimum number of lines scrolled by dragging mouse out of window.
575 Moving the mouse out the top or bottom edge of the window begins
576 scrolling repeatedly. The number of lines scrolled per repetition
577 is normally equal to the number of lines beyond the window edge that
578 the mouse has moved. However, it always scrolls at least the number
579 of lines specified by this variable."
580 :type 'integer
581 :group 'mouse)
583 (defun mouse-scroll-subr (window jump &optional overlay start)
584 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
585 If OVERLAY is an overlay, let it stretch from START to the far edge of
586 the newly visible text.
587 Upon exit, point is at the far edge of the newly visible text."
588 (cond
589 ((and (> jump 0) (< jump mouse-scroll-min-lines))
590 (setq jump mouse-scroll-min-lines))
591 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
592 (setq jump (- mouse-scroll-min-lines))))
593 (let ((opoint (point)))
594 (while (progn
595 (goto-char (window-start window))
596 (if (not (zerop (vertical-motion jump window)))
597 (progn
598 (set-window-start window (point))
599 (if (natnump jump)
600 (if (window-end window)
601 (progn
602 (goto-char (window-end window))
603 ;; window-end doesn't reflect the window's new
604 ;; start position until the next redisplay.
605 (vertical-motion (1- jump) window))
606 (vertical-motion (- (window-height window) 2)))
607 (goto-char (window-start window)))
608 (if overlay
609 (move-overlay overlay start (point)))
610 ;; Now that we have scrolled WINDOW properly,
611 ;; put point back where it was for the redisplay
612 ;; so that we don't mess up the selected window.
613 (or (eq window (selected-window))
614 (goto-char opoint))
615 (sit-for mouse-scroll-delay)))))
616 (or (eq window (selected-window))
617 (goto-char opoint))))
619 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
620 (defvar mouse-drag-overlay (make-overlay 1 1))
621 (delete-overlay mouse-drag-overlay)
622 (overlay-put mouse-drag-overlay 'face 'region)
624 (defvar mouse-selection-click-count 0)
626 (defvar mouse-selection-click-count-buffer nil)
628 (defun mouse-drag-region (start-event)
629 "Set the region to the text that the mouse is dragged over.
630 Highlight the drag area as you move the mouse.
631 This must be bound to a button-down mouse event.
632 In Transient Mark mode, the highlighting remains as long as the mark
633 remains active. Otherwise, it remains until the next input event."
634 (interactive "e")
635 (mouse-minibuffer-check start-event)
636 (let* ((echo-keystrokes 0)
637 (start-posn (event-start start-event))
638 (start-point (posn-point start-posn))
639 (start-window (posn-window start-posn))
640 (start-frame (window-frame start-window))
641 (start-hscroll (window-hscroll start-window))
642 (bounds (window-edges start-window))
643 (top (nth 1 bounds))
644 (bottom (if (window-minibuffer-p start-window)
645 (nth 3 bounds)
646 ;; Don't count the mode line.
647 (1- (nth 3 bounds))))
648 (click-count (1- (event-click-count start-event))))
649 (setq mouse-selection-click-count click-count)
650 (setq mouse-selection-click-count-buffer (current-buffer))
651 (mouse-set-point start-event)
652 ;; In case the down click is in the middle of some intangible text,
653 ;; use the end of that text, and put it in START-POINT.
654 (if (< (point) start-point)
655 (goto-char start-point))
656 (setq start-point (point))
657 (let ((range (mouse-start-end start-point start-point click-count)))
658 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
659 (window-buffer start-window)))
660 (deactivate-mark)
661 ;; end-of-range is used only in the single-click case.
662 ;; It is the place where the drag has reached so far
663 ;; (but not outside the window where the drag started).
664 (let (event end end-point last-end-point (end-of-range (point)))
665 (track-mouse
666 (while (progn
667 (setq event (read-event))
668 (or (mouse-movement-p event)
669 (eq (car-safe event) 'switch-frame)))
670 (if (eq (car-safe event) 'switch-frame)
672 (setq end (event-end event)
673 end-point (posn-point end))
674 (if (numberp end-point)
675 (setq last-end-point end-point))
677 (cond
678 ;; Are we moving within the original window?
679 ((and (eq (posn-window end) start-window)
680 (integer-or-marker-p end-point))
681 ;; Go to START-POINT first, so that when we move to END-POINT,
682 ;; if it's in the middle of intangible text,
683 ;; point jumps in the direction away from START-POINT.
684 (goto-char start-point)
685 (goto-char end-point)
686 (if (zerop (% click-count 3))
687 (setq end-of-range (point)))
688 (let ((range (mouse-start-end start-point (point) click-count)))
689 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
692 (let ((mouse-row (cdr (cdr (mouse-position)))))
693 (cond
694 ((null mouse-row))
695 ((< mouse-row top)
696 (mouse-scroll-subr start-window (- mouse-row top)
697 mouse-drag-overlay start-point)
698 ;; Without this, point tends to jump back to the starting
699 ;; position where the mouse button was pressed down.
700 (setq end-of-range (overlay-start mouse-drag-overlay)))
701 ((>= mouse-row bottom)
702 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
703 mouse-drag-overlay start-point)
704 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
705 ;; In case we did not get a mouse-motion event
706 ;; for the final move of the mouse before a drag event
707 ;; pretend that we did get one.
708 (when (and (memq 'drag (event-modifiers (car-safe event)))
709 (setq end (event-end event)
710 end-point (posn-point end))
711 (eq (posn-window end) start-window)
712 (integer-or-marker-p end-point))
714 ;; Go to START-POINT first, so that when we move to END-POINT,
715 ;; if it's in the middle of intangible text,
716 ;; point jumps in the direction away from START-POINT.
717 (goto-char start-point)
718 (goto-char end-point)
719 (if (zerop (% click-count 3))
720 (setq end-of-range (point)))
721 (let ((range (mouse-start-end start-point (point) click-count)))
722 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
724 (if (consp event)
725 (let ((fun (key-binding (vector (car event)))))
726 ;; Run the binding of the terminating up-event, if possible.
727 ;; In the case of a multiple click, it gives the wrong results,
728 ;; because it would fail to set up a region.
729 (if (not (= (overlay-start mouse-drag-overlay)
730 (overlay-end mouse-drag-overlay)))
731 (let* ((stop-point
732 (if (numberp (posn-point (event-end event)))
733 (posn-point (event-end event))
734 last-end-point))
735 ;; The end that comes from where we ended the drag.
736 ;; Point goes here.
737 (region-termination
738 (if (and stop-point (< stop-point start-point))
739 (overlay-start mouse-drag-overlay)
740 (overlay-end mouse-drag-overlay)))
741 ;; The end that comes from where we started the drag.
742 ;; Mark goes there.
743 (region-commencement
744 (- (+ (overlay-end mouse-drag-overlay)
745 (overlay-start mouse-drag-overlay))
746 region-termination))
747 last-command this-command)
748 (push-mark region-commencement t t)
749 (goto-char region-termination)
750 ;; Don't let copy-region-as-kill set deactivate-mark.
751 (let (deactivate-mark)
752 (copy-region-as-kill (point) (mark t)))
753 (let ((buffer (current-buffer)))
754 (mouse-show-mark)
755 ;; mouse-show-mark can call read-event,
756 ;; and that means the Emacs server could switch buffers
757 ;; under us. If that happened,
758 ;; avoid trying to use the region.
759 (and (mark t) mark-active
760 (eq buffer (current-buffer))
761 (mouse-set-region-1))))
762 (delete-overlay mouse-drag-overlay)
763 ;; Run the binding of the terminating up-event.
764 (when (and (functionp fun)
765 (= start-hscroll (window-hscroll start-window)))
766 (setq unread-command-events
767 (cons event unread-command-events)))))
768 (delete-overlay mouse-drag-overlay)))))
770 ;; Commands to handle xterm-style multiple clicks.
772 (defun mouse-skip-word (dir)
773 "Skip over word, over whitespace, or over identical punctuation.
774 If DIR is positive skip forward; if negative, skip backward."
775 (let* ((char (following-char))
776 (syntax (char-to-string (char-syntax char))))
777 (cond ((string= syntax "w")
778 ;; Here, we can't use skip-syntax-forward/backward because
779 ;; they don't pay attention to word-separating-categories,
780 ;; and thus they will skip over a true word boundary. So,
781 ;; we simularte the original behaviour by using
782 ;; forward-word.
783 (if (< dir 0)
784 (if (not (looking-at "\\<"))
785 (forward-word -1))
786 (if (or (looking-at "\\<") (not (looking-at "\\>")))
787 (forward-word 1))))
788 ((string= syntax " ")
789 (if (< dir 0)
790 (skip-syntax-backward syntax)
791 (skip-syntax-forward syntax)))
792 ((string= syntax "_")
793 (if (< dir 0)
794 (skip-syntax-backward "w_")
795 (skip-syntax-forward "w_")))
796 ((< dir 0)
797 (while (and (not (bobp)) (= (preceding-char) char))
798 (forward-char -1)))
800 (while (and (not (eobp)) (= (following-char) char))
801 (forward-char 1))))))
803 ;; Return a list of region bounds based on START and END according to MODE.
804 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
805 ;; If MODE is 1 then set point to start of word at (min START END),
806 ;; mark to end of word at (max START END).
807 ;; If MODE is 2 then do the same for lines.
808 (defun mouse-start-end (start end mode)
809 (if (> start end)
810 (let ((temp start))
811 (setq start end
812 end temp)))
813 (setq mode (mod mode 3))
814 (cond ((= mode 0)
815 (list start end))
816 ((and (= mode 1)
817 (= start end)
818 (char-after start)
819 (= (char-syntax (char-after start)) ?\())
820 (list start
821 (save-excursion
822 (goto-char start)
823 (forward-sexp 1)
824 (point))))
825 ((and (= mode 1)
826 (= start end)
827 (char-after start)
828 (= (char-syntax (char-after start)) ?\)))
829 (list (save-excursion
830 (goto-char (1+ start))
831 (backward-sexp 1)
832 (point))
833 (1+ start)))
834 ((and (= mode 1)
835 (= start end)
836 (char-after start)
837 (= (char-syntax (char-after start)) ?\"))
838 (let ((open (or (eq start (point-min))
839 (save-excursion
840 (goto-char (- start 1))
841 (looking-at "\\s(\\|\\s \\|\\s>")))))
842 (if open
843 (list start
844 (save-excursion
845 (condition-case nil
846 (progn
847 (goto-char start)
848 (forward-sexp 1)
849 (point))
850 (error end))))
851 (list (save-excursion
852 (condition-case nil
853 (progn
854 (goto-char (1+ start))
855 (backward-sexp 1)
856 (point))
857 (error end)))
858 (1+ start)))))
859 ((= mode 1)
860 (list (save-excursion
861 (goto-char start)
862 (mouse-skip-word -1)
863 (point))
864 (save-excursion
865 (goto-char end)
866 (mouse-skip-word 1)
867 (point))))
868 ((= mode 2)
869 (list (save-excursion
870 (goto-char start)
871 (beginning-of-line 1)
872 (point))
873 (save-excursion
874 (goto-char end)
875 (forward-line 1)
876 (point))))))
878 ;; Subroutine: set the mark where CLICK happened,
879 ;; but don't do anything else.
880 (defun mouse-set-mark-fast (click)
881 (mouse-minibuffer-check click)
882 (let ((posn (event-start click)))
883 (select-window (posn-window posn))
884 (if (numberp (posn-point posn))
885 (push-mark (posn-point posn) t t))))
887 (defun mouse-undouble-last-event (events)
888 (let* ((index (1- (length events)))
889 (last (nthcdr index events))
890 (event (car last))
891 (basic (event-basic-type event))
892 (old-modifiers (event-modifiers event))
893 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
894 (new
895 (if (consp event)
896 ;; Use reverse, not nreverse, since event-modifiers
897 ;; does not copy the list it returns.
898 (cons (event-convert-list (reverse (cons basic modifiers)))
899 (cdr event))
900 event)))
901 (setcar last new)
902 (if (and (not (equal modifiers old-modifiers))
903 (key-binding (apply 'vector events)))
905 (setcar last event)
906 nil)))
908 ;; Momentarily show where the mark is, if highlighting doesn't show it.
910 (defvar mouse-region-delete-keys '([delete])
911 "List of keys which shall cause the mouse region to be deleted.")
913 (defun mouse-show-mark ()
914 (if transient-mark-mode
915 (delete-overlay mouse-drag-overlay)
916 (let ((inhibit-quit t)
917 (echo-keystrokes 0)
918 event events key ignore
919 x-lost-selection-hooks)
920 (add-hook 'x-lost-selection-hooks
921 (lambda (seltype)
922 (if (eq seltype 'PRIMARY)
923 (progn (setq ignore t)
924 (throw 'mouse-show-mark t)))))
925 (move-overlay mouse-drag-overlay (point) (mark t))
926 (catch 'mouse-show-mark
927 ;; In this loop, execute scroll bar and switch-frame events.
928 ;; Also ignore down-events that are undefined.
929 (while (progn (setq event (read-event))
930 (setq events (append events (list event)))
931 (setq key (apply 'vector events))
932 (or (and (consp event)
933 (eq (car event) 'switch-frame))
934 (and (consp event)
935 (eq (posn-point (event-end event))
936 'vertical-scroll-bar))
937 (and (memq 'down (event-modifiers event))
938 (not (key-binding key))
939 (not (mouse-undouble-last-event events))
940 (not (member key mouse-region-delete-keys)))))
941 (and (consp event)
942 (or (eq (car event) 'switch-frame)
943 (eq (posn-point (event-end event))
944 'vertical-scroll-bar))
945 (let ((keys (vector 'vertical-scroll-bar event)))
946 (and (key-binding keys)
947 (progn
948 (call-interactively (key-binding keys)
949 nil keys)
950 (setq events nil)))))))
951 ;; If we lost the selection, just turn off the highlighting.
952 (if ignore
954 ;; For certain special keys, delete the region.
955 (if (member key mouse-region-delete-keys)
956 (delete-region (overlay-start mouse-drag-overlay)
957 (overlay-end mouse-drag-overlay))
958 ;; Otherwise, unread the key so it gets executed normally.
959 (setq unread-command-events
960 (nconc events unread-command-events))))
961 (setq quit-flag nil)
962 (delete-overlay mouse-drag-overlay))
963 (save-excursion
964 (goto-char (mark t))
965 (sit-for 1))))
967 (defun mouse-set-mark (click)
968 "Set mark at the position clicked on with the mouse.
969 Display cursor at that position for a second.
970 This must be bound to a mouse click."
971 (interactive "e")
972 (mouse-minibuffer-check click)
973 (select-window (posn-window (event-start click)))
974 ;; We don't use save-excursion because that preserves the mark too.
975 (let ((point-save (point)))
976 (unwind-protect
977 (progn (mouse-set-point click)
978 (push-mark nil t t)
979 (or transient-mark-mode
980 (sit-for 1)))
981 (goto-char point-save))))
983 (defun mouse-kill (click)
984 "Kill the region between point and the mouse click.
985 The text is saved in the kill ring, as with \\[kill-region]."
986 (interactive "e")
987 (mouse-minibuffer-check click)
988 (let* ((posn (event-start click))
989 (click-posn (posn-point posn)))
990 (select-window (posn-window posn))
991 (if (numberp click-posn)
992 (kill-region (min (point) click-posn)
993 (max (point) click-posn)))))
995 (defun mouse-yank-at-click (click arg)
996 "Insert the last stretch of killed text at the position clicked on.
997 Also move point to one end of the text thus inserted (normally the end),
998 and set mark at the beginning..
999 Prefix arguments are interpreted as with \\[yank].
1000 If `mouse-yank-at-point' is non-nil, insert at point
1001 regardless of where you click."
1002 (interactive "e\nP")
1003 ;; Give temporary modes such as isearch a chance to turn off.
1004 (run-hooks 'mouse-leave-buffer-hook)
1005 (or mouse-yank-at-point (mouse-set-point click))
1006 (setq this-command 'yank)
1007 (setq mouse-selection-click-count 0)
1008 (yank arg))
1010 (defun mouse-kill-ring-save (click)
1011 "Copy the region between point and the mouse click in the kill ring.
1012 This does not delete the region; it acts like \\[kill-ring-save]."
1013 (interactive "e")
1014 (mouse-set-mark-fast click)
1015 (let (this-command last-command)
1016 (kill-ring-save (point) (mark t)))
1017 (mouse-show-mark))
1019 ;;; This function used to delete the text between point and the mouse
1020 ;;; whenever it was equal to the front of the kill ring, but some
1021 ;;; people found that confusing.
1023 ;;; A list (TEXT START END), describing the text and position of the last
1024 ;;; invocation of mouse-save-then-kill.
1025 (defvar mouse-save-then-kill-posn nil)
1027 (defun mouse-save-then-kill-delete-region (beg end)
1028 ;; We must make our own undo boundaries
1029 ;; because they happen automatically only for the current buffer.
1030 (undo-boundary)
1031 (if (or (= beg end) (eq buffer-undo-list t))
1032 ;; If we have no undo list in this buffer,
1033 ;; just delete.
1034 (delete-region beg end)
1035 ;; Delete, but make the undo-list entry share with the kill ring.
1036 ;; First, delete just one char, so in case buffer is being modified
1037 ;; for the first time, the undo list records that fact.
1038 (let (before-change-functions after-change-functions)
1039 (delete-region beg
1040 (+ beg (if (> end beg) 1 -1))))
1041 (let ((buffer-undo-list buffer-undo-list))
1042 ;; Undo that deletion--but don't change the undo list!
1043 (let (before-change-functions after-change-functions)
1044 (primitive-undo 1 buffer-undo-list))
1045 ;; Now delete the rest of the specified region,
1046 ;; but don't record it.
1047 (setq buffer-undo-list t)
1048 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1049 (error "Lossage in mouse-save-then-kill-delete-region"))
1050 (delete-region beg end))
1051 (let ((tail buffer-undo-list))
1052 ;; Search back in buffer-undo-list for the string
1053 ;; that came from deleting one character.
1054 (while (and tail (not (stringp (car (car tail)))))
1055 (setq tail (cdr tail)))
1056 ;; Replace it with an entry for the entire deleted text.
1057 (and tail
1058 (setcar tail (cons (car kill-ring) (min beg end))))))
1059 (undo-boundary))
1061 (defun mouse-save-then-kill (click)
1062 "Save text to point in kill ring; the second time, kill the text.
1063 If the text between point and the mouse is the same as what's
1064 at the front of the kill ring, this deletes the text.
1065 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1066 which prepares for a second click to delete the text.
1068 If you have selected words or lines, this command extends the
1069 selection through the word or line clicked on. If you do this
1070 again in a different position, it extends the selection again.
1071 If you do this twice in the same position, the selection is killed."
1072 (interactive "e")
1073 (let ((before-scroll
1074 (with-current-buffer (window-buffer (posn-window (event-start click)))
1075 point-before-scroll)))
1076 (mouse-minibuffer-check click)
1077 (let ((click-posn (posn-point (event-start click)))
1078 ;; Don't let a subsequent kill command append to this one:
1079 ;; prevent setting this-command to kill-region.
1080 (this-command this-command))
1081 (if (and (save-excursion
1082 (set-buffer (window-buffer (posn-window (event-start click))))
1083 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1084 ;; Don't be fooled by a recent click in some other buffer.
1085 (eq mouse-selection-click-count-buffer
1086 (current-buffer)))))
1087 (if (not (and (eq last-command 'mouse-save-then-kill)
1088 (equal click-posn
1089 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1090 ;; Find both ends of the object selected by this click.
1091 (let* ((range
1092 (mouse-start-end click-posn click-posn
1093 mouse-selection-click-count)))
1094 ;; Move whichever end is closer to the click.
1095 ;; That's what xterm does, and it seems reasonable.
1096 (if (< (abs (- click-posn (mark t)))
1097 (abs (- click-posn (point))))
1098 (set-mark (car range))
1099 (goto-char (nth 1 range)))
1100 ;; We have already put the old region in the kill ring.
1101 ;; Replace it with the extended region.
1102 ;; (It would be annoying to make a separate entry.)
1103 (kill-new (buffer-substring (point) (mark t)) t)
1104 (mouse-set-region-1)
1105 ;; Arrange for a repeated mouse-3 to kill this region.
1106 (setq mouse-save-then-kill-posn
1107 (list (car kill-ring) (point) click-posn))
1108 (mouse-show-mark))
1109 ;; If we click this button again without moving it,
1110 ;; that time kill.
1111 (mouse-save-then-kill-delete-region (mark) (point))
1112 (setq mouse-selection-click-count 0)
1113 (setq mouse-save-then-kill-posn nil))
1114 (if (and (eq last-command 'mouse-save-then-kill)
1115 mouse-save-then-kill-posn
1116 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1117 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1118 ;; If this is the second time we've called
1119 ;; mouse-save-then-kill, delete the text from the buffer.
1120 (progn
1121 (mouse-save-then-kill-delete-region (point) (mark))
1122 ;; After we kill, another click counts as "the first time".
1123 (setq mouse-save-then-kill-posn nil))
1124 ;; This is not a repetition.
1125 ;; We are adjusting an old selection or creating a new one.
1126 (if (or (and (eq last-command 'mouse-save-then-kill)
1127 mouse-save-then-kill-posn)
1128 (and mark-active transient-mark-mode)
1129 (and (memq last-command
1130 '(mouse-drag-region mouse-set-region))
1131 (or mark-even-if-inactive
1132 (not transient-mark-mode))))
1133 ;; We have a selection or suitable region, so adjust it.
1134 (let* ((posn (event-start click))
1135 (new (posn-point posn)))
1136 (select-window (posn-window posn))
1137 (if (numberp new)
1138 (progn
1139 ;; Move whichever end of the region is closer to the click.
1140 ;; That is what xterm does, and it seems reasonable.
1141 (if (< (abs (- new (point))) (abs (- new (mark t))))
1142 (goto-char new)
1143 (set-mark new))
1144 (setq deactivate-mark nil)))
1145 (kill-new (buffer-substring (point) (mark t)) t)
1146 (mouse-show-mark))
1147 ;; Set the mark where point is, then move where clicked.
1148 (mouse-set-mark-fast click)
1149 (if before-scroll
1150 (goto-char before-scroll))
1151 (exchange-point-and-mark)
1152 (kill-new (buffer-substring (point) (mark t)))
1153 (mouse-show-mark))
1154 (mouse-set-region-1)
1155 (setq mouse-save-then-kill-posn
1156 (list (car kill-ring) (point) click-posn)))))))
1158 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1159 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1160 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1161 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1162 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1164 ;; An overlay which records the current secondary selection
1165 ;; or else is deleted when there is no secondary selection.
1166 ;; May be nil.
1167 (defvar mouse-secondary-overlay nil)
1169 (defvar mouse-secondary-click-count 0)
1171 ;; A marker which records the specified first end for a secondary selection.
1172 ;; May be nil.
1173 (defvar mouse-secondary-start nil)
1175 (defun mouse-start-secondary (click)
1176 "Set one end of the secondary selection to the position clicked on.
1177 Use \\[mouse-secondary-save-then-kill] to set the other end
1178 and complete the secondary selection."
1179 (interactive "e")
1180 (mouse-minibuffer-check click)
1181 (let ((posn (event-start click)))
1182 (save-excursion
1183 (set-buffer (window-buffer (posn-window posn)))
1184 ;; Cancel any preexisting secondary selection.
1185 (if mouse-secondary-overlay
1186 (delete-overlay mouse-secondary-overlay))
1187 (if (numberp (posn-point posn))
1188 (progn
1189 (or mouse-secondary-start
1190 (setq mouse-secondary-start (make-marker)))
1191 (move-marker mouse-secondary-start (posn-point posn)))))))
1193 (defun mouse-set-secondary (click)
1194 "Set the secondary selection to the text that the mouse is dragged over.
1195 This must be bound to a mouse drag event."
1196 (interactive "e")
1197 (mouse-minibuffer-check click)
1198 (let ((posn (event-start click))
1200 (end (event-end click)))
1201 (save-excursion
1202 (set-buffer (window-buffer (posn-window posn)))
1203 (if (numberp (posn-point posn))
1204 (setq beg (posn-point posn)))
1205 (if mouse-secondary-overlay
1206 (move-overlay mouse-secondary-overlay beg (posn-point end))
1207 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1208 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1210 (defun mouse-drag-secondary (start-event)
1211 "Set the secondary selection to the text that the mouse is dragged over.
1212 Highlight the drag area as you move the mouse.
1213 This must be bound to a button-down mouse event.
1214 The function returns a non-nil value if it creates a secondary selection."
1215 (interactive "e")
1216 (mouse-minibuffer-check start-event)
1217 (let* ((echo-keystrokes 0)
1218 (start-posn (event-start start-event))
1219 (start-point (posn-point start-posn))
1220 (start-window (posn-window start-posn))
1221 (start-frame (window-frame start-window))
1222 (bounds (window-edges start-window))
1223 (top (nth 1 bounds))
1224 (bottom (if (window-minibuffer-p start-window)
1225 (nth 3 bounds)
1226 ;; Don't count the mode line.
1227 (1- (nth 3 bounds))))
1228 (click-count (1- (event-click-count start-event))))
1229 (save-excursion
1230 (set-buffer (window-buffer start-window))
1231 (setq mouse-secondary-click-count click-count)
1232 (or mouse-secondary-overlay
1233 (setq mouse-secondary-overlay
1234 (make-overlay (point) (point))))
1235 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1236 (if (> (mod click-count 3) 0)
1237 ;; Double or triple press: make an initial selection
1238 ;; of one word or line.
1239 (let ((range (mouse-start-end start-point start-point click-count)))
1240 (set-marker mouse-secondary-start nil)
1241 (move-overlay mouse-secondary-overlay 1 1
1242 (window-buffer start-window))
1243 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1244 (window-buffer start-window)))
1245 ;; Single-press: cancel any preexisting secondary selection.
1246 (or mouse-secondary-start
1247 (setq mouse-secondary-start (make-marker)))
1248 (set-marker mouse-secondary-start start-point)
1249 (delete-overlay mouse-secondary-overlay))
1250 (let (event end end-point)
1251 (track-mouse
1252 (while (progn
1253 (setq event (read-event))
1254 (or (mouse-movement-p event)
1255 (eq (car-safe event) 'switch-frame)))
1257 (if (eq (car-safe event) 'switch-frame)
1259 (setq end (event-end event)
1260 end-point (posn-point end))
1261 (cond
1262 ;; Are we moving within the original window?
1263 ((and (eq (posn-window end) start-window)
1264 (integer-or-marker-p end-point))
1265 (let ((range (mouse-start-end start-point end-point
1266 click-count)))
1267 (if (or (/= start-point end-point)
1268 (null (marker-position mouse-secondary-start)))
1269 (progn
1270 (set-marker mouse-secondary-start nil)
1271 (move-overlay mouse-secondary-overlay
1272 (car range) (nth 1 range))))))
1274 (let ((mouse-row (cdr (cdr (mouse-position)))))
1275 (cond
1276 ((null mouse-row))
1277 ((< mouse-row top)
1278 (mouse-scroll-subr start-window (- mouse-row top)
1279 mouse-secondary-overlay start-point))
1280 ((>= mouse-row bottom)
1281 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1282 mouse-secondary-overlay start-point)))))))))
1284 (if (consp event)
1285 (if (marker-position mouse-secondary-start)
1286 (save-window-excursion
1287 (delete-overlay mouse-secondary-overlay)
1288 (x-set-selection 'SECONDARY nil)
1289 (select-window start-window)
1290 (save-excursion
1291 (goto-char mouse-secondary-start)
1292 (sit-for 1)
1293 nil))
1294 (x-set-selection
1295 'SECONDARY
1296 (buffer-substring (overlay-start mouse-secondary-overlay)
1297 (overlay-end mouse-secondary-overlay)))))))))
1299 (defun mouse-yank-secondary (click)
1300 "Insert the secondary selection at the position clicked on.
1301 Move point to the end of the inserted text.
1302 If `mouse-yank-at-point' is non-nil, insert at point
1303 regardless of where you click."
1304 (interactive "e")
1305 ;; Give temporary modes such as isearch a chance to turn off.
1306 (run-hooks 'mouse-leave-buffer-hook)
1307 (or mouse-yank-at-point (mouse-set-point click))
1308 (insert (x-get-selection 'SECONDARY)))
1310 (defun mouse-kill-secondary ()
1311 "Kill the text in the secondary selection.
1312 This is intended more as a keyboard command than as a mouse command
1313 but it can work as either one.
1315 The current buffer (in case of keyboard use), or the buffer clicked on,
1316 must be the one that the secondary selection is in. This requirement
1317 is to prevent accidents."
1318 (interactive)
1319 (let* ((keys (this-command-keys))
1320 (click (elt keys (1- (length keys)))))
1321 (or (eq (overlay-buffer mouse-secondary-overlay)
1322 (if (listp click)
1323 (window-buffer (posn-window (event-start click)))
1324 (current-buffer)))
1325 (error "Select or click on the buffer where the secondary selection is")))
1326 (let (this-command)
1327 (save-excursion
1328 (set-buffer (overlay-buffer mouse-secondary-overlay))
1329 (kill-region (overlay-start mouse-secondary-overlay)
1330 (overlay-end mouse-secondary-overlay))))
1331 (delete-overlay mouse-secondary-overlay)
1332 ;;; (x-set-selection 'SECONDARY nil)
1333 (setq mouse-secondary-overlay nil))
1335 (defun mouse-secondary-save-then-kill (click)
1336 "Save text to point in kill ring; the second time, kill the text.
1337 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1338 If the text between where you did \\[mouse-start-secondary] and where
1339 you use this command matches the text at the front of the kill ring,
1340 this command deletes the text.
1341 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1342 which prepares for a second click with this command to delete the text.
1344 If you have already made a secondary selection in that buffer,
1345 this command extends or retracts the selection to where you click.
1346 If you do this again in a different position, it extends or retracts
1347 again. If you do this twice in the same position, it kills the selection."
1348 (interactive "e")
1349 (mouse-minibuffer-check click)
1350 (let ((posn (event-start click))
1351 (click-posn (posn-point (event-start click)))
1352 ;; Don't let a subsequent kill command append to this one:
1353 ;; prevent setting this-command to kill-region.
1354 (this-command this-command))
1355 (or (eq (window-buffer (posn-window posn))
1356 (or (and mouse-secondary-overlay
1357 (overlay-buffer mouse-secondary-overlay))
1358 (if mouse-secondary-start
1359 (marker-buffer mouse-secondary-start))))
1360 (error "Wrong buffer"))
1361 (save-excursion
1362 (set-buffer (window-buffer (posn-window posn)))
1363 (if (> (mod mouse-secondary-click-count 3) 0)
1364 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1365 (equal click-posn
1366 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1367 ;; Find both ends of the object selected by this click.
1368 (let* ((range
1369 (mouse-start-end click-posn click-posn
1370 mouse-secondary-click-count)))
1371 ;; Move whichever end is closer to the click.
1372 ;; That's what xterm does, and it seems reasonable.
1373 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1374 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1375 (move-overlay mouse-secondary-overlay (car range)
1376 (overlay-end mouse-secondary-overlay))
1377 (move-overlay mouse-secondary-overlay
1378 (overlay-start mouse-secondary-overlay)
1379 (nth 1 range)))
1380 ;; We have already put the old region in the kill ring.
1381 ;; Replace it with the extended region.
1382 ;; (It would be annoying to make a separate entry.)
1383 (kill-new (buffer-substring
1384 (overlay-start mouse-secondary-overlay)
1385 (overlay-end mouse-secondary-overlay)) t)
1386 ;; Arrange for a repeated mouse-3 to kill this region.
1387 (setq mouse-save-then-kill-posn
1388 (list (car kill-ring) (point) click-posn)))
1389 ;; If we click this button again without moving it,
1390 ;; that time kill.
1391 (progn
1392 (mouse-save-then-kill-delete-region
1393 (overlay-start mouse-secondary-overlay)
1394 (overlay-end mouse-secondary-overlay))
1395 (setq mouse-save-then-kill-posn nil)
1396 (setq mouse-secondary-click-count 0)
1397 (delete-overlay mouse-secondary-overlay)))
1398 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1399 mouse-save-then-kill-posn
1400 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1401 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1402 ;; If this is the second time we've called
1403 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1404 (progn
1405 (mouse-save-then-kill-delete-region
1406 (overlay-start mouse-secondary-overlay)
1407 (overlay-end mouse-secondary-overlay))
1408 (setq mouse-save-then-kill-posn nil)
1409 (delete-overlay mouse-secondary-overlay))
1410 (if (overlay-start mouse-secondary-overlay)
1411 ;; We have a selection, so adjust it.
1412 (progn
1413 (if (numberp click-posn)
1414 (progn
1415 ;; Move whichever end of the region is closer to the click.
1416 ;; That is what xterm does, and it seems reasonable.
1417 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1418 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1419 (move-overlay mouse-secondary-overlay click-posn
1420 (overlay-end mouse-secondary-overlay))
1421 (move-overlay mouse-secondary-overlay
1422 (overlay-start mouse-secondary-overlay)
1423 click-posn))
1424 (setq deactivate-mark nil)))
1425 (if (eq last-command 'mouse-secondary-save-then-kill)
1426 ;; If the front of the kill ring comes from
1427 ;; an immediately previous use of this command,
1428 ;; replace it with the extended region.
1429 ;; (It would be annoying to make a separate entry.)
1430 (kill-new (buffer-substring
1431 (overlay-start mouse-secondary-overlay)
1432 (overlay-end mouse-secondary-overlay)) t)
1433 (let (deactivate-mark)
1434 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1435 (overlay-end mouse-secondary-overlay)))))
1436 (if mouse-secondary-start
1437 ;; All we have is one end of a selection,
1438 ;; so put the other end here.
1439 (let ((start (+ 0 mouse-secondary-start)))
1440 (kill-ring-save start click-posn)
1441 (if mouse-secondary-overlay
1442 (move-overlay mouse-secondary-overlay start click-posn)
1443 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1444 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1445 (setq mouse-save-then-kill-posn
1446 (list (car kill-ring) (point) click-posn))))
1447 (if (overlay-buffer mouse-secondary-overlay)
1448 (x-set-selection 'SECONDARY
1449 (buffer-substring
1450 (overlay-start mouse-secondary-overlay)
1451 (overlay-end mouse-secondary-overlay)))))))
1453 (defcustom mouse-buffer-menu-maxlen 20
1454 "*Number of buffers in one pane (submenu) of the buffer menu.
1455 If we have lots of buffers, divide them into groups of
1456 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1457 :type 'integer
1458 :group 'mouse)
1460 (defcustom mouse-buffer-menu-mode-mult 4
1461 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1462 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1463 will split the buffer menu by the major modes (see
1464 `mouse-buffer-menu-mode-groups') or just by menu length.
1465 Set to 1 (or even 0!) if you want to group by major mode always, and to
1466 a large number if you prefer a mixed multitude. The default is 4."
1467 :type 'integer
1468 :group 'mouse
1469 :version "20.3")
1471 (defvar mouse-buffer-menu-mode-groups
1472 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1473 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1474 . "Mail/News")
1475 ("\\<C\\>" . "C")
1476 ("ObjC" . "C")
1477 ("Text" . "Text")
1478 ("Outline" . "Text")
1479 ("Lisp" . "Lisp"))
1480 "How to group various major modes together in \\[mouse-buffer-menu].
1481 Each element has the form (REGEXP . GROUPNAME).
1482 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1484 (defun mouse-buffer-menu (event)
1485 "Pop up a menu of buffers for selection with the mouse.
1486 This switches buffers in the window that you clicked on,
1487 and selects that window."
1488 (interactive "e")
1489 (mouse-minibuffer-check event)
1490 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1491 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1492 (let ((tail buffers))
1493 (while tail
1494 ;; Divide all buffers into buckets for various major modes.
1495 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1496 (with-current-buffer (car tail)
1497 (let* ((adjusted-major-mode major-mode) elt)
1498 (let ((tail mouse-buffer-menu-mode-groups))
1499 (while tail
1500 (if (string-match (car (car tail)) mode-name)
1501 (setq adjusted-major-mode (cdr (car tail))))
1502 (setq tail (cdr tail))))
1503 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1504 (if (null elt)
1505 (setq elt (list adjusted-major-mode
1506 (if (stringp adjusted-major-mode)
1507 adjusted-major-mode
1508 mode-name))
1509 split-by-major-mode (cons elt split-by-major-mode)))
1510 (or (memq (car tail) (cdr (cdr elt)))
1511 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1512 (setq tail (cdr tail))))
1513 ;; Compute the sum of squares of sizes of the major-mode buckets.
1514 (let ((tail split-by-major-mode))
1515 (setq sum-of-squares 0)
1516 (while tail
1517 (setq sum-of-squares
1518 (+ sum-of-squares
1519 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1520 (setq tail (cdr tail))))
1521 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1522 (* (length buffers) (length buffers)))
1523 ;; Subdividing by major modes really helps, so let's do it.
1524 (let (subdivided-menus (buffers-left (length buffers)))
1525 ;; Sort the list to put the most popular major modes first.
1526 (setq split-by-major-mode
1527 (sort split-by-major-mode
1528 (function (lambda (elt1 elt2)
1529 (> (length elt1) (length elt2))))))
1530 ;; Make a separate submenu for each major mode
1531 ;; that has more than one buffer,
1532 ;; unless all the remaining buffers are less than 1/10 of them.
1533 (while (and split-by-major-mode
1534 (and (> (length (car split-by-major-mode)) 3)
1535 (> (* buffers-left 10) (length buffers))))
1536 (setq subdivided-menus
1537 (cons (cons
1538 (nth 1 (car split-by-major-mode))
1539 (mouse-buffer-menu-alist
1540 (cdr (cdr (car split-by-major-mode)))))
1541 subdivided-menus))
1542 (setq buffers-left
1543 (- buffers-left (length (cdr (car split-by-major-mode)))))
1544 (setq split-by-major-mode (cdr split-by-major-mode)))
1545 ;; If any major modes are left over,
1546 ;; make a single submenu for them.
1547 (if split-by-major-mode
1548 (setq subdivided-menus
1549 (cons (cons
1550 "Others"
1551 (mouse-buffer-menu-alist
1552 ;; we don't need split-by-major-mode any
1553 ;; more, so we can ditch it with nconc.
1554 (apply 'nconc (mapcar 'cddr split-by-major-mode))))
1555 subdivided-menus)))
1556 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1557 (progn
1558 (setq alist (mouse-buffer-menu-alist buffers))
1559 (setq menu (cons "Buffer Menu"
1560 (mouse-buffer-menu-split "Select Buffer" alist)))))
1561 (let ((buf (x-popup-menu event menu))
1562 (window (posn-window (event-start event))))
1563 (when buf
1564 (or (framep window) (select-window window))
1565 (switch-to-buffer buf)))))
1567 (defun mouse-buffer-menu-alist (buffers)
1568 (let (tail
1569 (maxlen 0)
1570 head)
1571 (setq buffers
1572 (sort buffers
1573 (function (lambda (elt1 elt2)
1574 (string< (buffer-name elt1) (buffer-name elt2))))))
1575 (setq tail buffers)
1576 (while tail
1577 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1578 (setq maxlen
1579 (max maxlen
1580 (length (buffer-name (car tail))))))
1581 (setq tail (cdr tail)))
1582 (setq tail buffers)
1583 (while tail
1584 (let ((elt (car tail)))
1585 (if (/= (aref (buffer-name elt) 0) ?\ )
1586 (setq head
1587 (cons
1588 (cons
1589 (format
1590 (format "%%%ds %%s%%s %%s" maxlen)
1591 (buffer-name elt)
1592 (if (buffer-modified-p elt) "*" " ")
1593 (save-excursion
1594 (set-buffer elt)
1595 (if buffer-read-only "%" " "))
1596 (or (buffer-file-name elt)
1597 (save-excursion
1598 (set-buffer elt)
1599 (if list-buffers-directory
1600 (expand-file-name
1601 list-buffers-directory)))
1602 ""))
1603 elt)
1604 head))))
1605 (setq tail (cdr tail)))
1606 ;; Compensate for the reversal that the above loop does.
1607 (nreverse head)))
1609 (defun mouse-buffer-menu-split (title alist)
1610 ;; If we have lots of buffers, divide them into groups of 20
1611 ;; and make a pane (or submenu) for each one.
1612 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1613 (let ((alist alist) sublists next
1614 (i 1))
1615 (while alist
1616 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1617 ;; and make them the next element of sublist.
1618 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1619 (if next
1620 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1621 nil))
1622 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1623 sublists))
1624 (setq i (1+ i))
1625 (setq alist next))
1626 (nreverse sublists))
1627 ;; Few buffers--put them all in one pane.
1628 (list (cons title alist))))
1630 ;;; These need to be rewritten for the new scroll bar implementation.
1632 ;;;!! ;; Commands for the scroll bar.
1633 ;;;!!
1634 ;;;!! (defun mouse-scroll-down (click)
1635 ;;;!! (interactive "@e")
1636 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1637 ;;;!!
1638 ;;;!! (defun mouse-scroll-up (click)
1639 ;;;!! (interactive "@e")
1640 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1641 ;;;!!
1642 ;;;!! (defun mouse-scroll-down-full ()
1643 ;;;!! (interactive "@")
1644 ;;;!! (scroll-down nil))
1645 ;;;!!
1646 ;;;!! (defun mouse-scroll-up-full ()
1647 ;;;!! (interactive "@")
1648 ;;;!! (scroll-up nil))
1649 ;;;!!
1650 ;;;!! (defun mouse-scroll-move-cursor (click)
1651 ;;;!! (interactive "@e")
1652 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1653 ;;;!!
1654 ;;;!! (defun mouse-scroll-absolute (event)
1655 ;;;!! (interactive "@e")
1656 ;;;!! (let* ((pos (car event))
1657 ;;;!! (position (car pos))
1658 ;;;!! (length (car (cdr pos))))
1659 ;;;!! (if (<= length 0) (setq length 1))
1660 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1661 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1662 ;;;!! position)
1663 ;;;!! length)
1664 ;;;!! scale-factor)))
1665 ;;;!! (goto-char newpos)
1666 ;;;!! (recenter '(4)))))
1667 ;;;!!
1668 ;;;!! (defun mouse-scroll-left (click)
1669 ;;;!! (interactive "@e")
1670 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1671 ;;;!!
1672 ;;;!! (defun mouse-scroll-right (click)
1673 ;;;!! (interactive "@e")
1674 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1675 ;;;!!
1676 ;;;!! (defun mouse-scroll-left-full ()
1677 ;;;!! (interactive "@")
1678 ;;;!! (scroll-left nil))
1679 ;;;!!
1680 ;;;!! (defun mouse-scroll-right-full ()
1681 ;;;!! (interactive "@")
1682 ;;;!! (scroll-right nil))
1683 ;;;!!
1684 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1685 ;;;!! (interactive "@e")
1686 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1687 ;;;!!
1688 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1689 ;;;!! (interactive "@e")
1690 ;;;!! (let* ((pos (car event))
1691 ;;;!! (position (car pos))
1692 ;;;!! (length (car (cdr pos))))
1693 ;;;!! (set-window-hscroll (selected-window) 33)))
1694 ;;;!!
1695 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1696 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1697 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1698 ;;;!!
1699 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1700 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1701 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1702 ;;;!!
1703 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1704 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1705 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1706 ;;;!!
1707 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1708 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1709 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1710 ;;;!!
1711 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1712 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1713 ;;;!! 'mouse-scroll-absolute-horizontally)
1714 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1715 ;;;!!
1716 ;;;!! (global-set-key [horizontal-slider mouse-1]
1717 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1718 ;;;!! (global-set-key [horizontal-slider mouse-2]
1719 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1720 ;;;!! (global-set-key [horizontal-slider mouse-3]
1721 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1722 ;;;!!
1723 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1724 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1725 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1726 ;;;!!
1727 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1728 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1729 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1730 ;;;!!
1731 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1732 ;;;!! 'mouse-split-window-horizontally)
1733 ;;;!! (global-set-key [mode-line S-mouse-2]
1734 ;;;!! 'mouse-split-window-horizontally)
1735 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1736 ;;;!! 'mouse-split-window)
1738 ;;;!! ;;;;
1739 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1740 ;;;!! ;;;; are of the form:
1741 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1742 ;;;!! ;;
1743 ;;;!! ;;;;
1744 ;;;!! ;;;; Dynamically track mouse coordinates
1745 ;;;!! ;;;;
1746 ;;;!! ;;
1747 ;;;!! ;;(defun track-mouse (event)
1748 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1749 ;;;!! ;; (interactive "@e")
1750 ;;;!! ;; (while mouse-grabbed
1751 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1752 ;;;!! ;; (abs-x (car pos))
1753 ;;;!! ;; (abs-y (cdr pos))
1754 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1755 ;;;!! ;; (list (car pos) (cdr pos))
1756 ;;;!! ;; (selected-window))))
1757 ;;;!! ;; (if (consp relative-coordinate)
1758 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1759 ;;;!! ;; (car relative-coordinate)
1760 ;;;!! ;; (car (cdr relative-coordinate)))
1761 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1762 ;;;!!
1763 ;;;!! ;;
1764 ;;;!! ;; Dynamically put a box around the line indicated by point
1765 ;;;!! ;;
1766 ;;;!! ;;
1767 ;;;!! ;;(require 'backquote)
1768 ;;;!! ;;
1769 ;;;!! ;;(defun mouse-select-buffer-line (event)
1770 ;;;!! ;; (interactive "@e")
1771 ;;;!! ;; (let ((relative-coordinate
1772 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1773 ;;;!! ;; (abs-y (car (cdr (car event)))))
1774 ;;;!! ;; (if (consp relative-coordinate)
1775 ;;;!! ;; (progn
1776 ;;;!! ;; (save-excursion
1777 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1778 ;;;!! ;; (x-draw-rectangle
1779 ;;;!! ;; (selected-screen)
1780 ;;;!! ;; abs-y 0
1781 ;;;!! ;; (save-excursion
1782 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1783 ;;;!! ;; (end-of-line)
1784 ;;;!! ;; (push-mark nil t)
1785 ;;;!! ;; (beginning-of-line)
1786 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1787 ;;;!! ;; (sit-for 1)
1788 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1789 ;;;!! ;;
1790 ;;;!! ;;(defvar last-line-drawn nil)
1791 ;;;!! ;;(defvar begin-delim "[^ \t]")
1792 ;;;!! ;;(defvar end-delim "[^ \t]")
1793 ;;;!! ;;
1794 ;;;!! ;;(defun mouse-boxing (event)
1795 ;;;!! ;; (interactive "@e")
1796 ;;;!! ;; (save-excursion
1797 ;;;!! ;; (let ((screen (selected-screen)))
1798 ;;;!! ;; (while (= (x-mouse-events) 0)
1799 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1800 ;;;!! ;; (abs-x (car pos))
1801 ;;;!! ;; (abs-y (cdr pos))
1802 ;;;!! ;; (relative-coordinate
1803 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1804 ;;;!! ;; (selected-window)))
1805 ;;;!! ;; (begin-reg nil)
1806 ;;;!! ;; (end-reg nil)
1807 ;;;!! ;; (end-column nil)
1808 ;;;!! ;; (begin-column nil))
1809 ;;;!! ;; (if (and (consp relative-coordinate)
1810 ;;;!! ;; (or (not last-line-drawn)
1811 ;;;!! ;; (not (= last-line-drawn abs-y))))
1812 ;;;!! ;; (progn
1813 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1814 ;;;!! ;; (if (= (following-char) 10)
1815 ;;;!! ;; ()
1816 ;;;!! ;; (progn
1817 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1818 ;;;!! ;; (setq begin-column (1- (current-column)))
1819 ;;;!! ;; (end-of-line)
1820 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1821 ;;;!! ;; (setq end-column (1+ (current-column)))
1822 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1823 ;;;!! ;; (x-draw-rectangle screen
1824 ;;;!! ;; (setq last-line-drawn abs-y)
1825 ;;;!! ;; begin-column
1826 ;;;!! ;; (- end-column begin-column) 1))))))))))
1827 ;;;!! ;;
1828 ;;;!! ;;(defun mouse-erase-box ()
1829 ;;;!! ;; (interactive)
1830 ;;;!! ;; (if last-line-drawn
1831 ;;;!! ;; (progn
1832 ;;;!! ;; (x-erase-rectangle (selected-screen))
1833 ;;;!! ;; (setq last-line-drawn nil))))
1834 ;;;!!
1835 ;;;!! ;;; (defun test-x-rectangle ()
1836 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1837 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1838 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1839 ;;;!!
1840 ;;;!! ;;
1841 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1842 ;;;!! ;;
1843 ;;;!!
1844 ;;;!! (defvar double-start nil)
1845 ;;;!! (defconst double-click-interval 300
1846 ;;;!! "Max ticks between clicks")
1847 ;;;!!
1848 ;;;!! (defun double-down (event)
1849 ;;;!! (interactive "@e")
1850 ;;;!! (if double-start
1851 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1852 ;;;!! (if (< interval double-click-interval)
1853 ;;;!! (progn
1854 ;;;!! (backward-up-list 1)
1855 ;;;!! ;; (message "Interval %d" interval)
1856 ;;;!! (sleep-for 1)))
1857 ;;;!! (setq double-start nil))
1858 ;;;!! (setq double-start (nth 4 event))))
1859 ;;;!!
1860 ;;;!! (defun double-up (event)
1861 ;;;!! (interactive "@e")
1862 ;;;!! (and double-start
1863 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1864 ;;;!! (setq double-start nil)))
1865 ;;;!!
1866 ;;;!! ;;; (defun x-test-doubleclick ()
1867 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1868 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1869 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1870 ;;;!!
1871 ;;;!! ;;
1872 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1873 ;;;!! ;;
1874 ;;;!!
1875 ;;;!! (defvar scrolled-lines 0)
1876 ;;;!! (defconst scroll-speed 1)
1877 ;;;!!
1878 ;;;!! (defun incr-scroll-down (event)
1879 ;;;!! (interactive "@e")
1880 ;;;!! (setq scrolled-lines 0)
1881 ;;;!! (incremental-scroll scroll-speed))
1882 ;;;!!
1883 ;;;!! (defun incr-scroll-up (event)
1884 ;;;!! (interactive "@e")
1885 ;;;!! (setq scrolled-lines 0)
1886 ;;;!! (incremental-scroll (- scroll-speed)))
1887 ;;;!!
1888 ;;;!! (defun incremental-scroll (n)
1889 ;;;!! (while (= (x-mouse-events) 0)
1890 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1891 ;;;!! (scroll-down n)
1892 ;;;!! (sit-for 300 t)))
1893 ;;;!!
1894 ;;;!! (defun incr-scroll-stop (event)
1895 ;;;!! (interactive "@e")
1896 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1897 ;;;!! (setq scrolled-lines 0)
1898 ;;;!! (sleep-for 1))
1899 ;;;!!
1900 ;;;!! ;;; (defun x-testing-scroll ()
1901 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1902 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1903 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1904 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1905 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1906 ;;;!!
1907 ;;;!! ;;
1908 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1909 ;;;!! ;;
1910 ;;;!!
1911 ;;;!! (defun mouse-kill-rectangle (event)
1912 ;;;!! "Kill the rectangle between point and the mouse cursor."
1913 ;;;!! (interactive "@e")
1914 ;;;!! (let ((point-save (point)))
1915 ;;;!! (save-excursion
1916 ;;;!! (mouse-set-point event)
1917 ;;;!! (push-mark nil t)
1918 ;;;!! (if (> point-save (point))
1919 ;;;!! (kill-rectangle (point) point-save)
1920 ;;;!! (kill-rectangle point-save (point))))))
1921 ;;;!!
1922 ;;;!! (defun mouse-open-rectangle (event)
1923 ;;;!! "Kill the rectangle between point and the mouse cursor."
1924 ;;;!! (interactive "@e")
1925 ;;;!! (let ((point-save (point)))
1926 ;;;!! (save-excursion
1927 ;;;!! (mouse-set-point event)
1928 ;;;!! (push-mark nil t)
1929 ;;;!! (if (> point-save (point))
1930 ;;;!! (open-rectangle (point) point-save)
1931 ;;;!! (open-rectangle point-save (point))))))
1932 ;;;!!
1933 ;;;!! ;; Must be a better way to do this.
1934 ;;;!!
1935 ;;;!! (defun mouse-multiple-insert (n char)
1936 ;;;!! (while (> n 0)
1937 ;;;!! (insert char)
1938 ;;;!! (setq n (1- n))))
1939 ;;;!!
1940 ;;;!! ;; What this could do is not finalize until button was released.
1941 ;;;!!
1942 ;;;!! (defun mouse-move-text (event)
1943 ;;;!! "Move text from point to cursor position, inserting spaces."
1944 ;;;!! (interactive "@e")
1945 ;;;!! (let* ((relative-coordinate
1946 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1947 ;;;!! (if (consp relative-coordinate)
1948 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1949 ;;;!! (delete-char
1950 ;;;!! (- (car relative-coordinate) (current-column))))
1951 ;;;!! ((< (current-column) (car relative-coordinate))
1952 ;;;!! (mouse-multiple-insert
1953 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1954 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1956 ;; Choose a completion with the mouse.
1958 (defun mouse-choose-completion (event)
1959 "Click on an alternative in the `*Completions*' buffer to choose it."
1960 (interactive "e")
1961 ;; Give temporary modes such as isearch a chance to turn off.
1962 (run-hooks 'mouse-leave-buffer-hook)
1963 (let ((buffer (window-buffer))
1964 choice
1965 base-size)
1966 (save-excursion
1967 (set-buffer (window-buffer (posn-window (event-start event))))
1968 (if completion-reference-buffer
1969 (setq buffer completion-reference-buffer))
1970 (setq base-size completion-base-size)
1971 (save-excursion
1972 (goto-char (posn-point (event-start event)))
1973 (let (beg end)
1974 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1975 (setq end (point) beg (1+ (point))))
1976 (if (null beg)
1977 (error "No completion here"))
1978 (setq beg (previous-single-property-change beg 'mouse-face))
1979 (setq end (or (next-single-property-change end 'mouse-face)
1980 (point-max)))
1981 (setq choice (buffer-substring beg end)))))
1982 (let ((owindow (selected-window)))
1983 (select-window (posn-window (event-start event)))
1984 (if (and (one-window-p t 'selected-frame)
1985 (window-dedicated-p (selected-window)))
1986 ;; This is a special buffer's frame
1987 (iconify-frame (selected-frame))
1988 (or (window-dedicated-p (selected-window))
1989 (bury-buffer)))
1990 (select-window owindow))
1991 (choose-completion-string choice buffer base-size)))
1993 ;; Font selection.
1995 (defun font-menu-add-default ()
1996 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1997 (font-alist x-fixed-font-alist)
1998 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1999 (if (assoc "Default" elt)
2000 (delete (assoc "Default" elt) elt))
2001 (setcdr elt
2002 (cons (list "Default" default)
2003 (cdr elt)))))
2005 (defvar x-fixed-font-alist
2006 '("Font menu"
2007 ("Misc"
2008 ;; For these, we specify the pixel height and width.
2009 ("fixed" "fixed")
2010 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2011 ("6x12"
2012 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2013 ("6x13"
2014 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2015 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2016 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2017 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2018 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2019 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2020 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2021 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2022 ("")
2023 ("clean 5x8"
2024 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2025 ("clean 6x8"
2026 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2027 ("clean 8x8"
2028 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2029 ("clean 8x10"
2030 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2031 ("clean 8x14"
2032 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2033 ("clean 8x16"
2034 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2035 ("")
2036 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2037 ;;; We don't seem to have these; who knows what they are.
2038 ;;; ("fg-18" "fg-18")
2039 ;;; ("fg-25" "fg-25")
2040 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2041 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2042 ("lucidasanstypewriter-bold-24"
2043 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2044 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2045 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2047 ("Courier"
2048 ;; For these, we specify the point height.
2049 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2050 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2051 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2052 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2053 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2054 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2055 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2056 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2057 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2058 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2059 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2060 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2061 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2062 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2063 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2064 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2065 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2066 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2067 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2068 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2069 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2070 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2071 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2072 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2074 "X fonts suitable for use in Emacs.")
2076 (defun mouse-set-font (&rest fonts)
2077 "Select an emacs font from a list of known good fonts and fontsets."
2078 (interactive
2079 (x-popup-menu
2080 last-nonmenu-event
2081 ;; Append list of fontsets currently defined.
2082 (append x-fixed-font-alist (list (generate-fontset-menu)))))
2083 (if fonts
2084 (let (font)
2085 (while fonts
2086 (condition-case nil
2087 (progn
2088 (set-default-font (car fonts))
2089 (setq font (car fonts))
2090 (setq fonts nil))
2091 (error
2092 (setq fonts (cdr fonts)))))
2093 (if (null font)
2094 (error "Font not found")))))
2096 ;;; Bindings for mouse commands.
2098 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2099 (global-set-key [mouse-1] 'mouse-set-point)
2100 (global-set-key [drag-mouse-1] 'mouse-set-region)
2102 ;; These are tested for in mouse-drag-region.
2103 (global-set-key [double-mouse-1] 'mouse-set-point)
2104 (global-set-key [triple-mouse-1] 'mouse-set-point)
2106 (global-set-key [mouse-2] 'mouse-yank-at-click)
2107 (global-set-key [mouse-3] 'mouse-save-then-kill)
2109 ;; By binding these to down-going events, we let the user use the up-going
2110 ;; event to make the selection, saving a click.
2111 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2112 (if (not (eq system-type 'ms-dos))
2113 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2114 ;; C-down-mouse-2 is bound in facemenu.el.
2115 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2118 ;; Replaced with dragging mouse-1
2119 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2121 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2122 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2123 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2124 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2125 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2126 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2127 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2128 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2129 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2130 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2131 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2133 (provide 'mouse)
2135 ;; This file contains the functionality of the old mldrag.el.
2136 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2137 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2138 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2139 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2140 (provide 'mldrag)
2142 ;;; mouse.el ends here