(LIBXTR6): If NEED_LIBW, add -lw.
[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")
f598fb03
RS
461 (mouse-minibuffer-check click)
462 (select-window (posn-window (event-start click)))
463 ;; We don't use save-excursion because that preserves the mark too.
72ea54a4
RS
464 (let ((point-save (point)))
465 (unwind-protect
cc0a8174 466 (progn (mouse-set-point click)
897897e3
RS
467 (push-mark nil t t)
468 (or transient-mark-mode
469 (sit-for 1)))
72ea54a4
RS
470 (goto-char point-save))))
471
cc0a8174
JB
472(defun mouse-kill (click)
473 "Kill the region between point and the mouse click.
474The text is saved in the kill ring, as with \\[kill-region]."
ec558adc 475 (interactive "e")
d65147f6 476 (mouse-minibuffer-check click)
142c7672
KH
477 (let* ((posn (event-start click))
478 (click-posn (posn-point posn)))
479 (select-window (posn-window posn))
bd307392
JB
480 (if (numberp click-posn)
481 (kill-region (min (point) click-posn)
482 (max (point) click-posn)))))
72ea54a4 483
87ef29fd
JB
484(defun mouse-yank-at-click (click arg)
485 "Insert the last stretch of killed text at the position clicked on.
50f58001
RS
486Also move point to one end of the text thus inserted (normally the end).
487Prefix arguments are interpreted as with \\[yank].
488If `mouse-yank-at-point' is non-nil, insert at point
489regardless of where you click."
ec558adc 490 (interactive "e\nP")
50f58001 491 (or mouse-yank-at-point (mouse-set-point click))
d89a4a47 492 (setq this-command 'yank)
87ef29fd
JB
493 (yank arg))
494
495(defun mouse-kill-ring-save (click)
cc0a8174
JB
496 "Copy the region between point and the mouse click in the kill ring.
497This does not delete the region; it acts like \\[kill-ring-save]."
ec558adc 498 (interactive "e")
3f26b32a 499 (mouse-set-mark-fast click)
6452d8a6
RS
500 (let (this-command last-command)
501 (kill-ring-save (point) (mark t)))
3f26b32a 502 (mouse-show-mark))
72ea54a4 503
dbc4e1c1
JB
504;;; This function used to delete the text between point and the mouse
505;;; whenever it was equal to the front of the kill ring, but some
506;;; people found that confusing.
507
508;;; A list (TEXT START END), describing the text and position of the last
509;;; invocation of mouse-save-then-kill.
510(defvar mouse-save-then-kill-posn nil)
511
26d280b9 512(defun mouse-save-then-kill-delete-region (beg end)
9a974c88
RS
513 ;; We must make our own undo boundaries
514 ;; because they happen automatically only for the current buffer.
515 (undo-boundary)
dd524dbd
RS
516 (if (or (= beg end) (eq buffer-undo-list t))
517 ;; If we have no undo list in this buffer,
518 ;; just delete.
519 (delete-region beg end)
520 ;; Delete, but make the undo-list entry share with the kill ring.
521 ;; First, delete just one char, so in case buffer is being modified
522 ;; for the first time, the undo list records that fact.
2f4b15ef
RS
523 (let (before-change-function after-change-function
524 before-change-functions after-change-functions)
525 (delete-region beg
526 (+ beg (if (> end beg) 1 -1))))
dd524dbd
RS
527 (let ((buffer-undo-list buffer-undo-list))
528 ;; Undo that deletion--but don't change the undo list!
2f4b15ef
RS
529 (let (before-change-function after-change-function
530 before-change-functions after-change-functions)
531 (primitive-undo 1 buffer-undo-list))
dd524dbd
RS
532 ;; Now delete the rest of the specified region,
533 ;; but don't record it.
534 (setq buffer-undo-list t)
9a974c88
RS
535 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
536 (error "Lossage in mouse-save-then-kill-delete-region"))
dd524dbd
RS
537 (delete-region beg end))
538 (let ((tail buffer-undo-list))
539 ;; Search back in buffer-undo-list for the string
540 ;; that came from deleting one character.
541 (while (and tail (not (stringp (car (car tail)))))
542 (setq tail (cdr tail)))
543 ;; Replace it with an entry for the entire deleted text.
544 (and tail
9a974c88
RS
545 (setcar tail (cons (car kill-ring) (min beg end))))))
546 (undo-boundary))
eb6ff46f 547
947da0c4 548(defun mouse-save-then-kill (click)
40a45a9f
RS
549 "Save text to point in kill ring; the second time, kill the text.
550If the text between point and the mouse is the same as what's
551at the front of the kill ring, this deletes the text.
552Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
eb6ff46f
RS
553which prepares for a second click to delete the text.
554
555If you have selected words or lines, this command extends the
556selection through the word or line clicked on. If you do this
557again in a different position, it extends the selection again.
558If you do this twice in the same position, the selection is killed."
947da0c4 559 (interactive "e")
d65147f6 560 (mouse-minibuffer-check click)
3f26b32a
RS
561 (let ((click-posn (posn-point (event-start click)))
562 ;; Don't let a subsequent kill command append to this one:
563 ;; prevent setting this-command to kill-region.
564 (this-command this-command))
9a974c88 565 (if (> (mod mouse-selection-click-count 3) 0)
eb6ff46f
RS
566 (if (not (and (eq last-command 'mouse-save-then-kill)
567 (equal click-posn
568 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
d89a4a47
RS
569 ;; Find both ends of the object selected by this click.
570 (let* ((range
571 (mouse-start-end click-posn click-posn
572 mouse-selection-click-count)))
573 ;; Move whichever end is closer to the click.
574 ;; That's what xterm does, and it seems reasonable.
575 (if (< (abs (- click-posn (mark t)))
576 (abs (- click-posn (point))))
577 (set-mark (car range))
578 (goto-char (nth 1 range)))
eb6ff46f
RS
579 ;; We have already put the old region in the kill ring.
580 ;; Replace it with the extended region.
581 ;; (It would be annoying to make a separate entry.)
f69140fd 582 (kill-new (buffer-substring (point) (mark t)) t)
d89a4a47
RS
583 ;; Arrange for a repeated mouse-3 to kill this region.
584 (setq mouse-save-then-kill-posn
585 (list (car kill-ring) (point) click-posn))
eb6ff46f
RS
586 (mouse-show-mark))
587 ;; If we click this button again without moving it,
588 ;; that time kill.
dd524dbd
RS
589 (mouse-save-then-kill-delete-region (point) (mark))
590 (setq mouse-selection-click-count 0)
591 (setq mouse-save-then-kill-posn nil))
eb6ff46f
RS
592 (if (and (eq last-command 'mouse-save-then-kill)
593 mouse-save-then-kill-posn
594 (eq (car mouse-save-then-kill-posn) (car kill-ring))
595 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
596 ;; If this is the second time we've called
597 ;; mouse-save-then-kill, delete the text from the buffer.
dd524dbd
RS
598 (progn
599 (mouse-save-then-kill-delete-region (point) (mark))
600 ;; After we kill, another click counts as "the first time".
601 (setq mouse-save-then-kill-posn nil))
602 (if (or (and (eq last-command 'mouse-save-then-kill)
603 mouse-save-then-kill-posn)
3544cfc9 604 (and mark-active transient-mark-mode)
d89a4a47 605 (and (eq last-command 'mouse-drag-region)
d89a4a47
RS
606 (or mark-even-if-inactive
607 (not transient-mark-mode))))
608 ;; We have a selection or suitable region, so adjust it.
609 (let* ((posn (event-start click))
610 (new (posn-point posn)))
611 (select-window (posn-window posn))
612 (if (numberp new)
613 (progn
614 ;; Move whichever end of the region is closer to the click.
615 ;; That is what xterm does, and it seems reasonable.
616 (if (< (abs (- new (point))) (abs (- new (mark t))))
617 (goto-char new)
618 (set-mark new))
619 (setq deactivate-mark nil)))
f69140fd 620 (kill-new (buffer-substring (point) (mark t)) t))
d89a4a47
RS
621 ;; We just have point, so set mark here.
622 (mouse-set-mark-fast click)
623 (kill-ring-save (point) (mark t))
624 (mouse-show-mark))
eb6ff46f
RS
625 (setq mouse-save-then-kill-posn
626 (list (car kill-ring) (point) click-posn))))))
e66feb07
RS
627\f
628(global-set-key [M-mouse-1] 'mouse-start-secondary)
629(global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
630(global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
631(global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
9a974c88 632(global-set-key [M-mouse-2] 'mouse-yank-secondary)
e66feb07
RS
633
634;; An overlay which records the current secondary selection
635;; or else is deleted when there is no secondary selection.
636;; May be nil.
637(defvar mouse-secondary-overlay nil)
638
639;; A marker which records the specified first end for a secondary selection.
640;; May be nil.
641(defvar mouse-secondary-start nil)
642
643(defun mouse-start-secondary (click)
644 "Set one end of the secondary selection to the position clicked on.
645Use \\[mouse-secondary-save-then-kill] to set the other end
646and complete the secondary selection."
647 (interactive "e")
d65147f6 648 (mouse-minibuffer-check click)
e66feb07 649 (let ((posn (event-start click)))
230aaa73
RS
650 (save-excursion
651 (set-buffer (window-buffer (posn-window posn)))
652 ;; Cancel any preexisting secondary selection.
653 (if mouse-secondary-overlay
654 (delete-overlay mouse-secondary-overlay))
655 (if (numberp (posn-point posn))
656 (progn
657 (or mouse-secondary-start
658 (setq mouse-secondary-start (make-marker)))
659 (move-marker mouse-secondary-start (posn-point posn)))))))
e66feb07
RS
660
661(defun mouse-set-secondary (click)
662 "Set the secondary selection to the text that the mouse is dragged over.
663This must be bound to a mouse drag event."
664 (interactive "e")
d65147f6 665 (mouse-minibuffer-check click)
e66feb07
RS
666 (let ((posn (event-start click))
667 beg
668 (end (event-end click)))
230aaa73
RS
669 (save-excursion
670 (set-buffer (window-buffer (posn-window posn)))
671 (if (numberp (posn-point posn))
672 (setq beg (posn-point posn)))
673 (if mouse-secondary-overlay
674 (move-overlay mouse-secondary-overlay beg (posn-point end))
675 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
676 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
947da0c4 677
d89a4a47 678(defun mouse-drag-secondary (start-event)
e66feb07 679 "Set the secondary selection to the text that the mouse is dragged over.
d89a4a47 680Highlight the drag area as you move the mouse.
e66feb07
RS
681This must be bound to a button-down mouse event."
682 (interactive "e")
d65147f6 683 (mouse-minibuffer-check start-event)
d89a4a47
RS
684 (let* ((start-posn (event-start start-event))
685 (start-point (posn-point start-posn))
686 (start-window (posn-window start-posn))
687 (start-frame (window-frame start-window))
688 (bounds (window-edges start-window))
689 (top (nth 1 bounds))
690 (bottom (if (window-minibuffer-p start-window)
691 (nth 3 bounds)
692 ;; Don't count the mode line.
693 (1- (nth 3 bounds))))
694 (click-count (1- (event-click-count start-event))))
695 (save-excursion
696 (set-buffer (window-buffer start-window))
697 (setq mouse-selection-click-count click-count)
d89a4a47
RS
698 (or mouse-secondary-overlay
699 (setq mouse-secondary-overlay
700 (make-overlay (point) (point))))
26d280b9 701 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
9a974c88 702 (if (> (mod click-count 3) 0)
26d280b9
RS
703 ;; Double or triple press: make an initial selection
704 ;; of one word or line.
d89a4a47
RS
705 (let ((range (mouse-start-end start-point start-point click-count)))
706 (set-marker mouse-secondary-start nil)
707 (move-overlay mouse-secondary-overlay 1 1
708 (window-buffer start-window))
709 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
710 (window-buffer start-window)))
26d280b9 711 ;; Single-press: cancel any preexisting secondary selection.
d89a4a47
RS
712 (or mouse-secondary-start
713 (setq mouse-secondary-start (make-marker)))
714 (set-marker mouse-secondary-start start-point)
715 (delete-overlay mouse-secondary-overlay))
716 (let (event end end-point)
717 (track-mouse
718 (while (progn
719 (setq event (read-event))
720 (or (mouse-movement-p event)
721 (eq (car-safe event) 'switch-frame)))
722
723 (if (eq (car-safe event) 'switch-frame)
724 nil
725 (setq end (event-end event)
726 end-point (posn-point end))
727 (cond
d89a4a47
RS
728 ;; Are we moving within the original window?
729 ((and (eq (posn-window end) start-window)
730 (integer-or-marker-p end-point))
d89a4a47
RS
731 (let ((range (mouse-start-end start-point end-point
732 click-count)))
0136e1e3
RS
733 (if (or (/= start-point end-point)
734 (null (marker-position mouse-secondary-start)))
735 (progn
736 (set-marker mouse-secondary-start nil)
737 (move-overlay mouse-secondary-overlay
738 (car range) (nth 1 range))))))
3b0aebe9
RS
739 (t
740 (let ((mouse-row (cdr (cdr (mouse-position)))))
741 (cond
742 ((null mouse-row))
743 ((< mouse-row top)
e919a622
RS
744 (mouse-scroll-subr start-window (- mouse-row top)
745 mouse-secondary-overlay start-point))
d2287ded 746 ((>= mouse-row bottom)
e919a622 747 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
3b0aebe9 748 mouse-secondary-overlay start-point)))))))))
d89a4a47 749
4e399a53
RS
750 (if (consp event)
751;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
752;;; (eq (posn-window (event-end event)) start-window)
753;;; (numberp (posn-point (event-end event)))
d89a4a47
RS
754 (if (marker-position mouse-secondary-start)
755 (save-window-excursion
756 (delete-overlay mouse-secondary-overlay)
9a974c88 757 (x-set-selection 'SECONDARY nil)
d89a4a47
RS
758 (select-window start-window)
759 (save-excursion
760 (goto-char mouse-secondary-start)
761 (sit-for 1)))
9a974c88
RS
762 (x-set-selection
763 'SECONDARY
764 (buffer-substring (overlay-start mouse-secondary-overlay)
765 (overlay-end mouse-secondary-overlay)))))))))
e66feb07 766
9a974c88 767(defun mouse-yank-secondary (click)
50f58001
RS
768 "Insert the secondary selection at the position clicked on.
769Move point to the end of the inserted text.
770If `mouse-yank-at-point' is non-nil, insert at point
771regardless of where you click."
9a974c88 772 (interactive "e")
50f58001
RS
773 (or mouse-yank-at-point (mouse-set-point click))
774 (insert (x-get-selection 'SECONDARY)))
9a974c88 775
7e6404f6 776(defun mouse-kill-secondary ()
9a974c88
RS
777 "Kill the text in the secondary selection.
778This is intended more as a keyboard command than as a mouse command
779but it can work as either one.
780
781The current buffer (in case of keyboard use), or the buffer clicked on,
782must be the one that the secondary selection is in. This requirement
783is to prevent accidents."
7e6404f6
RS
784 (interactive)
785 (let* ((keys (this-command-keys))
786 (click (elt keys (1- (length keys)))))
787 (or (eq (overlay-buffer mouse-secondary-overlay)
788 (if (listp click)
789 (window-buffer (posn-window (event-start click)))
790 (current-buffer)))
791 (error "Select or click on the buffer where the secondary selection is")))
9a974c88
RS
792 (save-excursion
793 (set-buffer (overlay-buffer mouse-secondary-overlay))
794 (kill-region (overlay-start mouse-secondary-overlay)
795 (overlay-end mouse-secondary-overlay)))
e66feb07 796 (delete-overlay mouse-secondary-overlay)
9a974c88 797 (x-set-selection 'SECONDARY nil)
e66feb07
RS
798 (setq mouse-secondary-overlay nil))
799
800(defun mouse-secondary-save-then-kill (click)
d89a4a47 801 "Save text to point in kill ring; the second time, kill the text.
7bbe2cc7
RS
802You must use this in a buffer where you have recently done \\[mouse-start-secondary].
803If the text between where you did \\[mouse-start-secondary] and where
804you use this command matches the text at the front of the kill ring,
805this command deletes the text.
e66feb07 806Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
7bbe2cc7 807which prepares for a second click with this command to delete the text.
d89a4a47 808
7bbe2cc7
RS
809If you have already made a secondary selection in that buffer,
810this command extends or retracts the selection to where you click.
811If you do this again in a different position, it extends or retracts
812again. If you do this twice in the same position, it kills the selection."
e66feb07 813 (interactive "e")
d65147f6 814 (mouse-minibuffer-check click)
d89a4a47
RS
815 (let ((posn (event-start click))
816 (click-posn (posn-point (event-start click)))
e66feb07
RS
817 ;; Don't let a subsequent kill command append to this one:
818 ;; prevent setting this-command to kill-region.
819 (this-command this-command))
9a974c88
RS
820 (or (eq (window-buffer (posn-window posn))
821 (or (and mouse-secondary-overlay
822 (overlay-buffer mouse-secondary-overlay))
823 (if mouse-secondary-start
824 (marker-buffer mouse-secondary-start))))
825 (error "Wrong buffer"))
826 (save-excursion
827 (set-buffer (window-buffer (posn-window posn)))
828 (if (> (mod mouse-selection-click-count 3) 0)
829 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
830 (equal click-posn
831 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
832 ;; Find both ends of the object selected by this click.
833 (let* ((range
834 (mouse-start-end click-posn click-posn
835 mouse-selection-click-count)))
836 ;; Move whichever end is closer to the click.
837 ;; That's what xterm does, and it seems reasonable.
838 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
839 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
840 (move-overlay mouse-secondary-overlay (car range)
841 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
842 (move-overlay mouse-secondary-overlay
843 (overlay-start mouse-secondary-overlay)
844 (nth 1 range)))
9a974c88
RS
845 ;; We have already put the old region in the kill ring.
846 ;; Replace it with the extended region.
847 ;; (It would be annoying to make a separate entry.)
f69140fd
KH
848 (kill-new (buffer-substring
849 (overlay-start mouse-secondary-overlay)
850 (overlay-end mouse-secondary-overlay)) t)
9a974c88
RS
851 ;; Arrange for a repeated mouse-3 to kill this region.
852 (setq mouse-save-then-kill-posn
853 (list (car kill-ring) (point) click-posn)))
854 ;; If we click this button again without moving it,
855 ;; that time kill.
d89a4a47 856 (progn
9a974c88
RS
857 (mouse-save-then-kill-delete-region
858 (overlay-start mouse-secondary-overlay)
859 (overlay-end mouse-secondary-overlay))
860 (setq mouse-save-then-kill-posn nil)
861 (setq mouse-selection-click-count 0)
862 (delete-overlay mouse-secondary-overlay)))
863 (if (and (eq last-command 'mouse-secondary-save-then-kill)
864 mouse-save-then-kill-posn
865 (eq (car mouse-save-then-kill-posn) (car kill-ring))
866 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
867 ;; If this is the second time we've called
868 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
869 (progn
870 (mouse-save-then-kill-delete-region
871 (overlay-start mouse-secondary-overlay)
872 (overlay-end mouse-secondary-overlay))
873 (setq mouse-save-then-kill-posn nil)
874 (delete-overlay mouse-secondary-overlay))
875 (if (overlay-start mouse-secondary-overlay)
876 ;; We have a selection, so adjust it.
877 (progn
878 (if (numberp click-posn)
879 (progn
880 ;; Move whichever end of the region is closer to the click.
881 ;; That is what xterm does, and it seems reasonable.
882 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
883 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
884 (move-overlay mouse-secondary-overlay click-posn
885 (overlay-end mouse-secondary-overlay))
d89a4a47
RS
886 (move-overlay mouse-secondary-overlay
887 (overlay-start mouse-secondary-overlay)
888 click-posn))
9a974c88 889 (setq deactivate-mark nil)))
0136e1e3 890 (if (eq last-command 'mouse-secondary-save-then-kill)
f69140fd
KH
891 ;; If the front of the kill ring comes from
892 ;; an immediately previous use of this command,
893 ;; replace it with the extended region.
894 ;; (It would be annoying to make a separate entry.)
895 (kill-new (buffer-substring
0136e1e3 896 (overlay-start mouse-secondary-overlay)
f69140fd 897 (overlay-end mouse-secondary-overlay)) t)
0136e1e3
RS
898 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
899 (overlay-end mouse-secondary-overlay))))
9a974c88
RS
900 (if mouse-secondary-start
901 ;; All we have is one end of a selection,
902 ;; so put the other end here.
903 (let ((start (+ 0 mouse-secondary-start)))
904 (kill-ring-save start click-posn)
905 (if mouse-secondary-overlay
906 (move-overlay mouse-secondary-overlay start click-posn)
907 (setq mouse-secondary-overlay (make-overlay start click-posn)))
908 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
909 (setq mouse-save-then-kill-posn
910 (list (car kill-ring) (point) click-posn))))
911 (x-set-selection 'SECONDARY
912 (if (overlay-buffer mouse-secondary-overlay)
913 (buffer-substring
914 (overlay-start mouse-secondary-overlay)
915 (overlay-end mouse-secondary-overlay)))))))
e66feb07 916\f
8b34e79d 917(defun mouse-buffer-menu (event)
2d82f7b9
RS
918 "Pop up a menu of buffers for selection with the mouse.
919This switches buffers in the window that you clicked on,
920and selects that window."
ec558adc 921 (interactive "e")
d65147f6 922 (mouse-minibuffer-check event)
8b34e79d
RS
923 (let ((menu
924 (list "Buffer Menu"
925 (cons "Select Buffer"
926 (let ((tail (buffer-list))
af2a85fe 927 (maxbuf 0)
8b34e79d 928 head)
af2a85fe
RS
929 (while tail
930 (or (eq ?\ (aref (buffer-name (car tail)) 0))
931 (setq maxbuf
932 (max maxbuf
933 (length (buffer-name (car tail))))))
934 (setq tail (cdr tail)))
935 (setq tail (buffer-list))
8b34e79d
RS
936 (while tail
937 (let ((elt (car tail)))
938 (if (not (string-match "^ "
939 (buffer-name elt)))
940 (setq head (cons
941 (cons
942 (format
af2a85fe
RS
943 (format "%%%ds %%s%%s %%s"
944 maxbuf)
8b34e79d 945 (buffer-name elt)
af2a85fe
RS
946 (if (buffer-modified-p elt)
947 "*" " ")
948 (save-excursion
949 (set-buffer elt)
950 (if buffer-read-only "%" " "))
8b34e79d
RS
951 (or (buffer-file-name elt) ""))
952 elt)
953 head))))
954 (setq tail (cdr tail)))
955 (reverse head))))))
2d82f7b9
RS
956 (let ((buf (x-popup-menu event menu))
957 (window (posn-window (event-start event))))
958 (if buf
959 (progn
c0bb9f3b 960 (or (framep window) (select-window window))
2d82f7b9 961 (switch-to-buffer buf))))))
72ea54a4 962\f
5ba2dc3f 963;;; These need to be rewritten for the new scroll bar implementation.
dbc4e1c1
JB
964
965;;;!! ;; Commands for the scroll bar.
966;;;!!
967;;;!! (defun mouse-scroll-down (click)
968;;;!! (interactive "@e")
969;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
970;;;!!
971;;;!! (defun mouse-scroll-up (click)
972;;;!! (interactive "@e")
973;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
974;;;!!
975;;;!! (defun mouse-scroll-down-full ()
976;;;!! (interactive "@")
977;;;!! (scroll-down nil))
978;;;!!
979;;;!! (defun mouse-scroll-up-full ()
980;;;!! (interactive "@")
981;;;!! (scroll-up nil))
982;;;!!
983;;;!! (defun mouse-scroll-move-cursor (click)
984;;;!! (interactive "@e")
985;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
986;;;!!
987;;;!! (defun mouse-scroll-absolute (event)
988;;;!! (interactive "@e")
989;;;!! (let* ((pos (car event))
990;;;!! (position (car pos))
991;;;!! (length (car (cdr pos))))
992;;;!! (if (<= length 0) (setq length 1))
993;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
994;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
995;;;!! position)
996;;;!! length)
997;;;!! scale-factor)))
998;;;!! (goto-char newpos)
999;;;!! (recenter '(4)))))
1000;;;!!
1001;;;!! (defun mouse-scroll-left (click)
1002;;;!! (interactive "@e")
1003;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1004;;;!!
1005;;;!! (defun mouse-scroll-right (click)
1006;;;!! (interactive "@e")
1007;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1008;;;!!
1009;;;!! (defun mouse-scroll-left-full ()
1010;;;!! (interactive "@")
1011;;;!! (scroll-left nil))
1012;;;!!
1013;;;!! (defun mouse-scroll-right-full ()
1014;;;!! (interactive "@")
1015;;;!! (scroll-right nil))
1016;;;!!
1017;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1018;;;!! (interactive "@e")
1019;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1020;;;!!
1021;;;!! (defun mouse-scroll-absolute-horizontally (event)
1022;;;!! (interactive "@e")
1023;;;!! (let* ((pos (car event))
1024;;;!! (position (car pos))
1025;;;!! (length (car (cdr pos))))
1026;;;!! (set-window-hscroll (selected-window) 33)))
1027;;;!!
1028;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1029;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1030;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1031;;;!!
1032;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1033;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1034;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1035;;;!!
1036;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1037;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1038;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1039;;;!!
1040;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1041;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1042;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1043;;;!!
1044;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1045;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1046;;;!! 'mouse-scroll-absolute-horizontally)
1047;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1048;;;!!
1049;;;!! (global-set-key [horizontal-slider mouse-1]
1050;;;!! 'mouse-scroll-move-cursor-horizontally)
1051;;;!! (global-set-key [horizontal-slider mouse-2]
1052;;;!! 'mouse-scroll-move-cursor-horizontally)
1053;;;!! (global-set-key [horizontal-slider mouse-3]
1054;;;!! 'mouse-scroll-move-cursor-horizontally)
1055;;;!!
1056;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1057;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1058;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1059;;;!!
1060;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1061;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1062;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1063;;;!!
1064;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1065;;;!! 'mouse-split-window-horizontally)
1066;;;!! (global-set-key [mode-line S-mouse-2]
1067;;;!! 'mouse-split-window-horizontally)
1068;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1069;;;!! 'mouse-split-window)
6b2154de 1070\f
dbc4e1c1
JB
1071;;;!! ;;;;
1072;;;!! ;;;; Here are experimental things being tested. Mouse events
1073;;;!! ;;;; are of the form:
1074;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1075;;;!! ;;
1076;;;!! ;;;;
1077;;;!! ;;;; Dynamically track mouse coordinates
1078;;;!! ;;;;
1079;;;!! ;;
1080;;;!! ;;(defun track-mouse (event)
1081;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1082;;;!! ;; (interactive "@e")
1083;;;!! ;; (while mouse-grabbed
1084;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1085;;;!! ;; (abs-x (car pos))
1086;;;!! ;; (abs-y (cdr pos))
1087;;;!! ;; (relative-coordinate (coordinates-in-window-p
1088;;;!! ;; (list (car pos) (cdr pos))
1089;;;!! ;; (selected-window))))
1090;;;!! ;; (if (consp relative-coordinate)
1091;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1092;;;!! ;; (car relative-coordinate)
1093;;;!! ;; (car (cdr relative-coordinate)))
1094;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1095;;;!!
1096;;;!! ;;
1097;;;!! ;; Dynamically put a box around the line indicated by point
1098;;;!! ;;
1099;;;!! ;;
1100;;;!! ;;(require 'backquote)
1101;;;!! ;;
1102;;;!! ;;(defun mouse-select-buffer-line (event)
1103;;;!! ;; (interactive "@e")
1104;;;!! ;; (let ((relative-coordinate
1105;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1106;;;!! ;; (abs-y (car (cdr (car event)))))
1107;;;!! ;; (if (consp relative-coordinate)
1108;;;!! ;; (progn
1109;;;!! ;; (save-excursion
1110;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1111;;;!! ;; (x-draw-rectangle
1112;;;!! ;; (selected-screen)
1113;;;!! ;; abs-y 0
1114;;;!! ;; (save-excursion
1115;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1116;;;!! ;; (end-of-line)
1117;;;!! ;; (push-mark nil t)
1118;;;!! ;; (beginning-of-line)
1119;;;!! ;; (- (region-end) (region-beginning))) 1))
1120;;;!! ;; (sit-for 1)
1121;;;!! ;; (x-erase-rectangle (selected-screen))))))
1122;;;!! ;;
1123;;;!! ;;(defvar last-line-drawn nil)
1124;;;!! ;;(defvar begin-delim "[^ \t]")
1125;;;!! ;;(defvar end-delim "[^ \t]")
1126;;;!! ;;
1127;;;!! ;;(defun mouse-boxing (event)
1128;;;!! ;; (interactive "@e")
1129;;;!! ;; (save-excursion
1130;;;!! ;; (let ((screen (selected-screen)))
1131;;;!! ;; (while (= (x-mouse-events) 0)
1132;;;!! ;; (let* ((pos (read-mouse-position screen))
1133;;;!! ;; (abs-x (car pos))
1134;;;!! ;; (abs-y (cdr pos))
1135;;;!! ;; (relative-coordinate
1136;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1137;;;!! ;; (selected-window)))
1138;;;!! ;; (begin-reg nil)
1139;;;!! ;; (end-reg nil)
1140;;;!! ;; (end-column nil)
1141;;;!! ;; (begin-column nil))
1142;;;!! ;; (if (and (consp relative-coordinate)
1143;;;!! ;; (or (not last-line-drawn)
1144;;;!! ;; (not (= last-line-drawn abs-y))))
1145;;;!! ;; (progn
1146;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1147;;;!! ;; (if (= (following-char) 10)
1148;;;!! ;; ()
1149;;;!! ;; (progn
1150;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1151;;;!! ;; (setq begin-column (1- (current-column)))
1152;;;!! ;; (end-of-line)
1153;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1154;;;!! ;; (setq end-column (1+ (current-column)))
1155;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1156;;;!! ;; (x-draw-rectangle screen
1157;;;!! ;; (setq last-line-drawn abs-y)
1158;;;!! ;; begin-column
1159;;;!! ;; (- end-column begin-column) 1))))))))))
1160;;;!! ;;
1161;;;!! ;;(defun mouse-erase-box ()
1162;;;!! ;; (interactive)
1163;;;!! ;; (if last-line-drawn
1164;;;!! ;; (progn
1165;;;!! ;; (x-erase-rectangle (selected-screen))
1166;;;!! ;; (setq last-line-drawn nil))))
1167;;;!!
1168;;;!! ;;; (defun test-x-rectangle ()
1169;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1170;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1171;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1172;;;!!
1173;;;!! ;;
1174;;;!! ;; Here is how to do double clicking in lisp. About to change.
1175;;;!! ;;
1176;;;!!
1177;;;!! (defvar double-start nil)
1178;;;!! (defconst double-click-interval 300
1179;;;!! "Max ticks between clicks")
1180;;;!!
1181;;;!! (defun double-down (event)
1182;;;!! (interactive "@e")
1183;;;!! (if double-start
1184;;;!! (let ((interval (- (nth 4 event) double-start)))
1185;;;!! (if (< interval double-click-interval)
1186;;;!! (progn
1187;;;!! (backward-up-list 1)
1188;;;!! ;; (message "Interval %d" interval)
1189;;;!! (sleep-for 1)))
1190;;;!! (setq double-start nil))
1191;;;!! (setq double-start (nth 4 event))))
1192;;;!!
1193;;;!! (defun double-up (event)
1194;;;!! (interactive "@e")
1195;;;!! (and double-start
1196;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1197;;;!! (setq double-start nil)))
1198;;;!!
1199;;;!! ;;; (defun x-test-doubleclick ()
1200;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1201;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1202;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1203;;;!!
1204;;;!! ;;
5ba2dc3f 1205;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
dbc4e1c1
JB
1206;;;!! ;;
1207;;;!!
1208;;;!! (defvar scrolled-lines 0)
1209;;;!! (defconst scroll-speed 1)
1210;;;!!
1211;;;!! (defun incr-scroll-down (event)
1212;;;!! (interactive "@e")
1213;;;!! (setq scrolled-lines 0)
1214;;;!! (incremental-scroll scroll-speed))
1215;;;!!
1216;;;!! (defun incr-scroll-up (event)
1217;;;!! (interactive "@e")
1218;;;!! (setq scrolled-lines 0)
1219;;;!! (incremental-scroll (- scroll-speed)))
1220;;;!!
1221;;;!! (defun incremental-scroll (n)
1222;;;!! (while (= (x-mouse-events) 0)
1223;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1224;;;!! (scroll-down n)
1225;;;!! (sit-for 300 t)))
1226;;;!!
1227;;;!! (defun incr-scroll-stop (event)
1228;;;!! (interactive "@e")
1229;;;!! (message "Scrolled %d lines" scrolled-lines)
1230;;;!! (setq scrolled-lines 0)
1231;;;!! (sleep-for 1))
1232;;;!!
1233;;;!! ;;; (defun x-testing-scroll ()
1234;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1235;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1236;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1237;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1238;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1239;;;!!
1240;;;!! ;;
1241;;;!! ;; Some playthings suitable for picture mode? They need work.
1242;;;!! ;;
1243;;;!!
1244;;;!! (defun mouse-kill-rectangle (event)
1245;;;!! "Kill the rectangle between point and the mouse cursor."
1246;;;!! (interactive "@e")
1247;;;!! (let ((point-save (point)))
1248;;;!! (save-excursion
1249;;;!! (mouse-set-point event)
1250;;;!! (push-mark nil t)
1251;;;!! (if (> point-save (point))
1252;;;!! (kill-rectangle (point) point-save)
1253;;;!! (kill-rectangle point-save (point))))))
1254;;;!!
1255;;;!! (defun mouse-open-rectangle (event)
1256;;;!! "Kill the rectangle between point and the mouse cursor."
1257;;;!! (interactive "@e")
1258;;;!! (let ((point-save (point)))
1259;;;!! (save-excursion
1260;;;!! (mouse-set-point event)
1261;;;!! (push-mark nil t)
1262;;;!! (if (> point-save (point))
1263;;;!! (open-rectangle (point) point-save)
1264;;;!! (open-rectangle point-save (point))))))
1265;;;!!
1266;;;!! ;; Must be a better way to do this.
1267;;;!!
1268;;;!! (defun mouse-multiple-insert (n char)
1269;;;!! (while (> n 0)
1270;;;!! (insert char)
1271;;;!! (setq n (1- n))))
1272;;;!!
1273;;;!! ;; What this could do is not finalize until button was released.
1274;;;!!
1275;;;!! (defun mouse-move-text (event)
1276;;;!! "Move text from point to cursor position, inserting spaces."
1277;;;!! (interactive "@e")
1278;;;!! (let* ((relative-coordinate
1279;;;!! (coordinates-in-window-p (car event) (selected-window))))
1280;;;!! (if (consp relative-coordinate)
1281;;;!! (cond ((> (current-column) (car relative-coordinate))
1282;;;!! (delete-char
1283;;;!! (- (car relative-coordinate) (current-column))))
1284;;;!! ((< (current-column) (car relative-coordinate))
1285;;;!! (mouse-multiple-insert
1286;;;!! (- (car relative-coordinate) (current-column)) " "))
1287;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
07a78410 1288\f
f936ae06
RS
1289;; Choose a completion with the mouse.
1290
1291(defun mouse-choose-completion (event)
49e61c42 1292 "Click on an alternative in the `*Completions*' buffer to choose it."
f936ae06 1293 (interactive "e")
d89a4a47 1294 (let ((buffer (window-buffer))
02680e9b
RS
1295 choice
1296 base-size)
f936ae06
RS
1297 (save-excursion
1298 (set-buffer (window-buffer (posn-window (event-start event))))
f36f4e9e
RS
1299 (if completion-reference-buffer
1300 (setq buffer completion-reference-buffer))
02680e9b 1301 (setq base-size completion-base-size)
f936ae06
RS
1302 (save-excursion
1303 (goto-char (posn-point (event-start event)))
b6be5d95 1304 (let (beg end)
9a43a594
RS
1305 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1306 (setq end (point) beg (1+ (point))))
1307 (if (null beg)
1308 (error "No completion here"))
1309 (setq beg (previous-single-property-change beg 'mouse-face))
2f5ed2e8
RS
1310 (setq end (or (next-single-property-change end 'mouse-face)
1311 (point-max)))
b6be5d95 1312 (setq choice (buffer-substring beg end)))))
73e2025f
RS
1313 (let ((owindow (selected-window)))
1314 (select-window (posn-window (event-start event)))
874a2cbd
RS
1315 (if (and (one-window-p t 'selected-frame)
1316 (window-dedicated-p (selected-window)))
1317 ;; This is a special buffer's frame
1318 (iconify-frame (selected-frame))
1319 (or (window-dedicated-p (selected-window))
1320 (bury-buffer)))
73e2025f 1321 (select-window owindow))
02680e9b 1322 (choose-completion-string choice buffer base-size)))
f936ae06 1323\f
07a78410
RS
1324;; Font selection.
1325
0eb9fef3
RS
1326(defun font-menu-add-default ()
1327 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1328 (font-alist x-fixed-font-alist)
0d94f5ca 1329 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
0eb9fef3
RS
1330 (if (assoc "Default" elt)
1331 (delete (assoc "Default" elt) elt))
1332 (setcdr elt
3d64d15b 1333 (cons (list "Default"
0eb9fef3
RS
1334 (cdr (assq 'font (frame-parameters (selected-frame)))))
1335 (cdr elt)))))
1336
07a78410
RS
1337(defvar x-fixed-font-alist
1338 '("Font menu"
1339 ("Misc"
19d973e8
RS
1340 ;; For these, we specify the pixel height and width.
1341 ("fixed" "fixed")
1342 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1343 ("6x12"
1344 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1345 ("6x13"
1346 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1347 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1348 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1349 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1350 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1351 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1352 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1353 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
fa21fdec 1354 ("")
f29c3695
RS
1355 ("clean 5x8"
1356 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1357 ("clean 6x8"
1358 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
19d973e8
RS
1359 ("clean 8x8"
1360 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1361 ("clean 8x10"
1362 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1363 ("clean 8x14"
1364 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1365 ("clean 8x16"
1366 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
fa21fdec 1367 ("")
19d973e8 1368 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
07a78410
RS
1369;;; We don't seem to have these; who knows what they are.
1370;;; ("fg-18" "fg-18")
1371;;; ("fg-25" "fg-25")
1372;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1373;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1374;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1375;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1376;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1377 ("Courier"
19d973e8 1378 ;; For these, we specify the point height.
82c048a9
RS
1379 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1380 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1381 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1382 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1383 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1384 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1385 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1386 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1387 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1388 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1389 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1390 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1391 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1392 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1393 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1394 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1395 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1396 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1397 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1398 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1399 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1400 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1401 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1402 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
07a78410
RS
1403 )
1404 "X fonts suitable for use in Emacs.")
1405
1900a92b 1406(defun mouse-set-font (&rest fonts)
07a78410
RS
1407 "Select an emacs font from a list of known good fonts"
1408 (interactive
1409 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
df4de8c6
KH
1410 (if fonts
1411 (let (font)
1412 (while fonts
1413 (condition-case nil
1414 (progn
3fadec1a 1415 (set-default-font (car fonts))
df4de8c6
KH
1416 (setq font (car fonts))
1417 (setq fonts nil))
3fadec1a
RS
1418 (error
1419 (setq fonts (cdr fonts)))))
df4de8c6 1420 (if (null font)
3fadec1a 1421 (error "Font not found")))))
cc0a8174
JB
1422\f
1423;;; Bindings for mouse commands.
1424
fcfc3c63 1425(define-key global-map [down-mouse-1] 'mouse-drag-region)
dbc4e1c1 1426(global-set-key [mouse-1] 'mouse-set-point)
dbc4e1c1 1427(global-set-key [drag-mouse-1] 'mouse-set-region)
fcfc3c63 1428
e37de120
RS
1429;; These are tested for in mouse-drag-region.
1430(global-set-key [double-mouse-1] 'mouse-set-point)
1431(global-set-key [triple-mouse-1] 'mouse-set-point)
1432
dbc4e1c1
JB
1433(global-set-key [mouse-2] 'mouse-yank-at-click)
1434(global-set-key [mouse-3] 'mouse-save-then-kill)
8b34e79d 1435
dbc4e1c1
JB
1436;; By binding these to down-going events, we let the user use the up-going
1437;; event to make the selection, saving a click.
1438(global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1439(global-set-key [C-down-mouse-3] 'mouse-set-font)
07a78410 1440
8b34e79d
RS
1441;; Replaced with dragging mouse-1
1442;; (global-set-key [S-mouse-1] 'mouse-set-mark)
947da0c4 1443
3c2dd2c0 1444(global-set-key [mode-line mouse-1] 'mouse-select-window)
544e7e73
RS
1445(global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1446(global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
3c2dd2c0 1447(global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
dbc4e1c1 1448(global-set-key [mode-line mouse-3] 'mouse-delete-window)
3c2dd2c0 1449(global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
9926ab64 1450(global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
b6522df6 1451(global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
49116ac0
JB
1452
1453(provide 'mouse)
1454
6594deb0 1455;;; mouse.el ends here