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