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