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