Commit | Line | Data |
---|---|---|
e1894109 | 1 | ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*- |
84176303 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1993-1995, 1999-2014 Free Software Foundation, Inc. |
eea8d4ef | 4 | |
84176303 | 5 | ;; Maintainer: FSF |
de420e82 | 6 | ;; Keywords: hardware, mouse |
bd78fa1d | 7 | ;; Package: emacs |
84176303 | 8 | |
be010748 | 9 | ;; This file is part of GNU Emacs. |
72ea54a4 | 10 | |
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
be010748 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) 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 |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
72ea54a4 | 23 | |
edbd2f74 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; This package provides various useful commands (including help | |
27 | ;; system access) through the mouse. All this code assumes that mouse | |
28 | ;; interpretation has been abstracted into Emacs input events. | |
29 | ;; | |
30 | ;; The code is rather X-dependent. | |
31 | ||
aae56ea7 ER |
32 | ;;; Code: |
33 | ||
cc0a8174 | 34 | ;;; Utility functions. |
72ea54a4 | 35 | |
ad0d18af | 36 | ;; Indent track-mouse like progn. |
cc0a8174 | 37 | (put 'track-mouse 'lisp-indent-function 0) |
72ea54a4 | 38 | |
3b558d41 | 39 | (defcustom mouse-yank-at-point nil |
9201cc28 | 40 | "If non-nil, mouse yank commands yank at point instead of at click." |
3b558d41 RS |
41 | :type 'boolean |
42 | :group 'mouse) | |
b2dae92a | 43 | |
f9d71b42 | 44 | (defcustom mouse-drag-copy-region nil |
93d68d4f DDLHG |
45 | "If non-nil, copy to kill-ring upon mouse adjustments of the region. |
46 | ||
47 | This affects `mouse-save-then-kill' (\\[mouse-save-then-kill]) in | |
48 | addition to mouse drags." | |
b2dae92a | 49 | :type 'boolean |
2490cbbc | 50 | :version "24.1" |
b2dae92a KS |
51 | :group 'mouse) |
52 | ||
5dbda518 | 53 | (defcustom mouse-1-click-follows-link 450 |
787e24c3 | 54 | "Non-nil means that clicking Mouse-1 on a link follows the link. |
65f76581 | 55 | |
787e24c3 KS |
56 | With the default setting, an ordinary Mouse-1 click on a link |
57 | performs the same action as Mouse-2 on that link, while a longer | |
97b1270c | 58 | Mouse-1 click \(hold down the Mouse-1 button for more than 450 |
787e24c3 | 59 | milliseconds) performs the original Mouse-1 binding \(which |
65f76581 KS |
60 | typically sets point where you click the mouse). |
61 | ||
62 | If value is an integer, the time elapsed between pressing and | |
63 | releasing the mouse button determines whether to follow the link | |
787e24c3 | 64 | or perform the normal Mouse-1 action (typically set point). |
22bcf204 | 65 | The absolute numeric value specifies the maximum duration of a |
65f76581 KS |
66 | \"short click\" in milliseconds. A positive value means that a |
67 | short click follows the link, and a longer click performs the | |
1d68acd3 | 68 | normal action. A negative value gives the opposite behavior. |
65f76581 KS |
69 | |
70 | If value is `double', a double click follows the link. | |
71 | ||
787e24c3 | 72 | Otherwise, a single Mouse-1 click unconditionally follows the link. |
65f76581 KS |
73 | |
74 | Note that dragging the mouse never follows the link. | |
75 | ||
76 | This feature only works in modes that specifically identify | |
77 | clickable text as links, so it may not work with some external | |
78 | packages. See `mouse-on-link-p' for details." | |
bf247b6e | 79 | :version "22.1" |
65f76581 KS |
80 | :type '(choice (const :tag "Disabled" nil) |
81 | (const :tag "Double click" double) | |
97b1270c | 82 | (number :tag "Single click time limit" :value 450) |
65f76581 KS |
83 | (other :tag "Single click" t)) |
84 | :group 'mouse) | |
85 | ||
185a53bb | 86 | (defcustom mouse-1-click-in-non-selected-windows t |
9201cc28 | 87 | "If non-nil, a Mouse-1 click also follows links in non-selected windows. |
185a53bb KS |
88 | |
89 | If nil, a Mouse-1 click on a link in a non-selected window performs | |
90 | the normal mouse-1 binding, typically selects the window and sets | |
91 | point at the click position." | |
92 | :type 'boolean | |
93 | :version "22.1" | |
94 | :group 'mouse) | |
95 | ||
27a98a62 SM |
96 | (defun mouse--down-1-maybe-follows-link (&optional _prompt) |
97 | "Turn `mouse-1' events into `mouse-2' events if follows-link. | |
98 | Expects to be bound to `down-mouse-1' in `key-translation-map'." | |
99 | (if (or (null mouse-1-click-follows-link) | |
100 | (not (eq (if (eq mouse-1-click-follows-link 'double) | |
101 | 'double-down-mouse-1 'down-mouse-1) | |
102 | (car-safe last-input-event))) | |
103 | (not (mouse-on-link-p (event-start last-input-event))) | |
104 | (and (not mouse-1-click-in-non-selected-windows) | |
105 | (not (eq (selected-window) | |
106 | (posn-window (event-start last-input-event)))))) | |
107 | nil | |
108 | (let ((this-event last-input-event) | |
109 | (timedout | |
110 | (sit-for (if (numberp mouse-1-click-follows-link) | |
111 | (/ (abs mouse-1-click-follows-link) 1000.0) | |
112 | 0)))) | |
113 | (if (if (and (numberp mouse-1-click-follows-link) | |
114 | (>= mouse-1-click-follows-link 0)) | |
115 | timedout (not timedout)) | |
116 | nil | |
117 | ||
118 | (let ((event (read-event))) | |
119 | (if (eq (car-safe event) (if (eq mouse-1-click-follows-link 'double) | |
120 | 'double-mouse-1 'mouse-1)) | |
121 | ;; Turn the mouse-1 into a mouse-2 to follow links. | |
122 | (let ((newup (if (eq mouse-1-click-follows-link 'double) | |
123 | 'double-mouse-2 'mouse-2)) | |
124 | (newdown (if (eq mouse-1-click-follows-link 'double) | |
125 | 'double-down-mouse-2 'down-mouse-2))) | |
126 | ;; If mouse-2 has never been done by the user, it doesn't have | |
127 | ;; the necessary property to be interpreted correctly. | |
128 | (put newup 'event-kind (get (car event) 'event-kind)) | |
129 | (put newdown 'event-kind (get (car this-event) 'event-kind)) | |
130 | (push (cons newup (cdr event)) unread-command-events) | |
0e6008c5 SM |
131 | ;; Modify the event in place, so read-key-sequence doesn't |
132 | ;; generate a second fake prefix key (see fake_prefixed_keys in | |
133 | ;; src/keyboard.c). | |
134 | (setcar this-event newdown) | |
135 | (vector this-event)) | |
27a98a62 SM |
136 | (push event unread-command-events) |
137 | nil)))))) | |
138 | ||
139 | (define-key key-translation-map [down-mouse-1] | |
140 | #'mouse--down-1-maybe-follows-link) | |
141 | (define-key key-translation-map [double-down-mouse-1] | |
142 | #'mouse--down-1-maybe-follows-link) | |
185a53bb | 143 | |
cc0a8174 | 144 | \f |
95132d1c RS |
145 | ;; Provide a mode-specific menu on a mouse button. |
146 | ||
7e4e9c66 | 147 | (defun minor-mode-menu-from-indicator (indicator) |
fb1a03c9 DN |
148 | "Show menu for minor mode specified by INDICATOR. |
149 | Interactively, INDICATOR is read using completion. | |
150 | If there is no menu defined for the minor mode, then create one with | |
151 | items `Turn Off' and `Help'." | |
152 | (interactive | |
5e38cdab | 153 | (list (completing-read |
fb1a03c9 DN |
154 | "Minor mode indicator: " |
155 | (describe-minor-mode-completion-table-for-indicator)))) | |
97b952b7 SM |
156 | (let* ((minor-mode (lookup-minor-mode-from-indicator indicator)) |
157 | (mm-fun (or (get minor-mode :minor-mode-function) minor-mode))) | |
fb1a03c9 DN |
158 | (unless minor-mode (error "Cannot find minor mode for `%s'" indicator)) |
159 | (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist))) | |
160 | (menu (and (keymapp map) (lookup-key map [menu-bar])))) | |
ad0d18af SM |
161 | (setq menu |
162 | (if menu | |
163 | (mouse-menu-non-singleton menu) | |
fb1a03c9 | 164 | `(keymap |
ad0d18af | 165 | ,indicator |
97b952b7 | 166 | (turn-off menu-item "Turn Off minor mode" ,mm-fun) |
ad0d18af | 167 | (help menu-item "Help for minor mode" |
5e38cdab | 168 | (lambda () (interactive) |
97b952b7 | 169 | (describe-function ',mm-fun)))))) |
fb1a03c9 | 170 | (popup-menu menu)))) |
7e4e9c66 GM |
171 | |
172 | (defun mouse-minor-mode-menu (event) | |
173 | "Show minor-mode menu for EVENT on minor modes area of the mode line." | |
174 | (interactive "@e") | |
175 | (let ((indicator (car (nth 4 (car (cdr event)))))) | |
176 | (minor-mode-menu-from-indicator indicator))) | |
177 | ||
0b2b62ff | 178 | (defun mouse-menu-major-mode-map () |
d5e6e0b6 | 179 | (run-hooks 'activate-menubar-hook 'menu-bar-update-hook) |
ad0d18af SM |
180 | (let* (;; Keymap from which to inherit; may be null. |
181 | (ancestor (mouse-menu-non-singleton | |
b98e5762 | 182 | (and (current-local-map) |
25db2767 | 183 | (local-key-binding [menu-bar])))) |
b98e5762 DL |
184 | ;; Make a keymap in which our last command leads to a menu or |
185 | ;; default to the edit menu. | |
186 | (newmap (if ancestor | |
48d33090 SM |
187 | (make-sparse-keymap (concat (format-mode-line mode-name) |
188 | " Mode")) | |
4d6769e1 | 189 | menu-bar-edit-menu))) |
b98e5762 | 190 | (if ancestor |
00f7c5ed | 191 | (set-keymap-parent newmap ancestor)) |
0b2b62ff | 192 | newmap)) |
95132d1c | 193 | |
ad0d18af | 194 | (defun mouse-menu-non-singleton (menubar) |
d1f14baa CY |
195 | "Return menu keybar MENUBAR, or a lone submenu inside it. |
196 | If MENUBAR defines exactly one submenu, return just that submenu. | |
197 | Otherwise, return MENUBAR." | |
95132d1c | 198 | (if menubar |
ad0d18af SM |
199 | (let (submap) |
200 | (map-keymap | |
201 | (lambda (k v) (setq submap (if submap t (cons k v)))) | |
00f7c5ed | 202 | (keymap-canonicalize menubar)) |
ad0d18af SM |
203 | (if (eq submap t) |
204 | menubar | |
205 | (lookup-key menubar (vector (car submap))))))) | |
de420e82 | 206 | |
0b2b62ff SM |
207 | (defun mouse-menu-bar-map () |
208 | "Return a keymap equivalent to the menu bar. | |
de420e82 DL |
209 | The contents are the items that would be in the menu bar whether or |
210 | not it is actually displayed." | |
d5e6e0b6 | 211 | (run-hooks 'activate-menubar-hook 'menu-bar-update-hook) |
7636d2a3 EZ |
212 | (let* ((local-menu (and (current-local-map) |
213 | (lookup-key (current-local-map) [menu-bar]))) | |
214 | (global-menu (lookup-key global-map [menu-bar])) | |
59836110 EZ |
215 | ;; If a keymap doesn't have a prompt string (a lazy |
216 | ;; programmer didn't bother to provide one), create it and | |
217 | ;; insert it into the keymap; each keymap gets its own | |
218 | ;; prompt. This is required for non-toolkit versions to | |
219 | ;; display non-empty menu pane names. | |
220 | (minor-mode-menus | |
221 | (mapcar | |
00f7c5ed SM |
222 | (lambda (menu) |
223 | (let* ((minor-mode (car menu)) | |
224 | (menu (cdr menu)) | |
225 | (title-or-map (cadr menu))) | |
226 | (or (stringp title-or-map) | |
227 | (setq menu | |
228 | (cons 'keymap | |
229 | (cons (concat | |
230 | (capitalize (subst-char-in-string | |
231 | ?- ?\s (symbol-name | |
232 | minor-mode))) | |
233 | " Menu") | |
234 | (cdr menu))))) | |
235 | menu)) | |
59836110 | 236 | (minor-mode-key-binding [menu-bar]))) |
7636d2a3 EZ |
237 | (local-title-or-map (and local-menu (cadr local-menu))) |
238 | (global-title-or-map (cadr global-menu))) | |
7636d2a3 EZ |
239 | (or (null local-menu) |
240 | (stringp local-title-or-map) | |
241 | (setq local-menu (cons 'keymap | |
b38f5e6f | 242 | (cons (concat (format-mode-line mode-name) |
48d33090 | 243 | " Mode Menu") |
7636d2a3 EZ |
244 | (cdr local-menu))))) |
245 | (or (stringp global-title-or-map) | |
246 | (setq global-menu (cons 'keymap | |
247 | (cons "Global Menu" | |
248 | (cdr global-menu))))) | |
de420e82 | 249 | ;; Supplying the list is faster than making a new map. |
0b2b62ff SM |
250 | ;; FIXME: We have a problem here: we have to use the global/local/minor |
251 | ;; so they're displayed in the expected order, but later on in the command | |
252 | ;; loop, they're actually looked up in the opposite order. | |
253 | (apply 'append | |
254 | global-menu | |
255 | local-menu | |
256 | minor-mode-menus))) | |
257 | ||
258 | (defun mouse-major-mode-menu (event &optional prefix) | |
259 | "Pop up a mode-specific menu of mouse commands. | |
260 | Default to the Edit menu if the major mode doesn't define a menu." | |
59f7af81 | 261 | (declare (obsolete mouse-menu-major-mode-map "23.1")) |
0b2b62ff SM |
262 | (interactive "@e\nP") |
263 | (run-hooks 'activate-menubar-hook 'menu-bar-update-hook) | |
264 | (popup-menu (mouse-menu-major-mode-map) event prefix)) | |
0b2b62ff SM |
265 | |
266 | (defun mouse-popup-menubar (event prefix) | |
267 | "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX. | |
268 | The contents are the items that would be in the menu bar whether or | |
269 | not it is actually displayed." | |
59f7af81 | 270 | (declare (obsolete mouse-menu-bar-map "23.1")) |
0b2b62ff SM |
271 | (interactive "@e \nP") |
272 | (run-hooks 'activate-menubar-hook 'menu-bar-update-hook) | |
fd6fa53f | 273 | (popup-menu (mouse-menu-bar-map) (unless (integerp event) event) prefix)) |
de420e82 DL |
274 | |
275 | (defun mouse-popup-menubar-stuff (event prefix) | |
276 | "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'. | |
277 | Use the former if the menu bar is showing, otherwise the latter." | |
59f7af81 | 278 | (declare (obsolete nil "23.1")) |
0b2b62ff SM |
279 | (interactive "@e\nP") |
280 | (run-hooks 'activate-menubar-hook 'menu-bar-update-hook) | |
281 | (popup-menu | |
282 | (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0)) | |
283 | (mouse-menu-bar-map) | |
284 | (mouse-menu-major-mode-map)) | |
285 | event prefix)) | |
95132d1c | 286 | \f |
544e7e73 RS |
287 | ;; Commands that operate on windows. |
288 | ||
d65147f6 KH |
289 | (defun mouse-minibuffer-check (event) |
290 | (let ((w (posn-window (event-start event)))) | |
291 | (and (window-minibuffer-p w) | |
292 | (not (minibuffer-window-active-p w)) | |
71873e2b | 293 | (user-error "Minibuffer window is not active"))) |
33c448cd RS |
294 | ;; Give temporary modes such as isearch a chance to turn off. |
295 | (run-hooks 'mouse-leave-buffer-hook)) | |
d65147f6 | 296 | |
cc0a8174 | 297 | (defun mouse-delete-window (click) |
947da0c4 | 298 | "Delete the window you click on. |
6b48d742 | 299 | Do nothing if the frame has just one window. |
a926a0fa | 300 | This command must be bound to a mouse click." |
ec558adc | 301 | (interactive "e") |
6b48d742 | 302 | (unless (one-window-p t) |
a926a0fa RS |
303 | (mouse-minibuffer-check click) |
304 | (delete-window (posn-window (event-start click))))) | |
cc0a8174 | 305 | |
3c2dd2c0 RS |
306 | (defun mouse-select-window (click) |
307 | "Select the window clicked on; don't move point." | |
308 | (interactive "e") | |
d65147f6 | 309 | (mouse-minibuffer-check click) |
3c2dd2c0 RS |
310 | (let ((oframe (selected-frame)) |
311 | (frame (window-frame (posn-window (event-start click))))) | |
312 | (select-window (posn-window (event-start click))) | |
313 | (raise-frame frame) | |
314 | (select-frame frame) | |
315 | (or (eq frame oframe) | |
e1877477 | 316 | (set-mouse-position (selected-frame) (1- (frame-width)) 0)))) |
3c2dd2c0 | 317 | |
b0f3a26b JB |
318 | (defun mouse-tear-off-window (click) |
319 | "Delete the window clicked on, and create a new frame displaying its buffer." | |
320 | (interactive "e") | |
d65147f6 | 321 | (mouse-minibuffer-check click) |
b0f3a26b JB |
322 | (let* ((window (posn-window (event-start click))) |
323 | (buf (window-buffer window)) | |
01a911e3 | 324 | (frame (make-frame))) |
b0f3a26b JB |
325 | (select-frame frame) |
326 | (switch-to-buffer buf) | |
327 | (delete-window window))) | |
328 | ||
b5370f03 | 329 | (defun mouse-delete-other-windows () |
5925bb84 | 330 | "Delete all windows except the one you click on." |
b5370f03 | 331 | (interactive "@") |
cc0a8174 | 332 | (delete-other-windows)) |
72ea54a4 | 333 | |
cc0a8174 JB |
334 | (defun mouse-split-window-vertically (click) |
335 | "Select Emacs window mouse is on, then split it vertically in half. | |
336 | The window is split at the line clicked on. | |
337 | This command must be bound to a mouse click." | |
947da0c4 | 338 | (interactive "@e") |
d65147f6 | 339 | (mouse-minibuffer-check click) |
b5370f03 JB |
340 | (let ((start (event-start click))) |
341 | (select-window (posn-window start)) | |
85d6b80b | 342 | (let ((new-height (1+ (cdr (posn-col-row (event-end click))))) |
5ba2dc3f JB |
343 | (first-line window-min-height) |
344 | (last-line (- (window-height) window-min-height))) | |
345 | (if (< last-line first-line) | |
0a50b993 | 346 | (error "Window too short to split") |
5ba2dc3f JB |
347 | (split-window-vertically |
348 | (min (max new-height first-line) last-line)))))) | |
cc0a8174 | 349 | |
947da0c4 RS |
350 | (defun mouse-split-window-horizontally (click) |
351 | "Select Emacs window mouse is on, then split it horizontally in half. | |
352 | The window is split at the column clicked on. | |
353 | This command must be bound to a mouse click." | |
354 | (interactive "@e") | |
d65147f6 | 355 | (mouse-minibuffer-check click) |
5ba2dc3f JB |
356 | (let ((start (event-start click))) |
357 | (select-window (posn-window start)) | |
358 | (let ((new-width (1+ (car (posn-col-row (event-end click))))) | |
359 | (first-col window-min-width) | |
360 | (last-col (- (window-width) window-min-width))) | |
361 | (if (< last-col first-col) | |
0a50b993 | 362 | (error "Window too narrow to split") |
5ba2dc3f JB |
363 | (split-window-horizontally |
364 | (min (max new-width first-col) last-col)))))) | |
947da0c4 | 365 | |
e07b9a6d MR |
366 | ;; `mouse-drag-line' is now the common routine for handling all line |
367 | ;; dragging events combining the earlier `mouse-drag-mode-line-1' and | |
368 | ;; `mouse-drag-vertical-line'. It should improve the behavior of line | |
369 | ;; dragging wrt Emacs 23 as follows: | |
370 | ||
371 | ;; (1) Gratuitous error messages and restrictions have been (hopefully) | |
372 | ;; removed. (The help-echo that dragging the mode-line can resize a | |
373 | ;; one-window-frame's window will still show through via bindings.el.) | |
374 | ||
375 | ;; (2) No gratuitous selection of other windows should happen. (This | |
376 | ;; has not been completely fixed for mouse-autoselected windows yet.) | |
377 | ||
378 | ;; (3) Mouse clicks below a scroll-bar should pass through via unread | |
379 | ;; command events. | |
380 | ||
381 | ;; Note that `window-in-direction' replaces `mouse-drag-window-above' | |
382 | ;; and `mouse-drag-vertical-line-rightward-window' with Emacs 24.1. | |
01ac65bd | 383 | |
e07b9a6d | 384 | (defun mouse-drag-line (start-event line) |
01ac65bd | 385 | "Drag a mode line, header line, or vertical line with the mouse. |
e07b9a6d | 386 | START-EVENT is the starting mouse-event of the drag action. LINE |
01ac65bd | 387 | must be one of the symbols `header', `mode', or `vertical'." |
33c448cd RS |
388 | ;; Give temporary modes such as isearch a chance to turn off. |
389 | (run-hooks 'mouse-leave-buffer-hook) | |
e07b9a6d | 390 | (let* ((echo-keystrokes 0) |
ee0e2bdd | 391 | (start (event-start start-event)) |
e07b9a6d MR |
392 | (window (posn-window start)) |
393 | (frame (window-frame window)) | |
394 | (minibuffer-window (minibuffer-window frame)) | |
880e6158 MR |
395 | (on-link (and mouse-1-click-follows-link |
396 | (mouse-on-link-p start))) | |
01ac65bd CY |
397 | (side (and (eq line 'vertical) |
398 | (or (cdr (assq 'vertical-scroll-bars | |
399 | (frame-parameters frame))) | |
400 | 'right))) | |
401 | (draggable t) | |
880e6158 | 402 | height finished event position growth dragged) |
e07b9a6d MR |
403 | (cond |
404 | ((eq line 'header) | |
405 | ;; Check whether header-line can be dragged at all. | |
53ebff1f | 406 | (if (window-at-side-p window 'top) |
01ac65bd | 407 | (setq draggable nil) |
880e6158 | 408 | (setq height (/ (window-header-line-height window) 2)) |
53ebff1f | 409 | (setq window (window-in-direction 'above window t)))) |
e07b9a6d MR |
410 | ((eq line 'mode) |
411 | ;; Check whether mode-line can be dragged at all. | |
880e6158 MR |
412 | (if (and (window-at-side-p window 'bottom) |
413 | ;; Allow resizing the minibuffer window if it's on the same | |
414 | ;; frame as and immediately below the clicked window, and | |
415 | ;; it's active or `resize-mini-windows' is nil. | |
416 | (not (and (eq (window-frame minibuffer-window) frame) | |
417 | (= (nth 1 (window-pixel-edges minibuffer-window)) | |
418 | (nth 3 (window-pixel-edges window))) | |
419 | (or (not resize-mini-windows) | |
420 | (eq minibuffer-window | |
421 | (active-minibuffer-window)))))) | |
422 | (setq draggable nil) | |
423 | (setq height (/ (window-mode-line-height window) 2)))) | |
e07b9a6d | 424 | ((eq line 'vertical) |
880e6158 MR |
425 | ;; Get the window to adjust for the vertical case. If the scroll |
426 | ;; bar is on the window's right or we drag a vertical divider, | |
427 | ;; adjust the window where the start-event occurred. If the | |
428 | ;; scroll bar is on the start-event window's left or there are no | |
429 | ;; scrollbars, adjust the window on the left of it. | |
430 | (unless (or (eq side 'right) | |
431 | (not (zerop (window-right-divider-width window)))) | |
01ac65bd | 432 | (setq window (window-in-direction 'left window t))))) |
e07b9a6d MR |
433 | |
434 | ;; Start tracking. | |
544e7e73 | 435 | (track-mouse |
880e6158 MR |
436 | ;; Loop reading events and sampling the position of the mouse. |
437 | (while (not finished) | |
438 | (setq event (read-event)) | |
439 | (setq position (mouse-pixel-position)) | |
c8e5a42c MR |
440 | ;; Do nothing if |
441 | ;; - there is a switch-frame event. | |
442 | ;; - the mouse isn't in the frame that we started in | |
443 | ;; - the mouse isn't in any Emacs frame | |
880e6158 MR |
444 | ;; Drag if |
445 | ;; - there is a mouse-movement event | |
446 | ;; - there is a scroll-bar-movement event (Why? -- cyd) | |
447 | ;; (same as mouse movement for our purposes) | |
448 | ;; Quit if | |
449 | ;; - there is a keyboard event or some other unknown event. | |
e07b9a6d | 450 | (cond |
880e6158 MR |
451 | ((not (consp event)) |
452 | (setq finished t)) | |
c8e5a42c MR |
453 | ((memq (car event) '(switch-frame select-window)) |
454 | nil) | |
880e6158 MR |
455 | ((not (memq (car event) '(mouse-movement scroll-bar-movement))) |
456 | (when (consp event) | |
457 | ;; Do not unread a drag-mouse-1 event to avoid selecting | |
458 | ;; some other window. For vertical line dragging do not | |
459 | ;; unread mouse-1 events either (but only if we dragged at | |
460 | ;; least once to allow mouse-1 clicks get through). | |
461 | (unless (and dragged | |
462 | (if (eq line 'vertical) | |
463 | (memq (car event) '(drag-mouse-1 mouse-1)) | |
464 | (eq (car event) 'drag-mouse-1))) | |
465 | (push event unread-command-events))) | |
466 | (setq finished t)) | |
467 | ((not (and (eq (car position) frame) | |
468 | (cadr position))) | |
e07b9a6d MR |
469 | nil) |
470 | ((eq line 'vertical) | |
880e6158 MR |
471 | ;; Drag vertical divider. This must be probably fixed like |
472 | ;; for the mode-line. | |
01ac65bd CY |
473 | (setq growth (- (cadr position) |
474 | (if (eq side 'right) 0 2) | |
880e6158 | 475 | (nth 2 (window-pixel-edges window)) |
01ac65bd | 476 | -1)) |
e07b9a6d | 477 | (unless (zerop growth) |
df183459 MR |
478 | (unless window-resize-pixelwise |
479 | (setq growth (min (/ growth (frame-char-width frame)) 1))) | |
880e6158 | 480 | (setq dragged t) |
df183459 MR |
481 | (adjust-window-trailing-edge |
482 | window growth t window-resize-pixelwise))) | |
01ac65bd CY |
483 | (draggable |
484 | ;; Drag horizontal divider. | |
e07b9a6d MR |
485 | (setq growth |
486 | (if (eq line 'mode) | |
880e6158 MR |
487 | (- (+ (cddr position) height) |
488 | (nth 3 (window-pixel-edges window))) | |
e07b9a6d | 489 | ;; The window's top includes the header line! |
880e6158 MR |
490 | (- (+ (nth 3 (window-pixel-edges window)) height) |
491 | (cddr position)))) | |
e07b9a6d | 492 | (unless (zerop growth) |
df183459 MR |
493 | (unless window-resize-pixelwise |
494 | (setq growth (min (/ growth (frame-char-height frame)) 1))) | |
880e6158 MR |
495 | (setq dragged t) |
496 | (adjust-window-trailing-edge | |
df183459 MR |
497 | window (if (eq line 'mode) growth (- growth)) nil |
498 | window-resize-pixelwise)))))) | |
01ac65bd | 499 | ;; Process the terminating event. |
880e6158 MR |
500 | (when (and (mouse-event-p event) on-link (not dragged) |
501 | (mouse--remap-link-click-p start-event event)) | |
502 | ;; If mouse-2 has never been done by the user, it doesn't have | |
503 | ;; the necessary property to be interpreted correctly. | |
504 | (put 'mouse-2 'event-kind 'mouse-click) | |
505 | (setcar event 'mouse-2) | |
8c7727c3 | 506 | (push event unread-command-events)))) |
e07b9a6d | 507 | |
b0d22e20 GM |
508 | (defun mouse-drag-mode-line (start-event) |
509 | "Change the height of a window by dragging on the mode line." | |
510 | (interactive "e") | |
e07b9a6d | 511 | (mouse-drag-line start-event 'mode)) |
b0d22e20 GM |
512 | |
513 | (defun mouse-drag-header-line (start-event) | |
e07b9a6d | 514 | "Change the height of a window by dragging on the header line." |
b0d22e20 | 515 | (interactive "e") |
e07b9a6d | 516 | (mouse-drag-line start-event 'header)) |
73c8f64c | 517 | |
08a1c178 RS |
518 | (defun mouse-drag-vertical-line (start-event) |
519 | "Change the width of a window by dragging on the vertical line." | |
520 | (interactive "e") | |
e07b9a6d | 521 | (mouse-drag-line start-event 'vertical)) |
08a1c178 | 522 | \f |
2a5fa27b | 523 | (defun mouse-set-point (event) |
cc0a8174 | 524 | "Move point to the position clicked on with the mouse. |
2a5fa27b | 525 | This should be bound to a mouse click event type." |
ec558adc | 526 | (interactive "e") |
d65147f6 | 527 | (mouse-minibuffer-check event) |
2a5fa27b RS |
528 | ;; Use event-end in case called from mouse-drag-region. |
529 | ;; If EVENT is a click, event-end and event-start give same value. | |
541a44d2 | 530 | (posn-set-point (event-end event))) |
cc0a8174 | 531 | |
e6c2f5d4 RS |
532 | (defvar mouse-last-region-beg nil) |
533 | (defvar mouse-last-region-end nil) | |
534 | (defvar mouse-last-region-tick nil) | |
535 | ||
536 | (defun mouse-region-match () | |
537 | "Return non-nil if there's an active region that was set with the mouse." | |
538 | (and (mark t) mark-active | |
539 | (eq mouse-last-region-beg (region-beginning)) | |
540 | (eq mouse-last-region-end (region-end)) | |
541 | (eq mouse-last-region-tick (buffer-modified-tick)))) | |
542 | ||
652ccd35 | 543 | (defun mouse-set-region (click) |
e37de120 | 544 | "Set the region to the text dragged over, and copy to kill ring. |
43f5740b LMI |
545 | This should be bound to a mouse drag event. |
546 | See the `mouse-drag-copy-region' variable to control whether this | |
547 | command alters the kill ring or not." | |
652ccd35 | 548 | (interactive "e") |
d65147f6 | 549 | (mouse-minibuffer-check click) |
f9be2e35 CY |
550 | (select-window (posn-window (event-start click))) |
551 | (let ((beg (posn-point (event-start click))) | |
552 | (end (posn-point (event-end click)))) | |
553 | (and mouse-drag-copy-region (integerp beg) (integerp end) | |
554 | ;; Don't set this-command to `kill-region', so a following | |
555 | ;; C-w won't double the text in the kill ring. Ignore | |
556 | ;; `last-command' so we don't append to a preceding kill. | |
557 | (let (this-command last-command deactivate-mark) | |
558 | (copy-region-as-kill beg end))) | |
559 | (if (numberp beg) (goto-char beg)) | |
560 | ;; On a text terminal, bounce the cursor. | |
33a35434 | 561 | (or transient-mark-mode |
f9be2e35 | 562 | (window-system) |
fcfc3c63 | 563 | (sit-for 1)) |
652ccd35 | 564 | (push-mark) |
1cc8a3f4 | 565 | (set-mark (point)) |
f9be2e35 | 566 | (if (numberp end) (goto-char end)) |
e6c2f5d4 RS |
567 | (mouse-set-region-1))) |
568 | ||
569 | (defun mouse-set-region-1 () | |
a5809dbd | 570 | ;; Set transient-mark-mode for a little while. |
688468a0 CY |
571 | (unless (eq (car-safe transient-mark-mode) 'only) |
572 | (setq transient-mark-mode | |
573 | (cons 'only | |
574 | (unless (eq transient-mark-mode 'lambda) | |
575 | transient-mark-mode)))) | |
e6c2f5d4 RS |
576 | (setq mouse-last-region-beg (region-beginning)) |
577 | (setq mouse-last-region-end (region-end)) | |
578 | (setq mouse-last-region-tick (buffer-modified-tick))) | |
652ccd35 | 579 | |
3b558d41 | 580 | (defcustom mouse-scroll-delay 0.25 |
9201cc28 | 581 | "The pause between scroll steps caused by mouse drags, in seconds. |
600c6e3a JB |
582 | If you drag the mouse beyond the edge of a window, Emacs scrolls the |
583 | window to bring the text beyond that edge into view, with a delay of | |
584 | this many seconds between scroll steps. Scrolling stops when you move | |
585 | the mouse back into the window, or release the button. | |
586 | This variable's value may be non-integral. | |
3b558d41 RS |
587 | Setting this to zero causes Emacs to scroll as fast as it can." |
588 | :type 'number | |
589 | :group 'mouse) | |
600c6e3a | 590 | |
3b558d41 | 591 | (defcustom mouse-scroll-min-lines 1 |
9201cc28 | 592 | "The minimum number of lines scrolled by dragging mouse out of window. |
a3d6bb97 RS |
593 | Moving the mouse out the top or bottom edge of the window begins |
594 | scrolling repeatedly. The number of lines scrolled per repetition | |
595 | is normally equal to the number of lines beyond the window edge that | |
596 | the mouse has moved. However, it always scrolls at least the number | |
3b558d41 RS |
597 | of lines specified by this variable." |
598 | :type 'integer | |
599 | :group 'mouse) | |
08a1c178 | 600 | |
e919a622 RS |
601 | (defun mouse-scroll-subr (window jump &optional overlay start) |
602 | "Scroll the window WINDOW, JUMP lines at a time, until new input arrives. | |
600c6e3a JB |
603 | If OVERLAY is an overlay, let it stretch from START to the far edge of |
604 | the newly visible text. | |
605 | Upon exit, point is at the far edge of the newly visible text." | |
08a1c178 RS |
606 | (cond |
607 | ((and (> jump 0) (< jump mouse-scroll-min-lines)) | |
608 | (setq jump mouse-scroll-min-lines)) | |
609 | ((and (< jump 0) (< (- jump) mouse-scroll-min-lines)) | |
610 | (setq jump (- mouse-scroll-min-lines)))) | |
4e399a53 RS |
611 | (let ((opoint (point))) |
612 | (while (progn | |
613 | (goto-char (window-start window)) | |
614 | (if (not (zerop (vertical-motion jump window))) | |
615 | (progn | |
616 | (set-window-start window (point)) | |
617 | (if (natnump jump) | |
0320e66f RS |
618 | (if (window-end window) |
619 | (progn | |
620 | (goto-char (window-end window)) | |
621 | ;; window-end doesn't reflect the window's new | |
622 | ;; start position until the next redisplay. | |
623 | (vertical-motion (1- jump) window)) | |
624 | (vertical-motion (- (window-height window) 2))) | |
4e399a53 RS |
625 | (goto-char (window-start window))) |
626 | (if overlay | |
627 | (move-overlay overlay start (point))) | |
628 | ;; Now that we have scrolled WINDOW properly, | |
629 | ;; put point back where it was for the redisplay | |
630 | ;; so that we don't mess up the selected window. | |
631 | (or (eq window (selected-window)) | |
632 | (goto-char opoint)) | |
d2287ded | 633 | (sit-for mouse-scroll-delay))))) |
4e399a53 RS |
634 | (or (eq window (selected-window)) |
635 | (goto-char opoint)))) | |
fcfc3c63 | 636 | |
dd524dbd | 637 | (defvar mouse-selection-click-count 0) |
eb6ff46f | 638 | |
c8c5bd24 RS |
639 | (defvar mouse-selection-click-count-buffer nil) |
640 | ||
600c6e3a | 641 | (defun mouse-drag-region (start-event) |
bcd5aef1 | 642 | "Set the region to the text that the mouse is dragged over. |
78210c95 RS |
643 | Highlight the drag area as you move the mouse. |
644 | This must be bound to a button-down mouse event. | |
d017deb2 | 645 | In Transient Mark mode, the highlighting remains as long as the mark |
6454615c RS |
646 | remains active. Otherwise, it remains until the next input event. |
647 | ||
648 | If the click is in the echo area, display the `*Messages*' buffer." | |
bcd5aef1 | 649 | (interactive "e") |
fd6fa53f SM |
650 | ;; Give temporary modes such as isearch a chance to turn off. |
651 | (run-hooks 'mouse-leave-buffer-hook) | |
652 | (mouse-drag-track start-event t)) | |
6454615c | 653 | |
65f76581 | 654 | |
91a2acb2 | 655 | (defun mouse-posn-property (pos property) |
9ed38a84 DK |
656 | "Look for a property at click position. |
657 | POS may be either a buffer position or a click position like | |
89bf250e | 658 | those returned from `event-start'. If the click position is on |
9ed38a84 DK |
659 | a string, the text property PROPERTY is examined. |
660 | If this is nil or the click is not on a string, then | |
661 | the corresponding buffer position is searched for PROPERTY. | |
662 | If PROPERTY is encountered in one of those places, | |
663 | its value is returned." | |
91a2acb2 DK |
664 | (if (consp pos) |
665 | (let ((w (posn-window pos)) (pt (posn-point pos)) | |
666 | (str (posn-string pos))) | |
667 | (or (and str | |
668 | (get-text-property (cdr str) property (car str))) | |
669 | (and pt | |
670 | (get-char-property pt property w)))) | |
671 | (get-char-property pos property))) | |
672 | ||
65f76581 KS |
673 | (defun mouse-on-link-p (pos) |
674 | "Return non-nil if POS is on a link in the current buffer. | |
23eb76c4 TTN |
675 | POS must be a buffer position in the current buffer or a mouse |
676 | event location in the selected window (see `event-start'). | |
185a53bb KS |
677 | However, if `mouse-1-click-in-non-selected-windows' is non-nil, |
678 | POS may be a mouse event location in any window. | |
65f76581 KS |
679 | |
680 | A clickable link is identified by one of the following methods: | |
681 | ||
b41a4019 | 682 | - If the character at POS has a non-nil `follow-link' text or |
e5fb57e9 | 683 | overlay property, the value of that property determines what to do. |
65f76581 | 684 | |
b41a4019 KS |
685 | - If there is a local key-binding or a keybinding at position POS |
686 | for the `follow-link' event, the binding of that event determines | |
687 | what to do. | |
65f76581 | 688 | |
b41a4019 KS |
689 | The resulting value determine whether POS is inside a link: |
690 | ||
691 | - If the value is `mouse-face', POS is inside a link if there | |
65f76581 KS |
692 | is a non-nil `mouse-face' property at POS. Return t in this case. |
693 | ||
b41a4019 | 694 | - If the value is a function, FUNC, POS is inside a link if |
65f76581 | 695 | the call \(FUNC POS) returns non-nil. Return the return value |
23eb76c4 | 696 | from that call. Arg is \(posn-point POS) if POS is a mouse event. |
65f76581 | 697 | |
b41a4019 | 698 | - Otherwise, return the value itself. |
65f76581 KS |
699 | |
700 | The return value is interpreted as follows: | |
701 | ||
702 | - If it is a string, the mouse-1 event is translated into the | |
703 | first character of the string, i.e. the action of the mouse-1 | |
704 | click is the local or global binding of that character. | |
705 | ||
706 | - If it is a vector, the mouse-1 event is translated into the | |
707 | first element of that vector, i.e. the action of the mouse-1 | |
708 | click is the local or global binding of that event. | |
709 | ||
710 | - Otherwise, the mouse-1 event is translated into a mouse-2 event | |
711 | at the same position." | |
91a2acb2 | 712 | (let ((action |
9a1ff164 | 713 | (and (or (not (consp pos)) |
91a2acb2 DK |
714 | mouse-1-click-in-non-selected-windows |
715 | (eq (selected-window) (posn-window pos))) | |
716 | (or (mouse-posn-property pos 'follow-link) | |
0e6008c5 SM |
717 | (let ((area (posn-area pos))) |
718 | (when area | |
719 | (key-binding (vector area 'follow-link) nil t pos))) | |
91a2acb2 DK |
720 | (key-binding [follow-link] nil t pos))))) |
721 | (cond | |
722 | ((eq action 'mouse-face) | |
723 | (and (mouse-posn-property pos 'mouse-face) t)) | |
724 | ((functionp action) | |
9ed38a84 | 725 | ;; FIXME: This seems questionable if the click is not in a buffer. |
e9ae308c | 726 | ;; Should we instead decide that `action' takes a `posn'? |
9ed38a84 DK |
727 | (if (consp pos) |
728 | (with-current-buffer (window-buffer (posn-window pos)) | |
624c972d | 729 | (funcall action (posn-point pos))) |
9ed38a84 | 730 | (funcall action pos))) |
91a2acb2 | 731 | (t action)))) |
b0ca1b8a | 732 | |
5dbda518 KS |
733 | (defun mouse-fixup-help-message (msg) |
734 | "Fix help message MSG for `mouse-1-click-follows-link'." | |
735 | (let (mp pos) | |
736 | (if (and mouse-1-click-follows-link | |
737 | (stringp msg) | |
26ee77a6 | 738 | (string-match-p "\\`mouse-2" msg) |
5dbda518 KS |
739 | (setq mp (mouse-pixel-position)) |
740 | (consp (setq pos (cdr mp))) | |
741 | (car pos) (>= (car pos) 0) | |
742 | (cdr pos) (>= (cdr pos) 0) | |
743 | (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp))) | |
744 | (windowp (posn-window pos))) | |
745 | (with-current-buffer (window-buffer (posn-window pos)) | |
746 | (if (mouse-on-link-p pos) | |
747 | (setq msg (concat | |
748 | (cond | |
749 | ((eq mouse-1-click-follows-link 'double) "double-") | |
750 | ((and (integerp mouse-1-click-follows-link) | |
751 | (< mouse-1-click-follows-link 0)) "Long ") | |
752 | (t "")) | |
753 | "mouse-1" (substring msg 7))))))) | |
754 | msg) | |
65f76581 | 755 | |
624c972d | 756 | (defun mouse-drag-track (start-event &optional |
f1c4f757 S |
757 | do-mouse-drag-region-post-process) |
758 | "Track mouse drags by highlighting area between point and cursor. | |
5592c08f CY |
759 | The region will be defined with mark and point. |
760 | DO-MOUSE-DRAG-REGION-POST-PROCESS should only be used by | |
761 | `mouse-drag-region'." | |
d65147f6 | 762 | (mouse-minibuffer-check start-event) |
aaf15b8b | 763 | (setq mouse-selection-click-count-buffer (current-buffer)) |
3f5c9cad | 764 | (deactivate-mark) |
371d6a61 | 765 | (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541). |
aaf15b8b SM |
766 | ;; We've recorded what we needed from the current buffer and |
767 | ;; window, now let's jump to the place of the event, where things | |
768 | ;; are happening. | |
769 | (_ (mouse-set-point start-event)) | |
770 | (echo-keystrokes 0) | |
4c9afcbe | 771 | (start-posn (event-start start-event)) |
600c6e3a JB |
772 | (start-point (posn-point start-posn)) |
773 | (start-window (posn-window start-posn)) | |
02beb936 | 774 | (start-window-start (window-start start-window)) |
1853aa6b | 775 | (start-hscroll (window-hscroll start-window)) |
600c6e3a | 776 | (bounds (window-edges start-window)) |
8413d0d2 | 777 | (make-cursor-line-fully-visible nil) |
600c6e3a JB |
778 | (top (nth 1 bounds)) |
779 | (bottom (if (window-minibuffer-p start-window) | |
780 | (nth 3 bounds) | |
781 | ;; Don't count the mode line. | |
e37de120 | 782 | (1- (nth 3 bounds)))) |
81133808 | 783 | (click-count (1- (event-click-count start-event))) |
a9d01547 CY |
784 | ;; Suppress automatic hscrolling, because that is a nuisance |
785 | ;; when setting point near the right fringe (but see below). | |
41f03f4d GM |
786 | (auto-hscroll-mode-saved auto-hscroll-mode) |
787 | (auto-hscroll-mode nil) | |
8cca9703 | 788 | moved-off-start event end end-point) |
5592c08f | 789 | |
81133808 | 790 | (setq mouse-selection-click-count click-count) |
08a1c178 RS |
791 | ;; In case the down click is in the middle of some intangible text, |
792 | ;; use the end of that text, and put it in START-POINT. | |
793 | (if (< (point) start-point) | |
794 | (goto-char start-point)) | |
795 | (setq start-point (point)) | |
5592c08f | 796 | |
81133808 CY |
797 | ;; Activate the region, using `mouse-start-end' to determine where |
798 | ;; to put point and mark (e.g., double-click will select a word). | |
5592c08f CY |
799 | (setq transient-mark-mode |
800 | (if (eq transient-mark-mode 'lambda) | |
801 | '(only) | |
802 | (cons 'only transient-mark-mode))) | |
9852377f | 803 | (let ((range (mouse-start-end start-point start-point click-count))) |
6d3e82d2 | 804 | (push-mark (nth 0 range) t t) |
81133808 | 805 | (goto-char (nth 1 range))) |
5592c08f CY |
806 | |
807 | ;; Track the mouse until we get a non-movement event. | |
808 | (track-mouse | |
809 | (while (progn | |
810 | (setq event (read-event)) | |
811 | (or (mouse-movement-p event) | |
812 | (memq (car-safe event) '(switch-frame select-window)))) | |
813 | (unless (memq (car-safe event) '(switch-frame select-window)) | |
814 | ;; Automatic hscrolling did not occur during the call to | |
815 | ;; `read-event'; but if the user subsequently drags the | |
816 | ;; mouse, go ahead and hscroll. | |
41f03f4d | 817 | (let ((auto-hscroll-mode auto-hscroll-mode-saved)) |
5592c08f CY |
818 | (redisplay)) |
819 | (setq end (event-end event) | |
820 | end-point (posn-point end)) | |
8cca9703 CY |
821 | ;; Note whether the mouse has left the starting position. |
822 | (unless (eq end-point start-point) | |
823 | (setq moved-off-start t)) | |
5592c08f | 824 | (if (and (eq (posn-window end) start-window) |
b846d039 | 825 | (integer-or-marker-p end-point)) |
6d3e82d2 CY |
826 | (mouse--drag-set-mark-and-point start-point |
827 | end-point click-count) | |
5592c08f CY |
828 | (let ((mouse-row (cdr (cdr (mouse-position))))) |
829 | (cond | |
830 | ((null mouse-row)) | |
831 | ((< mouse-row top) | |
832 | (mouse-scroll-subr start-window (- mouse-row top) | |
833 | nil start-point)) | |
834 | ((>= mouse-row bottom) | |
835 | (mouse-scroll-subr start-window (1+ (- mouse-row bottom)) | |
836 | nil start-point)))))))) | |
837 | ||
838 | ;; Handle the terminating event if possible. | |
839 | (when (consp event) | |
840 | ;; Ensure that point is on the end of the last event. | |
841 | (when (and (setq end-point (posn-point (event-end event))) | |
2e3329a5 | 842 | (eq (posn-window end) start-window) |
5592c08f CY |
843 | (integer-or-marker-p end-point) |
844 | (/= start-point end-point)) | |
6d3e82d2 CY |
845 | (mouse--drag-set-mark-and-point start-point |
846 | end-point click-count)) | |
847 | ||
5592c08f CY |
848 | ;; Find its binding. |
849 | (let* ((fun (key-binding (vector (car event)))) | |
925baaa9 GM |
850 | ;; FIXME This doesn't make sense, because |
851 | ;; event-click-count always returns something >= 1. | |
5592c08f CY |
852 | (do-multi-click (and (> (event-click-count event) 0) |
853 | (functionp fun) | |
854 | (not (memq fun '(mouse-set-point | |
855 | mouse-set-region)))))) | |
856 | (if (and (/= (mark) (point)) | |
857 | (not do-multi-click)) | |
f479ef6e | 858 | |
5592c08f CY |
859 | ;; If point has moved, finish the drag. |
860 | (let* (last-command this-command) | |
5592c08f CY |
861 | (and mouse-drag-copy-region |
862 | do-mouse-drag-region-post-process | |
863 | (let (deactivate-mark) | |
9852377f | 864 | (copy-region-as-kill (mark) (point))))) |
f479ef6e | 865 | |
8cca9703 | 866 | ;; Otherwise, run binding of terminating up-event. |
13841bfc | 867 | (deactivate-mark) |
05e89fea CY |
868 | (if do-multi-click |
869 | (goto-char start-point) | |
05e89fea CY |
870 | (unless moved-off-start |
871 | (pop-mark))) | |
872 | ||
5592c08f CY |
873 | (when (and (functionp fun) |
874 | (= start-hscroll (window-hscroll start-window)) | |
875 | ;; Don't run the up-event handler if the window | |
876 | ;; start changed in a redisplay after the | |
877 | ;; mouse-set-point for the down-mouse event at | |
878 | ;; the beginning of this function. When the | |
879 | ;; window start has changed, the up-mouse event | |
880 | ;; contains a different position due to the new | |
881 | ;; window contents, and point is set again. | |
882 | (or end-point | |
883 | (= (window-start start-window) | |
884 | start-window-start))) | |
5592c08f CY |
885 | (push event unread-command-events))))))) |
886 | ||
6d3e82d2 CY |
887 | (defun mouse--drag-set-mark-and-point (start click click-count) |
888 | (let* ((range (mouse-start-end start click click-count)) | |
889 | (beg (nth 0 range)) | |
890 | (end (nth 1 range))) | |
891 | (cond ((eq (mark) beg) | |
892 | (goto-char end)) | |
893 | ((eq (mark) end) | |
894 | (goto-char beg)) | |
895 | ((< click (mark)) | |
896 | (set-mark end) | |
897 | (goto-char beg)) | |
898 | (t | |
899 | (set-mark beg) | |
900 | (goto-char end))))) | |
901 | ||
e37de120 | 902 | ;; Commands to handle xterm-style multiple clicks. |
e37de120 RS |
903 | (defun mouse-skip-word (dir) |
904 | "Skip over word, over whitespace, or over identical punctuation. | |
905 | If DIR is positive skip forward; if negative, skip backward." | |
906 | (let* ((char (following-char)) | |
907 | (syntax (char-to-string (char-syntax char)))) | |
258e3295 KH |
908 | (cond ((string= syntax "w") |
909 | ;; Here, we can't use skip-syntax-forward/backward because | |
910 | ;; they don't pay attention to word-separating-categories, | |
911 | ;; and thus they will skip over a true word boundary. So, | |
fffa137c | 912 | ;; we simulate the original behavior by using forward-word. |
258e3295 KH |
913 | (if (< dir 0) |
914 | (if (not (looking-at "\\<")) | |
915 | (forward-word -1)) | |
916 | (if (or (looking-at "\\<") (not (looking-at "\\>"))) | |
917 | (forward-word 1)))) | |
918 | ((string= syntax " ") | |
08a1c178 RS |
919 | (if (< dir 0) |
920 | (skip-syntax-backward syntax) | |
921 | (skip-syntax-forward syntax))) | |
922 | ((string= syntax "_") | |
923 | (if (< dir 0) | |
924 | (skip-syntax-backward "w_") | |
925 | (skip-syntax-forward "w_"))) | |
926 | ((< dir 0) | |
927 | (while (and (not (bobp)) (= (preceding-char) char)) | |
928 | (forward-char -1))) | |
929 | (t | |
930 | (while (and (not (eobp)) (= (following-char) char)) | |
931 | (forward-char 1)))))) | |
e37de120 | 932 | |
eb6ff46f | 933 | (defun mouse-start-end (start end mode) |
c3bb6d70 | 934 | "Return a list of region bounds based on START and END according to MODE. |
a7df580d DL |
935 | If MODE is 0 then set point to (min START END), mark to (max START END). |
936 | If MODE is 1 then set point to start of word at (min START END), | |
937 | mark to end of word at (max START END). | |
938 | If MODE is 2 then do the same for lines." | |
e37de120 RS |
939 | (if (> start end) |
940 | (let ((temp start)) | |
941 | (setq start end | |
942 | end temp))) | |
9a974c88 | 943 | (setq mode (mod mode 3)) |
e37de120 RS |
944 | (cond ((= mode 0) |
945 | (list start end)) | |
946 | ((and (= mode 1) | |
947 | (= start end) | |
1ec71583 | 948 | (char-after start) |
e37de120 | 949 | (= (char-syntax (char-after start)) ?\()) |
6f482eec RS |
950 | (list start |
951 | (save-excursion | |
952 | (goto-char start) | |
953 | (forward-sexp 1) | |
954 | (point)))) | |
e37de120 RS |
955 | ((and (= mode 1) |
956 | (= start end) | |
1ec71583 | 957 | (char-after start) |
e37de120 | 958 | (= (char-syntax (char-after start)) ?\))) |
7b611de0 | 959 | (list (save-excursion |
e37de120 | 960 | (goto-char (1+ start)) |
d89a4a47 RS |
961 | (backward-sexp 1) |
962 | (point)) | |
e37de120 | 963 | (1+ start))) |
53770877 RS |
964 | ((and (= mode 1) |
965 | (= start end) | |
966 | (char-after start) | |
967 | (= (char-syntax (char-after start)) ?\")) | |
968 | (let ((open (or (eq start (point-min)) | |
969 | (save-excursion | |
970 | (goto-char (- start 1)) | |
971 | (looking-at "\\s(\\|\\s \\|\\s>"))))) | |
972 | (if open | |
973 | (list start | |
974 | (save-excursion | |
975 | (condition-case nil | |
7b611de0 | 976 | (progn |
53770877 RS |
977 | (goto-char start) |
978 | (forward-sexp 1) | |
979 | (point)) | |
980 | (error end)))) | |
f1017d55 | 981 | (list (save-excursion |
53770877 RS |
982 | (condition-case nil |
983 | (progn | |
984 | (goto-char (1+ start)) | |
985 | (backward-sexp 1) | |
986 | (point)) | |
f1017d55 RS |
987 | (error end))) |
988 | (1+ start))))) | |
e37de120 RS |
989 | ((= mode 1) |
990 | (list (save-excursion | |
991 | (goto-char start) | |
992 | (mouse-skip-word -1) | |
993 | (point)) | |
994 | (save-excursion | |
995 | (goto-char end) | |
996 | (mouse-skip-word 1) | |
997 | (point)))) | |
998 | ((= mode 2) | |
999 | (list (save-excursion | |
1000 | (goto-char start) | |
6d3e82d2 | 1001 | (line-beginning-position 1)) |
e37de120 RS |
1002 | (save-excursion |
1003 | (goto-char end) | |
1004 | (forward-line 1) | |
1005 | (point)))))) | |
e66feb07 | 1006 | \f |
3f26b32a RS |
1007 | ;; Subroutine: set the mark where CLICK happened, |
1008 | ;; but don't do anything else. | |
1009 | (defun mouse-set-mark-fast (click) | |
d65147f6 | 1010 | (mouse-minibuffer-check click) |
3f26b32a RS |
1011 | (let ((posn (event-start click))) |
1012 | (select-window (posn-window posn)) | |
1013 | (if (numberp (posn-point posn)) | |
1014 | (push-mark (posn-point posn) t t)))) | |
1015 | ||
98aac09d MB |
1016 | (defun mouse-undouble-last-event (events) |
1017 | (let* ((index (1- (length events))) | |
1018 | (last (nthcdr index events)) | |
1019 | (event (car last)) | |
1020 | (basic (event-basic-type event)) | |
397a88f3 RS |
1021 | (old-modifiers (event-modifiers event)) |
1022 | (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers)))) | |
98aac09d MB |
1023 | (new |
1024 | (if (consp event) | |
7132500b RS |
1025 | ;; Use reverse, not nreverse, since event-modifiers |
1026 | ;; does not copy the list it returns. | |
3bb846b8 | 1027 | (cons (event-convert-list (reverse (cons basic modifiers))) |
98aac09d MB |
1028 | (cdr event)) |
1029 | event))) | |
1030 | (setcar last new) | |
397a88f3 RS |
1031 | (if (and (not (equal modifiers old-modifiers)) |
1032 | (key-binding (apply 'vector events))) | |
98aac09d MB |
1033 | t |
1034 | (setcar last event) | |
1035 | nil))) | |
1036 | ||
7b611de0 | 1037 | ;; Momentarily show where the mark is, if highlighting doesn't show it. |
7aeb7370 | 1038 | |
cc0a8174 JB |
1039 | (defun mouse-set-mark (click) |
1040 | "Set mark at the position clicked on with the mouse. | |
1041 | Display cursor at that position for a second. | |
1042 | This must be bound to a mouse click." | |
ec558adc | 1043 | (interactive "e") |
f598fb03 RS |
1044 | (mouse-minibuffer-check click) |
1045 | (select-window (posn-window (event-start click))) | |
1046 | ;; We don't use save-excursion because that preserves the mark too. | |
72ea54a4 RS |
1047 | (let ((point-save (point))) |
1048 | (unwind-protect | |
cc0a8174 | 1049 | (progn (mouse-set-point click) |
897897e3 RS |
1050 | (push-mark nil t t) |
1051 | (or transient-mark-mode | |
1052 | (sit-for 1))) | |
72ea54a4 RS |
1053 | (goto-char point-save)))) |
1054 | ||
cc0a8174 JB |
1055 | (defun mouse-kill (click) |
1056 | "Kill the region between point and the mouse click. | |
1057 | The text is saved in the kill ring, as with \\[kill-region]." | |
ec558adc | 1058 | (interactive "e") |
d65147f6 | 1059 | (mouse-minibuffer-check click) |
142c7672 KH |
1060 | (let* ((posn (event-start click)) |
1061 | (click-posn (posn-point posn))) | |
1062 | (select-window (posn-window posn)) | |
bd307392 JB |
1063 | (if (numberp click-posn) |
1064 | (kill-region (min (point) click-posn) | |
1065 | (max (point) click-posn))))) | |
72ea54a4 | 1066 | |
87ef29fd JB |
1067 | (defun mouse-yank-at-click (click arg) |
1068 | "Insert the last stretch of killed text at the position clicked on. | |
0e57ab65 | 1069 | Also move point to one end of the text thus inserted (normally the end), |
231c4d1a | 1070 | and set mark at the beginning. |
50f58001 RS |
1071 | Prefix arguments are interpreted as with \\[yank]. |
1072 | If `mouse-yank-at-point' is non-nil, insert at point | |
9852377f | 1073 | regardless of where you click." |
a4cabe41 | 1074 | (interactive "e\nP") |
33c448cd RS |
1075 | ;; Give temporary modes such as isearch a chance to turn off. |
1076 | (run-hooks 'mouse-leave-buffer-hook) | |
3f5c9cad CY |
1077 | (when select-active-regions |
1078 | ;; Without this, confusing things happen upon e.g. inserting into | |
1079 | ;; the middle of an active region. | |
6f3a2682 | 1080 | (deactivate-mark)) |
50f58001 | 1081 | (or mouse-yank-at-point (mouse-set-point click)) |
d89a4a47 | 1082 | (setq this-command 'yank) |
a814e9df | 1083 | (setq mouse-selection-click-count 0) |
87ef29fd JB |
1084 | (yank arg)) |
1085 | ||
63571b5a RS |
1086 | (defun mouse-yank-primary (click) |
1087 | "Insert the primary selection at the position clicked on. | |
e1eb5385 CY |
1088 | Move point to the end of the inserted text, and set mark at |
1089 | beginning. If `mouse-yank-at-point' is non-nil, insert at point | |
63571b5a RS |
1090 | regardless of where you click." |
1091 | (interactive "e") | |
1092 | ;; Give temporary modes such as isearch a chance to turn off. | |
1093 | (run-hooks 'mouse-leave-buffer-hook) | |
1c409d0b CY |
1094 | ;; Without this, confusing things happen upon e.g. inserting into |
1095 | ;; the middle of an active region. | |
6c6a75d5 | 1096 | (when select-active-regions |
1c409d0b CY |
1097 | (let (select-active-regions) |
1098 | (deactivate-mark))) | |
63571b5a | 1099 | (or mouse-yank-at-point (mouse-set-point click)) |
e7afcf30 | 1100 | (let ((primary |
5f30349b GM |
1101 | (if (fboundp 'x-get-selection-value) |
1102 | (if (eq (framep (selected-frame)) 'w32) | |
1103 | ;; MS-Windows emulates PRIMARY in x-get-selection, but not | |
1104 | ;; in x-get-selection-value (the latter only accesses the | |
1105 | ;; clipboard). So try PRIMARY first, in case they selected | |
1106 | ;; something with the mouse in the current Emacs session. | |
1107 | (or (x-get-selection 'PRIMARY) | |
1108 | (x-get-selection-value)) | |
1109 | ;; Else MS-DOS or X. | |
1110 | ;; On X, x-get-selection-value supports more formats and | |
1111 | ;; encodings, so use it in preference to x-get-selection. | |
1112 | (or (x-get-selection-value) | |
1113 | (x-get-selection 'PRIMARY))) | |
1114 | ;; FIXME: What about xterm-mouse-mode etc.? | |
1115 | (x-get-selection 'PRIMARY)))) | |
e1eb5385 CY |
1116 | (unless primary |
1117 | (error "No selection is available")) | |
1118 | (push-mark (point)) | |
1119 | (insert primary))) | |
63571b5a | 1120 | |
87ef29fd | 1121 | (defun mouse-kill-ring-save (click) |
cc0a8174 JB |
1122 | "Copy the region between point and the mouse click in the kill ring. |
1123 | This does not delete the region; it acts like \\[kill-ring-save]." | |
ec558adc | 1124 | (interactive "e") |
3f26b32a | 1125 | (mouse-set-mark-fast click) |
6452d8a6 | 1126 | (let (this-command last-command) |
5592c08f | 1127 | (kill-ring-save (point) (mark t)))) |
72ea54a4 | 1128 | |
ad0d18af SM |
1129 | ;; This function used to delete the text between point and the mouse |
1130 | ;; whenever it was equal to the front of the kill ring, but some | |
1131 | ;; people found that confusing. | |
dbc4e1c1 | 1132 | |
d2625c3d | 1133 | ;; The position of the last invocation of `mouse-save-then-kill'. |
dbc4e1c1 JB |
1134 | (defvar mouse-save-then-kill-posn nil) |
1135 | ||
26d280b9 | 1136 | (defun mouse-save-then-kill-delete-region (beg end) |
9a974c88 RS |
1137 | ;; We must make our own undo boundaries |
1138 | ;; because they happen automatically only for the current buffer. | |
1139 | (undo-boundary) | |
dd524dbd RS |
1140 | (if (or (= beg end) (eq buffer-undo-list t)) |
1141 | ;; If we have no undo list in this buffer, | |
1142 | ;; just delete. | |
1143 | (delete-region beg end) | |
1144 | ;; Delete, but make the undo-list entry share with the kill ring. | |
1145 | ;; First, delete just one char, so in case buffer is being modified | |
1146 | ;; for the first time, the undo list records that fact. | |
8faa9707 | 1147 | (let (before-change-functions after-change-functions) |
2f4b15ef RS |
1148 | (delete-region beg |
1149 | (+ beg (if (> end beg) 1 -1)))) | |
dd524dbd RS |
1150 | (let ((buffer-undo-list buffer-undo-list)) |
1151 | ;; Undo that deletion--but don't change the undo list! | |
8faa9707 | 1152 | (let (before-change-functions after-change-functions) |
2f4b15ef | 1153 | (primitive-undo 1 buffer-undo-list)) |
dd524dbd RS |
1154 | ;; Now delete the rest of the specified region, |
1155 | ;; but don't record it. | |
1156 | (setq buffer-undo-list t) | |
9a974c88 RS |
1157 | (if (/= (length (car kill-ring)) (- (max end beg) (min end beg))) |
1158 | (error "Lossage in mouse-save-then-kill-delete-region")) | |
dd524dbd RS |
1159 | (delete-region beg end)) |
1160 | (let ((tail buffer-undo-list)) | |
1161 | ;; Search back in buffer-undo-list for the string | |
1162 | ;; that came from deleting one character. | |
1163 | (while (and tail (not (stringp (car (car tail))))) | |
1164 | (setq tail (cdr tail))) | |
1165 | ;; Replace it with an entry for the entire deleted text. | |
1166 | (and tail | |
9a974c88 RS |
1167 | (setcar tail (cons (car kill-ring) (min beg end)))))) |
1168 | (undo-boundary)) | |
eb6ff46f | 1169 | |
947da0c4 | 1170 | (defun mouse-save-then-kill (click) |
d2625c3d CY |
1171 | "Set the region according to CLICK; the second time, kill it. |
1172 | CLICK should be a mouse click event. | |
1173 | ||
1174 | If the region is inactive, activate it temporarily. Set mark at | |
1175 | the original point, and move point to the position of CLICK. | |
1176 | ||
1177 | If the region is already active, adjust it. Normally, do this by | |
1178 | moving point or mark, whichever is closer, to CLICK. But if you | |
1179 | have selected whole words or lines, move point or mark to the | |
1180 | word or line boundary closest to CLICK instead. | |
1181 | ||
93d68d4f DDLHG |
1182 | If `mouse-drag-copy-region' is non-nil, this command also saves the |
1183 | new region to the kill ring (replacing the previous kill if the | |
1184 | previous region was just saved to the kill ring). | |
1185 | ||
d2625c3d | 1186 | If this command is called a second consecutive time with the same |
93d68d4f DDLHG |
1187 | CLICK position, kill the region (or delete it |
1188 | if `mouse-drag-copy-region' is non-nil)" | |
947da0c4 | 1189 | (interactive "e") |
d2625c3d CY |
1190 | (mouse-minibuffer-check click) |
1191 | (let* ((posn (event-start click)) | |
1192 | (click-pt (posn-point posn)) | |
1193 | (window (posn-window posn)) | |
1194 | (buf (window-buffer window)) | |
1195 | ;; Don't let a subsequent kill command append to this one. | |
1196 | (this-command this-command) | |
1197 | ;; Check if the user has multi-clicked to select words/lines. | |
1198 | (click-count | |
1199 | (if (and (eq mouse-selection-click-count-buffer buf) | |
1200 | (with-current-buffer buf (mark t))) | |
1201 | mouse-selection-click-count | |
1202 | 0))) | |
1203 | (cond | |
1204 | ((not (numberp click-pt)) nil) | |
1205 | ;; If the user clicked without moving point, kill the region. | |
1206 | ;; This also resets `mouse-selection-click-count'. | |
1207 | ((and (eq last-command 'mouse-save-then-kill) | |
1208 | (eq click-pt mouse-save-then-kill-posn) | |
1209 | (eq window (selected-window))) | |
93d68d4f DDLHG |
1210 | (if mouse-drag-copy-region |
1211 | ;; Region already saved in the previous click; | |
1212 | ;; don't make a duplicate entry, just delete. | |
1213 | (delete-region (mark t) (point)) | |
1214 | (kill-region (mark t) (point))) | |
d2625c3d CY |
1215 | (setq mouse-selection-click-count 0) |
1216 | (setq mouse-save-then-kill-posn nil)) | |
1217 | ||
1218 | ;; Otherwise, if there is a suitable region, adjust it by moving | |
1219 | ;; one end (whichever is closer) to CLICK-PT. | |
1220 | ((or (with-current-buffer buf (region-active-p)) | |
1221 | (and (eq window (selected-window)) | |
1222 | (mark t) | |
1223 | (or (and (eq last-command 'mouse-save-then-kill) | |
1224 | mouse-save-then-kill-posn) | |
1225 | (and (memq last-command '(mouse-drag-region | |
1226 | mouse-set-region)) | |
1227 | (or mark-even-if-inactive | |
1228 | (not transient-mark-mode)))))) | |
1229 | (select-window window) | |
1230 | (let* ((range (mouse-start-end click-pt click-pt click-count))) | |
1231 | (if (< (abs (- click-pt (mark t))) | |
1232 | (abs (- click-pt (point)))) | |
1233 | (set-mark (car range)) | |
1234 | (goto-char (nth 1 range))) | |
1235 | (setq deactivate-mark nil) | |
1236 | (mouse-set-region-1) | |
93d68d4f DDLHG |
1237 | (when mouse-drag-copy-region |
1238 | ;; Region already copied to kill-ring once, so replace. | |
1239 | (kill-new (filter-buffer-substring (mark t) (point)) t)) | |
d2625c3d CY |
1240 | ;; Arrange for a repeated mouse-3 to kill the region. |
1241 | (setq mouse-save-then-kill-posn click-pt))) | |
1242 | ||
1243 | ;; Otherwise, set the mark where point is and move to CLICK-PT. | |
1244 | (t | |
1245 | (select-window window) | |
1246 | (mouse-set-mark-fast click) | |
1247 | (let ((before-scroll (with-current-buffer buf point-before-scroll))) | |
1248 | (if before-scroll (goto-char before-scroll))) | |
1249 | (exchange-point-and-mark) | |
1250 | (mouse-set-region-1) | |
93d68d4f DDLHG |
1251 | (when mouse-drag-copy-region |
1252 | (kill-new (filter-buffer-substring (mark t) (point)))) | |
d2625c3d CY |
1253 | (setq mouse-save-then-kill-posn click-pt))))) |
1254 | ||
e66feb07 RS |
1255 | \f |
1256 | (global-set-key [M-mouse-1] 'mouse-start-secondary) | |
1257 | (global-set-key [M-drag-mouse-1] 'mouse-set-secondary) | |
1258 | (global-set-key [M-down-mouse-1] 'mouse-drag-secondary) | |
1259 | (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill) | |
9a974c88 | 1260 | (global-set-key [M-mouse-2] 'mouse-yank-secondary) |
e66feb07 | 1261 | |
aaf15b8b SM |
1262 | (defconst mouse-secondary-overlay |
1263 | (let ((ol (make-overlay (point-min) (point-min)))) | |
1264 | (delete-overlay ol) | |
1265 | (overlay-put ol 'face 'secondary-selection) | |
1266 | ol) | |
1267 | "An overlay which records the current secondary selection. | |
1268 | It is deleted when there is no secondary selection.") | |
e66feb07 | 1269 | |
a94c7fcc RS |
1270 | (defvar mouse-secondary-click-count 0) |
1271 | ||
e66feb07 RS |
1272 | ;; A marker which records the specified first end for a secondary selection. |
1273 | ;; May be nil. | |
1274 | (defvar mouse-secondary-start nil) | |
1275 | ||
1276 | (defun mouse-start-secondary (click) | |
1277 | "Set one end of the secondary selection to the position clicked on. | |
1278 | Use \\[mouse-secondary-save-then-kill] to set the other end | |
1279 | and complete the secondary selection." | |
1280 | (interactive "e") | |
d65147f6 | 1281 | (mouse-minibuffer-check click) |
e66feb07 | 1282 | (let ((posn (event-start click))) |
aaf15b8b | 1283 | (with-current-buffer (window-buffer (posn-window posn)) |
230aaa73 | 1284 | ;; Cancel any preexisting secondary selection. |
aaf15b8b | 1285 | (delete-overlay mouse-secondary-overlay) |
230aaa73 RS |
1286 | (if (numberp (posn-point posn)) |
1287 | (progn | |
1288 | (or mouse-secondary-start | |
1289 | (setq mouse-secondary-start (make-marker))) | |
1290 | (move-marker mouse-secondary-start (posn-point posn))))))) | |
e66feb07 RS |
1291 | |
1292 | (defun mouse-set-secondary (click) | |
1293 | "Set the secondary selection to the text that the mouse is dragged over. | |
1294 | This must be bound to a mouse drag event." | |
1295 | (interactive "e") | |
d65147f6 | 1296 | (mouse-minibuffer-check click) |
e66feb07 RS |
1297 | (let ((posn (event-start click)) |
1298 | beg | |
1299 | (end (event-end click))) | |
aaf15b8b | 1300 | (with-current-buffer (window-buffer (posn-window posn)) |
230aaa73 RS |
1301 | (if (numberp (posn-point posn)) |
1302 | (setq beg (posn-point posn))) | |
3e4f866b CY |
1303 | (move-overlay mouse-secondary-overlay beg (posn-point end)) |
1304 | (x-set-selection | |
1305 | 'SECONDARY | |
1306 | (buffer-substring (overlay-start mouse-secondary-overlay) | |
1307 | (overlay-end mouse-secondary-overlay)))))) | |
947da0c4 | 1308 | |
d89a4a47 | 1309 | (defun mouse-drag-secondary (start-event) |
e66feb07 | 1310 | "Set the secondary selection to the text that the mouse is dragged over. |
d89a4a47 | 1311 | Highlight the drag area as you move the mouse. |
efc10c97 RS |
1312 | This must be bound to a button-down mouse event. |
1313 | The function returns a non-nil value if it creates a secondary selection." | |
e66feb07 | 1314 | (interactive "e") |
d65147f6 | 1315 | (mouse-minibuffer-check start-event) |
4c9afcbe RS |
1316 | (let* ((echo-keystrokes 0) |
1317 | (start-posn (event-start start-event)) | |
d89a4a47 RS |
1318 | (start-point (posn-point start-posn)) |
1319 | (start-window (posn-window start-posn)) | |
d89a4a47 RS |
1320 | (bounds (window-edges start-window)) |
1321 | (top (nth 1 bounds)) | |
1322 | (bottom (if (window-minibuffer-p start-window) | |
1323 | (nth 3 bounds) | |
1324 | ;; Don't count the mode line. | |
1325 | (1- (nth 3 bounds)))) | |
1326 | (click-count (1- (event-click-count start-event)))) | |
aaf15b8b | 1327 | (with-current-buffer (window-buffer start-window) |
a94c7fcc | 1328 | (setq mouse-secondary-click-count click-count) |
9a974c88 | 1329 | (if (> (mod click-count 3) 0) |
26d280b9 RS |
1330 | ;; Double or triple press: make an initial selection |
1331 | ;; of one word or line. | |
d89a4a47 RS |
1332 | (let ((range (mouse-start-end start-point start-point click-count))) |
1333 | (set-marker mouse-secondary-start nil) | |
d89a4a47 RS |
1334 | (move-overlay mouse-secondary-overlay (car range) (nth 1 range) |
1335 | (window-buffer start-window))) | |
26d280b9 | 1336 | ;; Single-press: cancel any preexisting secondary selection. |
d89a4a47 RS |
1337 | (or mouse-secondary-start |
1338 | (setq mouse-secondary-start (make-marker))) | |
1339 | (set-marker mouse-secondary-start start-point) | |
1340 | (delete-overlay mouse-secondary-overlay)) | |
1341 | (let (event end end-point) | |
1342 | (track-mouse | |
1343 | (while (progn | |
1344 | (setq event (read-event)) | |
1345 | (or (mouse-movement-p event) | |
fbd8dc8a | 1346 | (memq (car-safe event) '(switch-frame select-window)))) |
d89a4a47 | 1347 | |
fbd8dc8a | 1348 | (if (memq (car-safe event) '(switch-frame select-window)) |
d89a4a47 RS |
1349 | nil |
1350 | (setq end (event-end event) | |
1351 | end-point (posn-point end)) | |
1352 | (cond | |
d89a4a47 RS |
1353 | ;; Are we moving within the original window? |
1354 | ((and (eq (posn-window end) start-window) | |
1355 | (integer-or-marker-p end-point)) | |
d89a4a47 RS |
1356 | (let ((range (mouse-start-end start-point end-point |
1357 | click-count))) | |
0136e1e3 RS |
1358 | (if (or (/= start-point end-point) |
1359 | (null (marker-position mouse-secondary-start))) | |
1360 | (progn | |
1361 | (set-marker mouse-secondary-start nil) | |
1362 | (move-overlay mouse-secondary-overlay | |
1363 | (car range) (nth 1 range)))))) | |
3b0aebe9 RS |
1364 | (t |
1365 | (let ((mouse-row (cdr (cdr (mouse-position))))) | |
1366 | (cond | |
1367 | ((null mouse-row)) | |
1368 | ((< mouse-row top) | |
e919a622 RS |
1369 | (mouse-scroll-subr start-window (- mouse-row top) |
1370 | mouse-secondary-overlay start-point)) | |
d2287ded | 1371 | ((>= mouse-row bottom) |
e919a622 | 1372 | (mouse-scroll-subr start-window (1+ (- mouse-row bottom)) |
3b0aebe9 | 1373 | mouse-secondary-overlay start-point))))))))) |
d89a4a47 | 1374 | |
4e399a53 | 1375 | (if (consp event) |
d89a4a47 RS |
1376 | (if (marker-position mouse-secondary-start) |
1377 | (save-window-excursion | |
1378 | (delete-overlay mouse-secondary-overlay) | |
9a974c88 | 1379 | (x-set-selection 'SECONDARY nil) |
d89a4a47 RS |
1380 | (select-window start-window) |
1381 | (save-excursion | |
1382 | (goto-char mouse-secondary-start) | |
efc10c97 RS |
1383 | (sit-for 1) |
1384 | nil)) | |
9a974c88 RS |
1385 | (x-set-selection |
1386 | 'SECONDARY | |
1387 | (buffer-substring (overlay-start mouse-secondary-overlay) | |
1388 | (overlay-end mouse-secondary-overlay))))))))) | |
e66feb07 | 1389 | |
9a974c88 | 1390 | (defun mouse-yank-secondary (click) |
50f58001 RS |
1391 | "Insert the secondary selection at the position clicked on. |
1392 | Move point to the end of the inserted text. | |
1393 | If `mouse-yank-at-point' is non-nil, insert at point | |
1394 | regardless of where you click." | |
a4cabe41 | 1395 | (interactive "e") |
33c448cd RS |
1396 | ;; Give temporary modes such as isearch a chance to turn off. |
1397 | (run-hooks 'mouse-leave-buffer-hook) | |
50f58001 | 1398 | (or mouse-yank-at-point (mouse-set-point click)) |
b3709a53 RS |
1399 | (let ((secondary (x-get-selection 'SECONDARY))) |
1400 | (if secondary | |
aa2d4bd3 | 1401 | (insert secondary) |
b3709a53 | 1402 | (error "No secondary selection")))) |
9a974c88 | 1403 | |
7e6404f6 | 1404 | (defun mouse-kill-secondary () |
9a974c88 RS |
1405 | "Kill the text in the secondary selection. |
1406 | This is intended more as a keyboard command than as a mouse command | |
1407 | but it can work as either one. | |
1408 | ||
1409 | The current buffer (in case of keyboard use), or the buffer clicked on, | |
1410 | must be the one that the secondary selection is in. This requirement | |
1411 | is to prevent accidents." | |
7e6404f6 RS |
1412 | (interactive) |
1413 | (let* ((keys (this-command-keys)) | |
1414 | (click (elt keys (1- (length keys))))) | |
1415 | (or (eq (overlay-buffer mouse-secondary-overlay) | |
1416 | (if (listp click) | |
1417 | (window-buffer (posn-window (event-start click))) | |
1418 | (current-buffer))) | |
1419 | (error "Select or click on the buffer where the secondary selection is"))) | |
9dfab550 | 1420 | (let (this-command) |
aaf15b8b | 1421 | (with-current-buffer (overlay-buffer mouse-secondary-overlay) |
9dfab550 RS |
1422 | (kill-region (overlay-start mouse-secondary-overlay) |
1423 | (overlay-end mouse-secondary-overlay)))) | |
f9be2e35 | 1424 | (delete-overlay mouse-secondary-overlay)) |
e66feb07 RS |
1425 | |
1426 | (defun mouse-secondary-save-then-kill (click) | |
d2625c3d CY |
1427 | "Set the secondary selection and save it to the kill ring. |
1428 | The second time, kill it. CLICK should be a mouse click event. | |
1429 | ||
1430 | If you have not called `mouse-start-secondary' in the clicked | |
1431 | buffer, activate the secondary selection and set it between point | |
1432 | and the click position CLICK. | |
1433 | ||
1434 | Otherwise, adjust the bounds of the secondary selection. | |
1435 | Normally, do this by moving its beginning or end, whichever is | |
1436 | closer, to CLICK. But if you have selected whole words or lines, | |
1437 | adjust to the word or line boundary closest to CLICK instead. | |
1438 | ||
1439 | If this command is called a second consecutive time with the same | |
1440 | CLICK position, kill the secondary selection." | |
e66feb07 | 1441 | (interactive "e") |
d65147f6 | 1442 | (mouse-minibuffer-check click) |
d2625c3d CY |
1443 | (let* ((posn (event-start click)) |
1444 | (click-pt (posn-point posn)) | |
1445 | (window (posn-window posn)) | |
1446 | (buf (window-buffer window)) | |
1447 | ;; Don't let a subsequent kill command append to this one. | |
1448 | (this-command this-command) | |
1449 | ;; Check if the user has multi-clicked to select words/lines. | |
1450 | (click-count | |
1451 | (if (eq (overlay-buffer mouse-secondary-overlay) buf) | |
1452 | mouse-secondary-click-count | |
1453 | 0)) | |
1454 | (beg (overlay-start mouse-secondary-overlay)) | |
1455 | (end (overlay-end mouse-secondary-overlay))) | |
1456 | ||
1457 | (cond | |
1458 | ((not (numberp click-pt)) nil) | |
1459 | ||
1460 | ;; If the secondary selection is not active in BUF, activate it. | |
1461 | ((not (eq buf (or (overlay-buffer mouse-secondary-overlay) | |
1462 | (if mouse-secondary-start | |
1463 | (marker-buffer mouse-secondary-start))))) | |
1464 | (select-window window) | |
1465 | (setq mouse-secondary-start (make-marker)) | |
1466 | (move-marker mouse-secondary-start (point)) | |
1467 | (move-overlay mouse-secondary-overlay (point) click-pt buf) | |
1468 | (kill-ring-save (point) click-pt)) | |
1469 | ||
1470 | ;; If the user clicked without moving point, delete the secondary | |
1471 | ;; selection. This also resets `mouse-secondary-click-count'. | |
1472 | ((and (eq last-command 'mouse-secondary-save-then-kill) | |
1473 | (eq click-pt mouse-save-then-kill-posn) | |
1474 | (eq window (selected-window))) | |
1475 | (mouse-save-then-kill-delete-region beg end) | |
1476 | (delete-overlay mouse-secondary-overlay) | |
1477 | (setq mouse-secondary-click-count 0) | |
1478 | (setq mouse-save-then-kill-posn nil)) | |
1479 | ||
1480 | ;; Otherwise, if there is a suitable secondary selection overlay, | |
1481 | ;; adjust it by moving one end (whichever is closer) to CLICK-PT. | |
1482 | ((and beg (eq buf (overlay-buffer mouse-secondary-overlay))) | |
1483 | (let* ((range (mouse-start-end click-pt click-pt click-count))) | |
1484 | (if (< (abs (- click-pt beg)) | |
1485 | (abs (- click-pt end))) | |
1486 | (move-overlay mouse-secondary-overlay (car range) end) | |
1487 | (move-overlay mouse-secondary-overlay beg (nth 1 range)))) | |
1488 | (setq deactivate-mark nil) | |
1489 | (if (eq last-command 'mouse-secondary-save-then-kill) | |
1490 | ;; If the front of the kill ring comes from an immediately | |
1491 | ;; previous use of this command, replace the entry. | |
1492 | (kill-new | |
1493 | (buffer-substring (overlay-start mouse-secondary-overlay) | |
1494 | (overlay-end mouse-secondary-overlay)) | |
1495 | t) | |
1496 | (let (deactivate-mark) | |
1497 | (copy-region-as-kill (overlay-start mouse-secondary-overlay) | |
1498 | (overlay-end mouse-secondary-overlay)))) | |
1499 | (setq mouse-save-then-kill-posn click-pt)) | |
1500 | ||
1501 | ;; Otherwise, set the secondary selection overlay. | |
1502 | (t | |
1503 | (select-window window) | |
1504 | (if mouse-secondary-start | |
1505 | ;; All we have is one end of a selection, so put the other | |
1506 | ;; end here. | |
1507 | (let ((start (+ 0 mouse-secondary-start))) | |
1508 | (kill-ring-save start click-pt) | |
1509 | (move-overlay mouse-secondary-overlay start click-pt))) | |
1510 | (setq mouse-save-then-kill-posn click-pt)))) | |
1511 | ||
1512 | ;; Finally, set the window system's secondary selection. | |
1513 | (let (str) | |
1514 | (and (overlay-buffer mouse-secondary-overlay) | |
1515 | (setq str (buffer-substring (overlay-start mouse-secondary-overlay) | |
1516 | (overlay-end mouse-secondary-overlay))) | |
1517 | (> (length str) 0) | |
1518 | (x-set-selection 'SECONDARY str)))) | |
1519 | ||
e66feb07 | 1520 | \f |
0d9be91c | 1521 | (defcustom mouse-buffer-menu-maxlen 20 |
9201cc28 | 1522 | "Number of buffers in one pane (submenu) of the buffer menu. |
f1107960 | 1523 | If we have lots of buffers, divide them into groups of |
0d9be91c | 1524 | `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one." |
3b558d41 RS |
1525 | :type 'integer |
1526 | :group 'mouse) | |
f1107960 | 1527 | |
3ca87c7b | 1528 | (defcustom mouse-buffer-menu-mode-mult 4 |
9201cc28 | 1529 | "Group the buffers by the major mode groups on \\[mouse-buffer-menu]? |
3ca87c7b RS |
1530 | This number which determines (in a hairy way) whether \\[mouse-buffer-menu] |
1531 | will split the buffer menu by the major modes (see | |
1532 | `mouse-buffer-menu-mode-groups') or just by menu length. | |
1533 | Set to 1 (or even 0!) if you want to group by major mode always, and to | |
1534 | a large number if you prefer a mixed multitude. The default is 4." | |
1535 | :type 'integer | |
1536 | :group 'mouse | |
1537 | :version "20.3") | |
1538 | ||
284a88a3 | 1539 | (defvar mouse-buffer-menu-mode-groups |
e8e4d5c8 | 1540 | (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg)))) |
284a88a3 RS |
1541 | '(("Info\\|Help\\|Apropos\\|Man" . "Help") |
1542 | ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article" | |
1543 | . "Mail/News") | |
1544 | ("\\<C\\>" . "C") | |
1545 | ("ObjC" . "C") | |
1546 | ("Text" . "Text") | |
1547 | ("Outline" . "Text") | |
c71a58a3 | 1548 | ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML") |
f399e4ad | 1549 | ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"? |
7bc61bb7 SS |
1550 | ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger" |
1551 | . "GDB") | |
a7610c52 | 1552 | ("Lisp" . "Lisp"))) |
284a88a3 RS |
1553 | "How to group various major modes together in \\[mouse-buffer-menu]. |
1554 | Each element has the form (REGEXP . GROUPNAME). | |
1555 | If the major mode's name string matches REGEXP, use GROUPNAME instead.") | |
1556 | ||
8b34e79d | 1557 | (defun mouse-buffer-menu (event) |
2d82f7b9 RS |
1558 | "Pop up a menu of buffers for selection with the mouse. |
1559 | This switches buffers in the window that you clicked on, | |
1560 | and selects that window." | |
ec558adc | 1561 | (interactive "e") |
d65147f6 | 1562 | (mouse-minibuffer-check event) |
3ca87c7b | 1563 | (let ((buffers (buffer-list)) alist menu split-by-major-mode sum-of-squares) |
284a88a3 | 1564 | ;; Make an alist of elements that look like (MENU-ITEM . BUFFER). |
cc2ee8ec SS |
1565 | (dolist (buf buffers) |
1566 | ;; Divide all buffers into buckets for various major modes. | |
1567 | ;; Each bucket looks like (MODE NAMESTRING BUFFERS...). | |
1568 | (with-current-buffer buf | |
1569 | (let* ((adjusted-major-mode major-mode) elt) | |
1570 | (dolist (group mouse-buffer-menu-mode-groups) | |
1571 | (when (string-match (car group) (format-mode-line mode-name)) | |
1572 | (setq adjusted-major-mode (cdr group)))) | |
1573 | (setq elt (assoc adjusted-major-mode split-by-major-mode)) | |
1574 | (unless elt | |
1575 | (setq elt (list adjusted-major-mode | |
1576 | (if (stringp adjusted-major-mode) | |
1577 | adjusted-major-mode | |
48d33090 | 1578 | (format-mode-line mode-name nil nil buf))) |
cc2ee8ec SS |
1579 | split-by-major-mode (cons elt split-by-major-mode))) |
1580 | (or (memq buf (cdr (cdr elt))) | |
1581 | (setcdr (cdr elt) (cons buf (cdr (cdr elt)))))))) | |
284a88a3 RS |
1582 | ;; Compute the sum of squares of sizes of the major-mode buckets. |
1583 | (let ((tail split-by-major-mode)) | |
1584 | (setq sum-of-squares 0) | |
1585 | (while tail | |
1586 | (setq sum-of-squares | |
1587 | (+ sum-of-squares | |
3ca87c7b | 1588 | (let ((len (length (cdr (cdr (car tail)))))) (* len len)))) |
284a88a3 | 1589 | (setq tail (cdr tail)))) |
3ca87c7b RS |
1590 | (if (< (* sum-of-squares mouse-buffer-menu-mode-mult) |
1591 | (* (length buffers) (length buffers))) | |
284a88a3 RS |
1592 | ;; Subdividing by major modes really helps, so let's do it. |
1593 | (let (subdivided-menus (buffers-left (length buffers))) | |
1594 | ;; Sort the list to put the most popular major modes first. | |
1595 | (setq split-by-major-mode | |
1596 | (sort split-by-major-mode | |
1597 | (function (lambda (elt1 elt2) | |
1598 | (> (length elt1) (length elt2)))))) | |
1599 | ;; Make a separate submenu for each major mode | |
1600 | ;; that has more than one buffer, | |
1601 | ;; unless all the remaining buffers are less than 1/10 of them. | |
1602 | (while (and split-by-major-mode | |
1603 | (and (> (length (car split-by-major-mode)) 3) | |
1604 | (> (* buffers-left 10) (length buffers)))) | |
4b4ea1dc EZ |
1605 | (let ((this-mode-list (mouse-buffer-menu-alist |
1606 | (cdr (cdr (car split-by-major-mode)))))) | |
1607 | (and this-mode-list | |
1608 | (setq subdivided-menus | |
1609 | (cons (cons | |
1610 | (nth 1 (car split-by-major-mode)) | |
1611 | this-mode-list) | |
1612 | subdivided-menus)))) | |
284a88a3 RS |
1613 | (setq buffers-left |
1614 | (- buffers-left (length (cdr (car split-by-major-mode))))) | |
1615 | (setq split-by-major-mode (cdr split-by-major-mode))) | |
1616 | ;; If any major modes are left over, | |
1617 | ;; make a single submenu for them. | |
1618 | (if split-by-major-mode | |
4b4ea1dc EZ |
1619 | (let ((others-list |
1620 | (mouse-buffer-menu-alist | |
1621 | ;; we don't need split-by-major-mode any more, | |
1622 | ;; so we can ditch it with nconc. | |
1623 | (apply 'nconc (mapcar 'cddr split-by-major-mode))))) | |
1624 | (and others-list | |
1625 | (setq subdivided-menus | |
1626 | (cons (cons "Others" others-list) | |
1627 | subdivided-menus))))) | |
3ca87c7b | 1628 | (setq menu (cons "Buffer Menu" (nreverse subdivided-menus)))) |
284a88a3 RS |
1629 | (progn |
1630 | (setq alist (mouse-buffer-menu-alist buffers)) | |
1631 | (setq menu (cons "Buffer Menu" | |
1632 | (mouse-buffer-menu-split "Select Buffer" alist))))) | |
2d82f7b9 RS |
1633 | (let ((buf (x-popup-menu event menu)) |
1634 | (window (posn-window (event-start event)))) | |
3ca87c7b | 1635 | (when buf |
f134ad18 RS |
1636 | (select-window |
1637 | (if (framep window) (frame-selected-window window) | |
1638 | window)) | |
3ca87c7b | 1639 | (switch-to-buffer buf))))) |
284a88a3 RS |
1640 | |
1641 | (defun mouse-buffer-menu-alist (buffers) | |
1642 | (let (tail | |
1643 | (maxlen 0) | |
1644 | head) | |
1645 | (setq buffers | |
1646 | (sort buffers | |
1647 | (function (lambda (elt1 elt2) | |
1648 | (string< (buffer-name elt1) (buffer-name elt2)))))) | |
1649 | (setq tail buffers) | |
1650 | (while tail | |
fc2b429e | 1651 | (or (eq ?\s (aref (buffer-name (car tail)) 0)) |
284a88a3 RS |
1652 | (setq maxlen |
1653 | (max maxlen | |
1654 | (length (buffer-name (car tail)))))) | |
1655 | (setq tail (cdr tail))) | |
1656 | (setq tail buffers) | |
1657 | (while tail | |
1658 | (let ((elt (car tail))) | |
66564ab0 | 1659 | (if (/= (aref (buffer-name elt) 0) ?\s) |
284a88a3 RS |
1660 | (setq head |
1661 | (cons | |
1662 | (cons | |
1663 | (format | |
fc1f472b | 1664 | (format "%%-%ds %%s%%s %%s" maxlen) |
284a88a3 RS |
1665 | (buffer-name elt) |
1666 | (if (buffer-modified-p elt) "*" " ") | |
7fdbcd83 | 1667 | (with-current-buffer elt |
284a88a3 | 1668 | (if buffer-read-only "%" " ")) |
7b611de0 | 1669 | (or (buffer-file-name elt) |
7fdbcd83 | 1670 | (with-current-buffer elt |
284a88a3 RS |
1671 | (if list-buffers-directory |
1672 | (expand-file-name | |
1673 | list-buffers-directory))) | |
1674 | "")) | |
1675 | elt) | |
1676 | head)))) | |
1677 | (setq tail (cdr tail))) | |
1678 | ;; Compensate for the reversal that the above loop does. | |
1679 | (nreverse head))) | |
1680 | ||
1681 | (defun mouse-buffer-menu-split (title alist) | |
1682 | ;; If we have lots of buffers, divide them into groups of 20 | |
1683 | ;; and make a pane (or submenu) for each one. | |
0d9be91c | 1684 | (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2)) |
284a88a3 RS |
1685 | (let ((alist alist) sublists next |
1686 | (i 1)) | |
1687 | (while alist | |
0d9be91c | 1688 | ;; Pull off the next mouse-buffer-menu-maxlen buffers |
284a88a3 | 1689 | ;; and make them the next element of sublist. |
0d9be91c | 1690 | (setq next (nthcdr mouse-buffer-menu-maxlen alist)) |
284a88a3 | 1691 | (if next |
0d9be91c | 1692 | (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist) |
284a88a3 RS |
1693 | nil)) |
1694 | (setq sublists (cons (cons (format "Buffers %d" i) alist) | |
1695 | sublists)) | |
1696 | (setq i (1+ i)) | |
1697 | (setq alist next)) | |
1698 | (nreverse sublists)) | |
1699 | ;; Few buffers--put them all in one pane. | |
1700 | (list (cons title alist)))) | |
72ea54a4 | 1701 | \f |
d5e63715 SM |
1702 | (define-obsolete-function-alias |
1703 | 'mouse-choose-completion 'choose-completion "23.2") | |
f936ae06 | 1704 | |
07a78410 RS |
1705 | ;; Font selection. |
1706 | ||
0eb9fef3 RS |
1707 | (defun font-menu-add-default () |
1708 | (let* ((default (cdr (assq 'font (frame-parameters (selected-frame))))) | |
1709 | (font-alist x-fixed-font-alist) | |
0d94f5ca | 1710 | (elt (or (assoc "Misc" font-alist) (nth 1 font-alist)))) |
0eb9fef3 RS |
1711 | (if (assoc "Default" elt) |
1712 | (delete (assoc "Default" elt) elt)) | |
1713 | (setcdr elt | |
6b8e486e | 1714 | (cons (list "Default" default) |
0eb9fef3 RS |
1715 | (cdr elt))))) |
1716 | ||
07a78410 | 1717 | (defvar x-fixed-font-alist |
a7610c52 DN |
1718 | (list |
1719 | (purecopy "Font Menu") | |
1720 | (cons | |
1721 | (purecopy "Misc") | |
1722 | (mapcar | |
1723 | (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg)))) | |
19d973e8 | 1724 | ;; For these, we specify the pixel height and width. |
a7610c52 | 1725 | '(("fixed" "fixed") |
19d973e8 RS |
1726 | ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10") |
1727 | ("6x12" | |
1728 | "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12") | |
1729 | ("6x13" | |
1730 | "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13") | |
1731 | ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13") | |
1732 | ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14") | |
1733 | ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13") | |
1734 | ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15") | |
1735 | ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20") | |
1736 | ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18") | |
1737 | ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24") | |
fa21fdec | 1738 | ("") |
f29c3695 RS |
1739 | ("clean 5x8" |
1740 | "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1") | |
1741 | ("clean 6x8" | |
1742 | "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1") | |
19d973e8 RS |
1743 | ("clean 8x8" |
1744 | "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1") | |
1745 | ("clean 8x10" | |
1746 | "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1") | |
1747 | ("clean 8x14" | |
1748 | "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1") | |
1749 | ("clean 8x16" | |
1750 | "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1") | |
fa21fdec | 1751 | ("") |
29a3028d | 1752 | ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1") |
ad0d18af SM |
1753 | ;; We don't seem to have these; who knows what they are. |
1754 | ;; ("fg-18" "fg-18") | |
1755 | ;; ("fg-25" "fg-25") | |
29a3028d DL |
1756 | ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1") |
1757 | ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1") | |
1758 | ("lucidasanstypewriter-bold-24" | |
1759 | "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1") | |
ad0d18af SM |
1760 | ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1") |
1761 | ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*") | |
a7610c52 DN |
1762 | ))) |
1763 | ||
1764 | (cons | |
1765 | (purecopy "Courier") | |
1766 | (mapcar | |
1767 | (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg)))) | |
19d973e8 | 1768 | ;; For these, we specify the point height. |
a7610c52 | 1769 | '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1") |
82c048a9 RS |
1770 | ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1") |
1771 | ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1") | |
1772 | ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1") | |
1773 | ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1") | |
1774 | ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1") | |
1775 | ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1") | |
1776 | ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1") | |
1777 | ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1") | |
1778 | ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1") | |
1779 | ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1") | |
1780 | ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1") | |
1781 | ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1") | |
1782 | ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1") | |
1783 | ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1") | |
1784 | ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1") | |
1785 | ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1") | |
1786 | ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1") | |
1787 | ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1") | |
1788 | ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1") | |
1789 | ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1") | |
1790 | ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1") | |
1791 | ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1") | |
a7610c52 DN |
1792 | ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1") |
1793 | )))) | |
07a78410 RS |
1794 | "X fonts suitable for use in Emacs.") |
1795 | ||
aa360da1 GM |
1796 | (declare-function generate-fontset-menu "fontset" ()) |
1797 | ||
ecb044e1 CY |
1798 | (defun mouse-select-font () |
1799 | "Prompt for a font name, using `x-popup-menu', and return it." | |
1800 | (interactive) | |
1801 | (unless (display-multi-font-p) | |
1802 | (error "Cannot change fonts on this display")) | |
c1202d4b CY |
1803 | (car |
1804 | (x-popup-menu | |
1805 | (if (listp last-nonmenu-event) | |
1806 | last-nonmenu-event | |
1807 | (list '(0 0) (selected-window))) | |
1808 | (append x-fixed-font-alist | |
1809 | (list (generate-fontset-menu)))))) | |
ecb044e1 | 1810 | |
3db09e26 CY |
1811 | (declare-function text-scale-mode "face-remap") |
1812 | ||
1900a92b | 1813 | (defun mouse-set-font (&rest fonts) |
ecb044e1 CY |
1814 | "Set the default font for the selected frame. |
1815 | The argument FONTS is a list of font names; the first valid font | |
1816 | in this list is used. | |
1817 | ||
1818 | When called interactively, pop up a menu and allow the user to | |
1819 | choose a font." | |
07a78410 | 1820 | (interactive |
8eb1dc02 RS |
1821 | (progn (unless (display-multi-font-p) |
1822 | (error "Cannot change fonts on this display")) | |
1823 | (x-popup-menu | |
91c51412 LT |
1824 | (if (listp last-nonmenu-event) |
1825 | last-nonmenu-event | |
1826 | (list '(0 0) (selected-window))) | |
8eb1dc02 RS |
1827 | ;; Append list of fontsets currently defined. |
1828 | (append x-fixed-font-alist (list (generate-fontset-menu)))))) | |
df4de8c6 KH |
1829 | (if fonts |
1830 | (let (font) | |
1831 | (while fonts | |
1832 | (condition-case nil | |
1833 | (progn | |
b7d1d101 | 1834 | (set-frame-font (car fonts)) |
df4de8c6 KH |
1835 | (setq font (car fonts)) |
1836 | (setq fonts nil)) | |
3fadec1a RS |
1837 | (error |
1838 | (setq fonts (cdr fonts))))) | |
df4de8c6 | 1839 | (if (null font) |
8eb1dc02 | 1840 | (error "Font not found"))))) |
3db09e26 CY |
1841 | |
1842 | (defvar mouse-appearance-menu-map nil) | |
9c257005 | 1843 | (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK |
b816c5e3 GM |
1844 | (declare-function buffer-face-mode-invoke "face-remap" |
1845 | (face arg &optional interactive)) | |
f2d9c15f | 1846 | (declare-function font-face-attributes "font.c" (font &optional frame)) |
3db09e26 CY |
1847 | |
1848 | (defun mouse-appearance-menu (event) | |
e8579ebc | 1849 | "Show a menu for changing the default face in the current buffer." |
3db09e26 CY |
1850 | (interactive "@e") |
1851 | (require 'face-remap) | |
1852 | (when (display-multi-font-p) | |
1853 | (with-selected-window (car (event-start event)) | |
1854 | (if mouse-appearance-menu-map | |
1855 | nil ; regenerate new fonts | |
1856 | ;; Initialize mouse-appearance-menu-map | |
1857 | (setq mouse-appearance-menu-map | |
1858 | (make-sparse-keymap "Change Default Buffer Face")) | |
1859 | (define-key mouse-appearance-menu-map [face-remap-reset-base] | |
1860 | '(menu-item "Reset to Default" face-remap-reset-base)) | |
1861 | (define-key mouse-appearance-menu-map [text-scale-decrease] | |
1862 | '(menu-item "Decrease Buffer Text Size" text-scale-decrease)) | |
1863 | (define-key mouse-appearance-menu-map [text-scale-increase] | |
1864 | '(menu-item "Increase Buffer Text Size" text-scale-increase)) | |
1865 | ;; Font selector | |
1866 | (if (functionp 'x-select-font) | |
1867 | (define-key mouse-appearance-menu-map [x-select-font] | |
1868 | '(menu-item "Change Buffer Font..." x-select-font)) | |
1869 | ;; If the select-font is unavailable, construct a menu. | |
1870 | (let ((font-submenu (make-sparse-keymap "Change Text Font")) | |
1871 | (font-alist (cdr (append x-fixed-font-alist | |
1872 | (list (generate-fontset-menu)))))) | |
1873 | (dolist (family font-alist) | |
1874 | (let* ((submenu-name (car family)) | |
1875 | (submenu-map (make-sparse-keymap submenu-name))) | |
1876 | (dolist (font (cdr family)) | |
1877 | (let ((font-name (car font)) | |
1878 | font-symbol) | |
1879 | (if (string= font-name "") | |
1880 | (define-key submenu-map [space] | |
1881 | '("--")) | |
1882 | (setq font-symbol (intern (cadr font))) | |
1883 | (define-key submenu-map (vector font-symbol) | |
1884 | (list 'menu-item (car font) font-symbol))))) | |
1885 | (define-key font-submenu (vector (intern submenu-name)) | |
1886 | (list 'menu-item submenu-name submenu-map)))) | |
1887 | (define-key mouse-appearance-menu-map [font-submenu] | |
1888 | (list 'menu-item "Change Text Font" font-submenu))))) | |
1889 | (let ((choice (x-popup-menu event mouse-appearance-menu-map))) | |
1890 | (setq choice (nth (1- (length choice)) choice)) | |
1891 | (cond ((eq choice 'text-scale-increase) | |
1892 | (text-scale-increase 1)) | |
1893 | ((eq choice 'text-scale-decrease) | |
1894 | (text-scale-increase -1)) | |
1895 | ((eq choice 'face-remap-reset-base) | |
1896 | (text-scale-mode 0) | |
4271b424 | 1897 | (buffer-face-mode 0)) |
520713e7 | 1898 | (choice |
3db09e26 CY |
1899 | ;; Either choice == 'x-select-font, or choice is a |
1900 | ;; symbol whose name is a font. | |
383dcbf9 CY |
1901 | (let ((font (if (eq choice 'x-select-font) |
1902 | (x-select-font) | |
1903 | (symbol-name choice)))) | |
1904 | (buffer-face-mode-invoke | |
1905 | (if (fontp font 'font-spec) | |
1906 | (list :font font) | |
1907 | (font-face-attributes font)) | |
1908 | t (called-interactively-p 'interactive))))))))) | |
3db09e26 | 1909 | |
cc0a8174 JB |
1910 | \f |
1911 | ;;; Bindings for mouse commands. | |
1912 | ||
fcfc3c63 | 1913 | (define-key global-map [down-mouse-1] 'mouse-drag-region) |
dbc4e1c1 | 1914 | (global-set-key [mouse-1] 'mouse-set-point) |
dbc4e1c1 | 1915 | (global-set-key [drag-mouse-1] 'mouse-set-region) |
fcfc3c63 | 1916 | |
e37de120 RS |
1917 | ;; These are tested for in mouse-drag-region. |
1918 | (global-set-key [double-mouse-1] 'mouse-set-point) | |
1919 | (global-set-key [triple-mouse-1] 'mouse-set-point) | |
1920 | ||
fe3f64d5 SM |
1921 | (defun mouse--strip-first-event (_prompt) |
1922 | (substring (this-single-command-raw-keys) 1)) | |
1923 | ||
1924 | (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event) | |
1925 | (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event) | |
76693d12 | 1926 | |
f9d71b42 | 1927 | (global-set-key [mouse-2] 'mouse-yank-primary) |
3e9323d6 | 1928 | ;; Allow yanking also when the corresponding cursor is "in the fringe". |
fe3f64d5 SM |
1929 | (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event) |
1930 | (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event) | |
dbc4e1c1 | 1931 | (global-set-key [mouse-3] 'mouse-save-then-kill) |
fe3f64d5 SM |
1932 | (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event) |
1933 | (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event) | |
8b34e79d | 1934 | |
dbc4e1c1 JB |
1935 | ;; By binding these to down-going events, we let the user use the up-going |
1936 | ;; event to make the selection, saving a click. | |
08a1c178 RS |
1937 | (global-set-key [C-down-mouse-1] 'mouse-buffer-menu) |
1938 | (if (not (eq system-type 'ms-dos)) | |
3db09e26 | 1939 | (global-set-key [S-down-mouse-1] 'mouse-appearance-menu)) |
eef805d7 | 1940 | ;; C-down-mouse-2 is bound in facemenu.el. |
0b2b62ff | 1941 | (global-set-key [C-down-mouse-3] |
1e8780b1 | 1942 | `(menu-item ,(purecopy "Menu Bar") ignore |
0b2b62ff SM |
1943 | :filter (lambda (_) |
1944 | (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0)) | |
1945 | (mouse-menu-bar-map) | |
1946 | (mouse-menu-major-mode-map))))) | |
95132d1c | 1947 | |
06a8c9f8 EZ |
1948 | ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or |
1949 | ;; vertical-line prevents Emacs from signaling an error when the mouse | |
1950 | ;; button is released after dragging these lines, on non-toolkit | |
1951 | ;; versions. | |
3c2dd2c0 | 1952 | (global-set-key [mode-line mouse-1] 'mouse-select-window) |
544e7e73 RS |
1953 | (global-set-key [mode-line drag-mouse-1] 'mouse-select-window) |
1954 | (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line) | |
b0d22e20 | 1955 | (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line) |
06a8c9f8 | 1956 | (global-set-key [header-line mouse-1] 'mouse-select-window) |
3c2dd2c0 | 1957 | (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows) |
dbc4e1c1 | 1958 | (global-set-key [mode-line mouse-3] 'mouse-delete-window) |
3c2dd2c0 | 1959 | (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally) |
9926ab64 | 1960 | (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically) |
b6522df6 | 1961 | (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically) |
08a1c178 | 1962 | (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line) |
880e6158 MR |
1963 | (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line) |
1964 | (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line) | |
08a1c178 | 1965 | (global-set-key [vertical-line mouse-1] 'mouse-select-window) |
49116ac0 JB |
1966 | |
1967 | (provide 'mouse) | |
1968 | ||
6594deb0 | 1969 | ;;; mouse.el ends here |