Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / emulation / cua-base.el
blob0befb41826b5c440c23f71e7ba06c08d6df1fd44
1 ;;; cua-base.el --- emulate CUA key bindings
3 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulations convenience cua
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
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.
31 ;; CUA style key bindings for cut and paste
32 ;; ----------------------------------------
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
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!!!
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!
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).
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
65 ;; This behavior can be customized via the
66 ;; cua-prefix-override-inhibit-delay variable.
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].
72 ;; If you prefer to use the standard emacs cut, copy, paste, and undo
73 ;; bindings, customize cua-enable-cua-keys to nil.
76 ;; Typing text replaces the region
77 ;; -------------------------------
79 ;; When the region is active, i.e. highlighted, the text in region is
80 ;; replaced by the text you type.
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).
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.
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.
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:
108 ;; (setq cua-normal-cursor-color "red")
109 ;; (setq cua-overwrite-cursor-color "yellow")
110 ;; (setq cua-read-only-cursor-color "green")
113 ;; CUA register support
114 ;; --------------------
115 ;; Emacs's standard register support is also based on a separate set of
116 ;; "register commands".
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].
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.
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].
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.
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).
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!
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!
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.
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.
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.
179 ;; [C-return] cancels the rectangle
180 ;; [C-space] activates the region bounded by the rectangle
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
192 ;; cua-mode's rectangle support also includes all the normal rectangle
193 ;; functions with easy access:
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.
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].
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.
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).
250 ;; [TAB] Indents the current line or rectangle to the column of the
251 ;; global mark.
253 ;;; Code:
255 ;;; Customization
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"))
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)
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 `cua-repeat-replace-region'."
281 :type 'boolean
282 :group 'cua)
284 (defcustom cua-remap-control-z t
285 "If non-nil, C-z binding is used for undo."
286 :type 'boolean
287 :group 'cua)
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")
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)
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)
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)
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)
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)
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)
352 (defcustom cua-delete-copy-to-register-0 t
353 "If non-nil, save last deleted region or rectangle to register 0."
354 :type 'boolean
355 :group 'cua)
357 (defcustom cua-enable-region-auto-help nil
358 "If non-nil, automatically show help for active region."
359 :type 'boolean
360 :group 'cua)
362 (defcustom cua-enable-modeline-indications nil
363 "If non-nil, use minor-mode hook to show status in mode line."
364 :type 'boolean
365 :group 'cua)
367 (defcustom cua-check-pending-input t
368 "If non-nil, don't override prefix key if input pending.
369 It is rumored that `input-pending-p' is unreliable under some window
370 managers, so try setting this to nil, if prefix override doesn't work."
371 :type 'boolean
372 :group 'cua)
374 (defcustom cua-paste-pop-rotate-temporarily nil
375 "If non-nil, \\[cua-paste-pop] only rotates the kill-ring temporarily.
376 This means that both \\[yank] and the first \\[yank-pop] in a sequence always insert
377 the most recently killed text. Each immediately following \\[cua-paste-pop] replaces
378 the previous text with the next older element on the `kill-ring'.
379 With prefix arg, \\[universal-argument] \\[yank-pop] inserts the same text as the most
380 recent \\[yank-pop] (or \\[yank]) command."
381 :type 'boolean
382 :group 'cua)
384 ;;; Rectangle Customization
386 (defcustom cua-virtual-rectangle-edges t
387 "If non-nil, rectangles have virtual straight edges.
388 Note that although rectangles are always DISPLAYED with straight edges, the
389 buffer is NOT modified, until you execute a command that actually modifies it.
390 M-p toggles this feature when a rectangle is active."
391 :type 'boolean
392 :group 'cua)
394 (defcustom cua-auto-tabify-rectangles 1000
395 "If non-nil, automatically tabify after rectangle commands.
396 This basically means that `tabify' is applied to all lines that
397 are modified by inserting or deleting a rectangle. If value is
398 an integer, CUA will look for existing tabs in a region around
399 the rectangle, and only do the conversion if any tabs are already
400 present. The number specifies then number of characters before
401 and after the region marked by the rectangle to search."
402 :type '(choice (number :tag "Auto detect (limit)")
403 (const :tag "Disabled" nil)
404 (other :tag "Enabled" t))
405 :group 'cua)
407 (defvar cua-global-keymap) ; forward
408 (defvar cua--region-keymap) ; forward
410 (defcustom cua-rectangle-mark-key [(control return)]
411 "Global key used to toggle the cua rectangle mark."
412 :set #'(lambda (symbol value)
413 (set symbol value)
414 (when (and (boundp 'cua--keymaps-initialized)
415 cua--keymaps-initialized)
416 (define-key cua-global-keymap value
417 'cua-set-rectangle-mark)
418 (when (boundp 'cua--rectangle-keymap)
419 (define-key cua--rectangle-keymap value
420 'cua-clear-rectangle-mark)
421 (define-key cua--region-keymap value
422 'cua-toggle-rectangle-mark))))
423 :type 'key-sequence
424 :group 'cua)
426 (defcustom cua-rectangle-modifier-key 'meta
427 "Modifier key used for rectangle commands bindings.
428 On non-window systems, always use the meta modifier.
429 Must be set prior to enabling CUA."
430 :type '(choice (const :tag "Meta key" meta)
431 (const :tag "Alt key" alt)
432 (const :tag "Hyper key" hyper)
433 (const :tag "Super key" super))
434 :group 'cua)
436 (defcustom cua-enable-rectangle-auto-help t
437 "If non-nil, automatically show help for region, rectangle and global mark."
438 :type 'boolean
439 :group 'cua)
441 (defface cua-rectangle
442 '((default :inherit region)
443 (((class color)) :foreground "white" :background "maroon"))
444 "Font used by CUA for highlighting the rectangle."
445 :group 'cua)
447 (defface cua-rectangle-noselect
448 '((default :inherit region)
449 (((class color)) :foreground "white" :background "dimgray"))
450 "Font used by CUA for highlighting the non-selected rectangle lines."
451 :group 'cua)
454 ;;; Global Mark Customization
456 (defcustom cua-global-mark-keep-visible t
457 "If non-nil, always keep global mark visible in other window."
458 :type 'boolean
459 :group 'cua)
461 (defface cua-global-mark
462 '((((min-colors 88)(class color)) :foreground "black" :background "yellow1")
463 (((class color)) :foreground "black" :background "yellow")
464 (t :weight bold))
465 "Font used by CUA for highlighting the global mark."
466 :group 'cua)
468 (defcustom cua-global-mark-blink-cursor-interval 0.20
469 "Blink cursor at this interval when global mark is active."
470 :type '(choice (number :tag "Blink interval")
471 (const :tag "No blink" nil))
472 :group 'cua)
475 ;;; Cursor Indication Customization
477 (defcustom cua-enable-cursor-indications nil
478 "If non-nil, use different cursor colors for indications."
479 :type 'boolean
480 :group 'cua)
482 (defcustom cua-normal-cursor-color (or (and (boundp 'initial-cursor-color) initial-cursor-color)
483 (and (boundp 'initial-frame-alist)
484 (assoc 'cursor-color initial-frame-alist)
485 (cdr (assoc 'cursor-color initial-frame-alist)))
486 (and (boundp 'default-frame-alist)
487 (assoc 'cursor-color default-frame-alist)
488 (cdr (assoc 'cursor-color default-frame-alist)))
489 (frame-parameter nil 'cursor-color)
490 "red")
491 "Normal (non-overwrite) cursor color.
492 Default is to load cursor color from initial or default frame parameters.
494 If the value is a COLOR name, then only the `cursor-color' attribute will be
495 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
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 :initialize 'custom-initialize-default
499 :type '(choice
500 (color :tag "Color")
501 (choice :tag "Type"
502 (const :tag "Filled box" box)
503 (const :tag "Vertical bar" bar)
504 (const :tag "Horizontal bar" hbar)
505 (const :tag "Hollow box" hollow))
506 (cons :tag "Color and Type"
507 (choice :tag "Type"
508 (const :tag "Filled box" box)
509 (const :tag "Vertical bar" bar)
510 (const :tag "Horizontal bar" hbar)
511 (const :tag "Hollow box" hollow))
512 (color :tag "Color")))
513 :group 'cua)
515 (defcustom cua-read-only-cursor-color "darkgreen"
516 "Cursor color used in read-only buffers, if non-nil.
517 Only used when `cua-enable-cursor-indications' is non-nil.
519 If the value is a COLOR name, then only the `cursor-color' attribute will be
520 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
521 then only the `cursor-type' property will be affected. If the value is
522 a cons (TYPE . COLOR), then both properties are affected."
523 :type '(choice
524 (color :tag "Color")
525 (choice :tag "Type"
526 (const :tag "Filled box" box)
527 (const :tag "Vertical bar" bar)
528 (const :tag "Horizontal bar" hbar)
529 (const :tag "Hollow box" hollow))
530 (cons :tag "Color and Type"
531 (choice :tag "Type"
532 (const :tag "Filled box" box)
533 (const :tag "Vertical bar" bar)
534 (const :tag "Horizontal bar" hbar)
535 (const :tag "Hollow box" hollow))
536 (color :tag "Color")))
537 :group 'cua)
539 (defcustom cua-overwrite-cursor-color "yellow"
540 "Cursor color used when overwrite mode is set, if non-nil.
541 Only used when `cua-enable-cursor-indications' is non-nil.
543 If the value is a COLOR name, then only the `cursor-color' attribute will be
544 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
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)
552 (const :tag "Horizontal bar" hbar)
553 (const :tag "Hollow box" hollow))
554 (cons :tag "Color and Type"
555 (choice :tag "Type"
556 (const :tag "Filled box" box)
557 (const :tag "Vertical bar" bar)
558 (const :tag "Horizontal bar" hbar)
559 (const :tag "Hollow box" hollow))
560 (color :tag "Color")))
561 :group 'cua)
563 (defcustom cua-global-mark-cursor-color "cyan"
564 "Indication for active global mark.
565 Will change cursor color to specified color if string.
566 Only used when `cua-enable-cursor-indications' is non-nil.
568 If the value is a COLOR name, then only the `cursor-color' attribute will be
569 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
570 then only the `cursor-type' property will be affected. If the value is
571 a cons (TYPE . COLOR), then both properties are affected."
572 :type '(choice
573 (color :tag "Color")
574 (choice :tag "Type"
575 (const :tag "Filled box" box)
576 (const :tag "Vertical bar" bar)
577 (const :tag "Horizontal bar" hbar)
578 (const :tag "Hollow box" hollow))
579 (cons :tag "Color and Type"
580 (choice :tag "Type"
581 (const :tag "Filled box" box)
582 (const :tag "Vertical bar" bar)
583 (const :tag "Horizontal bar" hbar)
584 (const :tag "Hollow box" hollow))
585 (color :tag "Color")))
586 :group 'cua)
589 ;;; Rectangle support is in cua-rect.el
591 (autoload 'cua-set-rectangle-mark "cua-rect"
592 "Start rectangle at mouse click position." t nil)
594 ;; Stub definitions until it is loaded
595 (defvar cua--rectangle)
596 (defvar cua--last-killed-rectangle)
597 (unless (featurep 'cua-rect)
598 (setq cua--rectangle nil
599 cua--last-killed-rectangle nil))
601 ;; All behind cua--rectangle tests.
602 (declare-function cua--rectangle-left "cua-rect" (&optional val))
603 (declare-function cua--delete-rectangle "cua-rect" ())
604 (declare-function cua--insert-rectangle "cua-rect"
605 (rect &optional below paste-column line-count))
606 (declare-function cua--rectangle-corner "cua-rect" (&optional advance))
607 (declare-function cua--rectangle-assert "cua-rect" ())
609 ;;; Global Mark support is in cua-gmrk.el
611 (autoload 'cua-toggle-global-mark "cua-gmrk" nil t nil)
613 ;; Stub definitions until cua-gmrk.el is loaded
614 (defvar cua--global-mark-active)
615 (unless (featurep 'cua-gmrk)
616 (setq cua--global-mark-active nil))
618 (declare-function cua--insert-at-global-mark "cua-gmrk" (str &optional msg))
619 (declare-function cua--global-mark-post-command "cua-gmrk" ())
622 ;;; Low-level Interface
624 (defvar cua-inhibit-cua-keys nil
625 "Buffer-local variable that may disable the CUA keymappings.")
626 (make-variable-buffer-local 'cua-inhibit-cua-keys)
628 ;;; Aux. variables
630 ;; buffer + point prior to current command when rectangle is active
631 ;; checked in post-command hook to see if point was moved
632 (defvar cua--buffer-and-point-before-command nil)
634 ;; status string for mode line indications
635 (defvar cua--status-string nil)
636 (make-variable-buffer-local 'cua--status-string)
638 (defvar cua--debug nil)
641 ;;; Prefix key override mechanism
643 ;; The prefix override (when mark-active) operates in three substates:
644 ;; [1] Before using a prefix key
645 ;; [2] Immediately after using a prefix key
646 ;; [3] A fraction of a second later
648 ;; In state [1], the cua--prefix-override-keymap is active.
649 ;; This keymap binds the C-x and C-c prefix keys to the
650 ;; cua--prefix-override-handler function.
652 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
653 ;; will push back the keys already read to the event queue. If input is
654 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
655 ;; is started, and it changes to state [2].
657 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
658 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
659 ;; C-x to the cua--prefix-repeat-handler function.
661 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
662 ;; will cancel [T], back the keys already read (except for the second prefix
663 ;; keys) to the event queue, and changes to state [3].
665 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
666 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
667 ;; the cua--prefix-override-timeout function will push a `timeout' event on
668 ;; the event queue, and changes to state [3].
670 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
671 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
672 ;; normal prefix key binding from the global or local map will be used.
674 ;; The pre-command hook (executed as a consequence of the timeout or normal
675 ;; prefix key binding) will cancel [T] and change from state [3] back to
676 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
677 ;; are always called with state reset to [1]!
679 ;; State [1] is recognized by cua--prefix-override-timer is nil,
680 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
681 ;; state [3] is recognized by cua--prefix-override-timer is t.
683 (defvar cua--prefix-override-timer nil)
684 (defvar cua--prefix-override-length nil)
686 (defun cua--prefix-override-replay (arg repeat)
687 (let* ((keys (this-command-keys))
688 (i (length keys))
689 (key (aref keys (1- i))))
690 (setq cua--prefix-override-length (- i repeat))
691 (setq cua--prefix-override-timer
693 ;; In state [2], change to state [3]
694 (> repeat 0)
695 ;; In state [1], change directly to state [3]
696 (and cua-check-pending-input (input-pending-p))
697 ;; In state [1], [T] disabled, so change to state [3]
698 (not (numberp cua-prefix-override-inhibit-delay))
699 (<= cua-prefix-override-inhibit-delay 0)
700 ;; In state [1], start [T] and change to state [2]
701 (run-with-timer cua-prefix-override-inhibit-delay nil
702 'cua--prefix-override-timeout)))
703 ;; Don't record this command
704 (setq this-command last-command)
705 ;; Restore the prefix arg
706 (setq prefix-arg arg)
707 (reset-this-command-lengths)
708 ;; Push the key back on the event queue
709 (setq unread-command-events (cons key unread-command-events))))
711 (defun cua--prefix-override-handler (arg)
712 "Start timer waiting for prefix key to be followed by another key.
713 Repeating prefix key when region is active works as a single prefix key."
714 (interactive "P")
715 (cua--prefix-override-replay arg 0))
717 (defun cua--prefix-repeat-handler (arg)
718 "Repeating prefix key when region is active works as a single prefix key."
719 (interactive "P")
720 (cua--prefix-override-replay arg 1))
722 (defun cua--prefix-copy-handler (arg)
723 "Copy region/rectangle, then replay last key."
724 (interactive "P")
725 (cua-copy-region arg)
726 (let ((keys (this-single-command-keys)))
727 (setq unread-command-events
728 (cons (aref keys (1- (length keys))) unread-command-events))))
730 (defun cua--prefix-cut-handler (arg)
731 "Cut region/rectangle, then replay last key."
732 (interactive "P")
733 (cua-cut-region arg)
734 (let ((keys (this-single-command-keys)))
735 (setq unread-command-events
736 (cons (aref keys (1- (length keys))) unread-command-events))))
738 (defun cua--prefix-override-timeout ()
739 (setq cua--prefix-override-timer t)
740 (when (= (length (this-command-keys)) cua--prefix-override-length)
741 (setq unread-command-events (cons 'timeout unread-command-events))
742 (if prefix-arg
743 (reset-this-command-lengths)
744 (setq overriding-terminal-local-map nil))
745 (cua--select-keymaps)))
748 ;;; Aux. functions
750 (defun cua--fallback ()
751 ;; Execute original command
752 (setq this-command this-original-command)
753 (call-interactively this-command))
755 (defun cua--keep-active ()
756 (setq mark-active t
757 deactivate-mark nil))
759 (defun cua--deactivate (&optional now)
760 (if (not now)
761 (setq deactivate-mark t)
762 (deactivate-mark)))
764 (defun cua--filter-buffer-noprops (start end)
765 (let ((str (filter-buffer-substring start end)))
766 (set-text-properties 0 (length str) nil str)
767 str))
769 ;; The current register prefix
770 (defvar cua--register nil)
772 (defun cua--prefix-arg (arg)
773 (setq cua--register
774 (and cua-enable-register-prefix
775 (integerp arg) (>= arg 0) (< arg 10)
776 (let* ((prefix (aref (this-command-keys) 0))
777 (ctrl-u-prefix (and (integerp prefix)
778 (= prefix ?\C-u))))
779 (cond
780 ((eq cua-enable-register-prefix 'not-ctrl-u)
781 (not ctrl-u-prefix))
782 ((eq cua-enable-register-prefix 'ctrl-u-only)
783 ctrl-u-prefix)
784 (t t)))
785 (+ arg ?0)))
786 (if cua--register nil arg))
789 ;;; Region specific commands
791 (defvar cua--last-deleted-region-pos nil)
792 (defvar cua--last-deleted-region-text nil)
794 (defun cua-delete-region ()
795 "Delete the active region.
796 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
797 (interactive)
798 (let ((start (mark)) (end (point)))
799 (or (<= start end)
800 (setq start (prog1 end (setq end start))))
801 (setq cua--last-deleted-region-text
802 (funcall region-extract-function t))
803 (if cua-delete-copy-to-register-0
804 (set-register ?0 cua--last-deleted-region-text))
805 (setq cua--last-deleted-region-pos
806 (cons (current-buffer)
807 (and (consp buffer-undo-list)
808 (car buffer-undo-list))))
809 (cua--deactivate)
810 (/= start end)))
812 (defun cua-copy-region (arg)
813 "Copy the region to the kill ring.
814 With numeric prefix arg, copy to register 0-9 instead."
815 (interactive "P")
816 (setq arg (cua--prefix-arg arg))
817 (setq cua--last-killed-rectangle nil)
818 (let ((start (mark)) (end (point)))
819 (or (<= start end)
820 (setq start (prog1 end (setq end start))))
821 (cond
822 (cua--register
823 (copy-to-register cua--register start end nil 'region))
824 ((eq this-original-command 'clipboard-kill-ring-save)
825 (clipboard-kill-ring-save start end 'region))
827 (copy-region-as-kill start end 'region)))
828 (if cua-keep-region-after-copy
829 (cua--keep-active)
830 (cua--deactivate))))
832 (defun cua-cut-region (arg)
833 "Cut the region and copy to the kill ring.
834 With numeric prefix arg, copy to register 0-9 instead."
835 (interactive "P")
836 (setq cua--last-killed-rectangle nil)
837 (if buffer-read-only
838 (cua-copy-region arg)
839 (setq arg (cua--prefix-arg arg))
840 (let ((start (mark)) (end (point)))
841 (or (<= start end)
842 (setq start (prog1 end (setq end start))))
843 (cond
844 (cua--register
845 (copy-to-register cua--register start end t 'region))
846 ((eq this-original-command 'clipboard-kill-region)
847 (clipboard-kill-region start end 'region))
849 (kill-region start end 'region))))
850 (cua--deactivate)))
852 ;;; Generic commands for regions, rectangles, and global marks
854 (defun cua-cancel ()
855 "Cancel the active region, rectangle, or global mark."
856 (interactive)
857 (setq mark-active nil)
858 (if (fboundp 'cua--cancel-rectangle)
859 (cua--cancel-rectangle)))
861 (declare-function x-clipboard-yank "../term/x-win" ())
863 (put 'cua-paste 'delete-selection 'yank)
864 (defun cua-paste (arg)
865 "Paste last cut or copied region or rectangle.
866 An active region is deleted before executing the command.
867 With numeric prefix arg, paste from register 0-9 instead.
868 If global mark is active, copy from register or one character."
869 (interactive "P")
870 (setq arg (cua--prefix-arg arg))
871 (let ((regtxt (and cua--register (get-register cua--register)))
872 (count (prefix-numeric-value arg)))
873 (cond
874 ((and cua--register (not regtxt))
875 (message "Nothing in register %c" cua--register))
876 (cua--global-mark-active
877 (if regtxt
878 (cua--insert-at-global-mark regtxt)
879 (when (not (eobp))
880 (cua--insert-at-global-mark
881 (filter-buffer-substring (point) (+ (point) count)))
882 (forward-char count))))
883 (buffer-read-only
884 (error "Cannot paste into a read-only buffer"))
886 (cond
887 (regtxt
888 (cond
889 ;; This being a cons implies cua-rect is loaded?
890 ((consp regtxt) (cua--insert-rectangle regtxt))
891 ((stringp regtxt) (insert-for-yank regtxt))
892 (t (message "Unknown data in register %c" cua--register))))
893 ((eq this-original-command 'clipboard-yank)
894 (clipboard-yank))
895 ((eq this-original-command 'x-clipboard-yank)
896 (x-clipboard-yank))
897 (t (yank arg)))))))
900 ;; cua-paste-pop-rotate-temporarily == t mechanism:
902 ;; C-y M-y M-y => only rotates kill ring temporarily,
903 ;; so next C-y yanks what previous C-y yanked,
905 ;; M-y M-y M-y => equivalent to C-y M-y M-y
907 ;; But: After another command, C-u M-y remembers the temporary
908 ;; kill-ring position, so
909 ;; C-u M-y => yanks what the last M-y yanked
912 (defvar cua-paste-pop-count nil)
914 (defun cua-paste-pop (arg)
915 "Replace a just-pasted text or rectangle with a different text.
916 See `yank-pop' for details about the default behavior. For an alternative
917 behavior, see `cua-paste-pop-rotate-temporarily'."
918 (interactive "P")
919 (cond
920 ((eq last-command 'cua--paste-rectangle)
921 (undo)
922 (yank arg))
923 ((not cua-paste-pop-rotate-temporarily)
924 (yank-pop (prefix-numeric-value arg)))
926 (let ((rotate (if (consp arg) 1 (prefix-numeric-value arg))))
927 (cond
928 ((or (null cua-paste-pop-count)
929 (eq last-command 'yank)
930 (eq last-command 'cua-paste))
931 (setq cua-paste-pop-count rotate)
932 (setq last-command 'yank)
933 (yank-pop cua-paste-pop-count))
934 ((and (eq last-command 'cua-paste-pop) (not (consp arg)))
935 (setq cua-paste-pop-count (+ cua-paste-pop-count rotate))
936 (setq last-command 'yank)
937 (yank-pop cua-paste-pop-count))
939 (setq cua-paste-pop-count
940 (if (consp arg) (+ cua-paste-pop-count rotate -1) 1))
941 (yank (1+ cua-paste-pop-count)))))
942 ;; Undo rotating the kill-ring, so next C-y will
943 ;; yank the original head.
944 (setq kill-ring-yank-pointer kill-ring)
945 (setq this-command 'cua-paste-pop))))
947 (defun cua-exchange-point-and-mark (arg)
948 "Exchange point and mark.
949 Don't activate the mark if `cua-enable-cua-keys' is non-nil.
950 Otherwise, just activate the mark if a prefix ARG is given.
952 See also `exchange-point-and-mark'."
953 (interactive "P")
954 (cond ((null cua-enable-cua-keys)
955 (exchange-point-and-mark arg))
956 (arg
957 (setq mark-active t))
959 (let (mark-active)
960 (exchange-point-and-mark)
961 (if cua--rectangle
962 (cua--rectangle-corner 0))))))
964 ;; Typed text that replaced the highlighted region.
965 (defvar cua--repeat-replace-text nil)
967 (defun cua-repeat-replace-region (arg)
968 "Repeat replacing text of highlighted region with typed text.
969 Searches for the next stretch of text identical to the region last
970 replaced by typing text over it and replaces it with the same stretch
971 of text."
972 (interactive "P")
973 (when cua--last-deleted-region-pos
974 (with-current-buffer (car cua--last-deleted-region-pos)
975 (save-restriction
976 (widen)
977 ;; Find the text that replaced the region via the undo list.
978 (let ((ul buffer-undo-list)
979 (elt (cdr cua--last-deleted-region-pos))
980 u s e)
981 (when elt
982 (while (consp ul)
983 (setq u (car ul) ul (cdr ul))
984 (cond
985 ((eq u elt) ;; got it
986 (setq ul nil))
987 ((and (consp u) (integerp (car u)) (integerp (cdr u)))
988 (if (and s (= (cdr u) s))
989 (setq s (car u))
990 (setq s (car u) e (cdr u)))))))
991 (cond ((and s e (<= s e) (= s (mark t)))
992 (setq cua--repeat-replace-text (cua--filter-buffer-noprops s e)))
993 ((and (null s) (eq u elt)) ;; nothing inserted
994 (setq cua--repeat-replace-text
995 ""))
997 (message "Cannot locate replacement text"))))))
998 (setq cua--last-deleted-region-pos nil))
999 (if (and cua--last-deleted-region-text
1000 cua--repeat-replace-text
1001 (search-forward cua--last-deleted-region-text nil t nil))
1002 (replace-match cua--repeat-replace-text arg t)))
1004 (defun cua-help-for-region (&optional help)
1005 "Show region specific help in echo area."
1006 (interactive)
1007 (message
1008 (concat (if help "C-?:help " "")
1009 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
1012 ;;; Shift activated / extended region
1014 (defun cua-pop-to-last-change ()
1015 (let ((undo-list buffer-undo-list)
1016 pos elt)
1017 (while (and (not pos)
1018 (consp undo-list))
1019 (setq elt (car undo-list)
1020 undo-list (cdr undo-list))
1021 (cond
1022 ((integerp elt)
1023 (setq pos elt))
1024 ((not (consp elt)))
1025 ((and (integerp (cdr elt))
1026 (or (integerp (car elt)) (stringp (car elt))))
1027 (setq pos (cdr elt)))
1028 ((and (eq (car elt) 'apply) (consp (cdr elt)) (integerp (cadr elt)))
1029 (setq pos (nth 3 elt)))))
1030 (when (and pos
1031 (/= pos (point))
1032 (>= pos (point-min)) (<= pos (point-max)))
1033 (goto-char pos)
1034 t)))
1036 (defun cua-set-mark (&optional arg)
1037 "Set mark at where point is, clear mark, or jump to mark.
1039 With no prefix argument, clear mark if already set. Otherwise, set
1040 mark, and push old mark position on local mark ring; also push mark on
1041 global mark ring if last mark was set in another buffer.
1043 With argument, jump to mark, and pop a new position for mark off
1044 the local mark ring (this does not affect the global mark ring).
1045 Use \\[pop-global-mark] to jump to a mark off the global mark ring
1046 \(see `pop-global-mark').
1048 If `cua-auto-mark-last-change' is non-nil, this command behaves as if there
1049 was an implicit mark at the position of the last buffer change.
1051 Repeating the command without the prefix jumps to the next position
1052 off the local (or global) mark ring.
1054 With a double \\[universal-argument] prefix argument, unconditionally set mark."
1055 (interactive "P")
1056 (cond
1057 ((and (consp arg) (> (prefix-numeric-value arg) 4))
1058 (push-mark-command nil))
1059 ((eq last-command 'pop-to-mark-command)
1060 (setq this-command 'pop-to-mark-command)
1061 (pop-to-mark-command))
1062 ((and (eq last-command 'pop-global-mark) (not arg))
1063 (setq this-command 'pop-global-mark)
1064 (pop-global-mark))
1065 (arg
1066 (setq this-command 'pop-to-mark-command)
1067 (or (and cua-auto-mark-last-change
1068 (cua-pop-to-last-change))
1069 (pop-to-mark-command)))
1070 ((and cua-toggle-set-mark mark-active)
1071 (cua--deactivate)
1072 (message "Mark cleared"))
1074 (push-mark-command nil nil)
1075 (if cua-enable-region-auto-help
1076 (cua-help-for-region t)))))
1078 ;; Scrolling commands which do not signal errors at top/bottom
1079 ;; of buffer at first key-press (instead moves to top/bottom
1080 ;; of buffer).
1082 (defun cua-scroll-up (&optional arg)
1083 "Scroll text of current window upward ARG lines; or near full screen if no ARG.
1084 If window cannot be scrolled further, move cursor to bottom line instead.
1085 A near full screen is `next-screen-context-lines' less than a full screen.
1086 Negative ARG means scroll downward.
1087 If ARG is the atom `-', scroll downward by nearly full screen."
1088 (interactive "P")
1089 (cond
1090 ((eq arg '-) (cua-scroll-down nil))
1091 ((< (prefix-numeric-value arg) 0)
1092 (cua-scroll-down (- (prefix-numeric-value arg))))
1093 ((eobp)
1094 (scroll-up arg)) ; signal error
1096 (condition-case nil
1097 (scroll-up arg)
1098 (end-of-buffer (goto-char (point-max)))))))
1100 (put 'cua-scroll-up 'CUA 'move)
1101 (put 'cua-scroll-up 'isearch-scroll t)
1103 (defun cua-scroll-down (&optional arg)
1104 "Scroll text of current window downward ARG lines; or near full screen if no ARG.
1105 If window cannot be scrolled further, move cursor to top line instead.
1106 A near full screen is `next-screen-context-lines' less than a full screen.
1107 Negative ARG means scroll upward.
1108 If ARG is the atom `-', scroll upward by nearly full screen."
1109 (interactive "P")
1110 (cond
1111 ((eq arg '-) (cua-scroll-up nil))
1112 ((< (prefix-numeric-value arg) 0)
1113 (cua-scroll-up (- (prefix-numeric-value arg))))
1114 ((bobp)
1115 (scroll-down arg)) ; signal error
1117 (condition-case nil
1118 (scroll-down arg)
1119 (beginning-of-buffer (goto-char (point-min)))))))
1121 (put 'cua-scroll-down 'CUA 'move)
1122 (put 'cua-scroll-down 'isearch-scroll t)
1124 ;;; Cursor indications
1126 (defun cua--update-indications ()
1127 (let* ((cursor
1128 (cond
1129 ((and cua--global-mark-active
1130 cua-global-mark-cursor-color)
1131 cua-global-mark-cursor-color)
1132 ((and buffer-read-only
1133 cua-read-only-cursor-color)
1134 cua-read-only-cursor-color)
1135 ((and cua-overwrite-cursor-color overwrite-mode)
1136 cua-overwrite-cursor-color)
1137 (t cua-normal-cursor-color)))
1138 (color (if (consp cursor) (cdr cursor) cursor))
1139 (type (if (consp cursor) (car cursor) cursor)))
1140 (if (and color
1141 (stringp color)
1142 (not (equal color (frame-parameter nil 'cursor-color))))
1143 (set-cursor-color color))
1144 (if (and type
1145 (symbolp type)
1146 (not (eq type (default-value 'cursor-type))))
1147 (setq-default cursor-type type))))
1150 ;;; Pre-command hook
1152 (defun cua--pre-command-handler-1 ()
1153 ;; Cancel prefix key timeout if user enters another key.
1154 (when cua--prefix-override-timer
1155 (if (timerp cua--prefix-override-timer)
1156 (cancel-timer cua--prefix-override-timer))
1157 (setq cua--prefix-override-timer nil))
1159 (cond
1160 ;; Only symbol commands can have necessary properties
1161 ((not (symbolp this-command))
1162 nil)
1164 ((not (eq (get this-command 'CUA) 'move))
1165 nil)
1167 ;; Set mark if user explicitly said to do so
1168 (cua--rectangle ;FIXME: ??
1169 (unless mark-active
1170 (push-mark-command nil nil))))
1172 ;; Detect extension of rectangles by mouse or other movement
1173 (setq cua--buffer-and-point-before-command
1174 (if cua--rectangle (cons (current-buffer) (point)))))
1176 (defun cua--pre-command-handler ()
1177 (when cua-mode
1178 (condition-case nil
1179 (cua--pre-command-handler-1)
1180 (error nil))))
1182 ;;; Post-command hook
1184 (defun cua--post-command-handler-1 ()
1185 (when cua--global-mark-active
1186 (cua--global-mark-post-command))
1187 (when (fboundp 'cua--rectangle-post-command)
1188 (cua--rectangle-post-command))
1189 (setq cua--buffer-and-point-before-command nil)
1191 ;; Debugging
1192 (if cua--debug
1193 (cond
1194 (cua--rectangle (cua--rectangle-assert))
1195 (mark-active (message "Mark=%d Point=%d" (mark t) (point)))))
1197 (if cua-enable-cursor-indications
1198 (cua--update-indications))
1200 (cua--select-keymaps))
1202 (defun cua--post-command-handler ()
1203 (when cua-mode
1204 (condition-case nil
1205 (cua--post-command-handler-1)
1206 (error nil))))
1209 ;;; Keymaps
1211 ;; Cached value of actual cua-rectangle-modifier-key
1212 (defvar cua--rectangle-modifier-key 'meta)
1214 (defun cua--M/H-key (map key fct)
1215 ;; bind H-KEY or M-KEY to FCT in MAP
1216 (unless (listp key) (setq key (list key)))
1217 (define-key map (vector (cons cua--rectangle-modifier-key key)) fct))
1219 (defun cua--self-insert-char-p (def)
1220 ;; Return DEF if current key sequence is self-inserting in
1221 ;; global-map.
1222 (if (memq (global-key-binding (this-single-command-keys))
1223 '(self-insert-command))
1224 def nil))
1226 (defvar cua-global-keymap (make-sparse-keymap)
1227 "Global keymap for cua-mode; users may add to this keymap.")
1229 (defvar cua--cua-keys-keymap (make-sparse-keymap))
1230 (defvar cua--prefix-override-keymap (make-sparse-keymap))
1231 (defvar cua--prefix-repeat-keymap (make-sparse-keymap))
1232 (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initialized when cua-gmrk.el is loaded
1233 (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initialized when cua-rect.el is loaded
1234 (defvar cua--region-keymap (make-sparse-keymap))
1236 (defvar cua--ena-cua-keys-keymap nil)
1237 (defvar cua--ena-prefix-override-keymap nil)
1238 (defvar cua--ena-prefix-repeat-keymap nil)
1239 (defvar cua--ena-region-keymap nil)
1240 (defvar cua--ena-global-mark-keymap nil)
1242 (defvar cua--keymap-alist
1243 `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap)
1244 (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap)
1245 (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap)
1246 (cua--ena-global-mark-keymap . ,cua--global-mark-keymap)
1247 (cua--rectangle . ,cua--rectangle-keymap)
1248 (cua--ena-region-keymap . ,cua--region-keymap)
1249 (cua-mode . ,cua-global-keymap)))
1251 (defun cua--select-keymaps ()
1252 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
1253 (setq cua--ena-region-keymap
1254 (and mark-active (not deactivate-mark)))
1255 (setq cua--ena-prefix-override-keymap
1256 (and cua--ena-region-keymap
1257 cua-enable-cua-keys
1258 (not cua-inhibit-cua-keys)
1259 (or (eq cua-enable-cua-keys t)
1260 (region-active-p))
1261 (not executing-kbd-macro)
1262 (not cua--prefix-override-timer)))
1263 (setq cua--ena-prefix-repeat-keymap
1264 (and cua--ena-region-keymap
1265 (or (timerp cua--prefix-override-timer)
1266 (eq cua--prefix-override-timer 'shift))))
1267 (setq cua--ena-cua-keys-keymap
1268 (and cua-enable-cua-keys
1269 (not cua-inhibit-cua-keys)
1270 (or (eq cua-enable-cua-keys t)
1271 (region-active-p))))
1272 (setq cua--ena-global-mark-keymap
1273 (and cua--global-mark-active
1274 (not (window-minibuffer-p)))))
1276 (defvar cua--keymaps-initialized nil)
1278 (defun cua--shift-control-prefix (prefix arg)
1279 ;; handle S-C-x and S-C-c by emulating the fast double prefix function.
1280 ;; Don't record this command
1281 (setq this-command last-command)
1282 ;; Restore the prefix arg
1283 (setq prefix-arg arg)
1284 (reset-this-command-lengths)
1285 ;; Activate the cua--prefix-repeat-keymap
1286 (setq cua--prefix-override-timer 'shift)
1287 ;; Push duplicate keys back on the event queue
1288 (setq unread-command-events (cons prefix (cons prefix unread-command-events))))
1290 (defun cua--shift-control-c-prefix (arg)
1291 (interactive "P")
1292 (cua--shift-control-prefix ?\C-c arg))
1294 (defun cua--shift-control-x-prefix (arg)
1295 (interactive "P")
1296 (cua--shift-control-prefix ?\C-x arg))
1298 (defun cua--init-keymaps ()
1299 ;; Cache actual rectangle modifier key.
1300 (setq cua--rectangle-modifier-key
1301 (if (and cua-rectangle-modifier-key
1302 (memq window-system '(x)))
1303 cua-rectangle-modifier-key
1304 'meta))
1305 ;; C-return always toggles rectangle mark
1306 (define-key cua-global-keymap cua-rectangle-mark-key 'cua-set-rectangle-mark)
1307 (unless (eq cua--rectangle-modifier-key 'meta)
1308 (cua--M/H-key cua-global-keymap ?\s 'cua-set-rectangle-mark)
1309 (define-key cua-global-keymap
1310 (vector (list cua--rectangle-modifier-key 'mouse-1)) 'cua-mouse-set-rectangle-mark))
1312 (define-key cua-global-keymap [(shift control ?\s)] 'cua-toggle-global-mark)
1314 ;; replace region with rectangle or element on kill ring
1315 (define-key cua-global-keymap [remap yank] 'cua-paste)
1316 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1317 (define-key cua-global-keymap [remap x-clipboard-yank] 'cua-paste)
1318 ;; replace current yank with previous kill ring element
1319 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1320 ;; set mark
1321 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
1322 (define-key cua-global-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1324 ;; scrolling
1325 (define-key cua-global-keymap [remap scroll-up] 'cua-scroll-up)
1326 (define-key cua-global-keymap [remap scroll-down] 'cua-scroll-down)
1327 (define-key cua-global-keymap [remap scroll-up-command] 'cua-scroll-up)
1328 (define-key cua-global-keymap [remap scroll-down-command] 'cua-scroll-down)
1330 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
1331 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
1332 (when cua-remap-control-z
1333 (define-key cua--cua-keys-keymap [(control z)] 'undo))
1334 (when cua-remap-control-v
1335 (define-key cua--cua-keys-keymap [(control v)] 'yank)
1336 (define-key cua--cua-keys-keymap [(meta v)] 'cua-repeat-replace-region))
1338 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1339 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
1341 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1342 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1343 (dolist (key '(up down left right home end next prior))
1344 (define-key cua--prefix-repeat-keymap (vector '(control x) key) 'cua--prefix-cut-handler)
1345 (define-key cua--prefix-repeat-keymap (vector '(control c) key) 'cua--prefix-copy-handler))
1347 ;; Enable shifted fallbacks for C-x and C-c when region is active
1348 (define-key cua--region-keymap [(shift control x)] 'cua--shift-control-x-prefix)
1349 (define-key cua--region-keymap [(shift control c)] 'cua--shift-control-c-prefix)
1351 ;; delete current region
1352 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1353 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1354 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1355 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1356 (define-key cua--region-keymap [remap delete-forward-char] 'cua-delete-region)
1357 ;; kill region
1358 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1359 (define-key cua--region-keymap [remap clipboard-kill-region] 'cua-cut-region)
1360 ;; copy region
1361 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1362 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1363 (define-key cua--region-keymap [remap clipboard-kill-ring-save] 'cua-copy-region)
1364 ;; cancel current region/rectangle
1365 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1366 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1370 ;; Setup standard movement commands to be recognized by CUA.
1372 (dolist (cmd
1373 '(forward-char backward-char
1374 right-char left-char
1375 right-word left-word
1376 next-line previous-line
1377 forward-word backward-word
1378 end-of-line beginning-of-line
1379 end-of-visual-line beginning-of-visual-line
1380 move-end-of-line move-beginning-of-line
1381 end-of-buffer beginning-of-buffer
1382 scroll-up scroll-down
1383 scroll-up-command scroll-down-command
1384 up-list down-list backward-up-list
1385 end-of-defun beginning-of-defun
1386 forward-sexp backward-sexp
1387 forward-list backward-list
1388 forward-sentence backward-sentence
1389 forward-paragraph backward-paragraph
1390 ;; CC mode motion commands
1391 c-forward-conditional c-backward-conditional
1392 c-down-conditional c-up-conditional
1393 c-down-conditional-with-else c-up-conditional-with-else
1394 c-beginning-of-statement c-end-of-statement))
1395 (put cmd 'CUA 'move))
1397 ;; Only called if pc-selection-mode is t, which means pc-select is loaded.
1398 (declare-function pc-selection-mode "pc-select" (&optional arg))
1400 ;; State prior to enabling cua-mode
1401 ;; Value is a list with the following elements:
1402 ;; delete-selection-mode
1404 (defvar cua--saved-state nil)
1406 ;;;###autoload
1407 (define-minor-mode cua-mode
1408 "Toggle Common User Access style editing (CUA mode).
1409 With a prefix argument ARG, enable CUA mode if ARG is positive,
1410 and disable it otherwise. If called from Lisp, enable the mode
1411 if ARG is omitted or nil.
1413 CUA mode is a global minor mode. When enabled, typed text
1414 replaces the active selection, and you can use C-z, C-x, C-c, and
1415 C-v to undo, cut, copy, and paste in addition to the normal Emacs
1416 bindings. The C-x and C-c keys only do cut and copy when the
1417 region is active, so in most cases, they do not conflict with the
1418 normal function of these prefix keys.
1420 If you really need to perform a command which starts with one of
1421 the prefix keys even when the region is active, you have three
1422 options:
1423 - press the prefix key twice very quickly (within 0.2 seconds),
1424 - press the prefix key and the following key within 0.2 seconds, or
1425 - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c.
1427 You can customize `cua-enable-cua-keys' to completely disable the
1428 CUA bindings, or `cua-prefix-override-inhibit-delay' to change
1429 the prefix fallback behavior.
1431 CUA mode manages Transient Mark mode internally. Trying to disable
1432 Transient Mark mode while CUA mode is enabled does not work; if you
1433 only want to highlight the region when it is selected using a
1434 shifted movement key, set `cua-highlight-region-shift-only'."
1435 :global t
1436 :group 'cua
1437 :set-after '(cua-enable-modeline-indications
1438 cua-remap-control-v cua-remap-control-z
1439 cua-rectangle-mark-key cua-rectangle-modifier-key)
1440 :require 'cua-base
1441 :link '(emacs-commentary-link "cua-base.el")
1442 (setq mark-even-if-inactive t)
1443 (setq highlight-nonselected-windows nil)
1445 (unless cua--keymaps-initialized
1446 (cua--init-keymaps)
1447 (setq cua--keymaps-initialized t))
1449 (if cua-mode
1450 (progn
1451 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1452 (add-hook 'post-command-hook 'cua--post-command-handler)
1453 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1454 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
1455 (if cua-enable-cursor-indications
1456 (cua--update-indications)))
1458 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1459 (remove-hook 'post-command-hook 'cua--post-command-handler))
1461 (if (not cua-mode)
1462 (setq emulation-mode-map-alists
1463 (delq 'cua--keymap-alist emulation-mode-map-alists))
1464 (add-to-ordered-list 'emulation-mode-map-alists 'cua--keymap-alist 400)
1465 (cua--select-keymaps))
1467 (cond
1468 (cua-mode
1469 (setq cua--saved-state
1470 (list
1471 (and (boundp 'delete-selection-mode) delete-selection-mode)))
1472 (if cua-delete-selection
1473 (delete-selection-mode 1)
1474 (if (and (boundp 'delete-selection-mode) delete-selection-mode)
1475 (delete-selection-mode -1)))
1476 (if cua-highlight-region-shift-only (transient-mark-mode -1))
1477 (cua--deactivate))
1478 (cua--saved-state
1479 (if (nth 0 cua--saved-state)
1480 (delete-selection-mode 1)
1481 (if (and (boundp 'delete-selection-mode) delete-selection-mode)
1482 (delete-selection-mode -1)))
1483 (if (called-interactively-p 'interactive)
1484 (message "CUA mode disabled.%s"
1485 (if (nth 0 cua--saved-state) " Delete-Selection enabled" "")))
1486 (setq cua--saved-state nil))))
1489 ;;;###autoload
1490 (defun cua-selection-mode (arg)
1491 "Enable CUA selection mode without the C-z/C-x/C-c/C-v bindings."
1492 (interactive "P")
1493 (setq-default cua-enable-cua-keys nil)
1494 (if (not (called-interactively-p 'any))
1495 (cua-mode arg)
1496 ;; Use call-interactive to turn a nil prefix arg into `toggle'.
1497 (call-interactively 'cua-mode)
1498 (customize-mark-as-set 'cua-enable-cua-keys)))
1501 (defun cua-debug ()
1502 "Toggle CUA debugging."
1503 (interactive)
1504 (setq cua--debug (not cua--debug)))
1507 (provide 'cua-base)
1509 ;;; cua-base.el ends here