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