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