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