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