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