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