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