]> code.delx.au - gnu-emacs/blob - lisp/mouse.el
*** empty log message ***
[gnu-emacs] / lisp / mouse.el
1 ;;; mouse.el --- window system-independent mouse support
2
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: hardware, mouse
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This package provides various useful commands (including help
29 ;; system access) through the mouse. All this code assumes that mouse
30 ;; interpretation has been abstracted into Emacs input events.
31 ;;
32 ;; The code is rather X-dependent.
33
34 ;;; Code:
35
36 ;;; Utility functions.
37
38 ;;; Indent track-mouse like progn.
39 (put 'track-mouse 'lisp-indent-function 0)
40
41 (defcustom mouse-yank-at-point nil
42 "*If non-nil, mouse yank commands yank at point instead of at click."
43 :type 'boolean
44 :group 'mouse)
45 \f
46 ;; Provide a mode-specific menu on a mouse button.
47
48 (defun popup-menu (menu &optional position prefix)
49 "Popup the given menu and call the selected option.
50 MENU can be a keymap, an easymenu-style menu or a list of keymaps as for
51 `x-popup-menu'.
52 POSITION can be a click event or ((XOFFSET YOFFSET) WINDOW) and defaults to
53 the current mouse position.
54 PREFIX is the prefix argument (if any) to pass to the command."
55 (let* ((map (cond
56 ((keymapp menu) menu)
57 ((and (listp menu) (keymapp (car menu))) menu)
58 (t (let* ((map (easy-menu-create-menu (car menu) (cdr menu)))
59 (filter (when (symbolp map)
60 (plist-get (get map 'menu-prop) :filter))))
61 (if filter (funcall filter (symbol-function map)) map)))))
62 event cmd)
63 (unless position
64 (let ((mp (mouse-pixel-position)))
65 (setq position (list (list (cadr mp) (cddr mp)) (car mp)))))
66 ;; The looping behavior was taken from lmenu's popup-menu-popup
67 (while (and map (setq event
68 ;; map could be a prefix key, in which case
69 ;; we need to get its function cell
70 ;; definition.
71 (x-popup-menu position (indirect-function map))))
72 ;; Strangely x-popup-menu returns a list.
73 ;; mouse-major-mode-menu was using a weird:
74 ;; (key-binding (apply 'vector (append '(menu-bar) menu-prefix events)))
75 (setq cmd
76 (if (and (not (keymapp map)) (listp map))
77 ;; We were given a list of keymaps. Search them all
78 ;; in sequence until a first binding is found.
79 (let ((mouse-click (apply 'vector event))
80 binding)
81 (while (and map (null binding))
82 (setq binding (lookup-key (car map) mouse-click))
83 (if (numberp binding) ; `too long'
84 (setq binding nil))
85 (setq map (cdr map)))
86 binding)
87 ;; We were given a single keymap.
88 (lookup-key map (apply 'vector event))))
89 ;; Clear out echoing, which perhaps shows a prefix arg.
90 (message "")
91 ;; Maybe try again but with the submap.
92 (setq map (if (keymapp cmd) cmd)))
93 ;; If the user did not cancel by refusing to select,
94 ;; and if the result is a command, run it.
95 (when (and (null map) (commandp cmd))
96 (setq prefix-arg prefix)
97 ;; `setup-specified-language-environment', for instance,
98 ;; expects this to be set from a menu keymap.
99 (setq last-command-event (car (last event)))
100 ;; mouse-major-mode-menu was using `command-execute' instead.
101 (call-interactively cmd))))
102
103 (defvar mouse-major-mode-menu-prefix) ; dynamically bound
104
105 (defun mouse-major-mode-menu (event prefix)
106 "Pop up a mode-specific menu of mouse commands.
107 Default to the Edit menu if the major mode doesn't define a menu."
108 ;; Switch to the window clicked on, because otherwise
109 ;; the mode's commands may not make sense.
110 (interactive "@e\nP")
111 ;; Let the mode update its menus first.
112 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
113 (let* (;; This is where mouse-major-mode-menu-prefix
114 ;; returns the prefix we should use (after menu-bar).
115 ;; It is either nil or (SOME-SYMBOL).
116 (mouse-major-mode-menu-prefix nil)
117 ;; Keymap from which to inherit; may be null.
118 (ancestor (mouse-major-mode-menu-1
119 (and (current-local-map)
120 (local-key-binding [menu-bar]))))
121 ;; Make a keymap in which our last command leads to a menu or
122 ;; default to the edit menu.
123 (newmap (if ancestor
124 (make-sparse-keymap (concat mode-name " Mode"))
125 menu-bar-edit-menu))
126 result)
127 (if ancestor
128 ;; Make our menu inherit from the desired keymap which we want
129 ;; to display as the menu now.
130 (set-keymap-parent newmap ancestor))
131 (popup-menu newmap event prefix)))
132
133
134 ;; Compute and cache the equivalent keys in MENU and all its submenus.
135 ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
136 ;;; (and (eq (car menu) 'keymap)
137 ;;; (x-popup-menu nil menu))
138 ;;; (while menu
139 ;;; (and (consp (car menu))
140 ;;; (consp (cdr (car menu)))
141 ;;; (let ((tail (cdr (car menu))))
142 ;;; (while (and (consp tail)
143 ;;; (not (eq (car tail) 'keymap)))
144 ;;; (setq tail (cdr tail)))
145 ;;; (if (consp tail)
146 ;;; (mouse-major-mode-menu-compute-equiv-keys tail))))
147 ;;; (setq menu (cdr menu))))
148
149 ;; Given a mode's menu bar keymap,
150 ;; if it defines exactly one menu bar menu,
151 ;; return just that menu.
152 ;; Otherwise return a menu for all of them.
153 (defun mouse-major-mode-menu-1 (menubar)
154 (if menubar
155 (let ((tail menubar)
156 submap)
157 (while tail
158 (if (consp (car tail))
159 (if submap
160 (setq submap t)
161 (setq submap (car tail))))
162 (setq tail (cdr tail)))
163 (if (eq submap t)
164 menubar
165 (setq mouse-major-mode-menu-prefix (list (car submap)))
166 (lookup-key menubar (vector (car submap)))))))
167
168 (defun mouse-popup-menubar (event prefix)
169 "Pops up a menu equivalent to the menu bar a keyboard EVENT with PREFIX.
170 The contents are the items that would be in the menu bar whether or
171 not it is actually displayed."
172 (interactive "@e \nP")
173 (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
174 (let* ((local-menu (and (current-local-map)
175 (lookup-key (current-local-map) [menu-bar])))
176 (global-menu (lookup-key global-map [menu-bar]))
177 ;; If a keymap doesn't have a prompt string (a lazy
178 ;; programmer didn't bother to provide one), create it and
179 ;; insert it into the keymap; each keymap gets its own
180 ;; prompt. This is required for non-toolkit versions to
181 ;; display non-empty menu pane names.
182 (minor-mode-menus
183 (mapcar
184 (function
185 (lambda (menu)
186 (let* ((minor-mode (car menu))
187 (menu (cdr menu))
188 (title-or-map (cadr menu)))
189 (or (stringp title-or-map)
190 (setq menu
191 (cons 'keymap
192 (cons (concat
193 (capitalize (subst-char-in-string
194 ?- ?\ (symbol-name
195 minor-mode)))
196 " Menu")
197 (cdr menu)))))
198 menu)))
199 (minor-mode-key-binding [menu-bar])))
200 (local-title-or-map (and local-menu (cadr local-menu)))
201 (global-title-or-map (cadr global-menu)))
202 (or (null local-menu)
203 (stringp local-title-or-map)
204 (setq local-menu (cons 'keymap
205 (cons (concat mode-name " Mode Menu")
206 (cdr local-menu)))))
207 (or (stringp global-title-or-map)
208 (setq global-menu (cons 'keymap
209 (cons "Global Menu"
210 (cdr global-menu)))))
211 ;; Supplying the list is faster than making a new map.
212 (popup-menu (append (list global-menu)
213 (if local-menu
214 (list local-menu))
215 minor-mode-menus)
216 event prefix)))
217
218 (defun mouse-popup-menubar-stuff (event prefix)
219 "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
220 Use the former if the menu bar is showing, otherwise the latter."
221 (interactive "@e \nP")
222 (if (zerop (assoc-default 'menu-bar-lines (frame-parameters) 'eq 0))
223 (mouse-popup-menubar event prefix)
224 (mouse-major-mode-menu event prefix)))
225 \f
226 ;; Commands that operate on windows.
227
228 (defun mouse-minibuffer-check (event)
229 (let ((w (posn-window (event-start event))))
230 (and (window-minibuffer-p w)
231 (not (minibuffer-window-active-p w))
232 (error "Minibuffer window is not active")))
233 ;; Give temporary modes such as isearch a chance to turn off.
234 (run-hooks 'mouse-leave-buffer-hook))
235
236 (defun mouse-delete-window (click)
237 "Delete the window you click on.
238 Do nothing if the frame has just one window.
239 This command must be bound to a mouse click."
240 (interactive "e")
241 (unless (one-window-p t)
242 (mouse-minibuffer-check click)
243 (delete-window (posn-window (event-start click)))))
244
245 (defun mouse-select-window (click)
246 "Select the window clicked on; don't move point."
247 (interactive "e")
248 (mouse-minibuffer-check click)
249 (let ((oframe (selected-frame))
250 (frame (window-frame (posn-window (event-start click)))))
251 (select-window (posn-window (event-start click)))
252 (raise-frame frame)
253 (select-frame frame)
254 (or (eq frame oframe)
255 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
256
257 (defun mouse-tear-off-window (click)
258 "Delete the window clicked on, and create a new frame displaying its buffer."
259 (interactive "e")
260 (mouse-minibuffer-check click)
261 (let* ((window (posn-window (event-start click)))
262 (buf (window-buffer window))
263 (frame (make-frame)))
264 (select-frame frame)
265 (switch-to-buffer buf)
266 (delete-window window)))
267
268 (defun mouse-delete-other-windows ()
269 "Delete all windows except the one you click on."
270 (interactive "@")
271 (delete-other-windows))
272
273 (defun mouse-split-window-vertically (click)
274 "Select Emacs window mouse is on, then split it vertically in half.
275 The window is split at the line clicked on.
276 This command must be bound to a mouse click."
277 (interactive "@e")
278 (mouse-minibuffer-check click)
279 (let ((start (event-start click)))
280 (select-window (posn-window start))
281 (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
282 (first-line window-min-height)
283 (last-line (- (window-height) window-min-height)))
284 (if (< last-line first-line)
285 (error "Window too short to split")
286 (split-window-vertically
287 (min (max new-height first-line) last-line))))))
288
289 (defun mouse-split-window-horizontally (click)
290 "Select Emacs window mouse is on, then split it horizontally in half.
291 The window is split at the column clicked on.
292 This command must be bound to a mouse click."
293 (interactive "@e")
294 (mouse-minibuffer-check click)
295 (let ((start (event-start click)))
296 (select-window (posn-window start))
297 (let ((new-width (1+ (car (posn-col-row (event-end click)))))
298 (first-col window-min-width)
299 (last-col (- (window-width) window-min-width)))
300 (if (< last-col first-col)
301 (error "Window too narrow to split")
302 (split-window-horizontally
303 (min (max new-width first-col) last-col))))))
304
305 (defun mouse-drag-window-above (window)
306 "Return the (or a) window directly above WINDOW.
307 That means one whose bottom edge is at the same height as WINDOW's top edge."
308 (let ((top (nth 1 (window-edges window)))
309 (start-window window)
310 above-window)
311 (setq window (previous-window window 0))
312 (while (and (not above-window) (not (eq window start-window)))
313 (if (= (+ (window-height window) (nth 1 (window-edges window)))
314 top)
315 (setq above-window window))
316 (setq window (previous-window window)))
317 above-window))
318
319 (defun mouse-drag-move-window-bottom (window growth)
320 "Move the bottom of WINDOW up or down by GROWTH lines.
321 Move it down if GROWTH is positive, or up if GROWTH is negative.
322 If this would make WINDOW too short,
323 shrink the window or windows above it to make room."
324 (let ((excess (- window-min-height (+ (window-height window) growth))))
325 ;; EXCESS is the number of lines we need to take from windows above.
326 (if (> excess 0)
327 ;; This can recursively shrink windows all the way up.
328 (let ((window-above (mouse-drag-window-above window)))
329 (if window-above
330 (mouse-drag-move-window-bottom window-above (- excess))))))
331 (save-selected-window
332 (select-window window)
333 (enlarge-window growth nil (> growth 0))))
334
335 (defun mouse-drag-mode-line-1 (start-event mode-line-p)
336 "Change the height of a window by dragging on the mode or header line.
337 START-EVENT is the starting mouse-event of the drag action.
338 MODE-LINE-P non-nil means dragging a mode line; nil means a header line."
339 ;; Give temporary modes such as isearch a chance to turn off.
340 (run-hooks 'mouse-leave-buffer-hook)
341 (let* ((done nil)
342 (echo-keystrokes 0)
343 (start (event-start start-event))
344 (start-event-window (posn-window start))
345 (start-event-frame (window-frame start-event-window))
346 (start-nwindows (count-windows t))
347 (old-selected-window (selected-window))
348 (minibuffer (frame-parameter nil 'minibuffer))
349 should-enlarge-minibuffer event mouse y top bot edges wconfig growth)
350 (track-mouse
351 (progn
352 ;; enlarge-window only works on the selected window, so
353 ;; we must select the window where the start event originated.
354 ;; unwind-protect will restore the old selected window later.
355 (select-window start-event-window)
356
357 ;; if this is the bottommost ordinary window, then to
358 ;; move its modeline the minibuffer must be enlarged.
359 (setq should-enlarge-minibuffer
360 (and minibuffer
361 mode-line-p
362 (not (one-window-p t))
363 (= (nth 1 (window-edges minibuffer))
364 (nth 3 (window-edges)))))
365
366 ;; loop reading events and sampling the position of
367 ;; the mouse.
368 (while (not done)
369 (setq event (read-event)
370 mouse (mouse-position))
371
372 ;; do nothing if
373 ;; - there is a switch-frame event.
374 ;; - the mouse isn't in the frame that we started in
375 ;; - the mouse isn't in any Emacs frame
376 ;; drag if
377 ;; - there is a mouse-movement event
378 ;; - there is a scroll-bar-movement event
379 ;; (same as mouse movement for our purposes)
380 ;; quit if
381 ;; - there is a keyboard event or some other unknown event
382 ;; unknown event.
383 (cond ((integerp event)
384 (setq done t))
385
386 ((eq (car event) 'switch-frame)
387 nil)
388
389 ((not (memq (car event) '(mouse-movement scroll-bar-movement)))
390 (when (consp event)
391 (push event unread-command-events))
392 (setq done t))
393
394 ((not (eq (car mouse) start-event-frame))
395 nil)
396
397 ((null (car (cdr mouse)))
398 nil)
399
400 (t
401 (setq y (cdr (cdr mouse))
402 edges (window-edges)
403 top (nth 1 edges)
404 bot (nth 3 edges))
405
406 ;; compute size change needed
407 (cond (mode-line-p
408 (setq growth (- y bot -1)))
409 (t ; header line
410 (when (< (- bot y) window-min-height)
411 (setq y (- bot window-min-height)))
412 ;; The window's top includes the header line!
413 (setq growth (- top y))))
414 (setq wconfig (current-window-configuration))
415
416 ;; Check for an error case.
417 (when (and (/= growth 0)
418 (not minibuffer)
419 (one-window-p t))
420 (error "Attempt to resize sole window"))
421
422 ;; grow/shrink minibuffer?
423 (if should-enlarge-minibuffer
424 (progn
425 ;; yes. briefly select minibuffer so
426 ;; enlarge-window will affect the
427 ;; correct window.
428 (select-window minibuffer)
429 ;; scale back shrinkage if it would
430 ;; make the minibuffer less than 1
431 ;; line tall.
432 (if (and (> growth 0)
433 (< (- (window-height minibuffer)
434 growth)
435 1))
436 (setq growth (1- (window-height minibuffer))))
437 (enlarge-window (- growth))
438 (select-window start-event-window))
439 ;; no. grow/shrink the selected window
440 ;(message "growth = %d" growth)
441 (mouse-drag-move-window-bottom start-event-window growth))
442
443 ;; if this window's growth caused another
444 ;; window to be deleted because it was too
445 ;; short, rescind the change.
446 ;;
447 ;; if size change caused space to be stolen
448 ;; from a window above this one, rescind the
449 ;; change, but only if we didn't grow/shrink
450 ;; the minibuffer. minibuffer size changes
451 ;; can cause all windows to shrink... no way
452 ;; around it.
453 (when (or (/= start-nwindows (count-windows t))
454 (and (not should-enlarge-minibuffer)
455 (> growth 0)
456 mode-line-p
457 (/= top (nth 1 (window-edges)))))
458 (set-window-configuration wconfig)))))))))
459
460 (defun mouse-drag-mode-line (start-event)
461 "Change the height of a window by dragging on the mode line."
462 (interactive "e")
463 (mouse-drag-mode-line-1 start-event t))
464
465 (defun mouse-drag-header-line (start-event)
466 "Change the height of a window by dragging on the header line.
467 Windows whose header-lines are at the top of the frame cannot be
468 resized by dragging their header-line."
469 (interactive "e")
470 ;; Changing the window's size by dragging its header-line when the
471 ;; header-line is at the top of the frame is somewhat strange,
472 ;; because the header-line doesn't move, so don't do it.
473 (let* ((start (event-start start-event))
474 (window (posn-window start))
475 (frame (window-frame window))
476 (first-window (frame-first-window frame)))
477 (when (or (eq window first-window)
478 (= (nth 1 (window-edges window))
479 (nth 1 (window-edges first-window))))
480 (error "Cannot move header-line at the top of the frame"))
481 (mouse-drag-mode-line-1 start-event nil)))
482
483 \f
484 (defun mouse-drag-vertical-line (start-event)
485 "Change the width of a window by dragging on the vertical line."
486 (interactive "e")
487 ;; Give temporary modes such as isearch a chance to turn off.
488 (run-hooks 'mouse-leave-buffer-hook)
489 (let* ((done nil)
490 (echo-keystrokes 0)
491 (start-event-frame (window-frame (car (car (cdr start-event)))))
492 (start-event-window (car (car (cdr start-event))))
493 (start-nwindows (count-windows t))
494 (old-selected-window (selected-window))
495 event mouse x left right edges wconfig growth
496 (which-side
497 (or (cdr (assq 'vertical-scroll-bars (frame-parameters start-event-frame)))
498 'right)))
499 (if (one-window-p t)
500 (error "Attempt to resize sole ordinary window"))
501 (if (eq which-side 'right)
502 (if (= (nth 2 (window-edges start-event-window))
503 (frame-width start-event-frame))
504 (error "Attempt to drag rightmost scrollbar"))
505 (if (= (nth 0 (window-edges start-event-window)) 0)
506 (error "Attempt to drag leftmost scrollbar")))
507 (track-mouse
508 (progn
509 ;; enlarge-window only works on the selected window, so
510 ;; we must select the window where the start event originated.
511 ;; unwind-protect will restore the old selected window later.
512 (select-window start-event-window)
513 ;; loop reading events and sampling the position of
514 ;; the mouse.
515 (while (not done)
516 (setq event (read-event)
517 mouse (mouse-position))
518 ;; do nothing if
519 ;; - there is a switch-frame event.
520 ;; - the mouse isn't in the frame that we started in
521 ;; - the mouse isn't in any Emacs frame
522 ;; drag if
523 ;; - there is a mouse-movement event
524 ;; - there is a scroll-bar-movement event
525 ;; (same as mouse movement for our purposes)
526 ;; quit if
527 ;; - there is a keyboard event or some other unknown event
528 ;; unknown event.
529 (cond ((integerp event)
530 (setq done t))
531 ((eq (car event) 'switch-frame)
532 nil)
533 ((not (memq (car event)
534 '(mouse-movement scroll-bar-movement)))
535 (if (consp event)
536 (setq unread-command-events
537 (cons event unread-command-events)))
538 (setq done t))
539 ((not (eq (car mouse) start-event-frame))
540 nil)
541 ((null (car (cdr mouse)))
542 nil)
543 (t
544 (save-selected-window
545 ;; If the scroll bar is on the window's left,
546 ;; adjust the window on the left.
547 (unless (eq which-side 'right)
548 (select-window (previous-window)))
549 (setq x (- (car (cdr mouse))
550 (if (eq which-side 'right) 0 2))
551 edges (window-edges)
552 left (nth 0 edges)
553 right (nth 2 edges))
554 ;; scale back a move that would make the
555 ;; window too thin.
556 (if (< (- x left -1) window-min-width)
557 (setq x (+ left window-min-width -1)))
558 ;; compute size change needed
559 (setq growth (- x right -1)
560 wconfig (current-window-configuration))
561 (enlarge-window growth t)
562 ;; if this window's growth caused another
563 ;; window to be deleted because it was too
564 ;; thin, rescind the change.
565 ;;
566 ;; if size change caused space to be stolen
567 ;; from a window to the left of this one,
568 ;; rescind the change.
569 (if (or (/= start-nwindows (count-windows t))
570 (/= left (nth 0 (window-edges))))
571 (set-window-configuration wconfig))))))))))
572 \f
573 (defun mouse-set-point (event)
574 "Move point to the position clicked on with the mouse.
575 This should be bound to a mouse click event type."
576 (interactive "e")
577 (mouse-minibuffer-check event)
578 ;; Use event-end in case called from mouse-drag-region.
579 ;; If EVENT is a click, event-end and event-start give same value.
580 (let ((posn (event-end event)))
581 (if (not (windowp (posn-window posn)))
582 (error "Cursor not in text area of window"))
583 (select-window (posn-window posn))
584 (if (numberp (posn-point posn))
585 (goto-char (posn-point posn)))))
586
587 (defvar mouse-last-region-beg nil)
588 (defvar mouse-last-region-end nil)
589 (defvar mouse-last-region-tick nil)
590
591 (defun mouse-region-match ()
592 "Return non-nil if there's an active region that was set with the mouse."
593 (and (mark t) mark-active
594 (eq mouse-last-region-beg (region-beginning))
595 (eq mouse-last-region-end (region-end))
596 (eq mouse-last-region-tick (buffer-modified-tick))))
597
598 (defun mouse-set-region (click)
599 "Set the region to the text dragged over, and copy to kill ring.
600 This should be bound to a mouse drag event."
601 (interactive "e")
602 (mouse-minibuffer-check click)
603 (let ((posn (event-start click))
604 (end (event-end click)))
605 (select-window (posn-window posn))
606 (if (numberp (posn-point posn))
607 (goto-char (posn-point posn)))
608 ;; If mark is highlighted, no need to bounce the cursor.
609 ;; On X, we highlight while dragging, thus once again no need to bounce.
610 (or transient-mark-mode
611 (memq (framep (selected-frame)) '(x pc w32))
612 (sit-for 1))
613 (push-mark)
614 (set-mark (point))
615 (if (numberp (posn-point end))
616 (goto-char (posn-point end)))
617 ;; Don't set this-command to kill-region, so that a following
618 ;; C-w will not double the text in the kill ring.
619 ;; Ignore last-command so we don't append to a preceding kill.
620 (let (this-command last-command deactivate-mark)
621 (copy-region-as-kill (mark) (point)))
622 (mouse-set-region-1)))
623
624 (defun mouse-set-region-1 ()
625 (setq mouse-last-region-beg (region-beginning))
626 (setq mouse-last-region-end (region-end))
627 (setq mouse-last-region-tick (buffer-modified-tick)))
628
629 (defcustom mouse-scroll-delay 0.25
630 "*The pause between scroll steps caused by mouse drags, in seconds.
631 If you drag the mouse beyond the edge of a window, Emacs scrolls the
632 window to bring the text beyond that edge into view, with a delay of
633 this many seconds between scroll steps. Scrolling stops when you move
634 the mouse back into the window, or release the button.
635 This variable's value may be non-integral.
636 Setting this to zero causes Emacs to scroll as fast as it can."
637 :type 'number
638 :group 'mouse)
639
640 (defcustom mouse-scroll-min-lines 1
641 "*The minimum number of lines scrolled by dragging mouse out of window.
642 Moving the mouse out the top or bottom edge of the window begins
643 scrolling repeatedly. The number of lines scrolled per repetition
644 is normally equal to the number of lines beyond the window edge that
645 the mouse has moved. However, it always scrolls at least the number
646 of lines specified by this variable."
647 :type 'integer
648 :group 'mouse)
649
650 (defun mouse-scroll-subr (window jump &optional overlay start)
651 "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
652 If OVERLAY is an overlay, let it stretch from START to the far edge of
653 the newly visible text.
654 Upon exit, point is at the far edge of the newly visible text."
655 (cond
656 ((and (> jump 0) (< jump mouse-scroll-min-lines))
657 (setq jump mouse-scroll-min-lines))
658 ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
659 (setq jump (- mouse-scroll-min-lines))))
660 (let ((opoint (point)))
661 (while (progn
662 (goto-char (window-start window))
663 (if (not (zerop (vertical-motion jump window)))
664 (progn
665 (set-window-start window (point))
666 (if (natnump jump)
667 (if (window-end window)
668 (progn
669 (goto-char (window-end window))
670 ;; window-end doesn't reflect the window's new
671 ;; start position until the next redisplay.
672 (vertical-motion (1- jump) window))
673 (vertical-motion (- (window-height window) 2)))
674 (goto-char (window-start window)))
675 (if overlay
676 (move-overlay overlay start (point)))
677 ;; Now that we have scrolled WINDOW properly,
678 ;; put point back where it was for the redisplay
679 ;; so that we don't mess up the selected window.
680 (or (eq window (selected-window))
681 (goto-char opoint))
682 (sit-for mouse-scroll-delay)))))
683 (or (eq window (selected-window))
684 (goto-char opoint))))
685
686 ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
687 (defvar mouse-drag-overlay (make-overlay 1 1))
688 (delete-overlay mouse-drag-overlay)
689 (overlay-put mouse-drag-overlay 'face 'region)
690
691 (defvar mouse-selection-click-count 0)
692
693 (defvar mouse-selection-click-count-buffer nil)
694
695 (defun mouse-drag-region (start-event)
696 "Set the region to the text that the mouse is dragged over.
697 Highlight the drag area as you move the mouse.
698 This must be bound to a button-down mouse event.
699 In Transient Mark mode, the highlighting remains as long as the mark
700 remains active. Otherwise, it remains until the next input event.
701
702 If the click is in the echo area, display the `*Messages*' buffer."
703 (interactive "e")
704 (let ((w (posn-window (event-start start-event))))
705 (if (not (or (not (window-minibuffer-p w))
706 (minibuffer-window-active-p w)))
707 (save-excursion
708 (read-event)
709 (set-buffer "*Messages*")
710 (goto-char (point-max))
711 (display-buffer (current-buffer)))
712 ;; Give temporary modes such as isearch a chance to turn off.
713 (run-hooks 'mouse-leave-buffer-hook)
714 (mouse-drag-region-1 start-event))))
715
716 (defun mouse-drag-region-1 (start-event)
717 (mouse-minibuffer-check start-event)
718 (let* ((echo-keystrokes 0)
719 (start-posn (event-start start-event))
720 (start-point (posn-point start-posn))
721 (start-window (posn-window start-posn))
722 (start-window-start (window-start start-window))
723 (start-frame (window-frame start-window))
724 (start-hscroll (window-hscroll start-window))
725 (bounds (window-edges start-window))
726 (top (nth 1 bounds))
727 (bottom (if (window-minibuffer-p start-window)
728 (nth 3 bounds)
729 ;; Don't count the mode line.
730 (1- (nth 3 bounds))))
731 (click-count (1- (event-click-count start-event))))
732 (setq mouse-selection-click-count click-count)
733 (setq mouse-selection-click-count-buffer (current-buffer))
734 (mouse-set-point start-event)
735 ;; In case the down click is in the middle of some intangible text,
736 ;; use the end of that text, and put it in START-POINT.
737 (if (< (point) start-point)
738 (goto-char start-point))
739 (setq start-point (point))
740 (let ((range (mouse-start-end start-point start-point click-count)))
741 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
742 (window-buffer start-window)))
743 (deactivate-mark)
744 ;; end-of-range is used only in the single-click case.
745 ;; It is the place where the drag has reached so far
746 ;; (but not outside the window where the drag started).
747 (let (event end end-point last-end-point (end-of-range (point)))
748 (track-mouse
749 (while (progn
750 (setq event (read-event))
751 (or (mouse-movement-p event)
752 (eq (car-safe event) 'switch-frame)))
753 (if (eq (car-safe event) 'switch-frame)
754 nil
755 (setq end (event-end event)
756 end-point (posn-point end))
757 (if (numberp end-point)
758 (setq last-end-point end-point))
759
760 (cond
761 ;; Are we moving within the original window?
762 ((and (eq (posn-window end) start-window)
763 (integer-or-marker-p end-point))
764 ;; Go to START-POINT first, so that when we move to END-POINT,
765 ;; if it's in the middle of intangible text,
766 ;; point jumps in the direction away from START-POINT.
767 (goto-char start-point)
768 (goto-char end-point)
769 (if (zerop (% click-count 3))
770 (setq end-of-range (point)))
771 (let ((range (mouse-start-end start-point (point) click-count)))
772 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
773
774 (t
775 (let ((mouse-row (cdr (cdr (mouse-position)))))
776 (cond
777 ((null mouse-row))
778 ((< mouse-row top)
779 (mouse-scroll-subr start-window (- mouse-row top)
780 mouse-drag-overlay start-point)
781 ;; Without this, point tends to jump back to the starting
782 ;; position where the mouse button was pressed down.
783 (setq end-of-range (overlay-start mouse-drag-overlay)))
784 ((>= mouse-row bottom)
785 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
786 mouse-drag-overlay start-point)
787 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
788
789 ;; In case we did not get a mouse-motion event
790 ;; for the final move of the mouse before a drag event
791 ;; pretend that we did get one.
792 (when (and (memq 'drag (event-modifiers (car-safe event)))
793 (setq end (event-end event)
794 end-point (posn-point end))
795 (eq (posn-window end) start-window)
796 (integer-or-marker-p end-point))
797 ;; Go to START-POINT first, so that when we move to END-POINT,
798 ;; if it's in the middle of intangible text,
799 ;; point jumps in the direction away from START-POINT.
800 (goto-char start-point)
801 (goto-char end-point)
802 (if (zerop (% click-count 3))
803 (setq end-of-range (point)))
804 (let ((range (mouse-start-end start-point (point) click-count)))
805 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
806
807 (if (consp event)
808 (let ((fun (key-binding (vector (car event)))))
809 ;; Run the binding of the terminating up-event, if possible.
810 ;; In the case of a multiple click, it gives the wrong results,
811 ;; because it would fail to set up a region.
812 (if (not (= (overlay-start mouse-drag-overlay)
813 (overlay-end mouse-drag-overlay)))
814 (let* ((stop-point
815 (if (numberp (posn-point (event-end event)))
816 (posn-point (event-end event))
817 last-end-point))
818 ;; The end that comes from where we ended the drag.
819 ;; Point goes here.
820 (region-termination
821 (if (and stop-point (< stop-point start-point))
822 (overlay-start mouse-drag-overlay)
823 (overlay-end mouse-drag-overlay)))
824 ;; The end that comes from where we started the drag.
825 ;; Mark goes there.
826 (region-commencement
827 (- (+ (overlay-end mouse-drag-overlay)
828 (overlay-start mouse-drag-overlay))
829 region-termination))
830 last-command this-command)
831 (push-mark region-commencement t t)
832 (goto-char region-termination)
833 ;; Don't let copy-region-as-kill set deactivate-mark.
834 (let (deactivate-mark)
835 (copy-region-as-kill (point) (mark t)))
836 (let ((buffer (current-buffer)))
837 (mouse-show-mark)
838 ;; mouse-show-mark can call read-event,
839 ;; and that means the Emacs server could switch buffers
840 ;; under us. If that happened,
841 ;; avoid trying to use the region.
842 (and (mark t) mark-active
843 (eq buffer (current-buffer))
844 (mouse-set-region-1))))
845 (delete-overlay mouse-drag-overlay)
846 ;; Run the binding of the terminating up-event.
847 (when (and (functionp fun)
848 (= start-hscroll (window-hscroll start-window))
849 ;; Don't run the up-event handler if the
850 ;; window start changed in a redisplay after
851 ;; the mouse-set-point for the down-mouse
852 ;; event at the beginning of this function.
853 ;; When the window start has changed, the
854 ;; up-mouse event will contain a different
855 ;; position due to the new window contents,
856 ;; and point is set again.
857 (or end-point
858 (= (window-start start-window)
859 start-window-start)))
860 (setq unread-command-events
861 (cons event unread-command-events)))))
862 (delete-overlay mouse-drag-overlay)))))
863 \f
864 ;; Commands to handle xterm-style multiple clicks.
865
866 (defun mouse-skip-word (dir)
867 "Skip over word, over whitespace, or over identical punctuation.
868 If DIR is positive skip forward; if negative, skip backward."
869 (let* ((char (following-char))
870 (syntax (char-to-string (char-syntax char))))
871 (cond ((string= syntax "w")
872 ;; Here, we can't use skip-syntax-forward/backward because
873 ;; they don't pay attention to word-separating-categories,
874 ;; and thus they will skip over a true word boundary. So,
875 ;; we simularte the original behaviour by using
876 ;; forward-word.
877 (if (< dir 0)
878 (if (not (looking-at "\\<"))
879 (forward-word -1))
880 (if (or (looking-at "\\<") (not (looking-at "\\>")))
881 (forward-word 1))))
882 ((string= syntax " ")
883 (if (< dir 0)
884 (skip-syntax-backward syntax)
885 (skip-syntax-forward syntax)))
886 ((string= syntax "_")
887 (if (< dir 0)
888 (skip-syntax-backward "w_")
889 (skip-syntax-forward "w_")))
890 ((< dir 0)
891 (while (and (not (bobp)) (= (preceding-char) char))
892 (forward-char -1)))
893 (t
894 (while (and (not (eobp)) (= (following-char) char))
895 (forward-char 1))))))
896
897 ;; Return a list of region bounds based on START and END according to MODE.
898 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
899 ;; If MODE is 1 then set point to start of word at (min START END),
900 ;; mark to end of word at (max START END).
901 ;; If MODE is 2 then do the same for lines.
902 (defun mouse-start-end (start end mode)
903 (if (> start end)
904 (let ((temp start))
905 (setq start end
906 end temp)))
907 (setq mode (mod mode 3))
908 (cond ((= mode 0)
909 (list start end))
910 ((and (= mode 1)
911 (= start end)
912 (char-after start)
913 (= (char-syntax (char-after start)) ?\())
914 (list start
915 (save-excursion
916 (goto-char start)
917 (forward-sexp 1)
918 (point))))
919 ((and (= mode 1)
920 (= start end)
921 (char-after start)
922 (= (char-syntax (char-after start)) ?\)))
923 (list (save-excursion
924 (goto-char (1+ start))
925 (backward-sexp 1)
926 (point))
927 (1+ start)))
928 ((and (= mode 1)
929 (= start end)
930 (char-after start)
931 (= (char-syntax (char-after start)) ?\"))
932 (let ((open (or (eq start (point-min))
933 (save-excursion
934 (goto-char (- start 1))
935 (looking-at "\\s(\\|\\s \\|\\s>")))))
936 (if open
937 (list start
938 (save-excursion
939 (condition-case nil
940 (progn
941 (goto-char start)
942 (forward-sexp 1)
943 (point))
944 (error end))))
945 (list (save-excursion
946 (condition-case nil
947 (progn
948 (goto-char (1+ start))
949 (backward-sexp 1)
950 (point))
951 (error end)))
952 (1+ start)))))
953 ((= mode 1)
954 (list (save-excursion
955 (goto-char start)
956 (mouse-skip-word -1)
957 (point))
958 (save-excursion
959 (goto-char end)
960 (mouse-skip-word 1)
961 (point))))
962 ((= mode 2)
963 (list (save-excursion
964 (goto-char start)
965 (beginning-of-line 1)
966 (point))
967 (save-excursion
968 (goto-char end)
969 (forward-line 1)
970 (point))))))
971 \f
972 ;; Subroutine: set the mark where CLICK happened,
973 ;; but don't do anything else.
974 (defun mouse-set-mark-fast (click)
975 (mouse-minibuffer-check click)
976 (let ((posn (event-start click)))
977 (select-window (posn-window posn))
978 (if (numberp (posn-point posn))
979 (push-mark (posn-point posn) t t))))
980
981 (defun mouse-undouble-last-event (events)
982 (let* ((index (1- (length events)))
983 (last (nthcdr index events))
984 (event (car last))
985 (basic (event-basic-type event))
986 (old-modifiers (event-modifiers event))
987 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
988 (new
989 (if (consp event)
990 ;; Use reverse, not nreverse, since event-modifiers
991 ;; does not copy the list it returns.
992 (cons (event-convert-list (reverse (cons basic modifiers)))
993 (cdr event))
994 event)))
995 (setcar last new)
996 (if (and (not (equal modifiers old-modifiers))
997 (key-binding (apply 'vector events)))
998 t
999 (setcar last event)
1000 nil)))
1001
1002 ;; Momentarily show where the mark is, if highlighting doesn't show it.
1003
1004 (defvar mouse-region-delete-keys '([delete] [deletechar])
1005 "List of keys which shall cause the mouse region to be deleted.")
1006
1007 (defun mouse-show-mark ()
1008 (if transient-mark-mode
1009 (delete-overlay mouse-drag-overlay)
1010 (let ((inhibit-quit t)
1011 (echo-keystrokes 0)
1012 event events key ignore
1013 x-lost-selection-hooks)
1014 (add-hook 'x-lost-selection-hooks
1015 (lambda (seltype)
1016 (if (eq seltype 'PRIMARY)
1017 (progn (setq ignore t)
1018 (throw 'mouse-show-mark t)))))
1019 (move-overlay mouse-drag-overlay (point) (mark t))
1020 (catch 'mouse-show-mark
1021 ;; In this loop, execute scroll bar and switch-frame events.
1022 ;; Also ignore down-events that are undefined.
1023 (while (progn (setq event (read-event))
1024 (setq events (append events (list event)))
1025 (setq key (apply 'vector events))
1026 (or (and (consp event)
1027 (eq (car event) 'switch-frame))
1028 (and (consp event)
1029 (eq (posn-point (event-end event))
1030 'vertical-scroll-bar))
1031 (and (memq 'down (event-modifiers event))
1032 (not (key-binding key))
1033 (not (mouse-undouble-last-event events))
1034 (not (member key mouse-region-delete-keys)))))
1035 (and (consp event)
1036 (or (eq (car event) 'switch-frame)
1037 (eq (posn-point (event-end event))
1038 'vertical-scroll-bar))
1039 (let ((keys (vector 'vertical-scroll-bar event)))
1040 (and (key-binding keys)
1041 (progn
1042 (call-interactively (key-binding keys)
1043 nil keys)
1044 (setq events nil)))))))
1045 ;; If we lost the selection, just turn off the highlighting.
1046 (if ignore
1047 nil
1048 ;; For certain special keys, delete the region.
1049 (if (member key mouse-region-delete-keys)
1050 (delete-region (overlay-start mouse-drag-overlay)
1051 (overlay-end mouse-drag-overlay))
1052 ;; Otherwise, unread the key so it gets executed normally.
1053 (setq unread-command-events
1054 (nconc events unread-command-events))))
1055 (setq quit-flag nil)
1056 (delete-overlay mouse-drag-overlay))))
1057
1058 (defun mouse-set-mark (click)
1059 "Set mark at the position clicked on with the mouse.
1060 Display cursor at that position for a second.
1061 This must be bound to a mouse click."
1062 (interactive "e")
1063 (mouse-minibuffer-check click)
1064 (select-window (posn-window (event-start click)))
1065 ;; We don't use save-excursion because that preserves the mark too.
1066 (let ((point-save (point)))
1067 (unwind-protect
1068 (progn (mouse-set-point click)
1069 (push-mark nil t t)
1070 (or transient-mark-mode
1071 (sit-for 1)))
1072 (goto-char point-save))))
1073
1074 (defun mouse-kill (click)
1075 "Kill the region between point and the mouse click.
1076 The text is saved in the kill ring, as with \\[kill-region]."
1077 (interactive "e")
1078 (mouse-minibuffer-check click)
1079 (let* ((posn (event-start click))
1080 (click-posn (posn-point posn)))
1081 (select-window (posn-window posn))
1082 (if (numberp click-posn)
1083 (kill-region (min (point) click-posn)
1084 (max (point) click-posn)))))
1085
1086 (defun mouse-yank-at-click (click arg)
1087 "Insert the last stretch of killed text at the position clicked on.
1088 Also move point to one end of the text thus inserted (normally the end),
1089 and set mark at the beginning.
1090 Prefix arguments are interpreted as with \\[yank].
1091 If `mouse-yank-at-point' is non-nil, insert at point
1092 regardless of where you click."
1093 (interactive "e\nP")
1094 ;; Give temporary modes such as isearch a chance to turn off.
1095 (run-hooks 'mouse-leave-buffer-hook)
1096 (or mouse-yank-at-point (mouse-set-point click))
1097 (setq this-command 'yank)
1098 (setq mouse-selection-click-count 0)
1099 (yank arg))
1100
1101 (defun mouse-kill-ring-save (click)
1102 "Copy the region between point and the mouse click in the kill ring.
1103 This does not delete the region; it acts like \\[kill-ring-save]."
1104 (interactive "e")
1105 (mouse-set-mark-fast click)
1106 (let (this-command last-command)
1107 (kill-ring-save (point) (mark t)))
1108 (mouse-show-mark))
1109
1110 ;;; This function used to delete the text between point and the mouse
1111 ;;; whenever it was equal to the front of the kill ring, but some
1112 ;;; people found that confusing.
1113
1114 ;;; A list (TEXT START END), describing the text and position of the last
1115 ;;; invocation of mouse-save-then-kill.
1116 (defvar mouse-save-then-kill-posn nil)
1117
1118 (defun mouse-save-then-kill-delete-region (beg end)
1119 ;; We must make our own undo boundaries
1120 ;; because they happen automatically only for the current buffer.
1121 (undo-boundary)
1122 (if (or (= beg end) (eq buffer-undo-list t))
1123 ;; If we have no undo list in this buffer,
1124 ;; just delete.
1125 (delete-region beg end)
1126 ;; Delete, but make the undo-list entry share with the kill ring.
1127 ;; First, delete just one char, so in case buffer is being modified
1128 ;; for the first time, the undo list records that fact.
1129 (let (before-change-functions after-change-functions)
1130 (delete-region beg
1131 (+ beg (if (> end beg) 1 -1))))
1132 (let ((buffer-undo-list buffer-undo-list))
1133 ;; Undo that deletion--but don't change the undo list!
1134 (let (before-change-functions after-change-functions)
1135 (primitive-undo 1 buffer-undo-list))
1136 ;; Now delete the rest of the specified region,
1137 ;; but don't record it.
1138 (setq buffer-undo-list t)
1139 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1140 (error "Lossage in mouse-save-then-kill-delete-region"))
1141 (delete-region beg end))
1142 (let ((tail buffer-undo-list))
1143 ;; Search back in buffer-undo-list for the string
1144 ;; that came from deleting one character.
1145 (while (and tail (not (stringp (car (car tail)))))
1146 (setq tail (cdr tail)))
1147 ;; Replace it with an entry for the entire deleted text.
1148 (and tail
1149 (setcar tail (cons (car kill-ring) (min beg end))))))
1150 (undo-boundary))
1151
1152 (defun mouse-save-then-kill (click)
1153 "Save text to point in kill ring; the second time, kill the text.
1154 If the text between point and the mouse is the same as what's
1155 at the front of the kill ring, this deletes the text.
1156 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1157 which prepares for a second click to delete the text.
1158
1159 If you have selected words or lines, this command extends the
1160 selection through the word or line clicked on. If you do this
1161 again in a different position, it extends the selection again.
1162 If you do this twice in the same position, the selection is killed."
1163 (interactive "e")
1164 (let ((before-scroll
1165 (with-current-buffer (window-buffer (posn-window (event-start click)))
1166 point-before-scroll)))
1167 (mouse-minibuffer-check click)
1168 (let ((click-posn (posn-point (event-start click)))
1169 ;; Don't let a subsequent kill command append to this one:
1170 ;; prevent setting this-command to kill-region.
1171 (this-command this-command))
1172 (if (and (save-excursion
1173 (set-buffer (window-buffer (posn-window (event-start click))))
1174 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1175 ;; Don't be fooled by a recent click in some other buffer.
1176 (eq mouse-selection-click-count-buffer
1177 (current-buffer)))))
1178 (if (not (and (eq last-command 'mouse-save-then-kill)
1179 (equal click-posn
1180 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1181 ;; Find both ends of the object selected by this click.
1182 (let* ((range
1183 (mouse-start-end click-posn click-posn
1184 mouse-selection-click-count)))
1185 ;; Move whichever end is closer to the click.
1186 ;; That's what xterm does, and it seems reasonable.
1187 (if (< (abs (- click-posn (mark t)))
1188 (abs (- click-posn (point))))
1189 (set-mark (car range))
1190 (goto-char (nth 1 range)))
1191 ;; We have already put the old region in the kill ring.
1192 ;; Replace it with the extended region.
1193 ;; (It would be annoying to make a separate entry.)
1194 (kill-new (buffer-substring (point) (mark t)) t)
1195 (mouse-set-region-1)
1196 ;; Arrange for a repeated mouse-3 to kill this region.
1197 (setq mouse-save-then-kill-posn
1198 (list (car kill-ring) (point) click-posn))
1199 (mouse-show-mark))
1200 ;; If we click this button again without moving it,
1201 ;; that time kill.
1202 (mouse-save-then-kill-delete-region (mark) (point))
1203 (setq mouse-selection-click-count 0)
1204 (setq mouse-save-then-kill-posn nil))
1205 (if (and (eq last-command 'mouse-save-then-kill)
1206 mouse-save-then-kill-posn
1207 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1208 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1209 ;; If this is the second time we've called
1210 ;; mouse-save-then-kill, delete the text from the buffer.
1211 (progn
1212 (mouse-save-then-kill-delete-region (point) (mark))
1213 ;; After we kill, another click counts as "the first time".
1214 (setq mouse-save-then-kill-posn nil))
1215 ;; This is not a repetition.
1216 ;; We are adjusting an old selection or creating a new one.
1217 (if (or (and (eq last-command 'mouse-save-then-kill)
1218 mouse-save-then-kill-posn)
1219 (and mark-active transient-mark-mode)
1220 (and (memq last-command
1221 '(mouse-drag-region mouse-set-region))
1222 (or mark-even-if-inactive
1223 (not transient-mark-mode))))
1224 ;; We have a selection or suitable region, so adjust it.
1225 (let* ((posn (event-start click))
1226 (new (posn-point posn)))
1227 (select-window (posn-window posn))
1228 (if (numberp new)
1229 (progn
1230 ;; Move whichever end of the region is closer to the click.
1231 ;; That is what xterm does, and it seems reasonable.
1232 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1233 (goto-char new)
1234 (set-mark new))
1235 (setq deactivate-mark nil)))
1236 (kill-new (buffer-substring (point) (mark t)) t)
1237 (mouse-show-mark))
1238 ;; Set the mark where point is, then move where clicked.
1239 (mouse-set-mark-fast click)
1240 (if before-scroll
1241 (goto-char before-scroll))
1242 (exchange-point-and-mark)
1243 (kill-new (buffer-substring (point) (mark t)))
1244 (mouse-show-mark))
1245 (mouse-set-region-1)
1246 (setq mouse-save-then-kill-posn
1247 (list (car kill-ring) (point) click-posn)))))))
1248 \f
1249 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1250 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1251 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1252 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1253 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1254
1255 ;; An overlay which records the current secondary selection
1256 ;; or else is deleted when there is no secondary selection.
1257 ;; May be nil.
1258 (defvar mouse-secondary-overlay nil)
1259
1260 (defvar mouse-secondary-click-count 0)
1261
1262 ;; A marker which records the specified first end for a secondary selection.
1263 ;; May be nil.
1264 (defvar mouse-secondary-start nil)
1265
1266 (defun mouse-start-secondary (click)
1267 "Set one end of the secondary selection to the position clicked on.
1268 Use \\[mouse-secondary-save-then-kill] to set the other end
1269 and complete the secondary selection."
1270 (interactive "e")
1271 (mouse-minibuffer-check click)
1272 (let ((posn (event-start click)))
1273 (save-excursion
1274 (set-buffer (window-buffer (posn-window posn)))
1275 ;; Cancel any preexisting secondary selection.
1276 (if mouse-secondary-overlay
1277 (delete-overlay mouse-secondary-overlay))
1278 (if (numberp (posn-point posn))
1279 (progn
1280 (or mouse-secondary-start
1281 (setq mouse-secondary-start (make-marker)))
1282 (move-marker mouse-secondary-start (posn-point posn)))))))
1283
1284 (defun mouse-set-secondary (click)
1285 "Set the secondary selection to the text that the mouse is dragged over.
1286 This must be bound to a mouse drag event."
1287 (interactive "e")
1288 (mouse-minibuffer-check click)
1289 (let ((posn (event-start click))
1290 beg
1291 (end (event-end click)))
1292 (save-excursion
1293 (set-buffer (window-buffer (posn-window posn)))
1294 (if (numberp (posn-point posn))
1295 (setq beg (posn-point posn)))
1296 (if mouse-secondary-overlay
1297 (move-overlay mouse-secondary-overlay beg (posn-point end))
1298 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1299 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1300
1301 (defun mouse-drag-secondary (start-event)
1302 "Set the secondary selection to the text that the mouse is dragged over.
1303 Highlight the drag area as you move the mouse.
1304 This must be bound to a button-down mouse event.
1305 The function returns a non-nil value if it creates a secondary selection."
1306 (interactive "e")
1307 (mouse-minibuffer-check start-event)
1308 (let* ((echo-keystrokes 0)
1309 (start-posn (event-start start-event))
1310 (start-point (posn-point start-posn))
1311 (start-window (posn-window start-posn))
1312 (start-frame (window-frame start-window))
1313 (bounds (window-edges start-window))
1314 (top (nth 1 bounds))
1315 (bottom (if (window-minibuffer-p start-window)
1316 (nth 3 bounds)
1317 ;; Don't count the mode line.
1318 (1- (nth 3 bounds))))
1319 (click-count (1- (event-click-count start-event))))
1320 (save-excursion
1321 (set-buffer (window-buffer start-window))
1322 (setq mouse-secondary-click-count click-count)
1323 (or mouse-secondary-overlay
1324 (setq mouse-secondary-overlay
1325 (make-overlay (point) (point))))
1326 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1327 (if (> (mod click-count 3) 0)
1328 ;; Double or triple press: make an initial selection
1329 ;; of one word or line.
1330 (let ((range (mouse-start-end start-point start-point click-count)))
1331 (set-marker mouse-secondary-start nil)
1332 (move-overlay mouse-secondary-overlay 1 1
1333 (window-buffer start-window))
1334 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1335 (window-buffer start-window)))
1336 ;; Single-press: cancel any preexisting secondary selection.
1337 (or mouse-secondary-start
1338 (setq mouse-secondary-start (make-marker)))
1339 (set-marker mouse-secondary-start start-point)
1340 (delete-overlay mouse-secondary-overlay))
1341 (let (event end end-point)
1342 (track-mouse
1343 (while (progn
1344 (setq event (read-event))
1345 (or (mouse-movement-p event)
1346 (eq (car-safe event) 'switch-frame)))
1347
1348 (if (eq (car-safe event) 'switch-frame)
1349 nil
1350 (setq end (event-end event)
1351 end-point (posn-point end))
1352 (cond
1353 ;; Are we moving within the original window?
1354 ((and (eq (posn-window end) start-window)
1355 (integer-or-marker-p end-point))
1356 (let ((range (mouse-start-end start-point end-point
1357 click-count)))
1358 (if (or (/= start-point end-point)
1359 (null (marker-position mouse-secondary-start)))
1360 (progn
1361 (set-marker mouse-secondary-start nil)
1362 (move-overlay mouse-secondary-overlay
1363 (car range) (nth 1 range))))))
1364 (t
1365 (let ((mouse-row (cdr (cdr (mouse-position)))))
1366 (cond
1367 ((null mouse-row))
1368 ((< mouse-row top)
1369 (mouse-scroll-subr start-window (- mouse-row top)
1370 mouse-secondary-overlay start-point))
1371 ((>= mouse-row bottom)
1372 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1373 mouse-secondary-overlay start-point)))))))))
1374
1375 (if (consp event)
1376 (if (marker-position mouse-secondary-start)
1377 (save-window-excursion
1378 (delete-overlay mouse-secondary-overlay)
1379 (x-set-selection 'SECONDARY nil)
1380 (select-window start-window)
1381 (save-excursion
1382 (goto-char mouse-secondary-start)
1383 (sit-for 1)
1384 nil))
1385 (x-set-selection
1386 'SECONDARY
1387 (buffer-substring (overlay-start mouse-secondary-overlay)
1388 (overlay-end mouse-secondary-overlay)))))))))
1389
1390 (defun mouse-yank-secondary (click)
1391 "Insert the secondary selection at the position clicked on.
1392 Move point to the end of the inserted text.
1393 If `mouse-yank-at-point' is non-nil, insert at point
1394 regardless of where you click."
1395 (interactive "e")
1396 ;; Give temporary modes such as isearch a chance to turn off.
1397 (run-hooks 'mouse-leave-buffer-hook)
1398 (or mouse-yank-at-point (mouse-set-point click))
1399 (insert (x-get-selection 'SECONDARY)))
1400
1401 (defun mouse-kill-secondary ()
1402 "Kill the text in the secondary selection.
1403 This is intended more as a keyboard command than as a mouse command
1404 but it can work as either one.
1405
1406 The current buffer (in case of keyboard use), or the buffer clicked on,
1407 must be the one that the secondary selection is in. This requirement
1408 is to prevent accidents."
1409 (interactive)
1410 (let* ((keys (this-command-keys))
1411 (click (elt keys (1- (length keys)))))
1412 (or (eq (overlay-buffer mouse-secondary-overlay)
1413 (if (listp click)
1414 (window-buffer (posn-window (event-start click)))
1415 (current-buffer)))
1416 (error "Select or click on the buffer where the secondary selection is")))
1417 (let (this-command)
1418 (save-excursion
1419 (set-buffer (overlay-buffer mouse-secondary-overlay))
1420 (kill-region (overlay-start mouse-secondary-overlay)
1421 (overlay-end mouse-secondary-overlay))))
1422 (delete-overlay mouse-secondary-overlay)
1423 ;;; (x-set-selection 'SECONDARY nil)
1424 (setq mouse-secondary-overlay nil))
1425
1426 (defun mouse-secondary-save-then-kill (click)
1427 "Save text to point in kill ring; the second time, kill the text.
1428 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1429 If the text between where you did \\[mouse-start-secondary] and where
1430 you use this command matches the text at the front of the kill ring,
1431 this command deletes the text.
1432 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1433 which prepares for a second click with this command to delete the text.
1434
1435 If you have already made a secondary selection in that buffer,
1436 this command extends or retracts the selection to where you click.
1437 If you do this again in a different position, it extends or retracts
1438 again. If you do this twice in the same position, it kills the selection."
1439 (interactive "e")
1440 (mouse-minibuffer-check click)
1441 (let ((posn (event-start click))
1442 (click-posn (posn-point (event-start click)))
1443 ;; Don't let a subsequent kill command append to this one:
1444 ;; prevent setting this-command to kill-region.
1445 (this-command this-command))
1446 (or (eq (window-buffer (posn-window posn))
1447 (or (and mouse-secondary-overlay
1448 (overlay-buffer mouse-secondary-overlay))
1449 (if mouse-secondary-start
1450 (marker-buffer mouse-secondary-start))))
1451 (error "Wrong buffer"))
1452 (save-excursion
1453 (set-buffer (window-buffer (posn-window posn)))
1454 (if (> (mod mouse-secondary-click-count 3) 0)
1455 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1456 (equal click-posn
1457 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1458 ;; Find both ends of the object selected by this click.
1459 (let* ((range
1460 (mouse-start-end click-posn click-posn
1461 mouse-secondary-click-count)))
1462 ;; Move whichever end is closer to the click.
1463 ;; That's what xterm does, and it seems reasonable.
1464 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1465 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1466 (move-overlay mouse-secondary-overlay (car range)
1467 (overlay-end mouse-secondary-overlay))
1468 (move-overlay mouse-secondary-overlay
1469 (overlay-start mouse-secondary-overlay)
1470 (nth 1 range)))
1471 ;; We have already put the old region in the kill ring.
1472 ;; Replace it with the extended region.
1473 ;; (It would be annoying to make a separate entry.)
1474 (kill-new (buffer-substring
1475 (overlay-start mouse-secondary-overlay)
1476 (overlay-end mouse-secondary-overlay)) t)
1477 ;; Arrange for a repeated mouse-3 to kill this region.
1478 (setq mouse-save-then-kill-posn
1479 (list (car kill-ring) (point) click-posn)))
1480 ;; If we click this button again without moving it,
1481 ;; that time kill.
1482 (progn
1483 (mouse-save-then-kill-delete-region
1484 (overlay-start mouse-secondary-overlay)
1485 (overlay-end mouse-secondary-overlay))
1486 (setq mouse-save-then-kill-posn nil)
1487 (setq mouse-secondary-click-count 0)
1488 (delete-overlay mouse-secondary-overlay)))
1489 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1490 mouse-save-then-kill-posn
1491 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1492 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1493 ;; If this is the second time we've called
1494 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1495 (progn
1496 (mouse-save-then-kill-delete-region
1497 (overlay-start mouse-secondary-overlay)
1498 (overlay-end mouse-secondary-overlay))
1499 (setq mouse-save-then-kill-posn nil)
1500 (delete-overlay mouse-secondary-overlay))
1501 (if (overlay-start mouse-secondary-overlay)
1502 ;; We have a selection, so adjust it.
1503 (progn
1504 (if (numberp click-posn)
1505 (progn
1506 ;; Move whichever end of the region is closer to the click.
1507 ;; That is what xterm does, and it seems reasonable.
1508 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1509 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1510 (move-overlay mouse-secondary-overlay click-posn
1511 (overlay-end mouse-secondary-overlay))
1512 (move-overlay mouse-secondary-overlay
1513 (overlay-start mouse-secondary-overlay)
1514 click-posn))
1515 (setq deactivate-mark nil)))
1516 (if (eq last-command 'mouse-secondary-save-then-kill)
1517 ;; If the front of the kill ring comes from
1518 ;; an immediately previous use of this command,
1519 ;; replace it with the extended region.
1520 ;; (It would be annoying to make a separate entry.)
1521 (kill-new (buffer-substring
1522 (overlay-start mouse-secondary-overlay)
1523 (overlay-end mouse-secondary-overlay)) t)
1524 (let (deactivate-mark)
1525 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1526 (overlay-end mouse-secondary-overlay)))))
1527 (if mouse-secondary-start
1528 ;; All we have is one end of a selection,
1529 ;; so put the other end here.
1530 (let ((start (+ 0 mouse-secondary-start)))
1531 (kill-ring-save start click-posn)
1532 (if mouse-secondary-overlay
1533 (move-overlay mouse-secondary-overlay start click-posn)
1534 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1535 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1536 (setq mouse-save-then-kill-posn
1537 (list (car kill-ring) (point) click-posn))))
1538 (if (overlay-buffer mouse-secondary-overlay)
1539 (x-set-selection 'SECONDARY
1540 (buffer-substring
1541 (overlay-start mouse-secondary-overlay)
1542 (overlay-end mouse-secondary-overlay)))))))
1543 \f
1544 (defcustom mouse-buffer-menu-maxlen 20
1545 "*Number of buffers in one pane (submenu) of the buffer menu.
1546 If we have lots of buffers, divide them into groups of
1547 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1548 :type 'integer
1549 :group 'mouse)
1550
1551 (defcustom mouse-buffer-menu-mode-mult 4
1552 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1553 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1554 will split the buffer menu by the major modes (see
1555 `mouse-buffer-menu-mode-groups') or just by menu length.
1556 Set to 1 (or even 0!) if you want to group by major mode always, and to
1557 a large number if you prefer a mixed multitude. The default is 4."
1558 :type 'integer
1559 :group 'mouse
1560 :version "20.3")
1561
1562 (defvar mouse-buffer-menu-mode-groups
1563 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1564 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1565 . "Mail/News")
1566 ("\\<C\\>" . "C")
1567 ("ObjC" . "C")
1568 ("Text" . "Text")
1569 ("Outline" . "Text")
1570 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1571 ("log\\|diff\\|vc\\|cvs" . "Version Control") ; "Change Management"?
1572 ("Lisp" . "Lisp"))
1573 "How to group various major modes together in \\[mouse-buffer-menu].
1574 Each element has the form (REGEXP . GROUPNAME).
1575 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1576
1577 (defun mouse-buffer-menu (event)
1578 "Pop up a menu of buffers for selection with the mouse.
1579 This switches buffers in the window that you clicked on,
1580 and selects that window."
1581 (interactive "e")
1582 (mouse-minibuffer-check event)
1583 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1584 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1585 (let ((tail buffers))
1586 (while tail
1587 ;; Divide all buffers into buckets for various major modes.
1588 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1589 (with-current-buffer (car tail)
1590 (let* ((adjusted-major-mode major-mode) elt)
1591 (let ((tail mouse-buffer-menu-mode-groups))
1592 (while tail
1593 (if (string-match (car (car tail)) mode-name)
1594 (setq adjusted-major-mode (cdr (car tail))))
1595 (setq tail (cdr tail))))
1596 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1597 (if (null elt)
1598 (setq elt (list adjusted-major-mode
1599 (if (stringp adjusted-major-mode)
1600 adjusted-major-mode
1601 mode-name))
1602 split-by-major-mode (cons elt split-by-major-mode)))
1603 (or (memq (car tail) (cdr (cdr elt)))
1604 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1605 (setq tail (cdr tail))))
1606 ;; Compute the sum of squares of sizes of the major-mode buckets.
1607 (let ((tail split-by-major-mode))
1608 (setq sum-of-squares 0)
1609 (while tail
1610 (setq sum-of-squares
1611 (+ sum-of-squares
1612 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1613 (setq tail (cdr tail))))
1614 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1615 (* (length buffers) (length buffers)))
1616 ;; Subdividing by major modes really helps, so let's do it.
1617 (let (subdivided-menus (buffers-left (length buffers)))
1618 ;; Sort the list to put the most popular major modes first.
1619 (setq split-by-major-mode
1620 (sort split-by-major-mode
1621 (function (lambda (elt1 elt2)
1622 (> (length elt1) (length elt2))))))
1623 ;; Make a separate submenu for each major mode
1624 ;; that has more than one buffer,
1625 ;; unless all the remaining buffers are less than 1/10 of them.
1626 (while (and split-by-major-mode
1627 (and (> (length (car split-by-major-mode)) 3)
1628 (> (* buffers-left 10) (length buffers))))
1629 (let ((this-mode-list (mouse-buffer-menu-alist
1630 (cdr (cdr (car split-by-major-mode))))))
1631 (and this-mode-list
1632 (setq subdivided-menus
1633 (cons (cons
1634 (nth 1 (car split-by-major-mode))
1635 this-mode-list)
1636 subdivided-menus))))
1637 (setq buffers-left
1638 (- buffers-left (length (cdr (car split-by-major-mode)))))
1639 (setq split-by-major-mode (cdr split-by-major-mode)))
1640 ;; If any major modes are left over,
1641 ;; make a single submenu for them.
1642 (if split-by-major-mode
1643 (let ((others-list
1644 (mouse-buffer-menu-alist
1645 ;; we don't need split-by-major-mode any more,
1646 ;; so we can ditch it with nconc.
1647 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1648 (and others-list
1649 (setq subdivided-menus
1650 (cons (cons "Others" others-list)
1651 subdivided-menus)))))
1652 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1653 (progn
1654 (setq alist (mouse-buffer-menu-alist buffers))
1655 (setq menu (cons "Buffer Menu"
1656 (mouse-buffer-menu-split "Select Buffer" alist)))))
1657 (let ((buf (x-popup-menu event menu))
1658 (window (posn-window (event-start event))))
1659 (when buf
1660 (select-window
1661 (if (framep window) (frame-selected-window window)
1662 window))
1663 (switch-to-buffer buf)))))
1664
1665 (defun mouse-buffer-menu-alist (buffers)
1666 (let (tail
1667 (maxlen 0)
1668 head)
1669 (setq buffers
1670 (sort buffers
1671 (function (lambda (elt1 elt2)
1672 (string< (buffer-name elt1) (buffer-name elt2))))))
1673 (setq tail buffers)
1674 (while tail
1675 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1676 (setq maxlen
1677 (max maxlen
1678 (length (buffer-name (car tail))))))
1679 (setq tail (cdr tail)))
1680 (setq tail buffers)
1681 (while tail
1682 (let ((elt (car tail)))
1683 (if (/= (aref (buffer-name elt) 0) ?\ )
1684 (setq head
1685 (cons
1686 (cons
1687 (format
1688 (format "%%%ds %%s%%s %%s" maxlen)
1689 (buffer-name elt)
1690 (if (buffer-modified-p elt) "*" " ")
1691 (save-excursion
1692 (set-buffer elt)
1693 (if buffer-read-only "%" " "))
1694 (or (buffer-file-name elt)
1695 (save-excursion
1696 (set-buffer elt)
1697 (if list-buffers-directory
1698 (expand-file-name
1699 list-buffers-directory)))
1700 ""))
1701 elt)
1702 head))))
1703 (setq tail (cdr tail)))
1704 ;; Compensate for the reversal that the above loop does.
1705 (nreverse head)))
1706
1707 (defun mouse-buffer-menu-split (title alist)
1708 ;; If we have lots of buffers, divide them into groups of 20
1709 ;; and make a pane (or submenu) for each one.
1710 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1711 (let ((alist alist) sublists next
1712 (i 1))
1713 (while alist
1714 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1715 ;; and make them the next element of sublist.
1716 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1717 (if next
1718 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1719 nil))
1720 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1721 sublists))
1722 (setq i (1+ i))
1723 (setq alist next))
1724 (nreverse sublists))
1725 ;; Few buffers--put them all in one pane.
1726 (list (cons title alist))))
1727 \f
1728 ;;; These need to be rewritten for the new scroll bar implementation.
1729
1730 ;;;!! ;; Commands for the scroll bar.
1731 ;;;!!
1732 ;;;!! (defun mouse-scroll-down (click)
1733 ;;;!! (interactive "@e")
1734 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1735 ;;;!!
1736 ;;;!! (defun mouse-scroll-up (click)
1737 ;;;!! (interactive "@e")
1738 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1739 ;;;!!
1740 ;;;!! (defun mouse-scroll-down-full ()
1741 ;;;!! (interactive "@")
1742 ;;;!! (scroll-down nil))
1743 ;;;!!
1744 ;;;!! (defun mouse-scroll-up-full ()
1745 ;;;!! (interactive "@")
1746 ;;;!! (scroll-up nil))
1747 ;;;!!
1748 ;;;!! (defun mouse-scroll-move-cursor (click)
1749 ;;;!! (interactive "@e")
1750 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1751 ;;;!!
1752 ;;;!! (defun mouse-scroll-absolute (event)
1753 ;;;!! (interactive "@e")
1754 ;;;!! (let* ((pos (car event))
1755 ;;;!! (position (car pos))
1756 ;;;!! (length (car (cdr pos))))
1757 ;;;!! (if (<= length 0) (setq length 1))
1758 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1759 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1760 ;;;!! position)
1761 ;;;!! length)
1762 ;;;!! scale-factor)))
1763 ;;;!! (goto-char newpos)
1764 ;;;!! (recenter '(4)))))
1765 ;;;!!
1766 ;;;!! (defun mouse-scroll-left (click)
1767 ;;;!! (interactive "@e")
1768 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1769 ;;;!!
1770 ;;;!! (defun mouse-scroll-right (click)
1771 ;;;!! (interactive "@e")
1772 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1773 ;;;!!
1774 ;;;!! (defun mouse-scroll-left-full ()
1775 ;;;!! (interactive "@")
1776 ;;;!! (scroll-left nil))
1777 ;;;!!
1778 ;;;!! (defun mouse-scroll-right-full ()
1779 ;;;!! (interactive "@")
1780 ;;;!! (scroll-right nil))
1781 ;;;!!
1782 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1783 ;;;!! (interactive "@e")
1784 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1785 ;;;!!
1786 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1787 ;;;!! (interactive "@e")
1788 ;;;!! (let* ((pos (car event))
1789 ;;;!! (position (car pos))
1790 ;;;!! (length (car (cdr pos))))
1791 ;;;!! (set-window-hscroll (selected-window) 33)))
1792 ;;;!!
1793 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1794 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1795 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1796 ;;;!!
1797 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1798 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1799 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1800 ;;;!!
1801 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1802 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1803 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1804 ;;;!!
1805 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1806 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1807 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1808 ;;;!!
1809 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1810 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1811 ;;;!! 'mouse-scroll-absolute-horizontally)
1812 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1813 ;;;!!
1814 ;;;!! (global-set-key [horizontal-slider mouse-1]
1815 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1816 ;;;!! (global-set-key [horizontal-slider mouse-2]
1817 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1818 ;;;!! (global-set-key [horizontal-slider mouse-3]
1819 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1820 ;;;!!
1821 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1822 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1823 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1824 ;;;!!
1825 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1826 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1827 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1828 ;;;!!
1829 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1830 ;;;!! 'mouse-split-window-horizontally)
1831 ;;;!! (global-set-key [mode-line S-mouse-2]
1832 ;;;!! 'mouse-split-window-horizontally)
1833 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1834 ;;;!! 'mouse-split-window)
1835 \f
1836 ;;;!! ;;;;
1837 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1838 ;;;!! ;;;; are of the form:
1839 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1840 ;;;!! ;;
1841 ;;;!! ;;;;
1842 ;;;!! ;;;; Dynamically track mouse coordinates
1843 ;;;!! ;;;;
1844 ;;;!! ;;
1845 ;;;!! ;;(defun track-mouse (event)
1846 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1847 ;;;!! ;; (interactive "@e")
1848 ;;;!! ;; (while mouse-grabbed
1849 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1850 ;;;!! ;; (abs-x (car pos))
1851 ;;;!! ;; (abs-y (cdr pos))
1852 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1853 ;;;!! ;; (list (car pos) (cdr pos))
1854 ;;;!! ;; (selected-window))))
1855 ;;;!! ;; (if (consp relative-coordinate)
1856 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1857 ;;;!! ;; (car relative-coordinate)
1858 ;;;!! ;; (car (cdr relative-coordinate)))
1859 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1860 ;;;!!
1861 ;;;!! ;;
1862 ;;;!! ;; Dynamically put a box around the line indicated by point
1863 ;;;!! ;;
1864 ;;;!! ;;
1865 ;;;!! ;;(require 'backquote)
1866 ;;;!! ;;
1867 ;;;!! ;;(defun mouse-select-buffer-line (event)
1868 ;;;!! ;; (interactive "@e")
1869 ;;;!! ;; (let ((relative-coordinate
1870 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1871 ;;;!! ;; (abs-y (car (cdr (car event)))))
1872 ;;;!! ;; (if (consp relative-coordinate)
1873 ;;;!! ;; (progn
1874 ;;;!! ;; (save-excursion
1875 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1876 ;;;!! ;; (x-draw-rectangle
1877 ;;;!! ;; (selected-screen)
1878 ;;;!! ;; abs-y 0
1879 ;;;!! ;; (save-excursion
1880 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1881 ;;;!! ;; (end-of-line)
1882 ;;;!! ;; (push-mark nil t)
1883 ;;;!! ;; (beginning-of-line)
1884 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1885 ;;;!! ;; (sit-for 1)
1886 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1887 ;;;!! ;;
1888 ;;;!! ;;(defvar last-line-drawn nil)
1889 ;;;!! ;;(defvar begin-delim "[^ \t]")
1890 ;;;!! ;;(defvar end-delim "[^ \t]")
1891 ;;;!! ;;
1892 ;;;!! ;;(defun mouse-boxing (event)
1893 ;;;!! ;; (interactive "@e")
1894 ;;;!! ;; (save-excursion
1895 ;;;!! ;; (let ((screen (selected-screen)))
1896 ;;;!! ;; (while (= (x-mouse-events) 0)
1897 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1898 ;;;!! ;; (abs-x (car pos))
1899 ;;;!! ;; (abs-y (cdr pos))
1900 ;;;!! ;; (relative-coordinate
1901 ;;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
1902 ;;;!! ;; (selected-window)))
1903 ;;;!! ;; (begin-reg nil)
1904 ;;;!! ;; (end-reg nil)
1905 ;;;!! ;; (end-column nil)
1906 ;;;!! ;; (begin-column nil))
1907 ;;;!! ;; (if (and (consp relative-coordinate)
1908 ;;;!! ;; (or (not last-line-drawn)
1909 ;;;!! ;; (not (= last-line-drawn abs-y))))
1910 ;;;!! ;; (progn
1911 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1912 ;;;!! ;; (if (= (following-char) 10)
1913 ;;;!! ;; ()
1914 ;;;!! ;; (progn
1915 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1916 ;;;!! ;; (setq begin-column (1- (current-column)))
1917 ;;;!! ;; (end-of-line)
1918 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1919 ;;;!! ;; (setq end-column (1+ (current-column)))
1920 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1921 ;;;!! ;; (x-draw-rectangle screen
1922 ;;;!! ;; (setq last-line-drawn abs-y)
1923 ;;;!! ;; begin-column
1924 ;;;!! ;; (- end-column begin-column) 1))))))))))
1925 ;;;!! ;;
1926 ;;;!! ;;(defun mouse-erase-box ()
1927 ;;;!! ;; (interactive)
1928 ;;;!! ;; (if last-line-drawn
1929 ;;;!! ;; (progn
1930 ;;;!! ;; (x-erase-rectangle (selected-screen))
1931 ;;;!! ;; (setq last-line-drawn nil))))
1932 ;;;!!
1933 ;;;!! ;;; (defun test-x-rectangle ()
1934 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1935 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1936 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1937 ;;;!!
1938 ;;;!! ;;
1939 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1940 ;;;!! ;;
1941 ;;;!!
1942 ;;;!! (defvar double-start nil)
1943 ;;;!! (defconst double-click-interval 300
1944 ;;;!! "Max ticks between clicks")
1945 ;;;!!
1946 ;;;!! (defun double-down (event)
1947 ;;;!! (interactive "@e")
1948 ;;;!! (if double-start
1949 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1950 ;;;!! (if (< interval double-click-interval)
1951 ;;;!! (progn
1952 ;;;!! (backward-up-list 1)
1953 ;;;!! ;; (message "Interval %d" interval)
1954 ;;;!! (sleep-for 1)))
1955 ;;;!! (setq double-start nil))
1956 ;;;!! (setq double-start (nth 4 event))))
1957 ;;;!!
1958 ;;;!! (defun double-up (event)
1959 ;;;!! (interactive "@e")
1960 ;;;!! (and double-start
1961 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1962 ;;;!! (setq double-start nil)))
1963 ;;;!!
1964 ;;;!! ;;; (defun x-test-doubleclick ()
1965 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1966 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1967 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1968 ;;;!!
1969 ;;;!! ;;
1970 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1971 ;;;!! ;;
1972 ;;;!!
1973 ;;;!! (defvar scrolled-lines 0)
1974 ;;;!! (defconst scroll-speed 1)
1975 ;;;!!
1976 ;;;!! (defun incr-scroll-down (event)
1977 ;;;!! (interactive "@e")
1978 ;;;!! (setq scrolled-lines 0)
1979 ;;;!! (incremental-scroll scroll-speed))
1980 ;;;!!
1981 ;;;!! (defun incr-scroll-up (event)
1982 ;;;!! (interactive "@e")
1983 ;;;!! (setq scrolled-lines 0)
1984 ;;;!! (incremental-scroll (- scroll-speed)))
1985 ;;;!!
1986 ;;;!! (defun incremental-scroll (n)
1987 ;;;!! (while (= (x-mouse-events) 0)
1988 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1989 ;;;!! (scroll-down n)
1990 ;;;!! (sit-for 300 t)))
1991 ;;;!!
1992 ;;;!! (defun incr-scroll-stop (event)
1993 ;;;!! (interactive "@e")
1994 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1995 ;;;!! (setq scrolled-lines 0)
1996 ;;;!! (sleep-for 1))
1997 ;;;!!
1998 ;;;!! ;;; (defun x-testing-scroll ()
1999 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
2000 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
2001 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
2002 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
2003 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
2004 ;;;!!
2005 ;;;!! ;;
2006 ;;;!! ;; Some playthings suitable for picture mode? They need work.
2007 ;;;!! ;;
2008 ;;;!!
2009 ;;;!! (defun mouse-kill-rectangle (event)
2010 ;;;!! "Kill the rectangle between point and the mouse cursor."
2011 ;;;!! (interactive "@e")
2012 ;;;!! (let ((point-save (point)))
2013 ;;;!! (save-excursion
2014 ;;;!! (mouse-set-point event)
2015 ;;;!! (push-mark nil t)
2016 ;;;!! (if (> point-save (point))
2017 ;;;!! (kill-rectangle (point) point-save)
2018 ;;;!! (kill-rectangle point-save (point))))))
2019 ;;;!!
2020 ;;;!! (defun mouse-open-rectangle (event)
2021 ;;;!! "Kill the rectangle between point and the mouse cursor."
2022 ;;;!! (interactive "@e")
2023 ;;;!! (let ((point-save (point)))
2024 ;;;!! (save-excursion
2025 ;;;!! (mouse-set-point event)
2026 ;;;!! (push-mark nil t)
2027 ;;;!! (if (> point-save (point))
2028 ;;;!! (open-rectangle (point) point-save)
2029 ;;;!! (open-rectangle point-save (point))))))
2030 ;;;!!
2031 ;;;!! ;; Must be a better way to do this.
2032 ;;;!!
2033 ;;;!! (defun mouse-multiple-insert (n char)
2034 ;;;!! (while (> n 0)
2035 ;;;!! (insert char)
2036 ;;;!! (setq n (1- n))))
2037 ;;;!!
2038 ;;;!! ;; What this could do is not finalize until button was released.
2039 ;;;!!
2040 ;;;!! (defun mouse-move-text (event)
2041 ;;;!! "Move text from point to cursor position, inserting spaces."
2042 ;;;!! (interactive "@e")
2043 ;;;!! (let* ((relative-coordinate
2044 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
2045 ;;;!! (if (consp relative-coordinate)
2046 ;;;!! (cond ((> (current-column) (car relative-coordinate))
2047 ;;;!! (delete-char
2048 ;;;!! (- (car relative-coordinate) (current-column))))
2049 ;;;!! ((< (current-column) (car relative-coordinate))
2050 ;;;!! (mouse-multiple-insert
2051 ;;;!! (- (car relative-coordinate) (current-column)) " "))
2052 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
2053 \f
2054 ;; Choose a completion with the mouse.
2055
2056 (defun mouse-choose-completion (event)
2057 "Click on an alternative in the `*Completions*' buffer to choose it."
2058 (interactive "e")
2059 ;; Give temporary modes such as isearch a chance to turn off.
2060 (run-hooks 'mouse-leave-buffer-hook)
2061 (let ((buffer (window-buffer))
2062 choice
2063 base-size)
2064 (save-excursion
2065 (set-buffer (window-buffer (posn-window (event-start event))))
2066 (if completion-reference-buffer
2067 (setq buffer completion-reference-buffer))
2068 (setq base-size completion-base-size)
2069 (save-excursion
2070 (goto-char (posn-point (event-start event)))
2071 (let (beg end)
2072 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2073 (setq end (point) beg (1+ (point))))
2074 (if (null beg)
2075 (error "No completion here"))
2076 (setq beg (previous-single-property-change beg 'mouse-face))
2077 (setq end (or (next-single-property-change end 'mouse-face)
2078 (point-max)))
2079 (setq choice (buffer-substring beg end)))))
2080 (let ((owindow (selected-window)))
2081 (select-window (posn-window (event-start event)))
2082 (if (and (one-window-p t 'selected-frame)
2083 (window-dedicated-p (selected-window)))
2084 ;; This is a special buffer's frame
2085 (iconify-frame (selected-frame))
2086 (or (window-dedicated-p (selected-window))
2087 (bury-buffer)))
2088 (select-window owindow))
2089 (choose-completion-string choice buffer base-size)))
2090 \f
2091 ;; Font selection.
2092
2093 (defun font-menu-add-default ()
2094 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2095 (font-alist x-fixed-font-alist)
2096 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2097 (if (assoc "Default" elt)
2098 (delete (assoc "Default" elt) elt))
2099 (setcdr elt
2100 (cons (list "Default" default)
2101 (cdr elt)))))
2102
2103 (defvar x-fixed-font-alist
2104 '("Font menu"
2105 ("Misc"
2106 ;; For these, we specify the pixel height and width.
2107 ("fixed" "fixed")
2108 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2109 ("6x12"
2110 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2111 ("6x13"
2112 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2113 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2114 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2115 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2116 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2117 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2118 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2119 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2120 ("")
2121 ("clean 5x8"
2122 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2123 ("clean 6x8"
2124 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2125 ("clean 8x8"
2126 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2127 ("clean 8x10"
2128 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2129 ("clean 8x14"
2130 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2131 ("clean 8x16"
2132 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2133 ("")
2134 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2135 ;;; We don't seem to have these; who knows what they are.
2136 ;;; ("fg-18" "fg-18")
2137 ;;; ("fg-25" "fg-25")
2138 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2139 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2140 ("lucidasanstypewriter-bold-24"
2141 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2142 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2143 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2144 )
2145 ("Courier"
2146 ;; For these, we specify the point height.
2147 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2148 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2149 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2150 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2151 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2152 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2153 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2154 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2155 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2156 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2157 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2158 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2159 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2160 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2161 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2162 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2163 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2164 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2165 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2166 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2167 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2168 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2169 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2170 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2171 )
2172 "X fonts suitable for use in Emacs.")
2173
2174 (defun mouse-set-font (&rest fonts)
2175 "Select an emacs font from a list of known good fonts and fontsets."
2176 (interactive
2177 (and (display-multi-font-p)
2178 (x-popup-menu
2179 last-nonmenu-event
2180 ;; Append list of fontsets currently defined.
2181 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2182 (if fonts
2183 (let (font)
2184 (while fonts
2185 (condition-case nil
2186 (progn
2187 (set-default-font (car fonts))
2188 (setq font (car fonts))
2189 (setq fonts nil))
2190 (error
2191 (setq fonts (cdr fonts)))))
2192 (if (null font)
2193 (error "Font not found")))
2194 (message "Cannot change fonts on this display")))
2195 \f
2196 ;;; Bindings for mouse commands.
2197
2198 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2199 (global-set-key [mouse-1] 'mouse-set-point)
2200 (global-set-key [drag-mouse-1] 'mouse-set-region)
2201
2202 ;; These are tested for in mouse-drag-region.
2203 (global-set-key [double-mouse-1] 'mouse-set-point)
2204 (global-set-key [triple-mouse-1] 'mouse-set-point)
2205
2206 (global-set-key [mouse-2] 'mouse-yank-at-click)
2207 (global-set-key [mouse-3] 'mouse-save-then-kill)
2208
2209 ;; By binding these to down-going events, we let the user use the up-going
2210 ;; event to make the selection, saving a click.
2211 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2212 (if (not (eq system-type 'ms-dos))
2213 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2214 ;; C-down-mouse-2 is bound in facemenu.el.
2215 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2216
2217
2218 ;; Replaced with dragging mouse-1
2219 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2220
2221 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2222 ;; vertical-line prevents Emacs from signaling an error when the mouse
2223 ;; button is released after dragging these lines, on non-toolkit
2224 ;; versions.
2225 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2226 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2227 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2228 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2229 (global-set-key [header-line mouse-1] 'mouse-select-window)
2230 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2231 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2232 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2233 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2234 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2235 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2236 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2237
2238 (provide 'mouse)
2239
2240 ;; This file contains the functionality of the old mldrag.el.
2241 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2242 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2243 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2244 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2245 (provide 'mldrag)
2246
2247 ;;; mouse.el ends here