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