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