]> code.delx.au - gnu-emacs/blob - lisp/mouse.el
(f90-mode-abbrev-table): Mark all the predefined abbrevs as "system"
[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 (interactive "e")
702 (mouse-minibuffer-check start-event)
703 (let* ((echo-keystrokes 0)
704 (start-posn (event-start start-event))
705 (start-point (posn-point start-posn))
706 (start-window (posn-window start-posn))
707 (start-window-start (window-start start-window))
708 (start-frame (window-frame start-window))
709 (start-hscroll (window-hscroll start-window))
710 (bounds (window-edges start-window))
711 (top (nth 1 bounds))
712 (bottom (if (window-minibuffer-p start-window)
713 (nth 3 bounds)
714 ;; Don't count the mode line.
715 (1- (nth 3 bounds))))
716 (click-count (1- (event-click-count start-event))))
717 (setq mouse-selection-click-count click-count)
718 (setq mouse-selection-click-count-buffer (current-buffer))
719 (mouse-set-point start-event)
720 ;; In case the down click is in the middle of some intangible text,
721 ;; use the end of that text, and put it in START-POINT.
722 (if (< (point) start-point)
723 (goto-char start-point))
724 (setq start-point (point))
725 (let ((range (mouse-start-end start-point start-point click-count)))
726 (move-overlay mouse-drag-overlay (car range) (nth 1 range)
727 (window-buffer start-window)))
728 (deactivate-mark)
729 ;; end-of-range is used only in the single-click case.
730 ;; It is the place where the drag has reached so far
731 ;; (but not outside the window where the drag started).
732 (let (event end end-point last-end-point (end-of-range (point)))
733 (track-mouse
734 (while (progn
735 (setq event (read-event))
736 (or (mouse-movement-p event)
737 (eq (car-safe event) 'switch-frame)))
738 (if (eq (car-safe event) 'switch-frame)
739 nil
740 (setq end (event-end event)
741 end-point (posn-point end))
742 (if (numberp end-point)
743 (setq last-end-point end-point))
744
745 (cond
746 ;; Are we moving within the original window?
747 ((and (eq (posn-window end) start-window)
748 (integer-or-marker-p end-point))
749 ;; Go to START-POINT first, so that when we move to END-POINT,
750 ;; if it's in the middle of intangible text,
751 ;; point jumps in the direction away from START-POINT.
752 (goto-char start-point)
753 (goto-char end-point)
754 (if (zerop (% click-count 3))
755 (setq end-of-range (point)))
756 (let ((range (mouse-start-end start-point (point) click-count)))
757 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
758
759 (t
760 (let ((mouse-row (cdr (cdr (mouse-position)))))
761 (cond
762 ((null mouse-row))
763 ((< mouse-row top)
764 (mouse-scroll-subr start-window (- mouse-row top)
765 mouse-drag-overlay start-point)
766 ;; Without this, point tends to jump back to the starting
767 ;; position where the mouse button was pressed down.
768 (setq end-of-range (overlay-start mouse-drag-overlay)))
769 ((>= mouse-row bottom)
770 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
771 mouse-drag-overlay start-point)
772 (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
773
774 ;; In case we did not get a mouse-motion event
775 ;; for the final move of the mouse before a drag event
776 ;; pretend that we did get one.
777 (when (and (memq 'drag (event-modifiers (car-safe event)))
778 (setq end (event-end event)
779 end-point (posn-point end))
780 (eq (posn-window end) start-window)
781 (integer-or-marker-p end-point))
782 ;; Go to START-POINT first, so that when we move to END-POINT,
783 ;; if it's in the middle of intangible text,
784 ;; point jumps in the direction away from START-POINT.
785 (goto-char start-point)
786 (goto-char end-point)
787 (if (zerop (% click-count 3))
788 (setq end-of-range (point)))
789 (let ((range (mouse-start-end start-point (point) click-count)))
790 (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
791
792 (if (consp event)
793 (let ((fun (key-binding (vector (car event)))))
794 ;; Run the binding of the terminating up-event, if possible.
795 ;; In the case of a multiple click, it gives the wrong results,
796 ;; because it would fail to set up a region.
797 (if (not (= (overlay-start mouse-drag-overlay)
798 (overlay-end mouse-drag-overlay)))
799 (let* ((stop-point
800 (if (numberp (posn-point (event-end event)))
801 (posn-point (event-end event))
802 last-end-point))
803 ;; The end that comes from where we ended the drag.
804 ;; Point goes here.
805 (region-termination
806 (if (and stop-point (< stop-point start-point))
807 (overlay-start mouse-drag-overlay)
808 (overlay-end mouse-drag-overlay)))
809 ;; The end that comes from where we started the drag.
810 ;; Mark goes there.
811 (region-commencement
812 (- (+ (overlay-end mouse-drag-overlay)
813 (overlay-start mouse-drag-overlay))
814 region-termination))
815 last-command this-command)
816 (push-mark region-commencement t t)
817 (goto-char region-termination)
818 ;; Don't let copy-region-as-kill set deactivate-mark.
819 (let (deactivate-mark)
820 (copy-region-as-kill (point) (mark t)))
821 (let ((buffer (current-buffer)))
822 (mouse-show-mark)
823 ;; mouse-show-mark can call read-event,
824 ;; and that means the Emacs server could switch buffers
825 ;; under us. If that happened,
826 ;; avoid trying to use the region.
827 (and (mark t) mark-active
828 (eq buffer (current-buffer))
829 (mouse-set-region-1))))
830 (delete-overlay mouse-drag-overlay)
831 ;; Run the binding of the terminating up-event.
832 (when (and (functionp fun)
833 (= start-hscroll (window-hscroll start-window))
834 ;; Don't run the up-event handler if the
835 ;; window start changed in a redisplay after
836 ;; the mouse-set-point for the down-mouse
837 ;; event at the beginning of this function.
838 ;; When the window start has changed, the
839 ;; up-mouse event will contain a different
840 ;; position due to the new window contents,
841 ;; and point is set again.
842 (or end-point
843 (= (window-start start-window)
844 start-window-start)))
845 (setq unread-command-events
846 (cons event unread-command-events)))))
847 (delete-overlay mouse-drag-overlay)))))
848 \f
849 ;; Commands to handle xterm-style multiple clicks.
850
851 (defun mouse-skip-word (dir)
852 "Skip over word, over whitespace, or over identical punctuation.
853 If DIR is positive skip forward; if negative, skip backward."
854 (let* ((char (following-char))
855 (syntax (char-to-string (char-syntax char))))
856 (cond ((string= syntax "w")
857 ;; Here, we can't use skip-syntax-forward/backward because
858 ;; they don't pay attention to word-separating-categories,
859 ;; and thus they will skip over a true word boundary. So,
860 ;; we simularte the original behaviour by using
861 ;; forward-word.
862 (if (< dir 0)
863 (if (not (looking-at "\\<"))
864 (forward-word -1))
865 (if (or (looking-at "\\<") (not (looking-at "\\>")))
866 (forward-word 1))))
867 ((string= syntax " ")
868 (if (< dir 0)
869 (skip-syntax-backward syntax)
870 (skip-syntax-forward syntax)))
871 ((string= syntax "_")
872 (if (< dir 0)
873 (skip-syntax-backward "w_")
874 (skip-syntax-forward "w_")))
875 ((< dir 0)
876 (while (and (not (bobp)) (= (preceding-char) char))
877 (forward-char -1)))
878 (t
879 (while (and (not (eobp)) (= (following-char) char))
880 (forward-char 1))))))
881
882 ;; Return a list of region bounds based on START and END according to MODE.
883 ;; If MODE is 0 then set point to (min START END), mark to (max START END).
884 ;; If MODE is 1 then set point to start of word at (min START END),
885 ;; mark to end of word at (max START END).
886 ;; If MODE is 2 then do the same for lines.
887 (defun mouse-start-end (start end mode)
888 (if (> start end)
889 (let ((temp start))
890 (setq start end
891 end temp)))
892 (setq mode (mod mode 3))
893 (cond ((= mode 0)
894 (list start end))
895 ((and (= mode 1)
896 (= start end)
897 (char-after start)
898 (= (char-syntax (char-after start)) ?\())
899 (list start
900 (save-excursion
901 (goto-char start)
902 (forward-sexp 1)
903 (point))))
904 ((and (= mode 1)
905 (= start end)
906 (char-after start)
907 (= (char-syntax (char-after start)) ?\)))
908 (list (save-excursion
909 (goto-char (1+ start))
910 (backward-sexp 1)
911 (point))
912 (1+ start)))
913 ((and (= mode 1)
914 (= start end)
915 (char-after start)
916 (= (char-syntax (char-after start)) ?\"))
917 (let ((open (or (eq start (point-min))
918 (save-excursion
919 (goto-char (- start 1))
920 (looking-at "\\s(\\|\\s \\|\\s>")))))
921 (if open
922 (list start
923 (save-excursion
924 (condition-case nil
925 (progn
926 (goto-char start)
927 (forward-sexp 1)
928 (point))
929 (error end))))
930 (list (save-excursion
931 (condition-case nil
932 (progn
933 (goto-char (1+ start))
934 (backward-sexp 1)
935 (point))
936 (error end)))
937 (1+ start)))))
938 ((= mode 1)
939 (list (save-excursion
940 (goto-char start)
941 (mouse-skip-word -1)
942 (point))
943 (save-excursion
944 (goto-char end)
945 (mouse-skip-word 1)
946 (point))))
947 ((= mode 2)
948 (list (save-excursion
949 (goto-char start)
950 (beginning-of-line 1)
951 (point))
952 (save-excursion
953 (goto-char end)
954 (forward-line 1)
955 (point))))))
956 \f
957 ;; Subroutine: set the mark where CLICK happened,
958 ;; but don't do anything else.
959 (defun mouse-set-mark-fast (click)
960 (mouse-minibuffer-check click)
961 (let ((posn (event-start click)))
962 (select-window (posn-window posn))
963 (if (numberp (posn-point posn))
964 (push-mark (posn-point posn) t t))))
965
966 (defun mouse-undouble-last-event (events)
967 (let* ((index (1- (length events)))
968 (last (nthcdr index events))
969 (event (car last))
970 (basic (event-basic-type event))
971 (old-modifiers (event-modifiers event))
972 (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
973 (new
974 (if (consp event)
975 ;; Use reverse, not nreverse, since event-modifiers
976 ;; does not copy the list it returns.
977 (cons (event-convert-list (reverse (cons basic modifiers)))
978 (cdr event))
979 event)))
980 (setcar last new)
981 (if (and (not (equal modifiers old-modifiers))
982 (key-binding (apply 'vector events)))
983 t
984 (setcar last event)
985 nil)))
986
987 ;; Momentarily show where the mark is, if highlighting doesn't show it.
988
989 (defvar mouse-region-delete-keys '([delete])
990 "List of keys which shall cause the mouse region to be deleted.")
991
992 (defun mouse-show-mark ()
993 (if transient-mark-mode
994 (delete-overlay mouse-drag-overlay)
995 (if (not (display-graphic-p))
996 (save-excursion
997 (goto-char (mark t))
998 (sit-for 1))
999 (let ((inhibit-quit t)
1000 (echo-keystrokes 0)
1001 event events key ignore
1002 x-lost-selection-hooks)
1003 (add-hook 'x-lost-selection-hooks
1004 (lambda (seltype)
1005 (if (eq seltype 'PRIMARY)
1006 (progn (setq ignore t)
1007 (throw 'mouse-show-mark t)))))
1008 (move-overlay mouse-drag-overlay (point) (mark t))
1009 (catch 'mouse-show-mark
1010 ;; In this loop, execute scroll bar and switch-frame events.
1011 ;; Also ignore down-events that are undefined.
1012 (while (progn (setq event (read-event))
1013 (setq events (append events (list event)))
1014 (setq key (apply 'vector events))
1015 (or (and (consp event)
1016 (eq (car event) 'switch-frame))
1017 (and (consp event)
1018 (eq (posn-point (event-end event))
1019 'vertical-scroll-bar))
1020 (and (memq 'down (event-modifiers event))
1021 (not (key-binding key))
1022 (not (mouse-undouble-last-event events))
1023 (not (member key mouse-region-delete-keys)))))
1024 (and (consp event)
1025 (or (eq (car event) 'switch-frame)
1026 (eq (posn-point (event-end event))
1027 'vertical-scroll-bar))
1028 (let ((keys (vector 'vertical-scroll-bar event)))
1029 (and (key-binding keys)
1030 (progn
1031 (call-interactively (key-binding keys)
1032 nil keys)
1033 (setq events nil)))))))
1034 ;; If we lost the selection, just turn off the highlighting.
1035 (if ignore
1036 nil
1037 ;; For certain special keys, delete the region.
1038 (if (member key mouse-region-delete-keys)
1039 (delete-region (overlay-start mouse-drag-overlay)
1040 (overlay-end mouse-drag-overlay))
1041 ;; Otherwise, unread the key so it gets executed normally.
1042 (setq unread-command-events
1043 (nconc events unread-command-events))))
1044 (setq quit-flag nil)
1045 (delete-overlay mouse-drag-overlay)))))
1046
1047 (defun mouse-set-mark (click)
1048 "Set mark at the position clicked on with the mouse.
1049 Display cursor at that position for a second.
1050 This must be bound to a mouse click."
1051 (interactive "e")
1052 (mouse-minibuffer-check click)
1053 (select-window (posn-window (event-start click)))
1054 ;; We don't use save-excursion because that preserves the mark too.
1055 (let ((point-save (point)))
1056 (unwind-protect
1057 (progn (mouse-set-point click)
1058 (push-mark nil t t)
1059 (or transient-mark-mode
1060 (sit-for 1)))
1061 (goto-char point-save))))
1062
1063 (defun mouse-kill (click)
1064 "Kill the region between point and the mouse click.
1065 The text is saved in the kill ring, as with \\[kill-region]."
1066 (interactive "e")
1067 (mouse-minibuffer-check click)
1068 (let* ((posn (event-start click))
1069 (click-posn (posn-point posn)))
1070 (select-window (posn-window posn))
1071 (if (numberp click-posn)
1072 (kill-region (min (point) click-posn)
1073 (max (point) click-posn)))))
1074
1075 (defun mouse-yank-at-click (click arg)
1076 "Insert the last stretch of killed text at the position clicked on.
1077 Also move point to one end of the text thus inserted (normally the end),
1078 and set mark at the beginning.
1079 Prefix arguments are interpreted as with \\[yank].
1080 If `mouse-yank-at-point' is non-nil, insert at point
1081 regardless of where you click."
1082 (interactive "e\nP")
1083 ;; Give temporary modes such as isearch a chance to turn off.
1084 (run-hooks 'mouse-leave-buffer-hook)
1085 (or mouse-yank-at-point (mouse-set-point click))
1086 (setq this-command 'yank)
1087 (setq mouse-selection-click-count 0)
1088 (yank arg))
1089
1090 (defun mouse-kill-ring-save (click)
1091 "Copy the region between point and the mouse click in the kill ring.
1092 This does not delete the region; it acts like \\[kill-ring-save]."
1093 (interactive "e")
1094 (mouse-set-mark-fast click)
1095 (let (this-command last-command)
1096 (kill-ring-save (point) (mark t)))
1097 (mouse-show-mark))
1098
1099 ;;; This function used to delete the text between point and the mouse
1100 ;;; whenever it was equal to the front of the kill ring, but some
1101 ;;; people found that confusing.
1102
1103 ;;; A list (TEXT START END), describing the text and position of the last
1104 ;;; invocation of mouse-save-then-kill.
1105 (defvar mouse-save-then-kill-posn nil)
1106
1107 (defun mouse-save-then-kill-delete-region (beg end)
1108 ;; We must make our own undo boundaries
1109 ;; because they happen automatically only for the current buffer.
1110 (undo-boundary)
1111 (if (or (= beg end) (eq buffer-undo-list t))
1112 ;; If we have no undo list in this buffer,
1113 ;; just delete.
1114 (delete-region beg end)
1115 ;; Delete, but make the undo-list entry share with the kill ring.
1116 ;; First, delete just one char, so in case buffer is being modified
1117 ;; for the first time, the undo list records that fact.
1118 (let (before-change-functions after-change-functions)
1119 (delete-region beg
1120 (+ beg (if (> end beg) 1 -1))))
1121 (let ((buffer-undo-list buffer-undo-list))
1122 ;; Undo that deletion--but don't change the undo list!
1123 (let (before-change-functions after-change-functions)
1124 (primitive-undo 1 buffer-undo-list))
1125 ;; Now delete the rest of the specified region,
1126 ;; but don't record it.
1127 (setq buffer-undo-list t)
1128 (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
1129 (error "Lossage in mouse-save-then-kill-delete-region"))
1130 (delete-region beg end))
1131 (let ((tail buffer-undo-list))
1132 ;; Search back in buffer-undo-list for the string
1133 ;; that came from deleting one character.
1134 (while (and tail (not (stringp (car (car tail)))))
1135 (setq tail (cdr tail)))
1136 ;; Replace it with an entry for the entire deleted text.
1137 (and tail
1138 (setcar tail (cons (car kill-ring) (min beg end))))))
1139 (undo-boundary))
1140
1141 (defun mouse-save-then-kill (click)
1142 "Save text to point in kill ring; the second time, kill the text.
1143 If the text between point and the mouse is the same as what's
1144 at the front of the kill ring, this deletes the text.
1145 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1146 which prepares for a second click to delete the text.
1147
1148 If you have selected words or lines, this command extends the
1149 selection through the word or line clicked on. If you do this
1150 again in a different position, it extends the selection again.
1151 If you do this twice in the same position, the selection is killed."
1152 (interactive "e")
1153 (let ((before-scroll
1154 (with-current-buffer (window-buffer (posn-window (event-start click)))
1155 point-before-scroll)))
1156 (mouse-minibuffer-check click)
1157 (let ((click-posn (posn-point (event-start click)))
1158 ;; Don't let a subsequent kill command append to this one:
1159 ;; prevent setting this-command to kill-region.
1160 (this-command this-command))
1161 (if (and (save-excursion
1162 (set-buffer (window-buffer (posn-window (event-start click))))
1163 (and (mark t) (> (mod mouse-selection-click-count 3) 0)
1164 ;; Don't be fooled by a recent click in some other buffer.
1165 (eq mouse-selection-click-count-buffer
1166 (current-buffer)))))
1167 (if (not (and (eq last-command 'mouse-save-then-kill)
1168 (equal click-posn
1169 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1170 ;; Find both ends of the object selected by this click.
1171 (let* ((range
1172 (mouse-start-end click-posn click-posn
1173 mouse-selection-click-count)))
1174 ;; Move whichever end is closer to the click.
1175 ;; That's what xterm does, and it seems reasonable.
1176 (if (< (abs (- click-posn (mark t)))
1177 (abs (- click-posn (point))))
1178 (set-mark (car range))
1179 (goto-char (nth 1 range)))
1180 ;; We have already put the old region in the kill ring.
1181 ;; Replace it with the extended region.
1182 ;; (It would be annoying to make a separate entry.)
1183 (kill-new (buffer-substring (point) (mark t)) t)
1184 (mouse-set-region-1)
1185 ;; Arrange for a repeated mouse-3 to kill this region.
1186 (setq mouse-save-then-kill-posn
1187 (list (car kill-ring) (point) click-posn))
1188 (mouse-show-mark))
1189 ;; If we click this button again without moving it,
1190 ;; that time kill.
1191 (mouse-save-then-kill-delete-region (mark) (point))
1192 (setq mouse-selection-click-count 0)
1193 (setq mouse-save-then-kill-posn nil))
1194 (if (and (eq last-command 'mouse-save-then-kill)
1195 mouse-save-then-kill-posn
1196 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1197 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1198 ;; If this is the second time we've called
1199 ;; mouse-save-then-kill, delete the text from the buffer.
1200 (progn
1201 (mouse-save-then-kill-delete-region (point) (mark))
1202 ;; After we kill, another click counts as "the first time".
1203 (setq mouse-save-then-kill-posn nil))
1204 ;; This is not a repetition.
1205 ;; We are adjusting an old selection or creating a new one.
1206 (if (or (and (eq last-command 'mouse-save-then-kill)
1207 mouse-save-then-kill-posn)
1208 (and mark-active transient-mark-mode)
1209 (and (memq last-command
1210 '(mouse-drag-region mouse-set-region))
1211 (or mark-even-if-inactive
1212 (not transient-mark-mode))))
1213 ;; We have a selection or suitable region, so adjust it.
1214 (let* ((posn (event-start click))
1215 (new (posn-point posn)))
1216 (select-window (posn-window posn))
1217 (if (numberp new)
1218 (progn
1219 ;; Move whichever end of the region is closer to the click.
1220 ;; That is what xterm does, and it seems reasonable.
1221 (if (<= (abs (- new (point))) (abs (- new (mark t))))
1222 (goto-char new)
1223 (set-mark new))
1224 (setq deactivate-mark nil)))
1225 (kill-new (buffer-substring (point) (mark t)) t)
1226 (mouse-show-mark))
1227 ;; Set the mark where point is, then move where clicked.
1228 (mouse-set-mark-fast click)
1229 (if before-scroll
1230 (goto-char before-scroll))
1231 (exchange-point-and-mark)
1232 (kill-new (buffer-substring (point) (mark t)))
1233 (mouse-show-mark))
1234 (mouse-set-region-1)
1235 (setq mouse-save-then-kill-posn
1236 (list (car kill-ring) (point) click-posn)))))))
1237 \f
1238 (global-set-key [M-mouse-1] 'mouse-start-secondary)
1239 (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
1240 (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
1241 (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
1242 (global-set-key [M-mouse-2] 'mouse-yank-secondary)
1243
1244 ;; An overlay which records the current secondary selection
1245 ;; or else is deleted when there is no secondary selection.
1246 ;; May be nil.
1247 (defvar mouse-secondary-overlay nil)
1248
1249 (defvar mouse-secondary-click-count 0)
1250
1251 ;; A marker which records the specified first end for a secondary selection.
1252 ;; May be nil.
1253 (defvar mouse-secondary-start nil)
1254
1255 (defun mouse-start-secondary (click)
1256 "Set one end of the secondary selection to the position clicked on.
1257 Use \\[mouse-secondary-save-then-kill] to set the other end
1258 and complete the secondary selection."
1259 (interactive "e")
1260 (mouse-minibuffer-check click)
1261 (let ((posn (event-start click)))
1262 (save-excursion
1263 (set-buffer (window-buffer (posn-window posn)))
1264 ;; Cancel any preexisting secondary selection.
1265 (if mouse-secondary-overlay
1266 (delete-overlay mouse-secondary-overlay))
1267 (if (numberp (posn-point posn))
1268 (progn
1269 (or mouse-secondary-start
1270 (setq mouse-secondary-start (make-marker)))
1271 (move-marker mouse-secondary-start (posn-point posn)))))))
1272
1273 (defun mouse-set-secondary (click)
1274 "Set the secondary selection to the text that the mouse is dragged over.
1275 This must be bound to a mouse drag event."
1276 (interactive "e")
1277 (mouse-minibuffer-check click)
1278 (let ((posn (event-start click))
1279 beg
1280 (end (event-end click)))
1281 (save-excursion
1282 (set-buffer (window-buffer (posn-window posn)))
1283 (if (numberp (posn-point posn))
1284 (setq beg (posn-point posn)))
1285 (if mouse-secondary-overlay
1286 (move-overlay mouse-secondary-overlay beg (posn-point end))
1287 (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
1288 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1289
1290 (defun mouse-drag-secondary (start-event)
1291 "Set the secondary selection to the text that the mouse is dragged over.
1292 Highlight the drag area as you move the mouse.
1293 This must be bound to a button-down mouse event.
1294 The function returns a non-nil value if it creates a secondary selection."
1295 (interactive "e")
1296 (mouse-minibuffer-check start-event)
1297 (let* ((echo-keystrokes 0)
1298 (start-posn (event-start start-event))
1299 (start-point (posn-point start-posn))
1300 (start-window (posn-window start-posn))
1301 (start-frame (window-frame start-window))
1302 (bounds (window-edges start-window))
1303 (top (nth 1 bounds))
1304 (bottom (if (window-minibuffer-p start-window)
1305 (nth 3 bounds)
1306 ;; Don't count the mode line.
1307 (1- (nth 3 bounds))))
1308 (click-count (1- (event-click-count start-event))))
1309 (save-excursion
1310 (set-buffer (window-buffer start-window))
1311 (setq mouse-secondary-click-count click-count)
1312 (or mouse-secondary-overlay
1313 (setq mouse-secondary-overlay
1314 (make-overlay (point) (point))))
1315 (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
1316 (if (> (mod click-count 3) 0)
1317 ;; Double or triple press: make an initial selection
1318 ;; of one word or line.
1319 (let ((range (mouse-start-end start-point start-point click-count)))
1320 (set-marker mouse-secondary-start nil)
1321 (move-overlay mouse-secondary-overlay 1 1
1322 (window-buffer start-window))
1323 (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
1324 (window-buffer start-window)))
1325 ;; Single-press: cancel any preexisting secondary selection.
1326 (or mouse-secondary-start
1327 (setq mouse-secondary-start (make-marker)))
1328 (set-marker mouse-secondary-start start-point)
1329 (delete-overlay mouse-secondary-overlay))
1330 (let (event end end-point)
1331 (track-mouse
1332 (while (progn
1333 (setq event (read-event))
1334 (or (mouse-movement-p event)
1335 (eq (car-safe event) 'switch-frame)))
1336
1337 (if (eq (car-safe event) 'switch-frame)
1338 nil
1339 (setq end (event-end event)
1340 end-point (posn-point end))
1341 (cond
1342 ;; Are we moving within the original window?
1343 ((and (eq (posn-window end) start-window)
1344 (integer-or-marker-p end-point))
1345 (let ((range (mouse-start-end start-point end-point
1346 click-count)))
1347 (if (or (/= start-point end-point)
1348 (null (marker-position mouse-secondary-start)))
1349 (progn
1350 (set-marker mouse-secondary-start nil)
1351 (move-overlay mouse-secondary-overlay
1352 (car range) (nth 1 range))))))
1353 (t
1354 (let ((mouse-row (cdr (cdr (mouse-position)))))
1355 (cond
1356 ((null mouse-row))
1357 ((< mouse-row top)
1358 (mouse-scroll-subr start-window (- mouse-row top)
1359 mouse-secondary-overlay start-point))
1360 ((>= mouse-row bottom)
1361 (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
1362 mouse-secondary-overlay start-point)))))))))
1363
1364 (if (consp event)
1365 (if (marker-position mouse-secondary-start)
1366 (save-window-excursion
1367 (delete-overlay mouse-secondary-overlay)
1368 (x-set-selection 'SECONDARY nil)
1369 (select-window start-window)
1370 (save-excursion
1371 (goto-char mouse-secondary-start)
1372 (sit-for 1)
1373 nil))
1374 (x-set-selection
1375 'SECONDARY
1376 (buffer-substring (overlay-start mouse-secondary-overlay)
1377 (overlay-end mouse-secondary-overlay)))))))))
1378
1379 (defun mouse-yank-secondary (click)
1380 "Insert the secondary selection at the position clicked on.
1381 Move point to the end of the inserted text.
1382 If `mouse-yank-at-point' is non-nil, insert at point
1383 regardless of where you click."
1384 (interactive "e")
1385 ;; Give temporary modes such as isearch a chance to turn off.
1386 (run-hooks 'mouse-leave-buffer-hook)
1387 (or mouse-yank-at-point (mouse-set-point click))
1388 (insert (x-get-selection 'SECONDARY)))
1389
1390 (defun mouse-kill-secondary ()
1391 "Kill the text in the secondary selection.
1392 This is intended more as a keyboard command than as a mouse command
1393 but it can work as either one.
1394
1395 The current buffer (in case of keyboard use), or the buffer clicked on,
1396 must be the one that the secondary selection is in. This requirement
1397 is to prevent accidents."
1398 (interactive)
1399 (let* ((keys (this-command-keys))
1400 (click (elt keys (1- (length keys)))))
1401 (or (eq (overlay-buffer mouse-secondary-overlay)
1402 (if (listp click)
1403 (window-buffer (posn-window (event-start click)))
1404 (current-buffer)))
1405 (error "Select or click on the buffer where the secondary selection is")))
1406 (let (this-command)
1407 (save-excursion
1408 (set-buffer (overlay-buffer mouse-secondary-overlay))
1409 (kill-region (overlay-start mouse-secondary-overlay)
1410 (overlay-end mouse-secondary-overlay))))
1411 (delete-overlay mouse-secondary-overlay)
1412 ;;; (x-set-selection 'SECONDARY nil)
1413 (setq mouse-secondary-overlay nil))
1414
1415 (defun mouse-secondary-save-then-kill (click)
1416 "Save text to point in kill ring; the second time, kill the text.
1417 You must use this in a buffer where you have recently done \\[mouse-start-secondary].
1418 If the text between where you did \\[mouse-start-secondary] and where
1419 you use this command matches the text at the front of the kill ring,
1420 this command deletes the text.
1421 Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
1422 which prepares for a second click with this command to delete the text.
1423
1424 If you have already made a secondary selection in that buffer,
1425 this command extends or retracts the selection to where you click.
1426 If you do this again in a different position, it extends or retracts
1427 again. If you do this twice in the same position, it kills the selection."
1428 (interactive "e")
1429 (mouse-minibuffer-check click)
1430 (let ((posn (event-start click))
1431 (click-posn (posn-point (event-start click)))
1432 ;; Don't let a subsequent kill command append to this one:
1433 ;; prevent setting this-command to kill-region.
1434 (this-command this-command))
1435 (or (eq (window-buffer (posn-window posn))
1436 (or (and mouse-secondary-overlay
1437 (overlay-buffer mouse-secondary-overlay))
1438 (if mouse-secondary-start
1439 (marker-buffer mouse-secondary-start))))
1440 (error "Wrong buffer"))
1441 (save-excursion
1442 (set-buffer (window-buffer (posn-window posn)))
1443 (if (> (mod mouse-secondary-click-count 3) 0)
1444 (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
1445 (equal click-posn
1446 (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
1447 ;; Find both ends of the object selected by this click.
1448 (let* ((range
1449 (mouse-start-end click-posn click-posn
1450 mouse-secondary-click-count)))
1451 ;; Move whichever end is closer to the click.
1452 ;; That's what xterm does, and it seems reasonable.
1453 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1454 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1455 (move-overlay mouse-secondary-overlay (car range)
1456 (overlay-end mouse-secondary-overlay))
1457 (move-overlay mouse-secondary-overlay
1458 (overlay-start mouse-secondary-overlay)
1459 (nth 1 range)))
1460 ;; We have already put the old region in the kill ring.
1461 ;; Replace it with the extended region.
1462 ;; (It would be annoying to make a separate entry.)
1463 (kill-new (buffer-substring
1464 (overlay-start mouse-secondary-overlay)
1465 (overlay-end mouse-secondary-overlay)) t)
1466 ;; Arrange for a repeated mouse-3 to kill this region.
1467 (setq mouse-save-then-kill-posn
1468 (list (car kill-ring) (point) click-posn)))
1469 ;; If we click this button again without moving it,
1470 ;; that time kill.
1471 (progn
1472 (mouse-save-then-kill-delete-region
1473 (overlay-start mouse-secondary-overlay)
1474 (overlay-end mouse-secondary-overlay))
1475 (setq mouse-save-then-kill-posn nil)
1476 (setq mouse-secondary-click-count 0)
1477 (delete-overlay mouse-secondary-overlay)))
1478 (if (and (eq last-command 'mouse-secondary-save-then-kill)
1479 mouse-save-then-kill-posn
1480 (eq (car mouse-save-then-kill-posn) (car kill-ring))
1481 (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
1482 ;; If this is the second time we've called
1483 ;; mouse-secondary-save-then-kill, delete the text from the buffer.
1484 (progn
1485 (mouse-save-then-kill-delete-region
1486 (overlay-start mouse-secondary-overlay)
1487 (overlay-end mouse-secondary-overlay))
1488 (setq mouse-save-then-kill-posn nil)
1489 (delete-overlay mouse-secondary-overlay))
1490 (if (overlay-start mouse-secondary-overlay)
1491 ;; We have a selection, so adjust it.
1492 (progn
1493 (if (numberp click-posn)
1494 (progn
1495 ;; Move whichever end of the region is closer to the click.
1496 ;; That is what xterm does, and it seems reasonable.
1497 (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
1498 (abs (- click-posn (overlay-end mouse-secondary-overlay))))
1499 (move-overlay mouse-secondary-overlay click-posn
1500 (overlay-end mouse-secondary-overlay))
1501 (move-overlay mouse-secondary-overlay
1502 (overlay-start mouse-secondary-overlay)
1503 click-posn))
1504 (setq deactivate-mark nil)))
1505 (if (eq last-command 'mouse-secondary-save-then-kill)
1506 ;; If the front of the kill ring comes from
1507 ;; an immediately previous use of this command,
1508 ;; replace it with the extended region.
1509 ;; (It would be annoying to make a separate entry.)
1510 (kill-new (buffer-substring
1511 (overlay-start mouse-secondary-overlay)
1512 (overlay-end mouse-secondary-overlay)) t)
1513 (let (deactivate-mark)
1514 (copy-region-as-kill (overlay-start mouse-secondary-overlay)
1515 (overlay-end mouse-secondary-overlay)))))
1516 (if mouse-secondary-start
1517 ;; All we have is one end of a selection,
1518 ;; so put the other end here.
1519 (let ((start (+ 0 mouse-secondary-start)))
1520 (kill-ring-save start click-posn)
1521 (if mouse-secondary-overlay
1522 (move-overlay mouse-secondary-overlay start click-posn)
1523 (setq mouse-secondary-overlay (make-overlay start click-posn)))
1524 (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
1525 (setq mouse-save-then-kill-posn
1526 (list (car kill-ring) (point) click-posn))))
1527 (if (overlay-buffer mouse-secondary-overlay)
1528 (x-set-selection 'SECONDARY
1529 (buffer-substring
1530 (overlay-start mouse-secondary-overlay)
1531 (overlay-end mouse-secondary-overlay)))))))
1532 \f
1533 (defcustom mouse-buffer-menu-maxlen 20
1534 "*Number of buffers in one pane (submenu) of the buffer menu.
1535 If we have lots of buffers, divide them into groups of
1536 `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
1537 :type 'integer
1538 :group 'mouse)
1539
1540 (defcustom mouse-buffer-menu-mode-mult 4
1541 "*Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
1542 This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
1543 will split the buffer menu by the major modes (see
1544 `mouse-buffer-menu-mode-groups') or just by menu length.
1545 Set to 1 (or even 0!) if you want to group by major mode always, and to
1546 a large number if you prefer a mixed multitude. The default is 4."
1547 :type 'integer
1548 :group 'mouse
1549 :version "20.3")
1550
1551 (defvar mouse-buffer-menu-mode-groups
1552 '(("Info\\|Help\\|Apropos\\|Man" . "Help")
1553 ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
1554 . "Mail/News")
1555 ("\\<C\\>" . "C")
1556 ("ObjC" . "C")
1557 ("Text" . "Text")
1558 ("Outline" . "Text")
1559 ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
1560 ("log\\|diff\\|vc\\|cvs" . "Version Control") ; "Change Management"?
1561 ("Lisp" . "Lisp"))
1562 "How to group various major modes together in \\[mouse-buffer-menu].
1563 Each element has the form (REGEXP . GROUPNAME).
1564 If the major mode's name string matches REGEXP, use GROUPNAME instead.")
1565
1566 (defun mouse-buffer-menu (event)
1567 "Pop up a menu of buffers for selection with the mouse.
1568 This switches buffers in the window that you clicked on,
1569 and selects that window."
1570 (interactive "e")
1571 (mouse-minibuffer-check event)
1572 (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares)
1573 ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
1574 (let ((tail buffers))
1575 (while tail
1576 ;; Divide all buffers into buckets for various major modes.
1577 ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
1578 (with-current-buffer (car tail)
1579 (let* ((adjusted-major-mode major-mode) elt)
1580 (let ((tail mouse-buffer-menu-mode-groups))
1581 (while tail
1582 (if (string-match (car (car tail)) mode-name)
1583 (setq adjusted-major-mode (cdr (car tail))))
1584 (setq tail (cdr tail))))
1585 (setq elt (assoc adjusted-major-mode split-by-major-mode))
1586 (if (null elt)
1587 (setq elt (list adjusted-major-mode
1588 (if (stringp adjusted-major-mode)
1589 adjusted-major-mode
1590 mode-name))
1591 split-by-major-mode (cons elt split-by-major-mode)))
1592 (or (memq (car tail) (cdr (cdr elt)))
1593 (setcdr (cdr elt) (cons (car tail) (cdr (cdr elt)))))))
1594 (setq tail (cdr tail))))
1595 ;; Compute the sum of squares of sizes of the major-mode buckets.
1596 (let ((tail split-by-major-mode))
1597 (setq sum-of-squares 0)
1598 (while tail
1599 (setq sum-of-squares
1600 (+ sum-of-squares
1601 (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
1602 (setq tail (cdr tail))))
1603 (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
1604 (* (length buffers) (length buffers)))
1605 ;; Subdividing by major modes really helps, so let's do it.
1606 (let (subdivided-menus (buffers-left (length buffers)))
1607 ;; Sort the list to put the most popular major modes first.
1608 (setq split-by-major-mode
1609 (sort split-by-major-mode
1610 (function (lambda (elt1 elt2)
1611 (> (length elt1) (length elt2))))))
1612 ;; Make a separate submenu for each major mode
1613 ;; that has more than one buffer,
1614 ;; unless all the remaining buffers are less than 1/10 of them.
1615 (while (and split-by-major-mode
1616 (and (> (length (car split-by-major-mode)) 3)
1617 (> (* buffers-left 10) (length buffers))))
1618 (let ((this-mode-list (mouse-buffer-menu-alist
1619 (cdr (cdr (car split-by-major-mode))))))
1620 (and this-mode-list
1621 (setq subdivided-menus
1622 (cons (cons
1623 (nth 1 (car split-by-major-mode))
1624 this-mode-list)
1625 subdivided-menus))))
1626 (setq buffers-left
1627 (- buffers-left (length (cdr (car split-by-major-mode)))))
1628 (setq split-by-major-mode (cdr split-by-major-mode)))
1629 ;; If any major modes are left over,
1630 ;; make a single submenu for them.
1631 (if split-by-major-mode
1632 (let ((others-list
1633 (mouse-buffer-menu-alist
1634 ;; we don't need split-by-major-mode any more,
1635 ;; so we can ditch it with nconc.
1636 (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
1637 (and others-list
1638 (setq subdivided-menus
1639 (cons (cons "Others" others-list)
1640 subdivided-menus)))))
1641 (setq menu (cons "Buffer Menu" (nreverse subdivided-menus))))
1642 (progn
1643 (setq alist (mouse-buffer-menu-alist buffers))
1644 (setq menu (cons "Buffer Menu"
1645 (mouse-buffer-menu-split "Select Buffer" alist)))))
1646 (let ((buf (x-popup-menu event menu))
1647 (window (posn-window (event-start event))))
1648 (when buf
1649 (select-window
1650 (if (framep window) (frame-selected-window window)
1651 window))
1652 (switch-to-buffer buf)))))
1653
1654 (defun mouse-buffer-menu-alist (buffers)
1655 (let (tail
1656 (maxlen 0)
1657 head)
1658 (setq buffers
1659 (sort buffers
1660 (function (lambda (elt1 elt2)
1661 (string< (buffer-name elt1) (buffer-name elt2))))))
1662 (setq tail buffers)
1663 (while tail
1664 (or (eq ?\ (aref (buffer-name (car tail)) 0))
1665 (setq maxlen
1666 (max maxlen
1667 (length (buffer-name (car tail))))))
1668 (setq tail (cdr tail)))
1669 (setq tail buffers)
1670 (while tail
1671 (let ((elt (car tail)))
1672 (if (/= (aref (buffer-name elt) 0) ?\ )
1673 (setq head
1674 (cons
1675 (cons
1676 (format
1677 (format "%%%ds %%s%%s %%s" maxlen)
1678 (buffer-name elt)
1679 (if (buffer-modified-p elt) "*" " ")
1680 (save-excursion
1681 (set-buffer elt)
1682 (if buffer-read-only "%" " "))
1683 (or (buffer-file-name elt)
1684 (save-excursion
1685 (set-buffer elt)
1686 (if list-buffers-directory
1687 (expand-file-name
1688 list-buffers-directory)))
1689 ""))
1690 elt)
1691 head))))
1692 (setq tail (cdr tail)))
1693 ;; Compensate for the reversal that the above loop does.
1694 (nreverse head)))
1695
1696 (defun mouse-buffer-menu-split (title alist)
1697 ;; If we have lots of buffers, divide them into groups of 20
1698 ;; and make a pane (or submenu) for each one.
1699 (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
1700 (let ((alist alist) sublists next
1701 (i 1))
1702 (while alist
1703 ;; Pull off the next mouse-buffer-menu-maxlen buffers
1704 ;; and make them the next element of sublist.
1705 (setq next (nthcdr mouse-buffer-menu-maxlen alist))
1706 (if next
1707 (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
1708 nil))
1709 (setq sublists (cons (cons (format "Buffers %d" i) alist)
1710 sublists))
1711 (setq i (1+ i))
1712 (setq alist next))
1713 (nreverse sublists))
1714 ;; Few buffers--put them all in one pane.
1715 (list (cons title alist))))
1716 \f
1717 ;;; These need to be rewritten for the new scroll bar implementation.
1718
1719 ;;;!! ;; Commands for the scroll bar.
1720 ;;;!!
1721 ;;;!! (defun mouse-scroll-down (click)
1722 ;;;!! (interactive "@e")
1723 ;;;!! (scroll-down (1+ (cdr (mouse-coords click)))))
1724 ;;;!!
1725 ;;;!! (defun mouse-scroll-up (click)
1726 ;;;!! (interactive "@e")
1727 ;;;!! (scroll-up (1+ (cdr (mouse-coords click)))))
1728 ;;;!!
1729 ;;;!! (defun mouse-scroll-down-full ()
1730 ;;;!! (interactive "@")
1731 ;;;!! (scroll-down nil))
1732 ;;;!!
1733 ;;;!! (defun mouse-scroll-up-full ()
1734 ;;;!! (interactive "@")
1735 ;;;!! (scroll-up nil))
1736 ;;;!!
1737 ;;;!! (defun mouse-scroll-move-cursor (click)
1738 ;;;!! (interactive "@e")
1739 ;;;!! (move-to-window-line (1+ (cdr (mouse-coords click)))))
1740 ;;;!!
1741 ;;;!! (defun mouse-scroll-absolute (event)
1742 ;;;!! (interactive "@e")
1743 ;;;!! (let* ((pos (car event))
1744 ;;;!! (position (car pos))
1745 ;;;!! (length (car (cdr pos))))
1746 ;;;!! (if (<= length 0) (setq length 1))
1747 ;;;!! (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
1748 ;;;!! (newpos (* (/ (* (/ (buffer-size) scale-factor)
1749 ;;;!! position)
1750 ;;;!! length)
1751 ;;;!! scale-factor)))
1752 ;;;!! (goto-char newpos)
1753 ;;;!! (recenter '(4)))))
1754 ;;;!!
1755 ;;;!! (defun mouse-scroll-left (click)
1756 ;;;!! (interactive "@e")
1757 ;;;!! (scroll-left (1+ (car (mouse-coords click)))))
1758 ;;;!!
1759 ;;;!! (defun mouse-scroll-right (click)
1760 ;;;!! (interactive "@e")
1761 ;;;!! (scroll-right (1+ (car (mouse-coords click)))))
1762 ;;;!!
1763 ;;;!! (defun mouse-scroll-left-full ()
1764 ;;;!! (interactive "@")
1765 ;;;!! (scroll-left nil))
1766 ;;;!!
1767 ;;;!! (defun mouse-scroll-right-full ()
1768 ;;;!! (interactive "@")
1769 ;;;!! (scroll-right nil))
1770 ;;;!!
1771 ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
1772 ;;;!! (interactive "@e")
1773 ;;;!! (move-to-column (1+ (car (mouse-coords click)))))
1774 ;;;!!
1775 ;;;!! (defun mouse-scroll-absolute-horizontally (event)
1776 ;;;!! (interactive "@e")
1777 ;;;!! (let* ((pos (car event))
1778 ;;;!! (position (car pos))
1779 ;;;!! (length (car (cdr pos))))
1780 ;;;!! (set-window-hscroll (selected-window) 33)))
1781 ;;;!!
1782 ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
1783 ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
1784 ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
1785 ;;;!!
1786 ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
1787 ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
1788 ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
1789 ;;;!!
1790 ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
1791 ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
1792 ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
1793 ;;;!!
1794 ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
1795 ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
1796 ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
1797 ;;;!!
1798 ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
1799 ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
1800 ;;;!! 'mouse-scroll-absolute-horizontally)
1801 ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
1802 ;;;!!
1803 ;;;!! (global-set-key [horizontal-slider mouse-1]
1804 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1805 ;;;!! (global-set-key [horizontal-slider mouse-2]
1806 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1807 ;;;!! (global-set-key [horizontal-slider mouse-3]
1808 ;;;!! 'mouse-scroll-move-cursor-horizontally)
1809 ;;;!!
1810 ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
1811 ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
1812 ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
1813 ;;;!!
1814 ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
1815 ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
1816 ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
1817 ;;;!!
1818 ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
1819 ;;;!! 'mouse-split-window-horizontally)
1820 ;;;!! (global-set-key [mode-line S-mouse-2]
1821 ;;;!! 'mouse-split-window-horizontally)
1822 ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
1823 ;;;!! 'mouse-split-window)
1824 \f
1825 ;;;!! ;;;;
1826 ;;;!! ;;;; Here are experimental things being tested. Mouse events
1827 ;;;!! ;;;; are of the form:
1828 ;;;!! ;;;; ((x y) window screen-part key-sequence timestamp)
1829 ;;;!! ;;
1830 ;;;!! ;;;;
1831 ;;;!! ;;;; Dynamically track mouse coordinates
1832 ;;;!! ;;;;
1833 ;;;!! ;;
1834 ;;;!! ;;(defun track-mouse (event)
1835 ;;;!! ;; "Track the coordinates, absolute and relative, of the mouse."
1836 ;;;!! ;; (interactive "@e")
1837 ;;;!! ;; (while mouse-grabbed
1838 ;;;!! ;; (let* ((pos (read-mouse-position (selected-screen)))
1839 ;;;!! ;; (abs-x (car pos))
1840 ;;;!! ;; (abs-y (cdr pos))
1841 ;;;!! ;; (relative-coordinate (coordinates-in-window-p
1842 ;;;!! ;; (list (car pos) (cdr pos))
1843 ;;;!! ;; (selected-window))))
1844 ;;;!! ;; (if (consp relative-coordinate)
1845 ;;;!! ;; (message "mouse: [%d %d], (%d %d)" abs-x abs-y
1846 ;;;!! ;; (car relative-coordinate)
1847 ;;;!! ;; (car (cdr relative-coordinate)))
1848 ;;;!! ;; (message "mouse: [%d %d]" abs-x abs-y)))))
1849 ;;;!!
1850 ;;;!! ;;
1851 ;;;!! ;; Dynamically put a box around the line indicated by point
1852 ;;;!! ;;
1853 ;;;!! ;;
1854 ;;;!! ;;(require 'backquote)
1855 ;;;!! ;;
1856 ;;;!! ;;(defun mouse-select-buffer-line (event)
1857 ;;;!! ;; (interactive "@e")
1858 ;;;!! ;; (let ((relative-coordinate
1859 ;;;!! ;; (coordinates-in-window-p (car event) (selected-window)))
1860 ;;;!! ;; (abs-y (car (cdr (car event)))))
1861 ;;;!! ;; (if (consp relative-coordinate)
1862 ;;;!! ;; (progn
1863 ;;;!! ;; (save-excursion
1864 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1865 ;;;!! ;; (x-draw-rectangle
1866 ;;;!! ;; (selected-screen)
1867 ;;;!! ;; abs-y 0
1868 ;;;!! ;; (save-excursion
1869 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1870 ;;;!! ;; (end-of-line)
1871 ;;;!! ;; (push-mark nil t)
1872 ;;;!! ;; (beginning-of-line)
1873 ;;;!! ;; (- (region-end) (region-beginning))) 1))
1874 ;;;!! ;; (sit-for 1)
1875 ;;;!! ;; (x-erase-rectangle (selected-screen))))))
1876 ;;;!! ;;
1877 ;;;!! ;;(defvar last-line-drawn nil)
1878 ;;;!! ;;(defvar begin-delim "[^ \t]")
1879 ;;;!! ;;(defvar end-delim "[^ \t]")
1880 ;;;!! ;;
1881 ;;;!! ;;(defun mouse-boxing (event)
1882 ;;;!! ;; (interactive "@e")
1883 ;;;!! ;; (save-excursion
1884 ;;;!! ;; (let ((screen (selected-screen)))
1885 ;;;!! ;; (while (= (x-mouse-events) 0)
1886 ;;;!! ;; (let* ((pos (read-mouse-position screen))
1887 ;;;!! ;; (abs-x (car pos))
1888 ;;;!! ;; (abs-y (cdr pos))
1889 ;;;!! ;; (relative-coordinate
1890 ;;;!! ;; (coordinates-in-window-p `(,abs-x ,abs-y)
1891 ;;;!! ;; (selected-window)))
1892 ;;;!! ;; (begin-reg nil)
1893 ;;;!! ;; (end-reg nil)
1894 ;;;!! ;; (end-column nil)
1895 ;;;!! ;; (begin-column nil))
1896 ;;;!! ;; (if (and (consp relative-coordinate)
1897 ;;;!! ;; (or (not last-line-drawn)
1898 ;;;!! ;; (not (= last-line-drawn abs-y))))
1899 ;;;!! ;; (progn
1900 ;;;!! ;; (move-to-window-line (car (cdr relative-coordinate)))
1901 ;;;!! ;; (if (= (following-char) 10)
1902 ;;;!! ;; ()
1903 ;;;!! ;; (progn
1904 ;;;!! ;; (setq begin-reg (1- (re-search-forward end-delim)))
1905 ;;;!! ;; (setq begin-column (1- (current-column)))
1906 ;;;!! ;; (end-of-line)
1907 ;;;!! ;; (setq end-reg (1+ (re-search-backward begin-delim)))
1908 ;;;!! ;; (setq end-column (1+ (current-column)))
1909 ;;;!! ;; (message "%s" (buffer-substring begin-reg end-reg))
1910 ;;;!! ;; (x-draw-rectangle screen
1911 ;;;!! ;; (setq last-line-drawn abs-y)
1912 ;;;!! ;; begin-column
1913 ;;;!! ;; (- end-column begin-column) 1))))))))))
1914 ;;;!! ;;
1915 ;;;!! ;;(defun mouse-erase-box ()
1916 ;;;!! ;; (interactive)
1917 ;;;!! ;; (if last-line-drawn
1918 ;;;!! ;; (progn
1919 ;;;!! ;; (x-erase-rectangle (selected-screen))
1920 ;;;!! ;; (setq last-line-drawn nil))))
1921 ;;;!!
1922 ;;;!! ;;; (defun test-x-rectangle ()
1923 ;;;!! ;;; (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
1924 ;;;!! ;;; (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
1925 ;;;!! ;;; (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
1926 ;;;!!
1927 ;;;!! ;;
1928 ;;;!! ;; Here is how to do double clicking in lisp. About to change.
1929 ;;;!! ;;
1930 ;;;!!
1931 ;;;!! (defvar double-start nil)
1932 ;;;!! (defconst double-click-interval 300
1933 ;;;!! "Max ticks between clicks")
1934 ;;;!!
1935 ;;;!! (defun double-down (event)
1936 ;;;!! (interactive "@e")
1937 ;;;!! (if double-start
1938 ;;;!! (let ((interval (- (nth 4 event) double-start)))
1939 ;;;!! (if (< interval double-click-interval)
1940 ;;;!! (progn
1941 ;;;!! (backward-up-list 1)
1942 ;;;!! ;; (message "Interval %d" interval)
1943 ;;;!! (sleep-for 1)))
1944 ;;;!! (setq double-start nil))
1945 ;;;!! (setq double-start (nth 4 event))))
1946 ;;;!!
1947 ;;;!! (defun double-up (event)
1948 ;;;!! (interactive "@e")
1949 ;;;!! (and double-start
1950 ;;;!! (> (- (nth 4 event ) double-start) double-click-interval)
1951 ;;;!! (setq double-start nil)))
1952 ;;;!!
1953 ;;;!! ;;; (defun x-test-doubleclick ()
1954 ;;;!! ;;; (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
1955 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left 'double-down)
1956 ;;;!! ;;; (define-key doubleclick-test-map mouse-button-left-up 'double-up))
1957 ;;;!!
1958 ;;;!! ;;
1959 ;;;!! ;; This scrolls while button is depressed. Use preferable in scroll bar.
1960 ;;;!! ;;
1961 ;;;!!
1962 ;;;!! (defvar scrolled-lines 0)
1963 ;;;!! (defconst scroll-speed 1)
1964 ;;;!!
1965 ;;;!! (defun incr-scroll-down (event)
1966 ;;;!! (interactive "@e")
1967 ;;;!! (setq scrolled-lines 0)
1968 ;;;!! (incremental-scroll scroll-speed))
1969 ;;;!!
1970 ;;;!! (defun incr-scroll-up (event)
1971 ;;;!! (interactive "@e")
1972 ;;;!! (setq scrolled-lines 0)
1973 ;;;!! (incremental-scroll (- scroll-speed)))
1974 ;;;!!
1975 ;;;!! (defun incremental-scroll (n)
1976 ;;;!! (while (= (x-mouse-events) 0)
1977 ;;;!! (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
1978 ;;;!! (scroll-down n)
1979 ;;;!! (sit-for 300 t)))
1980 ;;;!!
1981 ;;;!! (defun incr-scroll-stop (event)
1982 ;;;!! (interactive "@e")
1983 ;;;!! (message "Scrolled %d lines" scrolled-lines)
1984 ;;;!! (setq scrolled-lines 0)
1985 ;;;!! (sleep-for 1))
1986 ;;;!!
1987 ;;;!! ;;; (defun x-testing-scroll ()
1988 ;;;!! ;;; (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
1989 ;;;!! ;;; (define-key scrolling-map mouse-button-left 'incr-scroll-down)
1990 ;;;!! ;;; (define-key scrolling-map mouse-button-right 'incr-scroll-up)
1991 ;;;!! ;;; (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
1992 ;;;!! ;;; (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
1993 ;;;!!
1994 ;;;!! ;;
1995 ;;;!! ;; Some playthings suitable for picture mode? They need work.
1996 ;;;!! ;;
1997 ;;;!!
1998 ;;;!! (defun mouse-kill-rectangle (event)
1999 ;;;!! "Kill the rectangle between point and the mouse cursor."
2000 ;;;!! (interactive "@e")
2001 ;;;!! (let ((point-save (point)))
2002 ;;;!! (save-excursion
2003 ;;;!! (mouse-set-point event)
2004 ;;;!! (push-mark nil t)
2005 ;;;!! (if (> point-save (point))
2006 ;;;!! (kill-rectangle (point) point-save)
2007 ;;;!! (kill-rectangle point-save (point))))))
2008 ;;;!!
2009 ;;;!! (defun mouse-open-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 ;;;!! (open-rectangle (point) point-save)
2018 ;;;!! (open-rectangle point-save (point))))))
2019 ;;;!!
2020 ;;;!! ;; Must be a better way to do this.
2021 ;;;!!
2022 ;;;!! (defun mouse-multiple-insert (n char)
2023 ;;;!! (while (> n 0)
2024 ;;;!! (insert char)
2025 ;;;!! (setq n (1- n))))
2026 ;;;!!
2027 ;;;!! ;; What this could do is not finalize until button was released.
2028 ;;;!!
2029 ;;;!! (defun mouse-move-text (event)
2030 ;;;!! "Move text from point to cursor position, inserting spaces."
2031 ;;;!! (interactive "@e")
2032 ;;;!! (let* ((relative-coordinate
2033 ;;;!! (coordinates-in-window-p (car event) (selected-window))))
2034 ;;;!! (if (consp relative-coordinate)
2035 ;;;!! (cond ((> (current-column) (car relative-coordinate))
2036 ;;;!! (delete-char
2037 ;;;!! (- (car relative-coordinate) (current-column))))
2038 ;;;!! ((< (current-column) (car relative-coordinate))
2039 ;;;!! (mouse-multiple-insert
2040 ;;;!! (- (car relative-coordinate) (current-column)) " "))
2041 ;;;!! ((= (current-column) (car relative-coordinate)) (ding))))))
2042 \f
2043 ;; Choose a completion with the mouse.
2044
2045 (defun mouse-choose-completion (event)
2046 "Click on an alternative in the `*Completions*' buffer to choose it."
2047 (interactive "e")
2048 ;; Give temporary modes such as isearch a chance to turn off.
2049 (run-hooks 'mouse-leave-buffer-hook)
2050 (let ((buffer (window-buffer))
2051 choice
2052 base-size)
2053 (save-excursion
2054 (set-buffer (window-buffer (posn-window (event-start event))))
2055 (if completion-reference-buffer
2056 (setq buffer completion-reference-buffer))
2057 (setq base-size completion-base-size)
2058 (save-excursion
2059 (goto-char (posn-point (event-start event)))
2060 (let (beg end)
2061 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
2062 (setq end (point) beg (1+ (point))))
2063 (if (null beg)
2064 (error "No completion here"))
2065 (setq beg (previous-single-property-change beg 'mouse-face))
2066 (setq end (or (next-single-property-change end 'mouse-face)
2067 (point-max)))
2068 (setq choice (buffer-substring beg end)))))
2069 (let ((owindow (selected-window)))
2070 (select-window (posn-window (event-start event)))
2071 (if (and (one-window-p t 'selected-frame)
2072 (window-dedicated-p (selected-window)))
2073 ;; This is a special buffer's frame
2074 (iconify-frame (selected-frame))
2075 (or (window-dedicated-p (selected-window))
2076 (bury-buffer)))
2077 (select-window owindow))
2078 (choose-completion-string choice buffer base-size)))
2079 \f
2080 ;; Font selection.
2081
2082 (defun font-menu-add-default ()
2083 (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
2084 (font-alist x-fixed-font-alist)
2085 (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
2086 (if (assoc "Default" elt)
2087 (delete (assoc "Default" elt) elt))
2088 (setcdr elt
2089 (cons (list "Default" default)
2090 (cdr elt)))))
2091
2092 (defvar x-fixed-font-alist
2093 '("Font menu"
2094 ("Misc"
2095 ;; For these, we specify the pixel height and width.
2096 ("fixed" "fixed")
2097 ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
2098 ("6x12"
2099 "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
2100 ("6x13"
2101 "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
2102 ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
2103 ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
2104 ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
2105 ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
2106 ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
2107 ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
2108 ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
2109 ("")
2110 ("clean 5x8"
2111 "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
2112 ("clean 6x8"
2113 "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
2114 ("clean 8x8"
2115 "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
2116 ("clean 8x10"
2117 "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
2118 ("clean 8x14"
2119 "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
2120 ("clean 8x16"
2121 "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2122 ("")
2123 ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
2124 ;;; We don't seem to have these; who knows what they are.
2125 ;;; ("fg-18" "fg-18")
2126 ;;; ("fg-25" "fg-25")
2127 ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
2128 ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
2129 ("lucidasanstypewriter-bold-24"
2130 "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
2131 ;;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
2132 ;;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
2133 )
2134 ("Courier"
2135 ;; For these, we specify the point height.
2136 ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
2137 ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
2138 ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
2139 ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
2140 ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
2141 ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
2142 ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
2143 ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
2144 ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
2145 ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
2146 ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
2147 ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
2148 ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
2149 ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
2150 ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
2151 ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
2152 ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
2153 ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
2154 ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
2155 ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
2156 ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
2157 ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
2158 ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
2159 ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
2160 )
2161 "X fonts suitable for use in Emacs.")
2162
2163 (defun mouse-set-font (&rest fonts)
2164 "Select an emacs font from a list of known good fonts and fontsets."
2165 (interactive
2166 (and (display-multi-font-p)
2167 (x-popup-menu
2168 last-nonmenu-event
2169 ;; Append list of fontsets currently defined.
2170 (append x-fixed-font-alist (list (generate-fontset-menu))))))
2171 (if fonts
2172 (let (font)
2173 (while fonts
2174 (condition-case nil
2175 (progn
2176 (set-default-font (car fonts))
2177 (setq font (car fonts))
2178 (setq fonts nil))
2179 (error
2180 (setq fonts (cdr fonts)))))
2181 (if (null font)
2182 (error "Font not found")))
2183 (message "Cannot change fonts on this display")))
2184 \f
2185 ;;; Bindings for mouse commands.
2186
2187 (define-key global-map [down-mouse-1] 'mouse-drag-region)
2188 (global-set-key [mouse-1] 'mouse-set-point)
2189 (global-set-key [drag-mouse-1] 'mouse-set-region)
2190
2191 ;; These are tested for in mouse-drag-region.
2192 (global-set-key [double-mouse-1] 'mouse-set-point)
2193 (global-set-key [triple-mouse-1] 'mouse-set-point)
2194
2195 (global-set-key [mouse-2] 'mouse-yank-at-click)
2196 (global-set-key [mouse-3] 'mouse-save-then-kill)
2197
2198 ;; By binding these to down-going events, we let the user use the up-going
2199 ;; event to make the selection, saving a click.
2200 (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
2201 (if (not (eq system-type 'ms-dos))
2202 (global-set-key [S-down-mouse-1] 'mouse-set-font))
2203 ;; C-down-mouse-2 is bound in facemenu.el.
2204 (global-set-key [C-down-mouse-3] 'mouse-popup-menubar-stuff)
2205
2206
2207 ;; Replaced with dragging mouse-1
2208 ;; (global-set-key [S-mouse-1] 'mouse-set-mark)
2209
2210 ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
2211 ;; vertical-line prevents Emacs from signaling an error when the mouse
2212 ;; button is released after dragging these lines, on non-toolkit
2213 ;; versions.
2214 (global-set-key [mode-line mouse-1] 'mouse-select-window)
2215 (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
2216 (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
2217 (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
2218 (global-set-key [header-line mouse-1] 'mouse-select-window)
2219 (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
2220 (global-set-key [mode-line mouse-3] 'mouse-delete-window)
2221 (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
2222 (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
2223 (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
2224 (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
2225 (global-set-key [vertical-line mouse-1] 'mouse-select-window)
2226
2227 (provide 'mouse)
2228
2229 ;; This file contains the functionality of the old mldrag.el.
2230 (defalias 'mldrag-drag-mode-line 'mouse-drag-mode-line)
2231 (defalias 'mldrag-drag-vertical-line 'mouse-drag-vertical-line)
2232 (make-obsolete 'mldrag-drag-mode-line 'mouse-drag-mode-line "21.1")
2233 (make-obsolete 'mldrag-drag-vertical-line 'mouse-drag-vertical-line "21.1")
2234 (provide 'mldrag)
2235
2236 ;;; mouse.el ends here