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