(reveal-mode-map): Bind C-a to beginning-of-line.
[bpt/emacs.git] / lisp / emulation / cua-base.el
CommitLineData
72cc582e
KS
1;;; cua-base.el --- emulate CUA key bindings
2
4905133f 3;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
abca72d7 4;; Free Software Foundation, Inc.
72cc582e
KS
5
6;; Author: Kim F. Storm <storm@cua.dk>
7;; Keywords: keyboard emulation convenience cua
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
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
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
26
27;;; Commentary:
28
29;; This is the CUA package which provides a complete emulation of the
30;; standard CUA key bindings (Motif/Windows/Mac GUI) for selecting and
31;; manipulating the region where S-<movement> is used to highlight &
32;; extend the region.
33
34a63289
KS
34;; CUA style key bindings for cut and paste
35;; ----------------------------------------
36
37;; This package allows the C-z, C-x, C-c, and C-v keys to be
72cc582e
KS
38;; bound appropriately according to the Motif/Windows GUI, i.e.
39;; C-z -> undo
40;; C-x -> cut
41;; C-c -> copy
42;; C-v -> paste
43;;
44;; The tricky part is the handling of the C-x and C-c keys which
45;; are normally used as prefix keys for most of emacs' built-in
46;; commands. With CUA they still do!!!
47;;
48;; Only when the region is currently active (and highlighted since
49;; transient-mark-mode is used), the C-x and C-c keys will work as CUA
50;; keys
51;; C-x -> cut
52;; C-c -> copy
53;; When the region is not active, C-x and C-c works as prefix keys!
54;;
55;; This probably sounds strange and difficult to get used to - but
56;; based on my own experience and the feedback from many users of
57;; this package, it actually works very well and users adapt to it
a1506d29 58;; instantly - or at least very quickly. So give it a try!
72cc582e
KS
59;; ... and in the few cases where you make a mistake and accidentally
60;; delete the region - you just undo the mistake (with C-z).
61;;
62;; If you really need to perform a command which starts with one of
63;; the prefix keys even when the region is active, you have three options:
64;; - press the prefix key twice very quickly (within 0.2 seconds),
65;; - press the prefix key and the following key within 0.2 seconds), or
66;; - use the SHIFT key with the prefix key, i.e. C-X or C-C
67;;
68;; This behaviour can be customized via the
69;; cua-prefix-override-inhibit-delay variable.
70
71;; In addition to using the shifted movement keys, you can also use
72;; [C-space] to start the region and use unshifted movement keys to extend
73;; it. To cancel the region, use [C-space] or [C-g].
74
75;; If you prefer to use the standard emacs cut, copy, paste, and undo
76;; bindings, customize cua-enable-cua-keys to nil.
77
34a63289
KS
78
79;; Typing text replaces the region
80;; -------------------------------
81
82;; When the region is active, i.e. highlighted, the text in region is
83;; replaced by the text you type.
84
85;; The replaced text is saved in register 0 which can be inserted using
86;; the key sequence M-0 C-v (see the section on register support below).
87
88;; If you have just replaced a highlighted region with typed text,
89;; you can repeat the replace with M-v. This will search forward
90;; for a streach of text identical to the previous contents of the
91;; region (i.e. the contents of register 0) and replace it with the
92;; text you typed to replace the original region. Repeating M-v will
93;; replace the next matching region and so on.
94;;
95;; Example: Suppose you have a line like this
96;; The redo operation will redo the last redoable command
97;; which you want to change into
98;; The repeat operation will repeat the last repeatable command
99;; This is done by highlighting the first occurrence of "redo"
100;; and type "repeat" M-v M-v.
101
102;; Note: Since CUA-mode duplicates the functionality of the
103;; delete-selection-mode, that mode is automatically disabled when
104;; CUA-mode is enabled.
105
106
72cc582e
KS
107;; CUA mode indications
108;; --------------------
109;; You can choose to let CUA use different cursor colors to indicate
110;; overwrite mode and read-only buffers. For example, the following
111;; setting will use a RED cursor in normal (insertion) mode in
112;; read-write buffers, a YELLOW cursor in overwrite mode in read-write
113;; buffers, and a GREEN cursor read-only buffers:
114;;
115;; (setq cua-normal-cursor-color "red")
116;; (setq cua-overwrite-cursor-color "yellow")
117;; (setq cua-read-only-cursor-color "green")
118;;
119
120;; CUA register support
121;; --------------------
122;; Emacs' standard register support is also based on a separate set of
123;; "register commands".
a1506d29 124;;
72cc582e
KS
125;; CUA's register support is activated by providing a numeric
126;; prefix argument to the C-x, C-c, and C-v commands. For example,
127;; to copy the selected region to register 2, enter [M-2 C-c].
128;; Or if you have activated the keypad prefix mode, enter [kp-2 C-c].
a1506d29 129;;
72cc582e
KS
130;; And CUA will copy and paste normal region as well as rectangles
131;; into the registers, i.e. you use exactly the same command for both.
a1506d29 132;;
72cc582e
KS
133;; In addition, the last highlighted text that is deleted (not
134;; copied), e.g. by [delete] or by typing text over a highlighted
135;; region, is automatically saved in register 0, so you can insert it
136;; using [M-0 C-v].
137
138;; CUA rectangle support
139;; ---------------------
140;; Emacs' normal rectangle support is based on interpreting the region
141;; between the mark and point as a "virtual rectangle", and using a
142;; completely separate set of "rectangle commands" [C-x r ...] on the
143;; region to copy, kill, fill a.s.o. the virtual rectangle.
a1506d29 144;;
85d392cb
KS
145;; cua-mode's superior rectangle support uses a true visual
146;; representation of the selected rectangle, i.e. it highlights the
147;; actual part of the buffer that is currently selected as part of the
148;; rectangle. Unlike emacs' traditional rectangle commands, the
149;; selected rectangle always as straight left and right edges, even
150;; when those are in the middle of a TAB character or beyond the end
151;; of the current line. And it does this without actually modifying
152;; the buffer contents (it uses display overlays to visualize the
153;; virtual dimensions of the rectangle).
154;;
155;; This means that cua-mode's rectangles are not limited to the actual
156;; contents of the buffer, so if the cursor is currently at the end of a
157;; short line, you can still extend the rectangle to include more columns
158;; of longer lines in the same rectangle. And you can also have the
159;; left edge of a rectangle start in the middle of a TAB character.
160;; Sounds strange? Try it!
161;;
162;; To start a rectangle, use [S-return] and extend it using the normal
163;; movement keys (up, down, left, right, home, end, C-home,
164;; C-end). Once the rectangle has the desired size, you can cut or
165;; copy it using C-x and C-c (or C-w and M-w), and you can
166;; subsequently insert it - as a rectangle - using C-v (or C-y). So
167;; the only new command you need to know to work with cua-mode
168;; rectangles is S-return!
72cc582e
KS
169;;
170;; Normally, when you paste a rectangle using C-v (C-y), each line of
171;; the rectangle is inserted into the existing lines in the buffer.
172;; If overwrite-mode is active when you paste a rectangle, it is
173;; inserted as normal (multi-line) text.
a1506d29 174;;
85d392cb
KS
175;; If you prefer the traditional rectangle marking (i.e. don't want
176;; straight edges), [M-p] toggles this for the current rectangle,
177;; or you can customize cua-virtual-rectangle-edges.
72cc582e
KS
178
179;; And there's more: If you want to extend or reduce the size of the
180;; rectangle in one of the other corners of the rectangle, just use
181;; [return] to move the cursor to the "next" corner. Or you can use
182;; the [M-up], [M-down], [M-left], and [M-right] keys to move the
183;; entire rectangle overlay (but not the contents) in the given
184;; direction.
185;;
186;; [S-return] cancels the rectangle
187;; [C-space] activates the region bounded by the rectangle
188
189;; If you type a normal (self-inserting) character when the rectangle is
190;; active, the character is inserted on the "current side" of every line
191;; of the rectangle. The "current side" is the side on which the cursor
192;; is currently located. If the rectangle is only 1 column wide,
193;; insertion will be performed to the left when the cursor is at the
194;; bottom of the rectangle. So, for example, to comment out an entire
195;; paragraph like this one, just place the cursor on the first character
196;; of the first line, and enter the following:
197;; S-return M-} ; ; <space> S-return
a1506d29 198
72cc582e
KS
199;; cua-mode's rectangle support also includes all the normal rectangle
200;; functions with easy access:
201;;
202;; [M-a] aligns all words at the left edge of the rectangle
203;; [M-b] fills the rectangle with blanks (tabs and spaces)
204;; [M-c] closes the rectangle by removing all blanks at the left edge
205;; of the rectangle
206;; [M-f] fills the rectangle with a single character (prompt)
207;; [M-i] increases the first number found on each line of the rectangle
208;; by the amount given by the numeric prefix argument (default 1)
209;; It recognizes 0x... as hexadecimal numbers
210;; [M-k] kills the rectangle as normal multi-line text (for paste)
211;; [M-l] downcases the rectangle
212;; [M-m] copies the rectangle as normal multi-line text (for paste)
213;; [M-n] fills each line of the rectangle with increasing numbers using
214;; a supplied format string (prompt)
a1506d29 215;; [M-o] opens the rectangle by moving the highlighted text to the
72cc582e 216;; right of the rectangle and filling the rectangle with blanks.
85d392cb
KS
217;; [M-p] toggles virtual straight rectangle edges
218;; [M-P] inserts tabs and spaces (padding) to make real straight edges
72cc582e
KS
219;; [M-q] performs text filling on the rectangle
220;; [M-r] replaces REGEXP (prompt) by STRING (prompt) in rectangle
221;; [M-R] reverse the lines in the rectangle
222;; [M-s] fills each line of the rectangle with the same STRING (prompt)
223;; [M-t] performs text fill of the rectangle with TEXT (prompt)
224;; [M-u] upcases the rectangle
225;; [M-|] runs shell command on rectangle
226;; [M-'] restricts rectangle to lines with CHAR (prompt) at left column
227;; [M-/] restricts rectangle to lines matching REGEXP (prompt)
228;; [C-?] Shows a brief list of the above commands.
229
230;; [M-C-up] and [M-C-down] scrolls the lines INSIDE the rectangle up
231;; and down; lines scrolled outside the top or bottom of the rectangle
232;; are lost, but can be recovered using [C-z].
233
234;; CUA Global Mark
a1506d29 235;; ---------------
72cc582e
KS
236;; The final feature provided by CUA is the "global mark", which
237;; makes it very easy to copy bits and pieces from the same and other
238;; files into the current text. To enable and cancel the global mark,
239;; use [S-C-space]. The cursor will blink when the global mark
240;; is active. The following commands behave differently when the global
241;; mark is set:
a1506d29 242;; <ch> All characters (including newlines) you type are inserted
72cc582e
KS
243;; at the global mark!
244;; [C-x] If you cut a region or rectangle, it is automatically inserted
245;; at the global mark, and the global mark is advanced.
246;; [C-c] If you copy a region or rectangle, it is immediately inserted
247;; at the global mark, and the global mark is advanced.
248;; [C-v] Copies a single character to the global mark.
249;; [C-d] Moves (i.e. deletes and inserts) a single character to the
250;; global mark.
251;; [backspace] deletes the character before the global mark, while
252;; [delete] deltes the character after the global mark.
253
254;; [S-C-space] Jumps to and cancels the global mark.
255;; [C-u S-C-space] Cancels the global mark (stays in current buffer).
256
257;; [TAB] Indents the current line or rectangle to the column of the
258;; global mark.
259
260;;; Code:
261
262;;; Customization
263
264(defgroup cua nil
265 "Emulate CUA key bindings including C-x and C-c."
266 :prefix "cua"
267 :group 'editing-basics
268 :group 'convenience
269 :group 'emulations
bf247b6e 270 :version "22.1"
72cc582e
KS
271 :link '(emacs-commentary-link :tag "Commentary" "cua-base.el")
272 :link '(emacs-library-link :tag "Lisp File" "cua-base.el"))
273
72cc582e
KS
274(defcustom cua-enable-cua-keys t
275 "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste.
276If the value is t, these mappings are always enabled. If the value is
277'shift, these keys are only enabled if the last region was marked with
278a shifted movement key. If the value is nil, these keys are never
279enabled."
a1506d29 280 :type '(choice (const :tag "Disabled" nil)
72cc582e 281 (const :tag "Shift region only" shift)
c1cdea1a 282 (other :tag "Enabled" t))
72cc582e
KS
283 :group 'cua)
284
285(defcustom cua-highlight-region-shift-only nil
286 "*If non-nil, only highlight region if marked with S-<move>.
287When this is non-nil, CUA toggles `transient-mark-mode' on when the region
288is marked using shifted movement keys, and off when the mark is cleared.
289But when the mark was set using \\[cua-set-mark], transient-mark-mode
290is not turned on."
291 :type 'boolean
292 :group 'cua)
293
a1506d29 294(defcustom cua-prefix-override-inhibit-delay
72cc582e
KS
295 (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) nil)
296 "*If non-nil, time in seconds to delay before overriding prefix key.
297If there is additional input within this time, the prefix key is
298used as a normal prefix key. So typing a key sequence quickly will
299inhibit overriding the prefix key.
300As a special case, if the prefix keys repeated within this time, the
301first prefix key is discarded, so typing a prefix key twice in quick
302succession will also inhibit overriding the prefix key.
303If the value is nil, use a shifted prefix key to inhibit the override."
304 :type '(choice (number :tag "Inhibit delay")
305 (const :tag "No delay" nil))
306 :group 'cua)
307
308(defcustom cua-keep-region-after-copy nil
309 "If non-nil, don't deselect the region after copying."
310 :type 'boolean
311 :group 'cua)
312
313(defcustom cua-enable-register-prefix 'not-ctrl-u
314 "*If non-nil, registers are supported via numeric prefix arg.
315If the value is t, any numeric prefix arg in the range 0 to 9 will be
a1506d29 316interpreted as a register number.
72cc582e 317If the value is not-ctrl-u, using C-u to enter a numeric prefix is not
a1506d29 318interpreted as a register number.
72cc582e
KS
319If the value is ctrl-u-only, only numeric prefix entered with C-u is
320interpreted as a register number."
a1506d29 321 :type '(choice (const :tag "Disabled" nil)
72cc582e
KS
322 (const :tag "Enabled, but C-u arg is not a register" not-ctrl-u)
323 (const :tag "Enabled, but only for C-u arg" ctrl-u-only)
c1cdea1a 324 (other :tag "Enabled" t))
72cc582e
KS
325 :group 'cua)
326
327(defcustom cua-delete-copy-to-register-0 t
328 "*If non-nil, save last deleted region or rectangle to register 0."
329 :type 'boolean
330 :group 'cua)
331
332(defcustom cua-use-hyper-key nil
333 "*If non-nil, bind rectangle commands to H-? instead of M-?.
334If set to 'also, toggle region command is also on S-return.
335Must be set prior to enabling CUA."
336 :type '(choice (const :tag "Meta key and S-return" nil)
337 (const :tag "Hyper key only" only)
338 (const :tag "Hyper key and S-return" also))
339 :group 'cua)
340
341(defcustom cua-enable-region-auto-help nil
342 "*If non-nil, automatically show help for active region."
343 :type 'boolean
344 :group 'cua)
345
346(defcustom cua-enable-modeline-indications nil
347 "*If non-nil, use minor-mode hook to show status in mode line."
348 :type 'boolean
349 :group 'cua)
350
351(defcustom cua-check-pending-input t
352 "*If non-nil, don't override prefix key if input pending.
353It is rumoured that input-pending-p is unreliable under some window
354managers, so try setting this to nil, if prefix override doesn't work."
355 :type 'boolean
356 :group 'cua)
357
358
359;;; Rectangle Customization
360
85d392cb
KS
361(defcustom cua-virtual-rectangle-edges t
362 "*If non-nil, rectangles have virtual straight edges.
363Note that although rectangles are always DISPLAYED with straight edges, the
364buffer is NOT modified, until you execute a command that actually modifies it.
365\[M-p] toggles this feature when a rectangle is active."
72cc582e
KS
366 :type 'boolean
367 :group 'cua)
368
85d392cb
KS
369(defcustom cua-auto-tabify-rectangles 1000
370 "*If non-nil, automatically tabify after rectangle commands.
371This basically means that `tabify' is applied to all lines that
372are modified by inserting or deleting a rectangle. If value is
373an integer, cua will look for existing tabs in a region around
374the rectangle, and only do the conversion if any tabs are already
375present. The number specifies then number of characters before
376and after the region marked by the rectangle to search."
377 :type '(choice (number :tag "Auto detect (limit)")
378 (const :tag "Disabled" nil)
379 (other :tag "Enabled" t))
380 :group 'cua)
381
72cc582e
KS
382(defcustom cua-enable-rectangle-auto-help t
383 "*If non-nil, automatically show help for region, rectangle and global mark."
384 :type 'boolean
385 :group 'cua)
386
387(defface cua-rectangle-face 'nil
388 "*Font used by CUA for highlighting the rectangle."
389 :group 'cua)
390
391(defface cua-rectangle-noselect-face 'nil
392 "*Font used by CUA for highlighting the non-selected rectangle lines."
393 :group 'cua)
394
72cc582e
KS
395
396;;; Global Mark Customization
397
398(defcustom cua-global-mark-keep-visible t
399 "*If non-nil, always keep global mark visible in other window."
400 :type 'boolean
401 :group 'cua)
402
a1506d29 403(defface cua-global-mark-face '((((class color))
80d9508a
SM
404 :foreground "black"
405 :background "yellow")
406 (t :bold t))
72cc582e
KS
407 "*Font used by CUA for highlighting the global mark."
408 :group 'cua)
409
410(defcustom cua-global-mark-blink-cursor-interval 0.20
411 "*Blink cursor at this interval when global mark is active."
412 :type '(choice (number :tag "Blink interval")
413 (const :tag "No blink" nil))
414 :group 'cua)
415
416
417;;; Cursor Indication Customization
418
cadc926b 419(defcustom cua-enable-cursor-indications nil
72cc582e
KS
420 "*If non-nil, use different cursor colors for indications."
421 :type 'boolean
422 :group 'cua)
423
496c17d6
KS
424(defcustom cua-normal-cursor-color (or (and (boundp 'initial-cursor-color) initial-cursor-color)
425 (and (boundp 'initial-frame-alist)
426 (assoc 'cursor-color initial-frame-alist)
427 (cdr (assoc 'cursor-color initial-frame-alist)))
428 (and (boundp 'default-frame-alist)
429 (assoc 'cursor-color default-frame-alist)
430 (cdr (assoc 'cursor-color default-frame-alist)))
431 (frame-parameter nil 'cursor-color)
432 "red")
72cc582e 433 "Normal (non-overwrite) cursor color.
2ed2415d
KS
434Default is to load cursor color from initial or default frame parameters.
435
436If the value is a COLOR name, then only the `cursor-color' attribute will be
b1227a16 437affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
2ed2415d
KS
438then only the `cursor-type' property will be affected. If the value is
439a cons (TYPE . COLOR), then both properties are affected."
496c17d6 440 :initialize 'custom-initialize-default
2ed2415d
KS
441 :type '(choice
442 (color :tag "Color")
443 (choice :tag "Type"
444 (const :tag "Filled box" box)
445 (const :tag "Vertical bar" bar)
446 (const :tag "Horisontal bar" hbar)
b1227a16 447 (const :tag "Hollow box" hollow))
2ed2415d
KS
448 (cons :tag "Color and Type"
449 (choice :tag "Type"
450 (const :tag "Filled box" box)
451 (const :tag "Vertical bar" bar)
452 (const :tag "Horisontal bar" hbar)
b1227a16 453 (const :tag "Hollow box" hollow))
2ed2415d 454 (color :tag "Color")))
72cc582e
KS
455 :group 'cua)
456
457(defcustom cua-read-only-cursor-color "darkgreen"
3ffc8a8a 458 "*Cursor color used in read-only buffers, if non-nil.
2ed2415d
KS
459Only used when `cua-enable-cursor-indications' is non-nil.
460
461If the value is a COLOR name, then only the `cursor-color' attribute will be
b1227a16 462affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
2ed2415d
KS
463then only the `cursor-type' property will be affected. If the value is
464a cons (TYPE . COLOR), then both properties are affected."
465 :type '(choice
466 (color :tag "Color")
467 (choice :tag "Type"
468 (const :tag "Filled box" box)
469 (const :tag "Vertical bar" bar)
470 (const :tag "Horisontal bar" hbar)
b1227a16 471 (const :tag "Hollow box" hollow))
2ed2415d
KS
472 (cons :tag "Color and Type"
473 (choice :tag "Type"
474 (const :tag "Filled box" box)
475 (const :tag "Vertical bar" bar)
476 (const :tag "Horisontal bar" hbar)
b1227a16 477 (const :tag "Hollow box" hollow))
2ed2415d 478 (color :tag "Color")))
72cc582e
KS
479 :group 'cua)
480
481(defcustom cua-overwrite-cursor-color "yellow"
482 "*Cursor color used when overwrite mode is set, if non-nil.
2ed2415d
KS
483Only used when `cua-enable-cursor-indications' is non-nil.
484
485If the value is a COLOR name, then only the `cursor-color' attribute will be
b1227a16 486affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
2ed2415d
KS
487then only the `cursor-type' property will be affected. If the value is
488a cons (TYPE . COLOR), then both properties are affected."
489 :type '(choice
490 (color :tag "Color")
491 (choice :tag "Type"
492 (const :tag "Filled box" box)
493 (const :tag "Vertical bar" bar)
494 (const :tag "Horisontal bar" hbar)
b1227a16 495 (const :tag "Hollow box" hollow))
2ed2415d
KS
496 (cons :tag "Color and Type"
497 (choice :tag "Type"
498 (const :tag "Filled box" box)
499 (const :tag "Vertical bar" bar)
500 (const :tag "Horisontal bar" hbar)
b1227a16 501 (const :tag "Hollow box" hollow))
2ed2415d 502 (color :tag "Color")))
72cc582e
KS
503 :group 'cua)
504
505(defcustom cua-global-mark-cursor-color "cyan"
506 "*Indication for active global mark.
3ffc8a8a 507Will change cursor color to specified color if string.
2ed2415d
KS
508Only used when `cua-enable-cursor-indications' is non-nil.
509
510If the value is a COLOR name, then only the `cursor-color' attribute will be
b1227a16 511affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
2ed2415d
KS
512then only the `cursor-type' property will be affected. If the value is
513a cons (TYPE . COLOR), then both properties are affected."
514 :type '(choice
515 (color :tag "Color")
516 (choice :tag "Type"
517 (const :tag "Filled box" box)
518 (const :tag "Vertical bar" bar)
519 (const :tag "Horisontal bar" hbar)
b1227a16 520 (const :tag "Hollow box" hollow))
2ed2415d
KS
521 (cons :tag "Color and Type"
522 (choice :tag "Type"
523 (const :tag "Filled box" box)
524 (const :tag "Vertical bar" bar)
525 (const :tag "Horisontal bar" hbar)
b1227a16 526 (const :tag "Hollow box" hollow))
2ed2415d 527 (color :tag "Color")))
72cc582e
KS
528 :group 'cua)
529
530
531;;; Rectangle support is in cua-rect.el
532
533(autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
534
535;; Stub definitions until it is loaded
536
537(when (not (featurep 'cua-rect))
538 (defvar cua--rectangle)
539 (setq cua--rectangle nil)
540 (defvar cua--last-killed-rectangle)
541 (setq cua--last-killed-rectangle nil))
542
543
544
545;;; Global Mark support is in cua-gmrk.el
546
547(autoload 'cua-toggle-global-mark "cua-gmrk.el" nil t nil)
548
549;; Stub definitions until cua-gmrk.el is loaded
550
551(when (not (featurep 'cua-gmrk))
552 (defvar cua--global-mark-active)
553 (setq cua--global-mark-active nil))
554
555
556(provide 'cua-base)
557
558(eval-when-compile
559 (require 'cua-rect)
560 (require 'cua-gmrk)
561 )
562
63e6f5b3
KS
563
564;;; Low-level Interface
565
566(defvar cua-inhibit-cua-keys nil
567 "Buffer-local variable that may disable the cua keymappings.")
568(make-variable-buffer-local 'cua-inhibit-cua-keys)
569
72cc582e
KS
570;;; Aux. variables
571
572;; Current region was started using cua-set-mark.
573(defvar cua--explicit-region-start nil)
574
575;; Latest region was started using shifted movement command.
576(defvar cua--last-region-shifted nil)
577
578;; buffer + point prior to current command when rectangle is active
579;; checked in post-command hook to see if point was moved
580(defvar cua--buffer-and-point-before-command nil)
581
582;; status string for mode line indications
583(defvar cua--status-string nil)
584
585(defvar cua--debug nil)
586
587
588;;; Prefix key override mechanism
589
590;; The prefix override (when mark-active) operates in three substates:
591;; [1] Before using a prefix key
592;; [2] Immediately after using a prefix key
593;; [3] A fraction of a second later
594
595;; In state [1], the cua--prefix-override-keymap is active.
596;; This keymap binds the C-x and C-c prefix keys to the
597;; cua--prefix-override-handler function.
598
599;; When a prefix key is typed in state [1], cua--prefix-override-handler
600;; will push back the keys already read to the event queue. If input is
601;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
602;; is started, and it changes to state [2].
603
604;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
605;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
606;; C-x to the cua--prefix-repeat-handler function.
607
608;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
609;; will cancel [T], back the keys already read (except for the second prefix
610;; keys) to the event queue, and changes to state [3].
611
612;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
613;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
614;; the cua--prefix-override-timeout function will push a `timeout' event on
615;; the event queue, and changes to state [3].
616
617;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
618;; are inactive, so the timeout in cua-global-keymap binding is used, or the
619;; normal prefix key binding from the global or local map will be used.
620
621;; The pre-command hook (executed as a consequence of the timeout or normal
622;; prefix key binding) will cancel [T] and change from state [3] back to
623;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
624;; are always called with state reset to [1]!
625
626;; State [1] is recognized by cua--prefix-override-timer is nil,
627;; state [2] is recognized by cua--prefix-override-timer is a timer, and
628;; state [3] is recognized by cua--prefix-override-timer is t.
629
630(defvar cua--prefix-override-timer nil)
631(defvar cua--prefix-override-length nil)
632
633(defun cua--prefix-override-replay (arg repeat)
634 (let* ((keys (this-command-keys))
635 (i (length keys))
636 (key (aref keys (1- i))))
637 (setq cua--prefix-override-length (- i repeat))
638 (setq cua--prefix-override-timer
639 (or
640 ;; In state [2], change to state [3]
641 (> repeat 0)
642 ;; In state [1], change directly to state [3]
643 (and cua-check-pending-input (input-pending-p))
644 ;; In state [1], [T] disabled, so change to state [3]
645 (not (numberp cua-prefix-override-inhibit-delay))
646 (<= cua-prefix-override-inhibit-delay 0)
647 ;; In state [1], start [T] and change to state [2]
a1506d29 648 (run-with-timer cua-prefix-override-inhibit-delay nil
72cc582e
KS
649 'cua--prefix-override-timeout)))
650 ;; Don't record this command
651 (setq this-command last-command)
652 ;; Restore the prefix arg
653 (setq prefix-arg arg)
654 (reset-this-command-lengths)
655 ;; Push the key back on the event queue
656 (setq unread-command-events (cons key unread-command-events))))
657
658(defun cua--prefix-override-handler (arg)
659 "Start timer waiting for prefix key to be followed by another key.
660Repeating prefix key when region is active works as a single prefix key."
661 (interactive "P")
662 (cua--prefix-override-replay arg 0))
663
664(defun cua--prefix-repeat-handler (arg)
665 "Repeating prefix key when region is active works as a single prefix key."
666 (interactive "P")
667 (cua--prefix-override-replay arg 1))
668
669(defun cua--prefix-copy-handler (arg)
670 "Copy region/rectangle, then replay last key."
671 (interactive "P")
672 (if cua--rectangle
673 (cua-copy-rectangle arg)
674 (cua-copy-region arg))
675 (let ((keys (this-single-command-keys)))
a1506d29 676 (setq unread-command-events
72cc582e
KS
677 (cons (aref keys (1- (length keys))) unread-command-events))))
678
679(defun cua--prefix-cut-handler (arg)
680 "Cut region/rectangle, then replay last key."
681 (interactive "P")
682 (if cua--rectangle
683 (cua-cut-rectangle arg)
684 (cua-cut-region arg))
685 (let ((keys (this-single-command-keys)))
a1506d29 686 (setq unread-command-events
72cc582e
KS
687 (cons (aref keys (1- (length keys))) unread-command-events))))
688
689(defun cua--prefix-override-timeout ()
690 (setq cua--prefix-override-timer t)
691 (when (= (length (this-command-keys)) cua--prefix-override-length)
692 (setq unread-command-events (cons 'timeout unread-command-events))
693 (if prefix-arg
694 (reset-this-command-lengths)
695 (setq overriding-terminal-local-map nil))
a23a8440 696 (cua--select-keymaps)))
72cc582e
KS
697
698
699;;; Aux. functions
700
701(defun cua--fallback ()
702 ;; Execute original command
703 (setq this-command this-original-command)
704 (call-interactively this-command))
a1506d29 705
72cc582e
KS
706(defun cua--keep-active ()
707 (setq mark-active t
708 deactivate-mark nil))
709
710(defun cua--deactivate (&optional now)
711 (setq cua--explicit-region-start nil)
712 (if (not now)
713 (setq deactivate-mark t)
714 (setq mark-active nil)
715 (run-hooks 'deactivate-mark-hook)))
716
717
718;; The current register prefix
719(defvar cua--register nil)
720
721(defun cua--prefix-arg (arg)
a1506d29 722 (setq cua--register
72cc582e 723 (and cua-enable-register-prefix
72cc582e 724 (integerp arg) (>= arg 0) (< arg 10)
1c62c12f
KS
725 (let* ((prefix (aref (this-command-keys) 0))
726 (ctrl-u-prefix (and (integerp prefix)
5632c006 727 (= prefix ?\C-u))))
a1506d29 728 (cond
5632c006
KS
729 ((eq cua-enable-register-prefix 'not-ctrl-u)
730 (not ctrl-u-prefix))
731 ((eq cua-enable-register-prefix 'ctrl-u-only)
732 ctrl-u-prefix)
733 (t t)))
72cc582e
KS
734 (+ arg ?0)))
735 (if cua--register nil arg))
736
72cc582e
KS
737
738;;; Region specific commands
739
5834ac92
KS
740(defvar cua--last-deleted-region-pos nil)
741(defvar cua--last-deleted-region-text nil)
742
72cc582e
KS
743(defun cua-delete-region ()
744 "Delete the active region.
745Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
746 (interactive)
747 (let ((start (mark)) (end (point)))
748 (or (<= start end)
749 (setq start (prog1 end (setq end start))))
5834ac92 750 (setq cua--last-deleted-region-text (buffer-substring start end))
72cc582e 751 (if cua-delete-copy-to-register-0
5834ac92 752 (set-register ?0 cua--last-deleted-region-text))
72cc582e 753 (delete-region start end)
a1506d29 754 (setq cua--last-deleted-region-pos
5834ac92
KS
755 (cons (current-buffer)
756 (and (consp buffer-undo-list)
757 (car buffer-undo-list))))
72cc582e
KS
758 (cua--deactivate)))
759
760(defun cua-replace-region ()
761 "Replace the active region with the character you type."
762 (interactive)
763 (cua-delete-region)
5834ac92
KS
764 (unless (eq this-original-command this-command)
765 (cua--fallback)))
72cc582e
KS
766
767(defun cua-copy-region (arg)
768 "Copy the region to the kill ring.
769With numeric prefix arg, copy to register 0-9 instead."
770 (interactive "P")
771 (setq arg (cua--prefix-arg arg))
772 (setq cua--last-killed-rectangle nil)
773 (let ((start (mark)) (end (point)))
774 (or (<= start end)
775 (setq start (prog1 end (setq end start))))
776 (if cua--register
777 (copy-to-register cua--register start end nil)
778 (copy-region-as-kill start end))
779 (if cua-keep-region-after-copy
780 (cua--keep-active)
781 (cua--deactivate))))
782
783(defun cua-cut-region (arg)
784 "Cut the region and copy to the kill ring.
785With numeric prefix arg, copy to register 0-9 instead."
786 (interactive "P")
787 (setq cua--last-killed-rectangle nil)
788 (if buffer-read-only
789 (cua-copy-region arg)
790 (setq arg (cua--prefix-arg arg))
791 (let ((start (mark)) (end (point)))
792 (or (<= start end)
793 (setq start (prog1 end (setq end start))))
794 (if cua--register
795 (copy-to-register cua--register start end t)
796 (kill-region start end)))
797 (cua--deactivate)))
798
799;;; Generic commands for regions, rectangles, and global marks
800
801(defun cua-cancel ()
802 "Cancel the active region, rectangle, or global mark."
803 (interactive)
804 (setq mark-active nil)
805 (setq cua--explicit-region-start nil)
806 (if (fboundp 'cua--cancel-rectangle)
807 (cua--cancel-rectangle)))
808
809(defun cua-paste (arg)
810 "Paste last cut or copied region or rectangle.
811An active region is deleted before executing the command.
812With numeric prefix arg, paste from register 0-9 instead.
813If global mark is active, copy from register or one character."
814 (interactive "P")
815 (setq arg (cua--prefix-arg arg))
816 (let ((regtxt (and cua--register (get-register cua--register)))
85d392cb
KS
817 (count (prefix-numeric-value arg))
818 paste-column paste-lines)
72cc582e
KS
819 (cond
820 ((and cua--register (not regtxt))
821 (message "Nothing in register %c" cua--register))
822 (cua--global-mark-active
823 (if regtxt
824 (cua--insert-at-global-mark regtxt)
825 (when (not (eobp))
826 (cua--insert-at-global-mark (buffer-substring (point) (+ (point) count)))
827 (forward-char count))))
828 (buffer-read-only
829 (message "Cannot paste into a read-only buffer"))
830 (t
831 ;; Must save register here, since delete may override reg 0.
832 (if mark-active
833 ;; Before a yank command, make sure we don't yank
834 ;; the same region that we are going to delete.
835 ;; That would make yank a no-op.
836 (if cua--rectangle
85d392cb
KS
837 (progn
838 (goto-char (min (mark) (point)))
839 (setq paste-column (cua--rectangle-left))
840 (setq paste-lines (cua--delete-rectangle))
841 (if (= paste-lines 1)
842 (setq paste-lines nil))) ;; paste all
72cc582e
KS
843 (if (string= (buffer-substring (point) (mark))
844 (car kill-ring))
845 (current-kill 1))
846 (cua-delete-region)))
847 (cond
848 (regtxt
849 (cond
850 ((consp regtxt) (cua--insert-rectangle regtxt))
851 ((stringp regtxt) (insert-for-yank regtxt))
852 (t (message "Unknown data in register %c" cua--register))))
853 ((and cua--last-killed-rectangle
854 (eq (and kill-ring (car kill-ring)) (car cua--last-killed-rectangle)))
855 (let ((pt (point)))
856 (when (not (eq buffer-undo-list t))
857 (setq this-command 'cua--paste-rectangle)
858 (undo-boundary)
859 (setq buffer-undo-list (cons pt buffer-undo-list)))
85d392cb
KS
860 (cua--insert-rectangle (cdr cua--last-killed-rectangle)
861 nil paste-column paste-lines)
72cc582e
KS
862 (if arg (goto-char pt))))
863 (t (yank arg)))))))
864
865(defun cua-paste-pop (arg)
866 "Replace a just-pasted text or rectangle with a different text.
867See `yank-pop' for details."
868 (interactive "P")
869 (if (eq last-command 'cua--paste-rectangle)
870 (progn
871 (undo)
872 (yank arg))
873 (yank-pop (prefix-numeric-value arg))))
874
875(defun cua-exchange-point-and-mark (arg)
876 "Exchanges point and mark, but don't activate the mark.
877Activates the mark if a prefix argument is given."
878 (interactive "P")
879 (if arg
880 (setq mark-active t)
881 (let (mark-active)
882 (exchange-point-and-mark)
883 (if cua--rectangle
884 (cua--rectangle-corner 0)))))
885
34a63289
KS
886;; Typed text that replaced the highlighted region.
887(defvar cua--repeat-replace-text nil)
888
889(defun cua-repeat-replace-region (arg)
890 "Repeat replacing text of highlighted region with typed text.
891Searches for the next streach of text identical to the region last
892replaced by typing text over it and replaces it with the same streach
5834ac92 893of text."
34a63289 894 (interactive "P")
5834ac92
KS
895 (when cua--last-deleted-region-pos
896 (save-excursion
897 (save-restriction
898 (set-buffer (car cua--last-deleted-region-pos))
899 (widen)
900 ;; Find the text that replaced the region via the undo list.
901 (let ((ul buffer-undo-list)
902 (elt (cdr cua--last-deleted-region-pos))
903 u s e)
904 (when elt
905 (while (consp ul)
906 (setq u (car ul) ul (cdr ul))
907 (cond
908 ((eq u elt) ;; got it
909 (setq ul nil))
910 ((and (consp u) (integerp (car u)) (integerp (cdr u)))
911 (if (and s (= (cdr u) s))
912 (setq s (car u))
913 (setq s (car u) e (cdr u)))))))
914 (setq cua--repeat-replace-text
915 (cond ((and s e (<= s e) (= s (mark t)))
916 (buffer-substring-no-properties s e))
917 ((and (null s) (eq u elt)) ;; nothing inserted
918 "")
919 (t
920 (message "Cannot locate replacement text")
921 nil))))))
922 (setq cua--last-deleted-region-pos nil))
923 (if (and cua--last-deleted-region-text
924 cua--repeat-replace-text
925 (search-forward cua--last-deleted-region-text nil t nil))
926 (replace-match cua--repeat-replace-text arg t)))
34a63289 927
72cc582e
KS
928(defun cua-help-for-region (&optional help)
929 "Show region specific help in echo area."
930 (interactive)
a1506d29 931 (message
72cc582e
KS
932 (concat (if help "C-?:help " "")
933 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
934
935
936;;; Shift activated / extended region
937
938(defun cua-set-mark (&optional arg)
939 "Set mark at where point is, clear mark, or jump to mark.
f45a6831
KS
940
941With no prefix argument, clear mark if already set. Otherwise, set
942mark, and push old mark position on local mark ring; also push mark on
3ffc8a8a 943global mark ring if last mark was set in another buffer.
f45a6831
KS
944
945With argument, jump to mark, and pop a new position for mark off
946the local mark ring \(this does not affect the global mark ring\).
947Use \\[pop-global-mark] to jump to a mark off the global mark ring
ce92443f
KS
948\(see `pop-global-mark'\).
949
950Repeating the command without the prefix jumps to the next position
951off the local \(or global\) mark ring.
f45a6831
KS
952
953With a double \\[universal-argument] prefix argument, unconditionally set mark."
72cc582e 954 (interactive "P")
72cc582e 955 (cond
f45a6831
KS
956 ((and (consp arg) (> (prefix-numeric-value arg) 4))
957 (push-mark-command nil))
fc803e8d 958 ((eq last-command 'pop-to-mark-command)
f45a6831
KS
959 (setq this-command 'pop-to-mark-command)
960 (pop-to-mark-command))
ce92443f 961 ((and (eq last-command 'pop-global-mark) (not arg))
f45a6831
KS
962 (setq this-command 'pop-global-mark)
963 (pop-global-mark))
72cc582e 964 (arg
fc803e8d
KS
965 (setq this-command 'pop-to-mark-command)
966 (pop-to-mark-command))
72cc582e
KS
967 (mark-active
968 (cua--deactivate)
969 (message "Mark Cleared"))
970 (t
fc803e8d 971 (push-mark-command nil nil)
72cc582e
KS
972 (setq cua--explicit-region-start t)
973 (setq cua--last-region-shifted nil)
974 (if cua-enable-region-auto-help
975 (cua-help-for-region t)))))
976
82851d10
KS
977;;; Scrolling commands which does not signal errors at top/bottom
978;;; of buffer at first key-press (instead moves to top/bottom
979;;; of buffer).
980
981(defun cua-scroll-up (&optional arg)
982 "Scroll text of current window upward ARG lines; or near full screen if no ARG.
983If window cannot be scrolled further, move cursor to bottom line instead.
984A near full screen is `next-screen-context-lines' less than a full screen.
985Negative ARG means scroll downward.
986If ARG is the atom `-', scroll downward by nearly full screen."
987 (interactive "P")
988 (cond
989 ((eq arg '-) (cua-scroll-down nil))
990 ((< (prefix-numeric-value arg) 0)
991 (cua-scroll-down (- (prefix-numeric-value arg))))
992 ((eobp)
993 (scroll-up arg)) ; signal error
994 (t
995 (condition-case nil
996 (scroll-up arg)
997 (end-of-buffer (goto-char (point-max)))))))
998
4905133f
KS
999(put 'cua-scroll-up 'CUA 'move)
1000
82851d10
KS
1001(defun cua-scroll-down (&optional arg)
1002 "Scroll text of current window downward ARG lines; or near full screen if no ARG.
1003If window cannot be scrolled further, move cursor to top line instead.
1004A near full screen is `next-screen-context-lines' less than a full screen.
1005Negative ARG means scroll upward.
1006If ARG is the atom `-', scroll upward by nearly full screen."
1007 (interactive "P")
1008 (cond
1009 ((eq arg '-) (cua-scroll-up nil))
1010 ((< (prefix-numeric-value arg) 0)
1011 (cua-scroll-up (- (prefix-numeric-value arg))))
1012 ((bobp)
1013 (scroll-down arg)) ; signal error
1014 (t
1015 (condition-case nil
1016 (scroll-down arg)
1017 (beginning-of-buffer (goto-char (point-min)))))))
1018
4905133f
KS
1019(put 'cua-scroll-up 'CUA 'move)
1020
72cc582e
KS
1021;;; Cursor indications
1022
1023(defun cua--update-indications ()
2ed2415d
KS
1024 (let* ((cursor
1025 (cond
1026 ((and cua--global-mark-active
1027 cua-global-mark-cursor-color)
1028 cua-global-mark-cursor-color)
1029 ((and buffer-read-only
1030 cua-read-only-cursor-color)
1031 cua-read-only-cursor-color)
85d392cb 1032 ((and cua-overwrite-cursor-color overwrite-mode)
2ed2415d
KS
1033 cua-overwrite-cursor-color)
1034 (t cua-normal-cursor-color)))
1035 (color (if (consp cursor) (cdr cursor) cursor))
1036 (type (if (consp cursor) (car cursor) cursor)))
1037 (if (and color
1038 (stringp color)
1039 (not (equal color (frame-parameter nil 'cursor-color))))
1040 (set-cursor-color color))
1041 (if (and type
1042 (symbolp type)
6aa06cd8 1043 (not (eq type default-cursor-type)))
2ed2415d 1044 (setq default-cursor-type type))))
72cc582e
KS
1045
1046
1047;;; Pre-command hook
1048
1049(defun cua--pre-command-handler ()
1050 (condition-case nil
4905133f 1051 (let ((movement (eq (get this-command 'CUA) 'move)))
72cc582e
KS
1052
1053 ;; Cancel prefix key timeout if user enters another key.
1054 (when cua--prefix-override-timer
1055 (if (timerp cua--prefix-override-timer)
1056 (cancel-timer cua--prefix-override-timer))
1057 (setq cua--prefix-override-timer nil))
1058
1059 ;; Handle shifted cursor keys and other movement commands.
1060 ;; If region is not active, region is activated if key is shifted.
1061 ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC).
1062 ;; If rectangle is active, expand rectangle in specified direction and ignore the movement.
1063 (if movement
1064 (cond
ff996427
KS
1065 ((memq 'shift (event-modifiers
1066 (aref (if window-system
1067 (this-single-command-raw-keys)
1068 (this-single-command-keys)) 0)))
5acfef08 1069 (unless mark-active
fc803e8d 1070 (push-mark-command nil t))
72cc582e
KS
1071 (setq cua--last-region-shifted t)
1072 (setq cua--explicit-region-start nil))
1073 ((or cua--explicit-region-start cua--rectangle)
1074 (unless mark-active
fc803e8d 1075 (push-mark-command nil nil)))
72cc582e 1076 (t
a1506d29 1077 ;; If we set mark-active to nil here, the region highlight will not be
72cc582e
KS
1078 ;; removed by the direct_output_ commands.
1079 (setq deactivate-mark t)))
1080
1081 ;; Handle delete-selection property on other commands
e75d337c
KS
1082 (if (and mark-active (not deactivate-mark))
1083 (let* ((ds (or (get this-command 'delete-selection)
1084 (get this-command 'pending-delete)))
1085 (nc (cond
1086 ((not ds) nil)
a1506d29 1087 ((eq ds 'yank)
e75d337c
KS
1088 'cua-paste)
1089 ((eq ds 'kill)
1090 (if cua--rectangle
1091 'cua-copy-rectangle
1092 'cua-copy-region))
1093 ((eq ds 'supersede)
1094 (if cua--rectangle
ad9de65b
KS
1095 'cua-delete-rectangle
1096 'cua-delete-region))
e75d337c
KS
1097 (t
1098 (if cua--rectangle
ad9de65b
KS
1099 'cua-delete-rectangle ;; replace?
1100 'cua-replace-region)))))
e75d337c
KS
1101 (if nc
1102 (setq this-original-command this-command
1103 this-command nc)))))
a1506d29 1104
72cc582e 1105 ;; Detect extension of rectangles by mouse or other movement
a1506d29 1106 (setq cua--buffer-and-point-before-command
72cc582e
KS
1107 (if cua--rectangle (cons (current-buffer) (point))))
1108 )
1109 (error nil)))
1110
1111;;; Post-command hook
1112
1113(defun cua--post-command-handler ()
1114 (condition-case nil
1115 (progn
1116 (when cua--global-mark-active
1117 (cua--global-mark-post-command))
1118 (when (fboundp 'cua--rectangle-post-command)
1119 (cua--rectangle-post-command))
1120 (setq cua--buffer-and-point-before-command nil)
1121 (if (or (not mark-active) deactivate-mark)
1122 (setq cua--explicit-region-start nil))
1123
1124 ;; Debugging
1125 (if cua--debug
a1506d29 1126 (cond
72cc582e
KS
1127 (cua--rectangle (cua--rectangle-assert))
1128 (mark-active (message "Mark=%d Point=%d Expl=%s"
1129 (mark t) (point) cua--explicit-region-start))))
1130
1131 ;; Disable transient-mark-mode if rectangle active in current buffer.
1132 (if (not (window-minibuffer-p (selected-window)))
1133 (setq transient-mark-mode (and (not cua--rectangle)
1134 (if cua-highlight-region-shift-only
1135 (not cua--explicit-region-start)
1136 t))))
1137 (if cua-enable-cursor-indications
1138 (cua--update-indications))
1139
a23a8440 1140 (cua--select-keymaps)
72cc582e
KS
1141 )
1142
1143 (error nil)))
1144
1145
1146;;; Keymaps
1147
1148(defun cua--M/H-key (map key fct)
1149 ;; bind H-KEY or M-KEY to FCT in MAP
1150 (if (eq key 'space) (setq key ? ))
1151 (unless (listp key) (setq key (list key)))
1152 (define-key map (vector (cons (if cua-use-hyper-key 'hyper 'meta) key)) fct))
1153
20023b9c
KS
1154(defun cua--self-insert-char-p (def)
1155 ;; Return DEF if current key sequence is self-inserting in
1156 ;; global-map.
1157 (if (memq (global-key-binding (this-single-command-keys))
1158 '(self-insert-command self-insert-iso))
1159 def nil))
1160
a23a8440
KS
1161(defvar cua-global-keymap (make-sparse-keymap)
1162 "Global keymap for cua-mode; users may add to this keymap.")
1163
72cc582e
KS
1164(defvar cua--cua-keys-keymap (make-sparse-keymap))
1165(defvar cua--prefix-override-keymap (make-sparse-keymap))
1166(defvar cua--prefix-repeat-keymap (make-sparse-keymap))
1167(defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
1168(defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
1169(defvar cua--region-keymap (make-sparse-keymap))
1170
1171(defvar cua--ena-cua-keys-keymap nil)
1172(defvar cua--ena-prefix-override-keymap nil)
1173(defvar cua--ena-prefix-repeat-keymap nil)
1174(defvar cua--ena-region-keymap nil)
1175(defvar cua--ena-global-mark-keymap nil)
1176
a23a8440
KS
1177(defvar cua--keymap-alist
1178 `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap)
1179 (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap)
1180 (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap)
1181 (cua--ena-global-mark-keymap . ,cua--global-mark-keymap)
1182 (cua--rectangle . ,cua--rectangle-keymap)
1183 (cua--ena-region-keymap . ,cua--region-keymap)
1184 (cua-mode . ,cua-global-keymap)))
1185
1186(defun cua--select-keymaps ()
1187 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
72cc582e
KS
1188 (setq cua--ena-region-keymap
1189 (and mark-active (not deactivate-mark)))
1190 (setq cua--ena-prefix-override-keymap
1191 (and cua--ena-region-keymap
1192 cua-enable-cua-keys
63e6f5b3 1193 (not cua-inhibit-cua-keys)
72cc582e
KS
1194 (or (eq cua-enable-cua-keys t)
1195 (not cua--explicit-region-start))
1196 (not executing-kbd-macro)
1197 (not cua--prefix-override-timer)))
1198 (setq cua--ena-prefix-repeat-keymap
1199 (and cua--ena-region-keymap
1200 (timerp cua--prefix-override-timer)))
1201 (setq cua--ena-cua-keys-keymap
1202 (and cua-enable-cua-keys
63e6f5b3 1203 (not cua-inhibit-cua-keys)
72cc582e
KS
1204 (or (eq cua-enable-cua-keys t)
1205 cua--last-region-shifted)))
1206 (setq cua--ena-global-mark-keymap
1207 (and cua--global-mark-active
1208 (not (window-minibuffer-p)))))
1209
1210(defvar cua--keymaps-initalized nil)
1211
1212(defun cua--init-keymaps ()
1213 (unless (eq cua-use-hyper-key 'only)
1214 (define-key cua-global-keymap [(shift return)] 'cua-set-rectangle-mark))
1215 (when cua-use-hyper-key
1216 (cua--M/H-key cua-global-keymap 'space 'cua-set-rectangle-mark)
1217 (define-key cua-global-keymap [(hyper mouse-1)] 'cua-mouse-set-rectangle-mark))
1218
1219 (define-key cua-global-keymap [(shift control ? )] 'cua-toggle-global-mark)
1220
1221 ;; replace region with rectangle or element on kill ring
1222 (define-key cua-global-keymap [remap yank] 'cua-paste)
1223 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1224 ;; replace current yank with previous kill ring element
1225 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1226 ;; set mark
1227 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
72cc582e 1228
82851d10
KS
1229 ;; scrolling
1230 (define-key cua-global-keymap [remap scroll-up] 'cua-scroll-up)
1231 (define-key cua-global-keymap [remap scroll-down] 'cua-scroll-down)
1232
72cc582e 1233 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
72cc582e 1234 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
72cc582e
KS
1235 (define-key cua--cua-keys-keymap [(control z)] 'undo)
1236 (define-key cua--cua-keys-keymap [(control v)] 'yank)
34a63289 1237 (define-key cua--cua-keys-keymap [(meta v)] 'cua-repeat-replace-region)
72cc582e
KS
1238 (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1239
1240 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1241 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
a1506d29 1242
72cc582e
KS
1243 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1244 (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler)
1245 (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler)
1246 (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler)
1247 (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler)
1248 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1249 (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler)
1250 (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler)
1251 (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler)
1252 (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler)
1253
a1506d29 1254 ;; Enable shifted fallbacks for C-x and C-c when region is active
d6f9ff15
KS
1255 (define-key cua--region-keymap [(shift control x)] 'Control-X-prefix)
1256 (define-key cua--region-keymap [(shift control c)] 'mode-specific-command-prefix)
72cc582e
KS
1257 ;; replace current region
1258 (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region)
1259 (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region)
1260 (define-key cua--region-keymap [remap insert-register] 'cua-replace-region)
1261 (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region)
1262 (define-key cua--region-keymap [remap newline] 'cua-replace-region)
1263 (define-key cua--region-keymap [remap open-line] 'cua-replace-region)
1264 ;; delete current region
1265 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1266 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1267 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1268 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1269 ;; kill region
1270 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1271 ;; copy region
1272 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1273 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1274 ;; cancel current region/rectangle
1275 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1276 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1277 )
1278
4905133f
KS
1279
1280;; Setup standard movement commands to be recognized by CUA.
1281
1282(dolist (cmd
1283 '(forward-char backward-char
1284 next-line previous-line
1285 forward-word backward-word
1286 end-of-line beginning-of-line
1287 end-of-buffer beginning-of-buffer
1288 scroll-up scroll-down
1289 forward-sentence backward-sentence
1290 forward-paragraph backward-paragraph))
1291 (put cmd 'CUA 'move))
1292
cadc926b
KS
1293;; State prior to enabling cua-mode
1294;; Value is a list with the following elements:
1295;; transient-mark-mode
1296;; delete-selection-mode
1297;; pc-selection-mode
1298
1299(defvar cua--saved-state nil)
72cc582e
KS
1300
1301;;;###autoload
80d9508a 1302(define-minor-mode cua-mode
72cc582e
KS
1303 "Toggle CUA key-binding mode.
1304When enabled, using shifted movement keys will activate the region (and
1305highlight the region using `transient-mark-mode'), and typed text replaces
1306the active selection. C-z, C-x, C-c, and C-v will undo, cut, copy, and
1307paste (in addition to the normal emacs bindings)."
80d9508a
SM
1308 :global t
1309 :set-after '(cua-enable-modeline-indications cua-use-hyper-key)
1310 :require 'cua-base
1311 :link '(emacs-commentary-link "cua-base.el")
72cc582e
KS
1312 (setq mark-even-if-inactive t)
1313 (setq highlight-nonselected-windows nil)
1314 (make-variable-buffer-local 'cua--explicit-region-start)
1315 (make-variable-buffer-local 'cua--status-string)
1316
1317 (unless cua--keymaps-initalized
1318 (cua--init-keymaps)
1319 (setq cua--keymaps-initalized t))
1320
1321 (if cua-mode
1322 (progn
1323 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1324 (add-hook 'post-command-hook 'cua--post-command-handler)
1325 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1326 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
2ed2415d
KS
1327 (if cua-enable-cursor-indications
1328 (cua--update-indications)))
1329
72cc582e
KS
1330 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1331 (remove-hook 'post-command-hook 'cua--post-command-handler))
a23a8440
KS
1332
1333 (if (not cua-mode)
1334 (setq emulation-mode-map-alists (delq 'cua--keymap-alist emulation-mode-map-alists))
1335 (add-to-list 'emulation-mode-map-alists 'cua--keymap-alist)
1336 (cua--select-keymaps))
1337
cadc926b
KS
1338 (cond
1339 (cua-mode
1340 (setq cua--saved-state
1341 (list
1342 transient-mark-mode
1343 (and (boundp 'delete-selection-mode) delete-selection-mode)
1344 (and (boundp 'pc-selection-mode) pc-selection-mode)))
1345 (if (and (boundp 'delete-selection-mode) delete-selection-mode)
d7672cf8 1346 (delete-selection-mode -1))
cadc926b 1347 (if (and (boundp 'pc-selection-mode) pc-selection-mode)
d7672cf8 1348 (pc-selection-mode -1))
ef2887a2 1349 (cua--deactivate)
cadc926b
KS
1350 (setq transient-mark-mode (and cua-mode
1351 (if cua-highlight-region-shift-only
1352 (not cua--explicit-region-start)
80d9508a 1353 t))))
cadc926b
KS
1354 (cua--saved-state
1355 (setq transient-mark-mode (car cua--saved-state))
1356 (if (nth 1 cua--saved-state)
1357 (delete-selection-mode 1))
1358 (if (nth 2 cua--saved-state)
1359 (pc-selection-mode 1))
1360 (if (interactive-p)
1361 (message "CUA mode disabled.%s%s%s%s"
1362 (if (nth 1 cua--saved-state) " Delete-Selection" "")
1363 (if (and (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " and" "")
1364 (if (nth 2 cua--saved-state) " PC-Selection" "")
1365 (if (or (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " enabled" "")))
80d9508a 1366 (setq cua--saved-state nil))))
72cc582e
KS
1367
1368(defun cua-debug ()
1369 "Toggle cua debugging."
1370 (interactive)
1371 (setq cua--debug (not cua--debug)))
1372
06f8e605 1373;; Install run-time check for older versions of CUA-mode which does not
bf247b6e 1374;; work with GNU Emacs version 22.1 and newer.
06f8e605
KS
1375;;
1376;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
1377;; provided the `CUA-mode' feature. Since this is no longer true,
1378;; we can warn the user if the `CUA-mode' feature is ever provided.
1379
1380;;;###autoload (eval-after-load 'CUA-mode
1381;;;###autoload '(error (concat "\n\n"
1382;;;###autoload "CUA-mode is now part of the standard GNU Emacs distribution,\n"
1383;;;###autoload "so you may now enable and customize CUA via the Options menu.\n\n"
1384;;;###autoload "Your " (file-name-nondirectory user-init-file) " loads an older version of CUA-mode which does\n"
1385;;;###autoload "not work correctly with this version of GNU Emacs.\n"
1386;;;###autoload "To correct this, remove the loading and customization of the\n"
1387;;;###autoload "old version from the " user-init-file " file.\n\n")))
1388
f1cb14f8
KS
1389(provide 'cua)
1390
ab5796a9 1391;;; arch-tag: 21fb6289-ba25-4fee-bfdc-f9fb351acf05
72cc582e 1392;;; cua-base.el ends here