*** empty log message ***
[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.
2f4b15ef
RS
956 (let (before-change-function after-change-function
957 before-change-functions after-change-functions)
958 (delete-region beg
959 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
960 (let ((buffer-undo-list buffer-undo-list))
961 ;; Undo that deletion--but don't change the undo list!
2f4b15ef
RS
962 (let (before-change-function after-change-function
963 before-change-functions after-change-functions)
964 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
965 ;; Now delete the rest of the specified region,
966 ;; but don't record it.
967 (setq buffer-undo-list t)
9a974c88
RS
968 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
969 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
970 (delete-region beg end))
971 (let ((tail buffer-undo-list))
972 ;; Search back in buffer-undo-list for the string
973 ;; that came from deleting one character.
974 (while (and tail (not (stringp (car (car tail)))))
975 (setq tail (cdr tail)))
976 ;; Replace it with an entry for the entire deleted text.
977 (and tail
9a974c88
RS
978 (setcar tail (cons (car kill-ring) (min beg end))))))
979 (undo-boundary))
eb6ff46f 980
947da0c4 981(defun mouse-save-then-kill (click)
40a45a9f
RS
982 "Save text to point in kill ring; the second time, kill the text.
983If the text between point and the mouse is the same as what's
984at the front of the kill ring, this deletes the text.
985Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
eb6ff46f
RS
986which prepares for a second click to delete the text.
987
988If you have selected words or lines, this command extends the
989selection through the word or line clicked on. If you do this
990again in a different position, it extends the selection again.
991If you do this twice in the same position, the selection is killed."
947da0c4 992 (interactive "e")
53016bea
RS
993 (let ((before-scroll
994 (with-current-buffer (window-buffer (posn-window (event-start click)))
995 point-before-scroll)))
b64548c7
RS
996 (mouse-minibuffer-check click)
997 (let ((click-posn (posn-point (event-start click)))
998 ;; Don't let a subsequent kill command append to this one:
999 ;; prevent setting this-command to kill-region.
1000 (this-command this-command))
c8c5bd24
RS
1001 (if (and (save-excursion
1002 (set-buffer (window-buffer (posn-window (event-start click))))
1003 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1004 ;; Don't be fooled by a recent click in some other buffer.
1005 (eq mouse-selection-click-count-buffer
1006 (current-buffer)))))
b64548c7
RS
1007 (if (not (and (eq last-command 'mouse-save-then-kill)
1008 (equal click-posn
1009 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1010 ;; Find both ends of the object selected by this click.
1011 (let* ((range
1012 (mouse-start-end click-posn click-posn
1013 mouse-selection-click-count)))
1014 ;; Move whichever end is closer to the click.
1015 ;; That's what xterm does, and it seems reasonable.
1016 (if (< (abs (- click-posn (mark t)))
1017 (abs (- click-posn (point))))
1018 (set-mark (car range))
1019 (goto-char (nth 1 range)))
1020 ;; We have already put the old region in the kill ring.
1021 ;; Replace it with the extended region.
1022 ;; (It would be annoying to make a separate entry.)
1023 (kill-new (buffer-substring (point) (mark t)) t)
e6c2f5d4 1024 (mouse-set-region-1)
b64548c7
RS
1025 ;; Arrange for a repeated mouse-3 to kill this region.
1026 (setq mouse-save-then-kill-posn
1027 (list (car kill-ring) (point) click-posn))
1028 (mouse-show-mark))
1029 ;; If we click this button again without moving it,
1030 ;; that time kill.
bcde3748 1031 (mouse-save-then-kill-delete-region (mark) (point))
b64548c7 1032 (setq mouse-selection-click-count 0)
dd524dbd 1033 (setq mouse-save-then-kill-posn nil))
b64548c7
RS
1034 (if (and (eq last-command 'mouse-save-then-kill)
1035 mouse-save-then-kill-posn
1036 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1037 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1038 ;; If this is the second time we've called
1039 ;; mouse-save-then-kill, delete the text from the buffer.
1040 (progn
1041 (mouse-save-then-kill-delete-region (point) (mark))
1042 ;; After we kill, another click counts as "the first time".
1043 (setq mouse-save-then-kill-posn nil))
c8f264ae
RS
1044 ;; This is not a repetition.
1045 ;; We are adjusting an old selection or creating a new one.
b64548c7
RS
1046 (if (or (and (eq last-command 'mouse-save-then-kill)
1047 mouse-save-then-kill-posn)
1048 (and mark-active transient-mark-mode)
1049 (and (memq last-command
1050 '(mouse-drag-region mouse-set-region))
1051 (or mark-even-if-inactive
1052 (not transient-mark-mode))))
1053 ;; We have a selection or suitable region, so adjust it.
1054 (let* ((posn (event-start click))
1055 (new (posn-point posn)))
1056 (select-window (posn-window posn))
1057 (if (numberp new)
1058 (progn
1059 ;; Move whichever end of the region is closer to the click.
1060 ;; That is what xterm does, and it seems reasonable.
1061 (if (< (abs (- new (point))) (abs (- new (mark t))))
1062 (goto-char new)
1063 (set-mark new))
1064 (setq deactivate-mark nil)))
bcde3748
RS
1065 (kill-new (buffer-substring (point) (mark t)) t)
1066 (mouse-show-mark))
b64548c7
RS
1067 ;; Set the mark where point is, then move where clicked.
1068 (mouse-set-mark-fast click)
1069 (if before-scroll
1070 (goto-char before-scroll))
1071 (exchange-point-and-mark)
c8f264ae
RS
1072 (kill-new (buffer-substring (point) (mark t)))
1073 (if window-system
1074 (mouse-show-mark)))
e6c2f5d4 1075 (mouse-set-region-1)
b64548c7
RS
1076 (setq mouse-save-then-kill-posn
1077 (list (car kill-ring) (point) click-posn)))))))
e66feb07
RS
1078\f
1079(global-set-key [M-mouse-1] 'mouse-start-secondary)
1080(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1081(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1082(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 1083(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07
RS
1084
1085;; An overlay which records the current secondary selection
1086;; or else is deleted when there is no secondary selection.
1087;; May be nil.
1088(defvar mouse-secondary-overlay nil)
1089
a94c7fcc
RS
1090(defvar mouse-secondary-click-count 0)
1091
e66feb07
RS
1092;; A marker which records the specified first end for a secondary selection.
1093;; May be nil.
1094(defvar mouse-secondary-start nil)
1095
1096(defun mouse-start-secondary (click)
1097 "Set one end of the secondary selection to the position clicked on.
1098Use \\[mouse-secondary-save-then-kill] to set the other end
1099and complete the secondary selection."
1100 (interactive "e")
d65147f6 1101 (mouse-minibuffer-check click)
e66feb07 1102 (let ((posn (event-start click)))
230aaa73
RS
1103 (save-excursion
1104 (set-buffer (window-buffer (posn-window posn)))
1105 ;; Cancel any preexisting secondary selection.
1106 (if mouse-secondary-overlay
1107 (delete-overlay mouse-secondary-overlay))
1108 (if (numberp (posn-point posn))
1109 (progn
1110 (or mouse-secondary-start
1111 (setq mouse-secondary-start (make-marker)))
1112 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
1113
1114(defun mouse-set-secondary (click)
1115 "Set the secondary selection to the text that the mouse is dragged over.
1116This must be bound to a mouse drag event."
1117 (interactive "e")
d65147f6 1118 (mouse-minibuffer-check click)
e66feb07
RS
1119 (let ((posn (event-start click))
1120 beg
1121 (end (event-end click)))
230aaa73
RS
1122 (save-excursion
1123 (set-buffer (window-buffer (posn-window posn)))
1124 (if (numberp (posn-point posn))
1125 (setq beg (posn-point posn)))
1126 (if mouse-secondary-overlay
1127 (move-overlay mouse-secondary-overlay beg (posn-point end))
1128 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1129 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
947da0c4 1130
d89a4a47 1131(defun mouse-drag-secondary (start-event)
e66feb07 1132 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 1133Highlight the drag area as you move the mouse.
efc10c97
RS
1134This must be bound to a button-down mouse event.
1135The function returns a non-nil value if it creates a secondary selection."
e66feb07 1136 (interactive "e")
d65147f6 1137 (mouse-minibuffer-check start-event)
4c9afcbe
RS
1138 (let* ((echo-keystrokes 0)
1139 (start-posn (event-start start-event))
d89a4a47
RS
1140 (start-point (posn-point start-posn))
1141 (start-window (posn-window start-posn))
1142 (start-frame (window-frame start-window))
1143 (bounds (window-edges start-window))
1144 (top (nth 1 bounds))
1145 (bottom (if (window-minibuffer-p start-window)
1146 (nth 3 bounds)
1147 ;; Don't count the mode line.
1148 (1- (nth 3 bounds))))
1149 (click-count (1- (event-click-count start-event))))
1150 (save-excursion
1151 (set-buffer (window-buffer start-window))
a94c7fcc 1152 (setq mouse-secondary-click-count click-count)
d89a4a47
RS
1153 (or mouse-secondary-overlay
1154 (setq mouse-secondary-overlay
1155 (make-overlay (point) (point))))
26d280b9 1156 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
9a974c88 1157 (if (> (mod click-count 3) 0)
26d280b9
RS
1158 ;; Double or triple press: make an initial selection
1159 ;; of one word or line.
d89a4a47
RS
1160 (let ((range (mouse-start-end start-point start-point click-count)))
1161 (set-marker mouse-secondary-start nil)
1162 (move-overlay mouse-secondary-overlay 1 1
1163 (window-buffer start-window))
1164 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1165 (window-buffer start-window)))
26d280b9 1166 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
1167 (or mouse-secondary-start
1168 (setq mouse-secondary-start (make-marker)))
1169 (set-marker mouse-secondary-start start-point)
1170 (delete-overlay mouse-secondary-overlay))
1171 (let (event end end-point)
1172 (track-mouse
1173 (while (progn
1174 (setq event (read-event))
1175 (or (mouse-movement-p event)
1176 (eq (car-safe event) 'switch-frame)))
1177
1178 (if (eq (car-safe event) 'switch-frame)
1179 nil
1180 (setq end (event-end event)
1181 end-point (posn-point end))
1182 (cond
d89a4a47
RS
1183 ;; Are we moving within the original window?
1184 ((and (eq (posn-window end) start-window)
1185 (integer-or-marker-p end-point))
d89a4a47
RS
1186 (let ((range (mouse-start-end start-point end-point
1187 click-count)))
0136e1e3
RS
1188 (if (or (/= start-point end-point)
1189 (null (marker-position mouse-secondary-start)))
1190 (progn
1191 (set-marker mouse-secondary-start nil)
1192 (move-overlay mouse-secondary-overlay
1193 (car range) (nth 1 range))))))
3b0aebe9
RS
1194 (t
1195 (let ((mouse-row (cdr (cdr (mouse-position)))))
1196 (cond
1197 ((null mouse-row))
1198 ((< mouse-row top)
e919a622
RS
1199 (mouse-scroll-subr start-window (- mouse-row top)
1200 mouse-secondary-overlay start-point))
d2287ded 1201 ((>= mouse-row bottom)
e919a622 1202 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 1203 mouse-secondary-overlay start-point)))))))))
d89a4a47 1204
4e399a53 1205 (if (consp event)
d89a4a47
RS
1206 (if (marker-position mouse-secondary-start)
1207 (save-window-excursion
1208 (delete-overlay mouse-secondary-overlay)
9a974c88 1209 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
1210 (select-window start-window)
1211 (save-excursion
1212 (goto-char mouse-secondary-start)
efc10c97
RS
1213 (sit-for 1)
1214 nil))
9a974c88
RS
1215 (x-set-selection
1216 'SECONDARY
1217 (buffer-substring (overlay-start mouse-secondary-overlay)
1218 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 1219
9a974c88 1220(defun mouse-yank-secondary (click)
50f58001
RS
1221 "Insert the secondary selection at the position clicked on.
1222Move point to the end of the inserted text.
1223If `mouse-yank-at-point' is non-nil, insert at point
1224regardless of where you click."
9a974c88 1225 (interactive "e")
33c448cd
RS
1226 ;; Give temporary modes such as isearch a chance to turn off.
1227 (run-hooks 'mouse-leave-buffer-hook)
50f58001
RS
1228 (or mouse-yank-at-point (mouse-set-point click))
1229 (insert (x-get-selection 'SECONDARY)))
9a974c88 1230
7e6404f6 1231(defun mouse-kill-secondary ()
9a974c88
RS
1232 "Kill the text in the secondary selection.
1233This is intended more as a keyboard command than as a mouse command
1234but it can work as either one.
1235
1236The current buffer (in case of keyboard use), or the buffer clicked on,
1237must be the one that the secondary selection is in. This requirement
1238is to prevent accidents."
7e6404f6
RS
1239 (interactive)
1240 (let* ((keys (this-command-keys))
1241 (click (elt keys (1- (length keys)))))
1242 (or (eq (overlay-buffer mouse-secondary-overlay)
1243 (if (listp click)
1244 (window-buffer (posn-window (event-start click)))
1245 (current-buffer)))
1246 (error "Select or click on the buffer where the secondary selection is")))
9dfab550
RS
1247 (let (this-command)
1248 (save-excursion
1249 (set-buffer (overlay-buffer mouse-secondary-overlay))
1250 (kill-region (overlay-start mouse-secondary-overlay)
1251 (overlay-end mouse-secondary-overlay))))
e66feb07 1252 (delete-overlay mouse-secondary-overlay)
9dfab550 1253;;; (x-set-selection 'SECONDARY nil)
e66feb07
RS
1254 (setq mouse-secondary-overlay nil))
1255
1256(defun mouse-secondary-save-then-kill (click)
d89a4a47 1257 "Save text to point in kill ring; the second time, kill the text.
7bbe2cc7
RS
1258You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1259If the text between where you did \\[mouse-start-secondary] and where
1260you use this command matches the text at the front of the kill ring,
1261this command deletes the text.
e66feb07 1262Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
7bbe2cc7 1263which prepares for a second click with this command to delete the text.
d89a4a47 1264
7bbe2cc7
RS
1265If you have already made a secondary selection in that buffer,
1266this command extends or retracts the selection to where you click.
1267If you do this again in a different position, it extends or retracts
1268again. If you do this twice in the same position, it kills the selection."
e66feb07 1269 (interactive "e")
d65147f6 1270 (mouse-minibuffer-check click)
d89a4a47
RS
1271 (let ((posn (event-start click))
1272 (click-posn (posn-point (event-start click)))
e66feb07
RS
1273 ;; Don't let a subsequent kill command append to this one:
1274 ;; prevent setting this-command to kill-region.
1275 (this-command this-command))
9a974c88
RS
1276 (or (eq (window-buffer (posn-window posn))
1277 (or (and mouse-secondary-overlay
1278 (overlay-buffer mouse-secondary-overlay))
1279 (if mouse-secondary-start
1280 (marker-buffer mouse-secondary-start))))
1281 (error "Wrong buffer"))
1282 (save-excursion
1283 (set-buffer (window-buffer (posn-window posn)))
a94c7fcc 1284 (if (> (mod mouse-secondary-click-count 3) 0)
9a974c88
RS
1285 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1286 (equal click-posn
1287 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1288 ;; Find both ends of the object selected by this click.
1289 (let* ((range
1290 (mouse-start-end click-posn click-posn
a94c7fcc 1291 mouse-secondary-click-count)))
9a974c88
RS
1292 ;; Move whichever end is closer to the click.
1293 ;; That's what xterm does, and it seems reasonable.
1294 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1295 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1296 (move-overlay mouse-secondary-overlay (car range)
1297 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1298 (move-overlay mouse-secondary-overlay
1299 (overlay-start mouse-secondary-overlay)
1300 (nth 1 range)))
9a974c88
RS
1301 ;; We have already put the old region in the kill ring.
1302 ;; Replace it with the extended region.
1303 ;; (It would be annoying to make a separate entry.)
f69140fd
KH
1304 (kill-new (buffer-substring
1305 (overlay-start mouse-secondary-overlay)
1306 (overlay-end mouse-secondary-overlay)) t)
9a974c88
RS
1307 ;; Arrange for a repeated mouse-3 to kill this region.
1308 (setq mouse-save-then-kill-posn
1309 (list (car kill-ring) (point) click-posn)))
1310 ;; If we click this button again without moving it,
1311 ;; that time kill.
d89a4a47 1312 (progn
9a974c88
RS
1313 (mouse-save-then-kill-delete-region
1314 (overlay-start mouse-secondary-overlay)
1315 (overlay-end mouse-secondary-overlay))
1316 (setq mouse-save-then-kill-posn nil)
a94c7fcc 1317 (setq mouse-secondary-click-count 0)
9a974c88
RS
1318 (delete-overlay mouse-secondary-overlay)))
1319 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1320 mouse-save-then-kill-posn
1321 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1322 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1323 ;; If this is the second time we've called
1324 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1325 (progn
1326 (mouse-save-then-kill-delete-region
1327 (overlay-start mouse-secondary-overlay)
1328 (overlay-end mouse-secondary-overlay))
1329 (setq mouse-save-then-kill-posn nil)
1330 (delete-overlay mouse-secondary-overlay))
1331 (if (overlay-start mouse-secondary-overlay)
1332 ;; We have a selection, so adjust it.
1333 (progn
1334 (if (numberp click-posn)
1335 (progn
1336 ;; Move whichever end of the region is closer to the click.
1337 ;; That is what xterm does, and it seems reasonable.
1338 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1339 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1340 (move-overlay mouse-secondary-overlay click-posn
1341 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
1342 (move-overlay mouse-secondary-overlay
1343 (overlay-start mouse-secondary-overlay)
1344 click-posn))
9a974c88 1345 (setq deactivate-mark nil)))
0136e1e3 1346 (if (eq last-command 'mouse-secondary-save-then-kill)
f69140fd
KH
1347 ;; If the front of the kill ring comes from
1348 ;; an immediately previous use of this command,
1349 ;; replace it with the extended region.
1350 ;; (It would be annoying to make a separate entry.)
1351 (kill-new (buffer-substring
0136e1e3 1352 (overlay-start mouse-secondary-overlay)
f69140fd 1353 (overlay-end mouse-secondary-overlay)) t)
1fa01bcd
RS
1354 (let (deactivate-mark)
1355 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1356 (overlay-end mouse-secondary-overlay)))))
9a974c88
RS
1357 (if mouse-secondary-start
1358 ;; All we have is one end of a selection,
1359 ;; so put the other end here.
1360 (let ((start (+ 0 mouse-secondary-start)))
1361 (kill-ring-save start click-posn)
1362 (if mouse-secondary-overlay
1363 (move-overlay mouse-secondary-overlay start click-posn)
1364 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1365 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1366 (setq mouse-save-then-kill-posn
1367 (list (car kill-ring) (point) click-posn))))
fb28fd5b
RS
1368 (if (overlay-buffer mouse-secondary-overlay)
1369 (x-set-selection 'SECONDARY
9a974c88
RS
1370 (buffer-substring
1371 (overlay-start mouse-secondary-overlay)
1372 (overlay-end mouse-secondary-overlay)))))))
e66feb07 1373\f
0d9be91c 1374(defcustom mouse-buffer-menu-maxlen 20
f1107960
RS
1375 "*Number of buffers in one pane (submenu) of the buffer menu.
1376If we have lots of buffers, divide them into groups of
0d9be91c 1377`mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
3b558d41
RS
1378 :type 'integer
1379 :group 'mouse)
f1107960 1380
3ca87c7b
RS
1381(defcustom mouse-buffer-menu-mode-mult 4
1382 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1383This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1384will split the buffer menu by the major modes (see
1385`mouse-buffer-menu-mode-groups') or just by menu length.
1386Set to 1 (or even 0!) if you want to group by major mode always, and to
1387a large number if you prefer a mixed multitude. The default is 4."
1388 :type 'integer
1389 :group 'mouse
1390 :version "20.3")
1391
284a88a3
RS
1392(defvar mouse-buffer-menu-mode-groups
1393 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1394 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1395 . "Mail/News")
1396 ("\\<C\\>" . "C")
1397 ("ObjC" . "C")
1398 ("Text" . "Text")
1399 ("Outline" . "Text")
1400 ("Lisp" . "Lisp"))
1401 "How to group various major modes together in \\[mouse-buffer-menu].
1402Each element has the form (REGEXP . GROUPNAME).
1403If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1404
8b34e79d 1405(defun mouse-buffer-menu (event)
2d82f7b9
RS
1406 "Pop up a menu of buffers for selection with the mouse.
1407This switches buffers in the window that you clicked on,
1408and selects that window."
ec558adc 1409 (interactive "e")
d65147f6 1410 (mouse-minibuffer-check event)
3ca87c7b 1411 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
284a88a3
RS
1412 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1413 (let ((tail buffers))
1414 (while tail
1415 ;; Divide all buffers into buckets for various major modes.
1416 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1417 (with-current-buffer (car tail)
1418 (let* ((adjusted-major-mode major-mode) elt)
1419 (let ((tail mouse-buffer-menu-mode-groups))
1420 (while tail
1421 (if (string-match (car (car tail)) mode-name)
1422 (setq adjusted-major-mode (cdr (car tail))))
1423 (setq tail (cdr tail))))
1424 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1425 (if (null elt)
1426 (setq elt (list adjusted-major-mode
1427 (if (stringp adjusted-major-mode)
1428 adjusted-major-mode
1429 mode-name))
1430 split-by-major-mode (cons elt split-by-major-mode)))
1431 (or (memq (car tail) (cdr (cdr elt)))
1432 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1433 (setq tail (cdr tail))))
1434 ;; Compute the sum of squares of sizes of the major-mode buckets.
1435 (let ((tail split-by-major-mode))
1436 (setq sum-of-squares 0)
1437 (while tail
1438 (setq sum-of-squares
1439 (+ sum-of-squares
3ca87c7b 1440 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
284a88a3 1441 (setq tail (cdr tail))))
3ca87c7b
RS
1442 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1443 (* (length buffers) (length buffers)))
284a88a3
RS
1444 ;; Subdividing by major modes really helps, so let's do it.
1445 (let (subdivided-menus (buffers-left (length buffers)))
1446 ;; Sort the list to put the most popular major modes first.
1447 (setq split-by-major-mode
1448 (sort split-by-major-mode
1449 (function (lambda (elt1 elt2)
1450 (> (length elt1) (length elt2))))))
1451 ;; Make a separate submenu for each major mode
1452 ;; that has more than one buffer,
1453 ;; unless all the remaining buffers are less than 1/10 of them.
1454 (while (and split-by-major-mode
1455 (and (> (length (car split-by-major-mode)) 3)
1456 (> (* buffers-left 10) (length buffers))))
1457 (setq subdivided-menus
1458 (cons (cons
1459 (nth 1 (car split-by-major-mode))
1460 (mouse-buffer-menu-alist
1461 (cdr (cdr (car split-by-major-mode)))))
1462 subdivided-menus))
1463 (setq buffers-left
1464 (- buffers-left (length (cdr (car split-by-major-mode)))))
1465 (setq split-by-major-mode (cdr split-by-major-mode)))
1466 ;; If any major modes are left over,
1467 ;; make a single submenu for them.
1468 (if split-by-major-mode
1469 (setq subdivided-menus
1470 (cons (cons
1471 "Others"
1472 (mouse-buffer-menu-alist
3ca87c7b
RS
1473 ;; we don't need split-by-major-mode any
1474 ;; more, so we can ditch it with nconc.
1475 (apply 'nconc (mapcar 'cddr split-by-major-mode))))
284a88a3 1476 subdivided-menus)))
3ca87c7b 1477 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
284a88a3
RS
1478 (progn
1479 (setq alist (mouse-buffer-menu-alist buffers))
1480 (setq menu (cons "Buffer Menu"
1481 (mouse-buffer-menu-split "Select Buffer" alist)))))
2d82f7b9
RS
1482 (let ((buf (x-popup-menu event menu))
1483 (window (posn-window (event-start event))))
3ca87c7b 1484 (when buf
c0bb9f3b 1485 (or (framep window) (select-window window))
3ca87c7b 1486 (switch-to-buffer buf)))))
284a88a3
RS
1487
1488(defun mouse-buffer-menu-alist (buffers)
1489 (let (tail
1490 (maxlen 0)
1491 head)
1492 (setq buffers
1493 (sort buffers
1494 (function (lambda (elt1 elt2)
1495 (string< (buffer-name elt1) (buffer-name elt2))))))
1496 (setq tail buffers)
1497 (while tail
1498 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1499 (setq maxlen
1500 (max maxlen
1501 (length (buffer-name (car tail))))))
1502 (setq tail (cdr tail)))
1503 (setq tail buffers)
1504 (while tail
1505 (let ((elt (car tail)))
1506 (if (/= (aref (buffer-name elt) 0) ?\ )
1507 (setq head
1508 (cons
1509 (cons
1510 (format
1511 (format "%%%ds %%s%%s %%s" maxlen)
1512 (buffer-name elt)
1513 (if (buffer-modified-p elt) "*" " ")
1514 (save-excursion
1515 (set-buffer elt)
1516 (if buffer-read-only "%" " "))
1517 (or (buffer-file-name elt)
1518 (save-excursion
1519 (set-buffer elt)
1520 (if list-buffers-directory
1521 (expand-file-name
1522 list-buffers-directory)))
1523 ""))
1524 elt)
1525 head))))
1526 (setq tail (cdr tail)))
1527 ;; Compensate for the reversal that the above loop does.
1528 (nreverse head)))
1529
1530(defun mouse-buffer-menu-split (title alist)
1531 ;; If we have lots of buffers, divide them into groups of 20
1532 ;; and make a pane (or submenu) for each one.
0d9be91c 1533 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
284a88a3
RS
1534 (let ((alist alist) sublists next
1535 (i 1))
1536 (while alist
0d9be91c 1537 ;; Pull off the next mouse-buffer-menu-maxlen buffers
284a88a3 1538 ;; and make them the next element of sublist.
0d9be91c 1539 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
284a88a3 1540 (if next
0d9be91c 1541 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
284a88a3
RS
1542 nil))
1543 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1544 sublists))
1545 (setq i (1+ i))
1546 (setq alist next))
1547 (nreverse sublists))
1548 ;; Few buffers--put them all in one pane.
1549 (list (cons title alist))))
72ea54a4 1550\f
5ba2dc3f 1551;;; These need to be rewritten for the new scroll bar implementation.
dbc4e1c1
JB
1552
1553;;;!! ;; Commands for the scroll bar.
1554;;;!!
1555;;;!! (defun mouse-scroll-down (click)
1556;;;!! (interactive "@e")
1557;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1558;;;!!
1559;;;!! (defun mouse-scroll-up (click)
1560;;;!! (interactive "@e")
1561;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1562;;;!!
1563;;;!! (defun mouse-scroll-down-full ()
1564;;;!! (interactive "@")
1565;;;!! (scroll-down nil))
1566;;;!!
1567;;;!! (defun mouse-scroll-up-full ()
1568;;;!! (interactive "@")
1569;;;!! (scroll-up nil))
1570;;;!!
1571;;;!! (defun mouse-scroll-move-cursor (click)
1572;;;!! (interactive "@e")
1573;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1574;;;!!
1575;;;!! (defun mouse-scroll-absolute (event)
1576;;;!! (interactive "@e")
1577;;;!! (let* ((pos (car event))
1578;;;!! (position (car pos))
1579;;;!! (length (car (cdr pos))))
1580;;;!! (if (<= length 0) (setq length 1))
1581;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1582;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1583;;;!! position)
1584;;;!! length)
1585;;;!! scale-factor)))
1586;;;!! (goto-char newpos)
1587;;;!! (recenter '(4)))))
1588;;;!!
1589;;;!! (defun mouse-scroll-left (click)
1590;;;!! (interactive "@e")
1591;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1592;;;!!
1593;;;!! (defun mouse-scroll-right (click)
1594;;;!! (interactive "@e")
1595;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1596;;;!!
1597;;;!! (defun mouse-scroll-left-full ()
1598;;;!! (interactive "@")
1599;;;!! (scroll-left nil))
1600;;;!!
1601;;;!! (defun mouse-scroll-right-full ()
1602;;;!! (interactive "@")
1603;;;!! (scroll-right nil))
1604;;;!!
1605;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1606;;;!! (interactive "@e")
1607;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1608;;;!!
1609;;;!! (defun mouse-scroll-absolute-horizontally (event)
1610;;;!! (interactive "@e")
1611;;;!! (let* ((pos (car event))
1612;;;!! (position (car pos))
1613;;;!! (length (car (cdr pos))))
1614;;;!! (set-window-hscroll (selected-window) 33)))
1615;;;!!
1616;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1617;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1618;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1619;;;!!
1620;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1621;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1622;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1623;;;!!
1624;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1625;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1626;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1627;;;!!
1628;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1629;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1630;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1631;;;!!
1632;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1633;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1634;;;!! 'mouse-scroll-absolute-horizontally)
1635;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1636;;;!!
1637;;;!! (global-set-key [horizontal-slider mouse-1]
1638;;;!! 'mouse-scroll-move-cursor-horizontally)
1639;;;!! (global-set-key [horizontal-slider mouse-2]
1640;;;!! 'mouse-scroll-move-cursor-horizontally)
1641;;;!! (global-set-key [horizontal-slider mouse-3]
1642;;;!! 'mouse-scroll-move-cursor-horizontally)
1643;;;!!
1644;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1645;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1646;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1647;;;!!
1648;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1649;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1650;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1651;;;!!
1652;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1653;;;!! 'mouse-split-window-horizontally)
1654;;;!! (global-set-key [mode-line S-mouse-2]
1655;;;!! 'mouse-split-window-horizontally)
1656;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1657;;;!! 'mouse-split-window)
6b2154de 1658\f
dbc4e1c1
JB
1659;;;!! ;;;;
1660;;;!! ;;;; Here are experimental things being tested. Mouse events
1661;;;!! ;;;; are of the form:
1662;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1663;;;!! ;;
1664;;;!! ;;;;
1665;;;!! ;;;; Dynamically track mouse coordinates
1666;;;!! ;;;;
1667;;;!! ;;
1668;;;!! ;;(defun track-mouse (event)
1669;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1670;;;!! ;; (interactive "@e")
1671;;;!! ;; (while mouse-grabbed
1672;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1673;;;!! ;; (abs-x (car pos))
1674;;;!! ;; (abs-y (cdr pos))
1675;;;!! ;; (relative-coordinate (coordinates-in-window-p
1676;;;!! ;; (list (car pos) (cdr pos))
1677;;;!! ;; (selected-window))))
1678;;;!! ;; (if (consp relative-coordinate)
1679;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1680;;;!! ;; (car relative-coordinate)
1681;;;!! ;; (car (cdr relative-coordinate)))
1682;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1683;;;!!
1684;;;!! ;;
1685;;;!! ;; Dynamically put a box around the line indicated by point
1686;;;!! ;;
1687;;;!! ;;
1688;;;!! ;;(require 'backquote)
1689;;;!! ;;
1690;;;!! ;;(defun mouse-select-buffer-line (event)
1691;;;!! ;; (interactive "@e")
1692;;;!! ;; (let ((relative-coordinate
1693;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1694;;;!! ;; (abs-y (car (cdr (car event)))))
1695;;;!! ;; (if (consp relative-coordinate)
1696;;;!! ;; (progn
1697;;;!! ;; (save-excursion
1698;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1699;;;!! ;; (x-draw-rectangle
1700;;;!! ;; (selected-screen)
1701;;;!! ;; abs-y 0
1702;;;!! ;; (save-excursion
1703;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1704;;;!! ;; (end-of-line)
1705;;;!! ;; (push-mark nil t)
1706;;;!! ;; (beginning-of-line)
1707;;;!! ;; (- (region-end) (region-beginning))) 1))
1708;;;!! ;; (sit-for 1)
1709;;;!! ;; (x-erase-rectangle (selected-screen))))))
1710;;;!! ;;
1711;;;!! ;;(defvar last-line-drawn nil)
1712;;;!! ;;(defvar begin-delim "[^ \t]")
1713;;;!! ;;(defvar end-delim "[^ \t]")
1714;;;!! ;;
1715;;;!! ;;(defun mouse-boxing (event)
1716;;;!! ;; (interactive "@e")
1717;;;!! ;; (save-excursion
1718;;;!! ;; (let ((screen (selected-screen)))
1719;;;!! ;; (while (= (x-mouse-events) 0)
1720;;;!! ;; (let* ((pos (read-mouse-position screen))
1721;;;!! ;; (abs-x (car pos))
1722;;;!! ;; (abs-y (cdr pos))
1723;;;!! ;; (relative-coordinate
1724;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1725;;;!! ;; (selected-window)))
1726;;;!! ;; (begin-reg nil)
1727;;;!! ;; (end-reg nil)
1728;;;!! ;; (end-column nil)
1729;;;!! ;; (begin-column nil))
1730;;;!! ;; (if (and (consp relative-coordinate)
1731;;;!! ;; (or (not last-line-drawn)
1732;;;!! ;; (not (= last-line-drawn abs-y))))
1733;;;!! ;; (progn
1734;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1735;;;!! ;; (if (= (following-char) 10)
1736;;;!! ;; ()
1737;;;!! ;; (progn
1738;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1739;;;!! ;; (setq begin-column (1- (current-column)))
1740;;;!! ;; (end-of-line)
1741;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1742;;;!! ;; (setq end-column (1+ (current-column)))
1743;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1744;;;!! ;; (x-draw-rectangle screen
1745;;;!! ;; (setq last-line-drawn abs-y)
1746;;;!! ;; begin-column
1747;;;!! ;; (- end-column begin-column) 1))))))))))
1748;;;!! ;;
1749;;;!! ;;(defun mouse-erase-box ()
1750;;;!! ;; (interactive)
1751;;;!! ;; (if last-line-drawn
1752;;;!! ;; (progn
1753;;;!! ;; (x-erase-rectangle (selected-screen))
1754;;;!! ;; (setq last-line-drawn nil))))
1755;;;!!
1756;;;!! ;;; (defun test-x-rectangle ()
1757;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1758;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1759;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1760;;;!!
1761;;;!! ;;
1762;;;!! ;; Here is how to do double clicking in lisp. About to change.
1763;;;!! ;;
1764;;;!!
1765;;;!! (defvar double-start nil)
1766;;;!! (defconst double-click-interval 300
1767;;;!! "Max ticks between clicks")
1768;;;!!
1769;;;!! (defun double-down (event)
1770;;;!! (interactive "@e")
1771;;;!! (if double-start
1772;;;!! (let ((interval (- (nth 4 event) double-start)))
1773;;;!! (if (< interval double-click-interval)
1774;;;!! (progn
1775;;;!! (backward-up-list 1)
1776;;;!! ;; (message "Interval %d" interval)
1777;;;!! (sleep-for 1)))
1778;;;!! (setq double-start nil))
1779;;;!! (setq double-start (nth 4 event))))
1780;;;!!
1781;;;!! (defun double-up (event)
1782;;;!! (interactive "@e")
1783;;;!! (and double-start
1784;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1785;;;!! (setq double-start nil)))
1786;;;!!
1787;;;!! ;;; (defun x-test-doubleclick ()
1788;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1789;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1790;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1791;;;!!
1792;;;!! ;;
5ba2dc3f 1793;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
dbc4e1c1
JB
1794;;;!! ;;
1795;;;!!
1796;;;!! (defvar scrolled-lines 0)
1797;;;!! (defconst scroll-speed 1)
1798;;;!!
1799;;;!! (defun incr-scroll-down (event)
1800;;;!! (interactive "@e")
1801;;;!! (setq scrolled-lines 0)
1802;;;!! (incremental-scroll scroll-speed))
1803;;;!!
1804;;;!! (defun incr-scroll-up (event)
1805;;;!! (interactive "@e")
1806;;;!! (setq scrolled-lines 0)
1807;;;!! (incremental-scroll (- scroll-speed)))
1808;;;!!
1809;;;!! (defun incremental-scroll (n)
1810;;;!! (while (= (x-mouse-events) 0)
1811;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1812;;;!! (scroll-down n)
1813;;;!! (sit-for 300 t)))
1814;;;!!
1815;;;!! (defun incr-scroll-stop (event)
1816;;;!! (interactive "@e")
1817;;;!! (message "Scrolled %d lines" scrolled-lines)
1818;;;!! (setq scrolled-lines 0)
1819;;;!! (sleep-for 1))
1820;;;!!
1821;;;!! ;;; (defun x-testing-scroll ()
1822;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1823;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1824;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1825;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1826;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1827;;;!!
1828;;;!! ;;
1829;;;!! ;; Some playthings suitable for picture mode? They need work.
1830;;;!! ;;
1831;;;!!
1832;;;!! (defun mouse-kill-rectangle (event)
1833;;;!! "Kill the rectangle between point and the mouse cursor."
1834;;;!! (interactive "@e")
1835;;;!! (let ((point-save (point)))
1836;;;!! (save-excursion
1837;;;!! (mouse-set-point event)
1838;;;!! (push-mark nil t)
1839;;;!! (if (> point-save (point))
1840;;;!! (kill-rectangle (point) point-save)
1841;;;!! (kill-rectangle point-save (point))))))
1842;;;!!
1843;;;!! (defun mouse-open-rectangle (event)
1844;;;!! "Kill the rectangle between point and the mouse cursor."
1845;;;!! (interactive "@e")
1846;;;!! (let ((point-save (point)))
1847;;;!! (save-excursion
1848;;;!! (mouse-set-point event)
1849;;;!! (push-mark nil t)
1850;;;!! (if (> point-save (point))
1851;;;!! (open-rectangle (point) point-save)
1852;;;!! (open-rectangle point-save (point))))))
1853;;;!!
1854;;;!! ;; Must be a better way to do this.
1855;;;!!
1856;;;!! (defun mouse-multiple-insert (n char)
1857;;;!! (while (> n 0)
1858;;;!! (insert char)
1859;;;!! (setq n (1- n))))
1860;;;!!
1861;;;!! ;; What this could do is not finalize until button was released.
1862;;;!!
1863;;;!! (defun mouse-move-text (event)
1864;;;!! "Move text from point to cursor position, inserting spaces."
1865;;;!! (interactive "@e")
1866;;;!! (let* ((relative-coordinate
1867;;;!! (coordinates-in-window-p (car event) (selected-window))))
1868;;;!! (if (consp relative-coordinate)
1869;;;!! (cond ((> (current-column) (car relative-coordinate))
1870;;;!! (delete-char
1871;;;!! (- (car relative-coordinate) (current-column))))
1872;;;!! ((< (current-column) (car relative-coordinate))
1873;;;!! (mouse-multiple-insert
1874;;;!! (- (car relative-coordinate) (current-column)) " "))
1875;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
07a78410 1876\f
f936ae06
RS
1877;; Choose a completion with the mouse.
1878
1879(defun mouse-choose-completion (event)
49e61c42 1880 "Click on an alternative in the `*Completions*' buffer to choose it."
f936ae06 1881 (interactive "e")
33c448cd
RS
1882 ;; Give temporary modes such as isearch a chance to turn off.
1883 (run-hooks 'mouse-leave-buffer-hook)
d89a4a47 1884 (let ((buffer (window-buffer))
02680e9b
RS
1885 choice
1886 base-size)
f936ae06
RS
1887 (save-excursion
1888 (set-buffer (window-buffer (posn-window (event-start event))))
f36f4e9e
RS
1889 (if completion-reference-buffer
1890 (setq buffer completion-reference-buffer))
02680e9b 1891 (setq base-size completion-base-size)
f936ae06
RS
1892 (save-excursion
1893 (goto-char (posn-point (event-start event)))
b6be5d95 1894 (let (beg end)
9a43a594
RS
1895 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1896 (setq end (point) beg (1+ (point))))
1897 (if (null beg)
1898 (error "No completion here"))
1899 (setq beg (previous-single-property-change beg 'mouse-face))
2f5ed2e8
RS
1900 (setq end (or (next-single-property-change end 'mouse-face)
1901 (point-max)))
b6be5d95 1902 (setq choice (buffer-substring beg end)))))
73e2025f
RS
1903 (let ((owindow (selected-window)))
1904 (select-window (posn-window (event-start event)))
874a2cbd
RS
1905 (if (and (one-window-p t 'selected-frame)
1906 (window-dedicated-p (selected-window)))
1907 ;; This is a special buffer's frame
1908 (iconify-frame (selected-frame))
1909 (or (window-dedicated-p (selected-window))
1910 (bury-buffer)))
73e2025f 1911 (select-window owindow))
02680e9b 1912 (choose-completion-string choice buffer base-size)))
f936ae06 1913\f
07a78410
RS
1914;; Font selection.
1915
0eb9fef3
RS
1916(defun font-menu-add-default ()
1917 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1918 (font-alist x-fixed-font-alist)
0d94f5ca 1919 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
1920 (if (assoc "Default" elt)
1921 (delete (assoc "Default" elt) elt))
1922 (setcdr elt
6b8e486e 1923 (cons (list "Default" default)
0eb9fef3
RS
1924 (cdr elt)))))
1925
07a78410
RS
1926(defvar x-fixed-font-alist
1927 '("Font menu"
1928 ("Misc"
19d973e8
RS
1929 ;; For these, we specify the pixel height and width.
1930 ("fixed" "fixed")
1931 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1932 ("6x12"
1933 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1934 ("6x13"
1935 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1936 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1937 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1938 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1939 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1940 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1941 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1942 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 1943 ("")
f29c3695
RS
1944 ("clean 5x8"
1945 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1946 ("clean 6x8"
1947 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
1948 ("clean 8x8"
1949 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1950 ("clean 8x10"
1951 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1952 ("clean 8x14"
1953 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1954 ("clean 8x16"
1955 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 1956 ("")
29a3028d 1957 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
07a78410
RS
1958;;; We don't seem to have these; who knows what they are.
1959;;; ("fg-18" "fg-18")
1960;;; ("fg-25" "fg-25")
29a3028d
DL
1961 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
1962 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
1963 ("lucidasanstypewriter-bold-24"
1964 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
07a78410
RS
1965;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1966;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
29a3028d 1967 )
07a78410 1968 ("Courier"
19d973e8 1969 ;; For these, we specify the point height.
82c048a9
RS
1970 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1971 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1972 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1973 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1974 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1975 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1976 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1977 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1978 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1979 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1980 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1981 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1982 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1983 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1984 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1985 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1986 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1987 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1988 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1989 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1990 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1991 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1992 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1993 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
07a78410
RS
1994 )
1995 "X fonts suitable for use in Emacs.")
1996
1900a92b 1997(defun mouse-set-font (&rest fonts)
51a02226 1998 "Select an emacs font from a list of known good fonts and fontsets."
07a78410 1999 (interactive
51a02226
KH
2000 (x-popup-menu
2001 last-nonmenu-event
2002 ;; Append list of fontsets currently defined.
2003 (append x-fixed-font-alist (list (generate-fontset-menu)))))
df4de8c6
KH
2004 (if fonts
2005 (let (font)
2006 (while fonts
2007 (condition-case nil
2008 (progn
3fadec1a 2009 (set-default-font (car fonts))
df4de8c6
KH
2010 (setq font (car fonts))
2011 (setq fonts nil))
3fadec1a
RS
2012 (error
2013 (setq fonts (cdr fonts)))))
df4de8c6 2014 (if (null font)
3fadec1a 2015 (error "Font not found")))))
cc0a8174
JB
2016\f
2017;;; Bindings for mouse commands.
2018
fcfc3c63 2019(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 2020(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 2021(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 2022
e37de120
RS
2023;; These are tested for in mouse-drag-region.
2024(global-set-key [double-mouse-1] 'mouse-set-point)
2025(global-set-key [triple-mouse-1] 'mouse-set-point)
2026
dbc4e1c1
JB
2027(global-set-key [mouse-2] 'mouse-yank-at-click)
2028(global-set-key [mouse-3] 'mouse-save-then-kill)
8b34e79d 2029
dbc4e1c1
JB
2030;; By binding these to down-going events, we let the user use the up-going
2031;; event to make the selection, saving a click.
08a1c178
RS
2032(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2033(if (not (eq system-type 'ms-dos))
2034 (global-set-key [S-down-mouse-1] 'mouse-set-font))
eef805d7 2035;; C-down-mouse-2 is bound in facemenu.el.
95132d1c
RS
2036(global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
2037
07a78410 2038
8b34e79d
RS
2039;; Replaced with dragging mouse-1
2040;; (global-set-key [S-mouse-1] 'mouse-set-mark)
947da0c4 2041
3c2dd2c0 2042(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
2043(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2044(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
b0d22e20 2045(global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
3c2dd2c0 2046(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 2047(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 2048(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 2049(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 2050(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
08a1c178
RS
2051(global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2052(global-set-key [vertical-line mouse-1] 'mouse-select-window)
49116ac0
JB
2053
2054(provide 'mouse)
2055
2fe85227
DL
2056;; This file contains the functionality of the old mldrag.el.
2057(defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2058(defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2059(make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2060(make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2061(provide 'mldrag)
2062
6594deb0 2063;;; mouse.el ends here