root/trunk/lisp/emulation/cua-base.el

Revision 4220, 58.6 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

Line 
1 ;;; cua-base.el --- emulate CUA key bindings
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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 3, 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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
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
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
58 ;; instantly - or at least very quickly.  So give it a try!
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
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
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".
124 ;;
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].
129 ;;
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.
132 ;;
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.
144 ;;
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 [C-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 C-return!
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.
174 ;;
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.
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 ;; [C-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 ;;     C-return M-} ; ; <space>  C-return
198
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)
215 ;; [M-o] opens the rectangle by moving the highlighted text to the
216 ;;       right of the rectangle and filling the rectangle with blanks.
217 ;; [M-p] toggles virtual straight rectangle edges
218 ;; [M-P] inserts tabs and spaces (padding) to make real straight edges
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
235 ;; ---------------
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:
242 ;; <ch>  All characters (including newlines) you type are inserted
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
270   :version "22.1"
271   :link '(emacs-commentary-link :tag "Commentary" "cua-base.el")
272   :link '(emacs-library-link :tag "Lisp File" "cua-base.el"))
273
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.
276 If 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
278 a shifted movement key.  If the value is nil, these keys are never
279 enabled."
280   :type '(choice (const :tag "Disabled" nil)
281                  (const :tag "Shift region only" shift)
282                  (other :tag "Enabled" t))
283   :group 'cua)
284
285 (defcustom cua-highlight-region-shift-only nil
286   "*If non-nil, only highlight region if marked with S-<move>.
287 When this is non-nil, CUA toggles `transient-mark-mode' on when the region
288 is marked using shifted movement keys, and off when the mark is cleared.
289 But when the mark was set using \\[cua-set-mark], Transient Mark mode
290 is not turned on."
291   :type 'boolean
292   :group 'cua)
293
294 (defcustom cua-prefix-override-inhibit-delay
295   (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) nil)
296   "*If non-nil, time in seconds to delay before overriding prefix key.
297 If there is additional input within this time, the prefix key is
298 used as a normal prefix key.  So typing a key sequence quickly will
299 inhibit overriding the prefix key.
300 As a special case, if the prefix keys repeated within this time, the
301 first prefix key is discarded, so typing a prefix key twice in quick
302 succession will also inhibit overriding the prefix key.
303 If 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-delete-selection t
309   "*If non-nil, typed text replaces text in the active selection."
310   :type '(choice (const :tag "Disabled" nil)
311                  (other :tag "Enabled" t))
312   :group 'cua)
313
314 (defcustom cua-keep-region-after-copy nil
315   "If non-nil, don't deselect the region after copying."
316   :type 'boolean
317   :group 'cua)
318
319 (defcustom cua-toggle-set-mark t
320   "*If non-nil, the `cua-set-mark' command toggles the mark."
321   :type '(choice (const :tag "Disabled" nil)
322                  (other :tag "Enabled" t))
323   :group 'cua)
324
325 (defcustom cua-auto-mark-last-change nil
326   "*If non-nil, set implicit mark at position of last buffer change.
327 This means that \\[universal-argument] \\[cua-set-mark] will jump to the position
328 of the last buffer change before jumping to the explicit marks on the mark ring.
329 See `cua-set-mark' for details."
330   :type 'boolean
331   :group 'cua)
332
333 (defcustom cua-enable-register-prefix 'not-ctrl-u
334   "*If non-nil, registers are supported via numeric prefix arg.
335 If the value is t, any numeric prefix arg in the range 0 to 9 will be
336 interpreted as a register number.
337 If the value is `not-ctrl-u', using C-u to enter a numeric prefix is not
338 interpreted as a register number.
339 If the value is `ctrl-u-only', only numeric prefix entered with C-u is
340 interpreted as a register number."
341   :type '(choice (const :tag "Disabled" nil)
342                  (const :tag "Enabled, but C-u arg is not a register" not-ctrl-u)
343                  (const :tag "Enabled, but only for C-u arg" ctrl-u-only)
344                  (other :tag "Enabled" t))
345   :group 'cua)
346
347 (defcustom cua-delete-copy-to-register-0 t
348   "*If non-nil, save last deleted region or rectangle to register 0."
349   :type 'boolean
350   :group 'cua)
351
352 (defcustom cua-enable-region-auto-help nil
353   "*If non-nil, automatically show help for active region."
354   :type 'boolean
355   :group 'cua)
356
357 (defcustom cua-enable-modeline-indications nil
358   "*If non-nil, use minor-mode hook to show status in mode line."
359   :type 'boolean
360   :group 'cua)
361
362 (defcustom cua-check-pending-input t
363   "*If non-nil, don't override prefix key if input pending.
364 It is rumoured that `input-pending-p' is unreliable under some window
365 managers, so try setting this to nil, if prefix override doesn't work."
366   :type 'boolean
367   :group 'cua)
368
369 (defcustom cua-paste-pop-rotate-temporarily nil
370   "*If non-nil, \\[cua-paste-pop] only rotates the kill-ring temporarily.
371 This means that both \\[yank] and the first \\[yank-pop] in a sequence always insert
372 the most recently killed text.  Each immediately following \\[cua-paste-pop] replaces
373 the previous text with the next older element on the `kill-ring'.
374 With prefix arg, \\[universal-argument] \\[yank-pop] inserts the same text as the most
375 recent \\[yank-pop] (or \\[yank]) command."
376   :type 'boolean
377   :group 'cua)
378
379 ;;; Rectangle Customization
380
381 (defcustom cua-virtual-rectangle-edges t
382   "*If non-nil, rectangles have virtual straight edges.
383 Note that although rectangles are always DISPLAYED with straight edges, the
384 buffer is NOT modified, until you execute a command that actually modifies it.
385 M-p toggles this feature when a rectangle is active."
386   :type 'boolean
387   :group 'cua)
388
389 (defcustom cua-auto-tabify-rectangles 1000
390   "*If non-nil, automatically tabify after rectangle commands.
391 This basically means that `tabify' is applied to all lines that
392 are modified by inserting or deleting a rectangle.  If value is
393 an integer, CUA will look for existing tabs in a region around
394 the rectangle, and only do the conversion if any tabs are already
395 present.  The number specifies then number of characters before
396 and after the region marked by the rectangle to search."
397   :type '(choice (number :tag "Auto detect (limit)")
398                  (const :tag "Disabled" nil)
399                  (other :tag "Enabled" t))
400   :group 'cua)
401
402 (defvar cua-global-keymap)              ; forward
403 (defvar cua--region-keymap)             ; forward
404
405 (defcustom cua-rectangle-mark-key [(control return)]
406   "Global key used to toggle the cua rectangle mark."
407   :set #'(lambda (symbol value)
408            (set symbol value)
409            (when (and (boundp 'cua--keymaps-initalized)
410                       cua--keymaps-initalized)
411              (define-key cua-global-keymap value
412                'cua-set-rectangle-mark)
413              (when (boundp 'cua--rectangle-keymap)
414                (define-key cua--rectangle-keymap value
415                  'cua-clear-rectangle-mark)
416                (define-key cua--region-keymap value
417                  'cua-toggle-rectangle-mark))))
418   :type 'key-sequence
419   :group 'cua)
420
421 (defcustom cua-rectangle-modifier-key 'meta
422   "*Modifier key used for rectangle commands bindings.
423 On non-window systems, always use the meta modifier.
424 Must be set prior to enabling CUA."
425   :type '(choice (const :tag "Meta key" meta)
426                  (const :tag "Alt key" alt)
427                  (const :tag "Hyper key" hyper)
428                  (const :tag "Super key" super))
429   :group 'cua)
430
431 (defcustom cua-enable-rectangle-auto-help t
432   "*If non-nil, automatically show help for region, rectangle and global mark."
433   :type 'boolean
434   :group 'cua)
435
436 (defface cua-rectangle
437   '((default :inherit region)
438     (((class color)) :foreground "white" :background "maroon"))
439   "*Font used by CUA for highlighting the rectangle."
440   :group 'cua)
441
442 (defface cua-rectangle-noselect
443   '((default :inherit region)
444     (((class color)) :foreground "white" :background "dimgray"))
445   "*Font used by CUA for highlighting the non-selected rectangle lines."
446   :group 'cua)
447
448
449 ;;; Global Mark Customization
450
451 (defcustom cua-global-mark-keep-visible t
452   "*If non-nil, always keep global mark visible in other window."
453   :type 'boolean
454   :group 'cua)
455
456 (defface cua-global-mark
457   '((((min-colors 88)(class color)) :foreground "black" :background "yellow1")
458     (((class color)) :foreground "black" :background "yellow")
459     (t :bold t))
460   "*Font used by CUA for highlighting the global mark."
461   :group 'cua)
462
463 (defcustom cua-global-mark-blink-cursor-interval 0.20
464   "*Blink cursor at this interval when global mark is active."
465   :type '(choice (number :tag "Blink interval")
466                  (const :tag "No blink" nil))
467   :group 'cua)
468
469
470 ;;; Cursor Indication Customization
471
472 (defcustom cua-enable-cursor-indications nil
473   "*If non-nil, use different cursor colors for indications."
474   :type 'boolean
475   :group 'cua)
476
477 (defcustom cua-normal-cursor-color (or (and (boundp 'initial-cursor-color) initial-cursor-color)
478                                        (and (boundp 'initial-frame-alist)
479                                             (assoc 'cursor-color initial-frame-alist)
480                                             (cdr (assoc 'cursor-color initial-frame-alist)))
481                                        (and (boundp 'default-frame-alist)
482                                             (assoc 'cursor-color default-frame-alist)
483                                             (cdr (assoc 'cursor-color default-frame-alist)))
484                                        (frame-parameter nil 'cursor-color)
485                                        "red")
486   "Normal (non-overwrite) cursor color.
487 Default is to load cursor color from initial or default frame parameters.
488
489 If the value is a COLOR name, then only the `cursor-color' attribute will be
490 affected.  If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
491 then only the `cursor-type' property will be affected.  If the value is
492 a cons (TYPE . COLOR), then both properties are affected."
493   :initialize 'custom-initialize-default
494   :type '(choice
495           (color :tag "Color")
496           (choice :tag "Type"
497                   (const :tag "Filled box" box)
498                   (const :tag "Vertical bar" bar)
499                   (const :tag "Horizontal bar" hbar)
500                   (const :tag "Hollow box" hollow))
501           (cons :tag "Color and Type"
502                 (choice :tag "Type"
503                         (const :tag "Filled box" box)
504                         (const :tag "Vertical bar" bar)
505                         (const :tag "Horizontal bar" hbar)
506                         (const :tag "Hollow box" hollow))
507                 (color :tag "Color")))
508   :group 'cua)
509
510 (defcustom cua-read-only-cursor-color "darkgreen"
511   "*Cursor color used in read-only buffers, if non-nil.
512 Only used when `cua-enable-cursor-indications' is non-nil.
513
514 If the value is a COLOR name, then only the `cursor-color' attribute will be
515 affected.  If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
516 then only the `cursor-type' property will be affected.  If the value is
517 a cons (TYPE . COLOR), then both properties are affected."
518   :type '(choice
519           (color :tag "Color")
520           (choice :tag "Type"
521                   (const :tag "Filled box" box)
522                   (const :tag "Vertical bar" bar)
523                   (const :tag "Horizontal bar" hbar)
524                   (const :tag "Hollow box" hollow))
525           (cons :tag "Color and Type"
526                 (choice :tag "Type"
527                         (const :tag "Filled box" box)
528                         (const :tag "Vertical bar" bar)
529                         (const :tag "Horizontal bar" hbar)
530                         (const :tag "Hollow box" hollow))
531                 (color :tag "Color")))
532   :group 'cua)
533
534 (defcustom cua-overwrite-cursor-color "yellow"
535   "*Cursor color used when overwrite mode is set, if non-nil.
536 Only used when `cua-enable-cursor-indications' is non-nil.
537
538 If the value is a COLOR name, then only the `cursor-color' attribute will be
539 affected.  If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
540 then only the `cursor-type' property will be affected.  If the value is
541 a cons (TYPE . COLOR), then both properties are affected."
542   :type '(choice
543           (color :tag "Color")
544           (choice :tag "Type"
545                   (const :tag "Filled box" box)
546                   (const :tag "Vertical bar" bar)
547                   (const :tag "Horizontal bar" hbar)
548                   (const :tag "Hollow box" hollow))
549           (cons :tag "Color and Type"
550                 (choice :tag "Type"
551                         (const :tag "Filled box" box)
552                         (const :tag "Vertical bar" bar)
553                         (const :tag "Horizontal bar" hbar)
554                         (const :tag "Hollow box" hollow))
555                 (color :tag "Color")))
556   :group 'cua)
557
558 (defcustom cua-global-mark-cursor-color "cyan"
559   "*Indication for active global mark.
560 Will change cursor color to specified color if string.
561 Only used when `cua-enable-cursor-indications' is non-nil.
562
563 If the value is a COLOR name, then only the `cursor-color' attribute will be
564 affected.  If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
565 then only the `cursor-type' property will be affected.  If the value is
566 a cons (TYPE . COLOR), then both properties are affected."
567   :type '(choice
568           (color :tag "Color")
569           (choice :tag "Type"
570                   (const :tag "Filled box" box)
571                   (const :tag "Vertical bar" bar)
572                   (const :tag "Horizontal bar" hbar)
573                   (const :tag "Hollow box" hollow))
574           (cons :tag "Color and Type"
575                 (choice :tag "Type"
576                         (const :tag "Filled box" box)
577                         (const :tag "Vertical bar" bar)
578                         (const :tag "Horizontal bar" hbar)
579                         (const :tag "Hollow box" hollow))
580                 (color :tag "Color")))
581   :group 'cua)
582
583
584 ;;; Rectangle support is in cua-rect.el
585
586 (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
587
588 ;; Stub definitions until it is loaded
589
590 (when (not (featurep 'cua-rect))
591   (defvar cua--rectangle)
592   (setq cua--rectangle nil)
593   (defvar cua--last-killed-rectangle)
594   (setq cua--last-killed-rectangle nil))
595
596
597
598 ;;; Global Mark support is in cua-gmrk.el
599
600 (autoload 'cua-toggle-global-mark "cua-gmrk" nil t nil)
601
602 ;; Stub definitions until cua-gmrk.el is loaded
603
604 (when (not (featurep 'cua-gmrk))
605   (defvar cua--global-mark-active)
606   (setq cua--global-mark-active nil))
607
608
609 (provide 'cua-base)
610
611 (eval-when-compile
612   (require 'cua-rect)
613   (require 'cua-gmrk)
614   )
615
616
617 ;;; Low-level Interface
618
619 (defvar cua-inhibit-cua-keys nil
620   "Buffer-local variable that may disable the CUA keymappings.")
621 (make-variable-buffer-local 'cua-inhibit-cua-keys)
622
623 ;;; Aux. variables
624
625 ;; Current region was started using cua-set-mark.
626 (defvar cua--explicit-region-start nil)
627 (make-variable-buffer-local 'cua--explicit-region-start)
628
629 ;; Latest region was started using shifted movement command.
630 (defvar cua--last-region-shifted nil)
631
632 ;; buffer + point prior to current command when rectangle is active
633 ;; checked in post-command hook to see if point was moved
634 (defvar cua--buffer-and-point-before-command nil)
635
636 ;; status string for mode line indications
637 (defvar cua--status-string nil)
638 (make-variable-buffer-local 'cua--status-string)
639
640 (defvar cua--debug nil)
641
642
643 ;;; Prefix key override mechanism
644
645 ;; The prefix override (when mark-active) operates in three substates:
646 ;; [1] Before using a prefix key
647 ;; [2] Immediately after using a prefix key
648 ;; [3] A fraction of a second later
649
650 ;; In state [1], the cua--prefix-override-keymap is active.
651 ;; This keymap binds the C-x and C-c prefix keys to the
652 ;; cua--prefix-override-handler function.
653
654 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
655 ;; will push back the keys already read to the event queue.  If input is
656 ;; pending, it changes directly to state [3].  Otherwise, a short timer [T]
657 ;; is started, and it changes to state [2].
658
659 ;; In state [2], the cua--prefix-override-keymap is inactive.  Instead the
660 ;; cua--prefix-repeat-keymap is active.  This keymap binds C-c C-c and C-x
661 ;; C-x to the cua--prefix-repeat-handler function.
662
663 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
664 ;; will cancel [T], back the keys already read (except for the second prefix
665 ;; keys) to the event queue, and changes to state [3].
666
667 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
668 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
669 ;; the cua--prefix-override-timeout function will push a `timeout' event on
670 ;; the event queue, and changes to state [3].
671
672 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
673 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
674 ;; normal prefix key binding from the global or local map will be used.
675
676 ;; The pre-command hook (executed as a consequence of the timeout or normal
677 ;; prefix key binding) will cancel [T] and change from state [3] back to
678 ;; state [1].  So cua--prefix-override-handler and cua--prefix-repeat-handler
679 ;; are always called with state reset to [1]!
680
681 ;; State [1] is recognized by cua--prefix-override-timer is nil,
682 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
683 ;; state [3] is recognized by cua--prefix-override-timer is t.
684
685 (defvar cua--prefix-override-timer nil)
686 (defvar cua--prefix-override-length nil)
687
688 (defun cua--prefix-override-replay (arg repeat)
689   (let* ((keys (this-command-keys))
690          (i (length keys))
691          (key (aref keys (1- i))))
692     (setq cua--prefix-override-length (- i repeat))
693     (setq cua--prefix-override-timer
694           (or
695            ;; In state [2], change to state [3]
696            (> repeat 0)
697            ;; In state [1], change directly to state [3]
698            (and cua-check-pending-input (input-pending-p))
699            ;; In state [1], [T] disabled, so change to state [3]
700            (not (numberp cua-prefix-override-inhibit-delay))
701            (<= cua-prefix-override-inhibit-delay 0)
702            ;; In state [1], start [T] and change to state [2]
703            (run-with-timer cua-prefix-override-inhibit-delay nil
704                            'cua--prefix-override-timeout)))
705     ;; Don't record this command
706     (setq this-command last-command)
707     ;; Restore the prefix arg
708     (setq prefix-arg arg)
709     (reset-this-command-lengths)
710     ;; Push the key back on the event queue
711     (setq unread-command-events (cons key unread-command-events))))
712
713 (defun cua--prefix-override-handler (arg)
714   "Start timer waiting for prefix key to be followed by another key.
715 Repeating prefix key when region is active works as a single prefix key."
716   (interactive "P")
717   (cua--prefix-override-replay arg 0))
718
719 (defun cua--prefix-repeat-handler (arg)
720   "Repeating prefix key when region is active works as a single prefix key."
721   (interactive "P")
722   (cua--prefix-override-replay arg 1))
723
724 (defun cua--prefix-copy-handler (arg)
725   "Copy region/rectangle, then replay last key."
726   (interactive "P")
727   (if cua--rectangle
728       (cua-copy-rectangle arg)
729     (cua-copy-region arg))
730   (let ((keys (this-single-command-keys)))
731     (setq unread-command-events
732           (cons (aref keys (1- (length keys))) unread-command-events))))
733
734 (defun cua--prefix-cut-handler (arg)
735   "Cut region/rectangle, then replay last key."
736   (interactive "P")
737   (if cua--rectangle
738       (cua-cut-rectangle arg)
739     (cua-cut-region arg))
740   (let ((keys (this-single-command-keys)))
741     (setq unread-command-events
742           (cons (aref keys (1- (length keys))) unread-command-events))))
743
744 (defun cua--prefix-override-timeout ()
745   (setq cua--prefix-override-timer t)
746   (when (= (length (this-command-keys)) cua--prefix-override-length)
747     (setq unread-command-events (cons 'timeout unread-command-events))
748     (if prefix-arg
749       (reset-this-command-lengths)
750       (setq overriding-terminal-local-map nil))
751     (cua--select-keymaps)))
752
753
754 ;;; Aux. functions
755
756 (defun cua--fallback ()
757   ;; Execute original command
758   (setq this-command this-original-command)
759   (call-interactively this-command))
760
761 (defun