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