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