(tibetan-pre-write-conversion): Cancel previous
[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 mouse-drag-overlay start-point)
589 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
590 (if (consp event)
591 (let ((fun (key-binding (vector (car event)))))
592 ;; Run the binding of the terminating up-event, if possible.
593 ;; In the case of a multiple click, it gives the wrong results,
594 ;; because it would fail to set up a region.
595 (if (not (= (overlay-start mouse-drag-overlay)
596 (overlay-end mouse-drag-overlay)))
597 (let* ((stop-point
598 (if (numberp (posn-point (event-end event)))
599 (posn-point (event-end event))
600 last-end-point))
601 ;; The end that comes from where we ended the drag.
602 ;; Point goes here.
603 (region-termination
604 (if (and stop-point (< stop-point start-point))
605 (overlay-start mouse-drag-overlay)
606 (overlay-end mouse-drag-overlay)))
607 ;; The end that comes from where we started the drag.
608 ;; Mark goes there.
609 (region-commencement
610 (- (+ (overlay-end mouse-drag-overlay)
611 (overlay-start mouse-drag-overlay))
612 region-termination))
613 last-command this-command)
614 (push-mark region-commencement t t)
615 (goto-char region-termination)
616 ;; Don't let copy-region-as-kill set deactivate-mark.
617 (let (deactivate-mark)
618 (copy-region-as-kill (point) (mark t)))
619 (let ((buffer (current-buffer)))
620 (mouse-show-mark)
621 ;; mouse-show-mark can call read-event,
622 ;; and that means the Emacs server could switch buffers
623 ;; under us. If that happened,
624 ;; avoid trying to use the region.
625 (and (mark t) mark-active
626 (eq buffer (current-buffer))
627 (mouse-set-region-1))))
628 (delete-overlay mouse-drag-overlay)
629 ;; Run the binding of the terminating up-event.
630 (if (fboundp fun)
631 (setq unread-command-events
632 (cons event unread-command-events)))))
633 (delete-overlay mouse-drag-overlay)))))
634 \f
635 ;; Commands to handle xterm-style multiple clicks.
636
637 (defun mouse-skip-word (dir)
638 "Skip over word, over whitespace, or over identical punctuation.
639 If DIR is positive skip forward; if negative, skip backward."
640 (let* ((char (following-char))
641 (syntax (char-to-string (char-syntax char))))
642 (cond ((string= syntax "w")
643 ;; Here, we can't use skip-syntax-forward/backward because
644 ;; they don't pay attention to word-separating-categories,
645 ;; and thus they will skip over a true word boundary. So,
646 ;; we simularte the original behaviour by using
647 ;; forward-word.
648 (if (< dir 0)
649 (if (not (looking-at "\\<"))
650 (forward-word -1))
651 (if (or (looking-at "\\<") (not (looking-at "\\>")))
652 (forward-word 1))))
653 ((string= syntax " ")
654 (if (< dir 0)
655 (skip-syntax-backward syntax)
656 (skip-syntax-forward syntax)))
657 ((string= syntax "_")
658 (if (< dir 0)
659 (skip-syntax-backward "w_")
660 (skip-syntax-forward "w_")))
661 ((< dir 0)
662 (while (and (not (bobp)) (= (preceding-char) char))
663 (forward-char -1)))
664 (t
665 (while (and (not (eobp)) (= (following-char) char))
666 (forward-char 1))))))
667
668 ;; Return a list of region bounds based on START and END according to MODE.
669 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
670 ;; If MODE is 1 then set point to start of word at (min START END),
671 ;; mark to end of word at (max START END).
672 ;; If MODE is 2 then do the same for lines.
673 (defun mouse-start-end (start end mode)
674 (if (> start end)
675 (let ((temp start))
676 (setq start end
677 end temp)))
678 (setq mode (mod mode 3))
679 (cond ((= mode 0)
680 (list start end))
681 ((and (= mode 1)
682 (= start end)
683 (char-after start)
684 (= (char-syntax (char-after start)) ?\())
685 (list start
686 (save-excursion
687 (goto-char start)
688 (forward-sexp 1)
689 (point))))
690 ((and (= mode 1)
691 (= start end)
692 (char-after start)
693 (= (char-syntax (char-after start)) ?\)))
694 (list (save-excursion
695 (goto-char (1+ start))
696 (backward-sexp 1)
697 (point))
698 (1+ start)))
699 ((and (= mode 1)
700 (= start end)
701 (char-after start)
702 (= (char-syntax (char-after start)) ?\"))
703 (let ((open (or (eq start (point-min))
704 (save-excursion
705 (goto-char (- start 1))
706 (looking-at "\\s(\\|\\s \\|\\s>")))))
707 (if open
708 (list start
709 (save-excursion
710 (condition-case nil
711 (progn
712 (goto-char start)
713 (forward-sexp 1)
714 (point))
715 (error end))))
716 (list (save-excursion
717 (condition-case nil
718 (progn
719 (goto-char (1+ start))
720 (backward-sexp 1)
721 (point))
722 (error end)))
723 (1+ start)))))
724 ((= mode 1)
725 (list (save-excursion
726 (goto-char start)
727 (mouse-skip-word -1)
728 (point))
729 (save-excursion
730 (goto-char end)
731 (mouse-skip-word 1)
732 (point))))
733 ((= mode 2)
734 (list (save-excursion
735 (goto-char start)
736 (beginning-of-line 1)
737 (point))
738 (save-excursion
739 (goto-char end)
740 (forward-line 1)
741 (point))))))
742 \f
743 ;; Subroutine: set the mark where CLICK happened,
744 ;; but don't do anything else.
745 (defun mouse-set-mark-fast (click)
746 (mouse-minibuffer-check click)
747 (let ((posn (event-start click)))
748 (select-window (posn-window posn))
749 (if (numberp (posn-point posn))
750 (push-mark (posn-point posn) t t))))
751
752 (defun mouse-undouble-last-event (events)
753 (let* ((index (1- (length events)))
754 (last (nthcdr index events))
755 (event (car last))
756 (basic (event-basic-type event))
757 (old-modifiers (event-modifiers event))
758 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
759 (new
760 (if (consp event)
761 ;; Use reverse, not nreverse, since event-modifiers
762 ;; does not copy the list it returns.
763 (cons (event-convert-list (reverse (cons basic modifiers)))
764 (cdr event))
765 event)))
766 (setcar last new)
767 (if (and (not (equal modifiers old-modifiers))
768 (key-binding (apply 'vector events)))
769 t
770 (setcar last event)
771 nil)))
772
773 ;; Momentarily show where the mark is, if highlighting doesn't show it.
774
775 (defvar mouse-region-delete-keys '([delete])
776 "List of keys which shall cause the mouse region to be deleted.")
777
778 (defun mouse-show-mark ()
779 (if transient-mark-mode
780 (if window-system
781 (delete-overlay mouse-drag-overlay))
782 (if window-system
783 (let ((inhibit-quit t)
784 (echo-keystrokes 0)
785 event events key ignore
786 x-lost-selection-hooks)
787 (add-hook 'x-lost-selection-hooks
788 '(lambda (seltype)
789 (if (eq seltype 'PRIMARY)
790 (progn (setq ignore t)
791 (throw 'mouse-show-mark t)))))
792 (move-overlay mouse-drag-overlay (point) (mark t))
793 (catch 'mouse-show-mark
794 ;; In this loop, execute scroll bar and switch-frame events.
795 ;; Also ignore down-events that are undefined.
796 (while (progn (setq event (read-event))
797 (setq events (append events (list event)))
798 (setq key (apply 'vector events))
799 (or (and (consp event)
800 (eq (car event) 'switch-frame))
801 (and (consp event)
802 (eq (posn-point (event-end event))
803 'vertical-scroll-bar))
804 (and (memq 'down (event-modifiers event))
805 (not (key-binding key))
806 (not (mouse-undouble-last-event events))
807 (not (member key mouse-region-delete-keys)))))
808 (and (consp event)
809 (or (eq (car event) 'switch-frame)
810 (eq (posn-point (event-end event))
811 'vertical-scroll-bar))
812 (let ((keys (vector 'vertical-scroll-bar event)))
813 (and (key-binding keys)
814 (progn
815 (call-interactively (key-binding keys)
816 nil keys)
817 (setq events nil)))))))
818 ;; If we lost the selection, just turn off the highlighting.
819 (if ignore
820 nil
821 ;; For certain special keys, delete the region.
822 (if (member key mouse-region-delete-keys)
823 (delete-region (overlay-start mouse-drag-overlay)
824 (overlay-end mouse-drag-overlay))
825 ;; Otherwise, unread the key so it gets executed normally.
826 (setq unread-command-events
827 (nconc events unread-command-events))))
828 (setq quit-flag nil)
829 (delete-overlay mouse-drag-overlay))
830 (save-excursion
831 (goto-char (mark t))
832 (sit-for 1)))))
833
834 (defun mouse-set-mark (click)
835 "Set mark at the position clicked on with the mouse.
836 Display cursor at that position for a second.
837 This must be bound to a mouse click."
838 (interactive "e")
839 (mouse-minibuffer-check click)
840 (select-window (posn-window (event-start click)))
841 ;; We don't use save-excursion because that preserves the mark too.
842 (let ((point-save (point)))
843 (unwind-protect
844 (progn (mouse-set-point click)
845 (push-mark nil t t)
846 (or transient-mark-mode
847 (sit-for 1)))
848 (goto-char point-save))))
849
850 (defun mouse-kill (click)
851 "Kill the region between point and the mouse click.
852 The text is saved in the kill ring, as with \\[kill-region]."
853 (interactive "e")
854 (mouse-minibuffer-check click)
855 (let* ((posn (event-start click))
856 (click-posn (posn-point posn)))
857 (select-window (posn-window posn))
858 (if (numberp click-posn)
859 (kill-region (min (point) click-posn)
860 (max (point) click-posn)))))
861
862 (defun mouse-yank-at-click (click arg)
863 "Insert the last stretch of killed text at the position clicked on.
864 Also move point to one end of the text thus inserted (normally the end).
865 Prefix arguments are interpreted as with \\[yank].
866 If `mouse-yank-at-point' is non-nil, insert at point
867 regardless of where you click."
868 (interactive "e\nP")
869 ;; Give temporary modes such as isearch a chance to turn off.
870 (run-hooks 'mouse-leave-buffer-hook)
871 (or mouse-yank-at-point (mouse-set-point click))
872 (setq this-command 'yank)
873 (setq mouse-selection-click-count 0)
874 (yank arg))
875
876 (defun mouse-kill-ring-save (click)
877 "Copy the region between point and the mouse click in the kill ring.
878 This does not delete the region; it acts like \\[kill-ring-save]."
879 (interactive "e")
880 (mouse-set-mark-fast click)
881 (let (this-command last-command)
882 (kill-ring-save (point) (mark t)))
883 (mouse-show-mark))
884
885 ;;; This function used to delete the text between point and the mouse
886 ;;; whenever it was equal to the front of the kill ring, but some
887 ;;; people found that confusing.
888
889 ;;; A list (TEXT START END), describing the text and position of the last
890 ;;; invocation of mouse-save-then-kill.
891 (defvar mouse-save-then-kill-posn nil)
892
893 (defun mouse-save-then-kill-delete-region (beg end)
894 ;; We must make our own undo boundaries
895 ;; because they happen automatically only for the current buffer.
896 (undo-boundary)
897 (if (or (= beg end) (eq buffer-undo-list t))
898 ;; If we have no undo list in this buffer,
899 ;; just delete.
900 (delete-region beg end)
901 ;; Delete, but make the undo-list entry share with the kill ring.
902 ;; First, delete just one char, so in case buffer is being modified
903 ;; for the first time, the undo list records that fact.
904 (let (before-change-function after-change-function
905 before-change-functions after-change-functions)
906 (delete-region beg
907 (+ beg (if (> end beg) 1 -1))))
908 (let ((buffer-undo-list buffer-undo-list))
909 ;; Undo that deletion--but don't change the undo list!
910 (let (before-change-function after-change-function
911 before-change-functions after-change-functions)
912 (primitive-undo 1 buffer-undo-list))
913 ;; Now delete the rest of the specified region,
914 ;; but don't record it.
915 (setq buffer-undo-list t)
916 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
917 (error "Lossage in mouse-save-then-kill-delete-region"))
918 (delete-region beg end))
919 (let ((tail buffer-undo-list))
920 ;; Search back in buffer-undo-list for the string
921 ;; that came from deleting one character.
922 (while (and tail (not (stringp (car (car tail)))))
923 (setq tail (cdr tail)))
924 ;; Replace it with an entry for the entire deleted text.
925 (and tail
926 (setcar tail (cons (car kill-ring) (min beg end))))))
927 (undo-boundary))
928
929 (defun mouse-save-then-kill (click)
930 "Save text to point in kill ring; the second time, kill the text.
931 If the text between point and the mouse is the same as what's
932 at the front of the kill ring, this deletes the text.
933 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
934 which prepares for a second click to delete the text.
935
936 If you have selected words or lines, this command extends the
937 selection through the word or line clicked on. If you do this
938 again in a different position, it extends the selection again.
939 If you do this twice in the same position, the selection is killed."
940 (interactive "e")
941 (let ((before-scroll
942 (with-current-buffer (window-buffer (posn-window (event-start click)))
943 point-before-scroll)))
944 (mouse-minibuffer-check click)
945 (let ((click-posn (posn-point (event-start click)))
946 ;; Don't let a subsequent kill command append to this one:
947 ;; prevent setting this-command to kill-region.
948 (this-command this-command))
949 (if (and (save-excursion
950 (set-buffer (window-buffer (posn-window (event-start click))))
951 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
952 ;; Don't be fooled by a recent click in some other buffer.
953 (eq mouse-selection-click-count-buffer
954 (current-buffer)))))
955 (if (not (and (eq last-command 'mouse-save-then-kill)
956 (equal click-posn
957 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
958 ;; Find both ends of the object selected by this click.
959 (let* ((range
960 (mouse-start-end click-posn click-posn
961 mouse-selection-click-count)))
962 ;; Move whichever end is closer to the click.
963 ;; That's what xterm does, and it seems reasonable.
964 (if (< (abs (- click-posn (mark t)))
965 (abs (- click-posn (point))))
966 (set-mark (car range))
967 (goto-char (nth 1 range)))
968 ;; We have already put the old region in the kill ring.
969 ;; Replace it with the extended region.
970 ;; (It would be annoying to make a separate entry.)
971 (kill-new (buffer-substring (point) (mark t)) t)
972 (mouse-set-region-1)
973 ;; Arrange for a repeated mouse-3 to kill this region.
974 (setq mouse-save-then-kill-posn
975 (list (car kill-ring) (point) click-posn))
976 (mouse-show-mark))
977 ;; If we click this button again without moving it,
978 ;; that time kill.
979 (mouse-save-then-kill-delete-region (mark) (point))
980 (setq mouse-selection-click-count 0)
981 (setq mouse-save-then-kill-posn nil))
982 (if (and (eq last-command 'mouse-save-then-kill)
983 mouse-save-then-kill-posn
984 (eq (car mouse-save-then-kill-posn) (car kill-ring))
985 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
986 ;; If this is the second time we've called
987 ;; mouse-save-then-kill, delete the text from the buffer.
988 (progn
989 (mouse-save-then-kill-delete-region (point) (mark))
990 ;; After we kill, another click counts as "the first time".
991 (setq mouse-save-then-kill-posn nil))
992 ;; This is not a repetition.
993 ;; We are adjusting an old selection or creating a new one.
994 (if (or (and (eq last-command 'mouse-save-then-kill)
995 mouse-save-then-kill-posn)
996 (and mark-active transient-mark-mode)
997 (and (memq last-command
998 '(mouse-drag-region mouse-set-region))
999 (or mark-even-if-inactive
1000 (not transient-mark-mode))))
1001 ;; We have a selection or suitable region, so adjust it.
1002 (let* ((posn (event-start click))
1003 (new (posn-point posn)))
1004 (select-window (posn-window posn))
1005 (if (numberp new)
1006 (progn
1007 ;; Move whichever end of the region is closer to the click.
1008 ;; That is what xterm does, and it seems reasonable.
1009 (if (< (abs (- new (point))) (abs (- new (mark t))))
1010 (goto-char new)
1011 (set-mark new))
1012 (setq deactivate-mark nil)))
1013 (kill-new (buffer-substring (point) (mark t)) t)
1014 (mouse-show-mark))
1015 ;; Set the mark where point is, then move where clicked.
1016 (mouse-set-mark-fast click)
1017 (if before-scroll
1018 (goto-char before-scroll))
1019 (exchange-point-and-mark)
1020 (kill-new (buffer-substring (point) (mark t)))
1021 (if window-system
1022 (mouse-show-mark)))
1023 (mouse-set-region-1)
1024 (setq mouse-save-then-kill-posn
1025 (list (car kill-ring) (point) click-posn)))))))
1026 \f
1027 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1028 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1029 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1030 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1031 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1032
1033 ;; An overlay which records the current secondary selection
1034 ;; or else is deleted when there is no secondary selection.
1035 ;; May be nil.
1036 (defvar mouse-secondary-overlay nil)
1037
1038 (defvar mouse-secondary-click-count 0)
1039
1040 ;; A marker which records the specified first end for a secondary selection.
1041 ;; May be nil.
1042 (defvar mouse-secondary-start nil)
1043
1044 (defun mouse-start-secondary (click)
1045 "Set one end of the secondary selection to the position clicked on.
1046 Use \\[mouse-secondary-save-then-kill] to set the other end
1047 and complete the secondary selection."
1048 (interactive "e")
1049 (mouse-minibuffer-check click)
1050 (let ((posn (event-start click)))
1051 (save-excursion
1052 (set-buffer (window-buffer (posn-window posn)))
1053 ;; Cancel any preexisting secondary selection.
1054 (if mouse-secondary-overlay
1055 (delete-overlay mouse-secondary-overlay))
1056 (if (numberp (posn-point posn))
1057 (progn
1058 (or mouse-secondary-start
1059 (setq mouse-secondary-start (make-marker)))
1060 (move-marker mouse-secondary-start (posn-point posn)))))))
1061
1062 (defun mouse-set-secondary (click)
1063 "Set the secondary selection to the text that the mouse is dragged over.
1064 This must be bound to a mouse drag event."
1065 (interactive "e")
1066 (mouse-minibuffer-check click)
1067 (let ((posn (event-start click))
1068 beg
1069 (end (event-end click)))
1070 (save-excursion
1071 (set-buffer (window-buffer (posn-window posn)))
1072 (if (numberp (posn-point posn))
1073 (setq beg (posn-point posn)))
1074 (if mouse-secondary-overlay
1075 (move-overlay mouse-secondary-overlay beg (posn-point end))
1076 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1077 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1078
1079 (defun mouse-drag-secondary (start-event)
1080 "Set the secondary selection to the text that the mouse is dragged over.
1081 Highlight the drag area as you move the mouse.
1082 This must be bound to a button-down mouse event.
1083 The function returns a non-nil value if it creates a secondary selection."
1084 (interactive "e")
1085 (mouse-minibuffer-check start-event)
1086 (let* ((echo-keystrokes 0)
1087 (start-posn (event-start start-event))
1088 (start-point (posn-point start-posn))
1089 (start-window (posn-window start-posn))
1090 (start-frame (window-frame start-window))
1091 (bounds (window-edges start-window))
1092 (top (nth 1 bounds))
1093 (bottom (if (window-minibuffer-p start-window)
1094 (nth 3 bounds)
1095 ;; Don't count the mode line.
1096 (1- (nth 3 bounds))))
1097 (click-count (1- (event-click-count start-event))))
1098 (save-excursion
1099 (set-buffer (window-buffer start-window))
1100 (setq mouse-secondary-click-count click-count)
1101 (or mouse-secondary-overlay
1102 (setq mouse-secondary-overlay
1103 (make-overlay (point) (point))))
1104 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1105 (if (> (mod click-count 3) 0)
1106 ;; Double or triple press: make an initial selection
1107 ;; of one word or line.
1108 (let ((range (mouse-start-end start-point start-point click-count)))
1109 (set-marker mouse-secondary-start nil)
1110 (move-overlay mouse-secondary-overlay 1 1
1111 (window-buffer start-window))
1112 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1113 (window-buffer start-window)))
1114 ;; Single-press: cancel any preexisting secondary selection.
1115 (or mouse-secondary-start
1116 (setq mouse-secondary-start (make-marker)))
1117 (set-marker mouse-secondary-start start-point)
1118 (delete-overlay mouse-secondary-overlay))
1119 (let (event end end-point)
1120 (track-mouse
1121 (while (progn
1122 (setq event (read-event))
1123 (or (mouse-movement-p event)
1124 (eq (car-safe event) 'switch-frame)))
1125
1126 (if (eq (car-safe event) 'switch-frame)
1127 nil
1128 (setq end (event-end event)
1129 end-point (posn-point end))
1130 (cond
1131 ;; Are we moving within the original window?
1132 ((and (eq (posn-window end) start-window)
1133 (integer-or-marker-p end-point))
1134 (let ((range (mouse-start-end start-point end-point
1135 click-count)))
1136 (if (or (/= start-point end-point)
1137 (null (marker-position mouse-secondary-start)))
1138 (progn
1139 (set-marker mouse-secondary-start nil)
1140 (move-overlay mouse-secondary-overlay
1141 (car range) (nth 1 range))))))
1142 (t
1143 (let ((mouse-row (cdr (cdr (mouse-position)))))
1144 (cond
1145 ((null mouse-row))
1146 ((< mouse-row top)
1147 (mouse-scroll-subr start-window (- mouse-row top)
1148 mouse-secondary-overlay start-point))
1149 ((>= mouse-row bottom)
1150 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1151 mouse-secondary-overlay start-point)))))))))
1152
1153 (if (consp event)
1154 (if (marker-position mouse-secondary-start)
1155 (save-window-excursion
1156 (delete-overlay mouse-secondary-overlay)
1157 (x-set-selection 'SECONDARY nil)
1158 (select-window start-window)
1159 (save-excursion
1160 (goto-char mouse-secondary-start)
1161 (sit-for 1)
1162 nil))
1163 (x-set-selection
1164 'SECONDARY
1165 (buffer-substring (overlay-start mouse-secondary-overlay)
1166 (overlay-end mouse-secondary-overlay)))))))))
1167
1168 (defun mouse-yank-secondary (click)
1169 "Insert the secondary selection at the position clicked on.
1170 Move point to the end of the inserted text.
1171 If `mouse-yank-at-point' is non-nil, insert at point
1172 regardless of where you click."
1173 (interactive "e")
1174 ;; Give temporary modes such as isearch a chance to turn off.
1175 (run-hooks 'mouse-leave-buffer-hook)
1176 (or mouse-yank-at-point (mouse-set-point click))
1177 (insert (x-get-selection 'SECONDARY)))
1178
1179 (defun mouse-kill-secondary ()
1180 "Kill the text in the secondary selection.
1181 This is intended more as a keyboard command than as a mouse command
1182 but it can work as either one.
1183
1184 The current buffer (in case of keyboard use), or the buffer clicked on,
1185 must be the one that the secondary selection is in. This requirement
1186 is to prevent accidents."
1187 (interactive)
1188 (let* ((keys (this-command-keys))
1189 (click (elt keys (1- (length keys)))))
1190 (or (eq (overlay-buffer mouse-secondary-overlay)
1191 (if (listp click)
1192 (window-buffer (posn-window (event-start click)))
1193 (current-buffer)))
1194 (error "Select or click on the buffer where the secondary selection is")))
1195 (let (this-command)
1196 (save-excursion
1197 (set-buffer (overlay-buffer mouse-secondary-overlay))
1198 (kill-region (overlay-start mouse-secondary-overlay)
1199 (overlay-end mouse-secondary-overlay))))
1200 (delete-overlay mouse-secondary-overlay)
1201 ;;; (x-set-selection 'SECONDARY nil)
1202 (setq mouse-secondary-overlay nil))
1203
1204 (defun mouse-secondary-save-then-kill (click)
1205 "Save text to point in kill ring; the second time, kill the text.
1206 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1207 If the text between where you did \\[mouse-start-secondary] and where
1208 you use this command matches the text at the front of the kill ring,
1209 this command deletes the text.
1210 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1211 which prepares for a second click with this command to delete the text.
1212
1213 If you have already made a secondary selection in that buffer,
1214 this command extends or retracts the selection to where you click.
1215 If you do this again in a different position, it extends or retracts
1216 again. If you do this twice in the same position, it kills the selection."
1217 (interactive "e")
1218 (mouse-minibuffer-check click)
1219 (let ((posn (event-start click))
1220 (click-posn (posn-point (event-start click)))
1221 ;; Don't let a subsequent kill command append to this one:
1222 ;; prevent setting this-command to kill-region.
1223 (this-command this-command))
1224 (or (eq (window-buffer (posn-window posn))
1225 (or (and mouse-secondary-overlay
1226 (overlay-buffer mouse-secondary-overlay))
1227 (if mouse-secondary-start
1228 (marker-buffer mouse-secondary-start))))
1229 (error "Wrong buffer"))
1230 (save-excursion
1231 (set-buffer (window-buffer (posn-window posn)))
1232 (if (> (mod mouse-secondary-click-count 3) 0)
1233 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1234 (equal click-posn
1235 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1236 ;; Find both ends of the object selected by this click.
1237 (let* ((range
1238 (mouse-start-end click-posn click-posn
1239 mouse-secondary-click-count)))
1240 ;; Move whichever end is closer to the click.
1241 ;; That's what xterm does, and it seems reasonable.
1242 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1243 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1244 (move-overlay mouse-secondary-overlay (car range)
1245 (overlay-end mouse-secondary-overlay))
1246 (move-overlay mouse-secondary-overlay
1247 (overlay-start mouse-secondary-overlay)
1248 (nth 1 range)))
1249 ;; We have already put the old region in the kill ring.
1250 ;; Replace it with the extended region.
1251 ;; (It would be annoying to make a separate entry.)
1252 (kill-new (buffer-substring
1253 (overlay-start mouse-secondary-overlay)
1254 (overlay-end mouse-secondary-overlay)) t)
1255 ;; Arrange for a repeated mouse-3 to kill this region.
1256 (setq mouse-save-then-kill-posn
1257 (list (car kill-ring) (point) click-posn)))
1258 ;; If we click this button again without moving it,
1259 ;; that time kill.
1260 (progn
1261 (mouse-save-then-kill-delete-region
1262 (overlay-start mouse-secondary-overlay)
1263 (overlay-end mouse-secondary-overlay))
1264 (setq mouse-save-then-kill-posn nil)
1265 (setq mouse-secondary-click-count 0)
1266 (delete-overlay mouse-secondary-overlay)))
1267 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1268 mouse-save-then-kill-posn
1269 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1270 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1271 ;; If this is the second time we've called
1272 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1273 (progn
1274 (mouse-save-then-kill-delete-region
1275 (overlay-start mouse-secondary-overlay)
1276 (overlay-end mouse-secondary-overlay))
1277 (setq mouse-save-then-kill-posn nil)
1278 (delete-overlay mouse-secondary-overlay))
1279 (if (overlay-start mouse-secondary-overlay)
1280 ;; We have a selection, so adjust it.
1281 (progn
1282 (if (numberp click-posn)
1283 (progn
1284 ;; Move whichever end of the region is closer to the click.
1285 ;; That is what xterm does, and it seems reasonable.
1286 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1287 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1288 (move-overlay mouse-secondary-overlay click-posn
1289 (overlay-end mouse-secondary-overlay))
1290 (move-overlay mouse-secondary-overlay
1291 (overlay-start mouse-secondary-overlay)
1292 click-posn))
1293 (setq deactivate-mark nil)))
1294 (if (eq last-command 'mouse-secondary-save-then-kill)
1295 ;; If the front of the kill ring comes from
1296 ;; an immediately previous use of this command,
1297 ;; replace it with the extended region.
1298 ;; (It would be annoying to make a separate entry.)
1299 (kill-new (buffer-substring
1300 (overlay-start mouse-secondary-overlay)
1301 (overlay-end mouse-secondary-overlay)) t)
1302 (let (deactivate-mark)
1303 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1304 (overlay-end mouse-secondary-overlay)))))
1305 (if mouse-secondary-start
1306 ;; All we have is one end of a selection,
1307 ;; so put the other end here.
1308 (let ((start (+ 0 mouse-secondary-start)))
1309 (kill-ring-save start click-posn)
1310 (if mouse-secondary-overlay
1311 (move-overlay mouse-secondary-overlay start click-posn)
1312 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1313 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1314 (setq mouse-save-then-kill-posn
1315 (list (car kill-ring) (point) click-posn))))
1316 (if (overlay-buffer mouse-secondary-overlay)
1317 (x-set-selection 'SECONDARY
1318 (buffer-substring
1319 (overlay-start mouse-secondary-overlay)
1320 (overlay-end mouse-secondary-overlay)))))))
1321 \f
1322 (defcustom mouse-buffer-menu-maxlen 20
1323 "*Number of buffers in one pane (submenu) of the buffer menu.
1324 If we have lots of buffers, divide them into groups of
1325 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1326 :type 'integer
1327 :group 'mouse)
1328
1329 (defcustom mouse-buffer-menu-mode-mult 4
1330 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1331 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1332 will split the buffer menu by the major modes (see
1333 `mouse-buffer-menu-mode-groups') or just by menu length.
1334 Set to 1 (or even 0!) if you want to group by major mode always, and to
1335 a large number if you prefer a mixed multitude. The default is 4."
1336 :type 'integer
1337 :group 'mouse
1338 :version "20.3")
1339
1340 (defvar mouse-buffer-menu-mode-groups
1341 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1342 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1343 . "Mail/News")
1344 ("\\<C\\>" . "C")
1345 ("ObjC" . "C")
1346 ("Text" . "Text")
1347 ("Outline" . "Text")
1348 ("Lisp" . "Lisp"))
1349 "How to group various major modes together in \\[mouse-buffer-menu].
1350 Each element has the form (REGEXP . GROUPNAME).
1351 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1352
1353 (defun mouse-buffer-menu (event)
1354 "Pop up a menu of buffers for selection with the mouse.
1355 This switches buffers in the window that you clicked on,
1356 and selects that window."
1357 (interactive "e")
1358 (mouse-minibuffer-check event)
1359 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1360 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1361 (let ((tail buffers))
1362 (while tail
1363 ;; Divide all buffers into buckets for various major modes.
1364 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1365 (with-current-buffer (car tail)
1366 (let* ((adjusted-major-mode major-mode) elt)
1367 (let ((tail mouse-buffer-menu-mode-groups))
1368 (while tail
1369 (if (string-match (car (car tail)) mode-name)
1370 (setq adjusted-major-mode (cdr (car tail))))
1371 (setq tail (cdr tail))))
1372 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1373 (if (null elt)
1374 (setq elt (list adjusted-major-mode
1375 (if (stringp adjusted-major-mode)
1376 adjusted-major-mode
1377 mode-name))
1378 split-by-major-mode (cons elt split-by-major-mode)))
1379 (or (memq (car tail) (cdr (cdr elt)))
1380 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1381 (setq tail (cdr tail))))
1382 ;; Compute the sum of squares of sizes of the major-mode buckets.
1383 (let ((tail split-by-major-mode))
1384 (setq sum-of-squares 0)
1385 (while tail
1386 (setq sum-of-squares
1387 (+ sum-of-squares
1388 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1389 (setq tail (cdr tail))))
1390 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1391 (* (length buffers) (length buffers)))
1392 ;; Subdividing by major modes really helps, so let's do it.
1393 (let (subdivided-menus (buffers-left (length buffers)))
1394 ;; Sort the list to put the most popular major modes first.
1395 (setq split-by-major-mode
1396 (sort split-by-major-mode
1397 (function (lambda (elt1 elt2)
1398 (> (length elt1) (length elt2))))))
1399 ;; Make a separate submenu for each major mode
1400 ;; that has more than one buffer,
1401 ;; unless all the remaining buffers are less than 1/10 of them.
1402 (while (and split-by-major-mode
1403 (and (> (length (car split-by-major-mode)) 3)
1404 (> (* buffers-left 10) (length buffers))))
1405 (setq subdivided-menus
1406 (cons (cons
1407 (nth 1 (car split-by-major-mode))
1408 (mouse-buffer-menu-alist
1409 (cdr (cdr (car split-by-major-mode)))))
1410 subdivided-menus))
1411 (setq buffers-left
1412 (- buffers-left (length (cdr (car split-by-major-mode)))))
1413 (setq split-by-major-mode (cdr split-by-major-mode)))
1414 ;; If any major modes are left over,
1415 ;; make a single submenu for them.
1416 (if split-by-major-mode
1417 (setq subdivided-menus
1418 (cons (cons
1419 "Others"
1420 (mouse-buffer-menu-alist
1421 ;; we don't need split-by-major-mode any
1422 ;; more, so we can ditch it with nconc.
1423 (apply 'nconc (mapcar 'cddr split-by-major-mode))))
1424 subdivided-menus)))
1425 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1426 (progn
1427 (setq alist (mouse-buffer-menu-alist buffers))
1428 (setq menu (cons "Buffer Menu"
1429 (mouse-buffer-menu-split "Select Buffer" alist)))))
1430 (let ((buf (x-popup-menu event menu))
1431 (window (posn-window (event-start event))))
1432 (when buf
1433 (or (framep window) (select-window window))
1434 (switch-to-buffer buf)))))
1435
1436 (defun mouse-buffer-menu-alist (buffers)
1437 (let (tail
1438 (maxlen 0)
1439 head)
1440 (setq buffers
1441 (sort buffers
1442 (function (lambda (elt1 elt2)
1443 (string< (buffer-name elt1) (buffer-name elt2))))))
1444 (setq tail buffers)
1445 (while tail
1446 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1447 (setq maxlen
1448 (max maxlen
1449 (length (buffer-name (car tail))))))
1450 (setq tail (cdr tail)))
1451 (setq tail buffers)
1452 (while tail
1453 (let ((elt (car tail)))
1454 (if (/= (aref (buffer-name elt) 0) ?\ )
1455 (setq head
1456 (cons
1457 (cons
1458 (format
1459 (format "%%%ds %%s%%s %%s" maxlen)
1460 (buffer-name elt)
1461 (if (buffer-modified-p elt) "*" " ")
1462 (save-excursion
1463 (set-buffer elt)
1464 (if buffer-read-only "%" " "))
1465 (or (buffer-file-name elt)
1466 (save-excursion
1467 (set-buffer elt)
1468 (if list-buffers-directory
1469 (expand-file-name
1470 list-buffers-directory)))
1471 ""))
1472 elt)
1473 head))))
1474 (setq tail (cdr tail)))
1475 ;; Compensate for the reversal that the above loop does.
1476 (nreverse head)))
1477
1478 (defun mouse-buffer-menu-split (title alist)
1479 ;; If we have lots of buffers, divide them into groups of 20
1480 ;; and make a pane (or submenu) for each one.
1481 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1482 (let ((alist alist) sublists next
1483 (i 1))
1484 (while alist
1485 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1486 ;; and make them the next element of sublist.
1487 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1488 (if next
1489 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1490 nil))
1491 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1492 sublists))
1493 (setq i (1+ i))
1494 (setq alist next))
1495 (nreverse sublists))
1496 ;; Few buffers--put them all in one pane.
1497 (list (cons title alist))))
1498 \f
1499 ;;; These need to be rewritten for the new scroll bar implementation.
1500
1501 ;;;!! ;; Commands for the scroll bar.
1502 ;;;!!
1503 ;;;!! (defun mouse-scroll-down (click)
1504 ;;;!! (interactive "@e")
1505 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1506 ;;;!!
1507 ;;;!! (defun mouse-scroll-up (click)
1508 ;;;!! (interactive "@e")
1509 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1510 ;;;!!
1511 ;;;!! (defun mouse-scroll-down-full ()
1512 ;;;!! (interactive "@")
1513 ;;;!! (scroll-down nil))
1514 ;;;!!
1515 ;;;!! (defun mouse-scroll-up-full ()
1516 ;;;!! (interactive "@")
1517 ;;;!! (scroll-up nil))
1518 ;;;!!
1519 ;;;!! (defun mouse-scroll-move-cursor (click)
1520 ;;;!! (interactive "@e")
1521 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1522 ;;;!!
1523 ;;;!! (defun mouse-scroll-absolute (event)
1524 ;;;!! (interactive "@e")
1525 ;;;!! (let* ((pos (car event))
1526 ;;;!! (position (car pos))
1527 ;;;!! (length (car (cdr pos))))
1528 ;;;!! (if (<= length 0) (setq length 1))
1529 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1530 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1531 ;;;!! position)
1532 ;;;!! length)
1533 ;;;!! scale-factor)))
1534 ;;;!! (goto-char newpos)
1535 ;;;!! (recenter '(4)))))
1536 ;;;!!
1537 ;;;!! (defun mouse-scroll-left (click)
1538 ;;;!! (interactive "@e")
1539 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1540 ;;;!!
1541 ;;;!! (defun mouse-scroll-right (click)
1542 ;;;!! (interactive "@e")
1543 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1544 ;;;!!
1545 ;;;!! (defun mouse-scroll-left-full ()
1546 ;;;!! (interactive "@")
1547 ;;;!! (scroll-left nil))
1548 ;;;!!
1549 ;;;!! (defun mouse-scroll-right-full ()
1550 ;;;!! (interactive "@")
1551 ;;;!! (scroll-right nil))
1552 ;;;!!
1553 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1554 ;;;!! (interactive "@e")
1555 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1556 ;;;!!
1557 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1558 ;;;!! (interactive "@e")
1559 ;;;!! (let* ((pos (car event))
1560 ;;;!! (position (car pos))
1561 ;;;!! (length (car (cdr pos))))
1562 ;;;!! (set-window-hscroll (selected-window) 33)))
1563 ;;;!!
1564 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1565 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1566 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1567 ;;;!!
1568 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1569 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1570 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1571 ;;;!!
1572 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1573 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1574 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1575 ;;;!!
1576 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1577 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1578 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1579 ;;;!!
1580 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1581 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1582 ;;;!! 'mouse-scroll-absolute-horizontally)
1583 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1584 ;;;!!
1585 ;;;!! (global-set-key [horizontal-slider mouse-1]
1586 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1587 ;;;!! (global-set-key [horizontal-slider mouse-2]
1588 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1589 ;;;!! (global-set-key [horizontal-slider mouse-3]
1590 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1591 ;;;!!
1592 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1593 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1594 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1595 ;;;!!
1596 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1597 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1598 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1599 ;;;!!
1600 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1601 ;;;!! 'mouse-split-window-horizontally)
1602 ;;;!! (global-set-key [mode-line S-mouse-2]
1603 ;;;!! 'mouse-split-window-horizontally)
1604 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1605 ;;;!! 'mouse-split-window)
1606 \f
1607 ;;;!! ;;;;
1608 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1609 ;;;!! ;;;; are of the form:
1610 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1611 ;;;!! ;;
1612 ;;;!! ;;;;
1613 ;;;!! ;;;; Dynamically track mouse coordinates
1614 ;;;!! ;;;;
1615 ;;;!! ;;
1616 ;;;!! ;;(defun track-mouse (event)
1617 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1618 ;;;!! ;; (interactive "@e")
1619 ;;;!! ;; (while mouse-grabbed
1620 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1621 ;;;!! ;; (abs-x (car pos))
1622 ;;;!! ;; (abs-y (cdr pos))
1623 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1624 ;;;!! ;; (list (car pos) (cdr pos))
1625 ;;;!! ;; (selected-window))))
1626 ;;;!! ;; (if (consp relative-coordinate)
1627 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1628 ;;;!! ;; (car relative-coordinate)
1629 ;;;!! ;; (car (cdr relative-coordinate)))
1630 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1631 ;;;!!
1632 ;;;!! ;;
1633 ;;;!! ;; Dynamically put a box around the line indicated by point
1634 ;;;!! ;;
1635 ;;;!! ;;
1636 ;;;!! ;;(require 'backquote)
1637 ;;;!! ;;
1638 ;;;!! ;;(defun mouse-select-buffer-line (event)
1639 ;;;!! ;; (interactive "@e")
1640 ;;;!! ;; (let ((relative-coordinate
1641 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1642 ;;;!! ;; (abs-y (car (cdr (car event)))))
1643 ;;;!! ;; (if (consp relative-coordinate)
1644 ;;;!! ;; (progn
1645 ;;;!! ;; (save-excursion
1646 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1647 ;;;!! ;; (x-draw-rectangle
1648 ;;;!! ;; (selected-screen)
1649 ;;;!! ;; abs-y 0
1650 ;;;!! ;; (save-excursion
1651 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1652 ;;;!! ;; (end-of-line)
1653 ;;;!! ;; (push-mark nil t)
1654 ;;;!! ;; (beginning-of-line)
1655 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1656 ;;;!! ;; (sit-for 1)
1657 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1658 ;;;!! ;;
1659 ;;;!! ;;(defvar last-line-drawn nil)
1660 ;;;!! ;;(defvar begin-delim "[^ \t]")
1661 ;;;!! ;;(defvar end-delim "[^ \t]")
1662 ;;;!! ;;
1663 ;;;!! ;;(defun mouse-boxing (event)
1664 ;;;!! ;; (interactive "@e")
1665 ;;;!! ;; (save-excursion
1666 ;;;!! ;; (let ((screen (selected-screen)))
1667 ;;;!! ;; (while (= (x-mouse-events) 0)
1668 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1669 ;;;!! ;; (abs-x (car pos))
1670 ;;;!! ;; (abs-y (cdr pos))
1671 ;;;!! ;; (relative-coordinate
1672 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1673 ;;;!! ;; (selected-window)))
1674 ;;;!! ;; (begin-reg nil)
1675 ;;;!! ;; (end-reg nil)
1676 ;;;!! ;; (end-column nil)
1677 ;;;!! ;; (begin-column nil))
1678 ;;;!! ;; (if (and (consp relative-coordinate)
1679 ;;;!! ;; (or (not last-line-drawn)
1680 ;;;!! ;; (not (= last-line-drawn abs-y))))
1681 ;;;!! ;; (progn
1682 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1683 ;;;!! ;; (if (= (following-char) 10)
1684 ;;;!! ;; ()
1685 ;;;!! ;; (progn
1686 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1687 ;;;!! ;; (setq begin-column (1- (current-column)))
1688 ;;;!! ;; (end-of-line)
1689 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1690 ;;;!! ;; (setq end-column (1+ (current-column)))
1691 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1692 ;;;!! ;; (x-draw-rectangle screen
1693 ;;;!! ;; (setq last-line-drawn abs-y)
1694 ;;;!! ;; begin-column
1695 ;;;!! ;; (- end-column begin-column) 1))))))))))
1696 ;;;!! ;;
1697 ;;;!! ;;(defun mouse-erase-box ()
1698 ;;;!! ;; (interactive)
1699 ;;;!! ;; (if last-line-drawn
1700 ;;;!! ;; (progn
1701 ;;;!! ;; (x-erase-rectangle (selected-screen))
1702 ;;;!! ;; (setq last-line-drawn nil))))
1703 ;;;!!
1704 ;;;!! ;;; (defun test-x-rectangle ()
1705 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1706 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1707 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1708 ;;;!!
1709 ;;;!! ;;
1710 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1711 ;;;!! ;;
1712 ;;;!!
1713 ;;;!! (defvar double-start nil)
1714 ;;;!! (defconst double-click-interval 300
1715 ;;;!! "Max ticks between clicks")
1716 ;;;!!
1717 ;;;!! (defun double-down (event)
1718 ;;;!! (interactive "@e")
1719 ;;;!! (if double-start
1720 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1721 ;;;!! (if (< interval double-click-interval)
1722 ;;;!! (progn
1723 ;;;!! (backward-up-list 1)
1724 ;;;!! ;; (message "Interval %d" interval)
1725 ;;;!! (sleep-for 1)))
1726 ;;;!! (setq double-start nil))
1727 ;;;!! (setq double-start (nth 4 event))))
1728 ;;;!!
1729 ;;;!! (defun double-up (event)
1730 ;;;!! (interactive "@e")
1731 ;;;!! (and double-start
1732 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1733 ;;;!! (setq double-start nil)))
1734 ;;;!!
1735 ;;;!! ;;; (defun x-test-doubleclick ()
1736 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1737 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1738 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1739 ;;;!!
1740 ;;;!! ;;
1741 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1742 ;;;!! ;;
1743 ;;;!!
1744 ;;;!! (defvar scrolled-lines 0)
1745 ;;;!! (defconst scroll-speed 1)
1746 ;;;!!
1747 ;;;!! (defun incr-scroll-down (event)
1748 ;;;!! (interactive "@e")
1749 ;;;!! (setq scrolled-lines 0)
1750 ;;;!! (incremental-scroll scroll-speed))
1751 ;;;!!
1752 ;;;!! (defun incr-scroll-up (event)
1753 ;;;!! (interactive "@e")
1754 ;;;!! (setq scrolled-lines 0)
1755 ;;;!! (incremental-scroll (- scroll-speed)))
1756 ;;;!!
1757 ;;;!! (defun incremental-scroll (n)
1758 ;;;!! (while (= (x-mouse-events) 0)
1759 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1760 ;;;!! (scroll-down n)
1761 ;;;!! (sit-for 300 t)))
1762 ;;;!!
1763 ;;;!! (defun incr-scroll-stop (event)
1764 ;;;!! (interactive "@e")
1765 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1766 ;;;!! (setq scrolled-lines 0)
1767 ;;;!! (sleep-for 1))
1768 ;;;!!
1769 ;;;!! ;;; (defun x-testing-scroll ()
1770 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1771 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1772 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1773 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1774 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1775 ;;;!!
1776 ;;;!! ;;
1777 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1778 ;;;!! ;;
1779 ;;;!!
1780 ;;;!! (defun mouse-kill-rectangle (event)
1781 ;;;!! "Kill the rectangle between point and the mouse cursor."
1782 ;;;!! (interactive "@e")
1783 ;;;!! (let ((point-save (point)))
1784 ;;;!! (save-excursion
1785 ;;;!! (mouse-set-point event)
1786 ;;;!! (push-mark nil t)
1787 ;;;!! (if (> point-save (point))
1788 ;;;!! (kill-rectangle (point) point-save)
1789 ;;;!! (kill-rectangle point-save (point))))))
1790 ;;;!!
1791 ;;;!! (defun mouse-open-rectangle (event)
1792 ;;;!! "Kill the rectangle between point and the mouse cursor."
1793 ;;;!! (interactive "@e")
1794 ;;;!! (let ((point-save (point)))
1795 ;;;!! (save-excursion
1796 ;;;!! (mouse-set-point event)
1797 ;;;!! (push-mark nil t)
1798 ;;;!! (if (> point-save (point))
1799 ;;;!! (open-rectangle (point) point-save)
1800 ;;;!! (open-rectangle point-save (point))))))
1801 ;;;!!
1802 ;;;!! ;; Must be a better way to do this.
1803 ;;;!!
1804 ;;;!! (defun mouse-multiple-insert (n char)
1805 ;;;!! (while (> n 0)
1806 ;;;!! (insert char)
1807 ;;;!! (setq n (1- n))))
1808 ;;;!!
1809 ;;;!! ;; What this could do is not finalize until button was released.
1810 ;;;!!
1811 ;;;!! (defun mouse-move-text (event)
1812 ;;;!! "Move text from point to cursor position, inserting spaces."
1813 ;;;!! (interactive "@e")
1814 ;;;!! (let* ((relative-coordinate
1815 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1816 ;;;!! (if (consp relative-coordinate)
1817 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1818 ;;;!! (delete-char
1819 ;;;!! (- (car relative-coordinate) (current-column))))
1820 ;;;!! ((< (current-column) (car relative-coordinate))
1821 ;;;!! (mouse-multiple-insert
1822 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1823 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1824 \f
1825 ;; Choose a completion with the mouse.
1826
1827 (defun mouse-choose-completion (event)
1828 "Click on an alternative in the `*Completions*' buffer to choose it."
1829 (interactive "e")
1830 ;; Give temporary modes such as isearch a chance to turn off.
1831 (run-hooks 'mouse-leave-buffer-hook)
1832 (let ((buffer (window-buffer))
1833 choice
1834 base-size)
1835 (save-excursion
1836 (set-buffer (window-buffer (posn-window (event-start event))))
1837 (if completion-reference-buffer
1838 (setq buffer completion-reference-buffer))
1839 (setq base-size completion-base-size)
1840 (save-excursion
1841 (goto-char (posn-point (event-start event)))
1842 (let (beg end)
1843 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1844 (setq end (point) beg (1+ (point))))
1845 (if (null beg)
1846 (error "No completion here"))
1847 (setq beg (previous-single-property-change beg 'mouse-face))
1848 (setq end (or (next-single-property-change end 'mouse-face)
1849 (point-max)))
1850 (setq choice (buffer-substring beg end)))))
1851 (let ((owindow (selected-window)))
1852 (select-window (posn-window (event-start event)))
1853 (if (and (one-window-p t 'selected-frame)
1854 (window-dedicated-p (selected-window)))
1855 ;; This is a special buffer's frame
1856 (iconify-frame (selected-frame))
1857 (or (window-dedicated-p (selected-window))
1858 (bury-buffer)))
1859 (select-window owindow))
1860 (choose-completion-string choice buffer base-size)))
1861 \f
1862 ;; Font selection.
1863
1864 (defun font-menu-add-default ()
1865 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1866 (font-alist x-fixed-font-alist)
1867 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1868 (if (assoc "Default" elt)
1869 (delete (assoc "Default" elt) elt))
1870 (setcdr elt
1871 (cons (list "Default"
1872 (cdr (assq 'font (frame-parameters (selected-frame)))))
1873 (cdr elt)))))
1874
1875 (defvar x-fixed-font-alist
1876 '("Font menu"
1877 ("Misc"
1878 ;; For these, we specify the pixel height and width.
1879 ("fixed" "fixed")
1880 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1881 ("6x12"
1882 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1883 ("6x13"
1884 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1885 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1886 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1887 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1888 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1889 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1890 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1891 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1892 ("")
1893 ("clean 5x8"
1894 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1895 ("clean 6x8"
1896 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1897 ("clean 8x8"
1898 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1899 ("clean 8x10"
1900 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1901 ("clean 8x14"
1902 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1903 ("clean 8x16"
1904 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1905 ("")
1906 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
1907 ;;; We don't seem to have these; who knows what they are.
1908 ;;; ("fg-18" "fg-18")
1909 ;;; ("fg-25" "fg-25")
1910 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1911 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1912 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1913 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1914 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1915 ("Courier"
1916 ;; For these, we specify the point height.
1917 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1918 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1919 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1920 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1921 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1922 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1923 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1924 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1925 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1926 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1927 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1928 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1929 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1930 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1931 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1932 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1933 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1934 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1935 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1936 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1937 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1938 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1939 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1940 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
1941 )
1942 "X fonts suitable for use in Emacs.")
1943
1944 (defun mouse-set-font (&rest fonts)
1945 "Select an emacs font from a list of known good fonts and fontsets."
1946 (interactive
1947 (x-popup-menu
1948 last-nonmenu-event
1949 ;; Append list of fontsets currently defined.
1950 (append x-fixed-font-alist (list (generate-fontset-menu)))))
1951 (if fonts
1952 (let (font)
1953 (while fonts
1954 (condition-case nil
1955 (progn
1956 (set-default-font (car fonts))
1957 (setq font (car fonts))
1958 (setq fonts nil))
1959 (error
1960 (setq fonts (cdr fonts)))))
1961 (if (null font)
1962 (error "Font not found")))))
1963 \f
1964 ;;; Bindings for mouse commands.
1965
1966 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1967 (global-set-key [mouse-1] 'mouse-set-point)
1968 (global-set-key [drag-mouse-1] 'mouse-set-region)
1969
1970 ;; These are tested for in mouse-drag-region.
1971 (global-set-key [double-mouse-1] 'mouse-set-point)
1972 (global-set-key [triple-mouse-1] 'mouse-set-point)
1973
1974 (global-set-key [mouse-2] 'mouse-yank-at-click)
1975 (global-set-key [mouse-3] 'mouse-save-then-kill)
1976
1977 ;; By binding these to down-going events, we let the user use the up-going
1978 ;; event to make the selection, saving a click.
1979 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1980 (if (not (eq system-type 'ms-dos))
1981 (global-set-key [S-down-mouse-1] 'mouse-set-font))
1982 ;; C-down-mouse-2 is bound in facemenu.el.
1983 (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1984
1985
1986 ;; Replaced with dragging mouse-1
1987 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1988
1989 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1990 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1991 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1992 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1993 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1994 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1995 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1996 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1997 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1998 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1999
2000 (provide 'mouse)
2001
2002 ;;; mouse.el ends here