]> code.delx.au - gnu-emacs/blob - lisp/mouse.el
(outline-discard-overlays):
[gnu-emacs] / 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
926 (with-current-buffer (window-buffer (posn-window (event-start click)))
927 point-before-scroll)))
928 (mouse-minibuffer-check click)
929 (let ((click-posn (posn-point (event-start click)))
930 ;; Don't let a subsequent kill command append to this one:
931 ;; prevent setting this-command to kill-region.
932 (this-command this-command))
933 (if (and (save-excursion
934 (set-buffer (window-buffer (posn-window (event-start click))))
935 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
936 ;; Don't be fooled by a recent click in some other buffer.
937 (eq mouse-selection-click-count-buffer
938 (current-buffer)))))
939 (if (not (and (eq last-command 'mouse-save-then-kill)
940 (equal click-posn
941 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
942 ;; Find both ends of the object selected by this click.
943 (let* ((range
944 (mouse-start-end click-posn click-posn
945 mouse-selection-click-count)))
946 ;; Move whichever end is closer to the click.
947 ;; That's what xterm does, and it seems reasonable.
948 (if (< (abs (- click-posn (mark t)))
949 (abs (- click-posn (point))))
950 (set-mark (car range))
951 (goto-char (nth 1 range)))
952 ;; We have already put the old region in the kill ring.
953 ;; Replace it with the extended region.
954 ;; (It would be annoying to make a separate entry.)
955 (kill-new (buffer-substring (point) (mark t)) t)
956 (mouse-set-region-1)
957 ;; Arrange for a repeated mouse-3 to kill this region.
958 (setq mouse-save-then-kill-posn
959 (list (car kill-ring) (point) click-posn))
960 (mouse-show-mark))
961 ;; If we click this button again without moving it,
962 ;; that time kill.
963 (mouse-save-then-kill-delete-region (mark) (point))
964 (setq mouse-selection-click-count 0)
965 (setq mouse-save-then-kill-posn nil))
966 (if (and (eq last-command 'mouse-save-then-kill)
967 mouse-save-then-kill-posn
968 (eq (car mouse-save-then-kill-posn) (car kill-ring))
969 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
970 ;; If this is the second time we've called
971 ;; mouse-save-then-kill, delete the text from the buffer.
972 (progn
973 (mouse-save-then-kill-delete-region (point) (mark))
974 ;; After we kill, another click counts as "the first time".
975 (setq mouse-save-then-kill-posn nil))
976 ;; This is not a repetition.
977 ;; We are adjusting an old selection or creating a new one.
978 (if (or (and (eq last-command 'mouse-save-then-kill)
979 mouse-save-then-kill-posn)
980 (and mark-active transient-mark-mode)
981 (and (memq last-command
982 '(mouse-drag-region mouse-set-region))
983 (or mark-even-if-inactive
984 (not transient-mark-mode))))
985 ;; We have a selection or suitable region, so adjust it.
986 (let* ((posn (event-start click))
987 (new (posn-point posn)))
988 (select-window (posn-window posn))
989 (if (numberp new)
990 (progn
991 ;; Move whichever end of the region is closer to the click.
992 ;; That is what xterm does, and it seems reasonable.
993 (if (< (abs (- new (point))) (abs (- new (mark t))))
994 (goto-char new)
995 (set-mark new))
996 (setq deactivate-mark nil)))
997 (kill-new (buffer-substring (point) (mark t)) t)
998 (mouse-show-mark))
999 ;; Set the mark where point is, then move where clicked.
1000 (mouse-set-mark-fast click)
1001 (if before-scroll
1002 (goto-char before-scroll))
1003 (exchange-point-and-mark)
1004 (kill-new (buffer-substring (point) (mark t)))
1005 (if window-system
1006 (mouse-show-mark)))
1007 (mouse-set-region-1)
1008 (setq mouse-save-then-kill-posn
1009 (list (car kill-ring) (point) click-posn)))))))
1010 \f
1011 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1012 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1013 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1014 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1015 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1016
1017 ;; An overlay which records the current secondary selection
1018 ;; or else is deleted when there is no secondary selection.
1019 ;; May be nil.
1020 (defvar mouse-secondary-overlay nil)
1021
1022 (defvar mouse-secondary-click-count 0)
1023
1024 ;; A marker which records the specified first end for a secondary selection.
1025 ;; May be nil.
1026 (defvar mouse-secondary-start nil)
1027
1028 (defun mouse-start-secondary (click)
1029 "Set one end of the secondary selection to the position clicked on.
1030 Use \\[mouse-secondary-save-then-kill] to set the other end
1031 and complete the secondary selection."
1032 (interactive "e")
1033 (mouse-minibuffer-check click)
1034 (let ((posn (event-start click)))
1035 (save-excursion
1036 (set-buffer (window-buffer (posn-window posn)))
1037 ;; Cancel any preexisting secondary selection.
1038 (if mouse-secondary-overlay
1039 (delete-overlay mouse-secondary-overlay))
1040 (if (numberp (posn-point posn))
1041 (progn
1042 (or mouse-secondary-start
1043 (setq mouse-secondary-start (make-marker)))
1044 (move-marker mouse-secondary-start (posn-point posn)))))))
1045
1046 (defun mouse-set-secondary (click)
1047 "Set the secondary selection to the text that the mouse is dragged over.
1048 This must be bound to a mouse drag event."
1049 (interactive "e")
1050 (mouse-minibuffer-check click)
1051 (let ((posn (event-start click))
1052 beg
1053 (end (event-end click)))
1054 (save-excursion
1055 (set-buffer (window-buffer (posn-window posn)))
1056 (if (numberp (posn-point posn))
1057 (setq beg (posn-point posn)))
1058 (if mouse-secondary-overlay
1059 (move-overlay mouse-secondary-overlay beg (posn-point end))
1060 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1061 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1062
1063 (defun mouse-drag-secondary (start-event)
1064 "Set the secondary selection to the text that the mouse is dragged over.
1065 Highlight the drag area as you move the mouse.
1066 This must be bound to a button-down mouse event.
1067 The function returns a non-nil value if it creates a secondary selection."
1068 (interactive "e")
1069 (mouse-minibuffer-check start-event)
1070 (let* ((echo-keystrokes 0)
1071 (start-posn (event-start start-event))
1072 (start-point (posn-point start-posn))
1073 (start-window (posn-window start-posn))
1074 (start-frame (window-frame start-window))
1075 (bounds (window-edges start-window))
1076 (top (nth 1 bounds))
1077 (bottom (if (window-minibuffer-p start-window)
1078 (nth 3 bounds)
1079 ;; Don't count the mode line.
1080 (1- (nth 3 bounds))))
1081 (click-count (1- (event-click-count start-event))))
1082 (save-excursion
1083 (set-buffer (window-buffer start-window))
1084 (setq mouse-secondary-click-count click-count)
1085 (or mouse-secondary-overlay
1086 (setq mouse-secondary-overlay
1087 (make-overlay (point) (point))))
1088 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1089 (if (> (mod click-count 3) 0)
1090 ;; Double or triple press: make an initial selection
1091 ;; of one word or line.
1092 (let ((range (mouse-start-end start-point start-point click-count)))
1093 (set-marker mouse-secondary-start nil)
1094 (move-overlay mouse-secondary-overlay 1 1
1095 (window-buffer start-window))
1096 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1097 (window-buffer start-window)))
1098 ;; Single-press: cancel any preexisting secondary selection.
1099 (or mouse-secondary-start
1100 (setq mouse-secondary-start (make-marker)))
1101 (set-marker mouse-secondary-start start-point)
1102 (delete-overlay mouse-secondary-overlay))
1103 (let (event end end-point)
1104 (track-mouse
1105 (while (progn
1106 (setq event (read-event))
1107 (or (mouse-movement-p event)
1108 (eq (car-safe event) 'switch-frame)))
1109
1110 (if (eq (car-safe event) 'switch-frame)
1111 nil
1112 (setq end (event-end event)
1113 end-point (posn-point end))
1114 (cond
1115 ;; Are we moving within the original window?
1116 ((and (eq (posn-window end) start-window)
1117 (integer-or-marker-p end-point))
1118 (let ((range (mouse-start-end start-point end-point
1119 click-count)))
1120 (if (or (/= start-point end-point)
1121 (null (marker-position mouse-secondary-start)))
1122 (progn
1123 (set-marker mouse-secondary-start nil)
1124 (move-overlay mouse-secondary-overlay
1125 (car range) (nth 1 range))))))
1126 (t
1127 (let ((mouse-row (cdr (cdr (mouse-position)))))
1128 (cond
1129 ((null mouse-row))
1130 ((< mouse-row top)
1131 (mouse-scroll-subr start-window (- mouse-row top)
1132 mouse-secondary-overlay start-point))
1133 ((>= mouse-row bottom)
1134 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1135 mouse-secondary-overlay start-point)))))))))
1136
1137 (if (consp event)
1138 (if (marker-position mouse-secondary-start)
1139 (save-window-excursion
1140 (delete-overlay mouse-secondary-overlay)
1141 (x-set-selection 'SECONDARY nil)
1142 (select-window start-window)
1143 (save-excursion
1144 (goto-char mouse-secondary-start)
1145 (sit-for 1)
1146 nil))
1147 (x-set-selection
1148 'SECONDARY
1149 (buffer-substring (overlay-start mouse-secondary-overlay)
1150 (overlay-end mouse-secondary-overlay)))))))))
1151
1152 (defun mouse-yank-secondary (click)
1153 "Insert the secondary selection at the position clicked on.
1154 Move point to the end of the inserted text.
1155 If `mouse-yank-at-point' is non-nil, insert at point
1156 regardless of where you click."
1157 (interactive "e")
1158 ;; Give temporary modes such as isearch a chance to turn off.
1159 (run-hooks 'mouse-leave-buffer-hook)
1160 (or mouse-yank-at-point (mouse-set-point click))
1161 (insert (x-get-selection 'SECONDARY)))
1162
1163 (defun mouse-kill-secondary ()
1164 "Kill the text in the secondary selection.
1165 This is intended more as a keyboard command than as a mouse command
1166 but it can work as either one.
1167
1168 The current buffer (in case of keyboard use), or the buffer clicked on,
1169 must be the one that the secondary selection is in. This requirement
1170 is to prevent accidents."
1171 (interactive)
1172 (let* ((keys (this-command-keys))
1173 (click (elt keys (1- (length keys)))))
1174 (or (eq (overlay-buffer mouse-secondary-overlay)
1175 (if (listp click)
1176 (window-buffer (posn-window (event-start click)))
1177 (current-buffer)))
1178 (error "Select or click on the buffer where the secondary selection is")))
1179 (let (this-command)
1180 (save-excursion
1181 (set-buffer (overlay-buffer mouse-secondary-overlay))
1182 (kill-region (overlay-start mouse-secondary-overlay)
1183 (overlay-end mouse-secondary-overlay))))
1184 (delete-overlay mouse-secondary-overlay)
1185 ;;; (x-set-selection 'SECONDARY nil)
1186 (setq mouse-secondary-overlay nil))
1187
1188 (defun mouse-secondary-save-then-kill (click)
1189 "Save text to point in kill ring; the second time, kill the text.
1190 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1191 If the text between where you did \\[mouse-start-secondary] and where
1192 you use this command matches the text at the front of the kill ring,
1193 this command deletes the text.
1194 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1195 which prepares for a second click with this command to delete the text.
1196
1197 If you have already made a secondary selection in that buffer,
1198 this command extends or retracts the selection to where you click.
1199 If you do this again in a different position, it extends or retracts
1200 again. If you do this twice in the same position, it kills the selection."
1201 (interactive "e")
1202 (mouse-minibuffer-check click)
1203 (let ((posn (event-start click))
1204 (click-posn (posn-point (event-start click)))
1205 ;; Don't let a subsequent kill command append to this one:
1206 ;; prevent setting this-command to kill-region.
1207 (this-command this-command))
1208 (or (eq (window-buffer (posn-window posn))
1209 (or (and mouse-secondary-overlay
1210 (overlay-buffer mouse-secondary-overlay))
1211 (if mouse-secondary-start
1212 (marker-buffer mouse-secondary-start))))
1213 (error "Wrong buffer"))
1214 (save-excursion
1215 (set-buffer (window-buffer (posn-window posn)))
1216 (if (> (mod mouse-secondary-click-count 3) 0)
1217 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1218 (equal click-posn
1219 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1220 ;; Find both ends of the object selected by this click.
1221 (let* ((range
1222 (mouse-start-end click-posn click-posn
1223 mouse-secondary-click-count)))
1224 ;; Move whichever end is closer to the click.
1225 ;; That's what xterm does, and it seems reasonable.
1226 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1227 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1228 (move-overlay mouse-secondary-overlay (car range)
1229 (overlay-end mouse-secondary-overlay))
1230 (move-overlay mouse-secondary-overlay
1231 (overlay-start mouse-secondary-overlay)
1232 (nth 1 range)))
1233 ;; We have already put the old region in the kill ring.
1234 ;; Replace it with the extended region.
1235 ;; (It would be annoying to make a separate entry.)
1236 (kill-new (buffer-substring
1237 (overlay-start mouse-secondary-overlay)
1238 (overlay-end mouse-secondary-overlay)) t)
1239 ;; Arrange for a repeated mouse-3 to kill this region.
1240 (setq mouse-save-then-kill-posn
1241 (list (car kill-ring) (point) click-posn)))
1242 ;; If we click this button again without moving it,
1243 ;; that time kill.
1244 (progn
1245 (mouse-save-then-kill-delete-region
1246 (overlay-start mouse-secondary-overlay)
1247 (overlay-end mouse-secondary-overlay))
1248 (setq mouse-save-then-kill-posn nil)
1249 (setq mouse-secondary-click-count 0)
1250 (delete-overlay mouse-secondary-overlay)))
1251 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1252 mouse-save-then-kill-posn
1253 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1254 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1255 ;; If this is the second time we've called
1256 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1257 (progn
1258 (mouse-save-then-kill-delete-region
1259 (overlay-start mouse-secondary-overlay)
1260 (overlay-end mouse-secondary-overlay))
1261 (setq mouse-save-then-kill-posn nil)
1262 (delete-overlay mouse-secondary-overlay))
1263 (if (overlay-start mouse-secondary-overlay)
1264 ;; We have a selection, so adjust it.
1265 (progn
1266 (if (numberp click-posn)
1267 (progn
1268 ;; Move whichever end of the region is closer to the click.
1269 ;; That is what xterm does, and it seems reasonable.
1270 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1271 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1272 (move-overlay mouse-secondary-overlay click-posn
1273 (overlay-end mouse-secondary-overlay))
1274 (move-overlay mouse-secondary-overlay
1275 (overlay-start mouse-secondary-overlay)
1276 click-posn))
1277 (setq deactivate-mark nil)))
1278 (if (eq last-command 'mouse-secondary-save-then-kill)
1279 ;; If the front of the kill ring comes from
1280 ;; an immediately previous use of this command,
1281 ;; replace it with the extended region.
1282 ;; (It would be annoying to make a separate entry.)
1283 (kill-new (buffer-substring
1284 (overlay-start mouse-secondary-overlay)
1285 (overlay-end mouse-secondary-overlay)) t)
1286 (let (deactivate-mark)
1287 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1288 (overlay-end mouse-secondary-overlay)))))
1289 (if mouse-secondary-start
1290 ;; All we have is one end of a selection,
1291 ;; so put the other end here.
1292 (let ((start (+ 0 mouse-secondary-start)))
1293 (kill-ring-save start click-posn)
1294 (if mouse-secondary-overlay
1295 (move-overlay mouse-secondary-overlay start click-posn)
1296 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1297 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1298 (setq mouse-save-then-kill-posn
1299 (list (car kill-ring) (point) click-posn))))
1300 (if (overlay-buffer mouse-secondary-overlay)
1301 (x-set-selection 'SECONDARY
1302 (buffer-substring
1303 (overlay-start mouse-secondary-overlay)
1304 (overlay-end mouse-secondary-overlay)))))))
1305 \f
1306 (defcustom mouse-buffer-menu-maxlen 20
1307 "*Number of buffers in one pane (submenu) of the buffer menu.
1308 If we have lots of buffers, divide them into groups of
1309 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1310 :type 'integer
1311 :group 'mouse)
1312
1313 (defcustom mouse-buffer-menu-mode-mult 4
1314 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1315 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1316 will split the buffer menu by the major modes (see
1317 `mouse-buffer-menu-mode-groups') or just by menu length.
1318 Set to 1 (or even 0!) if you want to group by major mode always, and to
1319 a large number if you prefer a mixed multitude. The default is 4."
1320 :type 'integer
1321 :group 'mouse
1322 :version "20.3")
1323
1324 (defvar mouse-buffer-menu-mode-groups
1325 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1326 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1327 . "Mail/News")
1328 ("\\<C\\>" . "C")
1329 ("ObjC" . "C")
1330 ("Text" . "Text")
1331 ("Outline" . "Text")
1332 ("Lisp" . "Lisp"))
1333 "How to group various major modes together in \\[mouse-buffer-menu].
1334 Each element has the form (REGEXP . GROUPNAME).
1335 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1336
1337 (defun mouse-buffer-menu (event)
1338 "Pop up a menu of buffers for selection with the mouse.
1339 This switches buffers in the window that you clicked on,
1340 and selects that window."
1341 (interactive "e")
1342 (mouse-minibuffer-check event)
1343 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1344 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1345 (let ((tail buffers))
1346 (while tail
1347 ;; Divide all buffers into buckets for various major modes.
1348 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1349 (with-current-buffer (car tail)
1350 (let* ((adjusted-major-mode major-mode) elt)
1351 (let ((tail mouse-buffer-menu-mode-groups))
1352 (while tail
1353 (if (string-match (car (car tail)) mode-name)
1354 (setq adjusted-major-mode (cdr (car tail))))
1355 (setq tail (cdr tail))))
1356 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1357 (if (null elt)
1358 (setq elt (list adjusted-major-mode
1359 (if (stringp adjusted-major-mode)
1360 adjusted-major-mode
1361 mode-name))
1362 split-by-major-mode (cons elt split-by-major-mode)))
1363 (or (memq (car tail) (cdr (cdr elt)))
1364 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1365 (setq tail (cdr tail))))
1366 ;; Compute the sum of squares of sizes of the major-mode buckets.
1367 (let ((tail split-by-major-mode))
1368 (setq sum-of-squares 0)
1369 (while tail
1370 (setq sum-of-squares
1371 (+ sum-of-squares
1372 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1373 (setq tail (cdr tail))))
1374 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1375 (* (length buffers) (length buffers)))
1376 ;; Subdividing by major modes really helps, so let's do it.
1377 (let (subdivided-menus (buffers-left (length buffers)))
1378 ;; Sort the list to put the most popular major modes first.
1379 (setq split-by-major-mode
1380 (sort split-by-major-mode
1381 (function (lambda (elt1 elt2)
1382 (> (length elt1) (length elt2))))))
1383 ;; Make a separate submenu for each major mode
1384 ;; that has more than one buffer,
1385 ;; unless all the remaining buffers are less than 1/10 of them.
1386 (while (and split-by-major-mode
1387 (and (> (length (car split-by-major-mode)) 3)
1388 (> (* buffers-left 10) (length buffers))))
1389 (setq subdivided-menus
1390 (cons (cons
1391 (nth 1 (car split-by-major-mode))
1392 (mouse-buffer-menu-alist
1393 (cdr (cdr (car split-by-major-mode)))))
1394 subdivided-menus))
1395 (setq buffers-left
1396 (- buffers-left (length (cdr (car split-by-major-mode)))))
1397 (setq split-by-major-mode (cdr split-by-major-mode)))
1398 ;; If any major modes are left over,
1399 ;; make a single submenu for them.
1400 (if split-by-major-mode
1401 (setq subdivided-menus
1402 (cons (cons
1403 "Others"
1404 (mouse-buffer-menu-alist
1405 ;; we don't need split-by-major-mode any
1406 ;; more, so we can ditch it with nconc.
1407 (apply 'nconc (mapcar 'cddr split-by-major-mode))))
1408 subdivided-menus)))
1409 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1410 (progn
1411 (setq alist (mouse-buffer-menu-alist buffers))
1412 (setq menu (cons "Buffer Menu"
1413 (mouse-buffer-menu-split "Select Buffer" alist)))))
1414 (let ((buf (x-popup-menu event menu))
1415 (window (posn-window (event-start event))))
1416 (when buf
1417 (or (framep window) (select-window window))
1418 (switch-to-buffer buf)))))
1419
1420 (defun mouse-buffer-menu-alist (buffers)
1421 (let (tail
1422 (maxlen 0)
1423 head)
1424 (setq buffers
1425 (sort buffers
1426 (function (lambda (elt1 elt2)
1427 (string< (buffer-name elt1) (buffer-name elt2))))))
1428 (setq tail buffers)
1429 (while tail
1430 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1431 (setq maxlen
1432 (max maxlen
1433 (length (buffer-name (car tail))))))
1434 (setq tail (cdr tail)))
1435 (setq tail buffers)
1436 (while tail
1437 (let ((elt (car tail)))
1438 (if (/= (aref (buffer-name elt) 0) ?\ )
1439 (setq head
1440 (cons
1441 (cons
1442 (format
1443 (format "%%%ds %%s%%s %%s" maxlen)
1444 (buffer-name elt)
1445 (if (buffer-modified-p elt) "*" " ")
1446 (save-excursion
1447 (set-buffer elt)
1448 (if buffer-read-only "%" " "))
1449 (or (buffer-file-name elt)
1450 (save-excursion
1451 (set-buffer elt)
1452 (if list-buffers-directory
1453 (expand-file-name
1454 list-buffers-directory)))
1455 ""))
1456 elt)
1457 head))))
1458 (setq tail (cdr tail)))
1459 ;; Compensate for the reversal that the above loop does.
1460 (nreverse head)))
1461
1462 (defun mouse-buffer-menu-split (title alist)
1463 ;; If we have lots of buffers, divide them into groups of 20
1464 ;; and make a pane (or submenu) for each one.
1465 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1466 (let ((alist alist) sublists next
1467 (i 1))
1468 (while alist
1469 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1470 ;; and make them the next element of sublist.
1471 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1472 (if next
1473 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1474 nil))
1475 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1476 sublists))
1477 (setq i (1+ i))
1478 (setq alist next))
1479 (nreverse sublists))
1480 ;; Few buffers--put them all in one pane.
1481 (list (cons title alist))))
1482 \f
1483 ;;; These need to be rewritten for the new scroll bar implementation.
1484
1485 ;;;!! ;; Commands for the scroll bar.
1486 ;;;!!
1487 ;;;!! (defun mouse-scroll-down (click)
1488 ;;;!! (interactive "@e")
1489 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1490 ;;;!!
1491 ;;;!! (defun mouse-scroll-up (click)
1492 ;;;!! (interactive "@e")
1493 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1494 ;;;!!
1495 ;;;!! (defun mouse-scroll-down-full ()
1496 ;;;!! (interactive "@")
1497 ;;;!! (scroll-down nil))
1498 ;;;!!
1499 ;;;!! (defun mouse-scroll-up-full ()
1500 ;;;!! (interactive "@")
1501 ;;;!! (scroll-up nil))
1502 ;;;!!
1503 ;;;!! (defun mouse-scroll-move-cursor (click)
1504 ;;;!! (interactive "@e")
1505 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1506 ;;;!!
1507 ;;;!! (defun mouse-scroll-absolute (event)
1508 ;;;!! (interactive "@e")
1509 ;;;!! (let* ((pos (car event))
1510 ;;;!! (position (car pos))
1511 ;;;!! (length (car (cdr pos))))
1512 ;;;!! (if (<= length 0) (setq length 1))
1513 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1514 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1515 ;;;!! position)
1516 ;;;!! length)
1517 ;;;!! scale-factor)))
1518 ;;;!! (goto-char newpos)
1519 ;;;!! (recenter '(4)))))
1520 ;;;!!
1521 ;;;!! (defun mouse-scroll-left (click)
1522 ;;;!! (interactive "@e")
1523 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1524 ;;;!!
1525 ;;;!! (defun mouse-scroll-right (click)
1526 ;;;!! (interactive "@e")
1527 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1528 ;;;!!
1529 ;;;!! (defun mouse-scroll-left-full ()
1530 ;;;!! (interactive "@")
1531 ;;;!! (scroll-left nil))
1532 ;;;!!
1533 ;;;!! (defun mouse-scroll-right-full ()
1534 ;;;!! (interactive "@")
1535 ;;;!! (scroll-right nil))
1536 ;;;!!
1537 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1538 ;;;!! (interactive "@e")
1539 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1540 ;;;!!
1541 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1542 ;;;!! (interactive "@e")
1543 ;;;!! (let* ((pos (car event))
1544 ;;;!! (position (car pos))
1545 ;;;!! (length (car (cdr pos))))
1546 ;;;!! (set-window-hscroll (selected-window) 33)))
1547 ;;;!!
1548 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1549 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1550 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1551 ;;;!!
1552 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1553 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1554 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1555 ;;;!!
1556 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1557 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1558 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1559 ;;;!!
1560 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1561 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1562 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1563 ;;;!!
1564 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1565 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1566 ;;;!! 'mouse-scroll-absolute-horizontally)
1567 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1568 ;;;!!
1569 ;;;!! (global-set-key [horizontal-slider mouse-1]
1570 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1571 ;;;!! (global-set-key [horizontal-slider mouse-2]
1572 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1573 ;;;!! (global-set-key [horizontal-slider mouse-3]
1574 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1575 ;;;!!
1576 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1577 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1578 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1579 ;;;!!
1580 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1581 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1582 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1583 ;;;!!
1584 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1585 ;;;!! 'mouse-split-window-horizontally)
1586 ;;;!! (global-set-key [mode-line S-mouse-2]
1587 ;;;!! 'mouse-split-window-horizontally)
1588 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1589 ;;;!! 'mouse-split-window)
1590 \f
1591 ;;;!! ;;;;
1592 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1593 ;;;!! ;;;; are of the form:
1594 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1595 ;;;!! ;;
1596 ;;;!! ;;;;
1597 ;;;!! ;;;; Dynamically track mouse coordinates
1598 ;;;!! ;;;;
1599 ;;;!! ;;
1600 ;;;!! ;;(defun track-mouse (event)
1601 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1602 ;;;!! ;; (interactive "@e")
1603 ;;;!! ;; (while mouse-grabbed
1604 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1605 ;;;!! ;; (abs-x (car pos))
1606 ;;;!! ;; (abs-y (cdr pos))
1607 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1608 ;;;!! ;; (list (car pos) (cdr pos))
1609 ;;;!! ;; (selected-window))))
1610 ;;;!! ;; (if (consp relative-coordinate)
1611 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1612 ;;;!! ;; (car relative-coordinate)
1613 ;;;!! ;; (car (cdr relative-coordinate)))
1614 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1615 ;;;!!
1616 ;;;!! ;;
1617 ;;;!! ;; Dynamically put a box around the line indicated by point
1618 ;;;!! ;;
1619 ;;;!! ;;
1620 ;;;!! ;;(require 'backquote)
1621 ;;;!! ;;
1622 ;;;!! ;;(defun mouse-select-buffer-line (event)
1623 ;;;!! ;; (interactive "@e")
1624 ;;;!! ;; (let ((relative-coordinate
1625 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1626 ;;;!! ;; (abs-y (car (cdr (car event)))))
1627 ;;;!! ;; (if (consp relative-coordinate)
1628 ;;;!! ;; (progn
1629 ;;;!! ;; (save-excursion
1630 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1631 ;;;!! ;; (x-draw-rectangle
1632 ;;;!! ;; (selected-screen)
1633 ;;;!! ;; abs-y 0
1634 ;;;!! ;; (save-excursion
1635 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1636 ;;;!! ;; (end-of-line)
1637 ;;;!! ;; (push-mark nil t)
1638 ;;;!! ;; (beginning-of-line)
1639 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1640 ;;;!! ;; (sit-for 1)
1641 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1642 ;;;!! ;;
1643 ;;;!! ;;(defvar last-line-drawn nil)
1644 ;;;!! ;;(defvar begin-delim "[^ \t]")
1645 ;;;!! ;;(defvar end-delim "[^ \t]")
1646 ;;;!! ;;
1647 ;;;!! ;;(defun mouse-boxing (event)
1648 ;;;!! ;; (interactive "@e")
1649 ;;;!! ;; (save-excursion
1650 ;;;!! ;; (let ((screen (selected-screen)))
1651 ;;;!! ;; (while (= (x-mouse-events) 0)
1652 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1653 ;;;!! ;; (abs-x (car pos))
1654 ;;;!! ;; (abs-y (cdr pos))
1655 ;;;!! ;; (relative-coordinate
1656 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1657 ;;;!! ;; (selected-window)))
1658 ;;;!! ;; (begin-reg nil)
1659 ;;;!! ;; (end-reg nil)
1660 ;;;!! ;; (end-column nil)
1661 ;;;!! ;; (begin-column nil))
1662 ;;;!! ;; (if (and (consp relative-coordinate)
1663 ;;;!! ;; (or (not last-line-drawn)
1664 ;;;!! ;; (not (= last-line-drawn abs-y))))
1665 ;;;!! ;; (progn
1666 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1667 ;;;!! ;; (if (= (following-char) 10)
1668 ;;;!! ;; ()
1669 ;;;!! ;; (progn
1670 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1671 ;;;!! ;; (setq begin-column (1- (current-column)))
1672 ;;;!! ;; (end-of-line)
1673 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1674 ;;;!! ;; (setq end-column (1+ (current-column)))
1675 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1676 ;;;!! ;; (x-draw-rectangle screen
1677 ;;;!! ;; (setq last-line-drawn abs-y)
1678 ;;;!! ;; begin-column
1679 ;;;!! ;; (- end-column begin-column) 1))))))))))
1680 ;;;!! ;;
1681 ;;;!! ;;(defun mouse-erase-box ()
1682 ;;;!! ;; (interactive)
1683 ;;;!! ;; (if last-line-drawn
1684 ;;;!! ;; (progn
1685 ;;;!! ;; (x-erase-rectangle (selected-screen))
1686 ;;;!! ;; (setq last-line-drawn nil))))
1687 ;;;!!
1688 ;;;!! ;;; (defun test-x-rectangle ()
1689 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1690 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1691 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1692 ;;;!!
1693 ;;;!! ;;
1694 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1695 ;;;!! ;;
1696 ;;;!!
1697 ;;;!! (defvar double-start nil)
1698 ;;;!! (defconst double-click-interval 300
1699 ;;;!! "Max ticks between clicks")
1700 ;;;!!
1701 ;;;!! (defun double-down (event)
1702 ;;;!! (interactive "@e")
1703 ;;;!! (if double-start
1704 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1705 ;;;!! (if (< interval double-click-interval)
1706 ;;;!! (progn
1707 ;;;!! (backward-up-list 1)
1708 ;;;!! ;; (message "Interval %d" interval)
1709 ;;;!! (sleep-for 1)))
1710 ;;;!! (setq double-start nil))
1711 ;;;!! (setq double-start (nth 4 event))))
1712 ;;;!!
1713 ;;;!! (defun double-up (event)
1714 ;;;!! (interactive "@e")
1715 ;;;!! (and double-start
1716 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1717 ;;;!! (setq double-start nil)))
1718 ;;;!!
1719 ;;;!! ;;; (defun x-test-doubleclick ()
1720 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1721 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1722 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1723 ;;;!!
1724 ;;;!! ;;
1725 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1726 ;;;!! ;;
1727 ;;;!!
1728 ;;;!! (defvar scrolled-lines 0)
1729 ;;;!! (defconst scroll-speed 1)
1730 ;;;!!
1731 ;;;!! (defun incr-scroll-down (event)
1732 ;;;!! (interactive "@e")
1733 ;;;!! (setq scrolled-lines 0)
1734 ;;;!! (incremental-scroll scroll-speed))
1735 ;;;!!
1736 ;;;!! (defun incr-scroll-up (event)
1737 ;;;!! (interactive "@e")
1738 ;;;!! (setq scrolled-lines 0)
1739 ;;;!! (incremental-scroll (- scroll-speed)))
1740 ;;;!!
1741 ;;;!! (defun incremental-scroll (n)
1742 ;;;!! (while (= (x-mouse-events) 0)
1743 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1744 ;;;!! (scroll-down n)
1745 ;;;!! (sit-for 300 t)))
1746 ;;;!!
1747 ;;;!! (defun incr-scroll-stop (event)
1748 ;;;!! (interactive "@e")
1749 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1750 ;;;!! (setq scrolled-lines 0)
1751 ;;;!! (sleep-for 1))
1752 ;;;!!
1753 ;;;!! ;;; (defun x-testing-scroll ()
1754 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1755 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1756 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1757 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1758 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1759 ;;;!!
1760 ;;;!! ;;
1761 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1762 ;;;!! ;;
1763 ;;;!!
1764 ;;;!! (defun mouse-kill-rectangle (event)
1765 ;;;!! "Kill the rectangle between point and the mouse cursor."
1766 ;;;!! (interactive "@e")
1767 ;;;!! (let ((point-save (point)))
1768 ;;;!! (save-excursion
1769 ;;;!! (mouse-set-point event)
1770 ;;;!! (push-mark nil t)
1771 ;;;!! (if (> point-save (point))
1772 ;;;!! (kill-rectangle (point) point-save)
1773 ;;;!! (kill-rectangle point-save (point))))))
1774 ;;;!!
1775 ;;;!! (defun mouse-open-rectangle (event)
1776 ;;;!! "Kill the rectangle between point and the mouse cursor."
1777 ;;;!! (interactive "@e")
1778 ;;;!! (let ((point-save (point)))
1779 ;;;!! (save-excursion
1780 ;;;!! (mouse-set-point event)
1781 ;;;!! (push-mark nil t)
1782 ;;;!! (if (> point-save (point))
1783 ;;;!! (open-rectangle (point) point-save)
1784 ;;;!! (open-rectangle point-save (point))))))
1785 ;;;!!
1786 ;;;!! ;; Must be a better way to do this.
1787 ;;;!!
1788 ;;;!! (defun mouse-multiple-insert (n char)
1789 ;;;!! (while (> n 0)
1790 ;;;!! (insert char)
1791 ;;;!! (setq n (1- n))))
1792 ;;;!!
1793 ;;;!! ;; What this could do is not finalize until button was released.
1794 ;;;!!
1795 ;;;!! (defun mouse-move-text (event)
1796 ;;;!! "Move text from point to cursor position, inserting spaces."
1797 ;;;!! (interactive "@e")
1798 ;;;!! (let* ((relative-coordinate
1799 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1800 ;;;!! (if (consp relative-coordinate)
1801 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1802 ;;;!! (delete-char
1803 ;;;!! (- (car relative-coordinate) (current-column))))
1804 ;;;!! ((< (current-column) (car relative-coordinate))
1805 ;;;!! (mouse-multiple-insert
1806 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1807 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1808 \f
1809 ;; Choose a completion with the mouse.
1810
1811 (defun mouse-choose-completion (event)
1812 "Click on an alternative in the `*Completions*' buffer to choose it."
1813 (interactive "e")
1814 ;; Give temporary modes such as isearch a chance to turn off.
1815 (run-hooks 'mouse-leave-buffer-hook)
1816 (let ((buffer (window-buffer))
1817 choice
1818 base-size)
1819 (save-excursion
1820 (set-buffer (window-buffer (posn-window (event-start event))))
1821 (if completion-reference-buffer
1822 (setq buffer completion-reference-buffer))
1823 (setq base-size completion-base-size)
1824 (save-excursion
1825 (goto-char (posn-point (event-start event)))
1826 (let (beg end)
1827 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1828 (setq end (point) beg (1+ (point))))
1829 (if (null beg)
1830 (error "No completion here"))
1831 (setq beg (previous-single-property-change beg 'mouse-face))
1832 (setq end (or (next-single-property-change end 'mouse-face)
1833 (point-max)))
1834 (setq choice (buffer-substring beg end)))))
1835 (let ((owindow (selected-window)))
1836 (select-window (posn-window (event-start event)))
1837 (if (and (one-window-p t 'selected-frame)
1838 (window-dedicated-p (selected-window)))
1839 ;; This is a special buffer's frame
1840 (iconify-frame (selected-frame))
1841 (or (window-dedicated-p (selected-window))
1842 (bury-buffer)))
1843 (select-window owindow))
1844 (choose-completion-string choice buffer base-size)))
1845 \f
1846 ;; Font selection.
1847
1848 (defun font-menu-add-default ()
1849 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1850 (font-alist x-fixed-font-alist)
1851 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1852 (if (assoc "Default" elt)
1853 (delete (assoc "Default" elt) elt))
1854 (setcdr elt
1855 (cons (list "Default"
1856 (cdr (assq 'font (frame-parameters (selected-frame)))))
1857 (cdr elt)))))
1858
1859 (defvar x-fixed-font-alist
1860 '("Font menu"
1861 ("Misc"
1862 ;; For these, we specify the pixel height and width.
1863 ("fixed" "fixed")
1864 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1865 ("6x12"
1866 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1867 ("6x13"
1868 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1869 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1870 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1871 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1872 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1873 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1874 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1875 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1876 ("")
1877 ("clean 5x8"
1878 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1879 ("clean 6x8"
1880 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1881 ("clean 8x8"
1882 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1883 ("clean 8x10"
1884 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1885 ("clean 8x14"
1886 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1887 ("clean 8x16"
1888 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1889 ("")
1890 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
1891 ;;; We don't seem to have these; who knows what they are.
1892 ;;; ("fg-18" "fg-18")
1893 ;;; ("fg-25" "fg-25")
1894 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1895 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1896 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1897 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1898 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1899 ("Courier"
1900 ;; For these, we specify the point height.
1901 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1902 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1903 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1904 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1905 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1906 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1907 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1908 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1909 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1910 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1911 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1912 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1913 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1914 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1915 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1916 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1917 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1918 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1919 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1920 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1921 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1922 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1923 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1924 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
1925 )
1926 "X fonts suitable for use in Emacs.")
1927
1928 (defun mouse-set-font (&rest fonts)
1929 "Select an emacs font from a list of known good fonts and fontsets."
1930 (interactive
1931 (x-popup-menu
1932 last-nonmenu-event
1933 ;; Append list of fontsets currently defined.
1934 (append x-fixed-font-alist (list (generate-fontset-menu)))))
1935 (if fonts
1936 (let (font)
1937 (while fonts
1938 (condition-case nil
1939 (progn
1940 (set-default-font (car fonts))
1941 (setq font (car fonts))
1942 (setq fonts nil))
1943 (error
1944 (setq fonts (cdr fonts)))))
1945 (if (null font)
1946 (error "Font not found")))))
1947 \f
1948 ;;; Bindings for mouse commands.
1949
1950 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1951 (global-set-key [mouse-1] 'mouse-set-point)
1952 (global-set-key [drag-mouse-1] 'mouse-set-region)
1953
1954 ;; These are tested for in mouse-drag-region.
1955 (global-set-key [double-mouse-1] 'mouse-set-point)
1956 (global-set-key [triple-mouse-1] 'mouse-set-point)
1957
1958 (global-set-key [mouse-2] 'mouse-yank-at-click)
1959 (global-set-key [mouse-3] 'mouse-save-then-kill)
1960
1961 ;; By binding these to down-going events, we let the user use the up-going
1962 ;; event to make the selection, saving a click.
1963 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1964 (if (not (eq system-type 'ms-dos))
1965 (global-set-key [S-down-mouse-1] 'mouse-set-font))
1966 ;; C-down-mouse-2 is bound in facemenu.el.
1967 (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1968
1969
1970 ;; Replaced with dragging mouse-1
1971 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1972
1973 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1974 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1975 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1976 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1977 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1978 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1979 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1980 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1981 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1982 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1983
1984 (provide 'mouse)
1985
1986 ;;; mouse.el ends here