Commit | Line | Data |
---|---|---|
72cc582e KS |
1 | ;;; cua-base.el --- emulate CUA key bindings |
2 | ||
5fd6d89f | 3 | ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
ceb4c4d3 | 4 | ;; 2005, 2006 Free Software Foundation, Inc. |
72cc582e KS |
5 | |
6 | ;; Author: Kim F. Storm <storm@cua.dk> | |
7 | ;; Keywords: keyboard emulation convenience cua | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
3a35cf56 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
72cc582e KS |
25 | |
26 | ||
27 | ;;; Commentary: | |
28 | ||
29 | ;; This is the CUA package which provides a complete emulation of the | |
30 | ;; standard CUA key bindings (Motif/Windows/Mac GUI) for selecting and | |
31 | ;; manipulating the region where S-<movement> is used to highlight & | |
32 | ;; extend the region. | |
33 | ||
34a63289 KS |
34 | ;; CUA style key bindings for cut and paste |
35 | ;; ---------------------------------------- | |
36 | ||
37 | ;; This package allows the C-z, C-x, C-c, and C-v keys to be | |
72cc582e KS |
38 | ;; bound appropriately according to the Motif/Windows GUI, i.e. |
39 | ;; C-z -> undo | |
40 | ;; C-x -> cut | |
41 | ;; C-c -> copy | |
42 | ;; C-v -> paste | |
43 | ;; | |
44 | ;; The tricky part is the handling of the C-x and C-c keys which | |
45 | ;; are normally used as prefix keys for most of emacs' built-in | |
46 | ;; commands. With CUA they still do!!! | |
47 | ;; | |
48 | ;; Only when the region is currently active (and highlighted since | |
49 | ;; transient-mark-mode is used), the C-x and C-c keys will work as CUA | |
50 | ;; keys | |
51 | ;; C-x -> cut | |
52 | ;; C-c -> copy | |
53 | ;; When the region is not active, C-x and C-c works as prefix keys! | |
54 | ;; | |
55 | ;; This probably sounds strange and difficult to get used to - but | |
56 | ;; based on my own experience and the feedback from many users of | |
57 | ;; this package, it actually works very well and users adapt to it | |
a1506d29 | 58 | ;; instantly - or at least very quickly. So give it a try! |
72cc582e KS |
59 | ;; ... and in the few cases where you make a mistake and accidentally |
60 | ;; delete the region - you just undo the mistake (with C-z). | |
61 | ;; | |
62 | ;; If you really need to perform a command which starts with one of | |
63 | ;; the prefix keys even when the region is active, you have three options: | |
64 | ;; - press the prefix key twice very quickly (within 0.2 seconds), | |
1023b169 | 65 | ;; - press the prefix key and the following key within 0.2 seconds, or |
72cc582e KS |
66 | ;; - use the SHIFT key with the prefix key, i.e. C-X or C-C |
67 | ;; | |
68 | ;; This behaviour can be customized via the | |
69 | ;; cua-prefix-override-inhibit-delay variable. | |
70 | ||
71 | ;; In addition to using the shifted movement keys, you can also use | |
72 | ;; [C-space] to start the region and use unshifted movement keys to extend | |
73 | ;; it. To cancel the region, use [C-space] or [C-g]. | |
74 | ||
75 | ;; If you prefer to use the standard emacs cut, copy, paste, and undo | |
76 | ;; bindings, customize cua-enable-cua-keys to nil. | |
77 | ||
34a63289 KS |
78 | |
79 | ;; Typing text replaces the region | |
80 | ;; ------------------------------- | |
81 | ||
82 | ;; When the region is active, i.e. highlighted, the text in region is | |
83 | ;; replaced by the text you type. | |
84 | ||
85 | ;; The replaced text is saved in register 0 which can be inserted using | |
86 | ;; the key sequence M-0 C-v (see the section on register support below). | |
87 | ||
88 | ;; If you have just replaced a highlighted region with typed text, | |
89 | ;; you can repeat the replace with M-v. This will search forward | |
90 | ;; for a streach of text identical to the previous contents of the | |
91 | ;; region (i.e. the contents of register 0) and replace it with the | |
92 | ;; text you typed to replace the original region. Repeating M-v will | |
93 | ;; replace the next matching region and so on. | |
94 | ;; | |
95 | ;; Example: Suppose you have a line like this | |
96 | ;; The redo operation will redo the last redoable command | |
97 | ;; which you want to change into | |
98 | ;; The repeat operation will repeat the last repeatable command | |
99 | ;; This is done by highlighting the first occurrence of "redo" | |
100 | ;; and type "repeat" M-v M-v. | |
101 | ||
102 | ;; Note: Since CUA-mode duplicates the functionality of the | |
103 | ;; delete-selection-mode, that mode is automatically disabled when | |
104 | ;; CUA-mode is enabled. | |
105 | ||
106 | ||
72cc582e KS |
107 | ;; CUA mode indications |
108 | ;; -------------------- | |
109 | ;; You can choose to let CUA use different cursor colors to indicate | |
110 | ;; overwrite mode and read-only buffers. For example, the following | |
111 | ;; setting will use a RED cursor in normal (insertion) mode in | |
112 | ;; read-write buffers, a YELLOW cursor in overwrite mode in read-write | |
113 | ;; buffers, and a GREEN cursor read-only buffers: | |
114 | ;; | |
115 | ;; (setq cua-normal-cursor-color "red") | |
116 | ;; (setq cua-overwrite-cursor-color "yellow") | |
117 | ;; (setq cua-read-only-cursor-color "green") | |
118 | ;; | |
119 | ||
120 | ;; CUA register support | |
121 | ;; -------------------- | |
122 | ;; Emacs' standard register support is also based on a separate set of | |
123 | ;; "register commands". | |
a1506d29 | 124 | ;; |
72cc582e KS |
125 | ;; CUA's register support is activated by providing a numeric |
126 | ;; prefix argument to the C-x, C-c, and C-v commands. For example, | |
127 | ;; to copy the selected region to register 2, enter [M-2 C-c]. | |
128 | ;; Or if you have activated the keypad prefix mode, enter [kp-2 C-c]. | |
a1506d29 | 129 | ;; |
72cc582e KS |
130 | ;; And CUA will copy and paste normal region as well as rectangles |
131 | ;; into the registers, i.e. you use exactly the same command for both. | |
a1506d29 | 132 | ;; |
72cc582e KS |
133 | ;; In addition, the last highlighted text that is deleted (not |
134 | ;; copied), e.g. by [delete] or by typing text over a highlighted | |
135 | ;; region, is automatically saved in register 0, so you can insert it | |
136 | ;; using [M-0 C-v]. | |
137 | ||
138 | ;; CUA rectangle support | |
139 | ;; --------------------- | |
140 | ;; Emacs' normal rectangle support is based on interpreting the region | |
141 | ;; between the mark and point as a "virtual rectangle", and using a | |
142 | ;; completely separate set of "rectangle commands" [C-x r ...] on the | |
143 | ;; region to copy, kill, fill a.s.o. the virtual rectangle. | |
a1506d29 | 144 | ;; |
85d392cb KS |
145 | ;; cua-mode's superior rectangle support uses a true visual |
146 | ;; representation of the selected rectangle, i.e. it highlights the | |
147 | ;; actual part of the buffer that is currently selected as part of the | |
148 | ;; rectangle. Unlike emacs' traditional rectangle commands, the | |
149 | ;; selected rectangle always as straight left and right edges, even | |
150 | ;; when those are in the middle of a TAB character or beyond the end | |
151 | ;; of the current line. And it does this without actually modifying | |
152 | ;; the buffer contents (it uses display overlays to visualize the | |
153 | ;; virtual dimensions of the rectangle). | |
154 | ;; | |
155 | ;; This means that cua-mode's rectangles are not limited to the actual | |
156 | ;; contents of the buffer, so if the cursor is currently at the end of a | |
157 | ;; short line, you can still extend the rectangle to include more columns | |
158 | ;; of longer lines in the same rectangle. And you can also have the | |
159 | ;; left edge of a rectangle start in the middle of a TAB character. | |
160 | ;; Sounds strange? Try it! | |
161 | ;; | |
c9ee062d | 162 | ;; To start a rectangle, use [C-return] and extend it using the normal |
85d392cb KS |
163 | ;; movement keys (up, down, left, right, home, end, C-home, |
164 | ;; C-end). Once the rectangle has the desired size, you can cut or | |
165 | ;; copy it using C-x and C-c (or C-w and M-w), and you can | |
166 | ;; subsequently insert it - as a rectangle - using C-v (or C-y). So | |
167 | ;; the only new command you need to know to work with cua-mode | |
c9ee062d | 168 | ;; rectangles is C-return! |
72cc582e KS |
169 | ;; |
170 | ;; Normally, when you paste a rectangle using C-v (C-y), each line of | |
171 | ;; the rectangle is inserted into the existing lines in the buffer. | |
172 | ;; If overwrite-mode is active when you paste a rectangle, it is | |
173 | ;; inserted as normal (multi-line) text. | |
a1506d29 | 174 | ;; |
85d392cb KS |
175 | ;; If you prefer the traditional rectangle marking (i.e. don't want |
176 | ;; straight edges), [M-p] toggles this for the current rectangle, | |
177 | ;; or you can customize cua-virtual-rectangle-edges. | |
72cc582e KS |
178 | |
179 | ;; And there's more: If you want to extend or reduce the size of the | |
180 | ;; rectangle in one of the other corners of the rectangle, just use | |
181 | ;; [return] to move the cursor to the "next" corner. Or you can use | |
182 | ;; the [M-up], [M-down], [M-left], and [M-right] keys to move the | |
183 | ;; entire rectangle overlay (but not the contents) in the given | |
184 | ;; direction. | |
185 | ;; | |
c9ee062d | 186 | ;; [C-return] cancels the rectangle |
72cc582e KS |
187 | ;; [C-space] activates the region bounded by the rectangle |
188 | ||
189 | ;; If you type a normal (self-inserting) character when the rectangle is | |
190 | ;; active, the character is inserted on the "current side" of every line | |
191 | ;; of the rectangle. The "current side" is the side on which the cursor | |
192 | ;; is currently located. If the rectangle is only 1 column wide, | |
193 | ;; insertion will be performed to the left when the cursor is at the | |
194 | ;; bottom of the rectangle. So, for example, to comment out an entire | |
195 | ;; paragraph like this one, just place the cursor on the first character | |
196 | ;; of the first line, and enter the following: | |
c9ee062d | 197 | ;; C-return M-} ; ; <space> C-return |
a1506d29 | 198 | |
72cc582e KS |
199 | ;; cua-mode's rectangle support also includes all the normal rectangle |
200 | ;; functions with easy access: | |
201 | ;; | |
202 | ;; [M-a] aligns all words at the left edge of the rectangle | |
203 | ;; [M-b] fills the rectangle with blanks (tabs and spaces) | |
204 | ;; [M-c] closes the rectangle by removing all blanks at the left edge | |
205 | ;; of the rectangle | |
206 | ;; [M-f] fills the rectangle with a single character (prompt) | |
207 | ;; [M-i] increases the first number found on each line of the rectangle | |
208 | ;; by the amount given by the numeric prefix argument (default 1) | |
209 | ;; It recognizes 0x... as hexadecimal numbers | |
210 | ;; [M-k] kills the rectangle as normal multi-line text (for paste) | |
211 | ;; [M-l] downcases the rectangle | |
212 | ;; [M-m] copies the rectangle as normal multi-line text (for paste) | |
213 | ;; [M-n] fills each line of the rectangle with increasing numbers using | |
214 | ;; a supplied format string (prompt) | |
a1506d29 | 215 | ;; [M-o] opens the rectangle by moving the highlighted text to the |
72cc582e | 216 | ;; right of the rectangle and filling the rectangle with blanks. |
85d392cb KS |
217 | ;; [M-p] toggles virtual straight rectangle edges |
218 | ;; [M-P] inserts tabs and spaces (padding) to make real straight edges | |
72cc582e KS |
219 | ;; [M-q] performs text filling on the rectangle |
220 | ;; [M-r] replaces REGEXP (prompt) by STRING (prompt) in rectangle | |
221 | ;; [M-R] reverse the lines in the rectangle | |
222 | ;; [M-s] fills each line of the rectangle with the same STRING (prompt) | |
223 | ;; [M-t] performs text fill of the rectangle with TEXT (prompt) | |
224 | ;; [M-u] upcases the rectangle | |
225 | ;; [M-|] runs shell command on rectangle | |
226 | ;; [M-'] restricts rectangle to lines with CHAR (prompt) at left column | |
227 | ;; [M-/] restricts rectangle to lines matching REGEXP (prompt) | |
228 | ;; [C-?] Shows a brief list of the above commands. | |
229 | ||
230 | ;; [M-C-up] and [M-C-down] scrolls the lines INSIDE the rectangle up | |
231 | ;; and down; lines scrolled outside the top or bottom of the rectangle | |
232 | ;; are lost, but can be recovered using [C-z]. | |
233 | ||
234 | ;; CUA Global Mark | |
a1506d29 | 235 | ;; --------------- |
72cc582e KS |
236 | ;; The final feature provided by CUA is the "global mark", which |
237 | ;; makes it very easy to copy bits and pieces from the same and other | |
238 | ;; files into the current text. To enable and cancel the global mark, | |
239 | ;; use [S-C-space]. The cursor will blink when the global mark | |
240 | ;; is active. The following commands behave differently when the global | |
241 | ;; mark is set: | |
a1506d29 | 242 | ;; <ch> All characters (including newlines) you type are inserted |
72cc582e KS |
243 | ;; at the global mark! |
244 | ;; [C-x] If you cut a region or rectangle, it is automatically inserted | |
245 | ;; at the global mark, and the global mark is advanced. | |
246 | ;; [C-c] If you copy a region or rectangle, it is immediately inserted | |
247 | ;; at the global mark, and the global mark is advanced. | |
248 | ;; [C-v] Copies a single character to the global mark. | |
249 | ;; [C-d] Moves (i.e. deletes and inserts) a single character to the | |
250 | ;; global mark. | |
251 | ;; [backspace] deletes the character before the global mark, while | |
252 | ;; [delete] deltes the character after the global mark. | |
253 | ||
254 | ;; [S-C-space] Jumps to and cancels the global mark. | |
255 | ;; [C-u S-C-space] Cancels the global mark (stays in current buffer). | |
256 | ||
257 | ;; [TAB] Indents the current line or rectangle to the column of the | |
258 | ;; global mark. | |
259 | ||
260 | ;;; Code: | |
261 | ||
262 | ;;; Customization | |
263 | ||
264 | (defgroup cua nil | |
265 | "Emulate CUA key bindings including C-x and C-c." | |
266 | :prefix "cua" | |
267 | :group 'editing-basics | |
268 | :group 'convenience | |
269 | :group 'emulations | |
bf247b6e | 270 | :version "22.1" |
72cc582e KS |
271 | :link '(emacs-commentary-link :tag "Commentary" "cua-base.el") |
272 | :link '(emacs-library-link :tag "Lisp File" "cua-base.el")) | |
273 | ||
72cc582e KS |
274 | (defcustom cua-enable-cua-keys t |
275 | "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste. | |
276 | If the value is t, these mappings are always enabled. If the value is | |
1023b169 | 277 | `shift', these keys are only enabled if the last region was marked with |
72cc582e KS |
278 | a shifted movement key. If the value is nil, these keys are never |
279 | enabled." | |
a1506d29 | 280 | :type '(choice (const :tag "Disabled" nil) |
72cc582e | 281 | (const :tag "Shift region only" shift) |
c1cdea1a | 282 | (other :tag "Enabled" t)) |
72cc582e KS |
283 | :group 'cua) |
284 | ||
285 | (defcustom cua-highlight-region-shift-only nil | |
286 | "*If non-nil, only highlight region if marked with S-<move>. | |
287 | When this is non-nil, CUA toggles `transient-mark-mode' on when the region | |
288 | is marked using shifted movement keys, and off when the mark is cleared. | |
289 | But when the mark was set using \\[cua-set-mark], transient-mark-mode | |
290 | is not turned on." | |
291 | :type 'boolean | |
292 | :group 'cua) | |
293 | ||
a1506d29 | 294 | (defcustom cua-prefix-override-inhibit-delay |
72cc582e KS |
295 | (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) nil) |
296 | "*If non-nil, time in seconds to delay before overriding prefix key. | |
297 | If there is additional input within this time, the prefix key is | |
298 | used as a normal prefix key. So typing a key sequence quickly will | |
299 | inhibit overriding the prefix key. | |
300 | As a special case, if the prefix keys repeated within this time, the | |
301 | first prefix key is discarded, so typing a prefix key twice in quick | |
302 | succession will also inhibit overriding the prefix key. | |
303 | If the value is nil, use a shifted prefix key to inhibit the override." | |
304 | :type '(choice (number :tag "Inhibit delay") | |
305 | (const :tag "No delay" nil)) | |
306 | :group 'cua) | |
307 | ||
ae6ffe99 KS |
308 | (defcustom cua-delete-selection t |
309 | "*If non-nil, typed text replaces text in the active selection." | |
310 | :type '(choice (const :tag "Disabled" nil) | |
311 | (other :tag "Enabled" t)) | |
312 | :group 'cua) | |
313 | ||
72cc582e KS |
314 | (defcustom cua-keep-region-after-copy nil |
315 | "If non-nil, don't deselect the region after copying." | |
316 | :type 'boolean | |
317 | :group 'cua) | |
318 | ||
ae6ffe99 KS |
319 | (defcustom cua-toggle-set-mark t |
320 | "*In non-nil, the `cua-set-mark' command toggles the mark." | |
321 | :type '(choice (const :tag "Disabled" nil) | |
322 | (other :tag "Enabled" t)) | |
323 | :group 'cua) | |
324 | ||
72cc582e KS |
325 | (defcustom cua-enable-register-prefix 'not-ctrl-u |
326 | "*If non-nil, registers are supported via numeric prefix arg. | |
327 | If the value is t, any numeric prefix arg in the range 0 to 9 will be | |
a1506d29 | 328 | interpreted as a register number. |
1023b169 | 329 | If the value is `not-ctrl-u', using C-u to enter a numeric prefix is not |
a1506d29 | 330 | interpreted as a register number. |
1023b169 | 331 | If the value is `ctrl-u-only', only numeric prefix entered with C-u is |
72cc582e | 332 | interpreted as a register number." |
a1506d29 | 333 | :type '(choice (const :tag "Disabled" nil) |
72cc582e KS |
334 | (const :tag "Enabled, but C-u arg is not a register" not-ctrl-u) |
335 | (const :tag "Enabled, but only for C-u arg" ctrl-u-only) | |
c1cdea1a | 336 | (other :tag "Enabled" t)) |
72cc582e KS |
337 | :group 'cua) |
338 | ||
339 | (defcustom cua-delete-copy-to-register-0 t | |
340 | "*If non-nil, save last deleted region or rectangle to register 0." | |
341 | :type 'boolean | |
342 | :group 'cua) | |
343 | ||
72cc582e KS |
344 | (defcustom cua-enable-region-auto-help nil |
345 | "*If non-nil, automatically show help for active region." | |
346 | :type 'boolean | |
347 | :group 'cua) | |
348 | ||
349 | (defcustom cua-enable-modeline-indications nil | |
350 | "*If non-nil, use minor-mode hook to show status in mode line." | |
351 | :type 'boolean | |
352 | :group 'cua) | |
353 | ||
354 | (defcustom cua-check-pending-input t | |
355 | "*If non-nil, don't override prefix key if input pending. | |
c77d7c92 | 356 | It is rumoured that `input-pending-p' is unreliable under some window |
72cc582e KS |
357 | managers, so try setting this to nil, if prefix override doesn't work." |
358 | :type 'boolean | |
359 | :group 'cua) | |
360 | ||
361 | ||
362 | ;;; Rectangle Customization | |
363 | ||
85d392cb KS |
364 | (defcustom cua-virtual-rectangle-edges t |
365 | "*If non-nil, rectangles have virtual straight edges. | |
366 | Note that although rectangles are always DISPLAYED with straight edges, the | |
367 | buffer is NOT modified, until you execute a command that actually modifies it. | |
1023b169 | 368 | M-p toggles this feature when a rectangle is active." |
72cc582e KS |
369 | :type 'boolean |
370 | :group 'cua) | |
371 | ||
85d392cb KS |
372 | (defcustom cua-auto-tabify-rectangles 1000 |
373 | "*If non-nil, automatically tabify after rectangle commands. | |
374 | This basically means that `tabify' is applied to all lines that | |
375 | are modified by inserting or deleting a rectangle. If value is | |
c77d7c92 | 376 | an integer, CUA will look for existing tabs in a region around |
85d392cb KS |
377 | the rectangle, and only do the conversion if any tabs are already |
378 | present. The number specifies then number of characters before | |
379 | and after the region marked by the rectangle to search." | |
380 | :type '(choice (number :tag "Auto detect (limit)") | |
381 | (const :tag "Disabled" nil) | |
382 | (other :tag "Enabled" t)) | |
383 | :group 'cua) | |
384 | ||
1cb225da KS |
385 | (defcustom cua-rectangle-mark-key [(control return)] |
386 | "Global key used to toggle the cua rectangle mark." | |
387 | :set #'(lambda (symbol value) | |
388 | (set symbol value) | |
389 | (when (and (boundp 'cua--keymaps-initalized) | |
390 | cua--keymaps-initalized) | |
391 | (define-key cua-global-keymap value | |
392 | 'cua-set-rectangle-mark) | |
393 | (when (boundp 'cua--rectangle-keymap) | |
394 | (define-key cua--rectangle-keymap value | |
395 | 'cua-clear-rectangle-mark) | |
396 | (define-key cua--region-keymap value | |
397 | 'cua-toggle-rectangle-mark)))) | |
398 | :type 'key-sequence | |
399 | :group 'cua) | |
400 | ||
1638bf54 KS |
401 | (defcustom cua-rectangle-modifier-key 'meta |
402 | "*Modifier key used for rectangle commands bindings. | |
403 | On non-window systems, always use the meta modifier. | |
404 | Must be set prior to enabling CUA." | |
405 | :type '(choice (const :tag "Meta key" meta) | |
ae6ffe99 KS |
406 | (const :tag "Alt key" alt) |
407 | (const :tag "Hyper key" hyper) | |
1638bf54 KS |
408 | (const :tag "Super key" super)) |
409 | :group 'cua) | |
410 | ||
72cc582e KS |
411 | (defcustom cua-enable-rectangle-auto-help t |
412 | "*If non-nil, automatically show help for region, rectangle and global mark." | |
413 | :type 'boolean | |
414 | :group 'cua) | |
415 | ||
1e98d199 | 416 | (defface cua-rectangle |
03622614 KS |
417 | '((default :inherit region) |
418 | (((class color)) :foreground "white" :background "maroon")) | |
72cc582e KS |
419 | "*Font used by CUA for highlighting the rectangle." |
420 | :group 'cua) | |
421 | ||
1e98d199 | 422 | (defface cua-rectangle-noselect |
03622614 KS |
423 | '((default :inherit region) |
424 | (((class color)) :foreground "white" :background "dimgray")) | |
72cc582e KS |
425 | "*Font used by CUA for highlighting the non-selected rectangle lines." |
426 | :group 'cua) | |
427 | ||
72cc582e KS |
428 | |
429 | ;;; Global Mark Customization | |
430 | ||
431 | (defcustom cua-global-mark-keep-visible t | |
432 | "*If non-nil, always keep global mark visible in other window." | |
433 | :type 'boolean | |
434 | :group 'cua) | |
435 | ||
1e98d199 | 436 | (defface cua-global-mark |
fbcaad36 DN |
437 | '((((min-colors 88)(class color)) :foreground "black" :background "yellow1") |
438 | (((class color)) :foreground "black" :background "yellow") | |
03622614 | 439 | (t :bold t)) |
72cc582e KS |
440 | "*Font used by CUA for highlighting the global mark." |
441 | :group 'cua) | |
442 | ||
443 | (defcustom cua-global-mark-blink-cursor-interval 0.20 | |
444 | "*Blink cursor at this interval when global mark is active." | |
445 | :type '(choice (number :tag "Blink interval") | |
446 | (const :tag "No blink" nil)) | |
447 | :group 'cua) | |
448 | ||
449 | ||
450 | ;;; Cursor Indication Customization | |
451 | ||
cadc926b | 452 | (defcustom cua-enable-cursor-indications nil |
72cc582e KS |
453 | "*If non-nil, use different cursor colors for indications." |
454 | :type 'boolean | |
455 | :group 'cua) | |
456 | ||
496c17d6 KS |
457 | (defcustom cua-normal-cursor-color (or (and (boundp 'initial-cursor-color) initial-cursor-color) |
458 | (and (boundp 'initial-frame-alist) | |
459 | (assoc 'cursor-color initial-frame-alist) | |
460 | (cdr (assoc 'cursor-color initial-frame-alist))) | |
461 | (and (boundp 'default-frame-alist) | |
462 | (assoc 'cursor-color default-frame-alist) | |
463 | (cdr (assoc 'cursor-color default-frame-alist))) | |
464 | (frame-parameter nil 'cursor-color) | |
465 | "red") | |
72cc582e | 466 | "Normal (non-overwrite) cursor color. |
2ed2415d KS |
467 | Default is to load cursor color from initial or default frame parameters. |
468 | ||
469 | If the value is a COLOR name, then only the `cursor-color' attribute will be | |
b1227a16 | 470 | affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar), |
2ed2415d KS |
471 | then only the `cursor-type' property will be affected. If the value is |
472 | a cons (TYPE . COLOR), then both properties are affected." | |
496c17d6 | 473 | :initialize 'custom-initialize-default |
2ed2415d KS |
474 | :type '(choice |
475 | (color :tag "Color") | |
476 | (choice :tag "Type" | |
477 | (const :tag "Filled box" box) | |
478 | (const :tag "Vertical bar" bar) | |
63baed99 | 479 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 480 | (const :tag "Hollow box" hollow)) |
2ed2415d KS |
481 | (cons :tag "Color and Type" |
482 | (choice :tag "Type" | |
483 | (const :tag "Filled box" box) | |
484 | (const :tag "Vertical bar" bar) | |
63baed99 | 485 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 486 | (const :tag "Hollow box" hollow)) |
2ed2415d | 487 | (color :tag "Color"))) |
72cc582e KS |
488 | :group 'cua) |
489 | ||
490 | (defcustom cua-read-only-cursor-color "darkgreen" | |
3ffc8a8a | 491 | "*Cursor color used in read-only buffers, if non-nil. |
2ed2415d KS |
492 | Only used when `cua-enable-cursor-indications' is non-nil. |
493 | ||
494 | If the value is a COLOR name, then only the `cursor-color' attribute will be | |
b1227a16 | 495 | affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar), |
2ed2415d KS |
496 | then only the `cursor-type' property will be affected. If the value is |
497 | a cons (TYPE . COLOR), then both properties are affected." | |
498 | :type '(choice | |
499 | (color :tag "Color") | |
500 | (choice :tag "Type" | |
501 | (const :tag "Filled box" box) | |
502 | (const :tag "Vertical bar" bar) | |
63baed99 | 503 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 504 | (const :tag "Hollow box" hollow)) |
2ed2415d KS |
505 | (cons :tag "Color and Type" |
506 | (choice :tag "Type" | |
507 | (const :tag "Filled box" box) | |
508 | (const :tag "Vertical bar" bar) | |
63baed99 | 509 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 510 | (const :tag "Hollow box" hollow)) |
2ed2415d | 511 | (color :tag "Color"))) |
72cc582e KS |
512 | :group 'cua) |
513 | ||
514 | (defcustom cua-overwrite-cursor-color "yellow" | |
515 | "*Cursor color used when overwrite mode is set, if non-nil. | |
2ed2415d KS |
516 | Only used when `cua-enable-cursor-indications' is non-nil. |
517 | ||
518 | If the value is a COLOR name, then only the `cursor-color' attribute will be | |
b1227a16 | 519 | affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar), |
2ed2415d KS |
520 | then only the `cursor-type' property will be affected. If the value is |
521 | a cons (TYPE . COLOR), then both properties are affected." | |
522 | :type '(choice | |
523 | (color :tag "Color") | |
524 | (choice :tag "Type" | |
525 | (const :tag "Filled box" box) | |
526 | (const :tag "Vertical bar" bar) | |
63baed99 | 527 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 528 | (const :tag "Hollow box" hollow)) |
2ed2415d KS |
529 | (cons :tag "Color and Type" |
530 | (choice :tag "Type" | |
531 | (const :tag "Filled box" box) | |
532 | (const :tag "Vertical bar" bar) | |
63baed99 | 533 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 534 | (const :tag "Hollow box" hollow)) |
2ed2415d | 535 | (color :tag "Color"))) |
72cc582e KS |
536 | :group 'cua) |
537 | ||
538 | (defcustom cua-global-mark-cursor-color "cyan" | |
539 | "*Indication for active global mark. | |
3ffc8a8a | 540 | Will change cursor color to specified color if string. |
2ed2415d KS |
541 | Only used when `cua-enable-cursor-indications' is non-nil. |
542 | ||
543 | If the value is a COLOR name, then only the `cursor-color' attribute will be | |
b1227a16 | 544 | affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar), |
2ed2415d KS |
545 | then only the `cursor-type' property will be affected. If the value is |
546 | a cons (TYPE . COLOR), then both properties are affected." | |
547 | :type '(choice | |
548 | (color :tag "Color") | |
549 | (choice :tag "Type" | |
550 | (const :tag "Filled box" box) | |
551 | (const :tag "Vertical bar" bar) | |
63baed99 | 552 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 553 | (const :tag "Hollow box" hollow)) |
2ed2415d KS |
554 | (cons :tag "Color and Type" |
555 | (choice :tag "Type" | |
556 | (const :tag "Filled box" box) | |
557 | (const :tag "Vertical bar" bar) | |
63baed99 | 558 | (const :tag "Horizontal bar" hbar) |
b1227a16 | 559 | (const :tag "Hollow box" hollow)) |
2ed2415d | 560 | (color :tag "Color"))) |
72cc582e KS |
561 | :group 'cua) |
562 | ||
563 | ||
564 | ;;; Rectangle support is in cua-rect.el | |
565 | ||
566 | (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil) | |
567 | ||
568 | ;; Stub definitions until it is loaded | |
569 | ||
570 | (when (not (featurep 'cua-rect)) | |
571 | (defvar cua--rectangle) | |
572 | (setq cua--rectangle nil) | |
573 | (defvar cua--last-killed-rectangle) | |
574 | (setq cua--last-killed-rectangle nil)) | |
575 | ||
576 | ||
577 | ||
578 | ;;; Global Mark support is in cua-gmrk.el | |
579 | ||
4fe3f297 | 580 | (autoload 'cua-toggle-global-mark "cua-gmrk" nil t nil) |
72cc582e KS |
581 | |
582 | ;; Stub definitions until cua-gmrk.el is loaded | |
583 | ||
584 | (when (not (featurep 'cua-gmrk)) | |
585 | (defvar cua--global-mark-active) | |
586 | (setq cua--global-mark-active nil)) | |
587 | ||
588 | ||
589 | (provide 'cua-base) | |
590 | ||
591 | (eval-when-compile | |
592 | (require 'cua-rect) | |
593 | (require 'cua-gmrk) | |
594 | ) | |
595 | ||
63e6f5b3 KS |
596 | |
597 | ;;; Low-level Interface | |
598 | ||
599 | (defvar cua-inhibit-cua-keys nil | |
c77d7c92 | 600 | "Buffer-local variable that may disable the CUA keymappings.") |
63e6f5b3 KS |
601 | (make-variable-buffer-local 'cua-inhibit-cua-keys) |
602 | ||
72cc582e KS |
603 | ;;; Aux. variables |
604 | ||
605 | ;; Current region was started using cua-set-mark. | |
606 | (defvar cua--explicit-region-start nil) | |
ecd61067 | 607 | (make-variable-buffer-local 'cua--explicit-region-start) |
72cc582e KS |
608 | |
609 | ;; Latest region was started using shifted movement command. | |
610 | (defvar cua--last-region-shifted nil) | |
611 | ||
612 | ;; buffer + point prior to current command when rectangle is active | |
613 | ;; checked in post-command hook to see if point was moved | |
614 | (defvar cua--buffer-and-point-before-command nil) | |
615 | ||
616 | ;; status string for mode line indications | |
617 | (defvar cua--status-string nil) | |
ecd61067 | 618 | (make-variable-buffer-local 'cua--status-string) |
72cc582e KS |
619 | |
620 | (defvar cua--debug nil) | |
621 | ||
622 | ||
623 | ;;; Prefix key override mechanism | |
624 | ||
625 | ;; The prefix override (when mark-active) operates in three substates: | |
626 | ;; [1] Before using a prefix key | |
627 | ;; [2] Immediately after using a prefix key | |
628 | ;; [3] A fraction of a second later | |
629 | ||
630 | ;; In state [1], the cua--prefix-override-keymap is active. | |
631 | ;; This keymap binds the C-x and C-c prefix keys to the | |
632 | ;; cua--prefix-override-handler function. | |
633 | ||
634 | ;; When a prefix key is typed in state [1], cua--prefix-override-handler | |
635 | ;; will push back the keys already read to the event queue. If input is | |
636 | ;; pending, it changes directly to state [3]. Otherwise, a short timer [T] | |
637 | ;; is started, and it changes to state [2]. | |
638 | ||
639 | ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the | |
640 | ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x | |
641 | ;; C-x to the cua--prefix-repeat-handler function. | |
642 | ||
643 | ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler | |
644 | ;; will cancel [T], back the keys already read (except for the second prefix | |
645 | ;; keys) to the event queue, and changes to state [3]. | |
646 | ||
647 | ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and | |
648 | ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2], | |
649 | ;; the cua--prefix-override-timeout function will push a `timeout' event on | |
650 | ;; the event queue, and changes to state [3]. | |
651 | ||
652 | ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap | |
653 | ;; are inactive, so the timeout in cua-global-keymap binding is used, or the | |
654 | ;; normal prefix key binding from the global or local map will be used. | |
655 | ||
656 | ;; The pre-command hook (executed as a consequence of the timeout or normal | |
657 | ;; prefix key binding) will cancel [T] and change from state [3] back to | |
658 | ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler | |
659 | ;; are always called with state reset to [1]! | |
660 | ||
661 | ;; State [1] is recognized by cua--prefix-override-timer is nil, | |
662 | ;; state [2] is recognized by cua--prefix-override-timer is a timer, and | |
663 | ;; state [3] is recognized by cua--prefix-override-timer is t. | |
664 | ||
665 | (defvar cua--prefix-override-timer nil) | |
666 | (defvar cua--prefix-override-length nil) | |
667 | ||
668 | (defun cua--prefix-override-replay (arg repeat) | |
669 | (let* ((keys (this-command-keys)) | |
670 | (i (length keys)) | |
671 | (key (aref keys (1- i)))) | |
672 | (setq cua--prefix-override-length (- i repeat)) | |
673 | (setq cua--prefix-override-timer | |
674 | (or | |
675 | ;; In state [2], change to state [3] | |
676 | (> repeat 0) | |
677 | ;; In state [1], change directly to state [3] | |
678 | (and cua-check-pending-input (input-pending-p)) | |
679 | ;; In state [1], [T] disabled, so change to state [3] | |
680 | (not (numberp cua-prefix-override-inhibit-delay)) | |
681 | (<= cua-prefix-override-inhibit-delay 0) | |
682 | ;; In state [1], start [T] and change to state [2] | |
a1506d29 | 683 | (run-with-timer cua-prefix-override-inhibit-delay nil |
72cc582e KS |
684 | 'cua--prefix-override-timeout))) |
685 | ;; Don't record this command | |
686 | (setq this-command last-command) | |
687 | ;; Restore the prefix arg | |
688 | (setq prefix-arg arg) | |
689 | (reset-this-command-lengths) | |
690 | ;; Push the key back on the event queue | |
691 | (setq unread-command-events (cons key unread-command-events)))) | |
692 | ||
693 | (defun cua--prefix-override-handler (arg) | |
694 | "Start timer waiting for prefix key to be followed by another key. | |
695 | Repeating prefix key when region is active works as a single prefix key." | |
696 | (interactive "P") | |
697 | (cua--prefix-override-replay arg 0)) | |
698 | ||
699 | (defun cua--prefix-repeat-handler (arg) | |
700 | "Repeating prefix key when region is active works as a single prefix key." | |
701 | (interactive "P") | |
702 | (cua--prefix-override-replay arg 1)) | |
703 | ||
704 | (defun cua--prefix-copy-handler (arg) | |
705 | "Copy region/rectangle, then replay last key." | |
706 | (interactive "P") | |
707 | (if cua--rectangle | |
708 | (cua-copy-rectangle arg) | |
709 | (cua-copy-region arg)) | |
710 | (let ((keys (this-single-command-keys))) | |
a1506d29 | 711 | (setq unread-command-events |
72cc582e KS |
712 | (cons (aref keys (1- (length keys))) unread-command-events)))) |
713 | ||
714 | (defun cua--prefix-cut-handler (arg) | |
715 | "Cut region/rectangle, then replay last key." | |
716 | (interactive "P") | |
717 | (if cua--rectangle | |
718 | (cua-cut-rectangle arg) | |
719 | (cua-cut-region arg)) | |
720 | (let ((keys (this-single-command-keys))) | |
a1506d29 | 721 | (setq unread-command-events |
72cc582e KS |
722 | (cons (aref keys (1- (length keys))) unread-command-events)))) |
723 | ||
724 | (defun cua--prefix-override-timeout () | |
725 | (setq cua--prefix-override-timer t) | |
726 | (when (= (length (this-command-keys)) cua--prefix-override-length) | |
727 | (setq unread-command-events (cons 'timeout unread-command-events)) | |
728 | (if prefix-arg | |
729 | (reset-this-command-lengths) | |
730 | (setq overriding-terminal-local-map nil)) | |
a23a8440 | 731 | (cua--select-keymaps))) |
72cc582e KS |
732 | |
733 | ||
734 | ;;; Aux. functions | |
735 | ||
736 | (defun cua--fallback () | |
737 | ;; Execute original command | |
738 | (setq this-command this-original-command) | |
739 | (call-interactively this-command)) | |
a1506d29 | 740 | |
72cc582e KS |
741 | (defun cua--keep-active () |
742 | (setq mark-active t | |
743 | deactivate-mark nil)) | |
744 | ||
745 | (defun cua--deactivate (&optional now) | |
746 | (setq cua--explicit-region-start nil) | |
747 | (if (not now) | |
748 | (setq deactivate-mark t) | |
749 | (setq mark-active nil) | |
750 | (run-hooks 'deactivate-mark-hook))) | |
751 | ||
752 | ||
753 | ;; The current register prefix | |
754 | (defvar cua--register nil) | |
755 | ||
756 | (defun cua--prefix-arg (arg) | |
a1506d29 | 757 | (setq cua--register |
72cc582e | 758 | (and cua-enable-register-prefix |
72cc582e | 759 | (integerp arg) (>= arg 0) (< arg 10) |
1c62c12f KS |
760 | (let* ((prefix (aref (this-command-keys) 0)) |
761 | (ctrl-u-prefix (and (integerp prefix) | |
5632c006 | 762 | (= prefix ?\C-u)))) |
a1506d29 | 763 | (cond |
5632c006 KS |
764 | ((eq cua-enable-register-prefix 'not-ctrl-u) |
765 | (not ctrl-u-prefix)) | |
766 | ((eq cua-enable-register-prefix 'ctrl-u-only) | |
767 | ctrl-u-prefix) | |
768 | (t t))) | |
72cc582e KS |
769 | (+ arg ?0))) |
770 | (if cua--register nil arg)) | |
771 | ||
72cc582e KS |
772 | |
773 | ;;; Region specific commands | |
774 | ||
5834ac92 KS |
775 | (defvar cua--last-deleted-region-pos nil) |
776 | (defvar cua--last-deleted-region-text nil) | |
777 | ||
72cc582e KS |
778 | (defun cua-delete-region () |
779 | "Delete the active region. | |
780 | Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil." | |
781 | (interactive) | |
782 | (let ((start (mark)) (end (point))) | |
783 | (or (<= start end) | |
784 | (setq start (prog1 end (setq end start)))) | |
cf211404 | 785 | (setq cua--last-deleted-region-text (filter-buffer-substring start end)) |
72cc582e | 786 | (if cua-delete-copy-to-register-0 |
5834ac92 | 787 | (set-register ?0 cua--last-deleted-region-text)) |
72cc582e | 788 | (delete-region start end) |
a1506d29 | 789 | (setq cua--last-deleted-region-pos |
5834ac92 KS |
790 | (cons (current-buffer) |
791 | (and (consp buffer-undo-list) | |
792 | (car buffer-undo-list)))) | |
ecd61067 KS |
793 | (cua--deactivate) |
794 | (/= start end))) | |
72cc582e KS |
795 | |
796 | (defun cua-replace-region () | |
797 | "Replace the active region with the character you type." | |
798 | (interactive) | |
ae6ffe99 | 799 | (let ((not-empty (and cua-delete-selection (cua-delete-region)))) |
ecd61067 KS |
800 | (unless (eq this-original-command this-command) |
801 | (let ((overwrite-mode | |
802 | (and overwrite-mode | |
803 | not-empty | |
804 | (not (eq this-original-command 'self-insert-command))))) | |
805 | (cua--fallback))))) | |
72cc582e KS |
806 | |
807 | (defun cua-copy-region (arg) | |
808 | "Copy the region to the kill ring. | |
809 | With numeric prefix arg, copy to register 0-9 instead." | |
810 | (interactive "P") | |
811 | (setq arg (cua--prefix-arg arg)) | |
812 | (setq cua--last-killed-rectangle nil) | |
813 | (let ((start (mark)) (end (point))) | |
814 | (or (<= start end) | |
815 | (setq start (prog1 end (setq end start)))) | |
6a350b06 KS |
816 | (cond |
817 | (cua--register | |
818 | (copy-to-register cua--register start end nil)) | |
819 | ((eq this-original-command 'clipboard-kill-ring-save) | |
820 | (clipboard-kill-ring-save start end)) | |
821 | (t | |
822 | (copy-region-as-kill start end))) | |
72cc582e KS |
823 | (if cua-keep-region-after-copy |
824 | (cua--keep-active) | |
825 | (cua--deactivate)))) | |
826 | ||
827 | (defun cua-cut-region (arg) | |
828 | "Cut the region and copy to the kill ring. | |
829 | With numeric prefix arg, copy to register 0-9 instead." | |
830 | (interactive "P") | |
831 | (setq cua--last-killed-rectangle nil) | |
832 | (if buffer-read-only | |
833 | (cua-copy-region arg) | |
834 | (setq arg (cua--prefix-arg arg)) | |
835 | (let ((start (mark)) (end (point))) | |
836 | (or (<= start end) | |
837 | (setq start (prog1 end (setq end start)))) | |
6a350b06 KS |
838 | (cond |
839 | (cua--register | |
840 | (copy-to-register cua--register start end t)) | |
841 | ((eq this-original-command 'clipboard-kill-region) | |
842 | (clipboard-kill-region start end)) | |
843 | (t | |
844 | (kill-region start end)))) | |
72cc582e KS |
845 | (cua--deactivate))) |
846 | ||
847 | ;;; Generic commands for regions, rectangles, and global marks | |
848 | ||
849 | (defun cua-cancel () | |
850 | "Cancel the active region, rectangle, or global mark." | |
851 | (interactive) | |
852 | (setq mark-active nil) | |
853 | (setq cua--explicit-region-start nil) | |
854 | (if (fboundp 'cua--cancel-rectangle) | |
855 | (cua--cancel-rectangle))) | |
856 | ||
857 | (defun cua-paste (arg) | |
858 | "Paste last cut or copied region or rectangle. | |
859 | An active region is deleted before executing the command. | |
860 | With numeric prefix arg, paste from register 0-9 instead. | |
861 | If global mark is active, copy from register or one character." | |
862 | (interactive "P") | |
863 | (setq arg (cua--prefix-arg arg)) | |
864 | (let ((regtxt (and cua--register (get-register cua--register))) | |
85d392cb KS |
865 | (count (prefix-numeric-value arg)) |
866 | paste-column paste-lines) | |
72cc582e KS |
867 | (cond |
868 | ((and cua--register (not regtxt)) | |
869 | (message "Nothing in register %c" cua--register)) | |
870 | (cua--global-mark-active | |
871 | (if regtxt | |
872 | (cua--insert-at-global-mark regtxt) | |
873 | (when (not (eobp)) | |
cf211404 | 874 | (cua--insert-at-global-mark (filter-buffer-substring (point) (+ (point) count))) |
72cc582e KS |
875 | (forward-char count)))) |
876 | (buffer-read-only | |
877 | (message "Cannot paste into a read-only buffer")) | |
878 | (t | |
879 | ;; Must save register here, since delete may override reg 0. | |
880 | (if mark-active | |
881 | ;; Before a yank command, make sure we don't yank | |
882 | ;; the same region that we are going to delete. | |
883 | ;; That would make yank a no-op. | |
884 | (if cua--rectangle | |
85d392cb KS |
885 | (progn |
886 | (goto-char (min (mark) (point))) | |
887 | (setq paste-column (cua--rectangle-left)) | |
888 | (setq paste-lines (cua--delete-rectangle)) | |
889 | (if (= paste-lines 1) | |
890 | (setq paste-lines nil))) ;; paste all | |
cf211404 | 891 | (if (string= (filter-buffer-substring (point) (mark)) |
72cc582e KS |
892 | (car kill-ring)) |
893 | (current-kill 1)) | |
894 | (cua-delete-region))) | |
895 | (cond | |
896 | (regtxt | |
897 | (cond | |
898 | ((consp regtxt) (cua--insert-rectangle regtxt)) | |
899 | ((stringp regtxt) (insert-for-yank regtxt)) | |
900 | (t (message "Unknown data in register %c" cua--register)))) | |
901 | ((and cua--last-killed-rectangle | |
902 | (eq (and kill-ring (car kill-ring)) (car cua--last-killed-rectangle))) | |
903 | (let ((pt (point))) | |
904 | (when (not (eq buffer-undo-list t)) | |
905 | (setq this-command 'cua--paste-rectangle) | |
906 | (undo-boundary) | |
907 | (setq buffer-undo-list (cons pt buffer-undo-list))) | |
85d392cb KS |
908 | (cua--insert-rectangle (cdr cua--last-killed-rectangle) |
909 | nil paste-column paste-lines) | |
72cc582e | 910 | (if arg (goto-char pt)))) |
6a350b06 KS |
911 | ((eq this-original-command 'clipboard-yank) |
912 | (clipboard-yank)) | |
72cc582e KS |
913 | (t (yank arg))))))) |
914 | ||
915 | (defun cua-paste-pop (arg) | |
916 | "Replace a just-pasted text or rectangle with a different text. | |
917 | See `yank-pop' for details." | |
918 | (interactive "P") | |
919 | (if (eq last-command 'cua--paste-rectangle) | |
920 | (progn | |
921 | (undo) | |
922 | (yank arg)) | |
923 | (yank-pop (prefix-numeric-value arg)))) | |
924 | ||
925 | (defun cua-exchange-point-and-mark (arg) | |
926 | "Exchanges point and mark, but don't activate the mark. | |
927 | Activates the mark if a prefix argument is given." | |
928 | (interactive "P") | |
929 | (if arg | |
930 | (setq mark-active t) | |
931 | (let (mark-active) | |
932 | (exchange-point-and-mark) | |
933 | (if cua--rectangle | |
934 | (cua--rectangle-corner 0))))) | |
935 | ||
34a63289 KS |
936 | ;; Typed text that replaced the highlighted region. |
937 | (defvar cua--repeat-replace-text nil) | |
938 | ||
939 | (defun cua-repeat-replace-region (arg) | |
940 | "Repeat replacing text of highlighted region with typed text. | |
c77d7c92 JB |
941 | Searches for the next stretch of text identical to the region last |
942 | replaced by typing text over it and replaces it with the same stretch | |
5834ac92 | 943 | of text." |
34a63289 | 944 | (interactive "P") |
5834ac92 KS |
945 | (when cua--last-deleted-region-pos |
946 | (save-excursion | |
947 | (save-restriction | |
948 | (set-buffer (car cua--last-deleted-region-pos)) | |
949 | (widen) | |
950 | ;; Find the text that replaced the region via the undo list. | |
951 | (let ((ul buffer-undo-list) | |
952 | (elt (cdr cua--last-deleted-region-pos)) | |
953 | u s e) | |
954 | (when elt | |
955 | (while (consp ul) | |
956 | (setq u (car ul) ul (cdr ul)) | |
957 | (cond | |
958 | ((eq u elt) ;; got it | |
959 | (setq ul nil)) | |
960 | ((and (consp u) (integerp (car u)) (integerp (cdr u))) | |
961 | (if (and s (= (cdr u) s)) | |
962 | (setq s (car u)) | |
963 | (setq s (car u) e (cdr u))))))) | |
964 | (setq cua--repeat-replace-text | |
965 | (cond ((and s e (<= s e) (= s (mark t))) | |
cf211404 | 966 | (filter-buffer-substring s e nil t)) |
5834ac92 KS |
967 | ((and (null s) (eq u elt)) ;; nothing inserted |
968 | "") | |
969 | (t | |
970 | (message "Cannot locate replacement text") | |
971 | nil)))))) | |
972 | (setq cua--last-deleted-region-pos nil)) | |
973 | (if (and cua--last-deleted-region-text | |
974 | cua--repeat-replace-text | |
975 | (search-forward cua--last-deleted-region-text nil t nil)) | |
976 | (replace-match cua--repeat-replace-text arg t))) | |
34a63289 | 977 | |
72cc582e KS |
978 | (defun cua-help-for-region (&optional help) |
979 | "Show region specific help in echo area." | |
980 | (interactive) | |
a1506d29 | 981 | (message |
72cc582e KS |
982 | (concat (if help "C-?:help " "") |
983 | "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect"))) | |
984 | ||
985 | ||
986 | ;;; Shift activated / extended region | |
987 | ||
988 | (defun cua-set-mark (&optional arg) | |
989 | "Set mark at where point is, clear mark, or jump to mark. | |
f45a6831 KS |
990 | |
991 | With no prefix argument, clear mark if already set. Otherwise, set | |
992 | mark, and push old mark position on local mark ring; also push mark on | |
3ffc8a8a | 993 | global mark ring if last mark was set in another buffer. |
f45a6831 KS |
994 | |
995 | With argument, jump to mark, and pop a new position for mark off | |
996 | the local mark ring \(this does not affect the global mark ring\). | |
997 | Use \\[pop-global-mark] to jump to a mark off the global mark ring | |
ce92443f KS |
998 | \(see `pop-global-mark'\). |
999 | ||
1000 | Repeating the command without the prefix jumps to the next position | |
1001 | off the local \(or global\) mark ring. | |
f45a6831 KS |
1002 | |
1003 | With a double \\[universal-argument] prefix argument, unconditionally set mark." | |
72cc582e | 1004 | (interactive "P") |
72cc582e | 1005 | (cond |
f45a6831 KS |
1006 | ((and (consp arg) (> (prefix-numeric-value arg) 4)) |
1007 | (push-mark-command nil)) | |
fc803e8d | 1008 | ((eq last-command 'pop-to-mark-command) |
f45a6831 KS |
1009 | (setq this-command 'pop-to-mark-command) |
1010 | (pop-to-mark-command)) | |
ce92443f | 1011 | ((and (eq last-command 'pop-global-mark) (not arg)) |
f45a6831 KS |
1012 | (setq this-command 'pop-global-mark) |
1013 | (pop-global-mark)) | |
72cc582e | 1014 | (arg |
fc803e8d KS |
1015 | (setq this-command 'pop-to-mark-command) |
1016 | (pop-to-mark-command)) | |
ae6ffe99 | 1017 | ((and cua-toggle-set-mark mark-active) |
72cc582e KS |
1018 | (cua--deactivate) |
1019 | (message "Mark Cleared")) | |
1020 | (t | |
fc803e8d | 1021 | (push-mark-command nil nil) |
72cc582e KS |
1022 | (setq cua--explicit-region-start t) |
1023 | (setq cua--last-region-shifted nil) | |
1024 | (if cua-enable-region-auto-help | |
1025 | (cua-help-for-region t))))) | |
1026 | ||
82851d10 KS |
1027 | ;;; Scrolling commands which does not signal errors at top/bottom |
1028 | ;;; of buffer at first key-press (instead moves to top/bottom | |
1029 | ;;; of buffer). | |
1030 | ||
1031 | (defun cua-scroll-up (&optional arg) | |
1032 | "Scroll text of current window upward ARG lines; or near full screen if no ARG. | |
1033 | If window cannot be scrolled further, move cursor to bottom line instead. | |
1034 | A near full screen is `next-screen-context-lines' less than a full screen. | |
1035 | Negative ARG means scroll downward. | |
1036 | If ARG is the atom `-', scroll downward by nearly full screen." | |
1037 | (interactive "P") | |
1038 | (cond | |
1039 | ((eq arg '-) (cua-scroll-down nil)) | |
1040 | ((< (prefix-numeric-value arg) 0) | |
1041 | (cua-scroll-down (- (prefix-numeric-value arg)))) | |
1042 | ((eobp) | |
1043 | (scroll-up arg)) ; signal error | |
1044 | (t | |
1045 | (condition-case nil | |
1046 | (scroll-up arg) | |
1047 | (end-of-buffer (goto-char (point-max))))))) | |
1048 | ||
4905133f KS |
1049 | (put 'cua-scroll-up 'CUA 'move) |
1050 | ||
82851d10 KS |
1051 | (defun cua-scroll-down (&optional arg) |
1052 | "Scroll text of current window downward ARG lines; or near full screen if no ARG. | |
1053 | If window cannot be scrolled further, move cursor to top line instead. | |
1054 | A near full screen is `next-screen-context-lines' less than a full screen. | |
1055 | Negative ARG means scroll upward. | |
1056 | If ARG is the atom `-', scroll upward by nearly full screen." | |
1057 | (interactive "P") | |
1058 | (cond | |
1059 | ((eq arg '-) (cua-scroll-up nil)) | |
1060 | ((< (prefix-numeric-value arg) 0) | |
1061 | (cua-scroll-up (- (prefix-numeric-value arg)))) | |
1062 | ((bobp) | |
1063 | (scroll-down arg)) ; signal error | |
1064 | (t | |
1065 | (condition-case nil | |
1066 | (scroll-down arg) | |
1067 | (beginning-of-buffer (goto-char (point-min))))))) | |
1068 | ||
ec503c28 | 1069 | (put 'cua-scroll-down 'CUA 'move) |
4905133f | 1070 | |
72cc582e KS |
1071 | ;;; Cursor indications |
1072 | ||
1073 | (defun cua--update-indications () | |
2ed2415d KS |
1074 | (let* ((cursor |
1075 | (cond | |
1076 | ((and cua--global-mark-active | |
1077 | cua-global-mark-cursor-color) | |
1078 | cua-global-mark-cursor-color) | |
1079 | ((and buffer-read-only | |
1080 | cua-read-only-cursor-color) | |
1081 | cua-read-only-cursor-color) | |
85d392cb | 1082 | ((and cua-overwrite-cursor-color overwrite-mode) |
2ed2415d KS |
1083 | cua-overwrite-cursor-color) |
1084 | (t cua-normal-cursor-color))) | |
1085 | (color (if (consp cursor) (cdr cursor) cursor)) | |
1086 | (type (if (consp cursor) (car cursor) cursor))) | |
1087 | (if (and color | |
1088 | (stringp color) | |
1089 | (not (equal color (frame-parameter nil 'cursor-color)))) | |
1090 | (set-cursor-color color)) | |
1091 | (if (and type | |
1092 | (symbolp type) | |
6aa06cd8 | 1093 | (not (eq type default-cursor-type))) |
2ed2415d | 1094 | (setq default-cursor-type type)))) |
72cc582e KS |
1095 | |
1096 | ||
1097 | ;;; Pre-command hook | |
1098 | ||
01096a7b | 1099 | (defun cua--pre-command-handler-1 () |
53c11b7e KS |
1100 | ;; Cancel prefix key timeout if user enters another key. |
1101 | (when cua--prefix-override-timer | |
1102 | (if (timerp cua--prefix-override-timer) | |
1103 | (cancel-timer cua--prefix-override-timer)) | |
1104 | (setq cua--prefix-override-timer nil)) | |
1105 | ||
1106 | (cond | |
1107 | ;; Only symbol commands can have necessary properties | |
1108 | ((not (symbolp this-command)) | |
1109 | nil) | |
1110 | ||
1111 | ;; Handle delete-selection property on non-movement commands | |
1112 | ((not (eq (get this-command 'CUA) 'move)) | |
1113 | (when (and mark-active (not deactivate-mark)) | |
1114 | (let* ((ds (or (get this-command 'delete-selection) | |
1115 | (get this-command 'pending-delete))) | |
1116 | (nc (cond | |
1117 | ((not ds) nil) | |
1118 | ((eq ds 'yank) | |
1119 | 'cua-paste) | |
1120 | ((eq ds 'kill) | |
1121 | (if cua--rectangle | |
1122 | 'cua-copy-rectangle | |
1123 | 'cua-copy-region)) | |
1124 | ((eq ds 'supersede) | |
1125 | (if cua--rectangle | |
1126 | 'cua-delete-rectangle | |
1127 | 'cua-delete-region)) | |
1128 | (t | |
1129 | (if cua--rectangle | |
1130 | 'cua-delete-rectangle ;; replace? | |
1131 | 'cua-replace-region))))) | |
1132 | (if nc | |
1133 | (setq this-original-command this-command | |
1134 | this-command nc))))) | |
1135 | ||
1136 | ;; Handle shifted cursor keys and other movement commands. | |
1137 | ;; If region is not active, region is activated if key is shifted. | |
1138 | ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC). | |
1139 | ;; If rectangle is active, expand rectangle in specified direction and ignore the movement. | |
1140 | ((if window-system | |
1141 | (memq 'shift (event-modifiers | |
1142 | (aref (this-single-command-raw-keys) 0))) | |
1143 | (or | |
1144 | (memq 'shift (event-modifiers | |
1145 | (aref (this-single-command-keys) 0))) | |
1146 | ;; See if raw escape sequence maps to a shifted event, e.g. S-up or C-S-home. | |
1147 | (and (boundp 'function-key-map) | |
1148 | function-key-map | |
1149 | (let ((ev (lookup-key function-key-map | |
1150 | (this-single-command-raw-keys)))) | |
1151 | (and (vector ev) | |
1152 | (symbolp (setq ev (aref ev 0))) | |
1153 | (string-match "S-" (symbol-name ev))))))) | |
1154 | (unless mark-active | |
1155 | (push-mark-command nil t)) | |
1156 | (setq cua--last-region-shifted t) | |
1157 | (setq cua--explicit-region-start nil)) | |
1158 | ||
1159 | ;; Set mark if user explicitly said to do so | |
1160 | ((or cua--explicit-region-start cua--rectangle) | |
1161 | (unless mark-active | |
1162 | (push-mark-command nil nil))) | |
1163 | ||
1164 | ;; Else clear mark after this command. | |
1165 | (t | |
1166 | ;; If we set mark-active to nil here, the region highlight will not be | |
1167 | ;; removed by the direct_output_ commands. | |
1168 | (setq deactivate-mark t))) | |
1169 | ||
1170 | ;; Detect extension of rectangles by mouse or other movement | |
1171 | (setq cua--buffer-and-point-before-command | |
1172 | (if cua--rectangle (cons (current-buffer) (point))))) | |
01096a7b | 1173 | |
72cc582e | 1174 | (defun cua--pre-command-handler () |
01096a7b KS |
1175 | (when cua-mode |
1176 | (condition-case nil | |
1177 | (cua--pre-command-handler-1) | |
1178 | (error nil)))) | |
72cc582e KS |
1179 | |
1180 | ;;; Post-command hook | |
1181 | ||
01096a7b KS |
1182 | (defun cua--post-command-handler-1 () |
1183 | (when cua--global-mark-active | |
1184 | (cua--global-mark-post-command)) | |
1185 | (when (fboundp 'cua--rectangle-post-command) | |
1186 | (cua--rectangle-post-command)) | |
1187 | (setq cua--buffer-and-point-before-command nil) | |
1188 | (if (or (not mark-active) deactivate-mark) | |
1189 | (setq cua--explicit-region-start nil)) | |
1190 | ||
1191 | ;; Debugging | |
1192 | (if cua--debug | |
1193 | (cond | |
1194 | (cua--rectangle (cua--rectangle-assert)) | |
1195 | (mark-active (message "Mark=%d Point=%d Expl=%s" | |
1196 | (mark t) (point) cua--explicit-region-start)))) | |
72cc582e | 1197 | |
01096a7b KS |
1198 | ;; Disable transient-mark-mode if rectangle active in current buffer. |
1199 | (if (not (window-minibuffer-p (selected-window))) | |
1200 | (setq transient-mark-mode (and (not cua--rectangle) | |
1201 | (if cua-highlight-region-shift-only | |
1202 | (not cua--explicit-region-start) | |
1203 | t)))) | |
1204 | (if cua-enable-cursor-indications | |
1205 | (cua--update-indications)) | |
72cc582e | 1206 | |
01096a7b KS |
1207 | (cua--select-keymaps)) |
1208 | ||
1209 | (defun cua--post-command-handler () | |
1210 | (when cua-mode | |
1211 | (condition-case nil | |
1212 | (cua--post-command-handler-1) | |
1213 | (error nil)))) | |
72cc582e KS |
1214 | |
1215 | ||
1216 | ;;; Keymaps | |
1217 | ||
1638bf54 KS |
1218 | ;; Cached value of actual cua-rectangle-modifier-key |
1219 | (defvar cua--rectangle-modifier-key 'meta) | |
1220 | ||
72cc582e KS |
1221 | (defun cua--M/H-key (map key fct) |
1222 | ;; bind H-KEY or M-KEY to FCT in MAP | |
72cc582e | 1223 | (unless (listp key) (setq key (list key))) |
1638bf54 | 1224 | (define-key map (vector (cons cua--rectangle-modifier-key key)) fct)) |
72cc582e | 1225 | |
20023b9c KS |
1226 | (defun cua--self-insert-char-p (def) |
1227 | ;; Return DEF if current key sequence is self-inserting in | |
1228 | ;; global-map. | |
1229 | (if (memq (global-key-binding (this-single-command-keys)) | |
1230 | '(self-insert-command self-insert-iso)) | |
1231 | def nil)) | |
1232 | ||
a23a8440 KS |
1233 | (defvar cua-global-keymap (make-sparse-keymap) |
1234 | "Global keymap for cua-mode; users may add to this keymap.") | |
1235 | ||
72cc582e KS |
1236 | (defvar cua--cua-keys-keymap (make-sparse-keymap)) |
1237 | (defvar cua--prefix-override-keymap (make-sparse-keymap)) | |
1238 | (defvar cua--prefix-repeat-keymap (make-sparse-keymap)) | |
1239 | (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded | |
1240 | (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded | |
1241 | (defvar cua--region-keymap (make-sparse-keymap)) | |
1242 | ||
1243 | (defvar cua--ena-cua-keys-keymap nil) | |
1244 | (defvar cua--ena-prefix-override-keymap nil) | |
1245 | (defvar cua--ena-prefix-repeat-keymap nil) | |
1246 | (defvar cua--ena-region-keymap nil) | |
1247 | (defvar cua--ena-global-mark-keymap nil) | |
1248 | ||
a23a8440 KS |
1249 | (defvar cua--keymap-alist |
1250 | `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap) | |
1251 | (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap) | |
1252 | (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap) | |
1253 | (cua--ena-global-mark-keymap . ,cua--global-mark-keymap) | |
1254 | (cua--rectangle . ,cua--rectangle-keymap) | |
1255 | (cua--ena-region-keymap . ,cua--region-keymap) | |
1256 | (cua-mode . ,cua-global-keymap))) | |
1257 | ||
1258 | (defun cua--select-keymaps () | |
1259 | ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist. | |
72cc582e KS |
1260 | (setq cua--ena-region-keymap |
1261 | (and mark-active (not deactivate-mark))) | |
1262 | (setq cua--ena-prefix-override-keymap | |
1263 | (and cua--ena-region-keymap | |
1264 | cua-enable-cua-keys | |
63e6f5b3 | 1265 | (not cua-inhibit-cua-keys) |
72cc582e KS |
1266 | (or (eq cua-enable-cua-keys t) |
1267 | (not cua--explicit-region-start)) | |
1268 | (not executing-kbd-macro) | |
1269 | (not cua--prefix-override-timer))) | |
1270 | (setq cua--ena-prefix-repeat-keymap | |
1271 | (and cua--ena-region-keymap | |
186bccc3 KS |
1272 | (or (timerp cua--prefix-override-timer) |
1273 | (eq cua--prefix-override-timer 'shift)))) | |
72cc582e KS |
1274 | (setq cua--ena-cua-keys-keymap |
1275 | (and cua-enable-cua-keys | |
63e6f5b3 | 1276 | (not cua-inhibit-cua-keys) |
72cc582e KS |
1277 | (or (eq cua-enable-cua-keys t) |
1278 | cua--last-region-shifted))) | |
1279 | (setq cua--ena-global-mark-keymap | |
1280 | (and cua--global-mark-active | |
1281 | (not (window-minibuffer-p))))) | |
1282 | ||
1283 | (defvar cua--keymaps-initalized nil) | |
1284 | ||
186bccc3 KS |
1285 | (defun cua--shift-control-prefix (prefix arg) |
1286 | ;; handle S-C-x and S-C-c by emulating the fast double prefix function. | |
1287 | ;; Don't record this command | |
1288 | (setq this-command last-command) | |
1289 | ;; Restore the prefix arg | |
1290 | (setq prefix-arg arg) | |
1291 | (reset-this-command-lengths) | |
1292 | ;; Activate the cua--prefix-repeat-keymap | |
1293 | (setq cua--prefix-override-timer 'shift) | |
1294 | ;; Push duplicate keys back on the event queue | |
1295 | (setq unread-command-events (cons prefix (cons prefix unread-command-events)))) | |
1296 | ||
1297 | (defun cua--shift-control-c-prefix (arg) | |
1298 | (interactive "P") | |
1299 | (cua--shift-control-prefix ?\C-c arg)) | |
1300 | ||
1301 | (defun cua--shift-control-x-prefix (arg) | |
1302 | (interactive "P") | |
1303 | (cua--shift-control-prefix ?\C-x arg)) | |
1304 | ||
72cc582e | 1305 | (defun cua--init-keymaps () |
1638bf54 KS |
1306 | ;; Cache actual rectangle modifier key. |
1307 | (setq cua--rectangle-modifier-key | |
1308 | (if (and cua-rectangle-modifier-key | |
1309 | (memq window-system '(x))) | |
1310 | cua-rectangle-modifier-key | |
1311 | 'meta)) | |
1312 | ;; C-return always toggles rectangle mark | |
1cb225da | 1313 | (define-key cua-global-keymap cua-rectangle-mark-key 'cua-set-rectangle-mark) |
1638bf54 KS |
1314 | (unless (eq cua--rectangle-modifier-key 'meta) |
1315 | (cua--M/H-key cua-global-keymap ?\s 'cua-set-rectangle-mark) | |
1316 | (define-key cua-global-keymap | |
1317 | (vector (list cua--rectangle-modifier-key 'mouse-1)) 'cua-mouse-set-rectangle-mark)) | |
72cc582e | 1318 | |
1023b169 | 1319 | (define-key cua-global-keymap [(shift control ?\s)] 'cua-toggle-global-mark) |
72cc582e KS |
1320 | |
1321 | ;; replace region with rectangle or element on kill ring | |
1322 | (define-key cua-global-keymap [remap yank] 'cua-paste) | |
1323 | (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste) | |
1324 | ;; replace current yank with previous kill ring element | |
1325 | (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop) | |
1326 | ;; set mark | |
1327 | (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark) | |
72cc582e | 1328 | |
82851d10 KS |
1329 | ;; scrolling |
1330 | (define-key cua-global-keymap [remap scroll-up] 'cua-scroll-up) | |
1331 | (define-key cua-global-keymap [remap scroll-down] 'cua-scroll-down) | |
1332 | ||
72cc582e | 1333 | (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region) |
72cc582e | 1334 | (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill) |
72cc582e KS |
1335 | (define-key cua--cua-keys-keymap [(control z)] 'undo) |
1336 | (define-key cua--cua-keys-keymap [(control v)] 'yank) | |
34a63289 | 1337 | (define-key cua--cua-keys-keymap [(meta v)] 'cua-repeat-replace-region) |
72cc582e KS |
1338 | (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark) |
1339 | ||
1340 | (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler) | |
1341 | (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler) | |
a1506d29 | 1342 | |
72cc582e KS |
1343 | (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler) |
1344 | (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler) | |
1345 | (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler) | |
1346 | (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler) | |
1347 | (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler) | |
1348 | (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler) | |
1349 | (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler) | |
1350 | (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler) | |
1351 | (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler) | |
1352 | (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler) | |
1353 | ||
a1506d29 | 1354 | ;; Enable shifted fallbacks for C-x and C-c when region is active |
186bccc3 KS |
1355 | (define-key cua--region-keymap [(shift control x)] 'cua--shift-control-x-prefix) |
1356 | (define-key cua--region-keymap [(shift control c)] 'cua--shift-control-c-prefix) | |
1357 | ||
72cc582e KS |
1358 | ;; replace current region |
1359 | (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region) | |
1360 | (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region) | |
1361 | (define-key cua--region-keymap [remap insert-register] 'cua-replace-region) | |
1362 | (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region) | |
1363 | (define-key cua--region-keymap [remap newline] 'cua-replace-region) | |
1364 | (define-key cua--region-keymap [remap open-line] 'cua-replace-region) | |
1365 | ;; delete current region | |
1366 | (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region) | |
1367 | (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region) | |
1368 | (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region) | |
1369 | (define-key cua--region-keymap [remap delete-char] 'cua-delete-region) | |
1370 | ;; kill region | |
1371 | (define-key cua--region-keymap [remap kill-region] 'cua-cut-region) | |
6a350b06 | 1372 | (define-key cua--region-keymap [remap clipboard-kill-region] 'cua-cut-region) |
72cc582e KS |
1373 | ;; copy region |
1374 | (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region) | |
1375 | (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region) | |
6a350b06 | 1376 | (define-key cua--region-keymap [remap clipboard-kill-ring-save] 'cua-copy-region) |
72cc582e KS |
1377 | ;; cancel current region/rectangle |
1378 | (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel) | |
1379 | (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel) | |
1380 | ) | |
1381 | ||
4905133f KS |
1382 | |
1383 | ;; Setup standard movement commands to be recognized by CUA. | |
1384 | ||
1385 | (dolist (cmd | |
1386 | '(forward-char backward-char | |
1387 | next-line previous-line | |
1388 | forward-word backward-word | |
1389 | end-of-line beginning-of-line | |
bab88420 | 1390 | move-end-of-line move-beginning-of-line |
4905133f KS |
1391 | end-of-buffer beginning-of-buffer |
1392 | scroll-up scroll-down | |
72662103 KS |
1393 | up-list down-list backward-up-list |
1394 | end-of-defun beginning-of-defun | |
1395 | forward-sexp backward-sexp | |
1396 | forward-list backward-list | |
4905133f KS |
1397 | forward-sentence backward-sentence |
1398 | forward-paragraph backward-paragraph)) | |
1399 | (put cmd 'CUA 'move)) | |
1400 | ||
cadc926b KS |
1401 | ;; State prior to enabling cua-mode |
1402 | ;; Value is a list with the following elements: | |
1403 | ;; transient-mark-mode | |
1404 | ;; delete-selection-mode | |
1405 | ;; pc-selection-mode | |
1406 | ||
1407 | (defvar cua--saved-state nil) | |
72cc582e KS |
1408 | |
1409 | ;;;###autoload | |
80d9508a | 1410 | (define-minor-mode cua-mode |
72cc582e | 1411 | "Toggle CUA key-binding mode. |
3685c8a4 KS |
1412 | When enabled, using shifted movement keys will activate the |
1413 | region (and highlight the region using `transient-mark-mode'), | |
1414 | and typed text replaces the active selection. | |
1415 | ||
1416 | Also when enabled, you can use C-z, C-x, C-c, and C-v to undo, | |
1417 | cut, copy, and paste in addition to the normal Emacs bindings. | |
1418 | The C-x and C-c keys only do cut and copy when the region is | |
1419 | active, so in most cases, they do not conflict with the normal | |
1420 | function of these prefix keys. | |
1421 | ||
1422 | If you really need to perform a command which starts with one of | |
1423 | the prefix keys even when the region is active, you have three | |
1424 | options: | |
1425 | - press the prefix key twice very quickly (within 0.2 seconds), | |
1023b169 | 1426 | - press the prefix key and the following key within 0.2 seconds, or |
3685c8a4 KS |
1427 | - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c. |
1428 | ||
1429 | You can customize `cua-enable-cua-keys' to completely disable the | |
1430 | CUA bindings, or `cua-prefix-override-inhibit-delay' to change | |
2b3726c0 KS |
1431 | the prefix fallback behavior. |
1432 | ||
c36f1e67 LT |
1433 | CUA mode manages Transient Mark mode internally. Trying to disable |
1434 | Transient Mark mode while CUA mode is enabled does not work; if you | |
2b3726c0 KS |
1435 | only want to highlight the region when it is selected using a |
1436 | shifted movement key, set `cua-highlight-region-shift-only'." | |
80d9508a | 1437 | :global t |
ca0f8d34 | 1438 | :group 'cua |
1cb225da KS |
1439 | :set-after '(cua-enable-modeline-indications |
1440 | cua-rectangle-mark-key cua-rectangle-modifier-key) | |
80d9508a SM |
1441 | :require 'cua-base |
1442 | :link '(emacs-commentary-link "cua-base.el") | |
72cc582e KS |
1443 | (setq mark-even-if-inactive t) |
1444 | (setq highlight-nonselected-windows nil) | |
72cc582e KS |
1445 | |
1446 | (unless cua--keymaps-initalized | |
1447 | (cua--init-keymaps) | |
1448 | (setq cua--keymaps-initalized t)) | |
1449 | ||
1450 | (if cua-mode | |
1451 | (progn | |
1452 | (add-hook 'pre-command-hook 'cua--pre-command-handler) | |
1453 | (add-hook 'post-command-hook 'cua--post-command-handler) | |
1454 | (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist))) | |
1455 | (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist))) | |
2ed2415d KS |
1456 | (if cua-enable-cursor-indications |
1457 | (cua--update-indications))) | |
1458 | ||
72cc582e KS |
1459 | (remove-hook 'pre-command-hook 'cua--pre-command-handler) |
1460 | (remove-hook 'post-command-hook 'cua--post-command-handler)) | |
a23a8440 KS |
1461 | |
1462 | (if (not cua-mode) | |
1463 | (setq emulation-mode-map-alists (delq 'cua--keymap-alist emulation-mode-map-alists)) | |
1a2f4b9f | 1464 | (add-to-ordered-list 'emulation-mode-map-alists 'cua--keymap-alist 400) |
a23a8440 KS |
1465 | (cua--select-keymaps)) |
1466 | ||
cadc926b KS |
1467 | (cond |
1468 | (cua-mode | |
1469 | (setq cua--saved-state | |
1470 | (list | |
1471 | transient-mark-mode | |
1472 | (and (boundp 'delete-selection-mode) delete-selection-mode) | |
1473 | (and (boundp 'pc-selection-mode) pc-selection-mode))) | |
1474 | (if (and (boundp 'delete-selection-mode) delete-selection-mode) | |
d7672cf8 | 1475 | (delete-selection-mode -1)) |
cadc926b | 1476 | (if (and (boundp 'pc-selection-mode) pc-selection-mode) |
d7672cf8 | 1477 | (pc-selection-mode -1)) |
ef2887a2 | 1478 | (cua--deactivate) |
cadc926b KS |
1479 | (setq transient-mark-mode (and cua-mode |
1480 | (if cua-highlight-region-shift-only | |
1481 | (not cua--explicit-region-start) | |
80d9508a | 1482 | t)))) |
cadc926b KS |
1483 | (cua--saved-state |
1484 | (setq transient-mark-mode (car cua--saved-state)) | |
1485 | (if (nth 1 cua--saved-state) | |
1486 | (delete-selection-mode 1)) | |
1487 | (if (nth 2 cua--saved-state) | |
1488 | (pc-selection-mode 1)) | |
1489 | (if (interactive-p) | |
1490 | (message "CUA mode disabled.%s%s%s%s" | |
1491 | (if (nth 1 cua--saved-state) " Delete-Selection" "") | |
1492 | (if (and (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " and" "") | |
1493 | (if (nth 2 cua--saved-state) " PC-Selection" "") | |
1494 | (if (or (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " enabled" ""))) | |
80d9508a | 1495 | (setq cua--saved-state nil)))) |
72cc582e | 1496 | |
01096a7b KS |
1497 | |
1498 | ;;;###autoload | |
1499 | (defun cua-selection-mode (arg) | |
1500 | "Enable CUA selection mode without the C-z/C-x/C-c/C-v bindings." | |
1501 | (interactive "P") | |
1502 | (setq-default cua-enable-cua-keys nil) | |
1503 | (cua-mode arg)) | |
1504 | ||
1505 | ||
72cc582e | 1506 | (defun cua-debug () |
c77d7c92 | 1507 | "Toggle CUA debugging." |
72cc582e KS |
1508 | (interactive) |
1509 | (setq cua--debug (not cua--debug))) | |
1510 | ||
06f8e605 | 1511 | ;; Install run-time check for older versions of CUA-mode which does not |
bf247b6e | 1512 | ;; work with GNU Emacs version 22.1 and newer. |
06f8e605 KS |
1513 | ;; |
1514 | ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode | |
1515 | ;; provided the `CUA-mode' feature. Since this is no longer true, | |
1516 | ;; we can warn the user if the `CUA-mode' feature is ever provided. | |
1517 | ||
1518 | ;;;###autoload (eval-after-load 'CUA-mode | |
1519 | ;;;###autoload '(error (concat "\n\n" | |
398d70ce KS |
1520 | ;;;###autoload "CUA-mode is now part of the standard GNU Emacs distribution, so you may\n" |
1521 | ;;;###autoload "now enable CUA via the Options menu or by customizing option `cua-mode'.\n\n" | |
8736f4c2 KS |
1522 | ;;;###autoload "You have loaded an older version of CUA-mode which does\n" |
1523 | ;;;###autoload "not work correctly with this version of GNU Emacs.\n\n" | |
1934f529 | 1524 | ;;;###autoload (if user-init-file (concat |
06f8e605 | 1525 | ;;;###autoload "To correct this, remove the loading and customization of the\n" |
8736f4c2 | 1526 | ;;;###autoload "old version from the " user-init-file " file.\n\n"))))) |
06f8e605 | 1527 | |
f1cb14f8 KS |
1528 | (provide 'cua) |
1529 | ||
ab5796a9 | 1530 | ;;; arch-tag: 21fb6289-ba25-4fee-bfdc-f9fb351acf05 |
72cc582e | 1531 | ;;; cua-base.el ends here |