(rmail-retry-failure): Narrow to just the old header
[bpt/emacs.git] / lisp / mouse.el
CommitLineData
6594deb0 1;;; mouse.el --- window system-independent mouse support.
84176303 2
d733c5ec 3;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
eea8d4ef 4
84176303 5;; Maintainer: FSF
84176303
ER
6;; Keywords: hardware
7
cc0a8174 8;;; This file is part of GNU Emacs.
72ea54a4 9
cc0a8174
JB
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
87ef29fd 12;;; the Free Software Foundation; either version 2, or (at your option)
cc0a8174 13;;; any later version.
72ea54a4 14
cc0a8174
JB
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
cc0a8174
JB
20;;; You should have received a copy of the GNU General Public License
21;;; along with GNU Emacs; see the file COPYING. If not, write to
22;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
72ea54a4 23
edbd2f74
ER
24;;; Commentary:
25
26;; This package provides various useful commands (including help
27;; system access) through the mouse. All this code assumes that mouse
28;; interpretation has been abstracted into Emacs input events.
29;;
30;; The code is rather X-dependent.
31
aae56ea7
ER
32;;; Code:
33
cc0a8174 34;;; Utility functions.
72ea54a4 35
cc0a8174
JB
36;;; Indent track-mouse like progn.
37(put 'track-mouse 'lisp-indent-function 0)
72ea54a4 38
50f58001
RS
39(defvar mouse-yank-at-point nil
40 "*If non-nil, mouse yank commands yank at point instead of at click.")
cc0a8174 41\f
544e7e73
RS
42;; Commands that operate on windows.
43
d65147f6
KH
44(defun mouse-minibuffer-check (event)
45 (let ((w (posn-window (event-start event))))
46 (and (window-minibuffer-p w)
47 (not (minibuffer-window-active-p w))
48 (error "Minibuffer window is not active"))))
49
cc0a8174 50(defun mouse-delete-window (click)
947da0c4 51 "Delete the window you click on.
cc0a8174 52This must be bound to a mouse click."
ec558adc 53 (interactive "e")
d65147f6 54 (mouse-minibuffer-check click)
b5370f03 55 (delete-window (posn-window (event-start click))))
cc0a8174 56
3c2dd2c0
RS
57(defun mouse-select-window (click)
58 "Select the window clicked on; don't move point."
59 (interactive "e")
d65147f6 60 (mouse-minibuffer-check click)
3c2dd2c0
RS
61 (let ((oframe (selected-frame))
62 (frame (window-frame (posn-window (event-start click)))))
63 (select-window (posn-window (event-start click)))
64 (raise-frame frame)
65 (select-frame frame)
66 (or (eq frame oframe)
67 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
68 (unfocus-frame)))
69
b0f3a26b
JB
70(defun mouse-tear-off-window (click)
71 "Delete the window clicked on, and create a new frame displaying its buffer."
72 (interactive "e")
d65147f6 73 (mouse-minibuffer-check click)
b0f3a26b
JB
74 (let* ((window (posn-window (event-start click)))
75 (buf (window-buffer window))
01a911e3 76 (frame (make-frame)))
b0f3a26b
JB
77 (select-frame frame)
78 (switch-to-buffer buf)
79 (delete-window window)))
80
b5370f03 81(defun mouse-delete-other-windows ()
947da0c4 82 "Delete all window except the one you click on."
b5370f03 83 (interactive "@")
cc0a8174 84 (delete-other-windows))
72ea54a4 85
cc0a8174
JB
86(defun mouse-split-window-vertically (click)
87 "Select Emacs window mouse is on, then split it vertically in half.
88The window is split at the line clicked on.
89This command must be bound to a mouse click."
947da0c4 90 (interactive "@e")
d65147f6 91 (mouse-minibuffer-check click)
b5370f03
JB
92 (let ((start (event-start click)))
93 (select-window (posn-window start))
85d6b80b 94 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
5ba2dc3f
JB
95 (first-line window-min-height)
96 (last-line (- (window-height) window-min-height)))
97 (if (< last-line first-line)
0a50b993 98 (error "Window too short to split")
5ba2dc3f
JB
99 (split-window-vertically
100 (min (max new-height first-line) last-line))))))
cc0a8174 101
947da0c4
RS
102(defun mouse-split-window-horizontally (click)
103 "Select Emacs window mouse is on, then split it horizontally in half.
104The window is split at the column clicked on.
105This command must be bound to a mouse click."
106 (interactive "@e")
d65147f6 107 (mouse-minibuffer-check click)
5ba2dc3f
JB
108 (let ((start (event-start click)))
109 (select-window (posn-window start))
110 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
111 (first-col window-min-width)
112 (last-col (- (window-width) window-min-width)))
113 (if (< last-col first-col)
0a50b993 114 (error "Window too narrow to split")
5ba2dc3f
JB
115 (split-window-horizontally
116 (min (max new-width first-col) last-col))))))
947da0c4 117
544e7e73
RS
118(defun mouse-drag-mode-line (start-event)
119 "Change the height of a window by dragging on the mode line."
120 (interactive "e")
121 (let ((done nil)
122 (echo-keystrokes 0)
123 (start-event-frame (window-frame (car (car (cdr start-event)))))
124 (start-event-window (car (car (cdr start-event))))
125 (start-nwindows (count-windows t))
126 (old-selected-window (selected-window))
127 should-enlarge-minibuffer
128 event mouse minibuffer y top bot edges wconfig params growth)
129 (setq params (frame-parameters))
130 (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
131 (one-window-p t))
132 (error "Attempt to resize sole window"))
133 (track-mouse
134 (progn
135 ;; enlarge-window only works on the selected window, so
136 ;; we must select the window where the start event originated.
137 ;; unwind-protect will restore the old selected window later.
138 (select-window start-event-window)
139 ;; if this is the bottommost ordinary window, then to
140 ;; move its modeline the minibuffer must be enlarged.
141 (setq should-enlarge-minibuffer
142 (and minibuffer
143 (not (one-window-p t))
144 (= (nth 1 (window-edges minibuffer))
145 (nth 3 (window-edges)))))
146 ;; loop reading events and sampling the position of
147 ;; the mouse.
148 (while (not done)
149 (setq event (read-event)
150 mouse (mouse-position))
151 ;; do nothing if
152 ;; - there is a switch-frame event.
153 ;; - the mouse isn't in the frame that we started in
154 ;; - the mouse isn't in any Emacs frame
155 ;; drag if
156 ;; - there is a mouse-movement event
157 ;; - there is a scroll-bar-movement event
158 ;; (same as mouse movement for our purposes)
159 ;; quit if
160 ;; - there is a keyboard event or some other unknown event
161 ;; unknown event.
162 (cond ((integerp event)
163 (setq done t))
164 ((eq (car event) 'switch-frame)
165 nil)
166 ((not (memq (car event)
167 '(mouse-movement scroll-bar-movement)))
168 (if (consp event)
169 (setq unread-command-events
170 (cons event unread-command-events)))
171 (setq done t))
172 ((not (eq (car mouse) start-event-frame))
173 nil)
174 ((null (car (cdr mouse)))
175 nil)
176 (t
177 (setq y (cdr (cdr mouse))
178 edges (window-edges)
179 top (nth 1 edges)
180 bot (nth 3 edges))
181 ;; scale back a move that would make the
182 ;; window too short.
183 (cond ((< (- y top -1) window-min-height)
184 (setq y (+ top window-min-height -1))))
185 ;; compute size change needed
186 (setq growth (- y bot -1)
187 wconfig (current-window-configuration))
188 ;; grow/shrink minibuffer?
189 (if should-enlarge-minibuffer
190 (progn
191 ;; yes. briefly select minibuffer so
192 ;; enlarge-window will affect the
193 ;; correct window.
194 (select-window minibuffer)
195 ;; scale back shrinkage if it would
196 ;; make the minibuffer less than 1
197 ;; line tall.
198 (if (and (> growth 0)
199 (< (- (window-height minibuffer)
200 growth)
201 1))
202 (setq growth (1- (window-height minibuffer))))
203 (enlarge-window (- growth))
204 (select-window start-event-window))
205 ;; no. grow/shrink the selected window
206 (enlarge-window growth))
207 ;; if this window's growth caused another
208 ;; window to be deleted because it was too
209 ;; short, rescind the change.
210 ;;
211 ;; if size change caused space to be stolen
212 ;; from a window above this one, rescind the
213 ;; change, but only if we didn't grow/srhink
214 ;; the minibuffer. minibuffer size changes
215 ;; can cause all windows to shrink... no way
216 ;; around it.
217 (if (or (/= start-nwindows (count-windows t))
218 (and (not should-enlarge-minibuffer)
219 (/= top (nth 1 (window-edges)))))
220 (set-window-configuration wconfig)))))))))
221\f
2a5fa27b 222(defun mouse-set-point (event)
cc0a8174 223 "Move point to the position clicked on with the mouse.
2a5fa27b 224This should be bound to a mouse click event type."
ec558adc 225 (interactive "e")
d65147f6 226 (mouse-minibuffer-check event)
2a5fa27b
RS
227 ;; Use event-end in case called from mouse-drag-region.
228 ;; If EVENT is a click, event-end and event-start give same value.
229 (let ((posn (event-end event)))
0a50b993
RS
230 (if (not (windowp (posn-window posn)))
231 (error "Cursor not in text area of window"))
b5370f03
JB
232 (select-window (posn-window posn))
233 (if (numberp (posn-point posn))
234 (goto-char (posn-point posn)))))
cc0a8174 235
652ccd35 236(defun mouse-set-region (click)
e37de120 237 "Set the region to the text dragged over, and copy to kill ring.
2a5fa27b 238This should be bound to a mouse drag event."
652ccd35 239 (interactive "e")
d65147f6 240 (mouse-minibuffer-check click)
652ccd35
RS
241 (let ((posn (event-start click))
242 (end (event-end click)))
243 (select-window (posn-window posn))
244 (if (numberp (posn-point posn))
245 (goto-char (posn-point posn)))
fcfc3c63
RS
246 ;; If mark is highlighted, no need to bounce the cursor.
247 (or (and transient-mark-mode
248 (eq (framep (selected-frame)) 'x))
249 (sit-for 1))
652ccd35 250 (push-mark)
1cc8a3f4 251 (set-mark (point))
652ccd35 252 (if (numberp (posn-point end))
e37de120
RS
253 (goto-char (posn-point end)))
254 ;; Don't set this-command to kill-region, so that a following
255 ;; C-w will not double the text in the kill ring.
256 (let (this-command)
257 (copy-region-as-kill (mark) (point)))))
652ccd35 258
600c6e3a
JB
259(defvar mouse-scroll-delay 0.25
260 "*The pause between scroll steps caused by mouse drags, in seconds.
261If you drag the mouse beyond the edge of a window, Emacs scrolls the
262window to bring the text beyond that edge into view, with a delay of
263this many seconds between scroll steps. Scrolling stops when you move
264the mouse back into the window, or release the button.
265This variable's value may be non-integral.
266Setting this to zero causes Emacs to scroll as fast as it can.")
267
e919a622
RS
268(defun mouse-scroll-subr (window jump &optional overlay start)
269 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
600c6e3a
JB
270If OVERLAY is an overlay, let it stretch from START to the far edge of
271the newly visible text.
272Upon exit, point is at the far edge of the newly visible text."
4e399a53
RS
273 (let ((opoint (point)))
274 (while (progn
275 (goto-char (window-start window))
276 (if (not (zerop (vertical-motion jump window)))
277 (progn
278 (set-window-start window (point))
279 (if (natnump jump)
280 (progn
281 (goto-char (window-end window))
282 ;; window-end doesn't reflect the window's new
283 ;; start position until the next redisplay. Hurrah.
284 (vertical-motion (1- jump) window))
285 (goto-char (window-start window)))
286 (if overlay
287 (move-overlay overlay start (point)))
288 ;; Now that we have scrolled WINDOW properly,
289 ;; put point back where it was for the redisplay
290 ;; so that we don't mess up the selected window.
291 (or (eq window (selected-window))
292 (goto-char opoint))
d2287ded 293 (sit-for mouse-scroll-delay)))))
4e399a53
RS
294 (or (eq window (selected-window))
295 (goto-char opoint))))
fcfc3c63 296
600c6e3a
JB
297(defvar mouse-drag-overlay (make-overlay 1 1))
298(overlay-put mouse-drag-overlay 'face 'region)
299
dd524dbd 300(defvar mouse-selection-click-count 0)
eb6ff46f 301
600c6e3a 302(defun mouse-drag-region (start-event)
bcd5aef1 303 "Set the region to the text that the mouse is dragged over.
78210c95
RS
304Highlight the drag area as you move the mouse.
305This must be bound to a button-down mouse event.
306In Transient Mark mode, the highlighting remains once you
307release the mouse button. Otherwise, it does not."
bcd5aef1 308 (interactive "e")
d65147f6 309 (mouse-minibuffer-check start-event)
600c6e3a
JB
310 (let* ((start-posn (event-start start-event))
311 (start-point (posn-point start-posn))
312 (start-window (posn-window start-posn))
b846d039 313 (start-frame (window-frame start-window))
600c6e3a
JB
314 (bounds (window-edges start-window))
315 (top (nth 1 bounds))
316 (bottom (if (window-minibuffer-p start-window)
317 (nth 3 bounds)
318 ;; Don't count the mode line.
e37de120
RS
319 (1- (nth 3 bounds))))
320 (click-count (1- (event-click-count start-event))))
eb6ff46f 321 (setq mouse-selection-click-count click-count)
b80f1928 322 (mouse-set-point start-event)
e37de120
RS
323 (let ((range (mouse-start-end start-point start-point click-count)))
324 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
325 (window-buffer start-window)))
f767385c 326 (deactivate-mark)
600c6e3a 327 (let (event end end-point)
bcd5aef1 328 (track-mouse
600c6e3a 329 (while (progn
b846d039
JB
330 (setq event (read-event))
331 (or (mouse-movement-p event)
332 (eq (car-safe event) 'switch-frame)))
b846d039
JB
333 (if (eq (car-safe event) 'switch-frame)
334 nil
335 (setq end (event-end event)
336 end-point (posn-point end))
337
338 (cond
b846d039
JB
339 ;; Are we moving within the original window?
340 ((and (eq (posn-window end) start-window)
341 (integer-or-marker-p end-point))
342 (goto-char end-point)
e37de120
RS
343 (let ((range (mouse-start-end start-point (point) click-count)))
344 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
b846d039 345
50e527bc
KH
346 (t
347 (let ((mouse-row (cdr (cdr (mouse-position)))))
b846d039 348 (cond
50e527bc 349 ((null mouse-row))
b846d039 350 ((< mouse-row top)
e919a622
RS
351 (mouse-scroll-subr start-window (- mouse-row top)
352 mouse-drag-overlay start-point))
d2287ded 353 ((>= mouse-row bottom)
e919a622 354 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
50e527bc 355 mouse-drag-overlay start-point)))))))))
4e399a53
RS
356 (if (consp event)
357;;; When we scroll into the mode line or menu bar, or out of the window,
358;;; we get events that don't fit these criteria.
359;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
360;;; (eq (posn-window (event-end event)) start-window)
361;;; (numberp (posn-point (event-end event)))
e37de120 362 (let ((fun (key-binding (vector (car event)))))
4e399a53
RS
363 (if (not (= (overlay-start mouse-drag-overlay)
364 (overlay-end mouse-drag-overlay)))
6452d8a6 365 (let (last-command this-command)
4e399a53 366 (push-mark (overlay-start mouse-drag-overlay) t t)
3544cfc9 367 (goto-char (overlay-end mouse-drag-overlay))
4e399a53
RS
368 (copy-region-as-kill (point) (mark t)))
369 (goto-char (overlay-end mouse-drag-overlay))
370 (setq this-command 'mouse-set-point))))
600c6e3a 371 (delete-overlay mouse-drag-overlay))))
e37de120
RS
372\f
373;; Commands to handle xterm-style multiple clicks.
600c6e3a 374
e37de120
RS
375(defun mouse-skip-word (dir)
376 "Skip over word, over whitespace, or over identical punctuation.
377If DIR is positive skip forward; if negative, skip backward."
378 (let* ((char (following-char))
379 (syntax (char-to-string (char-syntax char))))
380 (if (or (string= syntax "w") (string= syntax " "))
381 (if (< dir 0)
382 (skip-syntax-backward syntax)
383 (skip-syntax-forward syntax))
384 (if (< dir 0)
d89a4a47 385 (while (and (not (bobp)) (= (preceding-char) char))
e37de120 386 (forward-char -1))
d89a4a47 387 (while (and (not (eobp)) (= (following-char) char))
e37de120
RS
388 (forward-char 1))))))
389
390;; Return a list of region bounds based on START and END according to MODE.
391;; If MODE is 0 then set point to (min START END), mark to (max START END).
392;; If MODE is 1 then set point to start of word at (min START END),
393;; mark to end of word at (max START END).
394;; If MODE is 2 then do the same for lines.
eb6ff46f 395(defun mouse-start-end (start end mode)
e37de120
RS
396 (if (> start end)
397 (let ((temp start))
398 (setq start end
399 end temp)))
9a974c88 400 (setq mode (mod mode 3))
e37de120
RS
401 (cond ((= mode 0)
402 (list start end))
403 ((and (= mode 1)
404 (= start end)
1ec71583 405 (char-after start)
e37de120 406 (= (char-syntax (char-after start)) ?\())
6f482eec
RS
407 (list start
408 (save-excursion
409 (goto-char start)
410 (forward-sexp 1)
411 (point))))
e37de120
RS
412 ((and (= mode 1)
413 (= start end)
1ec71583 414 (char-after start)
e37de120
RS
415 (= (char-syntax (char-after start)) ?\)))
416 (list (save-excursion
417 (goto-char (1+ start))
d89a4a47
RS
418 (backward-sexp 1)
419 (point))
e37de120
RS
420 (1+ start)))
421 ((= mode 1)
422 (list (save-excursion
423 (goto-char start)
424 (mouse-skip-word -1)
425 (point))
426 (save-excursion
427 (goto-char end)
428 (mouse-skip-word 1)
429 (point))))
430 ((= mode 2)
431 (list (save-excursion
432 (goto-char start)
433 (beginning-of-line 1)
434 (point))
435 (save-excursion
436 (goto-char end)
437 (forward-line 1)
438 (point))))))
e66feb07 439\f
3f26b32a
RS
440;; Subroutine: set the mark where CLICK happened,
441;; but don't do anything else.
442(defun mouse-set-mark-fast (click)
d65147f6 443 (mouse-minibuffer-check click)
3f26b32a
RS
444 (let ((posn (event-start click)))
445 (select-window (posn-window posn))
446 (if (numberp (posn-point posn))
447 (push-mark (posn-point posn) t t))))
448
449;; Momentarily show where the mark is, if highlighting doesn't show it.
450(defun mouse-show-mark ()
451 (or transient-mark-mode
452 (save-excursion
453 (goto-char (mark t))
454 (sit-for 1))))
455
cc0a8174
JB
456(defun mouse-set-mark (click)
457 "Set mark at the position clicked on with the mouse.
458Display cursor at that position for a second.
459This must be bound to a mouse click."
ec558adc 460 (interactive "e")
72ea54a4
RS
461 (let ((point-save (point)))
462 (unwind-protect
cc0a8174 463 (progn (mouse-set-point click)
897897e3
RS
464 (push-mark nil t t)
465 (or transient-mark-mode
466 (sit-for 1)))
72ea54a4
RS
467 (goto-char point-save))))
468
cc0a8174
JB
469(defun mouse-kill (click)
470 "Kill the region between point and the mouse click.
471The text is saved in the kill ring, as with \\[kill-region]."
ec558adc 472 (interactive "e")
d65147f6 473 (mouse-minibuffer-check click)
142c7672
KH
474 (let* ((posn (event-start click))
475 (click-posn (posn-point posn)))
476 (select-window (posn-window posn))
bd307392
JB
477 (if (numberp click-posn)
478 (kill-region (min (point) click-posn)
479 (max (point) click-posn)))))
72ea54a4 480
87ef29fd
JB
481(defun mouse-yank-at-click (click arg)
482 "Insert the last stretch of killed text at the position clicked on.
50f58001
RS
483Also move point to one end of the text thus inserted (normally the end).
484Prefix arguments are interpreted as with \\[yank].
485If `mouse-yank-at-point' is non-nil, insert at point
486regardless of where you click."
ec558adc 487 (interactive "e\nP")
50f58001 488 (or mouse-yank-at-point (mouse-set-point click))
d89a4a47 489 (setq this-command 'yank)
87ef29fd
JB
490 (yank arg))
491
492(defun mouse-kill-ring-save (click)
cc0a8174
JB
493 "Copy the region between point and the mouse click in the kill ring.
494This does not delete the region; it acts like \\[kill-ring-save]."
ec558adc 495 (interactive "e")
3f26b32a 496 (mouse-set-mark-fast click)
6452d8a6
RS
497 (let (this-command last-command)
498 (kill-ring-save (point) (mark t)))
3f26b32a 499 (mouse-show-mark))
72ea54a4 500
dbc4e1c1
JB
501;;; This function used to delete the text between point and the mouse
502;;; whenever it was equal to the front of the kill ring, but some
503;;; people found that confusing.
504
505;;; A list (TEXT START END), describing the text and position of the last
506;;; invocation of mouse-save-then-kill.
507(defvar mouse-save-then-kill-posn nil)
508
26d280b9 509(defun mouse-save-then-kill-delete-region (beg end)
9a974c88
RS
510 ;; We must make our own undo boundaries
511 ;; because they happen automatically only for the current buffer.
512 (undo-boundary)
dd524dbd
RS
513 (if (or (= beg end) (eq buffer-undo-list t))
514 ;; If we have no undo list in this buffer,
515 ;; just delete.
516 (delete-region beg end)
517 ;; Delete, but make the undo-list entry share with the kill ring.
518 ;; First, delete just one char, so in case buffer is being modified
519 ;; for the first time, the undo list records that fact.
2f4b15ef
RS
520 (let (before-change-function after-change-function
521 before-change-functions after-change-functions)
522 (delete-region beg
523 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
524 (let ((buffer-undo-list buffer-undo-list))
525 ;; Undo that deletion--but don't change the undo list!
2f4b15ef
RS
526 (let (before-change-function after-change-function
527 before-change-functions after-change-functions)
528 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
529 ;; Now delete the rest of the specified region,
530 ;; but don't record it.
531 (setq buffer-undo-list t)
9a974c88
RS
532 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
533 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
534 (delete-region beg end))
535 (let ((tail buffer-undo-list))
536 ;; Search back in buffer-undo-list for the string
537 ;; that came from deleting one character.
538 (while (and tail (not (stringp (car (car tail)))))
539 (setq tail (cdr tail)))
540 ;; Replace it with an entry for the entire deleted text.
541 (and tail
9a974c88
RS
542 (setcar tail (cons (car kill-ring) (min beg end))))))
543 (undo-boundary))
eb6ff46f 544
947da0c4 545(defun mouse-save-then-kill (click)
40a45a9f
RS
546 "Save text to point in kill ring; the second time, kill the text.
547If the text between point and the mouse is the same as what's
548at the front of the kill ring, this deletes the text.
549Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
eb6ff46f
RS
550which prepares for a second click to delete the text.
551
552If you have selected words or lines, this command extends the
553selection through the word or line clicked on. If you do this
554again in a different position, it extends the selection again.
555If you do this twice in the same position, the selection is killed."
947da0c4 556 (interactive "e")
d65147f6 557 (mouse-minibuffer-check click)
3f26b32a
RS
558 (let ((click-posn (posn-point (event-start click)))
559 ;; Don't let a subsequent kill command append to this one:
560 ;; prevent setting this-command to kill-region.
561 (this-command this-command))
9a974c88 562 (if (> (mod mouse-selection-click-count 3) 0)
eb6ff46f
RS
563 (if (not (and (eq last-command 'mouse-save-then-kill)
564 (equal click-posn
565 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
d89a4a47
RS
566 ;; Find both ends of the object selected by this click.
567 (let* ((range
568 (mouse-start-end click-posn click-posn
569 mouse-selection-click-count)))
570 ;; Move whichever end is closer to the click.
571 ;; That's what xterm does, and it seems reasonable.
572 (if (< (abs (- click-posn (mark t)))
573 (abs (- click-posn (point))))
574 (set-mark (car range))
575 (goto-char (nth 1 range)))
eb6ff46f
RS
576 ;; We have already put the old region in the kill ring.
577 ;; Replace it with the extended region.
578 ;; (It would be annoying to make a separate entry.)
579 (setcar kill-ring (buffer-substring (point) (mark t)))
d89a4a47
RS
580 (if interprogram-cut-function
581 (funcall interprogram-cut-function (car kill-ring)))
582 ;; Arrange for a repeated mouse-3 to kill this region.
583 (setq mouse-save-then-kill-posn
584 (list (car kill-ring) (point) click-posn))
eb6ff46f
RS
585 (mouse-show-mark))
586 ;; If we click this button again without moving it,
587 ;; that time kill.
dd524dbd
RS
588 (mouse-save-then-kill-delete-region (point) (mark))
589 (setq mouse-selection-click-count 0)
590 (setq mouse-save-then-kill-posn nil))
eb6ff46f
RS
591 (if (and (eq last-command 'mouse-save-then-kill)
592 mouse-save-then-kill-posn
593 (eq (car mouse-save-then-kill-posn) (car kill-ring))
594 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
595 ;; If this is the second time we've called
596 ;; mouse-save-then-kill, delete the text from the buffer.
dd524dbd
RS
597 (progn
598 (mouse-save-then-kill-delete-region (point) (mark))
599 ;; After we kill, another click counts as "the first time".
600 (setq mouse-save-then-kill-posn nil))
601 (if (or (and (eq last-command 'mouse-save-then-kill)
602 mouse-save-then-kill-posn)
3544cfc9 603 (and mark-active transient-mark-mode)
d89a4a47 604 (and (eq last-command 'mouse-drag-region)
d89a4a47
RS
605 (or mark-even-if-inactive
606 (not transient-mark-mode))))
607 ;; We have a selection or suitable region, so adjust it.
608 (let* ((posn (event-start click))
609 (new (posn-point posn)))
610 (select-window (posn-window posn))
611 (if (numberp new)
612 (progn
613 ;; Move whichever end of the region is closer to the click.
614 ;; That is what xterm does, and it seems reasonable.
615 (if (< (abs (- new (point))) (abs (- new (mark t))))
616 (goto-char new)
617 (set-mark new))
618 (setq deactivate-mark nil)))
619 (setcar kill-ring (buffer-substring (point) (mark t)))
620 (if interprogram-cut-function
621 (funcall interprogram-cut-function (car kill-ring))))
622 ;; We just have point, so set mark here.
623 (mouse-set-mark-fast click)
624 (kill-ring-save (point) (mark t))
625 (mouse-show-mark))
eb6ff46f
RS
626 (setq mouse-save-then-kill-posn
627 (list (car kill-ring) (point) click-posn))))))
e66feb07
RS
628\f
629(global-set-key [M-mouse-1] 'mouse-start-secondary)
630(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
631(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
632(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 633(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07
RS
634
635;; An overlay which records the current secondary selection
636;; or else is deleted when there is no secondary selection.
637;; May be nil.
638(defvar mouse-secondary-overlay nil)
639
640;; A marker which records the specified first end for a secondary selection.
641;; May be nil.
642(defvar mouse-secondary-start nil)
643
644(defun mouse-start-secondary (click)
645 "Set one end of the secondary selection to the position clicked on.
646Use \\[mouse-secondary-save-then-kill] to set the other end
647and complete the secondary selection."
648 (interactive "e")
d65147f6 649 (mouse-minibuffer-check click)
e66feb07 650 (let ((posn (event-start click)))
230aaa73
RS
651 (save-excursion
652 (set-buffer (window-buffer (posn-window posn)))
653 ;; Cancel any preexisting secondary selection.
654 (if mouse-secondary-overlay
655 (delete-overlay mouse-secondary-overlay))
656 (if (numberp (posn-point posn))
657 (progn
658 (or mouse-secondary-start
659 (setq mouse-secondary-start (make-marker)))
660 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
661
662(defun mouse-set-secondary (click)
663 "Set the secondary selection to the text that the mouse is dragged over.
664This must be bound to a mouse drag event."
665 (interactive "e")
d65147f6 666 (mouse-minibuffer-check click)
e66feb07
RS
667 (let ((posn (event-start click))
668 beg
669 (end (event-end click)))
230aaa73
RS
670 (save-excursion
671 (set-buffer (window-buffer (posn-window posn)))
672 (if (numberp (posn-point posn))
673 (setq beg (posn-point posn)))
674 (if mouse-secondary-overlay
675 (move-overlay mouse-secondary-overlay beg (posn-point end))
676 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
677 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
947da0c4 678
d89a4a47 679(defun mouse-drag-secondary (start-event)
e66feb07 680 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 681Highlight the drag area as you move the mouse.
e66feb07
RS
682This must be bound to a button-down mouse event."
683 (interactive "e")
d65147f6 684 (mouse-minibuffer-check start-event)
d89a4a47
RS
685 (let* ((start-posn (event-start start-event))
686 (start-point (posn-point start-posn))
687 (start-window (posn-window start-posn))
688 (start-frame (window-frame start-window))
689 (bounds (window-edges start-window))
690 (top (nth 1 bounds))
691 (bottom (if (window-minibuffer-p start-window)
692 (nth 3 bounds)
693 ;; Don't count the mode line.
694 (1- (nth 3 bounds))))
695 (click-count (1- (event-click-count start-event))))
696 (save-excursion
697 (set-buffer (window-buffer start-window))
698 (setq mouse-selection-click-count click-count)
d89a4a47
RS
699 (or mouse-secondary-overlay
700 (setq mouse-secondary-overlay
701 (make-overlay (point) (point))))
26d280b9 702 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
9a974c88 703 (if (> (mod click-count 3) 0)
26d280b9
RS
704 ;; Double or triple press: make an initial selection
705 ;; of one word or line.
d89a4a47
RS
706 (let ((range (mouse-start-end start-point start-point click-count)))
707 (set-marker mouse-secondary-start nil)
708 (move-overlay mouse-secondary-overlay 1 1
709 (window-buffer start-window))
710 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
711 (window-buffer start-window)))
26d280b9 712 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
713 (or mouse-secondary-start
714 (setq mouse-secondary-start (make-marker)))
715 (set-marker mouse-secondary-start start-point)
716 (delete-overlay mouse-secondary-overlay))
717 (let (event end end-point)
718 (track-mouse
719 (while (progn
720 (setq event (read-event))
721 (or (mouse-movement-p event)
722 (eq (car-safe event) 'switch-frame)))
723
724 (if (eq (car-safe event) 'switch-frame)
725 nil
726 (setq end (event-end event)
727 end-point (posn-point end))
728 (cond
d89a4a47
RS
729 ;; Are we moving within the original window?
730 ((and (eq (posn-window end) start-window)
731 (integer-or-marker-p end-point))
d89a4a47
RS
732 (let ((range (mouse-start-end start-point end-point
733 click-count)))
0136e1e3
RS
734 (if (or (/= start-point end-point)
735 (null (marker-position mouse-secondary-start)))
736 (progn
737 (set-marker mouse-secondary-start nil)
738 (move-overlay mouse-secondary-overlay
739 (car range) (nth 1 range))))))
3b0aebe9
RS
740 (t
741 (let ((mouse-row (cdr (cdr (mouse-position)))))
742 (cond
743 ((null mouse-row))
744 ((< mouse-row top)
e919a622
RS
745 (mouse-scroll-subr start-window (- mouse-row top)
746 mouse-secondary-overlay start-point))
d2287ded 747 ((>= mouse-row bottom)
e919a622 748 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 749 mouse-secondary-overlay start-point)))))))))
d89a4a47 750
4e399a53
RS
751 (if (consp event)
752;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
753;;; (eq (posn-window (event-end event)) start-window)
754;;; (numberp (posn-point (event-end event)))
d89a4a47
RS
755 (if (marker-position mouse-secondary-start)
756 (save-window-excursion
757 (delete-overlay mouse-secondary-overlay)
9a974c88 758 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
759 (select-window start-window)
760 (save-excursion
761 (goto-char mouse-secondary-start)
762 (sit-for 1)))
9a974c88
RS
763 (x-set-selection
764 'SECONDARY
765 (buffer-substring (overlay-start mouse-secondary-overlay)
766 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 767
9a974c88 768(defun mouse-yank-secondary (click)
50f58001
RS
769 "Insert the secondary selection at the position clicked on.
770Move point to the end of the inserted text.
771If `mouse-yank-at-point' is non-nil, insert at point
772regardless of where you click."
9a974c88 773 (interactive "e")
50f58001
RS
774 (or mouse-yank-at-point (mouse-set-point click))
775 (insert (x-get-selection 'SECONDARY)))
9a974c88 776
7e6404f6 777(defun mouse-kill-secondary ()
9a974c88
RS
778 "Kill the text in the secondary selection.
779This is intended more as a keyboard command than as a mouse command
780but it can work as either one.
781
782The current buffer (in case of keyboard use), or the buffer clicked on,
783must be the one that the secondary selection is in. This requirement
784is to prevent accidents."
7e6404f6
RS
785 (interactive)
786 (let* ((keys (this-command-keys))
787 (click (elt keys (1- (length keys)))))
788 (or (eq (overlay-buffer mouse-secondary-overlay)
789 (if (listp click)
790 (window-buffer (posn-window (event-start click)))
791 (current-buffer)))
792 (error "Select or click on the buffer where the secondary selection is")))
9a974c88
RS
793 (save-excursion
794 (set-buffer (overlay-buffer mouse-secondary-overlay))
795 (kill-region (overlay-start mouse-secondary-overlay)
796 (overlay-end mouse-secondary-overlay)))
e66feb07 797 (delete-overlay mouse-secondary-overlay)
9a974c88 798 (x-set-selection 'SECONDARY nil)
e66feb07
RS
799 (setq mouse-secondary-overlay nil))
800
801(defun mouse-secondary-save-then-kill (click)
d89a4a47 802 "Save text to point in kill ring; the second time, kill the text.
7bbe2cc7
RS
803You must use this in a buffer where you have recently done \\[mouse-start-secondary].
804If the text between where you did \\[mouse-start-secondary] and where
805you use this command matches the text at the front of the kill ring,
806this command deletes the text.
e66feb07 807Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
7bbe2cc7 808which prepares for a second click with this command to delete the text.
d89a4a47 809
7bbe2cc7
RS
810If you have already made a secondary selection in that buffer,
811this command extends or retracts the selection to where you click.
812If you do this again in a different position, it extends or retracts
813again. If you do this twice in the same position, it kills the selection."
e66feb07 814 (interactive "e")
d65147f6 815 (mouse-minibuffer-check click)
d89a4a47
RS
816 (let ((posn (event-start click))
817 (click-posn (posn-point (event-start click)))
e66feb07
RS
818 ;; Don't let a subsequent kill command append to this one:
819 ;; prevent setting this-command to kill-region.
820 (this-command this-command))
9a974c88
RS
821 (or (eq (window-buffer (posn-window posn))
822 (or (and mouse-secondary-overlay
823 (overlay-buffer mouse-secondary-overlay))
824 (if mouse-secondary-start
825 (marker-buffer mouse-secondary-start))))
826 (error "Wrong buffer"))
827 (save-excursion
828 (set-buffer (window-buffer (posn-window posn)))
829 (if (> (mod mouse-selection-click-count 3) 0)
830 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
831 (equal click-posn
832 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
833 ;; Find both ends of the object selected by this click.
834 (let* ((range
835 (mouse-start-end click-posn click-posn
836 mouse-selection-click-count)))
837 ;; Move whichever end is closer to the click.
838 ;; That's what xterm does, and it seems reasonable.
839 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
840 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
841 (move-overlay mouse-secondary-overlay (car range)
842 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
843 (move-overlay mouse-secondary-overlay
844 (overlay-start mouse-secondary-overlay)
845 (nth 1 range)))
9a974c88
RS
846 ;; We have already put the old region in the kill ring.
847 ;; Replace it with the extended region.
848 ;; (It would be annoying to make a separate entry.)
849 (setcar kill-ring (buffer-substring
850 (overlay-start mouse-secondary-overlay)
851 (overlay-end mouse-secondary-overlay)))
852 (if interprogram-cut-function
853 (funcall interprogram-cut-function (car kill-ring)))
854 ;; Arrange for a repeated mouse-3 to kill this region.
855 (setq mouse-save-then-kill-posn
856 (list (car kill-ring) (point) click-posn)))
857 ;; If we click this button again without moving it,
858 ;; that time kill.
d89a4a47 859 (progn
9a974c88
RS
860 (mouse-save-then-kill-delete-region
861 (overlay-start mouse-secondary-overlay)
862 (overlay-end mouse-secondary-overlay))
863 (setq mouse-save-then-kill-posn nil)
864 (setq mouse-selection-click-count 0)
865 (delete-overlay mouse-secondary-overlay)))
866 (if (and (eq last-command 'mouse-secondary-save-then-kill)
867 mouse-save-then-kill-posn
868 (eq (car mouse-save-then-kill-posn) (car kill-ring))
869 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
870 ;; If this is the second time we've called
871 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
872 (progn
873 (mouse-save-then-kill-delete-region
874 (overlay-start mouse-secondary-overlay)
875 (overlay-end mouse-secondary-overlay))
876 (setq mouse-save-then-kill-posn nil)
877 (delete-overlay mouse-secondary-overlay))
878 (if (overlay-start mouse-secondary-overlay)
879 ;; We have a selection, so adjust it.
880 (progn
881 (if (numberp click-posn)
882 (progn
883 ;; Move whichever end of the region is closer to the click.
884 ;; That is what xterm does, and it seems reasonable.
885 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
886 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
887 (move-overlay mouse-secondary-overlay click-posn
888 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
889 (move-overlay mouse-secondary-overlay
890 (overlay-start mouse-secondary-overlay)
891 click-posn))
9a974c88 892 (setq deactivate-mark nil)))
0136e1e3
RS
893 (if (eq last-command 'mouse-secondary-save-then-kill)
894 (progn
895 ;; If the front of the kill ring comes from
896 ;; an immediately previous use of this command,
897 ;; replace it with the extended region.
898 ;; (It would be annoying to make a separate entry.)
899 (setcar kill-ring
900 (buffer-substring
901 (overlay-start mouse-secondary-overlay)
902 (overlay-end mouse-secondary-overlay)))
903 (if interprogram-cut-function
904 (funcall interprogram-cut-function (car kill-ring))))
905 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
906 (overlay-end mouse-secondary-overlay))))
9a974c88
RS
907 (if mouse-secondary-start
908 ;; All we have is one end of a selection,
909 ;; so put the other end here.
910 (let ((start (+ 0 mouse-secondary-start)))
911 (kill-ring-save start click-posn)
912 (if mouse-secondary-overlay
913 (move-overlay mouse-secondary-overlay start click-posn)
914 (setq mouse-secondary-overlay (make-overlay start click-posn)))
915 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
916 (setq mouse-save-then-kill-posn
917 (list (car kill-ring) (point) click-posn))))
918 (x-set-selection 'SECONDARY
919 (if (overlay-buffer mouse-secondary-overlay)
920 (buffer-substring
921 (overlay-start mouse-secondary-overlay)
922 (overlay-end mouse-secondary-overlay)))))))
e66feb07 923\f
8b34e79d 924(defun mouse-buffer-menu (event)
2d82f7b9
RS
925 "Pop up a menu of buffers for selection with the mouse.
926This switches buffers in the window that you clicked on,
927and selects that window."
ec558adc 928 (interactive "e")
d65147f6 929 (mouse-minibuffer-check event)
8b34e79d
RS
930 (let ((menu
931 (list "Buffer Menu"
932 (cons "Select Buffer"
933 (let ((tail (buffer-list))
af2a85fe 934 (maxbuf 0)
8b34e79d 935 head)
af2a85fe
RS
936 (while tail
937 (or (eq ?\ (aref (buffer-name (car tail)) 0))
938 (setq maxbuf
939 (max maxbuf
940 (length (buffer-name (car tail))))))
941 (setq tail (cdr tail)))
942 (setq tail (buffer-list))
8b34e79d
RS
943 (while tail
944 (let ((elt (car tail)))
945 (if (not (string-match "^ "
946 (buffer-name elt)))
947 (setq head (cons
948 (cons
949 (format
af2a85fe
RS
950 (format "%%%ds %%s%%s %%s"
951 maxbuf)
8b34e79d 952 (buffer-name elt)
af2a85fe
RS
953 (if (buffer-modified-p elt)
954 "*" " ")
955 (save-excursion
956 (set-buffer elt)
957 (if buffer-read-only "%" " "))
8b34e79d
RS
958 (or (buffer-file-name elt) ""))
959 elt)
960 head))))
961 (setq tail (cdr tail)))
962 (reverse head))))))
2d82f7b9
RS
963 (let ((buf (x-popup-menu event menu))
964 (window (posn-window (event-start event))))
965 (if buf
966 (progn
c0bb9f3b 967 (or (framep window) (select-window window))
2d82f7b9 968 (switch-to-buffer buf))))))
72ea54a4 969\f
5ba2dc3f 970;;; These need to be rewritten for the new scroll bar implementation.
dbc4e1c1
JB
971
972;;;!! ;; Commands for the scroll bar.
973;;;!!
974;;;!! (defun mouse-scroll-down (click)
975;;;!! (interactive "@e")
976;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
977;;;!!
978;;;!! (defun mouse-scroll-up (click)
979;;;!! (interactive "@e")
980;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
981;;;!!
982;;;!! (defun mouse-scroll-down-full ()
983;;;!! (interactive "@")
984;;;!! (scroll-down nil))
985;;;!!
986;;;!! (defun mouse-scroll-up-full ()
987;;;!! (interactive "@")
988;;;!! (scroll-up nil))
989;;;!!
990;;;!! (defun mouse-scroll-move-cursor (click)
991;;;!! (interactive "@e")
992;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
993;;;!!
994;;;!! (defun mouse-scroll-absolute (event)
995;;;!! (interactive "@e")
996;;;!! (let* ((pos (car event))
997;;;!! (position (car pos))
998;;;!! (length (car (cdr pos))))
999;;;!! (if (<= length 0) (setq length 1))
1000;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1001;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1002;;;!! position)
1003;;;!! length)
1004;;;!! scale-factor)))
1005;;;!! (goto-char newpos)
1006;;;!! (recenter '(4)))))
1007;;;!!
1008;;;!! (defun mouse-scroll-left (click)
1009;;;!! (interactive "@e")
1010;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1011;;;!!
1012;;;!! (defun mouse-scroll-right (click)
1013;;;!! (interactive "@e")
1014;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1015;;;!!
1016;;;!! (defun mouse-scroll-left-full ()
1017;;;!! (interactive "@")
1018;;;!! (scroll-left nil))
1019;;;!!
1020;;;!! (defun mouse-scroll-right-full ()
1021;;;!! (interactive "@")
1022;;;!! (scroll-right nil))
1023;;;!!
1024;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1025;;;!! (interactive "@e")
1026;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1027;;;!!
1028;;;!! (defun mouse-scroll-absolute-horizontally (event)
1029;;;!! (interactive "@e")
1030;;;!! (let* ((pos (car event))
1031;;;!! (position (car pos))
1032;;;!! (length (car (cdr pos))))
1033;;;!! (set-window-hscroll (selected-window) 33)))
1034;;;!!
1035;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1036;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1037;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1038;;;!!
1039;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1040;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1041;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1042;;;!!
1043;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1044;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1045;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1046;;;!!
1047;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1048;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1049;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1050;;;!!
1051;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1052;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1053;;;!! 'mouse-scroll-absolute-horizontally)
1054;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1055;;;!!
1056;;;!! (global-set-key [horizontal-slider mouse-1]
1057;;;!! 'mouse-scroll-move-cursor-horizontally)
1058;;;!! (global-set-key [horizontal-slider mouse-2]
1059;;;!! 'mouse-scroll-move-cursor-horizontally)
1060;;;!! (global-set-key [horizontal-slider mouse-3]
1061;;;!! 'mouse-scroll-move-cursor-horizontally)
1062;;;!!
1063;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1064;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1065;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1066;;;!!
1067;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1068;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1069;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1070;;;!!
1071;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1072;;;!! 'mouse-split-window-horizontally)
1073;;;!! (global-set-key [mode-line S-mouse-2]
1074;;;!! 'mouse-split-window-horizontally)
1075;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1076;;;!! 'mouse-split-window)
6b2154de 1077\f
dbc4e1c1
JB
1078;;;!! ;;;;
1079;;;!! ;;;; Here are experimental things being tested. Mouse events
1080;;;!! ;;;; are of the form:
1081;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1082;;;!! ;;
1083;;;!! ;;;;
1084;;;!! ;;;; Dynamically track mouse coordinates
1085;;;!! ;;;;
1086;;;!! ;;
1087;;;!! ;;(defun track-mouse (event)
1088;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1089;;;!! ;; (interactive "@e")
1090;;;!! ;; (while mouse-grabbed
1091;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1092;;;!! ;; (abs-x (car pos))
1093;;;!! ;; (abs-y (cdr pos))
1094;;;!! ;; (relative-coordinate (coordinates-in-window-p
1095;;;!! ;; (list (car pos) (cdr pos))
1096;;;!! ;; (selected-window))))
1097;;;!! ;; (if (consp relative-coordinate)
1098;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1099;;;!! ;; (car relative-coordinate)
1100;;;!! ;; (car (cdr relative-coordinate)))
1101;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1102;;;!!
1103;;;!! ;;
1104;;;!! ;; Dynamically put a box around the line indicated by point
1105;;;!! ;;
1106;;;!! ;;
1107;;;!! ;;(require 'backquote)
1108;;;!! ;;
1109;;;!! ;;(defun mouse-select-buffer-line (event)
1110;;;!! ;; (interactive "@e")
1111;;;!! ;; (let ((relative-coordinate
1112;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1113;;;!! ;; (abs-y (car (cdr (car event)))))
1114;;;!! ;; (if (consp relative-coordinate)
1115;;;!! ;; (progn
1116;;;!! ;; (save-excursion
1117;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1118;;;!! ;; (x-draw-rectangle
1119;;;!! ;; (selected-screen)
1120;;;!! ;; abs-y 0
1121;;;!! ;; (save-excursion
1122;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1123;;;!! ;; (end-of-line)
1124;;;!! ;; (push-mark nil t)
1125;;;!! ;; (beginning-of-line)
1126;;;!! ;; (- (region-end) (region-beginning))) 1))
1127;;;!! ;; (sit-for 1)
1128;;;!! ;; (x-erase-rectangle (selected-screen))))))
1129;;;!! ;;
1130;;;!! ;;(defvar last-line-drawn nil)
1131;;;!! ;;(defvar begin-delim "[^ \t]")
1132;;;!! ;;(defvar end-delim "[^ \t]")
1133;;;!! ;;
1134;;;!! ;;(defun mouse-boxing (event)
1135;;;!! ;; (interactive "@e")
1136;;;!! ;; (save-excursion
1137;;;!! ;; (let ((screen (selected-screen)))
1138;;;!! ;; (while (= (x-mouse-events) 0)
1139;;;!! ;; (let* ((pos (read-mouse-position screen))
1140;;;!! ;; (abs-x (car pos))
1141;;;!! ;; (abs-y (cdr pos))
1142;;;!! ;; (relative-coordinate
1143;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1144;;;!! ;; (selected-window)))
1145;;;!! ;; (begin-reg nil)
1146;;;!! ;; (end-reg nil)
1147;;;!! ;; (end-column nil)
1148;;;!! ;; (begin-column nil))
1149;;;!! ;; (if (and (consp relative-coordinate)
1150;;;!! ;; (or (not last-line-drawn)
1151;;;!! ;; (not (= last-line-drawn abs-y))))
1152;;;!! ;; (progn
1153;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1154;;;!! ;; (if (= (following-char) 10)
1155;;;!! ;; ()
1156;;;!! ;; (progn
1157;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1158;;;!! ;; (setq begin-column (1- (current-column)))
1159;;;!! ;; (end-of-line)
1160;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1161;;;!! ;; (setq end-column (1+ (current-column)))
1162;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1163;;;!! ;; (x-draw-rectangle screen
1164;;;!! ;; (setq last-line-drawn abs-y)
1165;;;!! ;; begin-column
1166;;;!! ;; (- end-column begin-column) 1))))))))))
1167;;;!! ;;
1168;;;!! ;;(defun mouse-erase-box ()
1169;;;!! ;; (interactive)
1170;;;!! ;; (if last-line-drawn
1171;;;!! ;; (progn
1172;;;!! ;; (x-erase-rectangle (selected-screen))
1173;;;!! ;; (setq last-line-drawn nil))))
1174;;;!!
1175;;;!! ;;; (defun test-x-rectangle ()
1176;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1177;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1178;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1179;;;!!
1180;;;!! ;;
1181;;;!! ;; Here is how to do double clicking in lisp. About to change.
1182;;;!! ;;
1183;;;!!
1184;;;!! (defvar double-start nil)
1185;;;!! (defconst double-click-interval 300
1186;;;!! "Max ticks between clicks")
1187;;;!!
1188;;;!! (defun double-down (event)
1189;;;!! (interactive "@e")
1190;;;!! (if double-start
1191;;;!! (let ((interval (- (nth 4 event) double-start)))
1192;;;!! (if (< interval double-click-interval)
1193;;;!! (progn
1194;;;!! (backward-up-list 1)
1195;;;!! ;; (message "Interval %d" interval)
1196;;;!! (sleep-for 1)))
1197;;;!! (setq double-start nil))
1198;;;!! (setq double-start (nth 4 event))))
1199;;;!!
1200;;;!! (defun double-up (event)
1201;;;!! (interactive "@e")
1202;;;!! (and double-start
1203;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1204;;;!! (setq double-start nil)))
1205;;;!!
1206;;;!! ;;; (defun x-test-doubleclick ()
1207;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1208;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1209;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1210;;;!!
1211;;;!! ;;
5ba2dc3f 1212;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
dbc4e1c1
JB
1213;;;!! ;;
1214;;;!!
1215;;;!! (defvar scrolled-lines 0)
1216;;;!! (defconst scroll-speed 1)
1217;;;!!
1218;;;!! (defun incr-scroll-down (event)
1219;;;!! (interactive "@e")
1220;;;!! (setq scrolled-lines 0)
1221;;;!! (incremental-scroll scroll-speed))
1222;;;!!
1223;;;!! (defun incr-scroll-up (event)
1224;;;!! (interactive "@e")
1225;;;!! (setq scrolled-lines 0)
1226;;;!! (incremental-scroll (- scroll-speed)))
1227;;;!!
1228;;;!! (defun incremental-scroll (n)
1229;;;!! (while (= (x-mouse-events) 0)
1230;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1231;;;!! (scroll-down n)
1232;;;!! (sit-for 300 t)))
1233;;;!!
1234;;;!! (defun incr-scroll-stop (event)
1235;;;!! (interactive "@e")
1236;;;!! (message "Scrolled %d lines" scrolled-lines)
1237;;;!! (setq scrolled-lines 0)
1238;;;!! (sleep-for 1))
1239;;;!!
1240;;;!! ;;; (defun x-testing-scroll ()
1241;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1242;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1243;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1244;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1245;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1246;;;!!
1247;;;!! ;;
1248;;;!! ;; Some playthings suitable for picture mode? They need work.
1249;;;!! ;;
1250;;;!!
1251;;;!! (defun mouse-kill-rectangle (event)
1252;;;!! "Kill the rectangle between point and the mouse cursor."
1253;;;!! (interactive "@e")
1254;;;!! (let ((point-save (point)))
1255;;;!! (save-excursion
1256;;;!! (mouse-set-point event)
1257;;;!! (push-mark nil t)
1258;;;!! (if (> point-save (point))
1259;;;!! (kill-rectangle (point) point-save)
1260;;;!! (kill-rectangle point-save (point))))))
1261;;;!!
1262;;;!! (defun mouse-open-rectangle (event)
1263;;;!! "Kill the rectangle between point and the mouse cursor."
1264;;;!! (interactive "@e")
1265;;;!! (let ((point-save (point)))
1266;;;!! (save-excursion
1267;;;!! (mouse-set-point event)
1268;;;!! (push-mark nil t)
1269;;;!! (if (> point-save (point))
1270;;;!! (open-rectangle (point) point-save)
1271;;;!! (open-rectangle point-save (point))))))
1272;;;!!
1273;;;!! ;; Must be a better way to do this.
1274;;;!!
1275;;;!! (defun mouse-multiple-insert (n char)
1276;;;!! (while (> n 0)
1277;;;!! (insert char)
1278;;;!! (setq n (1- n))))
1279;;;!!
1280;;;!! ;; What this could do is not finalize until button was released.
1281;;;!!
1282;;;!! (defun mouse-move-text (event)
1283;;;!! "Move text from point to cursor position, inserting spaces."
1284;;;!! (interactive "@e")
1285;;;!! (let* ((relative-coordinate
1286;;;!! (coordinates-in-window-p (car event) (selected-window))))
1287;;;!! (if (consp relative-coordinate)
1288;;;!! (cond ((> (current-column) (car relative-coordinate))
1289;;;!! (delete-char
1290;;;!! (- (car relative-coordinate) (current-column))))
1291;;;!! ((< (current-column) (car relative-coordinate))
1292;;;!! (mouse-multiple-insert
1293;;;!! (- (car relative-coordinate) (current-column)) " "))
1294;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
07a78410 1295\f
f936ae06
RS
1296;; Choose a completion with the mouse.
1297
1298(defun mouse-choose-completion (event)
49e61c42 1299 "Click on an alternative in the `*Completions*' buffer to choose it."
f936ae06 1300 (interactive "e")
d89a4a47 1301 (let ((buffer (window-buffer))
02680e9b
RS
1302 choice
1303 base-size)
f936ae06
RS
1304 (save-excursion
1305 (set-buffer (window-buffer (posn-window (event-start event))))
f36f4e9e
RS
1306 (if completion-reference-buffer
1307 (setq buffer completion-reference-buffer))
02680e9b 1308 (setq base-size completion-base-size)
f936ae06
RS
1309 (save-excursion
1310 (goto-char (posn-point (event-start event)))
b6be5d95 1311 (let (beg end)
9a43a594
RS
1312 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1313 (setq end (point) beg (1+ (point))))
1314 (if (null beg)
1315 (error "No completion here"))
1316 (setq beg (previous-single-property-change beg 'mouse-face))
2f5ed2e8
RS
1317 (setq end (or (next-single-property-change end 'mouse-face)
1318 (point-max)))
b6be5d95 1319 (setq choice (buffer-substring beg end)))))
73e2025f
RS
1320 (let ((owindow (selected-window)))
1321 (select-window (posn-window (event-start event)))
874a2cbd
RS
1322 (if (and (one-window-p t 'selected-frame)
1323 (window-dedicated-p (selected-window)))
1324 ;; This is a special buffer's frame
1325 (iconify-frame (selected-frame))
1326 (or (window-dedicated-p (selected-window))
1327 (bury-buffer)))
73e2025f 1328 (select-window owindow))
02680e9b 1329 (choose-completion-string choice buffer base-size)))
f936ae06 1330\f
07a78410
RS
1331;; Font selection.
1332
0eb9fef3
RS
1333(defun font-menu-add-default ()
1334 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1335 (font-alist x-fixed-font-alist)
0d94f5ca 1336 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
1337 (if (assoc "Default" elt)
1338 (delete (assoc "Default" elt) elt))
1339 (setcdr elt
3d64d15b 1340 (cons (list "Default"
0eb9fef3
RS
1341 (cdr (assq 'font (frame-parameters (selected-frame)))))
1342 (cdr elt)))))
1343
07a78410
RS
1344(defvar x-fixed-font-alist
1345 '("Font menu"
1346 ("Misc"
19d973e8
RS
1347 ;; For these, we specify the pixel height and width.
1348 ("fixed" "fixed")
1349 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1350 ("6x12"
1351 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1352 ("6x13"
1353 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1354 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1355 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1356 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1357 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1358 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1359 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1360 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 1361 ("")
f29c3695
RS
1362 ("clean 5x8"
1363 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1364 ("clean 6x8"
1365 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
1366 ("clean 8x8"
1367 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1368 ("clean 8x10"
1369 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1370 ("clean 8x14"
1371 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1372 ("clean 8x16"
1373 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 1374 ("")
19d973e8 1375 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
07a78410
RS
1376;;; We don't seem to have these; who knows what they are.
1377;;; ("fg-18" "fg-18")
1378;;; ("fg-25" "fg-25")
1379;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1380;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1381;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1382;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1383;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1384 ("Courier"
19d973e8 1385 ;; For these, we specify the point height.
82c048a9
RS
1386 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1387 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1388 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1389 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1390 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1391 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1392 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1393 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1394 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1395 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1396 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1397 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1398 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1399 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1400 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1401 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1402 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1403 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1404 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1405 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1406 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1407 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1408 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1409 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
07a78410
RS
1410 )
1411 "X fonts suitable for use in Emacs.")
1412
1900a92b 1413(defun mouse-set-font (&rest fonts)
07a78410
RS
1414 "Select an emacs font from a list of known good fonts"
1415 (interactive
1416 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
df4de8c6
KH
1417 (if fonts
1418 (let (font)
1419 (while fonts
1420 (condition-case nil
1421 (progn
3fadec1a 1422 (set-default-font (car fonts))
df4de8c6
KH
1423 (setq font (car fonts))
1424 (setq fonts nil))
3fadec1a
RS
1425 (error
1426 (setq fonts (cdr fonts)))))
df4de8c6 1427 (if (null font)
3fadec1a 1428 (error "Font not found")))))
cc0a8174
JB
1429\f
1430;;; Bindings for mouse commands.
1431
fcfc3c63 1432(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 1433(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 1434(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 1435
e37de120
RS
1436;; These are tested for in mouse-drag-region.
1437(global-set-key [double-mouse-1] 'mouse-set-point)
1438(global-set-key [triple-mouse-1] 'mouse-set-point)
1439
dbc4e1c1
JB
1440(global-set-key [mouse-2] 'mouse-yank-at-click)
1441(global-set-key [mouse-3] 'mouse-save-then-kill)
8b34e79d 1442
dbc4e1c1
JB
1443;; By binding these to down-going events, we let the user use the up-going
1444;; event to make the selection, saving a click.
1445(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1446(global-set-key [C-down-mouse-3] 'mouse-set-font)
07a78410 1447
8b34e79d
RS
1448;; Replaced with dragging mouse-1
1449;; (global-set-key [S-mouse-1] 'mouse-set-mark)
947da0c4 1450
3c2dd2c0 1451(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
1452(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1453(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
3c2dd2c0 1454(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 1455(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 1456(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 1457(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 1458(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
49116ac0
JB
1459
1460(provide 'mouse)
1461
6594deb0 1462;;; mouse.el ends here