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