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