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