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