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