1 ;;; cua-base.el --- emulate CUA key bindings
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Kim F. Storm <storm@cua.dk>
7 ;; Keywords: keyboard emulations convenience cua
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This is the CUA package which provides a complete emulation of the
28 ;; standard CUA key bindings (Motif/Windows/Mac GUI) for selecting and
29 ;; manipulating the region where S-<movement> is used to highlight &
32 ;; CUA style key bindings for cut and paste
33 ;; ----------------------------------------
35 ;; This package allows the C-z, C-x, C-c, and C-v keys to be
36 ;; bound appropriately according to the Motif/Windows GUI, i.e.
42 ;; The tricky part is the handling of the C-x and C-c keys which
43 ;; are normally used as prefix keys for most of emacs' built-in
44 ;; commands. With CUA they still do!!!
46 ;; Only when the region is currently active (and highlighted since
47 ;; transient-mark-mode is used), the C-x and C-c keys will work as CUA
51 ;; When the region is not active, C-x and C-c works as prefix keys!
53 ;; This probably sounds strange and difficult to get used to - but
54 ;; based on my own experience and the feedback from many users of
55 ;; this package, it actually works very well and users adapt to it
56 ;; instantly - or at least very quickly. So give it a try!
57 ;; ... and in the few cases where you make a mistake and accidentally
58 ;; delete the region - you just undo the mistake (with C-z).
60 ;; If you really need to perform a command which starts with one of
61 ;; the prefix keys even when the region is active, you have three options:
62 ;; - press the prefix key twice very quickly (within 0.2 seconds),
63 ;; - press the prefix key and the following key within 0.2 seconds, or
64 ;; - use the SHIFT key with the prefix key, i.e. C-X or C-C
66 ;; This behavior can be customized via the
67 ;; cua-prefix-override-inhibit-delay variable.
69 ;; In addition to using the shifted movement keys, you can also use
70 ;; [C-space] to start the region and use unshifted movement keys to extend
71 ;; it. To cancel the region, use [C-space] or [C-g].
73 ;; If you prefer to use the standard emacs cut, copy, paste, and undo
74 ;; bindings, customize cua-enable-cua-keys to nil.
77 ;; Typing text replaces the region
78 ;; -------------------------------
80 ;; When the region is active, i.e. highlighted, the text in region is
81 ;; replaced by the text you type.
83 ;; The replaced text is saved in register 0 which can be inserted using
84 ;; the key sequence M-0 C-v (see the section on register support below).
86 ;; If you have just replaced a highlighted region with typed text,
87 ;; you can repeat the replace with M-v. This will search forward
88 ;; for a streach of text identical to the previous contents of the
89 ;; region (i.e. the contents of register 0) and replace it with the
90 ;; text you typed to replace the original region. Repeating M-v will
91 ;; replace the next matching region and so on.
93 ;; Example: Suppose you have a line like this
94 ;; The redo operation will redo the last redoable command
95 ;; which you want to change into
96 ;; The repeat operation will repeat the last repeatable command
97 ;; This is done by highlighting the first occurrence of "redo"
98 ;; and type "repeat" M-v M-v.
100 ;; Note: Since CUA-mode duplicates the functionality of the
101 ;; delete-selection-mode, that mode is automatically disabled when
102 ;; CUA-mode is enabled.
105 ;; CUA mode indications
106 ;; --------------------
107 ;; You can choose to let CUA use different cursor colors to indicate
108 ;; overwrite mode and read-only buffers. For example, the following
109 ;; setting will use a RED cursor in normal (insertion) mode in
110 ;; read-write buffers, a YELLOW cursor in overwrite mode in read-write
111 ;; buffers, and a GREEN cursor read-only buffers:
113 ;; (setq cua-normal-cursor-color "red")
114 ;; (setq cua-overwrite-cursor-color "yellow")
115 ;; (setq cua-read-only-cursor-color "green")
118 ;; CUA register support
119 ;; --------------------
120 ;; Emacs' standard register support is also based on a separate set of
121 ;; "register commands".
123 ;; CUA's register support is activated by providing a numeric
124 ;; prefix argument to the C-x, C-c, and C-v commands. For example,
125 ;; to copy the selected region to register 2, enter [M-2 C-c].
126 ;; Or if you have activated the keypad prefix mode, enter [kp-2 C-c].
128 ;; And CUA will copy and paste normal region as well as rectangles
129 ;; into the registers, i.e. you use exactly the same command for both.
131 ;; In addition, the last highlighted text that is deleted (not
132 ;; copied), e.g. by [delete] or by typing text over a highlighted
133 ;; region, is automatically saved in register 0, so you can insert it
136 ;; CUA rectangle support
137 ;; ---------------------
138 ;; Emacs' normal rectangle support is based on interpreting the region
139 ;; between the mark and point as a "virtual rectangle", and using a
140 ;; completely separate set of "rectangle commands" [C-x r ...] on the
141 ;; region to copy, kill, fill a.s.o. the virtual rectangle.
143 ;; cua-mode's superior rectangle support uses a true visual
144 ;; representation of the selected rectangle, i.e. it highlights the
145 ;; actual part of the buffer that is currently selected as part of the
146 ;; rectangle. Unlike emacs' traditional rectangle commands, the
147 ;; selected rectangle always as straight left and right edges, even
148 ;; when those are in the middle of a TAB character or beyond the end
149 ;; of the current line. And it does this without actually modifying
150 ;; the buffer contents (it uses display overlays to visualize the
151 ;; virtual dimensions of the rectangle).
153 ;; This means that cua-mode's rectangles are not limited to the actual
154 ;; contents of the buffer, so if the cursor is currently at the end of a
155 ;; short line, you can still extend the rectangle to include more columns
156 ;; of longer lines in the same rectangle. And you can also have the
157 ;; left edge of a rectangle start in the middle of a TAB character.
158 ;; Sounds strange? Try it!
160 ;; To start a rectangle, use [C-return] and extend it using the normal
161 ;; movement keys (up, down, left, right, home, end, C-home,
162 ;; C-end). Once the rectangle has the desired size, you can cut or
163 ;; copy it using C-x and C-c (or C-w and M-w), and you can
164 ;; subsequently insert it - as a rectangle - using C-v (or C-y). So
165 ;; the only new command you need to know to work with cua-mode
166 ;; rectangles is C-return!
168 ;; Normally, when you paste a rectangle using C-v (C-y), each line of
169 ;; the rectangle is inserted into the existing lines in the buffer.
170 ;; If overwrite-mode is active when you paste a rectangle, it is
171 ;; inserted as normal (multi-line) text.
173 ;; If you prefer the traditional rectangle marking (i.e. don't want
174 ;; straight edges), [M-p] toggles this for the current rectangle,
175 ;; or you can customize cua-virtual-rectangle-edges.
177 ;; And there's more: If you want to extend or reduce the size of the
178 ;; rectangle in one of the other corners of the rectangle, just use
179 ;; [return] to move the cursor to the "next" corner. Or you can use
180 ;; the [M-up], [M-down], [M-left], and [M-right] keys to move the
181 ;; entire rectangle overlay (but not the contents) in the given
184 ;; [C-return] cancels the rectangle
185 ;; [C-space] activates the region bounded by the rectangle
187 ;; If you type a normal (self-inserting) character when the rectangle is
188 ;; active, the character is inserted on the "current side" of every line
189 ;; of the rectangle. The "current side" is the side on which the cursor
190 ;; is currently located. If the rectangle is only 1 column wide,
191 ;; insertion will be performed to the left when the cursor is at the
192 ;; bottom of the rectangle. So, for example, to comment out an entire
193 ;; paragraph like this one, just place the cursor on the first character
194 ;; of the first line, and enter the following:
195 ;; C-return M-} ; ; <space> C-return
197 ;; cua-mode's rectangle support also includes all the normal rectangle
198 ;; functions with easy access:
200 ;; [M-a] aligns all words at the left edge of the rectangle
201 ;; [M-b] fills the rectangle with blanks (tabs and spaces)
202 ;; [M-c] closes the rectangle by removing all blanks at the left edge
204 ;; [M-f] fills the rectangle with a single character (prompt)
205 ;; [M-i] increases the first number found on each line of the rectangle
206 ;; by the amount given by the numeric prefix argument (default 1)
207 ;; It recognizes 0x... as hexadecimal numbers
208 ;; [M-k] kills the rectangle as normal multi-line text (for paste)
209 ;; [M-l] downcases the rectangle
210 ;; [M-m] copies the rectangle as normal multi-line text (for paste)
211 ;; [M-n] fills each line of the rectangle with increasing numbers using
212 ;; a supplied format string (prompt)
213 ;; [M-o] opens the rectangle by moving the highlighted text to the
214 ;; right of the rectangle and filling the rectangle with blanks.
215 ;; [M-p] toggles virtual straight rectangle edges
216 ;; [M-P] inserts tabs and spaces (padding) to make real straight edges
217 ;; [M-q] performs text filling on the rectangle
218 ;; [M-r] replaces REGEXP (prompt) by STRING (prompt) in rectangle
219 ;; [M-R] reverse the lines in the rectangle
220 ;; [M-s] fills each line of the rectangle with the same STRING (prompt)
221 ;; [M-t] performs text fill of the rectangle with TEXT (prompt)
222 ;; [M-u] upcases the rectangle
223 ;; [M-|] runs shell command on rectangle
224 ;; [M-'] restricts rectangle to lines with CHAR (prompt) at left column
225 ;; [M-/] restricts rectangle to lines matching REGEXP (prompt)
226 ;; [C-?] Shows a brief list of the above commands.
228 ;; [M-C-up] and [M-C-down] scrolls the lines INSIDE the rectangle up
229 ;; and down; lines scrolled outside the top or bottom of the rectangle
230 ;; are lost, but can be recovered using [C-z].
234 ;; The final feature provided by CUA is the "global mark", which
235 ;; makes it very easy to copy bits and pieces from the same and other
236 ;; files into the current text. To enable and cancel the global mark,
237 ;; use [S-C-space]. The cursor will blink when the global mark
238 ;; is active. The following commands behave differently when the global
240 ;; <ch> All characters (including newlines) you type are inserted
241 ;; at the global mark!
242 ;; [C-x] If you cut a region or rectangle, it is automatically inserted
243 ;; at the global mark, and the global mark is advanced.
244 ;; [C-c] If you copy a region or rectangle, it is immediately inserted
245 ;; at the global mark, and the global mark is advanced.
246 ;; [C-v] Copies a single character to the global mark.
247 ;; [C-d] Moves (i.e. deletes and inserts) a single character to the
249 ;; [backspace] deletes the character before the global mark, while
250 ;; [delete] deltes the character after the global mark.
252 ;; [S-C-space] Jumps to and cancels the global mark.
253 ;; [C-u S-C-space] Cancels the global mark (stays in current buffer).
255 ;; [TAB] Indents the current line or rectangle to the column of the
263 "Emulate CUA key bindings including C-x and C-c."
265 :group
'editing-basics
269 :link
'(emacs-commentary-link :tag
"Commentary" "cua-base.el")
270 :link
'(emacs-library-link :tag
"Lisp File" "cua-base.el"))
272 (defcustom cua-enable-cua-keys t
273 "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste.
274 If the value is t, these mappings are always enabled. If the value is
275 `shift', these keys are only enabled if the last region was marked with
276 a shifted movement key. If the value is nil, these keys are never
278 :type
'(choice (const :tag
"Disabled" nil
)
279 (const :tag
"Shift region only" shift
)
280 (other :tag
"Enabled" t
))
283 (defcustom cua-remap-control-v t
284 "*If non-nil, C-v binding is used for paste (yank).
285 Also, M-v is mapped to `cua-repeat-replace-region'."
289 (defcustom cua-remap-control-z t
290 "*If non-nil, C-z binding is used for undo."
294 (defcustom cua-highlight-region-shift-only nil
295 "*If non-nil, only highlight region if marked with S-<move>.
296 When this is non-nil, CUA toggles `transient-mark-mode' on when the region
297 is marked using shifted movement keys, and off when the mark is cleared.
298 But when the mark was set using \\[cua-set-mark], Transient Mark mode
303 (defcustom cua-prefix-override-inhibit-delay
304 (if (featurep 'lisp-float-type
) (/ (float 1) (float 5)) nil
)
305 "*If non-nil, time in seconds to delay before overriding prefix key.
306 If there is additional input within this time, the prefix key is
307 used as a normal prefix key. So typing a key sequence quickly will
308 inhibit overriding the prefix key.
309 As a special case, if the prefix keys repeated within this time, the
310 first prefix key is discarded, so typing a prefix key twice in quick
311 succession will also inhibit overriding the prefix key.
312 If the value is nil, use a shifted prefix key to inhibit the override."
313 :type
'(choice (number :tag
"Inhibit delay")
314 (const :tag
"No delay" nil
))
317 (defcustom cua-delete-selection t
318 "*If non-nil, typed text replaces text in the active selection."
319 :type
'(choice (const :tag
"Disabled" nil
)
320 (other :tag
"Enabled" t
))
323 (defcustom cua-keep-region-after-copy nil
324 "If non-nil, don't deselect the region after copying."
328 (defcustom cua-toggle-set-mark t
329 "*If non-nil, the `cua-set-mark' command toggles the mark."
330 :type
'(choice (const :tag
"Disabled" nil
)
331 (other :tag
"Enabled" t
))
334 (defcustom cua-auto-mark-last-change nil
335 "*If non-nil, set implicit mark at position of last buffer change.
336 This means that \\[universal-argument] \\[cua-set-mark] will jump to the position
337 of the last buffer change before jumping to the explicit marks on the mark ring.
338 See `cua-set-mark' for details."
342 (defcustom cua-enable-register-prefix
'not-ctrl-u
343 "*If non-nil, registers are supported via numeric prefix arg.
344 If the value is t, any numeric prefix arg in the range 0 to 9 will be
345 interpreted as a register number.
346 If the value is `not-ctrl-u', using C-u to enter a numeric prefix is not
347 interpreted as a register number.
348 If the value is `ctrl-u-only', only numeric prefix entered with C-u is
349 interpreted as a register number."
350 :type
'(choice (const :tag
"Disabled" nil
)
351 (const :tag
"Enabled, but C-u arg is not a register" not-ctrl-u
)
352 (const :tag
"Enabled, but only for C-u arg" ctrl-u-only
)
353 (other :tag
"Enabled" t
))
356 (defcustom cua-delete-copy-to-register-0 t
357 "*If non-nil, save last deleted region or rectangle to register 0."
361 (defcustom cua-enable-region-auto-help nil
362 "*If non-nil, automatically show help for active region."
366 (defcustom cua-enable-modeline-indications nil
367 "*If non-nil, use minor-mode hook to show status in mode line."
371 (defcustom cua-check-pending-input t
372 "*If non-nil, don't override prefix key if input pending.
373 It is rumoured that `input-pending-p' is unreliable under some window
374 managers, so try setting this to nil, if prefix override doesn't work."
378 (defcustom cua-paste-pop-rotate-temporarily nil
379 "*If non-nil, \\[cua-paste-pop] only rotates the kill-ring temporarily.
380 This means that both \\[yank] and the first \\[yank-pop] in a sequence always insert
381 the most recently killed text. Each immediately following \\[cua-paste-pop] replaces
382 the previous text with the next older element on the `kill-ring'.
383 With prefix arg, \\[universal-argument] \\[yank-pop] inserts the same text as the most
384 recent \\[yank-pop] (or \\[yank]) command."
388 ;;; Rectangle Customization
390 (defcustom cua-virtual-rectangle-edges t
391 "*If non-nil, rectangles have virtual straight edges.
392 Note that although rectangles are always DISPLAYED with straight edges, the
393 buffer is NOT modified, until you execute a command that actually modifies it.
394 M-p toggles this feature when a rectangle is active."
398 (defcustom cua-auto-tabify-rectangles
1000
399 "*If non-nil, automatically tabify after rectangle commands.
400 This basically means that `tabify' is applied to all lines that
401 are modified by inserting or deleting a rectangle. If value is
402 an integer, CUA will look for existing tabs in a region around
403 the rectangle, and only do the conversion if any tabs are already
404 present. The number specifies then number of characters before
405 and after the region marked by the rectangle to search."
406 :type
'(choice (number :tag
"Auto detect (limit)")
407 (const :tag
"Disabled" nil
)
408 (other :tag
"Enabled" t
))
411 (defvar cua-global-keymap
) ; forward
412 (defvar cua--region-keymap
) ; forward
414 (defcustom cua-rectangle-mark-key
[(control return
)]
415 "Global key used to toggle the cua rectangle mark."
416 :set
#'(lambda (symbol value
)
418 (when (and (boundp 'cua--keymaps-initialized
)
419 cua--keymaps-initialized
)
420 (define-key cua-global-keymap value
421 'cua-set-rectangle-mark
)
422 (when (boundp 'cua--rectangle-keymap
)
423 (define-key cua--rectangle-keymap value
424 'cua-clear-rectangle-mark
)
425 (define-key cua--region-keymap value
426 'cua-toggle-rectangle-mark
))))
430 (defcustom cua-rectangle-modifier-key
'meta
431 "*Modifier key used for rectangle commands bindings.
432 On non-window systems, always use the meta modifier.
433 Must be set prior to enabling CUA."
434 :type
'(choice (const :tag
"Meta key" meta
)
435 (const :tag
"Alt key" alt
)
436 (const :tag
"Hyper key" hyper
)
437 (const :tag
"Super key" super
))
440 (defcustom cua-enable-rectangle-auto-help t
441 "*If non-nil, automatically show help for region, rectangle and global mark."
445 (defface cua-rectangle
446 '((default :inherit region
)
447 (((class color
)) :foreground
"white" :background
"maroon"))
448 "*Font used by CUA for highlighting the rectangle."
451 (defface cua-rectangle-noselect
452 '((default :inherit region
)
453 (((class color
)) :foreground
"white" :background
"dimgray"))
454 "*Font used by CUA for highlighting the non-selected rectangle lines."
458 ;;; Global Mark Customization
460 (defcustom cua-global-mark-keep-visible t
461 "*If non-nil, always keep global mark visible in other window."
465 (defface cua-global-mark
466 '((((min-colors 88)(class color
)) :foreground
"black" :background
"yellow1")
467 (((class color
)) :foreground
"black" :background
"yellow")
469 "*Font used by CUA for highlighting the global mark."
472 (defcustom cua-global-mark-blink-cursor-interval
0.20
473 "*Blink cursor at this interval when global mark is active."
474 :type
'(choice (number :tag
"Blink interval")
475 (const :tag
"No blink" nil
))
479 ;;; Cursor Indication Customization
481 (defcustom cua-enable-cursor-indications nil
482 "*If non-nil, use different cursor colors for indications."
486 (defcustom cua-normal-cursor-color
(or (and (boundp 'initial-cursor-color
) initial-cursor-color
)
487 (and (boundp 'initial-frame-alist
)
488 (assoc 'cursor-color initial-frame-alist
)
489 (cdr (assoc 'cursor-color initial-frame-alist
)))
490 (and (boundp 'default-frame-alist
)
491 (assoc 'cursor-color default-frame-alist
)
492 (cdr (assoc 'cursor-color default-frame-alist
)))
493 (frame-parameter nil
'cursor-color
)
495 "Normal (non-overwrite) cursor color.
496 Default is to load cursor color from initial or default frame parameters.
498 If the value is a COLOR name, then only the `cursor-color' attribute will be
499 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
500 then only the `cursor-type' property will be affected. If the value is
501 a cons (TYPE . COLOR), then both properties are affected."
502 :initialize
'custom-initialize-default
506 (const :tag
"Filled box" box
)
507 (const :tag
"Vertical bar" bar
)
508 (const :tag
"Horizontal bar" hbar
)
509 (const :tag
"Hollow box" hollow
))
510 (cons :tag
"Color and Type"
512 (const :tag
"Filled box" box
)
513 (const :tag
"Vertical bar" bar
)
514 (const :tag
"Horizontal bar" hbar
)
515 (const :tag
"Hollow box" hollow
))
516 (color :tag
"Color")))
519 (defcustom cua-read-only-cursor-color
"darkgreen"
520 "*Cursor color used in read-only buffers, if non-nil.
521 Only used when `cua-enable-cursor-indications' is non-nil.
523 If the value is a COLOR name, then only the `cursor-color' attribute will be
524 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
525 then only the `cursor-type' property will be affected. If the value is
526 a cons (TYPE . COLOR), then both properties are affected."
530 (const :tag
"Filled box" box
)
531 (const :tag
"Vertical bar" bar
)
532 (const :tag
"Horizontal bar" hbar
)
533 (const :tag
"Hollow box" hollow
))
534 (cons :tag
"Color and Type"
536 (const :tag
"Filled box" box
)
537 (const :tag
"Vertical bar" bar
)
538 (const :tag
"Horizontal bar" hbar
)
539 (const :tag
"Hollow box" hollow
))
540 (color :tag
"Color")))
543 (defcustom cua-overwrite-cursor-color
"yellow"
544 "*Cursor color used when overwrite mode is set, if non-nil.
545 Only used when `cua-enable-cursor-indications' is non-nil.
547 If the value is a COLOR name, then only the `cursor-color' attribute will be
548 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
549 then only the `cursor-type' property will be affected. If the value is
550 a cons (TYPE . COLOR), then both properties are affected."
554 (const :tag
"Filled box" box
)
555 (const :tag
"Vertical bar" bar
)
556 (const :tag
"Horizontal bar" hbar
)
557 (const :tag
"Hollow box" hollow
))
558 (cons :tag
"Color and Type"
560 (const :tag
"Filled box" box
)
561 (const :tag
"Vertical bar" bar
)
562 (const :tag
"Horizontal bar" hbar
)
563 (const :tag
"Hollow box" hollow
))
564 (color :tag
"Color")))
567 (defcustom cua-global-mark-cursor-color
"cyan"
568 "*Indication for active global mark.
569 Will change cursor color to specified color if string.
570 Only used when `cua-enable-cursor-indications' is non-nil.
572 If the value is a COLOR name, then only the `cursor-color' attribute will be
573 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
574 then only the `cursor-type' property will be affected. If the value is
575 a cons (TYPE . COLOR), then both properties are affected."
579 (const :tag
"Filled box" box
)
580 (const :tag
"Vertical bar" bar
)
581 (const :tag
"Horizontal bar" hbar
)
582 (const :tag
"Hollow box" hollow
))
583 (cons :tag
"Color and Type"
585 (const :tag
"Filled box" box
)
586 (const :tag
"Vertical bar" bar
)
587 (const :tag
"Horizontal bar" hbar
)
588 (const :tag
"Hollow box" hollow
))
589 (color :tag
"Color")))
593 ;;; Rectangle support is in cua-rect.el
595 (autoload 'cua-set-rectangle-mark
"cua-rect"
596 "Start rectangle at mouse click position." t nil
)
598 ;; Stub definitions until it is loaded
599 (defvar cua--rectangle
)
600 (defvar cua--last-killed-rectangle
)
601 (unless (featurep 'cua-rect
)
602 (setq cua--rectangle nil
603 cua--last-killed-rectangle nil
))
605 ;; All behind cua--rectangle tests.
606 (declare-function cua-copy-rectangle
"cua-rect" (arg))
607 (declare-function cua-cut-rectangle
"cua-rect" (arg))
608 (declare-function cua--rectangle-left
"cua-rect" (&optional val
))
609 (declare-function cua--delete-rectangle
"cua-rect" ())
610 (declare-function cua--insert-rectangle
"cua-rect"
611 (rect &optional below paste-column line-count
))
612 (declare-function cua--rectangle-corner
"cua-rect" (&optional advance
))
613 (declare-function cua--rectangle-assert
"cua-rect" ())
615 ;;; Global Mark support is in cua-gmrk.el
617 (autoload 'cua-toggle-global-mark
"cua-gmrk" nil t nil
)
619 ;; Stub definitions until cua-gmrk.el is loaded
620 (defvar cua--global-mark-active
)
621 (unless (featurep 'cua-gmrk
)
622 (setq cua--global-mark-active nil
))
624 (declare-function cua--insert-at-global-mark
"cua-gmrk" (str &optional msg
))
625 (declare-function cua--global-mark-post-command
"cua-gmrk" ())
628 ;;; Low-level Interface
630 (defvar cua-inhibit-cua-keys nil
631 "Buffer-local variable that may disable the CUA keymappings.")
632 (make-variable-buffer-local 'cua-inhibit-cua-keys
)
636 ;; Current region was started using cua-set-mark.
637 (defvar cua--explicit-region-start nil
)
638 (make-variable-buffer-local 'cua--explicit-region-start
)
640 ;; Latest region was started using shifted movement command.
641 (defvar cua--last-region-shifted nil
)
643 ;; buffer + point prior to current command when rectangle is active
644 ;; checked in post-command hook to see if point was moved
645 (defvar cua--buffer-and-point-before-command nil
)
647 ;; status string for mode line indications
648 (defvar cua--status-string nil
)
649 (make-variable-buffer-local 'cua--status-string
)
651 (defvar cua--debug nil
)
654 ;;; Prefix key override mechanism
656 ;; The prefix override (when mark-active) operates in three substates:
657 ;; [1] Before using a prefix key
658 ;; [2] Immediately after using a prefix key
659 ;; [3] A fraction of a second later
661 ;; In state [1], the cua--prefix-override-keymap is active.
662 ;; This keymap binds the C-x and C-c prefix keys to the
663 ;; cua--prefix-override-handler function.
665 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
666 ;; will push back the keys already read to the event queue. If input is
667 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
668 ;; is started, and it changes to state [2].
670 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
671 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
672 ;; C-x to the cua--prefix-repeat-handler function.
674 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
675 ;; will cancel [T], back the keys already read (except for the second prefix
676 ;; keys) to the event queue, and changes to state [3].
678 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
679 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
680 ;; the cua--prefix-override-timeout function will push a `timeout' event on
681 ;; the event queue, and changes to state [3].
683 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
684 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
685 ;; normal prefix key binding from the global or local map will be used.
687 ;; The pre-command hook (executed as a consequence of the timeout or normal
688 ;; prefix key binding) will cancel [T] and change from state [3] back to
689 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
690 ;; are always called with state reset to [1]!
692 ;; State [1] is recognized by cua--prefix-override-timer is nil,
693 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
694 ;; state [3] is recognized by cua--prefix-override-timer is t.
696 (defvar cua--prefix-override-timer nil
)
697 (defvar cua--prefix-override-length nil
)
699 (defun cua--prefix-override-replay (arg repeat
)
700 (let* ((keys (this-command-keys))
702 (key (aref keys
(1- i
))))
703 (setq cua--prefix-override-length
(- i repeat
))
704 (setq cua--prefix-override-timer
706 ;; In state [2], change to state [3]
708 ;; In state [1], change directly to state [3]
709 (and cua-check-pending-input
(input-pending-p))
710 ;; In state [1], [T] disabled, so change to state [3]
711 (not (numberp cua-prefix-override-inhibit-delay
))
712 (<= cua-prefix-override-inhibit-delay
0)
713 ;; In state [1], start [T] and change to state [2]
714 (run-with-timer cua-prefix-override-inhibit-delay nil
715 'cua--prefix-override-timeout
)))
716 ;; Don't record this command
717 (setq this-command last-command
)
718 ;; Restore the prefix arg
719 (setq prefix-arg arg
)
720 (reset-this-command-lengths)
721 ;; Push the key back on the event queue
722 (setq unread-command-events
(cons key unread-command-events
))))
724 (defun cua--prefix-override-handler (arg)
725 "Start timer waiting for prefix key to be followed by another key.
726 Repeating prefix key when region is active works as a single prefix key."
728 (cua--prefix-override-replay arg
0))
730 (defun cua--prefix-repeat-handler (arg)
731 "Repeating prefix key when region is active works as a single prefix key."
733 (cua--prefix-override-replay arg
1))
735 (defun cua--prefix-copy-handler (arg)
736 "Copy region/rectangle, then replay last key."
739 (cua-copy-rectangle arg
)
740 (cua-copy-region arg
))
741 (let ((keys (this-single-command-keys)))
742 (setq unread-command-events
743 (cons (aref keys
(1- (length keys
))) unread-command-events
))))
745 (defun cua--prefix-cut-handler (arg)
746 "Cut region/rectangle, then replay last key."
749 (cua-cut-rectangle arg
)
750 (cua-cut-region arg
))
751 (let ((keys (this-single-command-keys)))
752 (setq unread-command-events
753 (cons (aref keys
(1- (length keys
))) unread-command-events
))))
755 (defun cua--prefix-override-timeout ()
756 (setq cua--prefix-override-timer t
)
757 (when (= (length (this-command-keys)) cua--prefix-override-length
)
758 (setq unread-command-events
(cons 'timeout unread-command-events
))
760 (reset-this-command-lengths)
761 (setq overriding-terminal-local-map nil
))
762 (cua--select-keymaps)))
767 (defun cua--fallback ()
768 ;; Execute original command
769 (setq this-command this-original-command
)
770 (call-interactively this-command
))
772 (defun cua--keep-active ()
774 deactivate-mark nil
))
776 (defun cua--deactivate (&optional now
)
777 (setq cua--explicit-region-start nil
)
779 (setq deactivate-mark t
)
780 (setq mark-active nil
)
781 (run-hooks 'deactivate-mark-hook
)))
783 (defun cua--filter-buffer-noprops (start end
)
784 (let ((str (filter-buffer-substring start end
)))
785 (set-text-properties 0 (length str
) nil str
)
788 ;; The current register prefix
789 (defvar cua--register nil
)
791 (defun cua--prefix-arg (arg)
793 (and cua-enable-register-prefix
794 (integerp arg
) (>= arg
0) (< arg
10)
795 (let* ((prefix (aref (this-command-keys) 0))
796 (ctrl-u-prefix (and (integerp prefix
)
799 ((eq cua-enable-register-prefix
'not-ctrl-u
)
801 ((eq cua-enable-register-prefix
'ctrl-u-only
)
805 (if cua--register nil arg
))
808 ;;; Region specific commands
810 (defvar cua--last-deleted-region-pos nil
)
811 (defvar cua--last-deleted-region-text nil
)
813 (defun cua-delete-region ()
814 "Delete the active region.
815 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
817 (let ((start (mark)) (end (point)))
819 (setq start
(prog1 end
(setq end start
))))
820 (setq cua--last-deleted-region-text
(filter-buffer-substring start end
))
821 (if cua-delete-copy-to-register-0
822 (set-register ?
0 cua--last-deleted-region-text
))
823 (delete-region start end
)
824 (setq cua--last-deleted-region-pos
825 (cons (current-buffer)
826 (and (consp buffer-undo-list
)
827 (car buffer-undo-list
))))
831 (defun cua-replace-region ()
832 "Replace the active region with the character you type."
834 (let ((not-empty (and cua-delete-selection
(cua-delete-region))))
835 (unless (eq this-original-command this-command
)
836 (let ((overwrite-mode
839 (not (eq this-original-command
'self-insert-command
)))))
842 (defun cua-copy-region (arg)
843 "Copy the region to the kill ring.
844 With numeric prefix arg, copy to register 0-9 instead."
846 (setq arg
(cua--prefix-arg arg
))
847 (setq cua--last-killed-rectangle nil
)
848 (let ((start (mark)) (end (point)))
850 (setq start
(prog1 end
(setq end start
))))
853 (copy-to-register cua--register start end nil
))
854 ((eq this-original-command
'clipboard-kill-ring-save
)
855 (clipboard-kill-ring-save start end
))
857 (copy-region-as-kill start end
)))
858 (if cua-keep-region-after-copy
862 (defun cua-cut-region (arg)
863 "Cut the region and copy to the kill ring.
864 With numeric prefix arg, copy to register 0-9 instead."
866 (setq cua--last-killed-rectangle nil
)
868 (cua-copy-region arg
)
869 (setq arg
(cua--prefix-arg arg
))
870 (let ((start (mark)) (end (point)))
872 (setq start
(prog1 end
(setq end start
))))
875 (copy-to-register cua--register start end t
))
876 ((eq this-original-command
'clipboard-kill-region
)
877 (clipboard-kill-region start end
))
879 (kill-region start end
))))
882 ;;; Generic commands for regions, rectangles, and global marks
885 "Cancel the active region, rectangle, or global mark."
887 (setq mark-active nil
)
888 (setq cua--explicit-region-start nil
)
889 (if (fboundp 'cua--cancel-rectangle
)
890 (cua--cancel-rectangle)))
892 (declare-function x-clipboard-yank
"../term/x-win" ())
894 (defun cua-paste (arg)
895 "Paste last cut or copied region or rectangle.
896 An active region is deleted before executing the command.
897 With numeric prefix arg, paste from register 0-9 instead.
898 If global mark is active, copy from register or one character."
900 (setq arg
(cua--prefix-arg arg
))
901 (let ((regtxt (and cua--register
(get-register cua--register
)))
902 (count (prefix-numeric-value arg
))
903 paste-column paste-lines
)
905 ((and cua--register
(not regtxt
))
906 (message "Nothing in register %c" cua--register
))
907 (cua--global-mark-active
909 (cua--insert-at-global-mark regtxt
)
911 (cua--insert-at-global-mark (filter-buffer-substring (point) (+ (point) count
)))
912 (forward-char count
))))
914 (error "Cannot paste into a read-only buffer"))
916 ;; Must save register here, since delete may override reg 0.
920 (goto-char (min (mark) (point)))
921 (setq paste-column
(cua--rectangle-left))
922 (setq paste-lines
(cua--delete-rectangle))
923 (if (= paste-lines
1)
924 (setq paste-lines nil
))) ;; paste all
925 ;; Before a yank command, make sure we don't yank the
926 ;; head of the kill-ring that really comes from the
927 ;; currently active region we are going to delete.
928 ;; That would make yank a no-op.
929 (if (and (string= (filter-buffer-substring (point) (mark))
931 (fboundp 'mouse-region-match
)
932 (mouse-region-match))
934 (cua-delete-region)))
938 ;; This being a cons implies cua-rect is loaded?
939 ((consp regtxt
) (cua--insert-rectangle regtxt
))
940 ((stringp regtxt
) (insert-for-yank regtxt
))
941 (t (message "Unknown data in register %c" cua--register
))))
942 ((and cua--last-killed-rectangle
943 (eq (and kill-ring
(car kill-ring
)) (car cua--last-killed-rectangle
)))
945 (when (not (eq buffer-undo-list t
))
946 (setq this-command
'cua--paste-rectangle
)
948 (setq buffer-undo-list
(cons pt buffer-undo-list
)))
949 (cua--insert-rectangle (cdr cua--last-killed-rectangle
)
950 nil paste-column paste-lines
)
951 (if arg
(goto-char pt
))))
952 ((eq this-original-command
'clipboard-yank
)
954 ((eq this-original-command
'x-clipboard-yank
)
959 ;; cua-paste-pop-rotate-temporarily == t mechanism:
961 ;; C-y M-y M-y => only rotates kill ring temporarily,
962 ;; so next C-y yanks what previous C-y yanked,
964 ;; M-y M-y M-y => equivalent to C-y M-y M-y
966 ;; But: After another command, C-u M-y remembers the temporary
967 ;; kill-ring position, so
968 ;; C-u M-y => yanks what the last M-y yanked
971 (defvar cua-paste-pop-count nil
)
973 (defun cua-paste-pop (arg)
974 "Replace a just-pasted text or rectangle with a different text.
975 See `yank-pop' for details about the default behavior. For an alternative
976 behavior, see `cua-paste-pop-rotate-temporarily'."
979 ((eq last-command
'cua--paste-rectangle
)
982 ((not cua-paste-pop-rotate-temporarily
)
983 (yank-pop (prefix-numeric-value arg
)))
985 (let ((rotate (if (consp arg
) 1 (prefix-numeric-value arg
))))
987 ((or (null cua-paste-pop-count
)
988 (eq last-command
'yank
)
989 (eq last-command
'cua-paste
))
990 (setq cua-paste-pop-count rotate
)
991 (setq last-command
'yank
)
992 (yank-pop cua-paste-pop-count
))
993 ((and (eq last-command
'cua-paste-pop
) (not (consp arg
)))
994 (setq cua-paste-pop-count
(+ cua-paste-pop-count rotate
))
995 (setq last-command
'yank
)
996 (yank-pop cua-paste-pop-count
))
998 (setq cua-paste-pop-count
999 (if (consp arg
) (+ cua-paste-pop-count rotate -
1) 1))
1000 (yank (1+ cua-paste-pop-count
)))))
1001 ;; Undo rotating the kill-ring, so next C-y will
1002 ;; yank the original head.
1003 (setq kill-ring-yank-pointer kill-ring
)
1004 (setq this-command
'cua-paste-pop
))))
1006 (defun cua-exchange-point-and-mark (arg)
1007 "Exchanges point and mark, but don't activate the mark.
1008 Activates the mark if a prefix argument is given."
1011 (setq mark-active t
)
1013 (exchange-point-and-mark)
1015 (cua--rectangle-corner 0)))))
1017 ;; Typed text that replaced the highlighted region.
1018 (defvar cua--repeat-replace-text nil
)
1020 (defun cua-repeat-replace-region (arg)
1021 "Repeat replacing text of highlighted region with typed text.
1022 Searches for the next stretch of text identical to the region last
1023 replaced by typing text over it and replaces it with the same stretch
1026 (when cua--last-deleted-region-pos
1029 (set-buffer (car cua--last-deleted-region-pos
))
1031 ;; Find the text that replaced the region via the undo list.
1032 (let ((ul buffer-undo-list
)
1033 (elt (cdr cua--last-deleted-region-pos
))
1037 (setq u
(car ul
) ul
(cdr ul
))
1039 ((eq u elt
) ;; got it
1041 ((and (consp u
) (integerp (car u
)) (integerp (cdr u
)))
1042 (if (and s
(= (cdr u
) s
))
1044 (setq s
(car u
) e
(cdr u
)))))))
1045 (cond ((and s e
(<= s e
) (= s
(mark t
)))
1046 (setq cua--repeat-replace-text
(cua--filter-buffer-noprops s e
)))
1047 ((and (null s
) (eq u elt
)) ;; nothing inserted
1048 (setq cua--repeat-replace-text
1051 (message "Cannot locate replacement text"))))))
1052 (setq cua--last-deleted-region-pos nil
))
1053 (if (and cua--last-deleted-region-text
1054 cua--repeat-replace-text
1055 (search-forward cua--last-deleted-region-text nil t nil
))
1056 (replace-match cua--repeat-replace-text arg t
)))
1058 (defun cua-help-for-region (&optional help
)
1059 "Show region specific help in echo area."
1062 (concat (if help
"C-?:help " "")
1063 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
1066 ;;; Shift activated / extended region
1068 (defun cua-pop-to-last-change ()
1069 (let ((undo-list buffer-undo-list
)
1071 (while (and (not pos
)
1073 (setq elt
(car undo-list
)
1074 undo-list
(cdr undo-list
))
1079 ((and (integerp (cdr elt
))
1080 (or (integerp (car elt
)) (stringp (car elt
))))
1081 (setq pos
(cdr elt
)))
1082 ((and (eq (car elt
) 'apply
) (consp (cdr elt
)) (integerp (cadr elt
)))
1083 (setq pos
(nth 3 elt
)))))
1086 (>= pos
(point-min)) (<= pos
(point-max)))
1090 (defun cua-set-mark (&optional arg
)
1091 "Set mark at where point is, clear mark, or jump to mark.
1093 With no prefix argument, clear mark if already set. Otherwise, set
1094 mark, and push old mark position on local mark ring; also push mark on
1095 global mark ring if last mark was set in another buffer.
1097 With argument, jump to mark, and pop a new position for mark off
1098 the local mark ring (this does not affect the global mark ring).
1099 Use \\[pop-global-mark] to jump to a mark off the global mark ring
1100 \(see `pop-global-mark').
1102 If `cua-auto-mark-last-change' is non-nil, this command behaves as if there
1103 was an implicit mark at the position of the last buffer change.
1105 Repeating the command without the prefix jumps to the next position
1106 off the local (or global) mark ring.
1108 With a double \\[universal-argument] prefix argument, unconditionally set mark."
1111 ((and (consp arg
) (> (prefix-numeric-value arg
) 4))
1112 (push-mark-command nil
))
1113 ((eq last-command
'pop-to-mark-command
)
1114 (setq this-command
'pop-to-mark-command
)
1115 (pop-to-mark-command))
1116 ((and (eq last-command
'pop-global-mark
) (not arg
))
1117 (setq this-command
'pop-global-mark
)
1120 (setq this-command
'pop-to-mark-command
)
1121 (or (and cua-auto-mark-last-change
1122 (cua-pop-to-last-change))
1123 (pop-to-mark-command)))
1124 ((and cua-toggle-set-mark mark-active
)
1126 (message "Mark Cleared"))
1128 (push-mark-command nil nil
)
1129 (setq cua--explicit-region-start t
)
1130 (setq cua--last-region-shifted nil
)
1131 (if cua-enable-region-auto-help
1132 (cua-help-for-region t
)))))
1134 ;;; Scrolling commands which does not signal errors at top/bottom
1135 ;;; of buffer at first key-press (instead moves to top/bottom
1138 (defun cua-scroll-up (&optional arg
)
1139 "Scroll text of current window upward ARG lines; or near full screen if no ARG.
1140 If window cannot be scrolled further, move cursor to bottom line instead.
1141 A near full screen is `next-screen-context-lines' less than a full screen.
1142 Negative ARG means scroll downward.
1143 If ARG is the atom `-', scroll downward by nearly full screen."
1146 ((eq arg
'-
) (cua-scroll-down nil
))
1147 ((< (prefix-numeric-value arg
) 0)
1148 (cua-scroll-down (- (prefix-numeric-value arg
))))
1150 (scroll-up arg
)) ; signal error
1154 (end-of-buffer (goto-char (point-max)))))))
1156 (put 'cua-scroll-up
'CUA
'move
)
1157 (put 'cua-scroll-up
'isearch-scroll t
)
1159 (defun cua-scroll-down (&optional arg
)
1160 "Scroll text of current window downward ARG lines; or near full screen if no ARG.
1161 If window cannot be scrolled further, move cursor to top line instead.
1162 A near full screen is `next-screen-context-lines' less than a full screen.
1163 Negative ARG means scroll upward.
1164 If ARG is the atom `-', scroll upward by nearly full screen."
1167 ((eq arg
'-
) (cua-scroll-up nil
))
1168 ((< (prefix-numeric-value arg
) 0)
1169 (cua-scroll-up (- (prefix-numeric-value arg
))))
1171 (scroll-down arg
)) ; signal error
1175 (beginning-of-buffer (goto-char (point-min)))))))
1177 (put 'cua-scroll-down
'CUA
'move
)
1178 (put 'cua-scroll-down
'isearch-scroll t
)
1180 ;;; Cursor indications
1182 (defun cua--update-indications ()
1185 ((and cua--global-mark-active
1186 cua-global-mark-cursor-color
)
1187 cua-global-mark-cursor-color
)
1188 ((and buffer-read-only
1189 cua-read-only-cursor-color
)
1190 cua-read-only-cursor-color
)
1191 ((and cua-overwrite-cursor-color overwrite-mode
)
1192 cua-overwrite-cursor-color
)
1193 (t cua-normal-cursor-color
)))
1194 (color (if (consp cursor
) (cdr cursor
) cursor
))
1195 (type (if (consp cursor
) (car cursor
) cursor
)))
1198 (not (equal color
(frame-parameter nil
'cursor-color
))))
1199 (set-cursor-color color
))
1202 (not (eq type
(default-value 'cursor-type
))))
1203 (setq-default cursor-type type
))))
1206 ;;; Pre-command hook
1208 (defun cua--pre-command-handler-1 ()
1209 ;; Cancel prefix key timeout if user enters another key.
1210 (when cua--prefix-override-timer
1211 (if (timerp cua--prefix-override-timer
)
1212 (cancel-timer cua--prefix-override-timer
))
1213 (setq cua--prefix-override-timer nil
))
1216 ;; Only symbol commands can have necessary properties
1217 ((not (symbolp this-command
))
1220 ;; Handle delete-selection property on non-movement commands
1221 ((not (eq (get this-command
'CUA
) 'move
))
1222 (when (and mark-active
(not deactivate-mark
))
1223 (let* ((ds (or (get this-command
'delete-selection
)
1224 (get this-command
'pending-delete
)))
1235 'cua-delete-rectangle
1236 'cua-delete-region
))
1239 'cua-delete-rectangle
;; replace?
1240 'cua-replace-region
)))))
1242 (setq this-original-command this-command
1243 this-command nc
)))))
1245 ;; Handle shifted cursor keys and other movement commands.
1246 ;; If region is not active, region is activated if key is shifted.
1247 ;; If region is active, region is cancelled if key is unshifted
1248 ;; (and region not started with C-SPC).
1249 ;; If rectangle is active, expand rectangle in specified direction and
1250 ;; ignore the movement.
1252 ;; Shortcut for window-system, assuming that input-decode-map is empty.
1253 (memq 'shift
(event-modifiers
1254 (aref (this-single-command-raw-keys) 0)))
1256 ;; Check if the final key-sequence was shifted.
1257 (memq 'shift
(event-modifiers
1258 (aref (this-single-command-keys) 0)))
1259 ;; If not, maybe the raw key-sequence was mapped by input-decode-map
1260 ;; to a shifted key (and then mapped down to its unshifted form).
1261 (let* ((keys (this-single-command-raw-keys))
1262 (ev (lookup-key input-decode-map keys
)))
1263 (or (and (vector ev
) (memq 'shift
(event-modifiers (aref ev
0))))
1264 ;; Or maybe, the raw key-sequence was not an escape sequence
1265 ;; and was shifted (and then mapped down to its unshifted form).
1266 (memq 'shift
(event-modifiers (aref keys
0)))))))
1268 (push-mark-command nil t
))
1269 (setq cua--last-region-shifted t
)
1270 (setq cua--explicit-region-start nil
))
1272 ;; Set mark if user explicitly said to do so
1273 ((or cua--explicit-region-start cua--rectangle
)
1275 (push-mark-command nil nil
)))
1277 ;; Else clear mark after this command.
1279 ;; If we set mark-active to nil here, the region highlight will not be
1280 ;; removed by the direct_output_ commands.
1281 (setq deactivate-mark t
)))
1283 ;; Detect extension of rectangles by mouse or other movement
1284 (setq cua--buffer-and-point-before-command
1285 (if cua--rectangle
(cons (current-buffer) (point)))))
1287 (defun cua--pre-command-handler ()
1290 (cua--pre-command-handler-1)
1293 ;;; Post-command hook
1295 (defun cua--post-command-handler-1 ()
1296 (when cua--global-mark-active
1297 (cua--global-mark-post-command))
1298 (when (fboundp 'cua--rectangle-post-command
)
1299 (cua--rectangle-post-command))
1300 (setq cua--buffer-and-point-before-command nil
)
1301 (if (or (not mark-active
) deactivate-mark
)
1302 (setq cua--explicit-region-start nil
))
1307 (cua--rectangle (cua--rectangle-assert))
1308 (mark-active (message "Mark=%d Point=%d Expl=%s"
1309 (mark t
) (point) cua--explicit-region-start
))))
1311 ;; Disable transient-mark-mode if rectangle active in current buffer.
1312 (if (not (window-minibuffer-p (selected-window)))
1313 (setq transient-mark-mode
(and (not cua--rectangle
)
1314 (if cua-highlight-region-shift-only
1315 (not cua--explicit-region-start
)
1317 (if cua-enable-cursor-indications
1318 (cua--update-indications))
1320 (cua--select-keymaps))
1322 (defun cua--post-command-handler ()
1325 (cua--post-command-handler-1)
1331 ;; Cached value of actual cua-rectangle-modifier-key
1332 (defvar cua--rectangle-modifier-key
'meta
)
1334 (defun cua--M/H-key
(map key fct
)
1335 ;; bind H-KEY or M-KEY to FCT in MAP
1336 (unless (listp key
) (setq key
(list key
)))
1337 (define-key map
(vector (cons cua--rectangle-modifier-key key
)) fct
))
1339 (defun cua--self-insert-char-p (def)
1340 ;; Return DEF if current key sequence is self-inserting in
1342 (if (memq (global-key-binding (this-single-command-keys))
1343 '(self-insert-command self-insert-iso
))
1346 (defvar cua-global-keymap
(make-sparse-keymap)
1347 "Global keymap for cua-mode; users may add to this keymap.")
1349 (defvar cua--cua-keys-keymap
(make-sparse-keymap))
1350 (defvar cua--prefix-override-keymap
(make-sparse-keymap))
1351 (defvar cua--prefix-repeat-keymap
(make-sparse-keymap))
1352 (defvar cua--global-mark-keymap
(make-sparse-keymap)) ; Initialized when cua-gmrk.el is loaded
1353 (defvar cua--rectangle-keymap
(make-sparse-keymap)) ; Initialized when cua-rect.el is loaded
1354 (defvar cua--region-keymap
(make-sparse-keymap))
1356 (defvar cua--ena-cua-keys-keymap nil
)
1357 (defvar cua--ena-prefix-override-keymap nil
)
1358 (defvar cua--ena-prefix-repeat-keymap nil
)
1359 (defvar cua--ena-region-keymap nil
)
1360 (defvar cua--ena-global-mark-keymap nil
)
1362 (defvar cua--keymap-alist
1363 `((cua--ena-prefix-override-keymap .
,cua--prefix-override-keymap
)
1364 (cua--ena-prefix-repeat-keymap .
,cua--prefix-repeat-keymap
)
1365 (cua--ena-cua-keys-keymap .
,cua--cua-keys-keymap
)
1366 (cua--ena-global-mark-keymap .
,cua--global-mark-keymap
)
1367 (cua--rectangle .
,cua--rectangle-keymap
)
1368 (cua--ena-region-keymap .
,cua--region-keymap
)
1369 (cua-mode .
,cua-global-keymap
)))
1371 (defun cua--select-keymaps ()
1372 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
1373 (setq cua--ena-region-keymap
1374 (and mark-active
(not deactivate-mark
)))
1375 (setq cua--ena-prefix-override-keymap
1376 (and cua--ena-region-keymap
1378 (not cua-inhibit-cua-keys
)
1379 (or (eq cua-enable-cua-keys t
)
1380 (not cua--explicit-region-start
))
1381 (not executing-kbd-macro
)
1382 (not cua--prefix-override-timer
)))
1383 (setq cua--ena-prefix-repeat-keymap
1384 (and cua--ena-region-keymap
1385 (or (timerp cua--prefix-override-timer
)
1386 (eq cua--prefix-override-timer
'shift
))))
1387 (setq cua--ena-cua-keys-keymap
1388 (and cua-enable-cua-keys
1389 (not cua-inhibit-cua-keys
)
1390 (or (eq cua-enable-cua-keys t
)
1391 cua--last-region-shifted
)))
1392 (setq cua--ena-global-mark-keymap
1393 (and cua--global-mark-active
1394 (not (window-minibuffer-p)))))
1396 (defvar cua--keymaps-initialized nil
)
1398 (defun cua--shift-control-prefix (prefix arg
)
1399 ;; handle S-C-x and S-C-c by emulating the fast double prefix function.
1400 ;; Don't record this command
1401 (setq this-command last-command
)
1402 ;; Restore the prefix arg
1403 (setq prefix-arg arg
)
1404 (reset-this-command-lengths)
1405 ;; Activate the cua--prefix-repeat-keymap
1406 (setq cua--prefix-override-timer
'shift
)
1407 ;; Push duplicate keys back on the event queue
1408 (setq unread-command-events
(cons prefix
(cons prefix unread-command-events
))))
1410 (defun cua--shift-control-c-prefix (arg)
1412 (cua--shift-control-prefix ?\C-c arg
))
1414 (defun cua--shift-control-x-prefix (arg)
1416 (cua--shift-control-prefix ?\C-x arg
))
1418 (defun cua--init-keymaps ()
1419 ;; Cache actual rectangle modifier key.
1420 (setq cua--rectangle-modifier-key
1421 (if (and cua-rectangle-modifier-key
1422 (memq window-system
'(x)))
1423 cua-rectangle-modifier-key
1425 ;; C-return always toggles rectangle mark
1426 (define-key cua-global-keymap cua-rectangle-mark-key
'cua-set-rectangle-mark
)
1427 (unless (eq cua--rectangle-modifier-key
'meta
)
1428 (cua--M/H-key cua-global-keymap ?\s
'cua-set-rectangle-mark
)
1429 (define-key cua-global-keymap
1430 (vector (list cua--rectangle-modifier-key
'mouse-1
)) 'cua-mouse-set-rectangle-mark
))
1432 (define-key cua-global-keymap
[(shift control ?\s
)] 'cua-toggle-global-mark
)
1434 ;; replace region with rectangle or element on kill ring
1435 (define-key cua-global-keymap
[remap yank
] 'cua-paste
)
1436 (define-key cua-global-keymap
[remap clipboard-yank
] 'cua-paste
)
1437 (define-key cua-global-keymap
[remap x-clipboard-yank
] 'cua-paste
)
1438 ;; replace current yank with previous kill ring element
1439 (define-key cua-global-keymap
[remap yank-pop
] 'cua-paste-pop
)
1441 (define-key cua-global-keymap
[remap set-mark-command
] 'cua-set-mark
)
1444 (define-key cua-global-keymap
[remap scroll-up
] 'cua-scroll-up
)
1445 (define-key cua-global-keymap
[remap scroll-down
] 'cua-scroll-down
)
1446 (define-key cua-global-keymap
[remap scroll-up-command
] 'cua-scroll-up
)
1447 (define-key cua-global-keymap
[remap scroll-down-command
] 'cua-scroll-down
)
1449 (define-key cua--cua-keys-keymap
[(control x
) timeout
] 'kill-region
)
1450 (define-key cua--cua-keys-keymap
[(control c
) timeout
] 'copy-region-as-kill
)
1451 (when cua-remap-control-z
1452 (define-key cua--cua-keys-keymap
[(control z
)] 'undo
))
1453 (when cua-remap-control-v
1454 (define-key cua--cua-keys-keymap
[(control v
)] 'yank
)
1455 (define-key cua--cua-keys-keymap
[(meta v
)] 'cua-repeat-replace-region
))
1456 (define-key cua--cua-keys-keymap
[remap exchange-point-and-mark
] 'cua-exchange-point-and-mark
)
1458 (define-key cua--prefix-override-keymap
[(control x
)] 'cua--prefix-override-handler
)
1459 (define-key cua--prefix-override-keymap
[(control c
)] 'cua--prefix-override-handler
)
1461 (define-key cua--prefix-repeat-keymap
[(control x
) (control x
)] 'cua--prefix-repeat-handler
)
1462 (define-key cua--prefix-repeat-keymap
[(control c
) (control c
)] 'cua--prefix-repeat-handler
)
1463 (dolist (key '(up down left right home end next prior
))
1464 (define-key cua--prefix-repeat-keymap
(vector '(control x
) key
) 'cua--prefix-cut-handler
)
1465 (define-key cua--prefix-repeat-keymap
(vector '(control c
) key
) 'cua--prefix-copy-handler
))
1467 ;; Enable shifted fallbacks for C-x and C-c when region is active
1468 (define-key cua--region-keymap
[(shift control x
)] 'cua--shift-control-x-prefix
)
1469 (define-key cua--region-keymap
[(shift control c
)] 'cua--shift-control-c-prefix
)
1471 ;; replace current region
1472 (define-key cua--region-keymap
[remap self-insert-command
] 'cua-replace-region
)
1473 (define-key cua--region-keymap
[remap self-insert-iso
] 'cua-replace-region
)
1474 (define-key cua--region-keymap
[remap insert-register
] 'cua-replace-region
)
1475 (define-key cua--region-keymap
[remap newline-and-indent
] 'cua-replace-region
)
1476 (define-key cua--region-keymap
[remap newline
] 'cua-replace-region
)
1477 (define-key cua--region-keymap
[remap open-line
] 'cua-replace-region
)
1478 ;; delete current region
1479 (define-key cua--region-keymap
[remap delete-backward-char
] 'cua-delete-region
)
1480 (define-key cua--region-keymap
[remap backward-delete-char
] 'cua-delete-region
)
1481 (define-key cua--region-keymap
[remap backward-delete-char-untabify
] 'cua-delete-region
)
1482 (define-key cua--region-keymap
[remap delete-char
] 'cua-delete-region
)
1484 (define-key cua--region-keymap
[remap kill-region
] 'cua-cut-region
)
1485 (define-key cua--region-keymap
[remap clipboard-kill-region
] 'cua-cut-region
)
1487 (define-key cua--region-keymap
[remap copy-region-as-kill
] 'cua-copy-region
)
1488 (define-key cua--region-keymap
[remap kill-ring-save
] 'cua-copy-region
)
1489 (define-key cua--region-keymap
[remap clipboard-kill-ring-save
] 'cua-copy-region
)
1490 ;; cancel current region/rectangle
1491 (define-key cua--region-keymap
[remap keyboard-escape-quit
] 'cua-cancel
)
1492 (define-key cua--region-keymap
[remap keyboard-quit
] 'cua-cancel
)
1496 ;; Setup standard movement commands to be recognized by CUA.
1499 '(forward-char backward-char
1500 right-char left-char
1501 right-word left-word
1502 next-line previous-line
1503 forward-word backward-word
1504 end-of-line beginning-of-line
1505 end-of-visual-line beginning-of-visual-line
1506 move-end-of-line move-beginning-of-line
1507 end-of-buffer beginning-of-buffer
1508 scroll-up scroll-down
1509 scroll-up-command scroll-down-command
1510 up-list down-list backward-up-list
1511 end-of-defun
beginning-of-defun
1512 forward-sexp backward-sexp
1513 forward-list backward-list
1514 forward-sentence backward-sentence
1515 forward-paragraph backward-paragraph
1516 ;; CC mode motion commands
1517 c-forward-conditional c-backward-conditional
1518 c-down-conditional c-up-conditional
1519 c-down-conditional-with-else c-up-conditional-with-else
1520 c-beginning-of-statement c-end-of-statement
))
1521 (put cmd
'CUA
'move
))
1523 ;; State prior to enabling cua-mode
1524 ;; Value is a list with the following elements:
1525 ;; transient-mark-mode
1526 ;; delete-selection-mode
1527 ;; pc-selection-mode
1529 (defvar cua--saved-state nil
)
1532 (define-minor-mode cua-mode
1533 "Toggle CUA key-binding mode.
1534 When enabled, using shifted movement keys will activate the
1535 region (and highlight the region using `transient-mark-mode'),
1536 and typed text replaces the active selection.
1538 Also when enabled, you can use C-z, C-x, C-c, and C-v to undo,
1539 cut, copy, and paste in addition to the normal Emacs bindings.
1540 The C-x and C-c keys only do cut and copy when the region is
1541 active, so in most cases, they do not conflict with the normal
1542 function of these prefix keys.
1544 If you really need to perform a command which starts with one of
1545 the prefix keys even when the region is active, you have three
1547 - press the prefix key twice very quickly (within 0.2 seconds),
1548 - press the prefix key and the following key within 0.2 seconds, or
1549 - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c.
1551 You can customize `cua-enable-cua-keys' to completely disable the
1552 CUA bindings, or `cua-prefix-override-inhibit-delay' to change
1553 the prefix fallback behavior.
1555 CUA mode manages Transient Mark mode internally. Trying to disable
1556 Transient Mark mode while CUA mode is enabled does not work; if you
1557 only want to highlight the region when it is selected using a
1558 shifted movement key, set `cua-highlight-region-shift-only'."
1561 :set-after
'(cua-enable-modeline-indications
1562 cua-remap-control-v cua-remap-control-z
1563 cua-rectangle-mark-key cua-rectangle-modifier-key
)
1565 :link
'(emacs-commentary-link "cua-base.el")
1566 (setq mark-even-if-inactive t
)
1567 (setq highlight-nonselected-windows nil
)
1569 (unless cua--keymaps-initialized
1571 (setq cua--keymaps-initialized t
))
1575 (add-hook 'pre-command-hook
'cua--pre-command-handler
)
1576 (add-hook 'post-command-hook
'cua--post-command-handler
)
1577 (if (and cua-enable-modeline-indications
(not (assoc 'cua-mode minor-mode-alist
)))
1578 (setq minor-mode-alist
(cons '(cua-mode cua--status-string
) minor-mode-alist
)))
1579 (if cua-enable-cursor-indications
1580 (cua--update-indications)))
1582 (remove-hook 'pre-command-hook
'cua--pre-command-handler
)
1583 (remove-hook 'post-command-hook
'cua--post-command-handler
))
1586 (setq emulation-mode-map-alists
(delq 'cua--keymap-alist emulation-mode-map-alists
))
1587 (add-to-ordered-list 'emulation-mode-map-alists
'cua--keymap-alist
400)
1588 (cua--select-keymaps))
1592 (setq cua--saved-state
1595 (and (boundp 'delete-selection-mode
) delete-selection-mode
)
1596 (and (boundp 'pc-selection-mode
) pc-selection-mode
)
1598 (if (and (boundp 'delete-selection-mode
) delete-selection-mode
)
1599 (delete-selection-mode -
1))
1600 (if (and (boundp 'pc-selection-mode
) pc-selection-mode
)
1601 (pc-selection-mode -
1))
1603 (setq shift-select-mode nil
)
1604 (setq transient-mark-mode
(and cua-mode
1605 (if cua-highlight-region-shift-only
1606 (not cua--explicit-region-start
)
1609 (setq transient-mark-mode
(car cua--saved-state
))
1610 (if (nth 1 cua--saved-state
)
1611 (delete-selection-mode 1))
1612 (if (nth 2 cua--saved-state
)
1613 (pc-selection-mode 1))
1614 (setq shift-select-mode
(nth 3 cua--saved-state
))
1615 (if (called-interactively-p 'interactive
)
1616 (message "CUA mode disabled.%s%s%s%s"
1617 (if (nth 1 cua--saved-state
) " Delete-Selection" "")
1618 (if (and (nth 1 cua--saved-state
) (nth 2 cua--saved-state
)) " and" "")
1619 (if (nth 2 cua--saved-state
) " PC-Selection" "")
1620 (if (or (nth 1 cua--saved-state
) (nth 2 cua--saved-state
)) " enabled" "")))
1621 (setq cua--saved-state nil
))))
1625 (defun cua-selection-mode (arg)
1626 "Enable CUA selection mode without the C-z/C-x/C-c/C-v bindings."
1628 (setq-default cua-enable-cua-keys nil
)
1633 "Toggle CUA debugging."
1635 (setq cua--debug
(not cua--debug
)))
1640 ;; arch-tag: 21fb6289-ba25-4fee-bfdc-f9fb351acf05
1641 ;;; cua-base.el ends here