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