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