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