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