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