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