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