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