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