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