1 ;;; cua-base.el --- emulate CUA key bindings
3 ;; Copyright (C) 1997,98,99,200,01,02,03 Free Software Foundation, Inc.
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulation convenience cua
8 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
28 ;; This is the CUA package which provides a complete emulation of the
29 ;; standard CUA key bindings (Motif/Windows/Mac GUI) for selecting and
30 ;; manipulating the region where S-<movement> is used to highlight &
33 ;; CUA style key bindings for cut and paste
34 ;; ----------------------------------------
36 ;; This package allows the C-z, C-x, C-c, and C-v keys to be
37 ;; bound appropriately according to the Motif/Windows GUI, i.e.
43 ;; The tricky part is the handling of the C-x and C-c keys which
44 ;; are normally used as prefix keys for most of emacs' built-in
45 ;; commands. With CUA they still do!!!
47 ;; Only when the region is currently active (and highlighted since
48 ;; transient-mark-mode is used), the C-x and C-c keys will work as CUA
52 ;; When the region is not active, C-x and C-c works as prefix keys!
54 ;; This probably sounds strange and difficult to get used to - but
55 ;; based on my own experience and the feedback from many users of
56 ;; this package, it actually works very well and users adapt to it
57 ;; instantly - or at least very quickly. So give it a try!
58 ;; ... and in the few cases where you make a mistake and accidentally
59 ;; delete the region - you just undo the mistake (with C-z).
61 ;; If you really need to perform a command which starts with one of
62 ;; the prefix keys even when the region is active, you have three options:
63 ;; - press the prefix key twice very quickly (within 0.2 seconds),
64 ;; - press the prefix key and the following key within 0.2 seconds), or
65 ;; - use the SHIFT key with the prefix key, i.e. C-X or C-C
67 ;; This behaviour can be customized via the
68 ;; cua-prefix-override-inhibit-delay variable.
70 ;; In addition to using the shifted movement keys, you can also use
71 ;; [C-space] to start the region and use unshifted movement keys to extend
72 ;; it. To cancel the region, use [C-space] or [C-g].
74 ;; If you prefer to use the standard emacs cut, copy, paste, and undo
75 ;; bindings, customize cua-enable-cua-keys to nil.
78 ;; Typing text replaces the region
79 ;; -------------------------------
81 ;; When the region is active, i.e. highlighted, the text in region is
82 ;; replaced by the text you type.
84 ;; The replaced text is saved in register 0 which can be inserted using
85 ;; the key sequence M-0 C-v (see the section on register support below).
87 ;; If you have just replaced a highlighted region with typed text,
88 ;; you can repeat the replace with M-v. This will search forward
89 ;; for a streach of text identical to the previous contents of the
90 ;; region (i.e. the contents of register 0) and replace it with the
91 ;; text you typed to replace the original region. Repeating M-v will
92 ;; replace the next matching region and so on.
94 ;; Example: Suppose you have a line like this
95 ;; The redo operation will redo the last redoable command
96 ;; which you want to change into
97 ;; The repeat operation will repeat the last repeatable command
98 ;; This is done by highlighting the first occurrence of "redo"
99 ;; and type "repeat" M-v M-v.
101 ;; Note: Since CUA-mode duplicates the functionality of the
102 ;; delete-selection-mode, that mode is automatically disabled when
103 ;; CUA-mode is enabled.
106 ;; CUA mode indications
107 ;; --------------------
108 ;; You can choose to let CUA use different cursor colors to indicate
109 ;; overwrite mode and read-only buffers. For example, the following
110 ;; setting will use a RED cursor in normal (insertion) mode in
111 ;; read-write buffers, a YELLOW cursor in overwrite mode in read-write
112 ;; buffers, and a GREEN cursor read-only buffers:
114 ;; (setq cua-normal-cursor-color "red")
115 ;; (setq cua-overwrite-cursor-color "yellow")
116 ;; (setq cua-read-only-cursor-color "green")
119 ;; CUA register support
120 ;; --------------------
121 ;; Emacs' standard register support is also based on a separate set of
122 ;; "register commands".
124 ;; CUA's register support is activated by providing a numeric
125 ;; prefix argument to the C-x, C-c, and C-v commands. For example,
126 ;; to copy the selected region to register 2, enter [M-2 C-c].
127 ;; Or if you have activated the keypad prefix mode, enter [kp-2 C-c].
129 ;; And CUA will copy and paste normal region as well as rectangles
130 ;; into the registers, i.e. you use exactly the same command for both.
132 ;; In addition, the last highlighted text that is deleted (not
133 ;; copied), e.g. by [delete] or by typing text over a highlighted
134 ;; region, is automatically saved in register 0, so you can insert it
137 ;; CUA rectangle support
138 ;; ---------------------
139 ;; Emacs' normal rectangle support is based on interpreting the region
140 ;; between the mark and point as a "virtual rectangle", and using a
141 ;; completely separate set of "rectangle commands" [C-x r ...] on the
142 ;; region to copy, kill, fill a.s.o. the virtual rectangle.
144 ;; cua-mode's superior rectangle support is based on using a true visual
145 ;; representation of the selected rectangle. To start a rectangle, use
146 ;; [S-return] and extend it using the normal movement keys (up, down,
147 ;; left, right, home, end, C-home, C-end). Once the rectangle has the
148 ;; desired size, you can cut or copy it using C-x and C-c (or C-w and M-w),
149 ;; and you can subsequently insert it - as a rectangle - using C-v (or
150 ;; C-y). So the only new command you need to know to work with
151 ;; cua-mode rectangles is S-return!
153 ;; Normally, when you paste a rectangle using C-v (C-y), each line of
154 ;; the rectangle is inserted into the existing lines in the buffer.
155 ;; If overwrite-mode is active when you paste a rectangle, it is
156 ;; inserted as normal (multi-line) text.
158 ;; Furthermore, cua-mode's rectangles are not limited to the actual
159 ;; contents of the buffer, so if the cursor is currently at the end of a
160 ;; short line, you can still extend the rectangle to include more columns
161 ;; of longer lines in the same rectangle. Sounds strange? Try it!
163 ;; You can enable padding for just this rectangle by pressing [M-p];
164 ;; this works like entering `picture-mode' where the tabs and spaces
165 ;; are automatically converted/inserted to make the rectangle truly
166 ;; rectangular. Or you can do it for all rectangles by setting the
167 ;; `cua-auto-expand-rectangles' variable.
169 ;; And there's more: If you want to extend or reduce the size of the
170 ;; rectangle in one of the other corners of the rectangle, just use
171 ;; [return] to move the cursor to the "next" corner. Or you can use
172 ;; the [M-up], [M-down], [M-left], and [M-right] keys to move the
173 ;; entire rectangle overlay (but not the contents) in the given
176 ;; [S-return] cancels the rectangle
177 ;; [C-space] activates the region bounded by the rectangle
179 ;; If you type a normal (self-inserting) character when the rectangle is
180 ;; active, the character is inserted on the "current side" of every line
181 ;; of the rectangle. The "current side" is the side on which the cursor
182 ;; is currently located. If the rectangle is only 1 column wide,
183 ;; insertion will be performed to the left when the cursor is at the
184 ;; bottom of the rectangle. So, for example, to comment out an entire
185 ;; paragraph like this one, just place the cursor on the first character
186 ;; of the first line, and enter the following:
187 ;; S-return M-} ; ; <space> S-return
189 ;; cua-mode's rectangle support also includes all the normal rectangle
190 ;; functions with easy access:
192 ;; [M-a] aligns all words at the left edge of the rectangle
193 ;; [M-b] fills the rectangle with blanks (tabs and spaces)
194 ;; [M-c] closes the rectangle by removing all blanks at the left edge
196 ;; [M-f] fills the rectangle with a single character (prompt)
197 ;; [M-i] increases the first number found on each line of the rectangle
198 ;; by the amount given by the numeric prefix argument (default 1)
199 ;; It recognizes 0x... as hexadecimal numbers
200 ;; [M-k] kills the rectangle as normal multi-line text (for paste)
201 ;; [M-l] downcases the rectangle
202 ;; [M-m] copies the rectangle as normal multi-line text (for paste)
203 ;; [M-n] fills each line of the rectangle with increasing numbers using
204 ;; a supplied format string (prompt)
205 ;; [M-o] opens the rectangle by moving the highlighted text to the
206 ;; right of the rectangle and filling the rectangle with blanks.
207 ;; [M-p] toggles rectangle padding, i.e. insert tabs and spaces to
208 ;; make rectangles truly rectangular
209 ;; [M-q] performs text filling on the rectangle
210 ;; [M-r] replaces REGEXP (prompt) by STRING (prompt) in rectangle
211 ;; [M-R] reverse the lines in the rectangle
212 ;; [M-s] fills each line of the rectangle with the same STRING (prompt)
213 ;; [M-t] performs text fill of the rectangle with TEXT (prompt)
214 ;; [M-u] upcases the rectangle
215 ;; [M-|] runs shell command on rectangle
216 ;; [M-'] restricts rectangle to lines with CHAR (prompt) at left column
217 ;; [M-/] restricts rectangle to lines matching REGEXP (prompt)
218 ;; [C-?] Shows a brief list of the above commands.
220 ;; [M-C-up] and [M-C-down] scrolls the lines INSIDE the rectangle up
221 ;; and down; lines scrolled outside the top or bottom of the rectangle
222 ;; are lost, but can be recovered using [C-z].
226 ;; The final feature provided by CUA is the "global mark", which
227 ;; makes it very easy to copy bits and pieces from the same and other
228 ;; files into the current text. To enable and cancel the global mark,
229 ;; use [S-C-space]. The cursor will blink when the global mark
230 ;; is active. The following commands behave differently when the global
232 ;; <ch> All characters (including newlines) you type are inserted
233 ;; at the global mark!
234 ;; [C-x] If you cut a region or rectangle, it is automatically inserted
235 ;; at the global mark, and the global mark is advanced.
236 ;; [C-c] If you copy a region or rectangle, it is immediately inserted
237 ;; at the global mark, and the global mark is advanced.
238 ;; [C-v] Copies a single character to the global mark.
239 ;; [C-d] Moves (i.e. deletes and inserts) a single character to the
241 ;; [backspace] deletes the character before the global mark, while
242 ;; [delete] deltes the character after the global mark.
244 ;; [S-C-space] Jumps to and cancels the global mark.
245 ;; [C-u S-C-space] Cancels the global mark (stays in current buffer).
247 ;; [TAB] Indents the current line or rectangle to the column of the
255 "Emulate CUA key bindings including C-x and C-c."
257 :group
'editing-basics
260 :link
'(emacs-commentary-link :tag
"Commentary" "cua-base.el")
261 :link
'(emacs-library-link :tag
"Lisp File" "cua-base.el"))
263 (defcustom cua-enable-cua-keys t
264 "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste.
265 If the value is t, these mappings are always enabled. If the value is
266 'shift, these keys are only enabled if the last region was marked with
267 a shifted movement key. If the value is nil, these keys are never
269 :type
'(choice (const :tag
"Disabled" nil
)
270 (const :tag
"Shift region only" shift
)
271 (other :tag
"Enabled" t
))
274 (defcustom cua-highlight-region-shift-only nil
275 "*If non-nil, only highlight region if marked with S-<move>.
276 When this is non-nil, CUA toggles `transient-mark-mode' on when the region
277 is marked using shifted movement keys, and off when the mark is cleared.
278 But when the mark was set using \\[cua-set-mark], transient-mark-mode
283 (defcustom cua-prefix-override-inhibit-delay
284 (if (featurep 'lisp-float-type
) (/ (float 1) (float 5)) nil
)
285 "*If non-nil, time in seconds to delay before overriding prefix key.
286 If there is additional input within this time, the prefix key is
287 used as a normal prefix key. So typing a key sequence quickly will
288 inhibit overriding the prefix key.
289 As a special case, if the prefix keys repeated within this time, the
290 first prefix key is discarded, so typing a prefix key twice in quick
291 succession will also inhibit overriding the prefix key.
292 If the value is nil, use a shifted prefix key to inhibit the override."
293 :type
'(choice (number :tag
"Inhibit delay")
294 (const :tag
"No delay" nil
))
297 (defcustom cua-keep-region-after-copy nil
298 "If non-nil, don't deselect the region after copying."
302 (defcustom cua-enable-register-prefix
'not-ctrl-u
303 "*If non-nil, registers are supported via numeric prefix arg.
304 If the value is t, any numeric prefix arg in the range 0 to 9 will be
305 interpreted as a register number.
306 If the value is not-ctrl-u, using C-u to enter a numeric prefix is not
307 interpreted as a register number.
308 If the value is ctrl-u-only, only numeric prefix entered with C-u is
309 interpreted as a register number."
310 :type
'(choice (const :tag
"Disabled" nil
)
311 (const :tag
"Enabled, but C-u arg is not a register" not-ctrl-u
)
312 (const :tag
"Enabled, but only for C-u arg" ctrl-u-only
)
313 (other :tag
"Enabled" t
))
316 (defcustom cua-delete-copy-to-register-0 t
317 "*If non-nil, save last deleted region or rectangle to register 0."
321 (defcustom cua-use-hyper-key nil
322 "*If non-nil, bind rectangle commands to H-? instead of M-?.
323 If set to 'also, toggle region command is also on S-return.
324 Must be set prior to enabling CUA."
325 :type
'(choice (const :tag
"Meta key and S-return" nil
)
326 (const :tag
"Hyper key only" only
)
327 (const :tag
"Hyper key and S-return" also
))
330 (defcustom cua-enable-region-auto-help nil
331 "*If non-nil, automatically show help for active region."
335 (defcustom cua-enable-modeline-indications nil
336 "*If non-nil, use minor-mode hook to show status in mode line."
340 (defcustom cua-check-pending-input t
341 "*If non-nil, don't override prefix key if input pending.
342 It is rumoured that input-pending-p is unreliable under some window
343 managers, so try setting this to nil, if prefix override doesn't work."
348 ;;; Rectangle Customization
350 (defcustom cua-auto-expand-rectangles nil
351 "*If non-nil, rectangles are padded with spaces to make straight edges.
352 This implies modifying buffer contents by expanding tabs and inserting spaces.
353 Consequently, this is inhibited in read-only buffers.
354 Can be toggled by [M-p] while the rectangle is active,"
358 (defcustom cua-enable-rectangle-auto-help t
359 "*If non-nil, automatically show help for region, rectangle and global mark."
363 (defface cua-rectangle-face
'nil
364 "*Font used by CUA for highlighting the rectangle."
367 (defface cua-rectangle-noselect-face
'nil
368 "*Font used by CUA for highlighting the non-selected rectangle lines."
371 (defcustom cua-undo-max
64
372 "*Max no of undoable CUA rectangle changes (including undo)."
377 ;;; Global Mark Customization
379 (defcustom cua-global-mark-keep-visible t
380 "*If non-nil, always keep global mark visible in other window."
384 (defface cua-global-mark-face
'((((class color
))
386 :background
"yellow")
388 "*Font used by CUA for highlighting the global mark."
391 (defcustom cua-global-mark-blink-cursor-interval
0.20
392 "*Blink cursor at this interval when global mark is active."
393 :type
'(choice (number :tag
"Blink interval")
394 (const :tag
"No blink" nil
))
398 ;;; Cursor Indication Customization
400 (defcustom cua-enable-cursor-indications nil
401 "*If non-nil, use different cursor colors for indications."
405 (defcustom cua-normal-cursor-color
(or (and (boundp 'initial-cursor-color
) initial-cursor-color
)
406 (and (boundp 'initial-frame-alist
)
407 (assoc 'cursor-color initial-frame-alist
)
408 (cdr (assoc 'cursor-color initial-frame-alist
)))
409 (and (boundp 'default-frame-alist
)
410 (assoc 'cursor-color default-frame-alist
)
411 (cdr (assoc 'cursor-color default-frame-alist
)))
412 (frame-parameter nil
'cursor-color
)
414 "Normal (non-overwrite) cursor color.
415 Also used to indicate that rectangle padding is not in effect.
416 Default is to load cursor color from initial or default frame parameters."
417 :initialize
'custom-initialize-default
421 (defcustom cua-read-only-cursor-color
"darkgreen"
422 "*Cursor color used in read-only buffers, if non-nil."
426 (defcustom cua-overwrite-cursor-color
"yellow"
427 "*Cursor color used when overwrite mode is set, if non-nil.
428 Also used to indicate that rectangle padding is in effect."
432 (defcustom cua-global-mark-cursor-color
"cyan"
433 "*Indication for active global mark.
434 Will change cursor color to specified color if string."
439 ;;; Rectangle support is in cua-rect.el
441 (autoload 'cua-set-rectangle-mark
"cua-rect" nil t nil
)
443 ;; Stub definitions until it is loaded
445 (when (not (featurep 'cua-rect
))
446 (defvar cua--rectangle
)
447 (setq cua--rectangle nil
)
448 (defvar cua--last-killed-rectangle
)
449 (setq cua--last-killed-rectangle nil
))
453 ;;; Global Mark support is in cua-gmrk.el
455 (autoload 'cua-toggle-global-mark
"cua-gmrk.el" nil t nil
)
457 ;; Stub definitions until cua-gmrk.el is loaded
459 (when (not (featurep 'cua-gmrk
))
460 (defvar cua--global-mark-active
)
461 (setq cua--global-mark-active nil
))
472 ;;; Low-level Interface
474 (defvar cua-inhibit-cua-keys nil
475 "Buffer-local variable that may disable the cua keymappings.")
476 (make-variable-buffer-local 'cua-inhibit-cua-keys
)
480 ;; Current region was started using cua-set-mark.
481 (defvar cua--explicit-region-start nil
)
483 ;; Latest region was started using shifted movement command.
484 (defvar cua--last-region-shifted nil
)
486 ;; buffer + point prior to current command when rectangle is active
487 ;; checked in post-command hook to see if point was moved
488 (defvar cua--buffer-and-point-before-command nil
)
490 ;; status string for mode line indications
491 (defvar cua--status-string nil
)
493 (defvar cua--debug nil
)
496 ;;; Prefix key override mechanism
498 ;; The prefix override (when mark-active) operates in three substates:
499 ;; [1] Before using a prefix key
500 ;; [2] Immediately after using a prefix key
501 ;; [3] A fraction of a second later
503 ;; In state [1], the cua--prefix-override-keymap is active.
504 ;; This keymap binds the C-x and C-c prefix keys to the
505 ;; cua--prefix-override-handler function.
507 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
508 ;; will push back the keys already read to the event queue. If input is
509 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
510 ;; is started, and it changes to state [2].
512 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
513 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
514 ;; C-x to the cua--prefix-repeat-handler function.
516 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
517 ;; will cancel [T], back the keys already read (except for the second prefix
518 ;; keys) to the event queue, and changes to state [3].
520 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
521 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
522 ;; the cua--prefix-override-timeout function will push a `timeout' event on
523 ;; the event queue, and changes to state [3].
525 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
526 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
527 ;; normal prefix key binding from the global or local map will be used.
529 ;; The pre-command hook (executed as a consequence of the timeout or normal
530 ;; prefix key binding) will cancel [T] and change from state [3] back to
531 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
532 ;; are always called with state reset to [1]!
534 ;; State [1] is recognized by cua--prefix-override-timer is nil,
535 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
536 ;; state [3] is recognized by cua--prefix-override-timer is t.
538 (defvar cua--prefix-override-timer nil
)
539 (defvar cua--prefix-override-length nil
)
541 (defun cua--prefix-override-replay (arg repeat
)
542 (let* ((keys (this-command-keys))
544 (key (aref keys
(1- i
))))
545 (setq cua--prefix-override-length
(- i repeat
))
546 (setq cua--prefix-override-timer
548 ;; In state [2], change to state [3]
550 ;; In state [1], change directly to state [3]
551 (and cua-check-pending-input
(input-pending-p))
552 ;; In state [1], [T] disabled, so change to state [3]
553 (not (numberp cua-prefix-override-inhibit-delay
))
554 (<= cua-prefix-override-inhibit-delay
0)
555 ;; In state [1], start [T] and change to state [2]
556 (run-with-timer cua-prefix-override-inhibit-delay nil
557 'cua--prefix-override-timeout
)))
558 ;; Don't record this command
559 (setq this-command last-command
)
560 ;; Restore the prefix arg
561 (setq prefix-arg arg
)
562 (reset-this-command-lengths)
563 ;; Push the key back on the event queue
564 (setq unread-command-events
(cons key unread-command-events
))))
566 (defun cua--prefix-override-handler (arg)
567 "Start timer waiting for prefix key to be followed by another key.
568 Repeating prefix key when region is active works as a single prefix key."
570 (cua--prefix-override-replay arg
0))
572 (defun cua--prefix-repeat-handler (arg)
573 "Repeating prefix key when region is active works as a single prefix key."
575 (cua--prefix-override-replay arg
1))
577 (defun cua--prefix-copy-handler (arg)
578 "Copy region/rectangle, then replay last key."
581 (cua-copy-rectangle arg
)
582 (cua-copy-region arg
))
583 (let ((keys (this-single-command-keys)))
584 (setq unread-command-events
585 (cons (aref keys
(1- (length keys
))) unread-command-events
))))
587 (defun cua--prefix-cut-handler (arg)
588 "Cut region/rectangle, then replay last key."
591 (cua-cut-rectangle arg
)
592 (cua-cut-region arg
))
593 (let ((keys (this-single-command-keys)))
594 (setq unread-command-events
595 (cons (aref keys
(1- (length keys
))) unread-command-events
))))
597 (defun cua--prefix-override-timeout ()
598 (setq cua--prefix-override-timer t
)
599 (when (= (length (this-command-keys)) cua--prefix-override-length
)
600 (setq unread-command-events
(cons 'timeout unread-command-events
))
602 (reset-this-command-lengths)
603 (setq overriding-terminal-local-map nil
))
604 (cua--select-keymaps)))
609 (defun cua--fallback ()
610 ;; Execute original command
611 (setq this-command this-original-command
)
612 (call-interactively this-command
))
614 (defun cua--keep-active ()
616 deactivate-mark nil
))
618 (defun cua--deactivate (&optional now
)
619 (setq cua--explicit-region-start nil
)
621 (setq deactivate-mark t
)
622 (setq mark-active nil
)
623 (run-hooks 'deactivate-mark-hook
)))
626 ;; The current register prefix
627 (defvar cua--register nil
)
629 (defun cua--prefix-arg (arg)
631 (and cua-enable-register-prefix
632 (integerp arg
) (>= arg
0) (< arg
10)
633 (let* ((prefix (aref (this-command-keys) 0))
634 (ctrl-u-prefix (and (integerp prefix
)
637 ((eq cua-enable-register-prefix
'not-ctrl-u
)
639 ((eq cua-enable-register-prefix
'ctrl-u-only
)
643 (if cua--register nil arg
))
645 ;;; Enhanced undo - restore rectangle selections
647 (defun cua-undo (&optional arg
)
648 "Undo some previous changes.
649 Knows about CUA rectangle highlighting in addition to standard undo."
651 (if (fboundp 'cua--rectangle-undo
)
652 (cua--rectangle-undo arg
)
655 ;;; Region specific commands
657 (defvar cua--last-deleted-region-pos nil
)
658 (defvar cua--last-deleted-region-text nil
)
660 (defun cua-delete-region ()
661 "Delete the active region.
662 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
664 (let ((start (mark)) (end (point)))
666 (setq start
(prog1 end
(setq end start
))))
667 (setq cua--last-deleted-region-text
(buffer-substring start end
))
668 (if cua-delete-copy-to-register-0
669 (set-register ?
0 cua--last-deleted-region-text
))
670 (delete-region start end
)
671 (setq cua--last-deleted-region-pos
672 (cons (current-buffer)
673 (and (consp buffer-undo-list
)
674 (car buffer-undo-list
))))
677 (defun cua-replace-region ()
678 "Replace the active region with the character you type."
681 (unless (eq this-original-command this-command
)
684 (defun cua-copy-region (arg)
685 "Copy the region to the kill ring.
686 With numeric prefix arg, copy to register 0-9 instead."
688 (setq arg
(cua--prefix-arg arg
))
689 (setq cua--last-killed-rectangle nil
)
690 (let ((start (mark)) (end (point)))
692 (setq start
(prog1 end
(setq end start
))))
694 (copy-to-register cua--register start end nil
)
695 (copy-region-as-kill start end
))
696 (if cua-keep-region-after-copy
700 (defun cua-cut-region (arg)
701 "Cut the region and copy to the kill ring.
702 With numeric prefix arg, copy to register 0-9 instead."
704 (setq cua--last-killed-rectangle nil
)
706 (cua-copy-region arg
)
707 (setq arg
(cua--prefix-arg arg
))
708 (let ((start (mark)) (end (point)))
710 (setq start
(prog1 end
(setq end start
))))
712 (copy-to-register cua--register start end t
)
713 (kill-region start end
)))
716 ;;; Generic commands for regions, rectangles, and global marks
719 "Cancel the active region, rectangle, or global mark."
721 (setq mark-active nil
)
722 (setq cua--explicit-region-start nil
)
723 (if (fboundp 'cua--cancel-rectangle
)
724 (cua--cancel-rectangle)))
726 (defun cua-paste (arg)
727 "Paste last cut or copied region or rectangle.
728 An active region is deleted before executing the command.
729 With numeric prefix arg, paste from register 0-9 instead.
730 If global mark is active, copy from register or one character."
732 (setq arg
(cua--prefix-arg arg
))
733 (let ((regtxt (and cua--register
(get-register cua--register
)))
734 (count (prefix-numeric-value arg
)))
736 ((and cua--register
(not regtxt
))
737 (message "Nothing in register %c" cua--register
))
738 (cua--global-mark-active
740 (cua--insert-at-global-mark regtxt
)
742 (cua--insert-at-global-mark (buffer-substring (point) (+ (point) count
)))
743 (forward-char count
))))
745 (message "Cannot paste into a read-only buffer"))
747 ;; Must save register here, since delete may override reg 0.
749 ;; Before a yank command, make sure we don't yank
750 ;; the same region that we are going to delete.
751 ;; That would make yank a no-op.
753 (cua--delete-rectangle)
754 (if (string= (buffer-substring (point) (mark))
757 (cua-delete-region)))
761 ((consp regtxt
) (cua--insert-rectangle regtxt
))
762 ((stringp regtxt
) (insert-for-yank regtxt
))
763 (t (message "Unknown data in register %c" cua--register
))))
764 ((and cua--last-killed-rectangle
765 (eq (and kill-ring
(car kill-ring
)) (car cua--last-killed-rectangle
)))
767 (when (not (eq buffer-undo-list t
))
768 (setq this-command
'cua--paste-rectangle
)
770 (setq buffer-undo-list
(cons pt buffer-undo-list
)))
771 (cua--insert-rectangle (cdr cua--last-killed-rectangle
))
772 (if arg
(goto-char pt
))))
775 (defun cua-paste-pop (arg)
776 "Replace a just-pasted text or rectangle with a different text.
777 See `yank-pop' for details."
779 (if (eq last-command
'cua--paste-rectangle
)
783 (yank-pop (prefix-numeric-value arg
))))
785 (defun cua-exchange-point-and-mark (arg)
786 "Exchanges point and mark, but don't activate the mark.
787 Activates the mark if a prefix argument is given."
792 (exchange-point-and-mark)
794 (cua--rectangle-corner 0)))))
796 ;; Typed text that replaced the highlighted region.
797 (defvar cua--repeat-replace-text nil
)
799 (defun cua-repeat-replace-region (arg)
800 "Repeat replacing text of highlighted region with typed text.
801 Searches for the next streach of text identical to the region last
802 replaced by typing text over it and replaces it with the same streach
805 (when cua--last-deleted-region-pos
808 (set-buffer (car cua--last-deleted-region-pos
))
810 ;; Find the text that replaced the region via the undo list.
811 (let ((ul buffer-undo-list
)
812 (elt (cdr cua--last-deleted-region-pos
))
816 (setq u
(car ul
) ul
(cdr ul
))
818 ((eq u elt
) ;; got it
820 ((and (consp u
) (integerp (car u
)) (integerp (cdr u
)))
821 (if (and s
(= (cdr u
) s
))
823 (setq s
(car u
) e
(cdr u
)))))))
824 (setq cua--repeat-replace-text
825 (cond ((and s e
(<= s e
) (= s
(mark t
)))
826 (buffer-substring-no-properties s e
))
827 ((and (null s
) (eq u elt
)) ;; nothing inserted
830 (message "Cannot locate replacement text")
832 (setq cua--last-deleted-region-pos nil
))
833 (if (and cua--last-deleted-region-text
834 cua--repeat-replace-text
835 (search-forward cua--last-deleted-region-text nil t nil
))
836 (replace-match cua--repeat-replace-text arg t
)))
838 (defun cua-help-for-region (&optional help
)
839 "Show region specific help in echo area."
842 (concat (if help
"C-?:help " "")
843 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
846 ;;; Shift activated / extended region
848 (defun cua-set-mark (&optional arg
)
849 "Set mark at where point is, clear mark, or jump to mark.
851 With no prefix argument, clear mark if already set. Otherwise, set
852 mark, and push old mark position on local mark ring; also push mark on
853 global mark ring if last mark was set in another buffer.
855 With argument, jump to mark, and pop a new position for mark off
856 the local mark ring \(this does not affect the global mark ring\).
857 Use \\[pop-global-mark] to jump to a mark off the global mark ring
858 \(see `pop-global-mark'\).
860 Repeating the command without the prefix jumps to the next position
861 off the local \(or global\) mark ring.
863 With a double \\[universal-argument] prefix argument, unconditionally set mark."
866 ((and (consp arg
) (> (prefix-numeric-value arg
) 4))
867 (push-mark-command nil
))
868 ((eq last-command
'pop-to-mark-command
)
869 (setq this-command
'pop-to-mark-command
)
870 (pop-to-mark-command))
871 ((and (eq last-command
'pop-global-mark
) (not arg
))
872 (setq this-command
'pop-global-mark
)
875 (setq this-command
'pop-to-mark-command
)
876 (pop-to-mark-command))
879 (message "Mark Cleared"))
881 (push-mark-command nil nil
)
882 (setq cua--explicit-region-start t
)
883 (setq cua--last-region-shifted nil
)
884 (if cua-enable-region-auto-help
885 (cua-help-for-region t
)))))
887 (defvar cua--standard-movement-commands
888 '(forward-char backward-char
889 next-line previous-line
890 forward-word backward-word
891 end-of-line beginning-of-line
892 end-of-buffer beginning-of-buffer
893 scroll-up scroll-down forward-paragraph backward-paragraph
)
894 "List of standard movement commands.
895 Extra commands should be added to `cua-movement-commands'")
897 (defvar cua-movement-commands nil
898 "User may add additional movement commands to this list.")
901 ;;; Cursor indications
903 (defun cua--update-indications ()
906 ((and cua--global-mark-active
907 (stringp cua-global-mark-cursor-color
))
908 cua-global-mark-cursor-color
)
909 ((and buffer-read-only
910 (stringp cua-read-only-cursor-color
))
911 cua-read-only-cursor-color
)
912 ((and (stringp cua-overwrite-cursor-color
)
914 (and cua--rectangle
(cua--rectangle-padding))))
915 cua-overwrite-cursor-color
)
916 (t cua-normal-cursor-color
))))
918 (not (equal cursor
(frame-parameter nil
'cursor-color
))))
919 (set-cursor-color cursor
))
925 (defun cua--pre-command-handler ()
927 (let ((movement (or (memq this-command cua--standard-movement-commands
)
928 (memq this-command cua-movement-commands
))))
930 ;; Cancel prefix key timeout if user enters another key.
931 (when cua--prefix-override-timer
932 (if (timerp cua--prefix-override-timer
)
933 (cancel-timer cua--prefix-override-timer
))
934 (setq cua--prefix-override-timer nil
))
936 ;; Handle shifted cursor keys and other movement commands.
937 ;; If region is not active, region is activated if key is shifted.
938 ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC).
939 ;; If rectangle is active, expand rectangle in specified direction and ignore the movement.
942 ((memq 'shift
(event-modifiers (aref (this-single-command-raw-keys) 0)))
944 (push-mark-command nil t
))
945 (setq cua--last-region-shifted t
)
946 (setq cua--explicit-region-start nil
))
947 ((or cua--explicit-region-start cua--rectangle
)
949 (push-mark-command nil nil
)))
951 ;; If we set mark-active to nil here, the region highlight will not be
952 ;; removed by the direct_output_ commands.
953 (setq deactivate-mark t
)))
955 ;; Handle delete-selection property on other commands
956 (if (and mark-active
(not deactivate-mark
))
957 (let* ((ds (or (get this-command
'delete-selection
)
958 (get this-command
'pending-delete
)))
969 'cua-delete-rectangle
973 'cua-delete-rectangle
;; replace?
974 'cua-replace-region
)))))
976 (setq this-original-command this-command
979 ;; Detect extension of rectangles by mouse or other movement
980 (setq cua--buffer-and-point-before-command
981 (if cua--rectangle
(cons (current-buffer) (point))))
985 ;;; Post-command hook
987 (defun cua--post-command-handler ()
990 (when cua--global-mark-active
991 (cua--global-mark-post-command))
992 (when (fboundp 'cua--rectangle-post-command
)
993 (cua--rectangle-post-command))
994 (setq cua--buffer-and-point-before-command nil
)
995 (if (or (not mark-active
) deactivate-mark
)
996 (setq cua--explicit-region-start nil
))
1001 (cua--rectangle (cua--rectangle-assert))
1002 (mark-active (message "Mark=%d Point=%d Expl=%s"
1003 (mark t
) (point) cua--explicit-region-start
))))
1005 ;; Disable transient-mark-mode if rectangle active in current buffer.
1006 (if (not (window-minibuffer-p (selected-window)))
1007 (setq transient-mark-mode
(and (not cua--rectangle
)
1008 (if cua-highlight-region-shift-only
1009 (not cua--explicit-region-start
)
1011 (if cua-enable-cursor-indications
1012 (cua--update-indications))
1014 (cua--select-keymaps)
1022 (defun cua--M/H-key
(map key fct
)
1023 ;; bind H-KEY or M-KEY to FCT in MAP
1024 (if (eq key
'space
) (setq key ?
))
1025 (unless (listp key
) (setq key
(list key
)))
1026 (define-key map
(vector (cons (if cua-use-hyper-key
'hyper
'meta
) key
)) fct
))
1028 (defun cua--self-insert-char-p (def)
1029 ;; Return DEF if current key sequence is self-inserting in
1031 (if (memq (global-key-binding (this-single-command-keys))
1032 '(self-insert-command self-insert-iso
))
1035 (defvar cua-global-keymap
(make-sparse-keymap)
1036 "Global keymap for cua-mode; users may add to this keymap.")
1038 (defvar cua--cua-keys-keymap
(make-sparse-keymap))
1039 (defvar cua--prefix-override-keymap
(make-sparse-keymap))
1040 (defvar cua--prefix-repeat-keymap
(make-sparse-keymap))
1041 (defvar cua--global-mark-keymap
(make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
1042 (defvar cua--rectangle-keymap
(make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
1043 (defvar cua--region-keymap
(make-sparse-keymap))
1045 (defvar cua--ena-cua-keys-keymap nil
)
1046 (defvar cua--ena-prefix-override-keymap nil
)
1047 (defvar cua--ena-prefix-repeat-keymap nil
)
1048 (defvar cua--ena-region-keymap nil
)
1049 (defvar cua--ena-global-mark-keymap nil
)
1051 (defvar cua--keymap-alist
1052 `((cua--ena-prefix-override-keymap .
,cua--prefix-override-keymap
)
1053 (cua--ena-prefix-repeat-keymap .
,cua--prefix-repeat-keymap
)
1054 (cua--ena-cua-keys-keymap .
,cua--cua-keys-keymap
)
1055 (cua--ena-global-mark-keymap .
,cua--global-mark-keymap
)
1056 (cua--rectangle .
,cua--rectangle-keymap
)
1057 (cua--ena-region-keymap .
,cua--region-keymap
)
1058 (cua-mode .
,cua-global-keymap
)))
1060 (defun cua--select-keymaps ()
1061 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
1062 (setq cua--ena-region-keymap
1063 (and mark-active
(not deactivate-mark
)))
1064 (setq cua--ena-prefix-override-keymap
1065 (and cua--ena-region-keymap
1067 (not cua-inhibit-cua-keys
)
1068 (or (eq cua-enable-cua-keys t
)
1069 (not cua--explicit-region-start
))
1070 (not executing-kbd-macro
)
1071 (not cua--prefix-override-timer
)))
1072 (setq cua--ena-prefix-repeat-keymap
1073 (and cua--ena-region-keymap
1074 (timerp cua--prefix-override-timer
)))
1075 (setq cua--ena-cua-keys-keymap
1076 (and cua-enable-cua-keys
1077 (not cua-inhibit-cua-keys
)
1078 (or (eq cua-enable-cua-keys t
)
1079 cua--last-region-shifted
)))
1080 (setq cua--ena-global-mark-keymap
1081 (and cua--global-mark-active
1082 (not (window-minibuffer-p)))))
1084 (defvar cua--keymaps-initalized nil
)
1086 (defun cua--init-keymaps ()
1087 (unless (eq cua-use-hyper-key
'only
)
1088 (define-key cua-global-keymap
[(shift return
)] 'cua-set-rectangle-mark
))
1089 (when cua-use-hyper-key
1090 (cua--M/H-key cua-global-keymap
'space
'cua-set-rectangle-mark
)
1091 (define-key cua-global-keymap
[(hyper mouse-1
)] 'cua-mouse-set-rectangle-mark
))
1093 (define-key cua-global-keymap
[(shift control ?
)] 'cua-toggle-global-mark
)
1095 ;; replace region with rectangle or element on kill ring
1096 (define-key cua-global-keymap
[remap yank
] 'cua-paste
)
1097 (define-key cua-global-keymap
[remap clipboard-yank
] 'cua-paste
)
1098 ;; replace current yank with previous kill ring element
1099 (define-key cua-global-keymap
[remap yank-pop
] 'cua-paste-pop
)
1101 (define-key cua-global-keymap
[remap set-mark-command
] 'cua-set-mark
)
1103 (define-key cua-global-keymap
[remap undo
] 'cua-undo
)
1104 (define-key cua-global-keymap
[remap advertised-undo
] 'cua-undo
)
1106 (define-key cua--cua-keys-keymap
[(control x
) timeout
] 'kill-region
)
1107 (define-key cua--cua-keys-keymap
[(control c
) timeout
] 'copy-region-as-kill
)
1108 (define-key cua--cua-keys-keymap
[(control z
)] 'undo
)
1109 (define-key cua--cua-keys-keymap
[(control v
)] 'yank
)
1110 (define-key cua--cua-keys-keymap
[(meta v
)] 'cua-repeat-replace-region
)
1111 (define-key cua--cua-keys-keymap
[remap exchange-point-and-mark
] 'cua-exchange-point-and-mark
)
1113 (define-key cua--prefix-override-keymap
[(control x
)] 'cua--prefix-override-handler
)
1114 (define-key cua--prefix-override-keymap
[(control c
)] 'cua--prefix-override-handler
)
1116 (define-key cua--prefix-repeat-keymap
[(control x
) (control x
)] 'cua--prefix-repeat-handler
)
1117 (define-key cua--prefix-repeat-keymap
[(control x
) up
] 'cua--prefix-cut-handler
)
1118 (define-key cua--prefix-repeat-keymap
[(control x
) down
] 'cua--prefix-cut-handler
)
1119 (define-key cua--prefix-repeat-keymap
[(control x
) left
] 'cua--prefix-cut-handler
)
1120 (define-key cua--prefix-repeat-keymap
[(control x
) right
] 'cua--prefix-cut-handler
)
1121 (define-key cua--prefix-repeat-keymap
[(control c
) (control c
)] 'cua--prefix-repeat-handler
)
1122 (define-key cua--prefix-repeat-keymap
[(control c
) up
] 'cua--prefix-copy-handler
)
1123 (define-key cua--prefix-repeat-keymap
[(control c
) down
] 'cua--prefix-copy-handler
)
1124 (define-key cua--prefix-repeat-keymap
[(control c
) left
] 'cua--prefix-copy-handler
)
1125 (define-key cua--prefix-repeat-keymap
[(control c
) right
] 'cua--prefix-copy-handler
)
1127 ;; Enable shifted fallbacks for C-x and C-c when region is active
1128 (define-key cua--region-keymap
[(shift control x
)] 'Control-X-prefix
)
1129 (define-key cua--region-keymap
[(shift control c
)] 'mode-specific-command-prefix
)
1130 ;; replace current region
1131 (define-key cua--region-keymap
[remap self-insert-command
] 'cua-replace-region
)
1132 (define-key cua--region-keymap
[remap self-insert-iso
] 'cua-replace-region
)
1133 (define-key cua--region-keymap
[remap insert-register
] 'cua-replace-region
)
1134 (define-key cua--region-keymap
[remap newline-and-indent
] 'cua-replace-region
)
1135 (define-key cua--region-keymap
[remap newline
] 'cua-replace-region
)
1136 (define-key cua--region-keymap
[remap open-line
] 'cua-replace-region
)
1137 ;; delete current region
1138 (define-key cua--region-keymap
[remap delete-backward-char
] 'cua-delete-region
)
1139 (define-key cua--region-keymap
[remap backward-delete-char
] 'cua-delete-region
)
1140 (define-key cua--region-keymap
[remap backward-delete-char-untabify
] 'cua-delete-region
)
1141 (define-key cua--region-keymap
[remap delete-char
] 'cua-delete-region
)
1143 (define-key cua--region-keymap
[remap kill-region
] 'cua-cut-region
)
1145 (define-key cua--region-keymap
[remap copy-region-as-kill
] 'cua-copy-region
)
1146 (define-key cua--region-keymap
[remap kill-ring-save
] 'cua-copy-region
)
1147 ;; cancel current region/rectangle
1148 (define-key cua--region-keymap
[remap keyboard-escape-quit
] 'cua-cancel
)
1149 (define-key cua--region-keymap
[remap keyboard-quit
] 'cua-cancel
)
1152 ;; State prior to enabling cua-mode
1153 ;; Value is a list with the following elements:
1154 ;; transient-mark-mode
1155 ;; delete-selection-mode
1156 ;; pc-selection-mode
1158 (defvar cua--saved-state nil
)
1161 (define-minor-mode cua-mode
1162 "Toggle CUA key-binding mode.
1163 When enabled, using shifted movement keys will activate the region (and
1164 highlight the region using `transient-mark-mode'), and typed text replaces
1165 the active selection. C-z, C-x, C-c, and C-v will undo, cut, copy, and
1166 paste (in addition to the normal emacs bindings)."
1168 :set-after
'(cua-enable-modeline-indications cua-use-hyper-key
)
1170 :link
'(emacs-commentary-link "cua-base.el")
1172 (setq mark-even-if-inactive t
)
1173 (setq highlight-nonselected-windows nil
)
1174 (make-variable-buffer-local 'cua--explicit-region-start
)
1175 (make-variable-buffer-local 'cua--status-string
)
1177 (unless cua--keymaps-initalized
1179 (setq cua--keymaps-initalized t
))
1183 (add-hook 'pre-command-hook
'cua--pre-command-handler
)
1184 (add-hook 'post-command-hook
'cua--post-command-handler
)
1185 (if (and cua-enable-modeline-indications
(not (assoc 'cua-mode minor-mode-alist
)))
1186 (setq minor-mode-alist
(cons '(cua-mode cua--status-string
) minor-mode-alist
)))
1188 (remove-hook 'pre-command-hook
'cua--pre-command-handler
)
1189 (remove-hook 'post-command-hook
'cua--post-command-handler
))
1192 (setq emulation-mode-map-alists
(delq 'cua--keymap-alist emulation-mode-map-alists
))
1193 (add-to-list 'emulation-mode-map-alists
'cua--keymap-alist
)
1194 (cua--select-keymaps))
1196 (if (fboundp 'cua--rectangle-on-off
)
1197 (cua--rectangle-on-off cua-mode
))
1201 (setq cua--saved-state
1204 (and (boundp 'delete-selection-mode
) delete-selection-mode
)
1205 (and (boundp 'pc-selection-mode
) pc-selection-mode
)))
1206 (if (and (boundp 'delete-selection-mode
) delete-selection-mode
)
1207 (delete-selection-mode -
1))
1208 (if (and (boundp 'pc-selection-mode
) pc-selection-mode
)
1209 (pc-selection-mode -
1))
1210 (setq transient-mark-mode
(and cua-mode
1211 (if cua-highlight-region-shift-only
1212 (not cua--explicit-region-start
)
1215 (setq transient-mark-mode
(car cua--saved-state
))
1216 (if (nth 1 cua--saved-state
)
1217 (delete-selection-mode 1))
1218 (if (nth 2 cua--saved-state
)
1219 (pc-selection-mode 1))
1221 (message "CUA mode disabled.%s%s%s%s"
1222 (if (nth 1 cua--saved-state
) " Delete-Selection" "")
1223 (if (and (nth 1 cua--saved-state
) (nth 2 cua--saved-state
)) " and" "")
1224 (if (nth 2 cua--saved-state
) " PC-Selection" "")
1225 (if (or (nth 1 cua--saved-state
) (nth 2 cua--saved-state
)) " enabled" "")))
1226 (setq cua--saved-state nil
))))
1229 "Toggle cua debugging."
1231 (setq cua--debug
(not cua--debug
)))
1233 ;; Install run-time check for older versions of CUA-mode which does not
1234 ;; work with GNU Emacs version 21.4 and newer.
1236 ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
1237 ;; provided the `CUA-mode' feature. Since this is no longer true,
1238 ;; we can warn the user if the `CUA-mode' feature is ever provided.
1240 ;;;###autoload (eval-after-load 'CUA-mode
1241 ;;;###autoload '(error (concat "\n\n"
1242 ;;;###autoload "CUA-mode is now part of the standard GNU Emacs distribution,\n"
1243 ;;;###autoload "so you may now enable and customize CUA via the Options menu.\n\n"
1244 ;;;###autoload "Your " (file-name-nondirectory user-init-file) " loads an older version of CUA-mode which does\n"
1245 ;;;###autoload "not work correctly with this version of GNU Emacs.\n"
1246 ;;;###autoload "To correct this, remove the loading and customization of the\n"
1247 ;;;###autoload "old version from the " user-init-file " file.\n\n")))
1251 ;;; arch-tag: 21fb6289-ba25-4fee-bfdc-f9fb351acf05
1252 ;;; cua-base.el ends here