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