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