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