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