Don't include filio.h if NO_FILIO_H.
[emacs.git] / lisp / mouse.el
blob7ffc98c7a9deaee105c2bb1563b1f471d249c5a2
1 ;;; mouse.el --- window system-independent mouse support
3 ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: hardware
8 ;; This file is part of GNU Emacs.
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.
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.
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package provides various useful commands (including help
28 ;; system access) through the mouse. All this code assumes that mouse
29 ;; interpretation has been abstracted into Emacs input events.
31 ;; The code is rather X-dependent.
33 ;;; Code:
35 ;;; Utility functions.
37 ;;; Indent track-mouse like progn.
38 (put 'track-mouse 'lisp-indent-function 0)
40 (defvar mouse-yank-at-point nil
41 "*If non-nil, mouse yank commands yank at point instead of at click.")
43 ;; Provide a mode-specific menu on a mouse button.
45 (defun mouse-major-mode-menu (event)
46 "Pop up a mode-specific menu of mouse commands."
47 ;; Switch to the window clicked on, because otherwise
48 ;; the mode's commands may not make sense.
49 (interactive "@e")
50 (let (;; This is where mouse-major-mode-menu-prefix
51 ;; returns the prefix we should use (after menu-bar).
52 ;; It is either nil or (SOME-SYMBOL).
53 (mouse-major-mode-menu-prefix nil)
54 ;; Make a keymap in which our last command leads to a menu
55 (newmap (make-sparse-keymap (concat mode-name " Mode")))
56 result)
57 ;; Make our menu inherit from the desired keymap
58 ;; which we want to display as the menu now.
59 (set-keymap-parent newmap
60 (mouse-major-mode-menu-1
61 (and (current-local-map)
62 (lookup-key (current-local-map) [menu-bar]))))
63 (setq result (x-popup-menu t (list newmap)))
64 (if result
65 (let ((command (key-binding
66 (apply 'vector (append '(menu-bar)
67 mouse-major-mode-menu-prefix
68 result)))))
69 (if command
70 (command-execute command))))))
72 ;; Compute and cache the equivalent keys in MENU and all its submenus.
73 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
74 ;;; (and (eq (car menu) 'keymap)
75 ;;; (x-popup-menu nil menu))
76 ;;; (while menu
77 ;;; (and (consp (car menu))
78 ;;; (consp (cdr (car menu)))
79 ;;; (let ((tail (cdr (car menu))))
80 ;;; (while (and (consp tail)
81 ;;; (not (eq (car tail) 'keymap)))
82 ;;; (setq tail (cdr tail)))
83 ;;; (if (consp tail)
84 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
85 ;;; (setq menu (cdr menu))))
87 ;; Given a mode's menu bar keymap,
88 ;; if it defines exactly one menu bar menu,
89 ;; return just that menu.
90 ;; Otherwise return a menu for all of them.
91 (defun mouse-major-mode-menu-1 (menubar)
92 (if menubar
93 (let ((tail menubar)
94 submap)
95 (while tail
96 (if (consp (car tail))
97 (if submap
98 (setq submap t)
99 (setq submap (car tail))))
100 (setq tail (cdr tail)))
101 (if (eq submap t)
102 menubar
103 (setq mouse-major-mode-menu-prefix (list (car submap)))
104 (cdr (cdr submap))))))
106 ;; Commands that operate on windows.
108 (defun mouse-minibuffer-check (event)
109 (let ((w (posn-window (event-start event))))
110 (and (window-minibuffer-p w)
111 (not (minibuffer-window-active-p w))
112 (error "Minibuffer window is not active")))
113 ;; Give temporary modes such as isearch a chance to turn off.
114 (run-hooks 'mouse-leave-buffer-hook))
116 (defun mouse-delete-window (click)
117 "Delete the window you click on.
118 This must be bound to a mouse click."
119 (interactive "e")
120 (mouse-minibuffer-check click)
121 (delete-window (posn-window (event-start click))))
123 (defun mouse-select-window (click)
124 "Select the window clicked on; don't move point."
125 (interactive "e")
126 (mouse-minibuffer-check click)
127 (let ((oframe (selected-frame))
128 (frame (window-frame (posn-window (event-start click)))))
129 (select-window (posn-window (event-start click)))
130 (raise-frame frame)
131 (select-frame frame)
132 (or (eq frame oframe)
133 (set-mouse-position (selected-frame) (1- (frame-width)) 0))
134 (unfocus-frame)))
136 (defun mouse-tear-off-window (click)
137 "Delete the window clicked on, and create a new frame displaying its buffer."
138 (interactive "e")
139 (mouse-minibuffer-check click)
140 (let* ((window (posn-window (event-start click)))
141 (buf (window-buffer window))
142 (frame (make-frame)))
143 (select-frame frame)
144 (switch-to-buffer buf)
145 (delete-window window)))
147 (defun mouse-delete-other-windows ()
148 "Delete all window except the one you click on."
149 (interactive "@")
150 (delete-other-windows))
152 (defun mouse-split-window-vertically (click)
153 "Select Emacs window mouse is on, then split it vertically in half.
154 The window is split at the line clicked on.
155 This command must be bound to a mouse click."
156 (interactive "@e")
157 (mouse-minibuffer-check click)
158 (let ((start (event-start click)))
159 (select-window (posn-window start))
160 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
161 (first-line window-min-height)
162 (last-line (- (window-height) window-min-height)))
163 (if (< last-line first-line)
164 (error "Window too short to split")
165 (split-window-vertically
166 (min (max new-height first-line) last-line))))))
168 (defun mouse-split-window-horizontally (click)
169 "Select Emacs window mouse is on, then split it horizontally in half.
170 The window is split at the column clicked on.
171 This command must be bound to a mouse click."
172 (interactive "@e")
173 (mouse-minibuffer-check click)
174 (let ((start (event-start click)))
175 (select-window (posn-window start))
176 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
177 (first-col window-min-width)
178 (last-col (- (window-width) window-min-width)))
179 (if (< last-col first-col)
180 (error "Window too narrow to split")
181 (split-window-horizontally
182 (min (max new-width first-col) last-col))))))
184 (defun mouse-drag-mode-line (start-event)
185 "Change the height of a window by dragging on the mode line."
186 (interactive "e")
187 ;; Give temporary modes such as isearch a chance to turn off.
188 (run-hooks 'mouse-leave-buffer-hook)
189 (let ((done nil)
190 (echo-keystrokes 0)
191 (start-event-frame (window-frame (car (car (cdr start-event)))))
192 (start-event-window (car (car (cdr start-event))))
193 (start-nwindows (count-windows t))
194 (old-selected-window (selected-window))
195 should-enlarge-minibuffer
196 event mouse minibuffer y top bot edges wconfig params growth)
197 (setq params (frame-parameters))
198 (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
199 (one-window-p t))
200 (error "Attempt to resize sole window"))
201 (track-mouse
202 (progn
203 ;; enlarge-window only works on the selected window, so
204 ;; we must select the window where the start event originated.
205 ;; unwind-protect will restore the old selected window later.
206 (select-window start-event-window)
207 ;; if this is the bottommost ordinary window, then to
208 ;; move its modeline the minibuffer must be enlarged.
209 (setq should-enlarge-minibuffer
210 (and minibuffer
211 (not (one-window-p t))
212 (= (nth 1 (window-edges minibuffer))
213 (nth 3 (window-edges)))))
214 ;; loop reading events and sampling the position of
215 ;; the mouse.
216 (while (not done)
217 (setq event (read-event)
218 mouse (mouse-position))
219 ;; do nothing if
220 ;; - there is a switch-frame event.
221 ;; - the mouse isn't in the frame that we started in
222 ;; - the mouse isn't in any Emacs frame
223 ;; drag if
224 ;; - there is a mouse-movement event
225 ;; - there is a scroll-bar-movement event
226 ;; (same as mouse movement for our purposes)
227 ;; quit if
228 ;; - there is a keyboard event or some other unknown event
229 ;; unknown event.
230 (cond ((integerp event)
231 (setq done t))
232 ((eq (car event) 'switch-frame)
233 nil)
234 ((not (memq (car event)
235 '(mouse-movement scroll-bar-movement)))
236 (if (consp event)
237 (setq unread-command-events
238 (cons event unread-command-events)))
239 (setq done t))
240 ((not (eq (car mouse) start-event-frame))
241 nil)
242 ((null (car (cdr mouse)))
243 nil)
245 (setq y (cdr (cdr mouse))
246 edges (window-edges)
247 top (nth 1 edges)
248 bot (nth 3 edges))
249 ;; scale back a move that would make the
250 ;; window too short.
251 (cond ((< (- y top -1) window-min-height)
252 (setq y (+ top window-min-height -1))))
253 ;; compute size change needed
254 (setq growth (- y bot -1)
255 wconfig (current-window-configuration))
256 ;; grow/shrink minibuffer?
257 (if should-enlarge-minibuffer
258 (progn
259 ;; yes. briefly select minibuffer so
260 ;; enlarge-window will affect the
261 ;; correct window.
262 (select-window minibuffer)
263 ;; scale back shrinkage if it would
264 ;; make the minibuffer less than 1
265 ;; line tall.
266 (if (and (> growth 0)
267 (< (- (window-height minibuffer)
268 growth)
270 (setq growth (1- (window-height minibuffer))))
271 (enlarge-window (- growth))
272 (select-window start-event-window))
273 ;; no. grow/shrink the selected window
274 (enlarge-window growth))
275 ;; if this window's growth caused another
276 ;; window to be deleted because it was too
277 ;; short, rescind the change.
279 ;; if size change caused space to be stolen
280 ;; from a window above this one, rescind the
281 ;; change, but only if we didn't grow/srhink
282 ;; the minibuffer. minibuffer size changes
283 ;; can cause all windows to shrink... no way
284 ;; around it.
285 (if (or (/= start-nwindows (count-windows t))
286 (and (not should-enlarge-minibuffer)
287 (/= top (nth 1 (window-edges)))))
288 (set-window-configuration wconfig)))))))))
290 (defun mouse-drag-vertical-line (start-event)
291 "Change the width of a window by dragging on the vertical line."
292 (interactive "e")
293 ;; Give temporary modes such as isearch a chance to turn off.
294 (run-hooks 'mouse-leave-buffer-hook)
295 (let ((done nil)
296 (echo-keystrokes 0)
297 (start-event-frame (window-frame (car (car (cdr start-event)))))
298 (start-event-window (car (car (cdr start-event))))
299 (start-nwindows (count-windows t))
300 (old-selected-window (selected-window))
301 event mouse x left right edges wconfig growth)
302 (if (one-window-p t)
303 (error "Attempt to resize sole ordinary window"))
304 (if (= (nth 2 (window-edges start-event-window))
305 (frame-width start-event-frame))
306 (error "Attempt to drag rightmost scrollbar"))
307 (track-mouse
308 (progn
309 ;; enlarge-window only works on the selected window, so
310 ;; we must select the window where the start event originated.
311 ;; unwind-protect will restore the old selected window later.
312 (select-window start-event-window)
313 ;; loop reading events and sampling the position of
314 ;; the mouse.
315 (while (not done)
316 (setq event (read-event)
317 mouse (mouse-position))
318 ;; do nothing if
319 ;; - there is a switch-frame event.
320 ;; - the mouse isn't in the frame that we started in
321 ;; - the mouse isn't in any Emacs frame
322 ;; drag if
323 ;; - there is a mouse-movement event
324 ;; - there is a scroll-bar-movement event
325 ;; (same as mouse movement for our purposes)
326 ;; quit if
327 ;; - there is a keyboard event or some other unknown event
328 ;; unknown event.
329 (cond ((integerp event)
330 (setq done t))
331 ((eq (car event) 'switch-frame)
332 nil)
333 ((not (memq (car event)
334 '(mouse-movement scroll-bar-movement)))
335 (if (consp event)
336 (setq unread-command-events
337 (cons event unread-command-events)))
338 (setq done t))
339 ((not (eq (car mouse) start-event-frame))
340 nil)
341 ((null (car (cdr mouse)))
342 nil)
344 (setq x (car (cdr mouse))
345 edges (window-edges)
346 left (nth 0 edges)
347 right (nth 2 edges))
348 ;; scale back a move that would make the
349 ;; window too thin.
350 (cond ((< (- x left -1) window-min-width)
351 (setq x (+ left window-min-width -1))))
352 ;; compute size change needed
353 (setq growth (- x right -1)
354 wconfig (current-window-configuration))
355 (enlarge-window growth t)
356 ;; if this window's growth caused another
357 ;; window to be deleted because it was too
358 ;; thin, rescind the change.
360 ;; if size change caused space to be stolen
361 ;; from a window to the left of this one,
362 ;; rescind the change.
363 (if (or (/= start-nwindows (count-windows t))
364 (/= left (nth 0 (window-edges))))
365 (set-window-configuration wconfig)))))))))
367 (defun mouse-set-point (event)
368 "Move point to the position clicked on with the mouse.
369 This should be bound to a mouse click event type."
370 (interactive "e")
371 (mouse-minibuffer-check event)
372 ;; Use event-end in case called from mouse-drag-region.
373 ;; If EVENT is a click, event-end and event-start give same value.
374 (let ((posn (event-end event)))
375 (if (not (windowp (posn-window posn)))
376 (error "Cursor not in text area of window"))
377 (select-window (posn-window posn))
378 (if (numberp (posn-point posn))
379 (goto-char (posn-point posn)))))
381 (defvar mouse-last-region-beg nil)
382 (defvar mouse-last-region-end nil)
383 (defvar mouse-last-region-tick nil)
385 (defun mouse-region-match ()
386 "Return non-nil if there's an active region that was set with the mouse."
387 (and (mark t) mark-active
388 (eq mouse-last-region-beg (region-beginning))
389 (eq mouse-last-region-end (region-end))
390 (eq mouse-last-region-tick (buffer-modified-tick))))
392 (defun mouse-set-region (click)
393 "Set the region to the text dragged over, and copy to kill ring.
394 This should be bound to a mouse drag event."
395 (interactive "e")
396 (mouse-minibuffer-check click)
397 (let ((posn (event-start click))
398 (end (event-end click)))
399 (select-window (posn-window posn))
400 (if (numberp (posn-point posn))
401 (goto-char (posn-point posn)))
402 ;; If mark is highlighted, no need to bounce the cursor.
403 ;; On X, we highlight while dragging, thus once again no need to bounce.
404 (or transient-mark-mode
405 (eq (framep (selected-frame)) 'x)
406 (eq (framep (selected-frame)) 'pc)
407 (eq (framep (selected-frame)) 'win32)
408 (sit-for 1))
409 (push-mark)
410 (set-mark (point))
411 (if (numberp (posn-point end))
412 (goto-char (posn-point end)))
413 ;; Don't set this-command to kill-region, so that a following
414 ;; C-w will not double the text in the kill ring.
415 ;; Ignore last-command so we don't append to a preceding kill.
416 (let (this-command last-command)
417 (copy-region-as-kill (mark) (point)))
418 (mouse-set-region-1)))
420 (defun mouse-set-region-1 ()
421 (setq mouse-last-region-beg (region-beginning))
422 (setq mouse-last-region-end (region-end))
423 (setq mouse-last-region-tick (buffer-modified-tick)))
425 (defvar mouse-scroll-delay 0.25
426 "*The pause between scroll steps caused by mouse drags, in seconds.
427 If you drag the mouse beyond the edge of a window, Emacs scrolls the
428 window to bring the text beyond that edge into view, with a delay of
429 this many seconds between scroll steps. Scrolling stops when you move
430 the mouse back into the window, or release the button.
431 This variable's value may be non-integral.
432 Setting this to zero causes Emacs to scroll as fast as it can.")
434 (defvar mouse-scroll-min-lines 1
435 "*The minimum number of lines scrolled by dragging mouse out of window.
436 Moving the mouse out the top or bottom edge of the window begins
437 scrolling repeatedly. The number of lines scrolled per repetition
438 is normally equal to the number of lines beyond the window edge that
439 the mouse has moved. However, it always scrolls at least the number
440 of lines specified by this variable.")
442 (defun mouse-scroll-subr (window jump &optional overlay start)
443 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
444 If OVERLAY is an overlay, let it stretch from START to the far edge of
445 the newly visible text.
446 Upon exit, point is at the far edge of the newly visible text."
447 (cond
448 ((and (> jump 0) (< jump mouse-scroll-min-lines))
449 (setq jump mouse-scroll-min-lines))
450 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
451 (setq jump (- mouse-scroll-min-lines))))
452 (let ((opoint (point)))
453 (while (progn
454 (goto-char (window-start window))
455 (if (not (zerop (vertical-motion jump window)))
456 (progn
457 (set-window-start window (point))
458 (if (natnump jump)
459 (progn
460 (goto-char (window-end window))
461 ;; window-end doesn't reflect the window's new
462 ;; start position until the next redisplay. Hurrah.
463 (vertical-motion (1- jump) window))
464 (goto-char (window-start window)))
465 (if overlay
466 (move-overlay overlay start (point)))
467 ;; Now that we have scrolled WINDOW properly,
468 ;; put point back where it was for the redisplay
469 ;; so that we don't mess up the selected window.
470 (or (eq window (selected-window))
471 (goto-char opoint))
472 (sit-for mouse-scroll-delay)))))
473 (or (eq window (selected-window))
474 (goto-char opoint))))
476 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
477 (defvar mouse-drag-overlay (make-overlay 1 1))
478 (delete-overlay mouse-drag-overlay)
479 (overlay-put mouse-drag-overlay 'face 'region)
481 (defvar mouse-selection-click-count 0)
483 (defvar mouse-selection-click-count-buffer nil)
485 (defun mouse-drag-region (start-event)
486 "Set the region to the text that the mouse is dragged over.
487 Highlight the drag area as you move the mouse.
488 This must be bound to a button-down mouse event.
489 In Transient Mark mode, the highlighting remains as long as the mark
490 remains active. Otherwise, it remains until the next input event."
491 (interactive "e")
492 (mouse-minibuffer-check start-event)
493 (let* ((echo-keystrokes 0)
494 (start-posn (event-start start-event))
495 (start-point (posn-point start-posn))
496 (start-window (posn-window start-posn))
497 (start-frame (window-frame start-window))
498 (bounds (window-edges start-window))
499 (top (nth 1 bounds))
500 (bottom (if (window-minibuffer-p start-window)
501 (nth 3 bounds)
502 ;; Don't count the mode line.
503 (1- (nth 3 bounds))))
504 (click-count (1- (event-click-count start-event))))
505 (setq mouse-selection-click-count click-count)
506 (setq mouse-selection-click-count-buffer (current-buffer))
507 (mouse-set-point start-event)
508 ;; In case the down click is in the middle of some intangible text,
509 ;; use the end of that text, and put it in START-POINT.
510 (if (< (point) start-point)
511 (goto-char start-point))
512 (setq start-point (point))
513 (let ((range (mouse-start-end start-point start-point click-count)))
514 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
515 (window-buffer start-window)))
516 (deactivate-mark)
517 ;; end-of-range is used only in the single-click case.
518 ;; It is the place where the drag has reached so far
519 ;; (but not outside the window where the drag started).
520 (let (event end end-point last-end-point (end-of-range (point)))
521 (track-mouse
522 (while (progn
523 (setq event (read-event))
524 (or (mouse-movement-p event)
525 (eq (car-safe event) 'switch-frame)))
526 (if (eq (car-safe event) 'switch-frame)
528 (setq end (event-end event)
529 end-point (posn-point end))
530 (if end-point
531 (setq last-end-point end-point))
533 (cond
534 ;; Are we moving within the original window?
535 ((and (eq (posn-window end) start-window)
536 (integer-or-marker-p end-point))
537 ;; Go to START-POINT first, so that when we move to END-POINT,
538 ;; if it's in the middle of intangible text,
539 ;; point jumps in the direction away from START-POINT.
540 (goto-char start-point)
541 (goto-char end-point)
542 (if (zerop (% click-count 3))
543 (setq end-of-range (point)))
544 (let ((range (mouse-start-end start-point (point) click-count)))
545 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
548 (let ((mouse-row (cdr (cdr (mouse-position)))))
549 (cond
550 ((null mouse-row))
551 ((< mouse-row top)
552 (mouse-scroll-subr start-window (- mouse-row top)
553 mouse-drag-overlay start-point)
554 ;; Without this, point tends to jump back to the starting
555 ;; position where the mouse button was pressed down.
556 (setq end-of-range (overlay-start mouse-drag-overlay)))
557 ((>= mouse-row bottom)
558 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
559 mouse-drag-overlay start-point)
560 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
561 (if (consp event)
562 (let ((fun (key-binding (vector (car event)))))
563 ;; Run the binding of the terminating up-event, if possible.
564 ;; In the case of a multiple click, it gives the wrong results,
565 ;; because it would fail to set up a region.
566 (if nil ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
567 ;; In this case, we can just let the up-event execute normally.
568 (let ((end (event-end event)))
569 ;; Set the position in the event before we replay it,
570 ;; because otherwise it may have a position in the wrong
571 ;; buffer.
572 (setcar (cdr end) end-of-range)
573 ;; Delete the overlay before calling the function,
574 ;; because delete-overlay increases buffer-modified-tick.
575 (delete-overlay mouse-drag-overlay)
576 (setq unread-command-events
577 (cons event unread-command-events)))
578 (if (not (= (overlay-start mouse-drag-overlay)
579 (overlay-end mouse-drag-overlay)))
580 (let* ((stop-point (or (posn-point (event-end event)) last-end-point))
581 ;; The end that comes from where we ended the drag.
582 ;; Point goes here.
583 (region-termination
584 (if (and stop-point (< stop-point start-point))
585 (overlay-start mouse-drag-overlay)
586 (overlay-end mouse-drag-overlay)))
587 ;; The end that comes from where we started the drag.
588 ;; Mark goes there.
589 (region-commencement
590 (- (+ (overlay-end mouse-drag-overlay)
591 (overlay-start mouse-drag-overlay))
592 region-termination))
593 last-command this-command)
594 (push-mark region-commencement t t)
595 (goto-char region-termination)
596 (copy-region-as-kill (point) (mark t))
597 (mouse-show-mark)
598 (mouse-set-region-1))
599 (goto-char (overlay-end mouse-drag-overlay))
600 (setq this-command 'mouse-set-point)
601 (delete-overlay mouse-drag-overlay))))
602 (delete-overlay mouse-drag-overlay)))))
604 ;; Commands to handle xterm-style multiple clicks.
606 (defun mouse-skip-word (dir)
607 "Skip over word, over whitespace, or over identical punctuation.
608 If DIR is positive skip forward; if negative, skip backward."
609 (let* ((char (following-char))
610 (syntax (char-to-string (char-syntax char))))
611 (cond ((or (string= syntax "w") (string= syntax " "))
612 (if (< dir 0)
613 (skip-syntax-backward syntax)
614 (skip-syntax-forward syntax)))
615 ((string= syntax "_")
616 (if (< dir 0)
617 (skip-syntax-backward "w_")
618 (skip-syntax-forward "w_")))
619 ((< dir 0)
620 (while (and (not (bobp)) (= (preceding-char) char))
621 (forward-char -1)))
623 (while (and (not (eobp)) (= (following-char) char))
624 (forward-char 1))))))
626 ;; Return a list of region bounds based on START and END according to MODE.
627 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
628 ;; If MODE is 1 then set point to start of word at (min START END),
629 ;; mark to end of word at (max START END).
630 ;; If MODE is 2 then do the same for lines.
631 (defun mouse-start-end (start end mode)
632 (if (> start end)
633 (let ((temp start))
634 (setq start end
635 end temp)))
636 (setq mode (mod mode 3))
637 (cond ((= mode 0)
638 (list start end))
639 ((and (= mode 1)
640 (= start end)
641 (char-after start)
642 (= (char-syntax (char-after start)) ?\())
643 (list start
644 (save-excursion
645 (goto-char start)
646 (forward-sexp 1)
647 (point))))
648 ((and (= mode 1)
649 (= start end)
650 (char-after start)
651 (= (char-syntax (char-after start)) ?\)))
652 (list (save-excursion
653 (goto-char (1+ start))
654 (backward-sexp 1)
655 (point))
656 (1+ start)))
657 ((and (= mode 1)
658 (= start end)
659 (char-after start)
660 (= (char-syntax (char-after start)) ?\"))
661 (let ((open (or (eq start (point-min))
662 (save-excursion
663 (goto-char (- start 1))
664 (looking-at "\\s(\\|\\s \\|\\s>")))))
665 (if open
666 (list start
667 (save-excursion
668 (condition-case nil
669 (progn
670 (goto-char start)
671 (forward-sexp 1)
672 (point))
673 (error end))))
674 (list (1+ start)
675 (save-excursion
676 (condition-case nil
677 (progn
678 (goto-char (1+ start))
679 (backward-sexp 1)
680 (point))
681 (error end)))))))
682 ((= mode 1)
683 (list (save-excursion
684 (goto-char start)
685 (mouse-skip-word -1)
686 (point))
687 (save-excursion
688 (goto-char end)
689 (mouse-skip-word 1)
690 (point))))
691 ((= mode 2)
692 (list (save-excursion
693 (goto-char start)
694 (beginning-of-line 1)
695 (point))
696 (save-excursion
697 (goto-char end)
698 (forward-line 1)
699 (point))))))
701 ;; Subroutine: set the mark where CLICK happened,
702 ;; but don't do anything else.
703 (defun mouse-set-mark-fast (click)
704 (mouse-minibuffer-check click)
705 (let ((posn (event-start click)))
706 (select-window (posn-window posn))
707 (if (numberp (posn-point posn))
708 (push-mark (posn-point posn) t t))))
710 (defun mouse-undouble-last-event (events)
711 (let* ((index (1- (length events)))
712 (last (nthcdr index events))
713 (event (car last))
714 (basic (event-basic-type event))
715 (modifiers (delq 'double (delq 'triple (copy-sequence (event-modifiers event)))))
716 (new
717 (if (consp event)
718 (cons (event-convert-list (nreverse (cons basic modifiers)))
719 (cdr event))
720 event)))
721 (setcar last new)
722 (if (key-binding (apply 'vector events))
724 (setcar last event)
725 nil)))
727 ;; Momentarily show where the mark is, if highlighting doesn't show it.
728 (defun mouse-show-mark ()
729 (if transient-mark-mode
730 (if window-system
731 (delete-overlay mouse-drag-overlay))
732 (if window-system
733 (let ((inhibit-quit t)
734 (echo-keystrokes 0)
735 event events)
736 (move-overlay mouse-drag-overlay (point) (mark t))
737 (while (progn (setq event (read-event))
738 (setq events (append events (list event)))
739 (and (memq 'down (event-modifiers event))
740 (not (key-binding (apply 'vector events)))
741 (not (mouse-undouble-last-event events)))))
742 (setq unread-command-events
743 (nconc events unread-command-events))
744 (setq quit-flag nil)
745 (delete-overlay mouse-drag-overlay))
746 (save-excursion
747 (goto-char (mark t))
748 (sit-for 1)))))
750 (defun mouse-set-mark (click)
751 "Set mark at the position clicked on with the mouse.
752 Display cursor at that position for a second.
753 This must be bound to a mouse click."
754 (interactive "e")
755 (mouse-minibuffer-check click)
756 (select-window (posn-window (event-start click)))
757 ;; We don't use save-excursion because that preserves the mark too.
758 (let ((point-save (point)))
759 (unwind-protect
760 (progn (mouse-set-point click)
761 (push-mark nil t t)
762 (or transient-mark-mode
763 (sit-for 1)))
764 (goto-char point-save))))
766 (defun mouse-kill (click)
767 "Kill the region between point and the mouse click.
768 The text is saved in the kill ring, as with \\[kill-region]."
769 (interactive "e")
770 (mouse-minibuffer-check click)
771 (let* ((posn (event-start click))
772 (click-posn (posn-point posn)))
773 (select-window (posn-window posn))
774 (if (numberp click-posn)
775 (kill-region (min (point) click-posn)
776 (max (point) click-posn)))))
778 (defun mouse-yank-at-click (click arg)
779 "Insert the last stretch of killed text at the position clicked on.
780 Also move point to one end of the text thus inserted (normally the end).
781 Prefix arguments are interpreted as with \\[yank].
782 If `mouse-yank-at-point' is non-nil, insert at point
783 regardless of where you click."
784 (interactive "e\nP")
785 ;; Give temporary modes such as isearch a chance to turn off.
786 (run-hooks 'mouse-leave-buffer-hook)
787 (or mouse-yank-at-point (mouse-set-point click))
788 (setq this-command 'yank)
789 (setq mouse-selection-click-count 0)
790 (yank arg))
792 (defun mouse-kill-ring-save (click)
793 "Copy the region between point and the mouse click in the kill ring.
794 This does not delete the region; it acts like \\[kill-ring-save]."
795 (interactive "e")
796 (mouse-set-mark-fast click)
797 (let (this-command last-command)
798 (kill-ring-save (point) (mark t)))
799 (mouse-show-mark))
801 ;;; This function used to delete the text between point and the mouse
802 ;;; whenever it was equal to the front of the kill ring, but some
803 ;;; people found that confusing.
805 ;;; A list (TEXT START END), describing the text and position of the last
806 ;;; invocation of mouse-save-then-kill.
807 (defvar mouse-save-then-kill-posn nil)
809 (defun mouse-save-then-kill-delete-region (beg end)
810 ;; We must make our own undo boundaries
811 ;; because they happen automatically only for the current buffer.
812 (undo-boundary)
813 (if (or (= beg end) (eq buffer-undo-list t))
814 ;; If we have no undo list in this buffer,
815 ;; just delete.
816 (delete-region beg end)
817 ;; Delete, but make the undo-list entry share with the kill ring.
818 ;; First, delete just one char, so in case buffer is being modified
819 ;; for the first time, the undo list records that fact.
820 (let (before-change-function after-change-function
821 before-change-functions after-change-functions)
822 (delete-region beg
823 (+ beg (if (> end beg) 1 -1))))
824 (let ((buffer-undo-list buffer-undo-list))
825 ;; Undo that deletion--but don't change the undo list!
826 (let (before-change-function after-change-function
827 before-change-functions after-change-functions)
828 (primitive-undo 1 buffer-undo-list))
829 ;; Now delete the rest of the specified region,
830 ;; but don't record it.
831 (setq buffer-undo-list t)
832 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
833 (error "Lossage in mouse-save-then-kill-delete-region"))
834 (delete-region beg end))
835 (let ((tail buffer-undo-list))
836 ;; Search back in buffer-undo-list for the string
837 ;; that came from deleting one character.
838 (while (and tail (not (stringp (car (car tail)))))
839 (setq tail (cdr tail)))
840 ;; Replace it with an entry for the entire deleted text.
841 (and tail
842 (setcar tail (cons (car kill-ring) (min beg end))))))
843 (undo-boundary))
845 (defun mouse-save-then-kill (click)
846 "Save text to point in kill ring; the second time, kill the text.
847 If the text between point and the mouse is the same as what's
848 at the front of the kill ring, this deletes the text.
849 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
850 which prepares for a second click to delete the text.
852 If you have selected words or lines, this command extends the
853 selection through the word or line clicked on. If you do this
854 again in a different position, it extends the selection again.
855 If you do this twice in the same position, the selection is killed."
856 (interactive "e")
857 (let ((before-scroll point-before-scroll))
858 (mouse-minibuffer-check click)
859 (let ((click-posn (posn-point (event-start click)))
860 ;; Don't let a subsequent kill command append to this one:
861 ;; prevent setting this-command to kill-region.
862 (this-command this-command))
863 (if (and (save-excursion
864 (set-buffer (window-buffer (posn-window (event-start click))))
865 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
866 ;; Don't be fooled by a recent click in some other buffer.
867 (eq mouse-selection-click-count-buffer
868 (current-buffer)))))
869 (if (not (and (eq last-command 'mouse-save-then-kill)
870 (equal click-posn
871 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
872 ;; Find both ends of the object selected by this click.
873 (let* ((range
874 (mouse-start-end click-posn click-posn
875 mouse-selection-click-count)))
876 ;; Move whichever end is closer to the click.
877 ;; That's what xterm does, and it seems reasonable.
878 (if (< (abs (- click-posn (mark t)))
879 (abs (- click-posn (point))))
880 (set-mark (car range))
881 (goto-char (nth 1 range)))
882 ;; We have already put the old region in the kill ring.
883 ;; Replace it with the extended region.
884 ;; (It would be annoying to make a separate entry.)
885 (kill-new (buffer-substring (point) (mark t)) t)
886 (mouse-set-region-1)
887 ;; Arrange for a repeated mouse-3 to kill this region.
888 (setq mouse-save-then-kill-posn
889 (list (car kill-ring) (point) click-posn))
890 (mouse-show-mark))
891 ;; If we click this button again without moving it,
892 ;; that time kill.
893 (mouse-save-then-kill-delete-region (mark) (point))
894 (setq mouse-selection-click-count 0)
895 (setq mouse-save-then-kill-posn nil))
896 (if (and (eq last-command 'mouse-save-then-kill)
897 mouse-save-then-kill-posn
898 (eq (car mouse-save-then-kill-posn) (car kill-ring))
899 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
900 ;; If this is the second time we've called
901 ;; mouse-save-then-kill, delete the text from the buffer.
902 (progn
903 (mouse-save-then-kill-delete-region (point) (mark))
904 ;; After we kill, another click counts as "the first time".
905 (setq mouse-save-then-kill-posn nil))
906 ;; This is not a repetition.
907 ;; We are adjusting an old selection or creating a new one.
908 (if (or (and (eq last-command 'mouse-save-then-kill)
909 mouse-save-then-kill-posn)
910 (and mark-active transient-mark-mode)
911 (and (memq last-command
912 '(mouse-drag-region mouse-set-region))
913 (or mark-even-if-inactive
914 (not transient-mark-mode))))
915 ;; We have a selection or suitable region, so adjust it.
916 (let* ((posn (event-start click))
917 (new (posn-point posn)))
918 (select-window (posn-window posn))
919 (if (numberp new)
920 (progn
921 ;; Move whichever end of the region is closer to the click.
922 ;; That is what xterm does, and it seems reasonable.
923 (if (< (abs (- new (point))) (abs (- new (mark t))))
924 (goto-char new)
925 (set-mark new))
926 (setq deactivate-mark nil)))
927 (kill-new (buffer-substring (point) (mark t)) t)
928 (mouse-show-mark))
929 ;; Set the mark where point is, then move where clicked.
930 (mouse-set-mark-fast click)
931 (if before-scroll
932 (goto-char before-scroll))
933 (exchange-point-and-mark)
934 (kill-new (buffer-substring (point) (mark t)))
935 (if window-system
936 (mouse-show-mark)))
937 (mouse-set-region-1)
938 (setq mouse-save-then-kill-posn
939 (list (car kill-ring) (point) click-posn)))))))
941 (global-set-key [M-mouse-1] 'mouse-start-secondary)
942 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
943 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
944 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
945 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
947 ;; An overlay which records the current secondary selection
948 ;; or else is deleted when there is no secondary selection.
949 ;; May be nil.
950 (defvar mouse-secondary-overlay nil)
952 (defvar mouse-secondary-click-count 0)
954 ;; A marker which records the specified first end for a secondary selection.
955 ;; May be nil.
956 (defvar mouse-secondary-start nil)
958 (defun mouse-start-secondary (click)
959 "Set one end of the secondary selection to the position clicked on.
960 Use \\[mouse-secondary-save-then-kill] to set the other end
961 and complete the secondary selection."
962 (interactive "e")
963 (mouse-minibuffer-check click)
964 (let ((posn (event-start click)))
965 (save-excursion
966 (set-buffer (window-buffer (posn-window posn)))
967 ;; Cancel any preexisting secondary selection.
968 (if mouse-secondary-overlay
969 (delete-overlay mouse-secondary-overlay))
970 (if (numberp (posn-point posn))
971 (progn
972 (or mouse-secondary-start
973 (setq mouse-secondary-start (make-marker)))
974 (move-marker mouse-secondary-start (posn-point posn)))))))
976 (defun mouse-set-secondary (click)
977 "Set the secondary selection to the text that the mouse is dragged over.
978 This must be bound to a mouse drag event."
979 (interactive "e")
980 (mouse-minibuffer-check click)
981 (let ((posn (event-start click))
983 (end (event-end click)))
984 (save-excursion
985 (set-buffer (window-buffer (posn-window posn)))
986 (if (numberp (posn-point posn))
987 (setq beg (posn-point posn)))
988 (if mouse-secondary-overlay
989 (move-overlay mouse-secondary-overlay beg (posn-point end))
990 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
991 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
993 (defun mouse-drag-secondary (start-event)
994 "Set the secondary selection to the text that the mouse is dragged over.
995 Highlight the drag area as you move the mouse.
996 This must be bound to a button-down mouse event."
997 (interactive "e")
998 (mouse-minibuffer-check start-event)
999 (let* ((echo-keystrokes 0)
1000 (start-posn (event-start start-event))
1001 (start-point (posn-point start-posn))
1002 (start-window (posn-window start-posn))
1003 (start-frame (window-frame start-window))
1004 (bounds (window-edges start-window))
1005 (top (nth 1 bounds))
1006 (bottom (if (window-minibuffer-p start-window)
1007 (nth 3 bounds)
1008 ;; Don't count the mode line.
1009 (1- (nth 3 bounds))))
1010 (click-count (1- (event-click-count start-event))))
1011 (save-excursion
1012 (set-buffer (window-buffer start-window))
1013 (setq mouse-secondary-click-count click-count)
1014 (or mouse-secondary-overlay
1015 (setq mouse-secondary-overlay
1016 (make-overlay (point) (point))))
1017 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1018 (if (> (mod click-count 3) 0)
1019 ;; Double or triple press: make an initial selection
1020 ;; of one word or line.
1021 (let ((range (mouse-start-end start-point start-point click-count)))
1022 (set-marker mouse-secondary-start nil)
1023 (move-overlay mouse-secondary-overlay 1 1
1024 (window-buffer start-window))
1025 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1026 (window-buffer start-window)))
1027 ;; Single-press: cancel any preexisting secondary selection.
1028 (or mouse-secondary-start
1029 (setq mouse-secondary-start (make-marker)))
1030 (set-marker mouse-secondary-start start-point)
1031 (delete-overlay mouse-secondary-overlay))
1032 (let (event end end-point)
1033 (track-mouse
1034 (while (progn
1035 (setq event (read-event))
1036 (or (mouse-movement-p event)
1037 (eq (car-safe event) 'switch-frame)))
1039 (if (eq (car-safe event) 'switch-frame)
1041 (setq end (event-end event)
1042 end-point (posn-point end))
1043 (cond
1044 ;; Are we moving within the original window?
1045 ((and (eq (posn-window end) start-window)
1046 (integer-or-marker-p end-point))
1047 (let ((range (mouse-start-end start-point end-point
1048 click-count)))
1049 (if (or (/= start-point end-point)
1050 (null (marker-position mouse-secondary-start)))
1051 (progn
1052 (set-marker mouse-secondary-start nil)
1053 (move-overlay mouse-secondary-overlay
1054 (car range) (nth 1 range))))))
1056 (let ((mouse-row (cdr (cdr (mouse-position)))))
1057 (cond
1058 ((null mouse-row))
1059 ((< mouse-row top)
1060 (mouse-scroll-subr start-window (- mouse-row top)
1061 mouse-secondary-overlay start-point))
1062 ((>= mouse-row bottom)
1063 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1064 mouse-secondary-overlay start-point)))))))))
1066 (if (consp event)
1067 ;;; (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
1068 ;;; (eq (posn-window (event-end event)) start-window)
1069 ;;; (numberp (posn-point (event-end event)))
1070 (if (marker-position mouse-secondary-start)
1071 (save-window-excursion
1072 (delete-overlay mouse-secondary-overlay)
1073 (x-set-selection 'SECONDARY nil)
1074 (select-window start-window)
1075 (save-excursion
1076 (goto-char mouse-secondary-start)
1077 (sit-for 1)))
1078 (x-set-selection
1079 'SECONDARY
1080 (buffer-substring (overlay-start mouse-secondary-overlay)
1081 (overlay-end mouse-secondary-overlay)))))))))
1083 (defun mouse-yank-secondary (click)
1084 "Insert the secondary selection at the position clicked on.
1085 Move point to the end of the inserted text.
1086 If `mouse-yank-at-point' is non-nil, insert at point
1087 regardless of where you click."
1088 (interactive "e")
1089 ;; Give temporary modes such as isearch a chance to turn off.
1090 (run-hooks 'mouse-leave-buffer-hook)
1091 (or mouse-yank-at-point (mouse-set-point click))
1092 (insert (x-get-selection 'SECONDARY)))
1094 (defun mouse-kill-secondary ()
1095 "Kill the text in the secondary selection.
1096 This is intended more as a keyboard command than as a mouse command
1097 but it can work as either one.
1099 The current buffer (in case of keyboard use), or the buffer clicked on,
1100 must be the one that the secondary selection is in. This requirement
1101 is to prevent accidents."
1102 (interactive)
1103 (let* ((keys (this-command-keys))
1104 (click (elt keys (1- (length keys)))))
1105 (or (eq (overlay-buffer mouse-secondary-overlay)
1106 (if (listp click)
1107 (window-buffer (posn-window (event-start click)))
1108 (current-buffer)))
1109 (error "Select or click on the buffer where the secondary selection is")))
1110 (let (this-command)
1111 (save-excursion
1112 (set-buffer (overlay-buffer mouse-secondary-overlay))
1113 (kill-region (overlay-start mouse-secondary-overlay)
1114 (overlay-end mouse-secondary-overlay))))
1115 (delete-overlay mouse-secondary-overlay)
1116 ;;; (x-set-selection 'SECONDARY nil)
1117 (setq mouse-secondary-overlay nil))
1119 (defun mouse-secondary-save-then-kill (click)
1120 "Save text to point in kill ring; the second time, kill the text.
1121 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1122 If the text between where you did \\[mouse-start-secondary] and where
1123 you use this command matches the text at the front of the kill ring,
1124 this command deletes the text.
1125 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1126 which prepares for a second click with this command to delete the text.
1128 If you have already made a secondary selection in that buffer,
1129 this command extends or retracts the selection to where you click.
1130 If you do this again in a different position, it extends or retracts
1131 again. If you do this twice in the same position, it kills the selection."
1132 (interactive "e")
1133 (mouse-minibuffer-check click)
1134 (let ((posn (event-start click))
1135 (click-posn (posn-point (event-start click)))
1136 ;; Don't let a subsequent kill command append to this one:
1137 ;; prevent setting this-command to kill-region.
1138 (this-command this-command))
1139 (or (eq (window-buffer (posn-window posn))
1140 (or (and mouse-secondary-overlay
1141 (overlay-buffer mouse-secondary-overlay))
1142 (if mouse-secondary-start
1143 (marker-buffer mouse-secondary-start))))
1144 (error "Wrong buffer"))
1145 (save-excursion
1146 (set-buffer (window-buffer (posn-window posn)))
1147 (if (> (mod mouse-secondary-click-count 3) 0)
1148 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1149 (equal click-posn
1150 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1151 ;; Find both ends of the object selected by this click.
1152 (let* ((range
1153 (mouse-start-end click-posn click-posn
1154 mouse-secondary-click-count)))
1155 ;; Move whichever end is closer to the click.
1156 ;; That's what xterm does, and it seems reasonable.
1157 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1158 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1159 (move-overlay mouse-secondary-overlay (car range)
1160 (overlay-end mouse-secondary-overlay))
1161 (move-overlay mouse-secondary-overlay
1162 (overlay-start mouse-secondary-overlay)
1163 (nth 1 range)))
1164 ;; We have already put the old region in the kill ring.
1165 ;; Replace it with the extended region.
1166 ;; (It would be annoying to make a separate entry.)
1167 (kill-new (buffer-substring
1168 (overlay-start mouse-secondary-overlay)
1169 (overlay-end mouse-secondary-overlay)) t)
1170 ;; Arrange for a repeated mouse-3 to kill this region.
1171 (setq mouse-save-then-kill-posn
1172 (list (car kill-ring) (point) click-posn)))
1173 ;; If we click this button again without moving it,
1174 ;; that time kill.
1175 (progn
1176 (mouse-save-then-kill-delete-region
1177 (overlay-start mouse-secondary-overlay)
1178 (overlay-end mouse-secondary-overlay))
1179 (setq mouse-save-then-kill-posn nil)
1180 (setq mouse-secondary-click-count 0)
1181 (delete-overlay mouse-secondary-overlay)))
1182 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1183 mouse-save-then-kill-posn
1184 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1185 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1186 ;; If this is the second time we've called
1187 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1188 (progn
1189 (mouse-save-then-kill-delete-region
1190 (overlay-start mouse-secondary-overlay)
1191 (overlay-end mouse-secondary-overlay))
1192 (setq mouse-save-then-kill-posn nil)
1193 (delete-overlay mouse-secondary-overlay))
1194 (if (overlay-start mouse-secondary-overlay)
1195 ;; We have a selection, so adjust it.
1196 (progn
1197 (if (numberp click-posn)
1198 (progn
1199 ;; Move whichever end of the region is closer to the click.
1200 ;; That is what xterm does, and it seems reasonable.
1201 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1202 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1203 (move-overlay mouse-secondary-overlay click-posn
1204 (overlay-end mouse-secondary-overlay))
1205 (move-overlay mouse-secondary-overlay
1206 (overlay-start mouse-secondary-overlay)
1207 click-posn))
1208 (setq deactivate-mark nil)))
1209 (if (eq last-command 'mouse-secondary-save-then-kill)
1210 ;; If the front of the kill ring comes from
1211 ;; an immediately previous use of this command,
1212 ;; replace it with the extended region.
1213 ;; (It would be annoying to make a separate entry.)
1214 (kill-new (buffer-substring
1215 (overlay-start mouse-secondary-overlay)
1216 (overlay-end mouse-secondary-overlay)) t)
1217 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1218 (overlay-end mouse-secondary-overlay))))
1219 (if mouse-secondary-start
1220 ;; All we have is one end of a selection,
1221 ;; so put the other end here.
1222 (let ((start (+ 0 mouse-secondary-start)))
1223 (kill-ring-save start click-posn)
1224 (if mouse-secondary-overlay
1225 (move-overlay mouse-secondary-overlay start click-posn)
1226 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1227 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1228 (setq mouse-save-then-kill-posn
1229 (list (car kill-ring) (point) click-posn))))
1230 (if (overlay-buffer mouse-secondary-overlay)
1231 (x-set-selection 'SECONDARY
1232 (buffer-substring
1233 (overlay-start mouse-secondary-overlay)
1234 (overlay-end mouse-secondary-overlay)))))))
1236 (defvar mouse-menu-buffer-maxlen 20
1237 "*Number of buffers in one pane (submenu) of the buffer menu.
1238 If we have lots of buffers, divide them into groups of
1239 `mouse-menu-buffer-maxlen' and make a pane (or submenu) for each one.")
1241 (defun mouse-buffer-menu (event)
1242 "Pop up a menu of buffers for selection with the mouse.
1243 This switches buffers in the window that you clicked on,
1244 and selects that window."
1245 (interactive "e")
1246 (mouse-minibuffer-check event)
1247 (let* ((buffers
1248 ;; Make an alist of (MENU-ITEM . BUFFER).
1249 (let ((tail (buffer-list))
1250 (maxlen 0)
1251 head)
1252 (while tail
1253 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1254 (setq maxlen
1255 (max maxlen
1256 (length (buffer-name (car tail))))))
1257 (setq tail (cdr tail)))
1258 (setq tail (buffer-list))
1259 (while tail
1260 (let ((elt (car tail)))
1261 (if (/= (aref (buffer-name elt) 0) ?\ )
1262 (setq head
1263 (cons
1264 (cons
1265 (format
1266 (format "%%%ds %%s%%s %%s" maxlen)
1267 (buffer-name elt)
1268 (if (buffer-modified-p elt) "*" " ")
1269 (save-excursion
1270 (set-buffer elt)
1271 (if buffer-read-only "%" " "))
1272 (or (buffer-file-name elt)
1273 (save-excursion
1274 (set-buffer elt)
1275 (if list-buffers-directory
1276 (expand-file-name
1277 list-buffers-directory)))
1278 ""))
1279 elt)
1280 head))))
1281 (setq tail (cdr tail)))
1282 ;; Compensate for the reversal that the above loop does.
1283 (nreverse head)))
1284 (menu
1285 ;; If we have lots of buffers, divide them into groups of 20
1286 ;; and make a pane (or submenu) for each one.
1287 (if (> (length buffers) (/ (* mouse-menu-buffer-maxlen 3) 2))
1288 (let ((buffers buffers) sublists next
1289 (i 1))
1290 (while buffers
1291 ;; Pull off the next mouse-menu-buffer-maxlen buffers
1292 ;; and make them the next element of sublist.
1293 (setq next (nthcdr mouse-menu-buffer-maxlen buffers))
1294 (if next
1295 (setcdr (nthcdr (1- mouse-menu-buffer-maxlen) buffers)
1296 nil))
1297 (setq sublists (cons (cons (format "Buffers %d" i) buffers)
1298 sublists))
1299 (setq i (1+ i))
1300 (setq buffers next))
1301 (cons "Buffer Menu" (nreverse sublists)))
1302 ;; Few buffers--put them all in one pane.
1303 (list "Buffer Menu" (cons "Select Buffer" buffers)))))
1304 (let ((buf (x-popup-menu event menu))
1305 (window (posn-window (event-start event))))
1306 (if buf
1307 (progn
1308 (or (framep window) (select-window window))
1309 (switch-to-buffer buf))))))
1311 ;;; These need to be rewritten for the new scroll bar implementation.
1313 ;;;!! ;; Commands for the scroll bar.
1314 ;;;!!
1315 ;;;!! (defun mouse-scroll-down (click)
1316 ;;;!! (interactive "@e")
1317 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1318 ;;;!!
1319 ;;;!! (defun mouse-scroll-up (click)
1320 ;;;!! (interactive "@e")
1321 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1322 ;;;!!
1323 ;;;!! (defun mouse-scroll-down-full ()
1324 ;;;!! (interactive "@")
1325 ;;;!! (scroll-down nil))
1326 ;;;!!
1327 ;;;!! (defun mouse-scroll-up-full ()
1328 ;;;!! (interactive "@")
1329 ;;;!! (scroll-up nil))
1330 ;;;!!
1331 ;;;!! (defun mouse-scroll-move-cursor (click)
1332 ;;;!! (interactive "@e")
1333 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1334 ;;;!!
1335 ;;;!! (defun mouse-scroll-absolute (event)
1336 ;;;!! (interactive "@e")
1337 ;;;!! (let* ((pos (car event))
1338 ;;;!! (position (car pos))
1339 ;;;!! (length (car (cdr pos))))
1340 ;;;!! (if (<= length 0) (setq length 1))
1341 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1342 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1343 ;;;!! position)
1344 ;;;!! length)
1345 ;;;!! scale-factor)))
1346 ;;;!! (goto-char newpos)
1347 ;;;!! (recenter '(4)))))
1348 ;;;!!
1349 ;;;!! (defun mouse-scroll-left (click)
1350 ;;;!! (interactive "@e")
1351 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1352 ;;;!!
1353 ;;;!! (defun mouse-scroll-right (click)
1354 ;;;!! (interactive "@e")
1355 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1356 ;;;!!
1357 ;;;!! (defun mouse-scroll-left-full ()
1358 ;;;!! (interactive "@")
1359 ;;;!! (scroll-left nil))
1360 ;;;!!
1361 ;;;!! (defun mouse-scroll-right-full ()
1362 ;;;!! (interactive "@")
1363 ;;;!! (scroll-right nil))
1364 ;;;!!
1365 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1366 ;;;!! (interactive "@e")
1367 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1368 ;;;!!
1369 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1370 ;;;!! (interactive "@e")
1371 ;;;!! (let* ((pos (car event))
1372 ;;;!! (position (car pos))
1373 ;;;!! (length (car (cdr pos))))
1374 ;;;!! (set-window-hscroll (selected-window) 33)))
1375 ;;;!!
1376 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1377 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1378 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1379 ;;;!!
1380 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1381 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1382 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1383 ;;;!!
1384 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1385 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1386 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1387 ;;;!!
1388 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1389 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1390 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1391 ;;;!!
1392 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1393 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1394 ;;;!! 'mouse-scroll-absolute-horizontally)
1395 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1396 ;;;!!
1397 ;;;!! (global-set-key [horizontal-slider mouse-1]
1398 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1399 ;;;!! (global-set-key [horizontal-slider mouse-2]
1400 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1401 ;;;!! (global-set-key [horizontal-slider mouse-3]
1402 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1403 ;;;!!
1404 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1405 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1406 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1407 ;;;!!
1408 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1409 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1410 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1411 ;;;!!
1412 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1413 ;;;!! 'mouse-split-window-horizontally)
1414 ;;;!! (global-set-key [mode-line S-mouse-2]
1415 ;;;!! 'mouse-split-window-horizontally)
1416 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1417 ;;;!! 'mouse-split-window)
1419 ;;;!! ;;;;
1420 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1421 ;;;!! ;;;; are of the form:
1422 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1423 ;;;!! ;;
1424 ;;;!! ;;;;
1425 ;;;!! ;;;; Dynamically track mouse coordinates
1426 ;;;!! ;;;;
1427 ;;;!! ;;
1428 ;;;!! ;;(defun track-mouse (event)
1429 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1430 ;;;!! ;; (interactive "@e")
1431 ;;;!! ;; (while mouse-grabbed
1432 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1433 ;;;!! ;; (abs-x (car pos))
1434 ;;;!! ;; (abs-y (cdr pos))
1435 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1436 ;;;!! ;; (list (car pos) (cdr pos))
1437 ;;;!! ;; (selected-window))))
1438 ;;;!! ;; (if (consp relative-coordinate)
1439 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1440 ;;;!! ;; (car relative-coordinate)
1441 ;;;!! ;; (car (cdr relative-coordinate)))
1442 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1443 ;;;!!
1444 ;;;!! ;;
1445 ;;;!! ;; Dynamically put a box around the line indicated by point
1446 ;;;!! ;;
1447 ;;;!! ;;
1448 ;;;!! ;;(require 'backquote)
1449 ;;;!! ;;
1450 ;;;!! ;;(defun mouse-select-buffer-line (event)
1451 ;;;!! ;; (interactive "@e")
1452 ;;;!! ;; (let ((relative-coordinate
1453 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1454 ;;;!! ;; (abs-y (car (cdr (car event)))))
1455 ;;;!! ;; (if (consp relative-coordinate)
1456 ;;;!! ;; (progn
1457 ;;;!! ;; (save-excursion
1458 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1459 ;;;!! ;; (x-draw-rectangle
1460 ;;;!! ;; (selected-screen)
1461 ;;;!! ;; abs-y 0
1462 ;;;!! ;; (save-excursion
1463 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1464 ;;;!! ;; (end-of-line)
1465 ;;;!! ;; (push-mark nil t)
1466 ;;;!! ;; (beginning-of-line)
1467 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1468 ;;;!! ;; (sit-for 1)
1469 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1470 ;;;!! ;;
1471 ;;;!! ;;(defvar last-line-drawn nil)
1472 ;;;!! ;;(defvar begin-delim "[^ \t]")
1473 ;;;!! ;;(defvar end-delim "[^ \t]")
1474 ;;;!! ;;
1475 ;;;!! ;;(defun mouse-boxing (event)
1476 ;;;!! ;; (interactive "@e")
1477 ;;;!! ;; (save-excursion
1478 ;;;!! ;; (let ((screen (selected-screen)))
1479 ;;;!! ;; (while (= (x-mouse-events) 0)
1480 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1481 ;;;!! ;; (abs-x (car pos))
1482 ;;;!! ;; (abs-y (cdr pos))
1483 ;;;!! ;; (relative-coordinate
1484 ;;;!! ;; (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
1485 ;;;!! ;; (selected-window)))
1486 ;;;!! ;; (begin-reg nil)
1487 ;;;!! ;; (end-reg nil)
1488 ;;;!! ;; (end-column nil)
1489 ;;;!! ;; (begin-column nil))
1490 ;;;!! ;; (if (and (consp relative-coordinate)
1491 ;;;!! ;; (or (not last-line-drawn)
1492 ;;;!! ;; (not (= last-line-drawn abs-y))))
1493 ;;;!! ;; (progn
1494 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1495 ;;;!! ;; (if (= (following-char) 10)
1496 ;;;!! ;; ()
1497 ;;;!! ;; (progn
1498 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1499 ;;;!! ;; (setq begin-column (1- (current-column)))
1500 ;;;!! ;; (end-of-line)
1501 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1502 ;;;!! ;; (setq end-column (1+ (current-column)))
1503 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1504 ;;;!! ;; (x-draw-rectangle screen
1505 ;;;!! ;; (setq last-line-drawn abs-y)
1506 ;;;!! ;; begin-column
1507 ;;;!! ;; (- end-column begin-column) 1))))))))))
1508 ;;;!! ;;
1509 ;;;!! ;;(defun mouse-erase-box ()
1510 ;;;!! ;; (interactive)
1511 ;;;!! ;; (if last-line-drawn
1512 ;;;!! ;; (progn
1513 ;;;!! ;; (x-erase-rectangle (selected-screen))
1514 ;;;!! ;; (setq last-line-drawn nil))))
1515 ;;;!!
1516 ;;;!! ;;; (defun test-x-rectangle ()
1517 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1518 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1519 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1520 ;;;!!
1521 ;;;!! ;;
1522 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1523 ;;;!! ;;
1524 ;;;!!
1525 ;;;!! (defvar double-start nil)
1526 ;;;!! (defconst double-click-interval 300
1527 ;;;!! "Max ticks between clicks")
1528 ;;;!!
1529 ;;;!! (defun double-down (event)
1530 ;;;!! (interactive "@e")
1531 ;;;!! (if double-start
1532 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1533 ;;;!! (if (< interval double-click-interval)
1534 ;;;!! (progn
1535 ;;;!! (backward-up-list 1)
1536 ;;;!! ;; (message "Interval %d" interval)
1537 ;;;!! (sleep-for 1)))
1538 ;;;!! (setq double-start nil))
1539 ;;;!! (setq double-start (nth 4 event))))
1540 ;;;!!
1541 ;;;!! (defun double-up (event)
1542 ;;;!! (interactive "@e")
1543 ;;;!! (and double-start
1544 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1545 ;;;!! (setq double-start nil)))
1546 ;;;!!
1547 ;;;!! ;;; (defun x-test-doubleclick ()
1548 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1549 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1550 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1551 ;;;!!
1552 ;;;!! ;;
1553 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1554 ;;;!! ;;
1555 ;;;!!
1556 ;;;!! (defvar scrolled-lines 0)
1557 ;;;!! (defconst scroll-speed 1)
1558 ;;;!!
1559 ;;;!! (defun incr-scroll-down (event)
1560 ;;;!! (interactive "@e")
1561 ;;;!! (setq scrolled-lines 0)
1562 ;;;!! (incremental-scroll scroll-speed))
1563 ;;;!!
1564 ;;;!! (defun incr-scroll-up (event)
1565 ;;;!! (interactive "@e")
1566 ;;;!! (setq scrolled-lines 0)
1567 ;;;!! (incremental-scroll (- scroll-speed)))
1568 ;;;!!
1569 ;;;!! (defun incremental-scroll (n)
1570 ;;;!! (while (= (x-mouse-events) 0)
1571 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1572 ;;;!! (scroll-down n)
1573 ;;;!! (sit-for 300 t)))
1574 ;;;!!
1575 ;;;!! (defun incr-scroll-stop (event)
1576 ;;;!! (interactive "@e")
1577 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1578 ;;;!! (setq scrolled-lines 0)
1579 ;;;!! (sleep-for 1))
1580 ;;;!!
1581 ;;;!! ;;; (defun x-testing-scroll ()
1582 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1583 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1584 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1585 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1586 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1587 ;;;!!
1588 ;;;!! ;;
1589 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1590 ;;;!! ;;
1591 ;;;!!
1592 ;;;!! (defun mouse-kill-rectangle (event)
1593 ;;;!! "Kill the rectangle between point and the mouse cursor."
1594 ;;;!! (interactive "@e")
1595 ;;;!! (let ((point-save (point)))
1596 ;;;!! (save-excursion
1597 ;;;!! (mouse-set-point event)
1598 ;;;!! (push-mark nil t)
1599 ;;;!! (if (> point-save (point))
1600 ;;;!! (kill-rectangle (point) point-save)
1601 ;;;!! (kill-rectangle point-save (point))))))
1602 ;;;!!
1603 ;;;!! (defun mouse-open-rectangle (event)
1604 ;;;!! "Kill the rectangle between point and the mouse cursor."
1605 ;;;!! (interactive "@e")
1606 ;;;!! (let ((point-save (point)))
1607 ;;;!! (save-excursion
1608 ;;;!! (mouse-set-point event)
1609 ;;;!! (push-mark nil t)
1610 ;;;!! (if (> point-save (point))
1611 ;;;!! (open-rectangle (point) point-save)
1612 ;;;!! (open-rectangle point-save (point))))))
1613 ;;;!!
1614 ;;;!! ;; Must be a better way to do this.
1615 ;;;!!
1616 ;;;!! (defun mouse-multiple-insert (n char)
1617 ;;;!! (while (> n 0)
1618 ;;;!! (insert char)
1619 ;;;!! (setq n (1- n))))
1620 ;;;!!
1621 ;;;!! ;; What this could do is not finalize until button was released.
1622 ;;;!!
1623 ;;;!! (defun mouse-move-text (event)
1624 ;;;!! "Move text from point to cursor position, inserting spaces."
1625 ;;;!! (interactive "@e")
1626 ;;;!! (let* ((relative-coordinate
1627 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
1628 ;;;!! (if (consp relative-coordinate)
1629 ;;;!! (cond ((> (current-column) (car relative-coordinate))
1630 ;;;!! (delete-char
1631 ;;;!! (- (car relative-coordinate) (current-column))))
1632 ;;;!! ((< (current-column) (car relative-coordinate))
1633 ;;;!! (mouse-multiple-insert
1634 ;;;!! (- (car relative-coordinate) (current-column)) " "))
1635 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
1637 ;; Choose a completion with the mouse.
1639 (defun mouse-choose-completion (event)
1640 "Click on an alternative in the `*Completions*' buffer to choose it."
1641 (interactive "e")
1642 ;; Give temporary modes such as isearch a chance to turn off.
1643 (run-hooks 'mouse-leave-buffer-hook)
1644 (let ((buffer (window-buffer))
1645 choice
1646 base-size)
1647 (save-excursion
1648 (set-buffer (window-buffer (posn-window (event-start event))))
1649 (if completion-reference-buffer
1650 (setq buffer completion-reference-buffer))
1651 (setq base-size completion-base-size)
1652 (save-excursion
1653 (goto-char (posn-point (event-start event)))
1654 (let (beg end)
1655 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
1656 (setq end (point) beg (1+ (point))))
1657 (if (null beg)
1658 (error "No completion here"))
1659 (setq beg (previous-single-property-change beg 'mouse-face))
1660 (setq end (or (next-single-property-change end 'mouse-face)
1661 (point-max)))
1662 (setq choice (buffer-substring beg end)))))
1663 (let ((owindow (selected-window)))
1664 (select-window (posn-window (event-start event)))
1665 (if (and (one-window-p t 'selected-frame)
1666 (window-dedicated-p (selected-window)))
1667 ;; This is a special buffer's frame
1668 (iconify-frame (selected-frame))
1669 (or (window-dedicated-p (selected-window))
1670 (bury-buffer)))
1671 (select-window owindow))
1672 (choose-completion-string choice buffer base-size)))
1674 ;; Font selection.
1676 (defun font-menu-add-default ()
1677 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
1678 (font-alist x-fixed-font-alist)
1679 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
1680 (if (assoc "Default" elt)
1681 (delete (assoc "Default" elt) elt))
1682 (setcdr elt
1683 (cons (list "Default"
1684 (cdr (assq 'font (frame-parameters (selected-frame)))))
1685 (cdr elt)))))
1687 (defvar x-fixed-font-alist
1688 '("Font menu"
1689 ("Misc"
1690 ;; For these, we specify the pixel height and width.
1691 ("fixed" "fixed")
1692 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
1693 ("6x12"
1694 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
1695 ("6x13"
1696 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
1697 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
1698 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
1699 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
1700 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
1701 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
1702 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
1703 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
1704 ("")
1705 ("clean 5x8"
1706 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
1707 ("clean 6x8"
1708 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
1709 ("clean 8x8"
1710 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
1711 ("clean 8x10"
1712 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
1713 ("clean 8x14"
1714 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
1715 ("clean 8x16"
1716 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
1717 ("")
1718 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
1719 ;;; We don't seem to have these; who knows what they are.
1720 ;;; ("fg-18" "fg-18")
1721 ;;; ("fg-25" "fg-25")
1722 ;;; ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
1723 ;;; ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
1724 ;;; ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
1725 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
1726 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
1727 ("Courier"
1728 ;; For these, we specify the point height.
1729 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
1730 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
1731 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
1732 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
1733 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
1734 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
1735 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
1736 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
1737 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
1738 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
1739 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
1740 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
1741 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
1742 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
1743 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
1744 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
1745 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
1746 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
1747 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
1748 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
1749 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
1750 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
1751 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
1752 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
1754 "X fonts suitable for use in Emacs.")
1756 (defun mouse-set-font (&rest fonts)
1757 "Select an emacs font from a list of known good fonts"
1758 (interactive
1759 (x-popup-menu last-nonmenu-event x-fixed-font-alist))
1760 (if fonts
1761 (let (font)
1762 (while fonts
1763 (condition-case nil
1764 (progn
1765 (set-default-font (car fonts))
1766 (setq font (car fonts))
1767 (setq fonts nil))
1768 (error
1769 (setq fonts (cdr fonts)))))
1770 (if (null font)
1771 (error "Font not found")))))
1773 ;;; Bindings for mouse commands.
1775 (define-key global-map [down-mouse-1] 'mouse-drag-region)
1776 (global-set-key [mouse-1] 'mouse-set-point)
1777 (global-set-key [drag-mouse-1] 'mouse-set-region)
1779 ;; These are tested for in mouse-drag-region.
1780 (global-set-key [double-mouse-1] 'mouse-set-point)
1781 (global-set-key [triple-mouse-1] 'mouse-set-point)
1783 (global-set-key [mouse-2] 'mouse-yank-at-click)
1784 (global-set-key [mouse-3] 'mouse-save-then-kill)
1786 ;; By binding these to down-going events, we let the user use the up-going
1787 ;; event to make the selection, saving a click.
1788 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
1789 (if (not (eq system-type 'ms-dos))
1790 (global-set-key [S-down-mouse-1] 'mouse-set-font))
1791 ;; C-down-mouse-2 is bound in facemenu.el.
1792 (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
1795 ;; Replaced with dragging mouse-1
1796 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
1798 (global-set-key [mode-line mouse-1] 'mouse-select-window)
1799 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
1800 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
1801 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
1802 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
1803 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
1804 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
1805 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
1806 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
1807 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
1809 (provide 'mouse)
1811 ;;; mouse.el ends here