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