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