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