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