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