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