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