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