new version
[emacs.git] / lisp / mouse.el
blob410a9ff6b193402e27f9301c547d1112d872e514
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package provides various useful commands (including help
28 ;; system access) through the mouse. All this code assumes that mouse
29 ;; interpretation has been abstracted into Emacs input events.
31 ;; The code is rather X-dependent.
33 ;;; Code:
35 ;;; Utility functions.
37 ;;; Indent track-mouse like progn.
38 (put 'track-mouse 'lisp-indent-function 0)
40 (defcustom mouse-yank-at-point nil
41 "*If non-nil, mouse yank commands yank at point instead of at click."
42 :type 'boolean
43 :group 'mouse)
45 ;; Provide a mode-specific menu on a mouse button.
47 (defun mouse-major-mode-menu (event prefix)
48 "Pop up a mode-specific menu of mouse commands."
49 ;; Switch to the window clicked on, because otherwise
50 ;; the mode's commands may not make sense.
51 (interactive "@e\nP")
52 (let (;; This is where mouse-major-mode-menu-prefix
53 ;; returns the prefix we should use (after menu-bar).
54 ;; It is either nil or (SOME-SYMBOL).
55 (mouse-major-mode-menu-prefix nil)
56 ;; Make a keymap in which our last command leads to a menu
57 (newmap (make-sparse-keymap (concat mode-name " Mode")))
58 result)
59 ;; Make our menu inherit from the desired keymap
60 ;; which we want to display as the menu now.
61 (set-keymap-parent newmap
62 (mouse-major-mode-menu-1
63 (and (current-local-map)
64 (lookup-key (current-local-map) [menu-bar]))))
65 (setq result (x-popup-menu t (list newmap)))
66 (if result
67 (let ((command (key-binding
68 (apply 'vector (append '(menu-bar)
69 mouse-major-mode-menu-prefix
70 result)))))
71 ;; Clear out echoing, which perhaps shows a prefix arg.
72 (message "")
73 (if command
74 (progn
75 (setq prefix-arg prefix)
76 (command-execute command)))))))
78 ;; Compute and cache the equivalent keys in MENU and all its submenus.
79 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
80 ;;; (and (eq (car menu) 'keymap)
81 ;;; (x-popup-menu nil menu))
82 ;;; (while menu
83 ;;; (and (consp (car menu))
84 ;;; (consp (cdr (car menu)))
85 ;;; (let ((tail (cdr (car menu))))
86 ;;; (while (and (consp tail)
87 ;;; (not (eq (car tail) 'keymap)))
88 ;;; (setq tail (cdr tail)))
89 ;;; (if (consp tail)
90 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
91 ;;; (setq menu (cdr menu))))
93 ;; Given a mode's menu bar keymap,
94 ;; if it defines exactly one menu bar menu,
95 ;; return just that menu.
96 ;; Otherwise return a menu for all of them.
97 (defun mouse-major-mode-menu-1 (menubar)
98 (if menubar
99 (let ((tail menubar)
100 submap)
101 (while tail
102 (if (consp (car tail))
103 (if submap
104 (setq submap t)
105 (setq submap (car tail))))
106 (setq tail (cdr tail)))
107 (if (eq submap t)
108 menubar
109 (setq mouse-major-mode-menu-prefix (list (car submap)))
110 (cdr (cdr submap))))))
112 ;; Commands that operate on windows.
114 (defun mouse-minibuffer-check (event)
115 (let ((w (posn-window (event-start event))))
116 (and (window-minibuffer-p w)
117 (not (minibuffer-window-active-p w))
118 (error "Minibuffer window is not active")))
119 ;; Give temporary modes such as isearch a chance to turn off.
120 (run-hooks 'mouse-leave-buffer-hook))
122 (defun mouse-delete-window (click)
123 "Delete the window you click on.
124 This must be bound to a mouse click."
125 (interactive "e")
126 (mouse-minibuffer-check click)
127 (delete-window (posn-window (event-start click))))
129 (defun mouse-select-window (click)
130 "Select the window clicked on; don't move point."
131 (interactive "e")
132 (mouse-minibuffer-check click)
133 (let ((oframe (selected-frame))
134 (frame (window-frame (posn-window (event-start click)))))
135 (select-window (posn-window (event-start click)))
136 (raise-frame frame)
137 (select-frame frame)
138 (or (eq frame oframe)
139 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
141 (defun mouse-tear-off-window (click)
142 "Delete the window clicked on, and create a new frame displaying its buffer."
143 (interactive "e")
144 (mouse-minibuffer-check click)
145 (let* ((window (posn-window (event-start click)))
146 (buf (window-buffer window))
147 (frame (make-frame)))
148 (select-frame frame)
149 (switch-to-buffer buf)
150 (delete-window window)))
152 (defun mouse-delete-other-windows ()
153 "Delete all window except the one you click on."
154 (interactive "@")
155 (delete-other-windows))
157 (defun mouse-split-window-vertically (click)
158 "Select Emacs window mouse is on, then split it vertically in half.
159 The window is split at the line 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-height (1+ (cdr (posn-col-row (event-end click)))))
166 (first-line window-min-height)
167 (last-line (- (window-height) window-min-height)))
168 (if (< last-line first-line)
169 (error "Window too short to split")
170 (split-window-vertically
171 (min (max new-height first-line) last-line))))))
173 (defun mouse-split-window-horizontally (click)
174 "Select Emacs window mouse is on, then split it horizontally in half.
175 The window is split at the column clicked on.
176 This command must be bound to a mouse click."
177 (interactive "@e")
178 (mouse-minibuffer-check click)
179 (let ((start (event-start click)))
180 (select-window (posn-window start))
181 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
182 (first-col window-min-width)
183 (last-col (- (window-width) window-min-width)))
184 (if (< last-col first-col)
185 (error "Window too narrow to split")
186 (split-window-horizontally
187 (min (max new-width first-col) last-col))))))
189 (defun mouse-drag-mode-line (start-event)
190 "Change the height of a window by dragging on the mode line."
191 (interactive "e")
192 ;; Give temporary modes such as isearch a chance to turn off.
193 (run-hooks 'mouse-leave-buffer-hook)
194 (let ((done nil)
195 (echo-keystrokes 0)
196 (start-event-frame (window-frame (car (car (cdr start-event)))))
197 (start-event-window (car (car (cdr start-event))))
198 (start-nwindows (count-windows t))
199 (old-selected-window (selected-window))
200 should-enlarge-minibuffer
201 event mouse minibuffer y top bot edges wconfig params growth)
202 (setq params (frame-parameters))
203 (setq minibuffer (cdr (assq 'minibuffer params)))
204 (track-mouse
205 (progn
206 ;; enlarge-window only works on the selected window, so
207 ;; we must select the window where the start event originated.
208 ;; unwind-protect will restore the old selected window later.
209 (select-window start-event-window)
210 ;; if this is the bottommost ordinary window, then to
211 ;; move its modeline the minibuffer must be enlarged.
212 (setq should-enlarge-minibuffer
213 (and minibuffer
214 (not (one-window-p t))
215 (= (nth 1 (window-edges minibuffer))
216 (nth 3 (window-edges)))))
217 ;; loop reading events and sampling the position of
218 ;; the mouse.
219 (while (not done)
220 (setq event (read-event)
221 mouse (mouse-position))
222 ;; do nothing if
223 ;; - there is a switch-frame event.
224 ;; - the mouse isn't in the frame that we started in
225 ;; - the mouse isn't in any Emacs frame
226 ;; drag if
227 ;; - there is a mouse-movement event
228 ;; - there is a scroll-bar-movement event
229 ;; (same as mouse movement for our purposes)
230 ;; quit if
231 ;; - there is a keyboard event or some other unknown event
232 ;; unknown event.
233 (cond ((integerp event)
234 (setq done t))
235 ((eq (car event) 'switch-frame)
236 nil)
237 ((not (memq (car event)
238 '(mouse-movement scroll-bar-movement)))
239 (if (consp event)
240 (setq unread-command-events
241 (cons event unread-command-events)))
242 (setq done t))
243 ((not (eq (car mouse) start-event-frame))
244 nil)
245 ((null (car (cdr mouse)))
246 nil)
248 (setq y (cdr (cdr mouse))
249 edges (window-edges)
250 top (nth 1 edges)
251 bot (nth 3 edges))
252 ;; scale back a move that would make the
253 ;; window too short.
254 (cond ((< (- y top -1) window-min-height)
255 (setq y (+ top window-min-height -1))))
256 ;; compute size change needed
257 (setq growth (- y bot -1)
258 wconfig (current-window-configuration))
259 ;; Check for an error case.
260 (if (and (/= growth 0)
261 (not minibuffer)
262 (one-window-p t))
263 (error "Attempt to resize sole window"))
264 ;; grow/shrink minibuffer?
265 (if should-enlarge-minibuffer
266 (progn
267 ;; yes. briefly select minibuffer so
268 ;; enlarge-window will affect the
269 ;; correct window.
270 (select-window minibuffer)
271 ;; scale back shrinkage if it would
272 ;; make the minibuffer less than 1
273 ;; line tall.
274 (if (and (> growth 0)
275 (< (- (window-height minibuffer)
276 growth)
278 (setq growth (1- (window-height minibuffer))))
279 (enlarge-window (- growth))
280 (select-window start-event-window))
281 ;; no. grow/shrink the selected window
282 (enlarge-window growth))
283 ;; if this window's growth caused another
284 ;; window to be deleted because it was too
285 ;; short, rescind the change.
287 ;; if size change caused space to be stolen
288 ;; from a window above this one, rescind the
289 ;; change, but only if we didn't grow/srhink
290 ;; the minibuffer. minibuffer size changes
291 ;; can cause all windows to shrink... no way
292 ;; around it.
293 (if (or (/= start-nwindows (count-windows t))
294 (and (not should-enlarge-minibuffer)
295 (/= top (nth 1 (window-edges)))))
296 (set-window-configuration wconfig)))))))))
298 (defun mouse-drag-vertical-line (start-event)
299 "Change the width of a window by dragging on the vertical line."
300 (interactive "e")
301 ;; Give temporary modes such as isearch a chance to turn off.
302 (run-hooks 'mouse-leave-buffer-hook)
303 (let ((done nil)
304 (echo-keystrokes 0)
305 (start-event-frame (window-frame (car (car (cdr start-event)))))
306 (start-event-window (car (car (cdr start-event))))
307 (start-nwindows (count-windows t))
308 (old-selected-window (selected-window))
309 event mouse x left right edges wconfig growth)
310 (if (one-window-p t)
311 (error "Attempt to resize sole ordinary window"))
312 (if (= (nth 2 (window-edges start-event-window))
313 (frame-width start-event-frame))
314 (error "Attempt to drag rightmost scrollbar"))
315 (track-mouse
316 (progn
317 ;; enlarge-window only works on the selected window, so
318 ;; we must select the window where the start event originated.
319 ;; unwind-protect will restore the old selected window later.
320 (select-window start-event-window)
321 ;; loop reading events and sampling the position of
322 ;; the mouse.
323 (while (not done)
324 (setq event (read-event)
325 mouse (mouse-position))
326 ;; do nothing if
327 ;; - there is a switch-frame event.
328 ;; - the mouse isn't in the frame that we started in
329 ;; - the mouse isn't in any Emacs frame
330 ;; drag if
331 ;; - there is a mouse-movement event
332 ;; - there is a scroll-bar-movement event
333 ;; (same as mouse movement for our purposes)
334 ;; quit if
335 ;; - there is a keyboard event or some other unknown event
336 ;; unknown event.
337 (cond ((integerp event)
338 (setq done t))
339 ((eq (car event) 'switch-frame)
340 nil)
341 ((not (memq (car event)
342 '(mouse-movement scroll-bar-movement)))
343 (if (consp event)
344 (setq unread-command-events
345 (cons event unread-command-events)))
346 (setq done t))
347 ((not (eq (car mouse) start-event-frame))
348 nil)
349 ((null (car (cdr mouse)))
350 nil)
352 (setq x (car (cdr mouse))
353 edges (window-edges)
354 left (nth 0 edges)
355 right (nth 2 edges))
356 ;; scale back a move that would make the
357 ;; window too thin.
358 (cond ((< (- x left -1) window-min-width)
359 (setq x (+ left window-min-width -1))))
360 ;; compute size change needed
361 (setq growth (- x right -1)
362 wconfig (current-window-configuration))
363 (enlarge-window growth t)
364 ;; if this window's growth caused another
365 ;; window to be deleted because it was too
366 ;; thin, rescind the change.
368 ;; if size change caused space to be stolen
369 ;; from a window to the left of this one,
370 ;; rescind the change.
371 (if (or (/= start-nwindows (count-windows t))
372 (/= left (nth 0 (window-edges))))
373 (set-window-configuration wconfig)))))))))
375 (defun mouse-set-point (event)
376 "Move point to the position clicked on with the mouse.
377 This should be bound to a mouse click event type."
378 (interactive "e")
379 (mouse-minibuffer-check event)
380 ;; Use event-end in case called from mouse-drag-region.
381 ;; If EVENT is a click, event-end and event-start give same value.
382 (let ((posn (event-end event)))
383 (if (not (windowp (posn-window posn)))
384 (error "Cursor not in text area of window"))
385 (select-window (posn-window posn))
386 (if (numberp (posn-point posn))
387 (goto-char (posn-point posn)))))
389 (defvar mouse-last-region-beg nil)
390 (defvar mouse-last-region-end nil)
391 (defvar mouse-last-region-tick nil)
393 (defun mouse-region-match ()
394 "Return non-nil if there's an active region that was set with the mouse."
395 (and (mark t) mark-active
396 (eq mouse-last-region-beg (region-beginning))
397 (eq mouse-last-region-end (region-end))
398 (eq mouse-last-region-tick (buffer-modified-tick))))
400 (defun mouse-set-region (click)
401 "Set the region to the text dragged over, and copy to kill ring.
402 This should be bound to a mouse drag event."
403 (interactive "e")
404 (mouse-minibuffer-check click)
405 (let ((posn (event-start click))
406 (end (event-end click)))
407 (select-window (posn-window posn))
408 (if (numberp (posn-point posn))
409 (goto-char (posn-point posn)))
410 ;; If mark is highlighted, no need to bounce the cursor.
411 ;; On X, we highlight while dragging, thus once again no need to bounce.
412 (or transient-mark-mode
413 (memq (framep (selected-frame)) '(x pc w32))
414 (sit-for 1))
415 (push-mark)
416 (set-mark (point))
417 (if (numberp (posn-point end))
418 (goto-char (posn-point end)))
419 ;; Don't set this-command to kill-region, so that a following
420 ;; C-w will not double the text in the kill ring.
421 ;; Ignore last-command so we don't append to a preceding kill.
422 (let (this-command last-command)
423 (copy-region-as-kill (mark) (point)))
424 (mouse-set-region-1)))
426 (defun mouse-set-region-1 ()
427 (setq mouse-last-region-beg (region-beginning))
428 (setq mouse-last-region-end (region-end))
429 (setq mouse-last-region-tick (buffer-modified-tick)))
431 (defcustom mouse-scroll-delay 0.25
432 "*The pause between scroll steps caused by mouse drags, in seconds.
433 If you drag the mouse beyond the edge of a window, Emacs scrolls the
434 window to bring the text beyond that edge into view, with a delay of
435 this many seconds between scroll steps. Scrolling stops when you move
436 the mouse back into the window, or release the button.
437 This variable's value may be non-integral.
438 Setting this to zero causes Emacs to scroll as fast as it can."
439 :type 'number
440 :group 'mouse)
442 (defcustom mouse-scroll-min-lines 1
443 "*The minimum number of lines scrolled by dragging mouse out of window.
444 Moving the mouse out the top or bottom edge of the window begins
445 scrolling repeatedly. The number of lines scrolled per repetition
446 is normally equal to the number of lines beyond the window edge that
447 the mouse has moved. However, it always scrolls at least the number
448 of lines specified by this variable."
449 :type 'integer
450 :group 'mouse)
452 (defun mouse-scroll-subr (window jump &optional overlay start)
453 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
454 If OVERLAY is an overlay, let it stretch from START to the far edge of
455 the newly visible text.
456 Upon exit, point is at the far edge of the newly visible text."
457 (cond
458 ((and (> jump 0) (< jump mouse-scroll-min-lines))
459 (setq jump mouse-scroll-min-lines))
460 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
461 (setq jump (- mouse-scroll-min-lines))))
462 (let ((opoint (point)))
463 (while (progn
464 (goto-char (window-start window))
465 (if (not (zerop (vertical-motion jump window)))
466 (progn
467 (set-window-start window (point))
468 (if (natnump jump)
469 (progn
470 (goto-char (window-end window))
471 ;; window-end doesn't reflect the window's new
472 ;; start position until the next redisplay. Hurrah.
473 (vertical-motion (1- jump) window))
474 (goto-char (window-start window)))
475 (if overlay
476 (move-overlay overlay start (point)))
477 ;; Now that we have scrolled WINDOW properly,
478 ;; put point back where it was for the redisplay
479 ;; so that we don't mess up the selected window.
480 (or (eq window (selected-window))
481 (goto-char opoint))
482 (sit-for mouse-scroll-delay)))))
483 (or (eq window (selected-window))
484 (goto-char opoint))))
486 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
487 (defvar mouse-drag-overlay (make-overlay 1 1))
488 (delete-overlay mouse-drag-overlay)
489 (overlay-put mouse-drag-overlay 'face 'region)
491 (defvar mouse-selection-click-count 0)
493 (defvar mouse-selection-click-count-buffer nil)
495 (defun mouse-drag-region (start-event)
496 "Set the region to the text that the mouse is dragged over.
497 Highlight the drag area as you move the mouse.
498 This must be bound to a button-down mouse event.
499 In Transient Mark mode, the highlighting remains as long as the mark
500 remains active. Otherwise, it remains until the next input event."
501 (interactive "e")
502 (mouse-minibuffer-check start-event)
503 (let* ((echo-keystrokes 0)
504 (start-posn (event-start start-event))
505 (start-point (posn-point start-posn))
506 (start-window (posn-window start-posn))
507 (start-frame (window-frame start-window))
508 (bounds (window-edges start-window))
509 (top (nth 1 bounds))
510 (bottom (if (window-minibuffer-p start-window)
511 (nth 3 bounds)
512 ;; Don't count the mode line.
513 (1- (nth 3 bounds))))
514 (click-count (1- (event-click-count start-event))))
515 (setq mouse-selection-click-count click-count)
516 (setq mouse-selection-click-count-buffer (current-buffer))
517 (mouse-set-point start-event)
518 ;; In case the down click is in the middle of some intangible text,
519 ;; use the end of that text, and put it in START-POINT.
520 (if (< (point) start-point)
521 (goto-char start-point))
522 (setq start-point (point))
523 (let ((range (mouse-start-end start-point start-point click-count)))
524 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
525 (window-buffer start-window)))
526 (deactivate-mark)
527 ;; end-of-range is used only in the single-click case.
528 ;; It is the place where the drag has reached so far
529 ;; (but not outside the window where the drag started).
530 (let (event end end-point last-end-point (end-of-range (point)))
531 (track-mouse
532 (while (progn
533 (setq event (read-event))
534 (or (mouse-movement-p event)
535 (eq (car-safe event) 'switch-frame)))
536 (if (eq (car-safe event) 'switch-frame)
538 (setq end (event-end event)
539 end-point (posn-point end))
540 (if (numberp end-point)
541 (setq last-end-point end-point))
543 (cond
544 ;; Are we moving within the original window?
545 ((and (eq (posn-window end) start-window)
546 (integer-or-marker-p end-point))
547 ;; Go to START-POINT first, so that when we move to END-POINT,
548 ;; if it's in the middle of intangible text,
549 ;; point jumps in the direction away from START-POINT.
550 (goto-char start-point)
551 (goto-char end-point)
552 (if (zerop (% click-count 3))
553 (setq end-of-range (point)))
554 (let ((range (mouse-start-end start-point (point) click-count)))
555 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
558 (let ((mouse-row (cdr (cdr (mouse-position)))))
559 (cond
560 ((null mouse-row))
561 ((< mouse-row top)
562 (mouse-scroll-subr start-window (- mouse-row top)
563 mouse-drag-overlay start-point)
564 ;; Without this, point tends to jump back to the starting
565 ;; position where the mouse button was pressed down.
566 (setq end-of-range (overlay-start mouse-drag-overlay)))
567 ((>= mouse-row bottom)
568 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
569 mouse-drag-overlay start-point)
570 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
571 (if (consp event)
572 (let ((fun (key-binding (vector (car event)))))
573 ;; Run the binding of the terminating up-event, if possible.
574 ;; In the case of a multiple click, it gives the wrong results,
575 ;; because it would fail to set up a region.
576 (if nil ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
577 ;; In this case, we can just let the up-event execute normally.
578 (let ((end (event-end event)))
579 ;; Set the position in the event before we replay it,
580 ;; because otherwise it may have a position in the wrong
581 ;; buffer.
582 (setcar (cdr end) end-of-range)
583 ;; Delete the overlay before calling the function,
584 ;; because delete-overlay increases buffer-modified-tick.
585 (delete-overlay mouse-drag-overlay)
586 (setq unread-command-events
587 (cons event unread-command-events)))
588 (if (not (= (overlay-start mouse-drag-overlay)
589 (overlay-end mouse-drag-overlay)))
590 (let* ((stop-point
591 (if (numberp (posn-point (event-end event)))
592 (posn-point (event-end event))
593 last-end-point))
594 ;; The end that comes from where we ended the drag.
595 ;; Point goes here.
596 (region-termination
597 (if (and stop-point (< stop-point start-point))
598 (overlay-start mouse-drag-overlay)
599 (overlay-end mouse-drag-overlay)))
600 ;; The end that comes from where we started the drag.
601 ;; Mark goes there.
602 (region-commencement
603 (- (+ (overlay-end mouse-drag-overlay)
604 (overlay-start mouse-drag-overlay))
605 region-termination))
606 last-command this-command)
607 (push-mark region-commencement t t)
608 (goto-char region-termination)
609 (copy-region-as-kill (point) (mark t))
610 (let ((buffer (current-buffer)))
611 (mouse-show-mark)
612 ;; mouse-show-mark can call read-event,
613 ;; and that means the Emacs server could switch buffers
614 ;; under us. If that happened,
615 ;; avoid trying to use the region.
616 (and (mark t) mark-active
617 (eq buffer (current-buffer))
618 (mouse-set-region-1))))
619 (goto-char (overlay-end mouse-drag-overlay))
620 (setq this-command 'mouse-set-point)
621 (delete-overlay mouse-drag-overlay))))
622 (delete-overlay mouse-drag-overlay)))))
624 ;; Commands to handle xterm-style multiple clicks.
626 (defun mouse-skip-word (dir)
627 "Skip over word, over whitespace, or over identical punctuation.
628 If DIR is positive skip forward; if negative, skip backward."
629 (let* ((char (following-char))
630 (syntax (char-to-string (char-syntax char))))
631 (cond ((or (string= syntax "w") (string= syntax " "))
632 (if (< dir 0)
633 (skip-syntax-backward syntax)
634 (skip-syntax-forward syntax)))
635 ((string= syntax "_")
636 (if (< dir 0)
637 (skip-syntax-backward "w_")
638 (skip-syntax-forward "w_")))
639 ((< dir 0)
640 (while (and (not (bobp)) (= (preceding-char) char))
641 (forward-char -1)))
643 (while (and (not (eobp)) (= (following-char) char))
644 (forward-char 1))))))
646 ;; Return a list of region bounds based on START and END according to MODE.
647 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
648 ;; If MODE is 1 then set point to start of word at (min START END),
649 ;; mark to end of word at (max START END).
650 ;; If MODE is 2 then do the same for lines.
651 (defun mouse-start-end (start end mode)
652 (if (> start end)
653 (let ((temp start))
654 (setq start end
655 end temp)))
656 (setq mode (mod mode 3))
657 (cond ((= mode 0)
658 (list start end))
659 ((and (= mode 1)
660 (= start end)
661 (char-after start)
662 (= (char-syntax (char-after start)) ?\())
663 (list start
664 (save-excursion
665 (goto-char start)
666 (forward-sexp 1)
667 (point))))
668 ((and (= mode 1)
669 (= start end)
670 (char-after start)
671 (= (char-syntax (char-after start)) ?\)))
672 (list (save-excursion
673 (goto-char (1+ start))
674 (backward-sexp 1)
675 (point))
676 (1+ start)))
677 ((and (= mode 1)
678 (= start end)
679 (char-after start)
680 (= (char-syntax (char-after start)) ?\"))
681 (let ((open (or (eq start (point-min))
682 (save-excursion
683 (goto-char (- start 1))
684 (looking-at "\\s(\\|\\s \\|\\s>")))))
685 (if open
686 (list start
687 (save-excursion
688 (condition-case nil
689 (progn
690 (goto-char start)
691 (forward-sexp 1)
692 (point))
693 (error end))))
694 (list (save-excursion
695 (condition-case nil
696 (progn
697 (goto-char (1+ start))
698 (backward-sexp 1)
699 (point))
700 (error end)))
701 (1+ start)))))
702 ((= mode 1)
703 (list (save-excursion
704 (goto-char start)
705 (mouse-skip-word -1)
706 (point))
707 (save-excursion
708 (goto-char end)
709 (mouse-skip-word 1)
710 (point))))
711 ((= mode 2)
712 (list (save-excursion
713 (goto-char start)
714 (beginning-of-line 1)
715 (point))
716 (save-excursion
717 (goto-char end)
718 (forward-line 1)
719 (point))))))
721 ;; Subroutine: set the mark where CLICK happened,
722 ;; but don't do anything else.
723 (defun mouse-set-mark-fast (click)
724 (mouse-minibuffer-check click)
725 (let ((posn (event-start click)))
726 (select-window (posn-window posn))
727 (if (numberp (posn-point posn))
728 (push-mark (posn-point posn) t t))))
730 (defun mouse-undouble-last-event (events)
731 (let* ((index (1- (length events)))
732 (last (nthcdr index events))
733 (event (car last))
734 (basic (event-basic-type event))
735 (old-modifiers (event-modifiers event))
736 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
737 (new
738 (if (consp event)
739 (cons (event-convert-list (nreverse (cons basic old-modifiers)))
740 (cdr event))
741 event)))
742 (setcar last new)
743 (if (and (not (equal modifiers old-modifiers))
744 (key-binding (apply 'vector events)))
746 (setcar last event)
747 nil)))
749 ;; Momentarily show where the mark is, if highlighting doesn't show it.
751 (defvar mouse-region-delete-keys '([delete])
752 "List of keys which shall cause the mouse region to be deleted.")
754 (defun mouse-show-mark ()
755 (if transient-mark-mode
756 (if window-system
757 (delete-overlay mouse-drag-overlay))
758 (if window-system
759 (let ((inhibit-quit t)
760 (echo-keystrokes 0)
761 event events key ignore
762 x-lost-selection-hooks)
763 (add-hook 'x-lost-selection-hooks
764 '(lambda (seltype)
765 (if (eq seltype 'PRIMARY)
766 (progn (setq ignore t)
767 (throw 'mouse-show-mark t)))))
768 (move-overlay mouse-drag-overlay (point) (mark t))
769 (catch 'mouse-show-mark
770 ;; In this loop, read and execute scroll bar events.
771 ;; Otherwise, if we
772 (while (progn (setq event (read-event))
773 (setq events (append events (list event)))
774 (setq key (apply 'vector events))
775 (or (and (consp event)
776 (eq (posn-point (event-end event))
777 'vertical-scroll-bar))
778 (and (memq 'down (event-modifiers event))
779 (not (key-binding key))
780 (not (mouse-undouble-last-event events))
781 (not (member key mouse-region-delete-keys)))))
782 (and (consp event)
783 (eq (posn-point (event-end event))
784 'vertical-scroll-bar)
785 (let ((keys (vector 'vertical-scroll-bar event)))
786 (and (key-binding keys)
787 (progn
788 (call-interactively (key-binding keys)
789 nil keys)
790 (setq events nil)))))))
791 ;; If we lost the selection, just turn off the highlighting.
792 (if ignore
794 ;; For certain special keys, delete the region.
795 (if (member key mouse-region-delete-keys)
796 (delete-region (overlay-start mouse-drag-overlay)
797 (overlay-end mouse-drag-overlay))
798 ;; Otherwise, unread the key so it gets executed normally.
799 (setq unread-command-events
800 (nconc events unread-command-events))))
801 (setq quit-flag nil)
802 (delete-overlay mouse-drag-overlay))
803 (save-excursion
804 (goto-char (mark t))
805 (sit-for 1)))))
807 (defun mouse-set-mark (click)
808 "Set mark at the position clicked on with the mouse.
809 Display cursor at that position for a second.
810 This must be bound to a mouse click."
811 (interactive "e")
812 (mouse-minibuffer-check click)
813 (select-window (posn-window (event-start click)))
814 ;; We don't use save-excursion because that preserves the mark too.
815 (let ((point-save (point)))
816 (unwind-protect
817 (progn (mouse-set-point click)
818 (push-mark nil t t)
819 (or transient-mark-mode
820 (sit-for 1)))
821 (goto-char point-save))))
823 (defun mouse-kill (click)
824 "Kill the region between point and the mouse click.
825 The text is saved in the kill ring, as with \\[kill-region]."
826 (interactive "e")
827 (mouse-minibuffer-check click)
828 (let* ((posn (event-start click))
829 (click-posn (posn-point posn)))
830 (select-window (posn-window posn))
831 (if (numberp click-posn)
832 (kill-region (min (point) click-posn)
833 (max (point) click-posn)))))
835 (defun mouse-yank-at-click (click arg)
836 "Insert the last stretch of killed text at the position clicked on.
837 Also move point to one end of the text thus inserted (normally the end).
838 Prefix arguments are interpreted as with \\[yank].
839 If `mouse-yank-at-point' is non-nil, insert at point
840 regardless of where you click."
841 (interactive "e\nP")
842 ;; Give temporary modes such as isearch a chance to turn off.
843 (run-hooks 'mouse-leave-buffer-hook)
844 (or mouse-yank-at-point (mouse-set-point click))
845 (setq this-command 'yank)
846 (setq mouse-selection-click-count 0)
847 (yank arg))
849 (defun mouse-kill-ring-save (click)
850 "Copy the region between point and the mouse click in the kill ring.
851 This does not delete the region; it acts like \\[kill-ring-save]."
852 (interactive "e")
853 (mouse-set-mark-fast click)
854 (let (this-command last-command)
855 (kill-ring-save (point) (mark t)))
856 (mouse-show-mark))
858 ;;; This function used to delete the text between point and the mouse
859 ;;; whenever it was equal to the front of the kill ring, but some
860 ;;; people found that confusing.
862 ;;; A list (TEXT START END), describing the text and position of the last
863 ;;; invocation of mouse-save-then-kill.
864 (defvar mouse-save-then-kill-posn nil)
866 (defun mouse-save-then-kill-delete-region (beg end)
867 ;; We must make our own undo boundaries
868 ;; because they happen automatically only for the current buffer.
869 (undo-boundary)
870 (if (or (= beg end) (eq buffer-undo-list t))
871 ;; If we have no undo list in this buffer,
872 ;; just delete.
873 (delete-region beg end)
874 ;; Delete, but make the undo-list entry share with the kill ring.
875 ;; First, delete just one char, so in case buffer is being modified
876 ;; for the first time, the undo list records that fact.
877 (let (before-change-function after-change-function
878 before-change-functions after-change-functions)
879 (delete-region beg
880 (+ beg (if (> end beg) 1 -1))))
881 (let ((buffer-undo-list buffer-undo-list))
882 ;; Undo that deletion--but don't change the undo list!
883 (let (before-change-function after-change-function
884 before-change-functions after-change-functions)
885 (primitive-undo 1 buffer-undo-list))
886 ;; Now delete the rest of the specified region,
887 ;; but don't record it.
888 (setq buffer-undo-list t)
889 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
890 (error "Lossage in mouse-save-then-kill-delete-region"))
891 (delete-region beg end))
892 (let ((tail buffer-undo-list))
893 ;; Search back in buffer-undo-list for the string
894 ;; that came from deleting one character.
895 (while (and tail (not (stringp (car (car tail)))))
896 (setq tail (cdr tail)))
897 ;; Replace it with an entry for the entire deleted text.
898 (and tail
899 (setcar tail (cons (car kill-ring) (min beg end))))))
900 (undo-boundary))
902 (defun mouse-save-then-kill (click)
903 "Save text to point in kill ring; the second time, kill the text.
904 If the text between point and the mouse is the same as what's
905 at the front of the kill ring, this deletes the text.
906 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
907 which prepares for a second click to delete the text.
909 If you have selected words or lines, this command extends the
910 selection through the word or line clicked on. If you do this
911 again in a different position, it extends the selection again.
912 If you do this twice in the same position, the selection is killed."
913 (interactive "e")
914 (let ((before-scroll point-before-scroll))
915 (mouse-minibuffer-check click)
916 (let ((click-posn (posn-point (event-start click)))
917 ;; Don't let a subsequent kill command append to this one:
918 ;; prevent setting this-command to kill-region.
919 (this-command this-command))
920 (if (and (save-excursion
921 (set-buffer (window-buffer (posn-window (event-start click))))
922 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
923 ;; Don't be fooled by a recent click in some other buffer.
924 (eq mouse-selection-click-count-buffer
925 (current-buffer)))))
926 (if (not (and (eq last-command 'mouse-save-then-kill)
927 (equal click-posn
928 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
929 ;; Find both ends of the object selected by this click.
930 (let* ((range
931 (mouse-start-end click-posn click-posn
932 mouse-selection-click-count)))
933 ;; Move whichever end is closer to the click.
934 ;; That's what xterm does, and it seems reasonable.
935 (if (< (abs (- click-posn (mark t)))
936 (abs (- click-posn (point))))
937 (set-mark (car range))
938 (goto-char (nth 1 range)))
939 ;; We have already put the old region in the kill ring.
940 ;; Replace it with the extended region.
941 ;; (It would be annoying to make a separate entry.)
942 (kill-new (buffer-substring (point) (mark t)) t)
943 (mouse-set-region-1)
944 ;; Arrange for a repeated mouse-3 to kill this region.
945 (setq mouse-save-then-kill-posn
946 (list (car kill-ring) (point) click-posn))
947 (mouse-show-mark))
948 ;; If we click this button again without moving it,
949 ;; that time kill.
950 (mouse-save-then-kill-delete-region (mark) (point))
951 (setq mouse-selection-click-count 0)
952 (setq mouse-save-then-kill-posn nil))
953 (if (and (eq last-command 'mouse-save-then-kill)
954 mouse-save-then-kill-posn
955 (eq (car mouse-save-then-kill-posn) (car kill-ring))
956 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
957 ;; If this is the second time we've called
958 ;; mouse-save-then-kill, delete the text from the buffer.
959 (progn
960 (mouse-save-then-kill-delete-region (point) (mark))
961 ;; After we kill, another click counts as "the first time".
962 (setq mouse-save-then-kill-posn nil))
963 ;; This is not a repetition.
964 ;; We are adjusting an old selection or creating a new one.
965 (if (or (and (eq last-command 'mouse-save-then-kill)
966 mouse-save-then-kill-posn)
967 (and mark-active transient-mark-mode)
968 (and (memq last-command
969 '(mouse-drag-region mouse-set-region))
970 (or mark-even-if-inactive
971 (not transient-mark-mode))))
972 ;; We have a selection or suitable region, so adjust it.
973 (let* ((posn (event-start click))
974 (new (posn-point posn)))
975 (select-window (posn-window posn))
976 (if (numberp new)
977 (progn
978 ;; Move whichever end of the region is closer to the click.
979 ;; That is what xterm does, and it seems reasonable.
980 (if (< (abs (- new (point))) (abs (- new (mark t))))
981 (goto-char new)
982 (set-mark new))
983 (setq deactivate-mark nil)))
984 (kill-new (buffer-substring (point) (mark t)) t)
985 (mouse-show-mark))
986 ;; Set the mark where point is, then move where clicked.
987 (mouse-set-mark-fast click)
988 (if before-scroll
989 (goto-char before-scroll))
990 (exchange-point-and-mark)
991 (kill-new (buffer-substring (point) (mark t)))
992 (if window-system
993 (mouse-show-mark)))
994 (mouse-set-region-1)
995 (setq mouse-save-then-kill-posn
996 (list (car kill-ring) (point) click-posn)))))))
998 (global-set-key [M-mouse-1] 'mouse-start-secondary)
999 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1000 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1001 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1002 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1004 ;; An overlay which records the current secondary selection
1005 ;; or else is deleted when there is no secondary selection.
1006 ;; May be nil.
1007 (defvar mouse-secondary-overlay nil)
1009 (defvar mouse-secondary-click-count 0)
1011 ;; A marker which records the specified first end for a secondary selection.
1012 ;; May be nil.
1013 (defvar mouse-secondary-start nil)
1015 (defun mouse-start-secondary (click)
1016 "Set one end of the secondary selection to the position clicked on.
1017 Use \\[mouse-secondary-save-then-kill] to set the other end
1018 and complete the secondary selection."
1019 (interactive "e")
1020 (mouse-minibuffer-check click)
1021 (let ((posn (event-start click)))
1022 (save-excursion
1023 (set-buffer (window-buffer (posn-window posn)))
1024 ;; Cancel any preexisting secondary selection.
1025 (if mouse-secondary-overlay
1026 (delete-overlay mouse-secondary-overlay))
1027 (if (numberp (posn-point posn))
1028 (progn
1029 (or mouse-secondary-start
1030 (setq mouse-secondary-start (make-marker)))
1031 (move-marker mouse-secondary-start (posn-point posn)))))))
1033 (defun mouse-set-secondary (click)
1034 "Set the secondary selection to the text that the mouse is dragged over.
1035 This must be bound to a mouse drag event."
1036 (interactive "e")
1037 (mouse-minibuffer-check click)
1038 (let ((posn (event-start click))
1040 (end (event-end click)))
1041 (save-excursion
1042 (set-buffer (window-buffer (posn-window posn)))
1043 (if (numberp (posn-point posn))
1044 (setq beg (posn-point posn)))
1045 (if mouse-secondary-overlay
1046 (move-overlay mouse-secondary-overlay beg (posn-point end))
1047 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1048 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1050 (defun mouse-drag-secondary (start-event)
1051 "Set the secondary selection to the text that the mouse is dragged over.
1052 Highlight the drag area as you move the mouse.
1053 This must be bound to a button-down mouse event.
1054 The function returns a non-nil value if it creates a secondary selection."
1055 (interactive "e")
1056 (mouse-minibuffer-check start-event)
1057 (let* ((echo-keystrokes 0)
1058 (start-posn (event-start start-event))
1059 (start-point (posn-point start-posn))
1060 (start-window (posn-window start-posn))
1061 (start-frame (window-frame start-window))
1062 (bounds (window-edges start-window))
1063 (top (nth 1 bounds))
1064 (bottom (if (window-minibuffer-p start-window)
1065 (nth 3 bounds)
1066 ;; Don't count the mode line.
1067 (1- (nth 3 bounds))))
1068 (click-count (1- (event-click-count start-event))))
1069 (save-excursion
1070 (set-buffer (window-buffer start-window))
1071 (setq mouse-secondary-click-count click-count)
1072 (or mouse-secondary-overlay
1073 (setq mouse-secondary-overlay
1074 (make-overlay (point) (point))))
1075 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1076 (if (> (mod click-count 3) 0)
1077 ;; Double or triple press: make an initial selection
1078 ;; of one word or line.
1079 (let ((range (mouse-start-end start-point start-point click-count)))
1080 (set-marker mouse-secondary-start nil)
1081 (move-overlay mouse-secondary-overlay 1 1
1082 (window-buffer start-window))
1083 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1084 (window-buffer start-window)))
1085 ;; Single-press: cancel any preexisting secondary selection.
1086 (or mouse-secondary-start
1087 (setq mouse-secondary-start (make-marker)))
1088 (set-marker mouse-secondary-start start-point)
1089 (delete-overlay mouse-secondary-overlay))
1090 (let (event end end-point)
1091 (track-mouse
1092 (while (progn
1093 (setq event (read-event))
1094 (or (mouse-movement-p event)
1095 (eq (car-safe event) 'switch-frame)))
1097 (if (eq (car-safe event) 'switch-frame)
1099 (setq end (event-end event)
1100 end-point (posn-point end))
1101 (cond
1102 ;; Are we moving within the original window?
1103 ((and (eq (posn-window end) start-window)
1104 (integer-or-marker-p end-point))
1105 (let ((range (mouse-start-end start-point end-point
1106 click-count)))
1107 (if (or (/= start-point end-point)
1108 (null (marker-position mouse-secondary-start)))
1109 (progn
1110 (set-marker mouse-secondary-start nil)
1111 (move-overlay mouse-secondary-overlay
1112 (car range) (nth 1 range))))))
1114 (let ((mouse-row (cdr (cdr (mouse-position)))))
1115 (cond
1116 ((null mouse-row))
1117 ((< mouse-row top)
1118 (mouse-scroll-subr start-window (- mouse-row top)
1119 mouse-secondary-overlay start-point))
1120 ((>= mouse-row bottom)
1121 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1122 mouse-secondary-overlay start-point)))))))))
1124 (if (consp event)
1125 (if (marker-position mouse-secondary-start)
1126 (save-window-excursion
1127 (delete-overlay mouse-secondary-overlay)
1128 (x-set-selection 'SECONDARY nil)
1129 (select-window start-window)
1130 (save-excursion
1131 (goto-char mouse-secondary-start)
1132 (sit-for 1)
1133 nil))
1134 (x-set-selection
1135 'SECONDARY
1136 (buffer-substring (overlay-start mouse-secondary-overlay)
1137 (overlay-end mouse-secondary-overlay)))))))))
1139 (defun mouse-yank-secondary (click)
1140 "Insert the secondary selection at the position clicked on.
1141 Move point to the end of the inserted text.
1142 If `mouse-yank-at-point' is non-nil, insert at point
1143 regardless of where you click."
1144 (interactive "e")
1145 ;; Give temporary modes such as isearch a chance to turn off.
1146 (run-hooks 'mouse-leave-buffer-hook)
1147 (or mouse-yank-at-point (mouse-set-point click))
1148 (insert (x-get-selection 'SECONDARY)))
1150 (defun mouse-kill-secondary ()
1151 "Kill the text in the secondary selection.
1152 This is intended more as a keyboard command than as a mouse command
1153 but it can work as either one.
1155 The current buffer (in case of keyboard use), or the buffer clicked on,
1156 must be the one that the secondary selection is in. This requirement
1157 is to prevent accidents."
1158 (interactive)
1159 (let* ((keys (this-command-keys))
1160 (click (elt keys (1- (length keys)))))
1161 (or (eq (overlay-buffer mouse-secondary-overlay)
1162 (if (listp click)
1163 (window-buffer (posn-window (event-start click)))
1164 (current-buffer)))
1165 (error "Select or click on the buffer where the secondary selection is")))
1166 (let (this-command)
1167 (save-excursion
1168 (set-buffer (overlay-buffer mouse-secondary-overlay))
1169 (kill-region (overlay-start mouse-secondary-overlay)
1170 (overlay-end mouse-secondary-overlay))))
1171 (delete-overlay mouse-secondary-overlay)
1172 ;;; (x-set-selection 'SECONDARY nil)
1173 (setq mouse-secondary-overlay nil))
1175 (defun mouse-secondary-save-then-kill (click)
1176 "Save text to point in kill ring; the second time, kill the text.
1177 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1178 If the text between where you did \\[mouse-start-secondary] and where
1179 you use this command matches the text at the front of the kill ring,
1180 this command deletes the text.
1181 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1182 which prepares for a second click with this command to delete the text.
1184 If you have already made a secondary selection in that buffer,
1185 this command extends or retracts the selection to where you click.
1186 If you do this again in a different position, it extends or retracts
1187 again. If you do this twice in the same position, it kills the selection."
1188 (interactive "e")
1189 (mouse-minibuffer-check click)
1190 (let ((posn (event-start click))
1191 (click-posn (posn-point (event-start click)))
1192 ;; Don't let a subsequent kill command append to this one:
1193 ;; prevent setting this-command to kill-region.
1194 (this-command this-command))
1195 (or (eq (window-buffer (posn-window posn))
1196 (or (and mouse-secondary-overlay
1197 (overlay-buffer mouse-secondary-overlay))
1198 (if mouse-secondary-start
1199 (marker-buffer mouse-secondary-start))))
1200 (error "Wrong buffer"))
1201 (save-excursion
1202 (set-buffer (window-buffer (posn-window posn)))
1203 (if (> (mod mouse-secondary-click-count 3) 0)
1204 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1205 (equal click-posn
1206 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1207 ;; Find both ends of the object selected by this click.
1208 (let* ((range
1209 (mouse-start-end click-posn click-posn
1210 mouse-secondary-click-count)))
1211 ;; Move whichever end is closer to the click.
1212 ;; That's what xterm does, and it seems reasonable.
1213 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1214 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1215 (move-overlay mouse-secondary-overlay (car range)
1216 (overlay-end mouse-secondary-overlay))
1217 (move-overlay mouse-secondary-overlay
1218 (overlay-start mouse-secondary-overlay)
1219 (nth 1 range)))
1220 ;; We have already put the old region in the kill ring.
1221 ;; Replace it with the extended region.
1222 ;; (It would be annoying to make a separate entry.)
1223 (kill-new (buffer-substring
1224 (overlay-start mouse-secondary-overlay)
1225 (overlay-end mouse-secondary-overlay)) t)
1226 ;; Arrange for a repeated mouse-3 to kill this region.
1227 (setq mouse-save-then-kill-posn
1228 (list (car kill-ring) (point) click-posn)))
1229 ;; If we click this button again without moving it,
1230 ;; that time kill.
1231 (progn
1232 (mouse-save-then-kill-delete-region
1233 (overlay-start mouse-secondary-overlay)
1234 (overlay-end mouse-secondary-overlay))
1235 (setq mouse-save-then-kill-posn nil)
1236 (setq mouse-secondary-click-count 0)
1237 (delete-overlay mouse-secondary-overlay)))
1238 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1239 mouse-save-then-kill-posn
1240 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1241 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1242 ;; If this is the second time we've called
1243 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1244 (progn
1245 (mouse-save-then-kill-delete-region
1246 (overlay-start mouse-secondary-overlay)
1247 (overlay-end mouse-secondary-overlay))
1248 (setq mouse-save-then-kill-posn nil)
1249 (delete-overlay mouse-secondary-overlay))
1250 (if (overlay-start mouse-secondary-overlay)
1251 ;; We have a selection, so adjust it.
1252 (progn
1253 (if (numberp click-posn)
1254 (progn
1255 ;; Move whichever end of the region is closer to the click.
1256 ;; That is what xterm does, and it seems reasonable.
1257 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1258 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1259 (move-overlay mouse-secondary-overlay click-posn
1260 (overlay-end mouse-secondary-overlay))
1261 (move-overlay mouse-secondary-overlay
1262 (overlay-start mouse-secondary-overlay)
1263 click-posn))
1264 (setq deactivate-mark nil)))
1265 (if (eq last-command 'mouse-secondary-save-then-kill)
1266 ;; If the front of the kill ring comes from
1267 ;; an immediately previous use of this command,
1268 ;; replace it with the extended region.
1269 ;; (It would be annoying to make a separate entry.)
1270 (kill-new (buffer-substring
1271 (overlay-start mouse-secondary-overlay)
1272 (overlay-end mouse-secondary-overlay)) t)
1273 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1274 (overlay-end mouse-secondary-overlay))))
1275 (if mouse-secondary-start
1276 ;; All we have is one end of a selection,
1277 ;; so put the other end here.
1278 (let ((start (+ 0 mouse-secondary-start)))
1279 (kill-ring-save start click-posn)
1280 (if mouse-secondary-overlay
1281 (move-overlay mouse-secondary-overlay start click-posn)
1282 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1283 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1284 (setq mouse-save-then-kill-posn
1285 (list (car kill-ring) (point) click-posn))))
1286 (if (overlay-buffer mouse-secondary-overlay)
1287 (x-set-selection 'SECONDARY
1288 (buffer-substring
1289 (overlay-start mouse-secondary-overlay)
1290 (overlay-end mouse-secondary-overlay)))))))
1292 (defcustom mouse-menu-buffer-maxlen 20
1293 "*Number of buffers in one pane (submenu) of the buffer menu.
1294 If we have lots of buffers, divide them into groups of
1295 `mouse-menu-buffer-maxlen' and make a pane (or submenu) for each one."
1296 :type 'integer
1297 :group 'mouse)
1299 (defvar mouse-buffer-menu-mode-groups
1300 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1301 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1302 . "Mail/News")
1303 ("\\<C\\>" . "C")
1304 ("ObjC" . "C")
1305 ("Text" . "Text")
1306 ("Outline" . "Text")
1307 ("Lisp" . "Lisp"))
1308 "How to group various major modes together in \\[mouse-buffer-menu].
1309 Each element has the form (REGEXP . GROUPNAME).
1310 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1312 (defun mouse-buffer-menu (event)
1313 "Pop up a menu of buffers for selection with the mouse.
1314 This switches buffers in the window that you clicked on,
1315 and selects that window."
1316 (interactive "e")
1317 (mouse-minibuffer-check event)
1318 (let (buffers alist menu split-by-major-mode sum-of-squares)
1319 (setq buffers (buffer-list))
1320 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1321 (let ((tail buffers))
1322 (while tail
1323 ;; Divide all buffers into buckets for various major modes.
1324 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1325 (with-current-buffer (car tail)
1326 (let* ((adjusted-major-mode major-mode) elt)
1327 (let ((tail mouse-buffer-menu-mode-groups))
1328 (while tail
1329 (if (string-match (car (car tail)) mode-name)
1330 (setq adjusted-major-mode (cdr (car tail))))
1331 (setq tail (cdr tail))))
1332 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1333 (if (null elt)
1334 (setq elt (list adjusted-major-mode
1335 (if (stringp adjusted-major-mode)
1336 adjusted-major-mode
1337 mode-name))
1338 split-by-major-mode (cons elt split-by-major-mode)))
1339 (or (memq (car tail) (cdr (cdr elt)))
1340 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1341 (setq tail (cdr tail))))
1342 ;; Compute the sum of squares of sizes of the major-mode buckets.
1343 (let ((tail split-by-major-mode))
1344 (setq sum-of-squares 0)
1345 (while tail
1346 (setq sum-of-squares
1347 (+ sum-of-squares
1348 (* (length (cdr (cdr (car tail))))
1349 (length (cdr (cdr (car tail)))))))
1350 (setq tail (cdr tail))))
1351 (if (< (* sum-of-squares 4) (* (length buffers) (length buffers)))
1352 ;; Subdividing by major modes really helps, so let's do it.
1353 (let (subdivided-menus (buffers-left (length buffers)))
1354 ;; Sort the list to put the most popular major modes first.
1355 (setq split-by-major-mode
1356 (sort split-by-major-mode
1357 (function (lambda (elt1 elt2)
1358 (> (length elt1) (length elt2))))))
1359 ;; Make a separate submenu for each major mode
1360 ;; that has more than one buffer,
1361 ;; unless all the remaining buffers are less than 1/10 of them.
1362 (while (and split-by-major-mode
1363 (and (> (length (car split-by-major-mode)) 3)
1364 (> (* buffers-left 10) (length buffers))))
1365 (setq subdivided-menus
1366 (cons (cons
1367 (nth 1 (car split-by-major-mode))
1368 (mouse-buffer-menu-alist
1369 (cdr (cdr (car split-by-major-mode)))))
1370 subdivided-menus))
1371 (setq buffers-left
1372 (- buffers-left (length (cdr (car split-by-major-mode)))))
1373 (setq split-by-major-mode (cdr split-by-major-mode)))
1374 ;; If any major modes are left over,
1375 ;; make a single submenu for them.
1376 (if split-by-major-mode
1377 (setq subdivided-menus
1378 (cons (cons
1379 "Others"
1380 (mouse-buffer-menu-alist
1381 (apply 'append
1382 (mapcar 'cdr
1383 (mapcar 'cdr split-by-major-mode)))))
1384 subdivided-menus)))
1385 (setq subdivided-menus
1386 (nreverse subdivided-menus))
1387 (setq menu (cons "Buffer Menu" subdivided-menus)))
1388 (progn
1389 (setq alist (mouse-buffer-menu-alist buffers))
1390 (setq menu (cons "Buffer Menu"
1391 (mouse-buffer-menu-split "Select Buffer" alist)))))
1392 (let ((buf (x-popup-menu event menu))
1393 (window (posn-window (event-start event))))
1394 (if buf
1395 (progn
1396 (or (framep window) (select-window window))
1397 (switch-to-buffer buf))))))
1399 (defun mouse-buffer-menu-alist (buffers)
1400 (let (tail
1401 (maxlen 0)
1402 head)
1403 (setq buffers
1404 (sort buffers
1405 (function (lambda (elt1 elt2)
1406 (string< (buffer-name elt1) (buffer-name elt2))))))
1407 (setq tail buffers)
1408 (while tail
1409 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1410 (setq maxlen
1411 (max maxlen
1412 (length (buffer-name (car tail))))))
1413 (setq tail (cdr tail)))
1414 (setq tail buffers)
1415 (while tail
1416 (let ((elt (car tail)))
1417 (if (/= (aref (buffer-name elt) 0) ?\ )
1418 (setq head
1419 (cons
1420 (cons
1421 (format
1422 (format "%%%ds %%s%%s %%s" maxlen)
1423 (buffer-name elt)
1424 (if (buffer-modified-p elt) "*" " ")
1425 (save-excursion
1426 (set-buffer elt)
1427 (if buffer-read-only "%" " "))
1428 (or (buffer-file-name elt)
1429 (save-excursion
1430 (set-buffer elt)
1431 (if list-buffers-directory
1432 (expand-file-name
1433 list-buffers-directory)))
1434 ""))
1435 elt)
1436 head))))
1437 (setq tail (cdr tail)))
1438 ;; Compensate for the reversal that the above loop does.
1439 (nreverse head)))
1441 (defun mouse-buffer-menu-split (title alist)
1442 ;; If we have lots of buffers, divide them into groups of 20
1443 ;; and make a pane (or submenu) for each one.
1444 (if (> (length alist) (/ (* mouse-menu-buffer-maxlen 3) 2))
1445 (let ((alist alist) sublists next
1446 (i 1))
1447 (while alist
1448 ;; Pull off the next mouse-menu-buffer-maxlen buffers
1449 ;; and make them the next element of sublist.
1450 (setq next (nthcdr mouse-menu-buffer-maxlen alist))
1451 (if next
1452 (setcdr (nthcdr (1- mouse-menu-buffer-maxlen) alist)
1453 nil))
1454 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1455 sublists))
1456 (setq i (1+ i))
1457 (setq alist next))
1458 (nreverse sublists))
1459 ;; Few buffers--put them all in one pane.
1460 (list (cons title alist))))
1462 ;;; These need to be rewritten for the new scroll bar implementation.
1464 ;;;!! ;; Commands for the scroll bar.
1465 ;;;!!
1466 ;;;!! (defun mouse-scroll-down (click)
1467 ;;;!! (interactive "@e")
1468 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1469 ;;;!!
1470 ;;;!! (defun mouse-scroll-up (click)
1471 ;;;!! (interactive "@e")
1472 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1473 ;;;!!
1474 ;;;!! (defun mouse-scroll-down-full ()
1475 ;;;!! (interactive "@")
1476 ;;;!! (scroll-down nil))
1477 ;;;!!
1478 ;;;!! (defun mouse-scroll-up-full ()
1479 ;;;!! (interactive "@")
1480 ;;;!! (scroll-up nil))
1481 ;;;!!
1482 ;;;!! (defun mouse-scroll-move-cursor (click)
1483 ;;;!! (interactive "@e")
1484 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1485 ;;;!!
1486 ;;;!! (defun mouse-scroll-absolute (event)
1487 ;;;!! (interactive "@e")
1488 ;;;!! (let* ((pos (car event))
1489 ;;;!! (position (car pos))
1490 ;;;!! (length (car (cdr pos))))
1491 ;;;!! (if (<= length 0) (setq length 1))
1492 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1493 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1494 ;;;!! position)
1495 ;;;!! length)
1496 ;;;!! scale-factor)))
1497 ;;;!! (goto-char newpos)
1498 ;;;!! (recenter '(4)))))
1499 ;;;!!
1500 ;;;!! (defun mouse-scroll-left (click)
1501 ;;;!! (interactive "@e")
1502 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1503 ;;;!!
1504 ;;;!! (defun mouse-scroll-right (click)
1505 ;;;!! (interactive "@e")
1506 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1507 ;;;!!
1508 ;;;!! (defun mouse-scroll-left-full ()
1509 ;;;!! (interactive "@")
1510 ;;;!! (scroll-left nil))
1511 ;;;!!
1512 ;;;!! (defun mouse-scroll-right-full ()
1513 ;;;!! (interactive "@")
1514 ;;;!! (scroll-right nil))
1515 ;;;!!
1516 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1517 ;;;!! (interactive "@e")
1518 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1519 ;;;!!
1520 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1521 ;;;!! (interactive "@e")
1522 ;;;!! (let* ((pos (car event))
1523 ;;;!! (position (car pos))
1524 ;;;!! (length (car (cdr pos))))
1525 ;;;!! (set-window-hscroll (selected-window) 33)))
1526 ;;;!!
1527 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1528 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1529 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1530 ;;;!!
1531 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1532 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1533 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1534 ;;;!!
1535 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1536 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1537 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1538 ;;;!!
1539 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1540 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1541 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1542 ;;;!!
1543 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1544 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1545 ;;;!! 'mouse-scroll-absolute-horizontally)
1546 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1547 ;;;!!
1548 ;;;!! (global-set-key [horizontal-slider mouse-1]
1549 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1550 ;;;!! (global-set-key [horizontal-slider mouse-2]
1551 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1552 ;;;!! (global-set-key [horizontal-slider mouse-3]
1553 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1554 ;;;!!
1555 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1556 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1557 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1558 ;;;!!
1559 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1560 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1561 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1562 ;;;!!
1563 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1564 ;;;!! 'mouse-split-window-horizontally)
1565 ;;;!! (global-set-key [mode-line S-mouse-2]
1566 ;;;!! 'mouse-split-window-horizontally)
1567 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1568 ;;;!! 'mouse-split-window)
1570 ;;;!! ;;;;
1571 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1572 ;;;!! ;;;; are of the form:
1573 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1574 ;;;!! ;;
1575 ;;;!! ;;;;
1576 ;;;!! ;;;; Dynamically track mouse coordinates
1577 ;;;!! ;;;;
1578 ;;;!! ;;
1579 ;;;!! ;;(defun track-mouse (event)
1580 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1581 ;;;!! ;; (interactive "@e")
1582 ;;;!! ;; (while mouse-grabbed
1583 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1584 ;;;!! ;; (abs-x (car pos))
1585 ;;;!! ;; (abs-y (cdr pos))
1586 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1587 ;;;!! ;; (list (car pos) (cdr pos))
1588 ;;;!! ;; (selected-window))))
1589 ;;;!! ;; (if (consp relative-coordinate)
1590 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1591 ;;;!! ;; (car relative-coordinate)
1592 ;;;!! ;; (car (cdr relative-coordinate)))
1593 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1594 ;;;!!
1595 ;;;!! ;;
1596 ;;;!! ;; Dynamically put a box around the line indicated by point
1597 ;;;!! ;;
1598 ;;;!! ;;
1599 ;;;!! ;;(require 'backquote)
1600 ;;;!! ;;
1601 ;;;!! ;;(defun mouse-select-buffer-line (event)
1602 ;;;!! ;; (interactive "@e")
1603 ;;;!! ;; (let ((relative-coordinate
1604 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1605 ;;;!! ;; (abs-y (car (cdr (car event)))))
1606 ;;;!! ;; (if (consp relative-coordinate)
1607 ;;;!! ;; (progn
1608 ;;;!! ;; (save-excursion
1609 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1610 ;;;!! ;; (x-draw-rectangle
1611 ;;;!! ;; (selected-screen)
1612 ;;;!! ;; abs-y 0
1613 ;;;!! ;; (save-excursion
1614 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1615 ;;;!! ;; (end-of-line)
1616 ;;;!! ;; (push-mark nil t)
1617 ;;;!! ;; (beginning-of-line)
1618 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1619 ;;;!! ;; (sit-for 1)
1620 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1621 ;;;!! ;;
1622 ;;;!! ;;(defvar last-line-drawn nil)
1623 ;;;!! ;;(defvar begin-delim "[^ \t]")
1624 ;;;!! ;;(defvar end-delim "[^ \t]")
1625 ;;;!! ;;
1626 ;;;!! ;;(defun mouse-boxing (event)
1627 ;;;!! ;; (interactive "@e")
1628 ;;;!! ;; (save-excursion
1629 ;;;!! ;; (let ((screen (selected-screen)))
1630 ;;;!! ;; (while (= (x-mouse-events) 0)
1631 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1632 ;;;!! ;; (abs-x (car pos))
1633 ;;;!! ;; (abs-y (cdr pos))
1634 ;;;!! ;; (relative-coordinate
1635 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1636 ;;;!! ;; (selected-window)))
1637 ;;;!! ;; (begin-reg nil)
1638 ;;;!! ;; (end-reg nil)
1639 ;;;!! ;; (end-column nil)
1640 ;;;!! ;; (begin-column nil))
1641 ;;;!! ;; (if (and (consp relative-coordinate)
1642 ;;;!! ;; (or (not last-line-drawn)
1643 ;;;!! ;; (not (= last-line-drawn abs-y))))
1644 ;;;!! ;; (progn
1645 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1646 ;;;!! ;; (if (= (following-char) 10)
1647 ;;;!! ;; ()
1648 ;;;!! ;; (progn
1649 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1650 ;;;!! ;; (setq begin-column (1- (current-column)))
1651 ;;;!! ;; (end-of-line)
1652 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1653 ;;;!! ;; (setq end-column (1+ (current-column)))
1654 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1655 ;;;!! ;; (x-draw-rectangle screen
1656 ;;;!! ;; (setq last-line-drawn abs-y)
1657 ;;;!! ;; begin-column
1658 ;;;!! ;; (- end-column begin-column) 1))))))))))
1659 ;;;!! ;;
1660 ;;;!! ;;(defun mouse-erase-box ()
1661 ;;;!! ;; (interactive)
1662 ;;;!! ;; (if last-line-drawn
1663 ;;;!! ;; (progn
1664 ;;;!! ;; (x-erase-rectangle (selected-screen))
1665 ;;;!! ;; (setq last-line-drawn nil))))
1666 ;;;!!
1667 ;;;!! ;;; (defun test-x-rectangle ()
1668 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1669 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1670 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1671 ;;;!!
1672 ;;;!! ;;
1673 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1674 ;;;!! ;;
1675 ;;;!!
1676 ;;;!! (defvar double-start nil)
1677 ;;;!! (defconst double-click-interval 300
1678 ;;;!! "Max ticks between clicks")
1679 ;;;!!
1680 ;;;!! (defun double-down (event)
1681 ;;;!! (interactive "@e")
1682 ;;;!! (if double-start
1683 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1684 ;;;!! (if (< interval double-click-interval)
1685 ;;;!! (progn
1686 ;;;!! (backward-up-list 1)
1687 ;;;!! ;; (message "Interval %d" interval)
1688 ;;;!! (sleep-for 1)))
1689 ;;;!! (setq double-start nil))
1690 ;;;!! (setq double-start (nth 4 event))))
1691 ;;;!!
1692 ;;;!! (defun double-up (event)
1693 ;;;!! (interactive "@e")
1694 ;;;!! (and double-start
1695 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1696 ;;;!! (setq double-start nil)))
1697 ;;;!!
1698 ;;;!! ;;; (defun x-test-doubleclick ()
1699 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1700 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1701 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1702 ;;;!!
1703 ;;;!! ;;
1704 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1705 ;;;!! ;;
1706 ;;;!!
1707 ;;;!! (defvar scrolled-lines 0)
1708 ;;;!! (defconst scroll-speed 1)
1709 ;;;!!
1710 ;;;!! (defun incr-scroll-down (event)
1711 ;;;!! (interactive "@e")
1712 ;;;!! (setq scrolled-lines 0)
1713 ;;;!! (incremental-scroll scroll-speed))
1714 ;;;!!
1715 ;;;!! (defun incr-scroll-up (event)
1716 ;;;!! (interactive "@e")
1717 ;;;!! (setq scrolled-lines 0)
1718 ;;;!! (incremental-scroll (- scroll-speed)))
1719 ;;;!!
1720 ;;;!! (defun incremental-scroll (n)
1721 ;;;!! (while (= (x-mouse-events) 0)
1722 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1723 ;;;!! (scroll-down n)
1724 ;;;!! (sit-for 300 t)))
1725 ;;;!!
1726 ;;;!! (defun incr-scroll-stop (event)
1727 ;;;!! (interactive "@e")
1728 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1729 ;;;!! (setq scrolled-lines 0)
1730 ;;;!! (sleep-for 1))
1731 ;;;!!
1732 ;;;!! ;;; (defun x-testing-scroll ()
1733 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1734 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1735 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1736 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1737 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1738 ;;;!!
1739 ;;;!! ;;
1740 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1741 ;;;!! ;;
1742 ;;;!!
1743 ;;;!! (defun mouse-kill-rectangle (event)
1744 ;;;!! "Kill the rectangle between point and the mouse cursor."
1745 ;;;!! (interactive "@e")
1746 ;;;!! (let ((point-save (point)))
1747 ;;;!! (save-excursion
1748 ;;;!! (mouse-set-point event)
1749 ;;;!! (push-mark nil t)
1750 ;;;!! (if (> point-save (point))
1751 ;;;!! (kill-rectangle (point) point-save)
1752 ;;;!! (kill-rectangle point-save (point))))))
1753 ;;;!!
1754 ;;;!! (defun mouse-open-rectangle (event)
1755 ;;;!! "Kill the rectangle between point and the mouse cursor."
1756 ;;;!! (interactive "@e")
1757 ;;;!! (let ((point-save (point)))
1758 ;;;!! (save-excursion
1759 ;;;!! (mouse-set-point event)
1760 ;;;!! (push-mark nil t)
1761 ;;;!! (if (> point-save (point))
1762 ;;;!! (open-rectangle (point) point-save)
1763 ;;;!! (open-rectangle point-save (point))))))
1764 ;;;!!
1765 ;;;!! ;; Must be a better way to do this.
1766 ;;;!!
1767 ;;;!! (defun mouse-multiple-insert (n char)
1768 ;;;!! (while (> n 0)
1769 ;;;!! (insert char)
1770 ;;;!! (setq n (1- n))))
1771 ;;;!!
1772 ;;;!! ;; What this could do is not finalize until button was released.
1773 ;;;!!
1774 ;;;!! (defun mouse-move-text (event)
1775 ;;;!! "Move text from point to cursor position, inserting spaces."
1776 ;;;!! (interactive "@e")
1777 ;;;!! (let* ((relative-coordinate
1778 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1779 ;;;!! (if (consp relative-coordinate)
1780 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1781 ;;;!! (delete-char
1782 ;;;!! (- (car relative-coordinate) (current-column))))
1783 ;;;!! ((< (current-column) (car relative-coordinate))
1784 ;;;!! (mouse-multiple-insert
1785 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1786 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1788 ;; Choose a completion with the mouse.
1790 (defun mouse-choose-completion (event)
1791 "Click on an alternative in the `*Completions*' buffer to choose it."
1792 (interactive "e")
1793 ;; Give temporary modes such as isearch a chance to turn off.
1794 (run-hooks 'mouse-leave-buffer-hook)
1795 (let ((buffer (window-buffer))
1796 choice
1797 base-size)
1798 (save-excursion
1799 (set-buffer (window-buffer (posn-window (event-start event))))
1800 (if completion-reference-buffer
1801 (setq buffer completion-reference-buffer))
1802 (setq base-size completion-base-size)
1803 (save-excursion
1804 (goto-char (posn-point (event-start event)))
1805 (let (beg end)
1806 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1807 (setq end (point) beg (1+ (point))))
1808 (if (null beg)
1809 (error "No completion here"))
1810 (setq beg (previous-single-property-change beg 'mouse-face))
1811 (setq end (or (next-single-property-change end 'mouse-face)
1812 (point-max)))
1813 (setq choice (buffer-substring beg end)))))
1814 (let ((owindow (selected-window)))
1815 (select-window (posn-window (event-start event)))
1816 (if (and (one-window-p t 'selected-frame)
1817 (window-dedicated-p (selected-window)))
1818 ;; This is a special buffer's frame
1819 (iconify-frame (selected-frame))
1820 (or (window-dedicated-p (selected-window))
1821 (bury-buffer)))
1822 (select-window owindow))
1823 (choose-completion-string choice buffer base-size)))
1825 ;; Font selection.
1827 (defun font-menu-add-default ()
1828 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1829 (font-alist x-fixed-font-alist)
1830 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1831 (if (assoc "Default" elt)
1832 (delete (assoc "Default" elt) elt))
1833 (setcdr elt
1834 (cons (list "Default"
1835 (cdr (assq 'font (frame-parameters (selected-frame)))))
1836 (cdr elt)))))
1838 (defvar x-fixed-font-alist
1839 '("Font menu"
1840 ("Misc"
1841 ;; For these, we specify the pixel height and width.
1842 ("fixed" "fixed")
1843 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1844 ("6x12"
1845 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1846 ("6x13"
1847 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1848 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1849 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1850 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1851 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1852 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1853 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1854 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1855 ("")
1856 ("clean 5x8"
1857 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1858 ("clean 6x8"
1859 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1860 ("clean 8x8"
1861 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1862 ("clean 8x10"
1863 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1864 ("clean 8x14"
1865 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1866 ("clean 8x16"
1867 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1868 ("")
1869 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
1870 ;;; We don't seem to have these; who knows what they are.
1871 ;;; ("fg-18" "fg-18")
1872 ;;; ("fg-25" "fg-25")
1873 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1874 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1875 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1876 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1877 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1878 ("Courier"
1879 ;; For these, we specify the point height.
1880 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1881 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1882 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1883 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1884 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1885 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1886 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1887 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1888 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1889 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1890 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1891 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1892 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1893 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1894 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1895 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1896 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1897 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1898 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1899 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1900 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1901 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1902 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1903 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
1905 "X fonts suitable for use in Emacs.")
1907 (defun mouse-set-font (&rest fonts)
1908 "Select an emacs font from a list of known good fonts and fontsets."
1909 (interactive
1910 (x-popup-menu
1911 last-nonmenu-event
1912 ;; Append list of fontsets currently defined.
1913 (append x-fixed-font-alist (list (generate-fontset-menu)))))
1914 (if fonts
1915 (let (font)
1916 (while fonts
1917 (condition-case nil
1918 (progn
1919 (set-default-font (car fonts))
1920 (setq font (car fonts))
1921 (setq fonts nil))
1922 (error
1923 (setq fonts (cdr fonts)))))
1924 (if (null font)
1925 (error "Font not found")))))
1927 ;;; Bindings for mouse commands.
1929 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1930 (global-set-key [mouse-1] 'mouse-set-point)
1931 (global-set-key [drag-mouse-1] 'mouse-set-region)
1933 ;; These are tested for in mouse-drag-region.
1934 (global-set-key [double-mouse-1] 'mouse-set-point)
1935 (global-set-key [triple-mouse-1] 'mouse-set-point)
1937 (global-set-key [mouse-2] 'mouse-yank-at-click)
1938 (global-set-key [mouse-3] 'mouse-save-then-kill)
1940 ;; By binding these to down-going events, we let the user use the up-going
1941 ;; event to make the selection, saving a click.
1942 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1943 (if (not (eq system-type 'ms-dos))
1944 (global-set-key [S-down-mouse-1] 'mouse-set-font))
1945 ;; C-down-mouse-2 is bound in facemenu.el.
1946 (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1949 ;; Replaced with dragging mouse-1
1950 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1952 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1953 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1954 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1955 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1956 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1957 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1958 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1959 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1960 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1961 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1963 (provide 'mouse)
1965 ;;; mouse.el ends here