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