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