Update copyright.
[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
22 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; This package provides various useful commands (including help
27 ;; system access) through the mouse. All this code assumes that mouse
28 ;; interpretation has been abstracted into Emacs input events.
29 ;;
30 ;; The code is rather X-dependent.
31
32 ;;; Code:
33
34 ;;; Utility functions.
35
36 ;;; Indent track-mouse like progn.
37 (put 'track-mouse 'lisp-indent-function 0)
38
39 (defvar mouse-yank-at-point nil
40 "*If non-nil, mouse yank commands yank at point instead of at click.")
41 \f
42 ;; Provide a mode-specific menu on a mouse button.
43
44 (defun mouse-major-mode-menu (event)
45 "Pop up a mode-specific menu of mouse commands."
46 ;; Switch to the window clicked on, because otherwise
47 ;; the mode's commands may not make sense.
48 (interactive "@e")
49 (let ((newmap (make-sparse-keymap))
50 (unread-command-events (list event)))
51 ;; Make a keymap in which our last command leads to a menu
52 (define-key newmap (vector (car event))
53 (nconc (make-sparse-keymap "Menu")
54 (mouse-major-mode-menu-1
55 (and (current-local-map)
56 (lookup-key (current-local-map) [menu-bar])))))
57 (mouse-major-mode-menu-compute-equiv-keys newmap)
58 (command-execute
59 ;; Make NEWMAP override the usual definition
60 ;; of the mouse button that got us here.
61 ;; Then read the user's menu choice.
62 (let ((minor-mode-map-alist
63 (cons (cons t newmap) minor-mode-map-alist)))
64 (lookup-key newmap (read-key-sequence ""))))))
65
66 ;; Compute and cache the equivalent keys in MENU and all its submenus.
67 (defun mouse-major-mode-menu-compute-equiv-keys (menu)
68 (and (eq (car menu) 'keymap)
69 (x-popup-menu nil menu))
70 (while menu
71 (and (consp (car menu))
72 (consp (cdr (car menu)))
73 (let ((tail (cdr (car menu))))
74 (while (and (consp tail)
75 (not (eq (car tail) 'keymap)))
76 (setq tail (cdr tail)))
77 (if (consp tail)
78 (mouse-major-mode-menu-compute-equiv-keys tail))))
79 (setq menu (cdr menu))))
80
81 ;; Given a mode's menu bar keymap,
82 ;; if it defines exactly one menu bar menu,
83 ;; return just that menu.
84 ;; Otherwise return a menu for all of them.
85 (defun mouse-major-mode-menu-1 (menubar)
86 (if menubar
87 (let ((tail menubar)
88 submap)
89 (while tail
90 (if (consp (car tail))
91 (if submap
92 (setq submap t)
93 (setq submap (cdr (car tail)))))
94 (setq tail (cdr tail)))
95 (if (eq submap t) menubar
96 submap))))
97 \f
98 ;; Commands that operate on windows.
99
100 (defun mouse-minibuffer-check (event)
101 (let ((w (posn-window (event-start event))))
102 (and (window-minibuffer-p w)
103 (not (minibuffer-window-active-p w))
104 (error "Minibuffer window is not active")))
105 ;; Give temporary modes such as isearch a chance to turn off.
106 (run-hooks 'mouse-leave-buffer-hook))
107
108 (defun mouse-delete-window (click)
109 "Delete the window you click on.
110 This must be bound to a mouse click."
111 (interactive "e")
112 (mouse-minibuffer-check click)
113 (delete-window (posn-window (event-start click))))
114
115 (defun mouse-select-window (click)
116 "Select the window clicked on; don't move point."
117 (interactive "e")
118 (mouse-minibuffer-check click)
119 (let ((oframe (selected-frame))
120 (frame (window-frame (posn-window (event-start click)))))
121 (select-window (posn-window (event-start click)))
122 (raise-frame frame)
123 (select-frame frame)
124 (or (eq frame oframe)
125 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
126 (unfocus-frame)))
127
128 (defun mouse-tear-off-window (click)
129 "Delete the window clicked on, and create a new frame displaying its buffer."
130 (interactive "e")
131 (mouse-minibuffer-check click)
132 (let* ((window (posn-window (event-start click)))
133 (buf (window-buffer window))
134 (frame (make-frame)))
135 (select-frame frame)
136 (switch-to-buffer buf)
137 (delete-window window)))
138
139 (defun mouse-delete-other-windows ()
140 "Delete all window except the one you click on."
141 (interactive "@")
142 (delete-other-windows))
143
144 (defun mouse-split-window-vertically (click)
145 "Select Emacs window mouse is on, then split it vertically in half.
146 The window is split at the line clicked on.
147 This command must be bound to a mouse click."
148 (interactive "@e")
149 (mouse-minibuffer-check click)
150 (let ((start (event-start click)))
151 (select-window (posn-window start))
152 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
153 (first-line window-min-height)
154 (last-line (- (window-height) window-min-height)))
155 (if (< last-line first-line)
156 (error "Window too short to split")
157 (split-window-vertically
158 (min (max new-height first-line) last-line))))))
159
160 (defun mouse-split-window-horizontally (click)
161 "Select Emacs window mouse is on, then split it horizontally in half.
162 The window is split at the column clicked on.
163 This command must be bound to a mouse click."
164 (interactive "@e")
165 (mouse-minibuffer-check click)
166 (let ((start (event-start click)))
167 (select-window (posn-window start))
168 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
169 (first-col window-min-width)
170 (last-col (- (window-width) window-min-width)))
171 (if (< last-col first-col)
172 (error "Window too narrow to split")
173 (split-window-horizontally
174 (min (max new-width first-col) last-col))))))
175
176 (defun mouse-drag-mode-line (start-event)
177 "Change the height of a window by dragging on the mode line."
178 (interactive "e")
179 ;; Give temporary modes such as isearch a chance to turn off.
180 (run-hooks 'mouse-leave-buffer-hook)
181 (let ((done nil)
182 (echo-keystrokes 0)
183 (start-event-frame (window-frame (car (car (cdr start-event)))))
184 (start-event-window (car (car (cdr start-event))))
185 (start-nwindows (count-windows t))
186 (old-selected-window (selected-window))
187 should-enlarge-minibuffer
188 event mouse minibuffer y top bot edges wconfig params growth)
189 (setq params (frame-parameters))
190 (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
191 (one-window-p t))
192 (error "Attempt to resize sole window"))
193 (track-mouse
194 (progn
195 ;; enlarge-window only works on the selected window, so
196 ;; we must select the window where the start event originated.
197 ;; unwind-protect will restore the old selected window later.
198 (select-window start-event-window)
199 ;; if this is the bottommost ordinary window, then to
200 ;; move its modeline the minibuffer must be enlarged.
201 (setq should-enlarge-minibuffer
202 (and minibuffer
203 (not (one-window-p t))
204 (= (nth 1 (window-edges minibuffer))
205 (nth 3 (window-edges)))))
206 ;; loop reading events and sampling the position of
207 ;; the mouse.
208 (while (not done)
209 (setq event (read-event)
210 mouse (mouse-position))
211 ;; do nothing if
212 ;; - there is a switch-frame event.
213 ;; - the mouse isn't in the frame that we started in
214 ;; - the mouse isn't in any Emacs frame
215 ;; drag if
216 ;; - there is a mouse-movement event
217 ;; - there is a scroll-bar-movement event
218 ;; (same as mouse movement for our purposes)
219 ;; quit if
220 ;; - there is a keyboard event or some other unknown event
221 ;; unknown event.
222 (cond ((integerp event)
223 (setq done t))
224 ((eq (car event) 'switch-frame)
225 nil)
226 ((not (memq (car event)
227 '(mouse-movement scroll-bar-movement)))
228 (if (consp event)
229 (setq unread-command-events
230 (cons event unread-command-events)))
231 (setq done t))
232 ((not (eq (car mouse) start-event-frame))
233 nil)
234 ((null (car (cdr mouse)))
235 nil)
236 (t
237 (setq y (cdr (cdr mouse))
238 edges (window-edges)
239 top (nth 1 edges)
240 bot (nth 3 edges))
241 ;; scale back a move that would make the
242 ;; window too short.
243 (cond ((< (- y top -1) window-min-height)
244 (setq y (+ top window-min-height -1))))
245 ;; compute size change needed
246 (setq growth (- y bot -1)
247 wconfig (current-window-configuration))
248 ;; grow/shrink minibuffer?
249 (if should-enlarge-minibuffer
250 (progn
251 ;; yes. briefly select minibuffer so
252 ;; enlarge-window will affect the
253 ;; correct window.
254 (select-window minibuffer)
255 ;; scale back shrinkage if it would
256 ;; make the minibuffer less than 1
257 ;; line tall.
258 (if (and (> growth 0)
259 (< (- (window-height minibuffer)
260 growth)
261 1))
262 (setq growth (1- (window-height minibuffer))))
263 (enlarge-window (- growth))
264 (select-window start-event-window))
265 ;; no. grow/shrink the selected window
266 (enlarge-window growth))
267 ;; if this window's growth caused another
268 ;; window to be deleted because it was too
269 ;; short, rescind the change.
270 ;;
271 ;; if size change caused space to be stolen
272 ;; from a window above this one, rescind the
273 ;; change, but only if we didn't grow/srhink
274 ;; the minibuffer. minibuffer size changes
275 ;; can cause all windows to shrink... no way
276 ;; around it.
277 (if (or (/= start-nwindows (count-windows t))
278 (and (not should-enlarge-minibuffer)
279 (/= top (nth 1 (window-edges)))))
280 (set-window-configuration wconfig)))))))))
281 \f
282 (defun mouse-set-point (event)
283 "Move point to the position clicked on with the mouse.
284 This should be bound to a mouse click event type."
285 (interactive "e")
286 (mouse-minibuffer-check event)
287 ;; Use event-end in case called from mouse-drag-region.
288 ;; If EVENT is a click, event-end and event-start give same value.
289 (let ((posn (event-end event)))
290 (if (not (windowp (posn-window posn)))
291 (error "Cursor not in text area of window"))
292 (select-window (posn-window posn))
293 (if (numberp (posn-point posn))
294 (goto-char (posn-point posn)))))
295
296 (defvar mouse-last-region-beg nil)
297 (defvar mouse-last-region-end nil)
298 (defvar mouse-last-region-tick nil)
299
300 (defun mouse-region-match ()
301 "Return non-nil if there's an active region that was set with the mouse."
302 (and (mark t) mark-active
303 (eq mouse-last-region-beg (region-beginning))
304 (eq mouse-last-region-end (region-end))
305 (eq mouse-last-region-tick (buffer-modified-tick))))
306
307 (defun mouse-set-region (click)
308 "Set the region to the text dragged over, and copy to kill ring.
309 This should be bound to a mouse drag event."
310 (interactive "e")
311 (mouse-minibuffer-check click)
312 (let ((posn (event-start click))
313 (end (event-end click)))
314 (select-window (posn-window posn))
315 (if (numberp (posn-point posn))
316 (goto-char (posn-point posn)))
317 ;; If mark is highlighted, no need to bounce the cursor.
318 (or (and transient-mark-mode
319 (framep (selected-frame)))
320 (sit-for 1))
321 (push-mark)
322 (set-mark (point))
323 (if (numberp (posn-point end))
324 (goto-char (posn-point end)))
325 ;; Don't set this-command to kill-region, so that a following
326 ;; C-w will not double the text in the kill ring.
327 (let (this-command)
328 (copy-region-as-kill (mark) (point)))
329 (mouse-set-region-1)))
330
331 (defun mouse-set-region-1 ()
332 (setq mouse-last-region-beg (region-beginning))
333 (setq mouse-last-region-end (region-end))
334 (setq mouse-last-region-tick (buffer-modified-tick)))
335
336 (defvar mouse-scroll-delay 0.25
337 "*The pause between scroll steps caused by mouse drags, in seconds.
338 If you drag the mouse beyond the edge of a window, Emacs scrolls the
339 window to bring the text beyond that edge into view, with a delay of
340 this many seconds between scroll steps. Scrolling stops when you move
341 the mouse back into the window, or release the button.
342 This variable's value may be non-integral.
343 Setting this to zero causes Emacs to scroll as fast as it can.")
344
345 (defun mouse-scroll-subr (window jump &optional overlay start)
346 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
347 If OVERLAY is an overlay, let it stretch from START to the far edge of
348 the newly visible text.
349 Upon exit, point is at the far edge of the newly visible text."
350 (let ((opoint (point)))
351 (while (progn
352 (goto-char (window-start window))
353 (if (not (zerop (vertical-motion jump window)))
354 (progn
355 (set-window-start window (point))
356 (if (natnump jump)
357 (progn
358 (goto-char (window-end window))
359 ;; window-end doesn't reflect the window's new
360 ;; start position until the next redisplay. Hurrah.
361 (vertical-motion (1- jump) window))
362 (goto-char (window-start window)))
363 (if overlay
364 (move-overlay overlay start (point)))
365 ;; Now that we have scrolled WINDOW properly,
366 ;; put point back where it was for the redisplay
367 ;; so that we don't mess up the selected window.
368 (or (eq window (selected-window))
369 (goto-char opoint))
370 (sit-for mouse-scroll-delay)))))
371 (or (eq window (selected-window))
372 (goto-char opoint))))
373
374 (defvar mouse-drag-overlay (make-overlay 1 1))
375 (overlay-put mouse-drag-overlay 'face 'region)
376
377 (defvar mouse-selection-click-count 0)
378
379 (defun mouse-drag-region (start-event)
380 "Set the region to the text that the mouse is dragged over.
381 Highlight the drag area as you move the mouse.
382 This must be bound to a button-down mouse event.
383 In Transient Mark mode, the highlighting remains once you
384 release the mouse button. Otherwise, it does not."
385 (interactive "e")
386 (mouse-minibuffer-check start-event)
387 (let* ((start-posn (event-start start-event))
388 (start-point (posn-point start-posn))
389 (start-window (posn-window start-posn))
390 (start-frame (window-frame start-window))
391 (bounds (window-edges start-window))
392 (top (nth 1 bounds))
393 (bottom (if (window-minibuffer-p start-window)
394 (nth 3 bounds)
395 ;; Don't count the mode line.
396 (1- (nth 3 bounds))))
397 (click-count (1- (event-click-count start-event))))
398 (setq mouse-selection-click-count click-count)
399 (mouse-set-point start-event)
400 (let ((range (mouse-start-end start-point start-point click-count)))
401 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
402 (window-buffer start-window)))
403 (deactivate-mark)
404 (let (event end end-point)
405 (track-mouse
406 (while (progn
407 (setq event (read-event))
408 (or (mouse-movement-p event)
409 (eq (car-safe event) 'switch-frame)))
410 (if (eq (car-safe event) 'switch-frame)
411 nil
412 (setq end (event-end event)
413 end-point (posn-point end))
414
415 (cond
416 ;; Are we moving within the original window?
417 ((and (eq (posn-window end) start-window)
418 (integer-or-marker-p end-point))
419 (goto-char end-point)
420 (let ((range (mouse-start-end start-point (point) click-count)))
421 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
422
423 (t
424 (let ((mouse-row (cdr (cdr (mouse-position)))))
425 (cond
426 ((null mouse-row))
427 ((< mouse-row top)
428 (mouse-scroll-subr start-window (- mouse-row top)
429 mouse-drag-overlay start-point))
430 ((>= mouse-row bottom)
431 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
432 mouse-drag-overlay start-point)))))))))
433 (if (consp event)
434 (let ((fun (key-binding (vector (car event)))))
435 ;; Run the binding of the terminating up-event, if possible.
436 ;; In the case of a multiple click, it gives the wrong results,
437 ;; because it would fail to set up a region.
438 (if (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
439 (progn
440 (setq this-command fun)
441 ;; Delete the overlay before calling the function,
442 ;; because delete-overlay increases buffer-modified-tick.
443 (delete-overlay mouse-drag-overlay)
444 (funcall fun event))
445 (if (not (= (overlay-start mouse-drag-overlay)
446 (overlay-end mouse-drag-overlay)))
447 (let (last-command this-command)
448 (push-mark (overlay-start mouse-drag-overlay) t t)
449 (goto-char (overlay-end mouse-drag-overlay))
450 (delete-overlay mouse-drag-overlay)
451 (copy-region-as-kill (point) (mark t))
452 (mouse-set-region-1))
453 (goto-char (overlay-end mouse-drag-overlay))
454 (setq this-command 'mouse-set-point)
455 (delete-overlay mouse-drag-overlay))))
456 (delete-overlay mouse-drag-overlay)))))
457 \f
458 ;; Commands to handle xterm-style multiple clicks.
459
460 (defun mouse-skip-word (dir)
461 "Skip over word, over whitespace, or over identical punctuation.
462 If DIR is positive skip forward; if negative, skip backward."
463 (let* ((char (following-char))
464 (syntax (char-to-string (char-syntax char))))
465 (if (or (string= syntax "w") (string= syntax " "))
466 (if (< dir 0)
467 (skip-syntax-backward syntax)
468 (skip-syntax-forward syntax))
469 (if (< dir 0)
470 (while (and (not (bobp)) (= (preceding-char) char))
471 (forward-char -1))
472 (while (and (not (eobp)) (= (following-char) char))
473 (forward-char 1))))))
474
475 ;; Return a list of region bounds based on START and END according to MODE.
476 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
477 ;; If MODE is 1 then set point to start of word at (min START END),
478 ;; mark to end of word at (max START END).
479 ;; If MODE is 2 then do the same for lines.
480 (defun mouse-start-end (start end mode)
481 (if (> start end)
482 (let ((temp start))
483 (setq start end
484 end temp)))
485 (setq mode (mod mode 3))
486 (cond ((= mode 0)
487 (list start end))
488 ((and (= mode 1)
489 (= start end)
490 (char-after start)
491 (= (char-syntax (char-after start)) ?\())
492 (list start
493 (save-excursion
494 (goto-char start)
495 (forward-sexp 1)
496 (point))))
497 ((and (= mode 1)
498 (= start end)
499 (char-after start)
500 (= (char-syntax (char-after start)) ?\)))
501 (list (save-excursion
502 (goto-char (1+ start))
503 (backward-sexp 1)
504 (point))
505 (1+ start)))
506 ((= mode 1)
507 (list (save-excursion
508 (goto-char start)
509 (mouse-skip-word -1)
510 (point))
511 (save-excursion
512 (goto-char end)
513 (mouse-skip-word 1)
514 (point))))
515 ((= mode 2)
516 (list (save-excursion
517 (goto-char start)
518 (beginning-of-line 1)
519 (point))
520 (save-excursion
521 (goto-char end)
522 (forward-line 1)
523 (point))))))
524 \f
525 ;; Subroutine: set the mark where CLICK happened,
526 ;; but don't do anything else.
527 (defun mouse-set-mark-fast (click)
528 (mouse-minibuffer-check click)
529 (let ((posn (event-start click)))
530 (select-window (posn-window posn))
531 (if (numberp (posn-point posn))
532 (push-mark (posn-point posn) t t))))
533
534 ;; Momentarily show where the mark is, if highlighting doesn't show it.
535 (defun mouse-show-mark ()
536 (or transient-mark-mode
537 (save-excursion
538 (goto-char (mark t))
539 (sit-for 1))))
540
541 (defun mouse-set-mark (click)
542 "Set mark at the position clicked on with the mouse.
543 Display cursor at that position for a second.
544 This must be bound to a mouse click."
545 (interactive "e")
546 (mouse-minibuffer-check click)
547 (select-window (posn-window (event-start click)))
548 ;; We don't use save-excursion because that preserves the mark too.
549 (let ((point-save (point)))
550 (unwind-protect
551 (progn (mouse-set-point click)
552 (push-mark nil t t)
553 (or transient-mark-mode
554 (sit-for 1)))
555 (goto-char point-save))))
556
557 (defun mouse-kill (click)
558 "Kill the region between point and the mouse click.
559 The text is saved in the kill ring, as with \\[kill-region]."
560 (interactive "e")
561 (mouse-minibuffer-check click)
562 (let* ((posn (event-start click))
563 (click-posn (posn-point posn)))
564 (select-window (posn-window posn))
565 (if (numberp click-posn)
566 (kill-region (min (point) click-posn)
567 (max (point) click-posn)))))
568
569 (defun mouse-yank-at-click (click arg)
570 "Insert the last stretch of killed text at the position clicked on.
571 Also move point to one end of the text thus inserted (normally the end).
572 Prefix arguments are interpreted as with \\[yank].
573 If `mouse-yank-at-point' is non-nil, insert at point
574 regardless of where you click."
575 (interactive "e\nP")
576 ;; Give temporary modes such as isearch a chance to turn off.
577 (run-hooks 'mouse-leave-buffer-hook)
578 (or mouse-yank-at-point (mouse-set-point click))
579 (setq this-command 'yank)
580 (yank arg))
581
582 (defun mouse-kill-ring-save (click)
583 "Copy the region between point and the mouse click in the kill ring.
584 This does not delete the region; it acts like \\[kill-ring-save]."
585 (interactive "e")
586 (mouse-set-mark-fast click)
587 (let (this-command last-command)
588 (kill-ring-save (point) (mark t)))
589 (mouse-show-mark))
590
591 ;;; This function used to delete the text between point and the mouse
592 ;;; whenever it was equal to the front of the kill ring, but some
593 ;;; people found that confusing.
594
595 ;;; A list (TEXT START END), describing the text and position of the last
596 ;;; invocation of mouse-save-then-kill.
597 (defvar mouse-save-then-kill-posn nil)
598
599 (defun mouse-save-then-kill-delete-region (beg end)
600 ;; We must make our own undo boundaries
601 ;; because they happen automatically only for the current buffer.
602 (undo-boundary)
603 (if (or (= beg end) (eq buffer-undo-list t))
604 ;; If we have no undo list in this buffer,
605 ;; just delete.
606 (delete-region beg end)
607 ;; Delete, but make the undo-list entry share with the kill ring.
608 ;; First, delete just one char, so in case buffer is being modified
609 ;; for the first time, the undo list records that fact.
610 (let (before-change-function after-change-function
611 before-change-functions after-change-functions)
612 (delete-region beg
613 (+ beg (if (> end beg) 1 -1))))
614 (let ((buffer-undo-list buffer-undo-list))
615 ;; Undo that deletion--but don't change the undo list!
616 (let (before-change-function after-change-function
617 before-change-functions after-change-functions)
618 (primitive-undo 1 buffer-undo-list))
619 ;; Now delete the rest of the specified region,
620 ;; but don't record it.
621 (setq buffer-undo-list t)
622 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
623 (error "Lossage in mouse-save-then-kill-delete-region"))
624 (delete-region beg end))
625 (let ((tail buffer-undo-list))
626 ;; Search back in buffer-undo-list for the string
627 ;; that came from deleting one character.
628 (while (and tail (not (stringp (car (car tail)))))
629 (setq tail (cdr tail)))
630 ;; Replace it with an entry for the entire deleted text.
631 (and tail
632 (setcar tail (cons (car kill-ring) (min beg end))))))
633 (undo-boundary))
634
635 (defun mouse-save-then-kill (click)
636 "Save text to point in kill ring; the second time, kill the text.
637 If the text between point and the mouse is the same as what's
638 at the front of the kill ring, this deletes the text.
639 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
640 which prepares for a second click to delete the text.
641
642 If you have selected words or lines, this command extends the
643 selection through the word or line clicked on. If you do this
644 again in a different position, it extends the selection again.
645 If you do this twice in the same position, the selection is killed."
646 (interactive "e")
647 (let ((before-scroll point-before-scroll))
648 (mouse-minibuffer-check click)
649 (let ((click-posn (posn-point (event-start click)))
650 ;; Don't let a subsequent kill command append to this one:
651 ;; prevent setting this-command to kill-region.
652 (this-command this-command))
653 (if (and (mark t) (> (mod mouse-selection-click-count 3) 0))
654 (if (not (and (eq last-command 'mouse-save-then-kill)
655 (equal click-posn
656 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
657 ;; Find both ends of the object selected by this click.
658 (let* ((range
659 (mouse-start-end click-posn click-posn
660 mouse-selection-click-count)))
661 ;; Move whichever end is closer to the click.
662 ;; That's what xterm does, and it seems reasonable.
663 (if (< (abs (- click-posn (mark t)))
664 (abs (- click-posn (point))))
665 (set-mark (car range))
666 (goto-char (nth 1 range)))
667 ;; We have already put the old region in the kill ring.
668 ;; Replace it with the extended region.
669 ;; (It would be annoying to make a separate entry.)
670 (kill-new (buffer-substring (point) (mark t)) t)
671 (mouse-set-region-1)
672 ;; Arrange for a repeated mouse-3 to kill this region.
673 (setq mouse-save-then-kill-posn
674 (list (car kill-ring) (point) click-posn))
675 (mouse-show-mark))
676 ;; If we click this button again without moving it,
677 ;; that time kill.
678 (mouse-save-then-kill-delete-region (point) (mark))
679 (setq mouse-selection-click-count 0)
680 (setq mouse-save-then-kill-posn nil))
681 (if (and (eq last-command 'mouse-save-then-kill)
682 mouse-save-then-kill-posn
683 (eq (car mouse-save-then-kill-posn) (car kill-ring))
684 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
685 ;; If this is the second time we've called
686 ;; mouse-save-then-kill, delete the text from the buffer.
687 (progn
688 (mouse-save-then-kill-delete-region (point) (mark))
689 ;; After we kill, another click counts as "the first time".
690 (setq mouse-save-then-kill-posn nil))
691 (if (or (and (eq last-command 'mouse-save-then-kill)
692 mouse-save-then-kill-posn)
693 (and mark-active transient-mark-mode)
694 (and (memq last-command
695 '(mouse-drag-region mouse-set-region))
696 (or mark-even-if-inactive
697 (not transient-mark-mode))))
698 ;; We have a selection or suitable region, so adjust it.
699 (let* ((posn (event-start click))
700 (new (posn-point posn)))
701 (select-window (posn-window posn))
702 (if (numberp new)
703 (progn
704 ;; Move whichever end of the region is closer to the click.
705 ;; That is what xterm does, and it seems reasonable.
706 (if (< (abs (- new (point))) (abs (- new (mark t))))
707 (goto-char new)
708 (set-mark new))
709 (setq deactivate-mark nil)))
710 (kill-new (buffer-substring (point) (mark t)) t))
711 ;; Set the mark where point is, then move where clicked.
712 (mouse-set-mark-fast click)
713 (if before-scroll
714 (goto-char before-scroll))
715 (exchange-point-and-mark)
716 (kill-ring-save (point) (mark t)))
717 (mouse-show-mark)
718 (mouse-set-region-1)
719 (setq mouse-save-then-kill-posn
720 (list (car kill-ring) (point) click-posn)))))))
721 \f
722 (global-set-key [M-mouse-1] 'mouse-start-secondary)
723 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
724 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
725 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
726 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
727
728 ;; An overlay which records the current secondary selection
729 ;; or else is deleted when there is no secondary selection.
730 ;; May be nil.
731 (defvar mouse-secondary-overlay nil)
732
733 (defvar mouse-secondary-click-count 0)
734
735 ;; A marker which records the specified first end for a secondary selection.
736 ;; May be nil.
737 (defvar mouse-secondary-start nil)
738
739 (defun mouse-start-secondary (click)
740 "Set one end of the secondary selection to the position clicked on.
741 Use \\[mouse-secondary-save-then-kill] to set the other end
742 and complete the secondary selection."
743 (interactive "e")
744 (mouse-minibuffer-check click)
745 (let ((posn (event-start click)))
746 (save-excursion
747 (set-buffer (window-buffer (posn-window posn)))
748 ;; Cancel any preexisting secondary selection.
749 (if mouse-secondary-overlay
750 (delete-overlay mouse-secondary-overlay))
751 (if (numberp (posn-point posn))
752 (progn
753 (or mouse-secondary-start
754 (setq mouse-secondary-start (make-marker)))
755 (move-marker mouse-secondary-start (posn-point posn)))))))
756
757 (defun mouse-set-secondary (click)
758 "Set the secondary selection to the text that the mouse is dragged over.
759 This must be bound to a mouse drag event."
760 (interactive "e")
761 (mouse-minibuffer-check click)
762 (let ((posn (event-start click))
763 beg
764 (end (event-end click)))
765 (save-excursion
766 (set-buffer (window-buffer (posn-window posn)))
767 (if (numberp (posn-point posn))
768 (setq beg (posn-point posn)))
769 (if mouse-secondary-overlay
770 (move-overlay mouse-secondary-overlay beg (posn-point end))
771 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
772 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
773
774 (defun mouse-drag-secondary (start-event)
775 "Set the secondary selection to the text that the mouse is dragged over.
776 Highlight the drag area as you move the mouse.
777 This must be bound to a button-down mouse event."
778 (interactive "e")
779 (mouse-minibuffer-check start-event)
780 (let* ((start-posn (event-start start-event))
781 (start-point (posn-point start-posn))
782 (start-window (posn-window start-posn))
783 (start-frame (window-frame start-window))
784 (bounds (window-edges start-window))
785 (top (nth 1 bounds))
786 (bottom (if (window-minibuffer-p start-window)
787 (nth 3 bounds)
788 ;; Don't count the mode line.
789 (1- (nth 3 bounds))))
790 (click-count (1- (event-click-count start-event))))
791 (save-excursion
792 (set-buffer (window-buffer start-window))
793 (setq mouse-secondary-click-count click-count)
794 (or mouse-secondary-overlay
795 (setq mouse-secondary-overlay
796 (make-overlay (point) (point))))
797 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
798 (if (> (mod click-count 3) 0)
799 ;; Double or triple press: make an initial selection
800 ;; of one word or line.
801 (let ((range (mouse-start-end start-point start-point click-count)))
802 (set-marker mouse-secondary-start nil)
803 (move-overlay mouse-secondary-overlay 1 1
804 (window-buffer start-window))
805 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
806 (window-buffer start-window)))
807 ;; Single-press: cancel any preexisting secondary selection.
808 (or mouse-secondary-start
809 (setq mouse-secondary-start (make-marker)))
810 (set-marker mouse-secondary-start start-point)
811 (delete-overlay mouse-secondary-overlay))
812 (let (event end end-point)
813 (track-mouse
814 (while (progn
815 (setq event (read-event))
816 (or (mouse-movement-p event)
817 (eq (car-safe event) 'switch-frame)))
818
819 (if (eq (car-safe event) 'switch-frame)
820 nil
821 (setq end (event-end event)
822 end-point (posn-point end))
823 (cond
824 ;; Are we moving within the original window?
825 ((and (eq (posn-window end) start-window)
826 (integer-or-marker-p end-point))
827 (let ((range (mouse-start-end start-point end-point
828 click-count)))
829 (if (or (/= start-point end-point)
830 (null (marker-position mouse-secondary-start)))
831 (progn
832 (set-marker mouse-secondary-start nil)
833 (move-overlay mouse-secondary-overlay
834 (car range) (nth 1 range))))))
835 (t
836 (let ((mouse-row (cdr (cdr (mouse-position)))))
837 (cond
838 ((null mouse-row))
839 ((< mouse-row top)
840 (mouse-scroll-subr start-window (- mouse-row top)
841 mouse-secondary-overlay start-point))
842 ((>= mouse-row bottom)
843 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
844 mouse-secondary-overlay start-point)))))))))
845
846 (if (consp event)
847 ;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
848 ;;; (eq (posn-window (event-end event)) start-window)
849 ;;; (numberp (posn-point (event-end event)))
850 (if (marker-position mouse-secondary-start)
851 (save-window-excursion
852 (delete-overlay mouse-secondary-overlay)
853 (x-set-selection 'SECONDARY nil)
854 (select-window start-window)
855 (save-excursion
856 (goto-char mouse-secondary-start)
857 (sit-for 1)))
858 (x-set-selection
859 'SECONDARY
860 (buffer-substring (overlay-start mouse-secondary-overlay)
861 (overlay-end mouse-secondary-overlay)))))))))
862
863 (defun mouse-yank-secondary (click)
864 "Insert the secondary selection at the position clicked on.
865 Move point to the end of the inserted text.
866 If `mouse-yank-at-point' is non-nil, insert at point
867 regardless of where you click."
868 (interactive "e")
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 (insert (x-get-selection 'SECONDARY)))
873
874 (defun mouse-kill-secondary ()
875 "Kill the text in the secondary selection.
876 This is intended more as a keyboard command than as a mouse command
877 but it can work as either one.
878
879 The current buffer (in case of keyboard use), or the buffer clicked on,
880 must be the one that the secondary selection is in. This requirement
881 is to prevent accidents."
882 (interactive)
883 (let* ((keys (this-command-keys))
884 (click (elt keys (1- (length keys)))))
885 (or (eq (overlay-buffer mouse-secondary-overlay)
886 (if (listp click)
887 (window-buffer (posn-window (event-start click)))
888 (current-buffer)))
889 (error "Select or click on the buffer where the secondary selection is")))
890 (let (this-command)
891 (save-excursion
892 (set-buffer (overlay-buffer mouse-secondary-overlay))
893 (kill-region (overlay-start mouse-secondary-overlay)
894 (overlay-end mouse-secondary-overlay))))
895 (delete-overlay mouse-secondary-overlay)
896 ;;; (x-set-selection 'SECONDARY nil)
897 (setq mouse-secondary-overlay nil))
898
899 (defun mouse-secondary-save-then-kill (click)
900 "Save text to point in kill ring; the second time, kill the text.
901 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
902 If the text between where you did \\[mouse-start-secondary] and where
903 you use this command matches the text at the front of the kill ring,
904 this command deletes the text.
905 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
906 which prepares for a second click with this command to delete the text.
907
908 If you have already made a secondary selection in that buffer,
909 this command extends or retracts the selection to where you click.
910 If you do this again in a different position, it extends or retracts
911 again. If you do this twice in the same position, it kills the selection."
912 (interactive "e")
913 (mouse-minibuffer-check click)
914 (let ((posn (event-start click))
915 (click-posn (posn-point (event-start click)))
916 ;; Don't let a subsequent kill command append to this one:
917 ;; prevent setting this-command to kill-region.
918 (this-command this-command))
919 (or (eq (window-buffer (posn-window posn))
920 (or (and mouse-secondary-overlay
921 (overlay-buffer mouse-secondary-overlay))
922 (if mouse-secondary-start
923 (marker-buffer mouse-secondary-start))))
924 (error "Wrong buffer"))
925 (save-excursion
926 (set-buffer (window-buffer (posn-window posn)))
927 (if (> (mod mouse-secondary-click-count 3) 0)
928 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
929 (equal click-posn
930 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
931 ;; Find both ends of the object selected by this click.
932 (let* ((range
933 (mouse-start-end click-posn click-posn
934 mouse-secondary-click-count)))
935 ;; Move whichever end is closer to the click.
936 ;; That's what xterm does, and it seems reasonable.
937 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
938 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
939 (move-overlay mouse-secondary-overlay (car range)
940 (overlay-end mouse-secondary-overlay))
941 (move-overlay mouse-secondary-overlay
942 (overlay-start mouse-secondary-overlay)
943 (nth 1 range)))
944 ;; We have already put the old region in the kill ring.
945 ;; Replace it with the extended region.
946 ;; (It would be annoying to make a separate entry.)
947 (kill-new (buffer-substring
948 (overlay-start mouse-secondary-overlay)
949 (overlay-end mouse-secondary-overlay)) t)
950 ;; Arrange for a repeated mouse-3 to kill this region.
951 (setq mouse-save-then-kill-posn
952 (list (car kill-ring) (point) click-posn)))
953 ;; If we click this button again without moving it,
954 ;; that time kill.
955 (progn
956 (mouse-save-then-kill-delete-region
957 (overlay-start mouse-secondary-overlay)
958 (overlay-end mouse-secondary-overlay))
959 (setq mouse-save-then-kill-posn nil)
960 (setq mouse-secondary-click-count 0)
961 (delete-overlay mouse-secondary-overlay)))
962 (if (and (eq last-command 'mouse-secondary-save-then-kill)
963 mouse-save-then-kill-posn
964 (eq (car mouse-save-then-kill-posn) (car kill-ring))
965 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
966 ;; If this is the second time we've called
967 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
968 (progn
969 (mouse-save-then-kill-delete-region
970 (overlay-start mouse-secondary-overlay)
971 (overlay-end mouse-secondary-overlay))
972 (setq mouse-save-then-kill-posn nil)
973 (delete-overlay mouse-secondary-overlay))
974 (if (overlay-start mouse-secondary-overlay)
975 ;; We have a selection, so adjust it.
976 (progn
977 (if (numberp click-posn)
978 (progn
979 ;; Move whichever end of the region is closer to the click.
980 ;; That is what xterm does, and it seems reasonable.
981 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
982 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
983 (move-overlay mouse-secondary-overlay click-posn
984 (overlay-end mouse-secondary-overlay))
985 (move-overlay mouse-secondary-overlay
986 (overlay-start mouse-secondary-overlay)
987 click-posn))
988 (setq deactivate-mark nil)))
989 (if (eq last-command 'mouse-secondary-save-then-kill)
990 ;; If the front of the kill ring comes from
991 ;; an immediately previous use of this command,
992 ;; replace it with the extended region.
993 ;; (It would be annoying to make a separate entry.)
994 (kill-new (buffer-substring
995 (overlay-start mouse-secondary-overlay)
996 (overlay-end mouse-secondary-overlay)) t)
997 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
998 (overlay-end mouse-secondary-overlay))))
999 (if mouse-secondary-start
1000 ;; All we have is one end of a selection,
1001 ;; so put the other end here.
1002 (let ((start (+ 0 mouse-secondary-start)))
1003 (kill-ring-save start click-posn)
1004 (if mouse-secondary-overlay
1005 (move-overlay mouse-secondary-overlay start click-posn)
1006 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1007 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1008 (setq mouse-save-then-kill-posn
1009 (list (car kill-ring) (point) click-posn))))
1010 (if (overlay-buffer mouse-secondary-overlay)
1011 (x-set-selection 'SECONDARY
1012 (buffer-substring
1013 (overlay-start mouse-secondary-overlay)
1014 (overlay-end mouse-secondary-overlay)))))))
1015 \f
1016 (defun mouse-buffer-menu (event)
1017 "Pop up a menu of buffers for selection with the mouse.
1018 This switches buffers in the window that you clicked on,
1019 and selects that window."
1020 (interactive "e")
1021 (mouse-minibuffer-check event)
1022 (let ((menu
1023 (list "Buffer Menu"
1024 (cons "Select Buffer"
1025 (let ((tail (buffer-list))
1026 (maxbuf 0)
1027 head)
1028 (while tail
1029 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1030 (setq maxbuf
1031 (max maxbuf
1032 (length (buffer-name (car tail))))))
1033 (setq tail (cdr tail)))
1034 (setq tail (buffer-list))
1035 (while tail
1036 (let ((elt (car tail)))
1037 (if (not (string-match "^ "
1038 (buffer-name elt)))
1039 (setq head
1040 (cons
1041 (cons
1042 (format
1043 (format "%%%ds %%s%%s %%s" maxbuf)
1044 (buffer-name elt)
1045 (if (buffer-modified-p elt) "*" " ")
1046 (save-excursion
1047 (set-buffer elt)
1048 (if buffer-read-only "%" " "))
1049 (or (buffer-file-name elt)
1050 (save-excursion
1051 (set-buffer elt)
1052 (if list-buffers-directory
1053 (expand-file-name
1054 list-buffers-directory)))
1055 ""))
1056 elt)
1057 head))))
1058 (setq tail (cdr tail)))
1059 (reverse head))))))
1060 (let ((buf (x-popup-menu event menu))
1061 (window (posn-window (event-start event))))
1062 (if buf
1063 (progn
1064 (or (framep window) (select-window window))
1065 (switch-to-buffer buf))))))
1066 \f
1067 ;;; These need to be rewritten for the new scroll bar implementation.
1068
1069 ;;;!! ;; Commands for the scroll bar.
1070 ;;;!!
1071 ;;;!! (defun mouse-scroll-down (click)
1072 ;;;!! (interactive "@e")
1073 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1074 ;;;!!
1075 ;;;!! (defun mouse-scroll-up (click)
1076 ;;;!! (interactive "@e")
1077 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1078 ;;;!!
1079 ;;;!! (defun mouse-scroll-down-full ()
1080 ;;;!! (interactive "@")
1081 ;;;!! (scroll-down nil))
1082 ;;;!!
1083 ;;;!! (defun mouse-scroll-up-full ()
1084 ;;;!! (interactive "@")
1085 ;;;!! (scroll-up nil))
1086 ;;;!!
1087 ;;;!! (defun mouse-scroll-move-cursor (click)
1088 ;;;!! (interactive "@e")
1089 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1090 ;;;!!
1091 ;;;!! (defun mouse-scroll-absolute (event)
1092 ;;;!! (interactive "@e")
1093 ;;;!! (let* ((pos (car event))
1094 ;;;!! (position (car pos))
1095 ;;;!! (length (car (cdr pos))))
1096 ;;;!! (if (<= length 0) (setq length 1))
1097 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1098 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1099 ;;;!! position)
1100 ;;;!! length)
1101 ;;;!! scale-factor)))
1102 ;;;!! (goto-char newpos)
1103 ;;;!! (recenter '(4)))))
1104 ;;;!!
1105 ;;;!! (defun mouse-scroll-left (click)
1106 ;;;!! (interactive "@e")
1107 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1108 ;;;!!
1109 ;;;!! (defun mouse-scroll-right (click)
1110 ;;;!! (interactive "@e")
1111 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1112 ;;;!!
1113 ;;;!! (defun mouse-scroll-left-full ()
1114 ;;;!! (interactive "@")
1115 ;;;!! (scroll-left nil))
1116 ;;;!!
1117 ;;;!! (defun mouse-scroll-right-full ()
1118 ;;;!! (interactive "@")
1119 ;;;!! (scroll-right nil))
1120 ;;;!!
1121 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1122 ;;;!! (interactive "@e")
1123 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1124 ;;;!!
1125 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1126 ;;;!! (interactive "@e")
1127 ;;;!! (let* ((pos (car event))
1128 ;;;!! (position (car pos))
1129 ;;;!! (length (car (cdr pos))))
1130 ;;;!! (set-window-hscroll (selected-window) 33)))
1131 ;;;!!
1132 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1133 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1134 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1135 ;;;!!
1136 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1137 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1138 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1139 ;;;!!
1140 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1141 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1142 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1143 ;;;!!
1144 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1145 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1146 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1147 ;;;!!
1148 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1149 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1150 ;;;!! 'mouse-scroll-absolute-horizontally)
1151 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1152 ;;;!!
1153 ;;;!! (global-set-key [horizontal-slider mouse-1]
1154 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1155 ;;;!! (global-set-key [horizontal-slider mouse-2]
1156 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1157 ;;;!! (global-set-key [horizontal-slider mouse-3]
1158 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1159 ;;;!!
1160 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1161 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1162 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1163 ;;;!!
1164 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1165 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1166 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1167 ;;;!!
1168 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1169 ;;;!! 'mouse-split-window-horizontally)
1170 ;;;!! (global-set-key [mode-line S-mouse-2]
1171 ;;;!! 'mouse-split-window-horizontally)
1172 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1173 ;;;!! 'mouse-split-window)
1174 \f
1175 ;;;!! ;;;;
1176 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1177 ;;;!! ;;;; are of the form:
1178 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1179 ;;;!! ;;
1180 ;;;!! ;;;;
1181 ;;;!! ;;;; Dynamically track mouse coordinates
1182 ;;;!! ;;;;
1183 ;;;!! ;;
1184 ;;;!! ;;(defun track-mouse (event)
1185 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1186 ;;;!! ;; (interactive "@e")
1187 ;;;!! ;; (while mouse-grabbed
1188 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1189 ;;;!! ;; (abs-x (car pos))
1190 ;;;!! ;; (abs-y (cdr pos))
1191 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1192 ;;;!! ;; (list (car pos) (cdr pos))
1193 ;;;!! ;; (selected-window))))
1194 ;;;!! ;; (if (consp relative-coordinate)
1195 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1196 ;;;!! ;; (car relative-coordinate)
1197 ;;;!! ;; (car (cdr relative-coordinate)))
1198 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1199 ;;;!!
1200 ;;;!! ;;
1201 ;;;!! ;; Dynamically put a box around the line indicated by point
1202 ;;;!! ;;
1203 ;;;!! ;;
1204 ;;;!! ;;(require 'backquote)
1205 ;;;!! ;;
1206 ;;;!! ;;(defun mouse-select-buffer-line (event)
1207 ;;;!! ;; (interactive "@e")
1208 ;;;!! ;; (let ((relative-coordinate
1209 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1210 ;;;!! ;; (abs-y (car (cdr (car event)))))
1211 ;;;!! ;; (if (consp relative-coordinate)
1212 ;;;!! ;; (progn
1213 ;;;!! ;; (save-excursion
1214 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1215 ;;;!! ;; (x-draw-rectangle
1216 ;;;!! ;; (selected-screen)
1217 ;;;!! ;; abs-y 0
1218 ;;;!! ;; (save-excursion
1219 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1220 ;;;!! ;; (end-of-line)
1221 ;;;!! ;; (push-mark nil t)
1222 ;;;!! ;; (beginning-of-line)
1223 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1224 ;;;!! ;; (sit-for 1)
1225 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1226 ;;;!! ;;
1227 ;;;!! ;;(defvar last-line-drawn nil)
1228 ;;;!! ;;(defvar begin-delim "[^ \t]")
1229 ;;;!! ;;(defvar end-delim "[^ \t]")
1230 ;;;!! ;;
1231 ;;;!! ;;(defun mouse-boxing (event)
1232 ;;;!! ;; (interactive "@e")
1233 ;;;!! ;; (save-excursion
1234 ;;;!! ;; (let ((screen (selected-screen)))
1235 ;;;!! ;; (while (= (x-mouse-events) 0)
1236 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1237 ;;;!! ;; (abs-x (car pos))
1238 ;;;!! ;; (abs-y (cdr pos))
1239 ;;;!! ;; (relative-coordinate
1240 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1241 ;;;!! ;; (selected-window)))
1242 ;;;!! ;; (begin-reg nil)
1243 ;;;!! ;; (end-reg nil)
1244 ;;;!! ;; (end-column nil)
1245 ;;;!! ;; (begin-column nil))
1246 ;;;!! ;; (if (and (consp relative-coordinate)
1247 ;;;!! ;; (or (not last-line-drawn)
1248 ;;;!! ;; (not (= last-line-drawn abs-y))))
1249 ;;;!! ;; (progn
1250 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1251 ;;;!! ;; (if (= (following-char) 10)
1252 ;;;!! ;; ()
1253 ;;;!! ;; (progn
1254 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1255 ;;;!! ;; (setq begin-column (1- (current-column)))
1256 ;;;!! ;; (end-of-line)
1257 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1258 ;;;!! ;; (setq end-column (1+ (current-column)))
1259 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1260 ;;;!! ;; (x-draw-rectangle screen
1261 ;;;!! ;; (setq last-line-drawn abs-y)
1262 ;;;!! ;; begin-column
1263 ;;;!! ;; (- end-column begin-column) 1))))))))))
1264 ;;;!! ;;
1265 ;;;!! ;;(defun mouse-erase-box ()
1266 ;;;!! ;; (interactive)
1267 ;;;!! ;; (if last-line-drawn
1268 ;;;!! ;; (progn
1269 ;;;!! ;; (x-erase-rectangle (selected-screen))
1270 ;;;!! ;; (setq last-line-drawn nil))))
1271 ;;;!!
1272 ;;;!! ;;; (defun test-x-rectangle ()
1273 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1274 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1275 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1276 ;;;!!
1277 ;;;!! ;;
1278 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1279 ;;;!! ;;
1280 ;;;!!
1281 ;;;!! (defvar double-start nil)
1282 ;;;!! (defconst double-click-interval 300
1283 ;;;!! "Max ticks between clicks")
1284 ;;;!!
1285 ;;;!! (defun double-down (event)
1286 ;;;!! (interactive "@e")
1287 ;;;!! (if double-start
1288 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1289 ;;;!! (if (< interval double-click-interval)
1290 ;;;!! (progn
1291 ;;;!! (backward-up-list 1)
1292 ;;;!! ;; (message "Interval %d" interval)
1293 ;;;!! (sleep-for 1)))
1294 ;;;!! (setq double-start nil))
1295 ;;;!! (setq double-start (nth 4 event))))
1296 ;;;!!
1297 ;;;!! (defun double-up (event)
1298 ;;;!! (interactive "@e")
1299 ;;;!! (and double-start
1300 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1301 ;;;!! (setq double-start nil)))
1302 ;;;!!
1303 ;;;!! ;;; (defun x-test-doubleclick ()
1304 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1305 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1306 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1307 ;;;!!
1308 ;;;!! ;;
1309 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1310 ;;;!! ;;
1311 ;;;!!
1312 ;;;!! (defvar scrolled-lines 0)
1313 ;;;!! (defconst scroll-speed 1)
1314 ;;;!!
1315 ;;;!! (defun incr-scroll-down (event)
1316 ;;;!! (interactive "@e")
1317 ;;;!! (setq scrolled-lines 0)
1318 ;;;!! (incremental-scroll scroll-speed))
1319 ;;;!!
1320 ;;;!! (defun incr-scroll-up (event)
1321 ;;;!! (interactive "@e")
1322 ;;;!! (setq scrolled-lines 0)
1323 ;;;!! (incremental-scroll (- scroll-speed)))
1324 ;;;!!
1325 ;;;!! (defun incremental-scroll (n)
1326 ;;;!! (while (= (x-mouse-events) 0)
1327 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1328 ;;;!! (scroll-down n)
1329 ;;;!! (sit-for 300 t)))
1330 ;;;!!
1331 ;;;!! (defun incr-scroll-stop (event)
1332 ;;;!! (interactive "@e")
1333 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1334 ;;;!! (setq scrolled-lines 0)
1335 ;;;!! (sleep-for 1))
1336 ;;;!!
1337 ;;;!! ;;; (defun x-testing-scroll ()
1338 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1339 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1340 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1341 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1342 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1343 ;;;!!
1344 ;;;!! ;;
1345 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1346 ;;;!! ;;
1347 ;;;!!
1348 ;;;!! (defun mouse-kill-rectangle (event)
1349 ;;;!! "Kill the rectangle between point and the mouse cursor."
1350 ;;;!! (interactive "@e")
1351 ;;;!! (let ((point-save (point)))
1352 ;;;!! (save-excursion
1353 ;;;!! (mouse-set-point event)
1354 ;;;!! (push-mark nil t)
1355 ;;;!! (if (> point-save (point))
1356 ;;;!! (kill-rectangle (point) point-save)
1357 ;;;!! (kill-rectangle point-save (point))))))
1358 ;;;!!
1359 ;;;!! (defun mouse-open-rectangle (event)
1360 ;;;!! "Kill the rectangle between point and the mouse cursor."
1361 ;;;!! (interactive "@e")
1362 ;;;!! (let ((point-save (point)))
1363 ;;;!! (save-excursion
1364 ;;;!! (mouse-set-point event)
1365 ;;;!! (push-mark nil t)
1366 ;;;!! (if (> point-save (point))
1367 ;;;!! (open-rectangle (point) point-save)
1368 ;;;!! (open-rectangle point-save (point))))))
1369 ;;;!!
1370 ;;;!! ;; Must be a better way to do this.
1371 ;;;!!
1372 ;;;!! (defun mouse-multiple-insert (n char)
1373 ;;;!! (while (> n 0)
1374 ;;;!! (insert char)
1375 ;;;!! (setq n (1- n))))
1376 ;;;!!
1377 ;;;!! ;; What this could do is not finalize until button was released.
1378 ;;;!!
1379 ;;;!! (defun mouse-move-text (event)
1380 ;;;!! "Move text from point to cursor position, inserting spaces."
1381 ;;;!! (interactive "@e")
1382 ;;;!! (let* ((relative-coordinate
1383 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1384 ;;;!! (if (consp relative-coordinate)
1385 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1386 ;;;!! (delete-char
1387 ;;;!! (- (car relative-coordinate) (current-column))))
1388 ;;;!! ((< (current-column) (car relative-coordinate))
1389 ;;;!! (mouse-multiple-insert
1390 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1391 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1392 \f
1393 ;; Choose a completion with the mouse.
1394
1395 (defun mouse-choose-completion (event)
1396 "Click on an alternative in the `*Completions*' buffer to choose it."
1397 (interactive "e")
1398 ;; Give temporary modes such as isearch a chance to turn off.
1399 (run-hooks 'mouse-leave-buffer-hook)
1400 (let ((buffer (window-buffer))
1401 choice
1402 base-size)
1403 (save-excursion
1404 (set-buffer (window-buffer (posn-window (event-start event))))
1405 (if completion-reference-buffer
1406 (setq buffer completion-reference-buffer))
1407 (setq base-size completion-base-size)
1408 (save-excursion
1409 (goto-char (posn-point (event-start event)))
1410 (let (beg end)
1411 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1412 (setq end (point) beg (1+ (point))))
1413 (if (null beg)
1414 (error "No completion here"))
1415 (setq beg (previous-single-property-change beg 'mouse-face))
1416 (setq end (or (next-single-property-change end 'mouse-face)
1417 (point-max)))
1418 (setq choice (buffer-substring beg end)))))
1419 (let ((owindow (selected-window)))
1420 (select-window (posn-window (event-start event)))
1421 (if (and (one-window-p t 'selected-frame)
1422 (window-dedicated-p (selected-window)))
1423 ;; This is a special buffer's frame
1424 (iconify-frame (selected-frame))
1425 (or (window-dedicated-p (selected-window))
1426 (bury-buffer)))
1427 (select-window owindow))
1428 (choose-completion-string choice buffer base-size)))
1429 \f
1430 ;; Font selection.
1431
1432 (defun font-menu-add-default ()
1433 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1434 (font-alist x-fixed-font-alist)
1435 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1436 (if (assoc "Default" elt)
1437 (delete (assoc "Default" elt) elt))
1438 (setcdr elt
1439 (cons (list "Default"
1440 (cdr (assq 'font (frame-parameters (selected-frame)))))
1441 (cdr elt)))))
1442
1443 (defvar x-fixed-font-alist
1444 '("Font menu"
1445 ("Misc"
1446 ;; For these, we specify the pixel height and width.
1447 ("fixed" "fixed")
1448 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1449 ("6x12"
1450 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1451 ("6x13"
1452 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1453 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1454 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1455 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1456 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1457 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1458 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1459 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1460 ("")
1461 ("clean 5x8"
1462 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1463 ("clean 6x8"
1464 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1465 ("clean 8x8"
1466 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1467 ("clean 8x10"
1468 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1469 ("clean 8x14"
1470 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1471 ("clean 8x16"
1472 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1473 ("")
1474 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
1475 ;;; We don't seem to have these; who knows what they are.
1476 ;;; ("fg-18" "fg-18")
1477 ;;; ("fg-25" "fg-25")
1478 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1479 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1480 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1481 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1482 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1483 ("Courier"
1484 ;; For these, we specify the point height.
1485 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1486 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1487 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1488 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1489 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1490 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1491 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1492 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1493 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1494 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1495 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1496 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1497 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1498 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1499 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1500 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1501 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1502 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1503 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1504 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1505 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1506 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1507 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1508 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
1509 )
1510 "X fonts suitable for use in Emacs.")
1511
1512 (defun mouse-set-font (&rest fonts)
1513 "Select an emacs font from a list of known good fonts"
1514 (interactive
1515 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
1516 (if fonts
1517 (let (font)
1518 (while fonts
1519 (condition-case nil
1520 (progn
1521 (set-default-font (car fonts))
1522 (setq font (car fonts))
1523 (setq fonts nil))
1524 (error
1525 (setq fonts (cdr fonts)))))
1526 (if (null font)
1527 (error "Font not found")))))
1528 \f
1529 ;;; Bindings for mouse commands.
1530
1531 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1532 (global-set-key [mouse-1] 'mouse-set-point)
1533 (global-set-key [drag-mouse-1] 'mouse-set-region)
1534
1535 ;; These are tested for in mouse-drag-region.
1536 (global-set-key [double-mouse-1] 'mouse-set-point)
1537 (global-set-key [triple-mouse-1] 'mouse-set-point)
1538
1539 (global-set-key [mouse-2] 'mouse-yank-at-click)
1540 (global-set-key [mouse-3] 'mouse-save-then-kill)
1541
1542 ;; By binding these to down-going events, we let the user use the up-going
1543 ;; event to make the selection, saving a click.
1544 (global-set-key [C-down-mouse-1] 'mouse-set-font)
1545 ;; C-down-mouse-2 is bound in facemenu.el.
1546 (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1547
1548
1549 ;; Replaced with dragging mouse-1
1550 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1551
1552 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1553 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1554 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1555 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1556 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1557 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1558 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1559 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1560
1561 (provide 'mouse)
1562
1563 ;;; mouse.el ends here