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