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