Merged from emacs@sv.gnu.org
[bpt/emacs.git] / lisp / emulation / cua-base.el
1 ;;; cua-base.el --- emulate CUA key bindings
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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 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., 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 (defcustom cua-rectangle-mark-key [(control return)]
403 "Global key used to toggle the cua rectangle mark."
404 :set #'(lambda (symbol value)
405 (set symbol value)
406 (when (and (boundp 'cua--keymaps-initalized)
407 cua--keymaps-initalized)
408 (define-key cua-global-keymap value
409 'cua-set-rectangle-mark)
410 (when (boundp 'cua--rectangle-keymap)
411 (define-key cua--rectangle-keymap value
412 'cua-clear-rectangle-mark)
413 (define-key cua--region-keymap value
414 'cua-toggle-rectangle-mark))))
415 :type 'key-sequence
416 :group 'cua)
417
418 (defcustom cua-rectangle-modifier-key 'meta
419 "*Modifier key used for rectangle commands bindings.
420 On non-window systems, always use the meta modifier.
421 Must be set prior to enabling CUA."
422 :type '(choice (const :tag "Meta key" meta)
423 (const :tag "Alt key" alt)
424 (const :tag "Hyper key" hyper)
425 (const :tag "Super key" super))
426 :group 'cua)
427
428 (defcustom cua-enable-rectangle-auto-help t
429 "*If non-nil, automatically show help for region, rectangle and global mark."
430 :type 'boolean
431 :group 'cua)
432
433 (defface cua-rectangle
434 '((default :inherit region)
435 (((class color)) :foreground "white" :background "maroon"))
436 "*Font used by CUA for highlighting the rectangle."
437 :group 'cua)
438
439 (defface cua-rectangle-noselect
440 '((default :inherit region)
441 (((class color)) :foreground "white" :background "dimgray"))
442 "*Font used by CUA for highlighting the non-selected rectangle lines."
443 :group 'cua)
444
445
446 ;;; Global Mark Customization
447
448 (defcustom cua-global-mark-keep-visible t
449 "*If non-nil, always keep global mark visible in other window."
450 :type 'boolean
451 :group 'cua)
452
453 (defface cua-global-mark
454 '((((min-colors 88)(class color)) :foreground "black" :background "yellow1")
455 (((class color)) :foreground "black" :background "yellow")
456 (t :bold t))
457 "*Font used by CUA for highlighting the global mark."
458 :group 'cua)
459
460 (defcustom cua-global-mark-blink-cursor-interval 0.20
461 "*Blink cursor at this interval when global mark is active."
462 :type '(choice (number :tag "Blink interval")
463 (const :tag "No blink" nil))
464 :group 'cua)
465
466
467 ;;; Cursor Indication Customization
468
469 (defcustom cua-enable-cursor-indications nil
470 "*If non-nil, use different cursor colors for indications."
471 :type 'boolean
472 :group 'cua)
473
474 (defcustom cua-normal-cursor-color (or (and (boundp 'initial-cursor-color) initial-cursor-color)
475 (and (boundp 'initial-frame-alist)
476 (assoc 'cursor-color initial-frame-alist)
477 (cdr (assoc 'cursor-color initial-frame-alist)))
478 (and (boundp 'default-frame-alist)
479 (assoc 'cursor-color default-frame-alist)
480 (cdr (assoc 'cursor-color default-frame-alist)))
481 (frame-parameter nil 'cursor-color)
482 "red")
483 "Normal (non-overwrite) cursor color.
484 Default is to load cursor color from initial or default frame parameters.
485
486 If the value is a COLOR name, then only the `cursor-color' attribute will be
487 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
488 then only the `cursor-type' property will be affected. If the value is
489 a cons (TYPE . COLOR), then both properties are affected."
490 :initialize 'custom-initialize-default
491 :type '(choice
492 (color :tag "Color")
493 (choice :tag "Type"
494 (const :tag "Filled box" box)
495 (const :tag "Vertical bar" bar)
496 (const :tag "Horizontal bar" hbar)
497 (const :tag "Hollow box" hollow))
498 (cons :tag "Color and Type"
499 (choice :tag "Type"
500 (const :tag "Filled box" box)
501 (const :tag "Vertical bar" bar)
502 (const :tag "Horizontal bar" hbar)
503 (const :tag "Hollow box" hollow))
504 (color :tag "Color")))
505 :group 'cua)
506
507 (defcustom cua-read-only-cursor-color "darkgreen"
508 "*Cursor color used in read-only buffers, if non-nil.
509 Only used when `cua-enable-cursor-indications' is non-nil.
510
511 If the value is a COLOR name, then only the `cursor-color' attribute will be
512 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
513 then only the `cursor-type' property will be affected. If the value is
514 a cons (TYPE . COLOR), then both properties are affected."
515 :type '(choice
516 (color :tag "Color")
517 (choice :tag "Type"
518 (const :tag "Filled box" box)
519 (const :tag "Vertical bar" bar)
520 (const :tag "Horizontal bar" hbar)
521 (const :tag "Hollow box" hollow))
522 (cons :tag "Color and Type"
523 (choice :tag "Type"
524 (const :tag "Filled box" box)
525 (const :tag "Vertical bar" bar)
526 (const :tag "Horizontal bar" hbar)
527 (const :tag "Hollow box" hollow))
528 (color :tag "Color")))
529 :group 'cua)
530
531 (defcustom cua-overwrite-cursor-color "yellow"
532 "*Cursor color used when overwrite mode is set, if non-nil.
533 Only used when `cua-enable-cursor-indications' is non-nil.
534
535 If the value is a COLOR name, then only the `cursor-color' attribute will be
536 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
537 then only the `cursor-type' property will be affected. If the value is
538 a cons (TYPE . COLOR), then both properties are affected."
539 :type '(choice
540 (color :tag "Color")
541 (choice :tag "Type"
542 (const :tag "Filled box" box)
543 (const :tag "Vertical bar" bar)
544 (const :tag "Horizontal bar" hbar)
545 (const :tag "Hollow box" hollow))
546 (cons :tag "Color and Type"
547 (choice :tag "Type"
548 (const :tag "Filled box" box)
549 (const :tag "Vertical bar" bar)
550 (const :tag "Horizontal bar" hbar)
551 (const :tag "Hollow box" hollow))
552 (color :tag "Color")))
553 :group 'cua)
554
555 (defcustom cua-global-mark-cursor-color "cyan"
556 "*Indication for active global mark.
557 Will change cursor color to specified color if string.
558 Only used when `cua-enable-cursor-indications' is non-nil.
559
560 If the value is a COLOR name, then only the `cursor-color' attribute will be
561 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
562 then only the `cursor-type' property will be affected. If the value is
563 a cons (TYPE . COLOR), then both properties are affected."
564 :type '(choice
565 (color :tag "Color")
566 (choice :tag "Type"
567 (const :tag "Filled box" box)
568 (const :tag "Vertical bar" bar)
569 (const :tag "Horizontal bar" hbar)
570 (const :tag "Hollow box" hollow))
571 (cons :tag "Color and Type"
572 (choice :tag "Type"
573 (const :tag "Filled box" box)
574 (const :tag "Vertical bar" bar)
575 (const :tag "Horizontal bar" hbar)
576 (const :tag "Hollow box" hollow))
577 (color :tag "Color")))
578 :group 'cua)
579
580
581 ;;; Rectangle support is in cua-rect.el
582
583 (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
584
585 ;; Stub definitions until it is loaded
586
587 (when (not (featurep 'cua-rect))
588 (defvar cua--rectangle)
589 (setq cua--rectangle nil)
590 (defvar cua--last-killed-rectangle)
591 (setq cua--last-killed-rectangle nil))
592
593
594
595 ;;; Global Mark support is in cua-gmrk.el
596
597 (autoload 'cua-toggle-global-mark "cua-gmrk" nil t nil)
598
599 ;; Stub definitions until cua-gmrk.el is loaded
600
601 (when (not (featurep 'cua-gmrk))
602 (defvar cua--global-mark-active)
603 (setq cua--global-mark-active nil))
604
605
606 (provide 'cua-base)
607
608 (eval-when-compile
609 (require 'cua-rect)
610 (require 'cua-gmrk)
611 )
612
613
614 ;;; Low-level Interface
615
616 (defvar cua-inhibit-cua-keys nil
617 "Buffer-local variable that may disable the CUA keymappings.")
618 (make-variable-buffer-local 'cua-inhibit-cua-keys)
619
620 ;;; Aux. variables
621
622 ;; Current region was started using cua-set-mark.
623 (defvar cua--explicit-region-start nil)
624 (make-variable-buffer-local 'cua--explicit-region-start)
625
626 ;; Latest region was started using shifted movement command.
627 (defvar cua--last-region-shifted nil)
628
629 ;; buffer + point prior to current command when rectangle is active
630 ;; checked in post-command hook to see if point was moved
631 (defvar cua--buffer-and-point-before-command nil)
632
633 ;; status string for mode line indications
634 (defvar cua--status-string nil)
635 (make-variable-buffer-local 'cua--status-string)
636
637 (defvar cua--debug nil)
638
639
640 ;;; Prefix key override mechanism
641
642 ;; The prefix override (when mark-active) operates in three substates:
643 ;; [1] Before using a prefix key
644 ;; [2] Immediately after using a prefix key
645 ;; [3] A fraction of a second later
646
647 ;; In state [1], the cua--prefix-override-keymap is active.
648 ;; This keymap binds the C-x and C-c prefix keys to the
649 ;; cua--prefix-override-handler function.
650
651 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
652 ;; will push back the keys already read to the event queue. If input is
653 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
654 ;; is started, and it changes to state [2].
655
656 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
657 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
658 ;; C-x to the cua--prefix-repeat-handler function.
659
660 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
661 ;; will cancel [T], back the keys already read (except for the second prefix
662 ;; keys) to the event queue, and changes to state [3].
663
664 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
665 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
666 ;; the cua--prefix-override-timeout function will push a `timeout' event on
667 ;; the event queue, and changes to state [3].
668
669 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
670 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
671 ;; normal prefix key binding from the global or local map will be used.
672
673 ;; The pre-command hook (executed as a consequence of the timeout or normal
674 ;; prefix key binding) will cancel [T] and change from state [3] back to
675 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
676 ;; are always called with state reset to [1]!
677
678 ;; State [1] is recognized by cua--prefix-override-timer is nil,
679 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
680 ;; state [3] is recognized by cua--prefix-override-timer is t.
681
682 (defvar cua--prefix-override-timer nil)
683 (defvar cua--prefix-override-length nil)
684
685 (defun cua--prefix-override-replay (arg repeat)
686 (let* ((keys (this-command-keys))
687 (i (length keys))
688 (key (aref keys (1- i))))
689 (setq cua--prefix-override-length (- i repeat))
690 (setq cua--prefix-override-timer
691 (or
692 ;; In state [2], change to state [3]
693 (> repeat 0)
694 ;; In state [1], change directly to state [3]
695 (and cua-check-pending-input (input-pending-p))
696 ;; In state [1], [T] disabled, so change to state [3]
697 (not (numberp cua-prefix-override-inhibit-delay))
698 (<= cua-prefix-override-inhibit-delay 0)
699 ;; In state [1], start [T] and change to state [2]
700 (run-with-timer cua-prefix-override-inhibit-delay nil
701 'cua--prefix-override-timeout)))
702 ;; Don't record this command
703 (setq this-command last-command)
704 ;; Restore the prefix arg
705 (setq prefix-arg arg)
706 (reset-this-command-lengths)
707 ;; Push the key back on the event queue
708 (setq unread-command-events (cons key unread-command-events))))
709
710 (defun cua--prefix-override-handler (arg)
711 "Start timer waiting for prefix key to be followed by another key.
712 Repeating prefix key when region is active works as a single prefix key."
713 (interactive "P")
714 (cua--prefix-override-replay arg 0))
715
716 (defun cua--prefix-repeat-handler (arg)
717 "Repeating prefix key when region is active works as a single prefix key."
718 (interactive "P")
719 (cua--prefix-override-replay arg 1))
720
721 (defun cua--prefix-copy-handler (arg)
722 "Copy region/rectangle, then replay last key."
723 (interactive "P")
724 (if cua--rectangle
725 (cua-copy-rectangle arg)
726 (cua-copy-region arg))
727 (let ((keys (this-single-command-keys)))
728 (setq unread-command-events
729 (cons (aref keys (1- (length keys))) unread-command-events))))
730
731 (defun cua--prefix-cut-handler (arg)
732 "Cut region/rectangle, then replay last key."
733 (interactive "P")
734 (if cua--rectangle
735 (cua-cut-rectangle arg)
736 (cua-cut-region arg))
737 (let ((keys (this-single-command-keys)))
738 (setq unread-command-events
739 (cons (aref keys (1- (length keys))) unread-command-events))))
740
741 (defun cua--prefix-override-timeout ()
742 (setq cua--prefix-override-timer t)
743 (when (= (length (this-command-keys)) cua--prefix-override-length)
744 (setq unread-command-events (cons 'timeout unread-command-events))
745 (if prefix-arg
746 (reset-this-command-lengths)
747 (setq overriding-terminal-local-map nil))
748 (cua--select-keymaps)))
749
750
751 ;;; Aux. functions
752
753 (defun cua--fallback ()
754 ;; Execute original command
755 (setq this-command this-original-command)
756 (call-interactively this-command))
757
758 (defun cua--keep-active ()
759 (setq mark-active t
760 deactivate-mark nil))
761
762 (defun cua--deactivate (&optional now)
763 (setq cua--explicit-region-start nil)
764 (if (not now)
765 (setq deactivate-mark t)
766 (setq mark-active nil)
767 (run-hooks 'deactivate-mark-hook)))
768
769
770 ;; The current register prefix
771 (defvar cua--register nil)
772
773 (defun cua--prefix-arg (arg)
774 (setq cua--register
775 (and cua-enable-register-prefix
776 (integerp arg) (>= arg 0) (< arg 10)
777 (let* ((prefix (aref (this-command-keys) 0))
778 (ctrl-u-prefix (and (integerp prefix)
779 (= prefix ?\C-u))))
780 (cond
781 ((eq cua-enable-register-prefix 'not-ctrl-u)
782 (not ctrl-u-prefix))
783 ((eq cua-enable-register-prefix 'ctrl-u-only)
784 ctrl-u-prefix)
785 (t t)))
786 (+ arg ?0)))
787 (if cua--register nil arg))
788
789
790 ;;; Region specific commands
791
792 (defvar cua--last-deleted-region-pos nil)
793 (defvar cua--last-deleted-region-text nil)
794
795 (defun cua-delete-region ()
796 "Delete the active region.
797 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
798 (interactive)
799 (let ((start (mark)) (end (point)))
800 (or (<= start end)
801 (setq start (prog1 end (setq end start))))
802 (setq cua--last-deleted-region-text (filter-buffer-substring start end))
803 (if cua-delete-copy-to-register-0
804 (set-register ?0 cua--last-deleted-region-text))
805 (delete-region start end)
806 (setq cua--last-deleted-region-pos
807 (cons (current-buffer)
808 (and (consp buffer-undo-list)
809 (car buffer-undo-list))))
810 (cua--deactivate)
811 (/= start end)))
812
813 (defun cua-replace-region ()
814 "Replace the active region with the character you type."
815 (interactive)
816 (let ((not-empty (and cua-delete-selection (cua-delete-region))))
817 (unless (eq this-original-command this-command)
818 (let ((overwrite-mode
819 (and overwrite-mode
820 not-empty
821 (not (eq this-original-command 'self-insert-command)))))
822 (cua--fallback)))))
823
824 (defun cua-copy-region (arg)
825 "Copy the region to the kill ring.
826 With numeric prefix arg, copy to register 0-9 instead."
827 (interactive "P")
828 (setq arg (cua--prefix-arg arg))
829 (setq cua--last-killed-rectangle nil)
830 (let ((start (mark)) (end (point)))
831 (or (<= start end)
832 (setq start (prog1 end (setq end start))))
833 (cond
834 (cua--register
835 (copy-to-register cua--register start end nil))
836 ((eq this-original-command 'clipboard-kill-ring-save)
837 (clipboard-kill-ring-save start end))
838 (t
839 (copy-region-as-kill start end)))
840 (if cua-keep-region-after-copy
841 (cua--keep-active)
842 (cua--deactivate))))
843
844 (defun cua-cut-region (arg)
845 "Cut the region and copy to the kill ring.
846 With numeric prefix arg, copy to register 0-9 instead."
847 (interactive "P")
848 (setq cua--last-killed-rectangle nil)
849 (if buffer-read-only
850 (cua-copy-region arg)
851 (setq arg (cua--prefix-arg arg))
852 (let ((start (mark)) (end (point)))
853 (or (<= start end)
854 (setq start (prog1 end (setq end start))))
855 (cond
856 (cua--register
857 (copy-to-register cua--register start end t))
858 ((eq this-original-command 'clipboard-kill-region)
859 (clipboard-kill-region start end))
860 (t
861 (kill-region start end))))
862 (cua--deactivate)))
863
864 ;;; Generic commands for regions, rectangles, and global marks
865
866 (defun cua-cancel ()
867 "Cancel the active region, rectangle, or global mark."
868 (interactive)
869 (setq mark-active nil)
870 (setq cua--explicit-region-start nil)
871 (if (fboundp 'cua--cancel-rectangle)
872 (cua--cancel-rectangle)))
873
874 (defun cua-paste (arg)
875 "Paste last cut or copied region or rectangle.
876 An active region is deleted before executing the command.
877 With numeric prefix arg, paste from register 0-9 instead.
878 If global mark is active, copy from register or one character."
879 (interactive "P")
880 (setq arg (cua--prefix-arg arg))
881 (let ((regtxt (and cua--register (get-register cua--register)))
882 (count (prefix-numeric-value arg))
883 paste-column paste-lines)
884 (cond
885 ((and cua--register (not regtxt))
886 (message "Nothing in register %c" cua--register))
887 (cua--global-mark-active
888 (if regtxt
889 (cua--insert-at-global-mark regtxt)
890 (when (not (eobp))
891 (cua--insert-at-global-mark (filter-buffer-substring (point) (+ (point) count)))
892 (forward-char count))))
893 (buffer-read-only
894 (message "Cannot paste into a read-only buffer"))
895 (t
896 ;; Must save register here, since delete may override reg 0.
897 (if mark-active
898 ;; Before a yank command, make sure we don't yank
899 ;; the same region that we are going to delete.
900 ;; That would make yank a no-op.
901 (if cua--rectangle
902 (progn
903 (goto-char (min (mark) (point)))
904 (setq paste-column (cua--rectangle-left))
905 (setq paste-lines (cua--delete-rectangle))
906 (if (= paste-lines 1)
907 (setq paste-lines nil))) ;; paste all
908 (if (string= (filter-buffer-substring (point) (mark))
909 (car kill-ring))
910 (current-kill 1))
911 (cua-delete-region)))
912 (cond
913 (regtxt
914 (cond
915 ((consp regtxt) (cua--insert-rectangle regtxt))
916 ((stringp regtxt) (insert-for-yank regtxt))
917 (t (message "Unknown data in register %c" cua--register))))
918 ((and cua--last-killed-rectangle
919 (eq (and kill-ring (car kill-ring)) (car cua--last-killed-rectangle)))
920 (let ((pt (point)))
921 (when (not (eq buffer-undo-list t))
922 (setq this-command 'cua--paste-rectangle)
923 (undo-boundary)
924 (setq buffer-undo-list (cons pt buffer-undo-list)))
925 (cua--insert-rectangle (cdr cua--last-killed-rectangle)
926 nil paste-column paste-lines)
927 (if arg (goto-char pt))))
928 ((eq this-original-command 'clipboard-yank)
929 (clipboard-yank))
930 (t (yank arg)))))))
931
932
933 ;; cua-paste-pop-rotate-temporarily == t mechanism:
934 ;;
935 ;; C-y M-y M-y => only rotates kill ring temporarily,
936 ;; so next C-y yanks what previous C-y yanked,
937 ;;
938 ;; M-y M-y M-y => equivalent to C-y M-y M-y
939 ;;
940 ;; But: After another command, C-u M-y remembers the temporary
941 ;; kill-ring position, so
942 ;; C-u M-y => yanks what the last M-y yanked
943 ;;
944
945 (defvar cua-paste-pop-count nil)
946
947 (defun cua-paste-pop (arg)
948 "Replace a just-pasted text or rectangle with a different text.
949 See `yank-pop' for details about the default behaviour. For an alternative
950 behaviour, see `cua-paste-pop-rotate-temporarily'."
951 (interactive "P")
952 (cond
953 ((eq last-command 'cua--paste-rectangle)
954 (undo)
955 (yank arg))
956 ((not cua-paste-pop-rotate-temporarily)
957 (yank-pop (prefix-numeric-value arg)))
958 (t
959 (let ((rotate (if (consp arg) 1 (prefix-numeric-value arg))))
960 (cond
961 ((or (null cua-paste-pop-count)
962 (eq last-command 'yank)
963 (eq last-command 'cua-paste))
964 (setq cua-paste-pop-count rotate)
965 (setq last-command 'yank)
966 (yank-pop cua-paste-pop-count))
967 ((and (eq last-command 'cua-paste-pop) (not (consp arg)))
968 (setq cua-paste-pop-count (+ cua-paste-pop-count rotate))
969 (setq last-command 'yank)
970 (yank-pop cua-paste-pop-count))
971 (t
972 (setq cua-paste-pop-count
973 (if (consp arg) (+ cua-paste-pop-count rotate -1) 1))
974 (yank (1+ cua-paste-pop-count)))))
975 ;; Undo rotating the kill-ring, so next C-y will
976 ;; yank the original head.
977 (setq kill-ring-yank-pointer kill-ring)
978 (setq this-command 'cua-paste-pop))))
979
980 (defun cua-exchange-point-and-mark (arg)
981 "Exchanges point and mark, but don't activate the mark.
982 Activates the mark if a prefix argument is given."
983 (interactive "P")
984 (if arg
985 (setq mark-active t)
986 (let (mark-active)
987 (exchange-point-and-mark)
988 (if cua--rectangle
989 (cua--rectangle-corner 0)))))
990
991 ;; Typed text that replaced the highlighted region.
992 (defvar cua--repeat-replace-text nil)
993
994 (defun cua-repeat-replace-region (arg)
995 "Repeat replacing text of highlighted region with typed text.
996 Searches for the next stretch of text identical to the region last
997 replaced by typing text over it and replaces it with the same stretch
998 of text."
999 (interactive "P")
1000 (when cua--last-deleted-region-pos
1001 (save-excursion
1002 (save-restriction
1003 (set-buffer (car cua--last-deleted-region-pos))
1004 (widen)
1005 ;; Find the text that replaced the region via the undo list.
1006 (let ((ul buffer-undo-list)
1007 (elt (cdr cua--last-deleted-region-pos))
1008 u s e)
1009 (when elt
1010 (while (consp ul)
1011 (setq u (car ul) ul (cdr ul))
1012 (cond
1013 ((eq u elt) ;; got it
1014 (setq ul nil))
1015 ((and (consp u) (integerp (car u)) (integerp (cdr u)))
1016 (if (and s (= (cdr u) s))
1017 (setq s (car u))
1018 (setq s (car u) e (cdr u)))))))
1019 (cond ((and s e (<= s e) (= s (mark t)))
1020 (setq cua--repeat-replace-text
1021 (filter-buffer-substring s e nil t)))
1022 ((and (null s) (eq u elt)) ;; nothing inserted
1023 (setq cua--repeat-replace-text
1024 ""))
1025 (t
1026 (message "Cannot locate replacement text"))))))
1027 (setq cua--last-deleted-region-pos nil))
1028 (if (and cua--last-deleted-region-text
1029 cua--repeat-replace-text
1030 (search-forward cua--last-deleted-region-text nil t nil))
1031 (replace-match cua--repeat-replace-text arg t)))
1032
1033 (defun cua-help-for-region (&optional help)
1034 "Show region specific help in echo area."
1035 (interactive)
1036 (message
1037 (concat (if help "C-?:help " "")
1038 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
1039
1040
1041 ;;; Shift activated / extended region
1042
1043 (defun cua-pop-to-last-change ()
1044 (let ((undo-list buffer-undo-list)
1045 pos elt)
1046 (while (and (not pos)
1047 (consp undo-list))
1048 (setq elt (car undo-list)
1049 undo-list (cdr undo-list))
1050 (cond
1051 ((integerp elt)
1052 (setq pos elt))
1053 ((not (consp elt)))
1054 ((and (integerp (cdr elt))
1055 (or (integerp (car elt)) (stringp (car elt))))
1056 (setq pos (cdr elt)))
1057 ((and (eq (car elt) 'apply) (consp (cdr elt)) (integerp (cadr elt)))
1058 (setq pos (nth 3 elt)))))
1059 (when (and pos
1060 (/= pos (point))
1061 (>= pos (point-min)) (<= pos (point-max)))
1062 (goto-char pos)
1063 t)))
1064
1065 (defun cua-set-mark (&optional arg)
1066 "Set mark at where point is, clear mark, or jump to mark.
1067
1068 With no prefix argument, clear mark if already set. Otherwise, set
1069 mark, and push old mark position on local mark ring; also push mark on
1070 global mark ring if last mark was set in another buffer.
1071
1072 With argument, jump to mark, and pop a new position for mark off
1073 the local mark ring (this does not affect the global mark ring).
1074 Use \\[pop-global-mark] to jump to a mark off the global mark ring
1075 \(see `pop-global-mark').
1076
1077 If `cua-auto-mark-last-change' is non-nil, this command behaves as if there
1078 was an implicit mark at the position of the last buffer change.
1079
1080 Repeating the command without the prefix jumps to the next position
1081 off the local (or global) mark ring.
1082
1083 With a double \\[universal-argument] prefix argument, unconditionally set mark."
1084 (interactive "P")
1085 (cond
1086 ((and (consp arg) (> (prefix-numeric-value arg) 4))
1087 (push-mark-command nil))
1088 ((eq last-command 'pop-to-mark-command)
1089 (setq this-command 'pop-to-mark-command)
1090 (pop-to-mark-command))
1091 ((and (eq last-command 'pop-global-mark) (not arg))
1092 (setq this-command 'pop-global-mark)
1093 (pop-global-mark))
1094 (arg
1095 (setq this-command 'pop-to-mark-command)
1096 (or (and cua-auto-mark-last-change
1097 (cua-pop-to-last-change))
1098 (pop-to-mark-command)))
1099 ((and cua-toggle-set-mark mark-active)
1100 (cua--deactivate)
1101 (message "Mark Cleared"))
1102 (t
1103 (push-mark-command nil nil)
1104 (setq cua--explicit-region-start t)
1105 (setq cua--last-region-shifted nil)
1106 (if cua-enable-region-auto-help
1107 (cua-help-for-region t)))))
1108
1109 ;;; Scrolling commands which does not signal errors at top/bottom
1110 ;;; of buffer at first key-press (instead moves to top/bottom
1111 ;;; of buffer).
1112
1113 (defun cua-scroll-up (&optional arg)
1114 "Scroll text of current window upward ARG lines; or near full screen if no ARG.
1115 If window cannot be scrolled further, move cursor to bottom line instead.
1116 A near full screen is `next-screen-context-lines' less than a full screen.
1117 Negative ARG means scroll downward.
1118 If ARG is the atom `-', scroll downward by nearly full screen."
1119 (interactive "P")
1120 (cond
1121 ((eq arg '-) (cua-scroll-down nil))
1122 ((< (prefix-numeric-value arg) 0)
1123 (cua-scroll-down (- (prefix-numeric-value arg))))
1124 ((eobp)
1125 (scroll-up arg)) ; signal error
1126 (t
1127 (condition-case nil
1128 (scroll-up arg)
1129 (end-of-buffer (goto-char (point-max)))))))
1130
1131 (put 'cua-scroll-up 'CUA 'move)
1132
1133 (defun cua-scroll-down (&optional arg)
1134 "Scroll text of current window downward ARG lines; or near full screen if no ARG.
1135 If window cannot be scrolled further, move cursor to top line instead.
1136 A near full screen is `next-screen-context-lines' less than a full screen.
1137 Negative ARG means scroll upward.
1138 If ARG is the atom `-', scroll upward by nearly full screen."
1139 (interactive "P")
1140 (cond
1141 ((eq arg '-) (cua-scroll-up nil))
1142 ((< (prefix-numeric-value arg) 0)
1143 (cua-scroll-up (- (prefix-numeric-value arg))))
1144 ((bobp)
1145 (scroll-down arg)) ; signal error
1146 (t
1147 (condition-case nil
1148 (scroll-down arg)
1149 (beginning-of-buffer (goto-char (point-min)))))))
1150
1151 (put 'cua-scroll-down 'CUA 'move)
1152
1153 ;;; Cursor indications
1154
1155 (defun cua--update-indications ()
1156 (let* ((cursor
1157 (cond
1158 ((and cua--global-mark-active
1159 cua-global-mark-cursor-color)
1160 cua-global-mark-cursor-color)
1161 ((and buffer-read-only
1162 cua-read-only-cursor-color)
1163 cua-read-only-cursor-color)
1164 ((and cua-overwrite-cursor-color overwrite-mode)
1165 cua-overwrite-cursor-color)
1166 (t cua-normal-cursor-color)))
1167 (color (if (consp cursor) (cdr cursor) cursor))
1168 (type (if (consp cursor) (car cursor) cursor)))
1169 (if (and color
1170 (stringp color)
1171 (not (equal color (frame-parameter nil 'cursor-color))))
1172 (set-cursor-color color))
1173 (if (and type
1174 (symbolp type)
1175 (not (eq type default-cursor-type)))
1176 (setq default-cursor-type type))))
1177
1178
1179 ;;; Pre-command hook
1180
1181 (defun cua--pre-command-handler-1 ()
1182 ;; Cancel prefix key timeout if user enters another key.
1183 (when cua--prefix-override-timer
1184 (if (timerp cua--prefix-override-timer)
1185 (cancel-timer cua--prefix-override-timer))
1186 (setq cua--prefix-override-timer nil))
1187
1188 (cond
1189 ;; Only symbol commands can have necessary properties
1190 ((not (symbolp this-command))
1191 nil)
1192
1193 ;; Handle delete-selection property on non-movement commands
1194 ((not (eq (get this-command 'CUA) 'move))
1195 (when (and mark-active (not deactivate-mark))
1196 (let* ((ds (or (get this-command 'delete-selection)
1197 (get this-command 'pending-delete)))
1198 (nc (cond
1199 ((not ds) nil)
1200 ((eq ds 'yank)
1201 'cua-paste)
1202 ((eq ds 'kill)
1203 (if cua--rectangle
1204 'cua-copy-rectangle
1205 'cua-copy-region))
1206 ((eq ds 'supersede)
1207 (if cua--rectangle
1208 'cua-delete-rectangle
1209 'cua-delete-region))
1210 (t
1211 (if cua--rectangle
1212 'cua-delete-rectangle ;; replace?
1213 'cua-replace-region)))))
1214 (if nc
1215 (setq this-original-command this-command
1216 this-command nc)))))
1217
1218 ;; Handle shifted cursor keys and other movement commands.
1219 ;; If region is not active, region is activated if key is shifted.
1220 ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC).
1221 ;; If rectangle is active, expand rectangle in specified direction and ignore the movement.
1222 ((if window-system
1223 (memq 'shift (event-modifiers
1224 (aref (this-single-command-raw-keys) 0)))
1225 (or
1226 (memq 'shift (event-modifiers
1227 (aref (this-single-command-keys) 0)))
1228 ;; See if raw escape sequence maps to a shifted event, e.g. S-up or C-S-home.
1229 (and (boundp 'local-function-key-map)
1230 local-function-key-map
1231 (let ((ev (lookup-key local-function-key-map
1232 (this-single-command-raw-keys))))
1233 (and (vector ev)
1234 (symbolp (setq ev (aref ev 0)))
1235 (string-match "S-" (symbol-name ev)))))))
1236 (unless mark-active
1237 (push-mark-command nil t))
1238 (setq cua--last-region-shifted t)
1239 (setq cua--explicit-region-start nil))
1240
1241 ;; Set mark if user explicitly said to do so
1242 ((or cua--explicit-region-start cua--rectangle)
1243 (unless mark-active
1244 (push-mark-command nil nil)))
1245
1246 ;; Else clear mark after this command.
1247 (t
1248 ;; If we set mark-active to nil here, the region highlight will not be
1249 ;; removed by the direct_output_ commands.
1250 (setq deactivate-mark t)))
1251
1252 ;; Detect extension of rectangles by mouse or other movement
1253 (setq cua--buffer-and-point-before-command
1254 (if cua--rectangle (cons (current-buffer) (point)))))
1255
1256 (defun cua--pre-command-handler ()
1257 (when cua-mode
1258 (condition-case nil
1259 (cua--pre-command-handler-1)
1260 (error nil))))
1261
1262 ;;; Post-command hook
1263
1264 (defun cua--post-command-handler-1 ()
1265 (when cua--global-mark-active
1266 (cua--global-mark-post-command))
1267 (when (fboundp 'cua--rectangle-post-command)
1268 (cua--rectangle-post-command))
1269 (setq cua--buffer-and-point-before-command nil)
1270 (if (or (not mark-active) deactivate-mark)
1271 (setq cua--explicit-region-start nil))
1272
1273 ;; Debugging
1274 (if cua--debug
1275 (cond
1276 (cua--rectangle (cua--rectangle-assert))
1277 (mark-active (message "Mark=%d Point=%d Expl=%s"
1278 (mark t) (point) cua--explicit-region-start))))
1279
1280 ;; Disable transient-mark-mode if rectangle active in current buffer.
1281 (if (not (window-minibuffer-p (selected-window)))
1282 (setq transient-mark-mode (and (not cua--rectangle)
1283 (if cua-highlight-region-shift-only
1284 (not cua--explicit-region-start)
1285 t))))
1286 (if cua-enable-cursor-indications
1287 (cua--update-indications))
1288
1289 (cua--select-keymaps))
1290
1291 (defun cua--post-command-handler ()
1292 (when cua-mode
1293 (condition-case nil
1294 (cua--post-command-handler-1)
1295 (error nil))))
1296
1297
1298 ;;; Keymaps
1299
1300 ;; Cached value of actual cua-rectangle-modifier-key
1301 (defvar cua--rectangle-modifier-key 'meta)
1302
1303 (defun cua--M/H-key (map key fct)
1304 ;; bind H-KEY or M-KEY to FCT in MAP
1305 (unless (listp key) (setq key (list key)))
1306 (define-key map (vector (cons cua--rectangle-modifier-key key)) fct))
1307
1308 (defun cua--self-insert-char-p (def)
1309 ;; Return DEF if current key sequence is self-inserting in
1310 ;; global-map.
1311 (if (memq (global-key-binding (this-single-command-keys))
1312 '(self-insert-command self-insert-iso))
1313 def nil))
1314
1315 (defvar cua-global-keymap (make-sparse-keymap)
1316 "Global keymap for cua-mode; users may add to this keymap.")
1317
1318 (defvar cua--cua-keys-keymap (make-sparse-keymap))
1319 (defvar cua--prefix-override-keymap (make-sparse-keymap))
1320 (defvar cua--prefix-repeat-keymap (make-sparse-keymap))
1321 (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
1322 (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
1323 (defvar cua--region-keymap (make-sparse-keymap))
1324
1325 (defvar cua--ena-cua-keys-keymap nil)
1326 (defvar cua--ena-prefix-override-keymap nil)
1327 (defvar cua--ena-prefix-repeat-keymap nil)
1328 (defvar cua--ena-region-keymap nil)
1329 (defvar cua--ena-global-mark-keymap nil)
1330
1331 (defvar cua--keymap-alist
1332 `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap)
1333 (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap)
1334 (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap)
1335 (cua--ena-global-mark-keymap . ,cua--global-mark-keymap)
1336 (cua--rectangle . ,cua--rectangle-keymap)
1337 (cua--ena-region-keymap . ,cua--region-keymap)
1338 (cua-mode . ,cua-global-keymap)))
1339
1340 (defun cua--select-keymaps ()
1341 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
1342 (setq cua--ena-region-keymap
1343 (and mark-active (not deactivate-mark)))
1344 (setq cua--ena-prefix-override-keymap
1345 (and cua--ena-region-keymap
1346 cua-enable-cua-keys
1347 (not cua-inhibit-cua-keys)
1348 (or (eq cua-enable-cua-keys t)
1349 (not cua--explicit-region-start))
1350 (not executing-kbd-macro)
1351 (not cua--prefix-override-timer)))
1352 (setq cua--ena-prefix-repeat-keymap
1353 (and cua--ena-region-keymap
1354 (or (timerp cua--prefix-override-timer)
1355 (eq cua--prefix-override-timer 'shift))))
1356 (setq cua--ena-cua-keys-keymap
1357 (and cua-enable-cua-keys
1358 (not cua-inhibit-cua-keys)
1359 (or (eq cua-enable-cua-keys t)
1360 cua--last-region-shifted)))
1361 (setq cua--ena-global-mark-keymap
1362 (and cua--global-mark-active
1363 (not (window-minibuffer-p)))))
1364
1365 (defvar cua--keymaps-initalized nil)
1366
1367 (defun cua--shift-control-prefix (prefix arg)
1368 ;; handle S-C-x and S-C-c by emulating the fast double prefix function.
1369 ;; Don't record this command
1370 (setq this-command last-command)
1371 ;; Restore the prefix arg
1372 (setq prefix-arg arg)
1373 (reset-this-command-lengths)
1374 ;; Activate the cua--prefix-repeat-keymap
1375 (setq cua--prefix-override-timer 'shift)
1376 ;; Push duplicate keys back on the event queue
1377 (setq unread-command-events (cons prefix (cons prefix unread-command-events))))
1378
1379 (defun cua--shift-control-c-prefix (arg)
1380 (interactive "P")
1381 (cua--shift-control-prefix ?\C-c arg))
1382
1383 (defun cua--shift-control-x-prefix (arg)
1384 (interactive "P")
1385 (cua--shift-control-prefix ?\C-x arg))
1386
1387 (defun cua--init-keymaps ()
1388 ;; Cache actual rectangle modifier key.
1389 (setq cua--rectangle-modifier-key
1390 (if (and cua-rectangle-modifier-key
1391 (memq window-system '(x)))
1392 cua-rectangle-modifier-key
1393 'meta))
1394 ;; C-return always toggles rectangle mark
1395 (define-key cua-global-keymap cua-rectangle-mark-key 'cua-set-rectangle-mark)
1396 (unless (eq cua--rectangle-modifier-key 'meta)
1397 (cua--M/H-key cua-global-keymap ?\s 'cua-set-rectangle-mark)
1398 (define-key cua-global-keymap
1399 (vector (list cua--rectangle-modifier-key 'mouse-1)) 'cua-mouse-set-rectangle-mark))
1400
1401 (define-key cua-global-keymap [(shift control ?\s)] 'cua-toggle-global-mark)
1402
1403 ;; replace region with rectangle or element on kill ring
1404 (define-key cua-global-keymap [remap yank] 'cua-paste)
1405 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1406 ;; replace current yank with previous kill ring element
1407 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1408 ;; set mark
1409 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
1410
1411 ;; scrolling
1412 (define-key cua-global-keymap [remap scroll-up] 'cua-scroll-up)
1413 (define-key cua-global-keymap [remap scroll-down] 'cua-scroll-down)
1414
1415 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
1416 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
1417 (define-key cua--cua-keys-keymap [(control z)] 'undo)
1418 (define-key cua--cua-keys-keymap [(control v)] 'yank)
1419 (define-key cua--cua-keys-keymap [(meta v)] 'cua-repeat-replace-region)
1420 (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1421
1422 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1423 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
1424
1425 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1426 (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler)
1427 (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler)
1428 (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler)
1429 (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler)
1430 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1431 (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler)
1432 (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler)
1433 (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler)
1434 (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler)
1435
1436 ;; Enable shifted fallbacks for C-x and C-c when region is active
1437 (define-key cua--region-keymap [(shift control x)] 'cua--shift-control-x-prefix)
1438 (define-key cua--region-keymap [(shift control c)] 'cua--shift-control-c-prefix)
1439
1440 ;; replace current region
1441 (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region)
1442 (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region)
1443 (define-key cua--region-keymap [remap insert-register] 'cua-replace-region)
1444 (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region)
1445 (define-key cua--region-keymap [remap newline] 'cua-replace-region)
1446 (define-key cua--region-keymap [remap open-line] 'cua-replace-region)
1447 ;; delete current region
1448 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1449 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1450 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1451 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1452 ;; kill region
1453 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1454 (define-key cua--region-keymap [remap clipboard-kill-region] 'cua-cut-region)
1455 ;; copy region
1456 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1457 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1458 (define-key cua--region-keymap [remap clipboard-kill-ring-save] 'cua-copy-region)
1459 ;; cancel current region/rectangle
1460 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1461 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1462 )
1463
1464
1465 ;; Setup standard movement commands to be recognized by CUA.
1466
1467 (dolist (cmd
1468 '(forward-char backward-char
1469 next-line previous-line
1470 forward-word backward-word
1471 end-of-line beginning-of-line
1472 move-end-of-line move-beginning-of-line
1473 end-of-buffer beginning-of-buffer
1474 scroll-up scroll-down
1475 up-list down-list backward-up-list
1476 end-of-defun beginning-of-defun
1477 forward-sexp backward-sexp
1478 forward-list backward-list
1479 forward-sentence backward-sentence
1480 forward-paragraph backward-paragraph))
1481 (put cmd 'CUA 'move))
1482
1483 ;; State prior to enabling cua-mode
1484 ;; Value is a list with the following elements:
1485 ;; transient-mark-mode
1486 ;; delete-selection-mode
1487 ;; pc-selection-mode
1488
1489 (defvar cua--saved-state nil)
1490
1491 ;;;###autoload
1492 (define-minor-mode cua-mode
1493 "Toggle CUA key-binding mode.
1494 When enabled, using shifted movement keys will activate the
1495 region (and highlight the region using `transient-mark-mode'),
1496 and typed text replaces the active selection.
1497
1498 Also when enabled, you can use C-z, C-x, C-c, and C-v to undo,
1499 cut, copy, and paste in addition to the normal Emacs bindings.
1500 The C-x and C-c keys only do cut and copy when the region is
1501 active, so in most cases, they do not conflict with the normal
1502 function of these prefix keys.
1503
1504 If you really need to perform a command which starts with one of
1505 the prefix keys even when the region is active, you have three
1506 options:
1507 - press the prefix key twice very quickly (within 0.2 seconds),
1508 - press the prefix key and the following key within 0.2 seconds, or
1509 - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c.
1510
1511 You can customize `cua-enable-cua-keys' to completely disable the
1512 CUA bindings, or `cua-prefix-override-inhibit-delay' to change
1513 the prefix fallback behavior.
1514
1515 CUA mode manages Transient Mark mode internally. Trying to disable
1516 Transient Mark mode while CUA mode is enabled does not work; if you
1517 only want to highlight the region when it is selected using a
1518 shifted movement key, set `cua-highlight-region-shift-only'."
1519 :global t
1520 :group 'cua
1521 :set-after '(cua-enable-modeline-indications
1522 cua-rectangle-mark-key cua-rectangle-modifier-key)
1523 :require 'cua-base
1524 :link '(emacs-commentary-link "cua-base.el")
1525 (setq mark-even-if-inactive t)
1526 (setq highlight-nonselected-windows nil)
1527
1528 (unless cua--keymaps-initalized
1529 (cua--init-keymaps)
1530 (setq cua--keymaps-initalized t))
1531
1532 (if cua-mode
1533 (progn
1534 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1535 (add-hook 'post-command-hook 'cua--post-command-handler)
1536 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1537 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
1538 (if cua-enable-cursor-indications
1539 (cua--update-indications)))
1540
1541 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1542 (remove-hook 'post-command-hook 'cua--post-command-handler))
1543
1544 (if (not cua-mode)
1545 (setq emulation-mode-map-alists (delq 'cua--keymap-alist emulation-mode-map-alists))
1546 (add-to-ordered-list 'emulation-mode-map-alists 'cua--keymap-alist 400)
1547 (cua--select-keymaps))
1548
1549 (cond
1550 (cua-mode
1551 (setq cua--saved-state
1552 (list
1553 transient-mark-mode
1554 (and (boundp 'delete-selection-mode) delete-selection-mode)
1555 (and (boundp 'pc-selection-mode) pc-selection-mode)))
1556 (if (and (boundp 'delete-selection-mode) delete-selection-mode)
1557 (delete-selection-mode -1))
1558 (if (and (boundp 'pc-selection-mode) pc-selection-mode)
1559 (pc-selection-mode -1))
1560 (cua--deactivate)
1561 (setq transient-mark-mode (and cua-mode
1562 (if cua-highlight-region-shift-only
1563 (not cua--explicit-region-start)
1564 t))))
1565 (cua--saved-state
1566 (setq transient-mark-mode (car cua--saved-state))
1567 (if (nth 1 cua--saved-state)
1568 (delete-selection-mode 1))
1569 (if (nth 2 cua--saved-state)
1570 (pc-selection-mode 1))
1571 (if (interactive-p)
1572 (message "CUA mode disabled.%s%s%s%s"
1573 (if (nth 1 cua--saved-state) " Delete-Selection" "")
1574 (if (and (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " and" "")
1575 (if (nth 2 cua--saved-state) " PC-Selection" "")
1576 (if (or (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " enabled" "")))
1577 (setq cua--saved-state nil))))
1578
1579
1580 ;;;###autoload
1581 (defun cua-selection-mode (arg)
1582 "Enable CUA selection mode without the C-z/C-x/C-c/C-v bindings."
1583 (interactive "P")
1584 (setq-default cua-enable-cua-keys nil)
1585 (cua-mode arg))
1586
1587
1588 (defun cua-debug ()
1589 "Toggle CUA debugging."
1590 (interactive)
1591 (setq cua--debug (not cua--debug)))
1592
1593 ;; Install run-time check for older versions of CUA-mode which does not
1594 ;; work with GNU Emacs version 22.1 and newer.
1595 ;;
1596 ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
1597 ;; provided the `CUA-mode' feature. Since this is no longer true,
1598 ;; we can warn the user if the `CUA-mode' feature is ever provided.
1599
1600 ;;;###autoload (eval-after-load 'CUA-mode
1601 ;;;###autoload '(error (concat "\n\n"
1602 ;;;###autoload "CUA-mode is now part of the standard GNU Emacs distribution, so you may\n"
1603 ;;;###autoload "now enable CUA via the Options menu or by customizing option `cua-mode'.\n\n"
1604 ;;;###autoload "You have loaded an older version of CUA-mode which does\n"
1605 ;;;###autoload "not work correctly with this version of GNU Emacs.\n\n"
1606 ;;;###autoload (if user-init-file (concat
1607 ;;;###autoload "To correct this, remove the loading and customization of the\n"
1608 ;;;###autoload "old version from the " user-init-file " file.\n\n")))))
1609
1610 (provide 'cua)
1611
1612 ;;; arch-tag: 21fb6289-ba25-4fee-bfdc-f9fb351acf05
1613 ;;; cua-base.el ends here