1 ;;; cua-base.el --- emulate CUA key bindings
3 ;; Copyright (C) 1997-2002 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 ;; This package allow the C-z, C-x, C-c, and C-v keys to be
34 ;; bound appropriately according to the Motif/Windows GUI, i.e.
40 ;; The tricky part is the handling of the C-x and C-c keys which
41 ;; are normally used as prefix keys for most of emacs' built-in
42 ;; commands. With CUA they still do!!!
44 ;; Only when the region is currently active (and highlighted since
45 ;; transient-mark-mode is used), the C-x and C-c keys will work as CUA
49 ;; When the region is not active, C-x and C-c works as prefix keys!
51 ;; This probably sounds strange and difficult to get used to - but
52 ;; based on my own experience and the feedback from many users of
53 ;; this package, it actually works very well and users adapt to it
54 ;; instantly - or at least very quickly. So give it a try!
55 ;; ... and in the few cases where you make a mistake and accidentally
56 ;; delete the region - you just undo the mistake (with C-z).
58 ;; If you really need to perform a command which starts with one of
59 ;; the prefix keys even when the region is active, you have three options:
60 ;; - press the prefix key twice very quickly (within 0.2 seconds),
61 ;; - press the prefix key and the following key within 0.2 seconds), or
62 ;; - use the SHIFT key with the prefix key, i.e. C-X or C-C
64 ;; This behaviour can be customized via the
65 ;; cua-prefix-override-inhibit-delay variable.
67 ;; In addition to using the shifted movement keys, you can also use
68 ;; [C-space] to start the region and use unshifted movement keys to extend
69 ;; it. To cancel the region, use [C-space] or [C-g].
71 ;; If you prefer to use the standard emacs cut, copy, paste, and undo
72 ;; bindings, customize cua-enable-cua-keys to nil.
74 ;; CUA mode indications
75 ;; --------------------
76 ;; You can choose to let CUA use different cursor colors to indicate
77 ;; overwrite mode and read-only buffers. For example, the following
78 ;; setting will use a RED cursor in normal (insertion) mode in
79 ;; read-write buffers, a YELLOW cursor in overwrite mode in read-write
80 ;; buffers, and a GREEN cursor read-only buffers:
82 ;; (setq cua-normal-cursor-color "red")
83 ;; (setq cua-overwrite-cursor-color "yellow")
84 ;; (setq cua-read-only-cursor-color "green")
87 ;; CUA register support
88 ;; --------------------
89 ;; Emacs' standard register support is also based on a separate set of
90 ;; "register commands".
92 ;; CUA's register support is activated by providing a numeric
93 ;; prefix argument to the C-x, C-c, and C-v commands. For example,
94 ;; to copy the selected region to register 2, enter [M-2 C-c].
95 ;; Or if you have activated the keypad prefix mode, enter [kp-2 C-c].
97 ;; And CUA will copy and paste normal region as well as rectangles
98 ;; into the registers, i.e. you use exactly the same command for both.
100 ;; In addition, the last highlighted text that is deleted (not
101 ;; copied), e.g. by [delete] or by typing text over a highlighted
102 ;; region, is automatically saved in register 0, so you can insert it
105 ;; CUA rectangle support
106 ;; ---------------------
107 ;; Emacs' normal rectangle support is based on interpreting the region
108 ;; between the mark and point as a "virtual rectangle", and using a
109 ;; completely separate set of "rectangle commands" [C-x r ...] on the
110 ;; region to copy, kill, fill a.s.o. the virtual rectangle.
112 ;; cua-mode's superior rectangle support is based on using a true visual
113 ;; representation of the selected rectangle. To start a rectangle, use
114 ;; [S-return] and extend it using the normal movement keys (up, down,
115 ;; left, right, home, end, C-home, C-end). Once the rectangle has the
116 ;; desired size, you can cut or copy it using C-x and C-c (or C-w and M-w),
117 ;; and you can subsequently insert it - as a rectangle - using C-v (or
118 ;; C-y). So the only new command you need to know to work with
119 ;; cua-mode rectangles is S-return!
121 ;; Normally, when you paste a rectangle using C-v (C-y), each line of
122 ;; the rectangle is inserted into the existing lines in the buffer.
123 ;; If overwrite-mode is active when you paste a rectangle, it is
124 ;; inserted as normal (multi-line) text.
126 ;; Furthermore, cua-mode's rectangles are not limited to the actual
127 ;; contents of the buffer, so if the cursor is currently at the end of a
128 ;; short line, you can still extend the rectangle to include more columns
129 ;; of longer lines in the same rectangle. Sounds strange? Try it!
131 ;; You can enable padding for just this rectangle by pressing [M-p];
132 ;; this works like entering `picture-mode' where the tabs and spaces
133 ;; are automatically converted/inserted to make the rectangle truly
134 ;; rectangular. Or you can do it for all rectangles by setting the
135 ;; `cua-auto-expand-rectangles' variable.
137 ;; And there's more: If you want to extend or reduce the size of the
138 ;; rectangle in one of the other corners of the rectangle, just use
139 ;; [return] to move the cursor to the "next" corner. Or you can use
140 ;; the [M-up], [M-down], [M-left], and [M-right] keys to move the
141 ;; entire rectangle overlay (but not the contents) in the given
144 ;; [S-return] cancels the rectangle
145 ;; [C-space] activates the region bounded by the rectangle
147 ;; If you type a normal (self-inserting) character when the rectangle is
148 ;; active, the character is inserted on the "current side" of every line
149 ;; of the rectangle. The "current side" is the side on which the cursor
150 ;; is currently located. If the rectangle is only 1 column wide,
151 ;; insertion will be performed to the left when the cursor is at the
152 ;; bottom of the rectangle. So, for example, to comment out an entire
153 ;; paragraph like this one, just place the cursor on the first character
154 ;; of the first line, and enter the following:
155 ;; S-return M-} ; ; <space> S-return
157 ;; cua-mode's rectangle support also includes all the normal rectangle
158 ;; functions with easy access:
160 ;; [M-a] aligns all words at the left edge of the rectangle
161 ;; [M-b] fills the rectangle with blanks (tabs and spaces)
162 ;; [M-c] closes the rectangle by removing all blanks at the left edge
164 ;; [M-f] fills the rectangle with a single character (prompt)
165 ;; [M-i] increases the first number found on each line of the rectangle
166 ;; by the amount given by the numeric prefix argument (default 1)
167 ;; It recognizes 0x... as hexadecimal numbers
168 ;; [M-k] kills the rectangle as normal multi-line text (for paste)
169 ;; [M-l] downcases the rectangle
170 ;; [M-m] copies the rectangle as normal multi-line text (for paste)
171 ;; [M-n] fills each line of the rectangle with increasing numbers using
172 ;; a supplied format string (prompt)
173 ;; [M-o] opens the rectangle by moving the highlighted text to the
174 ;; right of the rectangle and filling the rectangle with blanks.
175 ;; [M-p] toggles rectangle padding, i.e. insert tabs and spaces to
176 ;; make rectangles truly rectangular
177 ;; [M-q] performs text filling on the rectangle
178 ;; [M-r] replaces REGEXP (prompt) by STRING (prompt) in rectangle
179 ;; [M-R] reverse the lines in the rectangle
180 ;; [M-s] fills each line of the rectangle with the same STRING (prompt)
181 ;; [M-t] performs text fill of the rectangle with TEXT (prompt)
182 ;; [M-u] upcases the rectangle
183 ;; [M-|] runs shell command on rectangle
184 ;; [M-'] restricts rectangle to lines with CHAR (prompt) at left column
185 ;; [M-/] restricts rectangle to lines matching REGEXP (prompt)
186 ;; [C-?] Shows a brief list of the above commands.
188 ;; [M-C-up] and [M-C-down] scrolls the lines INSIDE the rectangle up
189 ;; and down; lines scrolled outside the top or bottom of the rectangle
190 ;; are lost, but can be recovered using [C-z].
194 ;; The final feature provided by CUA is the "global mark", which
195 ;; makes it very easy to copy bits and pieces from the same and other
196 ;; files into the current text. To enable and cancel the global mark,
197 ;; use [S-C-space]. The cursor will blink when the global mark
198 ;; is active. The following commands behave differently when the global
200 ;; <ch> All characters (including newlines) you type are inserted
201 ;; at the global mark!
202 ;; [C-x] If you cut a region or rectangle, it is automatically inserted
203 ;; at the global mark, and the global mark is advanced.
204 ;; [C-c] If you copy a region or rectangle, it is immediately inserted
205 ;; at the global mark, and the global mark is advanced.
206 ;; [C-v] Copies a single character to the global mark.
207 ;; [C-d] Moves (i.e. deletes and inserts) a single character to the
209 ;; [backspace] deletes the character before the global mark, while
210 ;; [delete] deltes the character after the global mark.
212 ;; [S-C-space] Jumps to and cancels the global mark.
213 ;; [C-u S-C-space] Cancels the global mark (stays in current buffer).
215 ;; [TAB] Indents the current line or rectangle to the column of the
223 "Emulate CUA key bindings including C-x and C-c."
225 :group 'editing-basics
228 :link '(emacs-commentary-link :tag "Commentary" "cua-base.el")
229 :link '(emacs-library-link :tag "Lisp File" "cua-base.el"))
232 (defcustom cua-mode nil
233 "Non-nil means that CUA emulation mode is enabled.
234 In CUA mode, shifted movement keys highlight and extend the region.
235 When a region is highlighted, the binding of the C-x and C-c keys are
236 temporarily changed to work as Motif, MAC or MS-Windows cut and paste.
237 Also, insertion commands first delete the region and then insert.
238 This mode enables Transient Mark mode and it provides a superset of the
239 PC Selection Mode and Delete Selection Modes.
241 Setting this variable directly does not take effect;
242 use either \\[customize] or the function `cua-mode'."
243 :set (lambda (symbol value)
244 (cua-mode (or value 0)))
245 :initialize 'custom-initialize-default
246 :set-after '(cua-enable-modeline-indications cua-use-hyper-key)
248 :link '(emacs-commentary-link "cua-base.el")
254 (defcustom cua-enable-cua-keys t
255 "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste.
256 If the value is t, these mappings are always enabled. If the value is
257 'shift, these keys are only enabled if the last region was marked with
258 a shifted movement key. If the value is nil, these keys are never
260 :type '(choice (const :tag "Disabled" nil)
261 (const :tag "Shift region only" shift)
262 (other :tag "Enabled"))
265 (defcustom cua-highlight-region-shift-only nil
266 "*If non-nil, only highlight region if marked with S-<move>.
267 When this is non-nil, CUA toggles `transient-mark-mode' on when the region
268 is marked using shifted movement keys, and off when the mark is cleared.
269 But when the mark was set using \\[cua-set-mark], transient-mark-mode
274 (defcustom cua-prefix-override-inhibit-delay
275 (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) nil)
276 "*If non-nil, time in seconds to delay before overriding prefix key.
277 If there is additional input within this time, the prefix key is
278 used as a normal prefix key. So typing a key sequence quickly will
279 inhibit overriding the prefix key.
280 As a special case, if the prefix keys repeated within this time, the
281 first prefix key is discarded, so typing a prefix key twice in quick
282 succession will also inhibit overriding the prefix key.
283 If the value is nil, use a shifted prefix key to inhibit the override."
284 :type '(choice (number :tag "Inhibit delay")
285 (const :tag "No delay" nil))
288 (defcustom cua-keep-region-after-copy nil
289 "If non-nil, don't deselect the region after copying."
293 (defcustom cua-enable-register-prefix 'not-ctrl-u
294 "*If non-nil, registers are supported via numeric prefix arg.
295 If the value is t, any numeric prefix arg in the range 0 to 9 will be
296 interpreted as a register number.
297 If the value is not-ctrl-u, using C-u to enter a numeric prefix is not
298 interpreted as a register number.
299 If the value is ctrl-u-only, only numeric prefix entered with C-u is
300 interpreted as a register number."
301 :type '(choice (const :tag "Disabled" nil)
302 (const :tag "Enabled, but C-u arg is not a register" not-ctrl-u)
303 (const :tag "Enabled, but only for C-u arg" ctrl-u-only)
304 (other :tag "Enabled"))
307 (defcustom cua-delete-copy-to-register-0 t
308 "*If non-nil, save last deleted region or rectangle to register 0."
312 (defcustom cua-use-hyper-key nil
313 "*If non-nil, bind rectangle commands to H-? instead of M-?.
314 If set to 'also, toggle region command is also on S-return.
315 Must be set prior to enabling CUA."
316 :type '(choice (const :tag "Meta key and S-return" nil)
317 (const :tag "Hyper key only" only)
318 (const :tag "Hyper key and S-return" also))
321 (defcustom cua-enable-region-auto-help nil
322 "*If non-nil, automatically show help for active region."
326 (defcustom cua-enable-modeline-indications nil
327 "*If non-nil, use minor-mode hook to show status in mode line."
331 (defcustom cua-check-pending-input t
332 "*If non-nil, don't override prefix key if input pending.
333 It is rumoured that input-pending-p is unreliable under some window
334 managers, so try setting this to nil, if prefix override doesn't work."
339 ;;; Rectangle Customization
341 (defcustom cua-auto-expand-rectangles nil
342 "*If non-nil, rectangles are padded with spaces to make straight edges.
343 This implies modifying buffer contents by expanding tabs and inserting spaces.
344 Consequently, this is inhibited in read-only buffers.
345 Can be toggled by [M-p] while the rectangle is active,"
349 (defcustom cua-enable-rectangle-auto-help t
350 "*If non-nil, automatically show help for region, rectangle and global mark."
354 (defface cua-rectangle-face 'nil
355 "*Font used by CUA for highlighting the rectangle."
358 (defface cua-rectangle-noselect-face 'nil
359 "*Font used by CUA for highlighting the non-selected rectangle lines."
362 (defcustom cua-undo-max 64
363 "*Max no of undoable CUA rectangle changes (including undo)."
368 ;;; Global Mark Customization
370 (defcustom cua-global-mark-keep-visible t
371 "*If non-nil, always keep global mark visible in other window."
375 (defface cua-global-mark-face '((((class color))
376 (:foreground "black")
377 (:background "yellow"))
379 "*Font used by CUA for highlighting the global mark."
382 (defcustom cua-global-mark-blink-cursor-interval 0.20
383 "*Blink cursor at this interval when global mark is active."
384 :type '(choice (number :tag "Blink interval")
385 (const :tag "No blink" nil))
389 ;;; Cursor Indication Customization
391 (defcustom cua-enable-cursor-indications t
392 "*If non-nil, use different cursor colors for indications."
396 (defcustom cua-normal-cursor-color nil
397 "Normal (non-overwrite) cursor color.
398 Also used to indicate that rectangle padding is not in effect.
399 Automatically loaded from frame parameters, if nil."
400 :initialize (lambda (symbol value)
401 (set symbol (or value
402 (and (boundp 'initial-cursor-color) initial-cursor-color)
403 (and (boundp 'initial-frame-alist)
404 (assoc 'cursor-color initial-frame-alist)
405 (cdr (assoc 'cursor-color initial-frame-alist)))
406 (and (boundp 'default-frame-alist)
407 (assoc 'cursor-color default-frame-alist)
408 (cdr (assoc 'cursor-color default-frame-alist)))
409 (frame-parameter nil 'cursor-color))))
413 (defcustom cua-read-only-cursor-color "darkgreen"
414 "*Cursor color used in read-only buffers, if non-nil."
418 (defcustom cua-overwrite-cursor-color "yellow"
419 "*Cursor color used when overwrite mode is set, if non-nil.
420 Also used to indicate that rectangle padding is in effect."
424 (defcustom cua-global-mark-cursor-color "cyan"
425 "*Indication for active global mark.
426 Will change cursor color to specified color if string."
431 ;;; Rectangle support is in cua-rect.el
433 (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
435 ;; Stub definitions until it is loaded
437 (when (not (featurep 'cua-rect))
438 (defvar cua--rectangle)
439 (setq cua--rectangle nil)
440 (defvar cua--last-killed-rectangle)
441 (setq cua--last-killed-rectangle nil))
445 ;;; Global Mark support is in cua-gmrk.el
447 (autoload 'cua-toggle-global-mark "cua-gmrk.el" nil t nil)
449 ;; Stub definitions until cua-gmrk.el is loaded
451 (when (not (featurep 'cua-gmrk))
452 (defvar cua--global-mark-active)
453 (setq cua--global-mark-active nil))
464 ;;; Low-level Interface
466 (defvar cua-inhibit-cua-keys nil
467 "Buffer-local variable that may disable the cua keymappings.")
468 (make-variable-buffer-local 'cua-inhibit-cua-keys)
472 ;; Current region was started using cua-set-mark.
473 (defvar cua--explicit-region-start nil)
475 ;; Latest region was started using shifted movement command.
476 (defvar cua--last-region-shifted nil)
478 ;; buffer + point prior to current command when rectangle is active
479 ;; checked in post-command hook to see if point was moved
480 (defvar cua--buffer-and-point-before-command nil)
482 ;; status string for mode line indications
483 (defvar cua--status-string nil)
485 (defvar cua--debug nil)
488 ;;; Prefix key override mechanism
490 ;; The prefix override (when mark-active) operates in three substates:
491 ;; [1] Before using a prefix key
492 ;; [2] Immediately after using a prefix key
493 ;; [3] A fraction of a second later
495 ;; In state [1], the cua--prefix-override-keymap is active.
496 ;; This keymap binds the C-x and C-c prefix keys to the
497 ;; cua--prefix-override-handler function.
499 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
500 ;; will push back the keys already read to the event queue. If input is
501 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
502 ;; is started, and it changes to state [2].
504 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
505 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
506 ;; C-x to the cua--prefix-repeat-handler function.
508 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
509 ;; will cancel [T], back the keys already read (except for the second prefix
510 ;; keys) to the event queue, and changes to state [3].
512 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
513 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
514 ;; the cua--prefix-override-timeout function will push a `timeout' event on
515 ;; the event queue, and changes to state [3].
517 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
518 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
519 ;; normal prefix key binding from the global or local map will be used.
521 ;; The pre-command hook (executed as a consequence of the timeout or normal
522 ;; prefix key binding) will cancel [T] and change from state [3] back to
523 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
524 ;; are always called with state reset to [1]!
526 ;; State [1] is recognized by cua--prefix-override-timer is nil,
527 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
528 ;; state [3] is recognized by cua--prefix-override-timer is t.
530 (defvar cua--prefix-override-timer nil)
531 (defvar cua--prefix-override-length nil)
533 (defun cua--prefix-override-replay (arg repeat)
534 (let* ((keys (this-command-keys))
536 (key (aref keys (1- i))))
537 (setq cua--prefix-override-length (- i repeat))
538 (setq cua--prefix-override-timer
540 ;; In state [2], change to state [3]
542 ;; In state [1], change directly to state [3]
543 (and cua-check-pending-input (input-pending-p))
544 ;; In state [1], [T] disabled, so change to state [3]
545 (not (numberp cua-prefix-override-inhibit-delay))
546 (<= cua-prefix-override-inhibit-delay 0)
547 ;; In state [1], start [T] and change to state [2]
548 (run-with-timer cua-prefix-override-inhibit-delay nil
549 'cua--prefix-override-timeout)))
550 ;; Don't record this command
551 (setq this-command last-command)
552 ;; Restore the prefix arg
553 (setq prefix-arg arg)
554 (reset-this-command-lengths)
555 ;; Push the key back on the event queue
556 (setq unread-command-events (cons key unread-command-events))))
558 (defun cua--prefix-override-handler (arg)
559 "Start timer waiting for prefix key to be followed by another key.
560 Repeating prefix key when region is active works as a single prefix key."
562 (cua--prefix-override-replay arg 0))
564 (defun cua--prefix-repeat-handler (arg)
565 "Repeating prefix key when region is active works as a single prefix key."
567 (cua--prefix-override-replay arg 1))
569 (defun cua--prefix-copy-handler (arg)
570 "Copy region/rectangle, then replay last key."
573 (cua-copy-rectangle arg)
574 (cua-copy-region arg))
575 (let ((keys (this-single-command-keys)))
576 (setq unread-command-events
577 (cons (aref keys (1- (length keys))) unread-command-events))))
579 (defun cua--prefix-cut-handler (arg)
580 "Cut region/rectangle, then replay last key."
583 (cua-cut-rectangle arg)
584 (cua-cut-region arg))
585 (let ((keys (this-single-command-keys)))
586 (setq unread-command-events
587 (cons (aref keys (1- (length keys))) unread-command-events))))
589 (defun cua--prefix-override-timeout ()
590 (setq cua--prefix-override-timer t)
591 (when (= (length (this-command-keys)) cua--prefix-override-length)
592 (setq unread-command-events (cons 'timeout unread-command-events))
594 (reset-this-command-lengths)
595 (setq overriding-terminal-local-map nil))
596 (cua--select-keymaps)))
601 (defun cua--fallback ()
602 ;; Execute original command
603 (setq this-command this-original-command)
604 (call-interactively this-command))
606 (defun cua--keep-active ()
608 deactivate-mark nil))
610 (defun cua--deactivate (&optional now)
611 (setq cua--explicit-region-start nil)
613 (setq deactivate-mark t)
614 (setq mark-active nil)
615 (run-hooks 'deactivate-mark-hook)))
618 ;; The current register prefix
619 (defvar cua--register nil)
621 (defun cua--prefix-arg (arg)
623 (and cua-enable-register-prefix
624 (integerp (this-command-keys))
625 (cond ((eq cua-enable-register-prefix 'not-ctrl-u)
626 (not (= (aref (this-command-keys) 0) ?\C-u)))
627 ((eq cua-enable-register-prefix 'ctrl-u-only)
628 (= (aref (this-command-keys) 0) ?\C-u))
630 (integerp arg) (>= arg 0) (< arg 10)
632 (if cua--register nil arg))
635 ;;; Enhanced undo - restore rectangle selections
637 (defun cua-undo (&optional arg)
638 "Undo some previous changes.
639 Knows about CUA rectangle highlighting in addition to standard undo."
641 (if (fboundp 'cua--rectangle-undo)
642 (cua--rectangle-undo arg)
645 ;;; Region specific commands
647 (defun cua-delete-region ()
648 "Delete the active region.
649 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
651 (let ((start (mark)) (end (point)))
653 (setq start (prog1 end (setq end start))))
654 (if cua-delete-copy-to-register-0
655 (copy-to-register ?0 start end nil))
656 (delete-region start end)
659 (defun cua-replace-region ()
660 "Replace the active region with the character you type."
663 (if (not (eq this-original-command this-command))
666 (defun cua-copy-region (arg)
667 "Copy the region to the kill ring.
668 With numeric prefix arg, copy to register 0-9 instead."
670 (setq arg (cua--prefix-arg arg))
671 (setq cua--last-killed-rectangle nil)
672 (let ((start (mark)) (end (point)))
674 (setq start (prog1 end (setq end start))))
676 (copy-to-register cua--register start end nil)
677 (copy-region-as-kill start end))
678 (if cua-keep-region-after-copy
682 (defun cua-cut-region (arg)
683 "Cut the region and copy to the kill ring.
684 With numeric prefix arg, copy to register 0-9 instead."
686 (setq cua--last-killed-rectangle nil)
688 (cua-copy-region arg)
689 (setq arg (cua--prefix-arg arg))
690 (let ((start (mark)) (end (point)))
692 (setq start (prog1 end (setq end start))))
694 (copy-to-register cua--register start end t)
695 (kill-region start end)))
698 ;;; Generic commands for regions, rectangles, and global marks
701 "Cancel the active region, rectangle, or global mark."
703 (setq mark-active nil)
704 (setq cua--explicit-region-start nil)
705 (if (fboundp 'cua--cancel-rectangle)
706 (cua--cancel-rectangle)))
708 (defun cua-paste (arg)
709 "Paste last cut or copied region or rectangle.
710 An active region is deleted before executing the command.
711 With numeric prefix arg, paste from register 0-9 instead.
712 If global mark is active, copy from register or one character."
714 (setq arg (cua--prefix-arg arg))
715 (let ((regtxt (and cua--register (get-register cua--register)))
716 (count (prefix-numeric-value arg)))
718 ((and cua--register (not regtxt))
719 (message "Nothing in register %c" cua--register))
720 (cua--global-mark-active
722 (cua--insert-at-global-mark regtxt)
724 (cua--insert-at-global-mark (buffer-substring (point) (+ (point) count)))
725 (forward-char count))))
727 (message "Cannot paste into a read-only buffer"))
729 ;; Must save register here, since delete may override reg 0.
731 ;; Before a yank command, make sure we don't yank
732 ;; the same region that we are going to delete.
733 ;; That would make yank a no-op.
735 (cua--delete-rectangle)
736 (if (string= (buffer-substring (point) (mark))
739 (cua-delete-region)))
743 ((consp regtxt) (cua--insert-rectangle regtxt))
744 ((stringp regtxt) (insert-for-yank regtxt))
745 (t (message "Unknown data in register %c" cua--register))))
746 ((and cua--last-killed-rectangle
747 (eq (and kill-ring (car kill-ring)) (car cua--last-killed-rectangle)))
749 (when (not (eq buffer-undo-list t))
750 (setq this-command 'cua--paste-rectangle)
752 (setq buffer-undo-list (cons pt buffer-undo-list)))
753 (cua--insert-rectangle (cdr cua--last-killed-rectangle))
754 (if arg (goto-char pt))))
757 (defun cua-paste-pop (arg)
758 "Replace a just-pasted text or rectangle with a different text.
759 See `yank-pop' for details."
761 (if (eq last-command 'cua--paste-rectangle)
765 (yank-pop (prefix-numeric-value arg))))
767 (defun cua-exchange-point-and-mark (arg)
768 "Exchanges point and mark, but don't activate the mark.
769 Activates the mark if a prefix argument is given."
774 (exchange-point-and-mark)
776 (cua--rectangle-corner 0)))))
778 (defun cua-help-for-region (&optional help)
779 "Show region specific help in echo area."
782 (concat (if help "C-?:help " "")
783 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
786 ;;; Shift activated / extended region
788 (defun cua-set-mark (&optional arg)
789 "Set mark at where point is, clear mark, or jump to mark.
790 With no prefix argument, set mark, push old mark position on local mark
791 ring, and push mark on global mark ring, or if mark is already set, clear mark.
792 With argument, jump to mark, and pop a new position for mark off the ring;
793 then it jumps to the next mark off the ring if repeated with no argument, or
794 sets the mark at the new position if repeated with argument."
796 (if (and (eq this-command last-command)
798 (setq arg (if arg nil last-prefix-arg)
799 current-prefix-arg arg))
803 (error "No mark set in this buffer")
808 (message "Mark Cleared"))
810 (push-mark nil nil t)
811 (setq cua--explicit-region-start t)
812 (setq cua--last-region-shifted nil)
813 (if cua-enable-region-auto-help
814 (cua-help-for-region t)))))
816 (defvar cua--standard-movement-commands
817 '(forward-char backward-char
818 next-line previous-line
819 forward-word backward-word
820 end-of-line beginning-of-line
821 end-of-buffer beginning-of-buffer
822 scroll-up scroll-down forward-paragraph backward-paragraph)
823 "List of standard movement commands.
824 Extra commands should be added to `cua-user-movement-commands'")
826 (defvar cua-movement-commands nil
827 "User may add additional movement commands to this list.")
830 ;;; Cursor indications
832 (defun cua--update-indications ()
835 ((and cua--global-mark-active
836 (stringp cua-global-mark-cursor-color))
837 cua-global-mark-cursor-color)
838 ((and buffer-read-only
839 (stringp cua-read-only-cursor-color))
840 cua-read-only-cursor-color)
841 ((and (stringp cua-overwrite-cursor-color)
843 (and cua--rectangle (cua--rectangle-padding))))
844 cua-overwrite-cursor-color)
845 (t cua-normal-cursor-color))))
847 (not (equal cursor (frame-parameter nil 'cursor-color))))
848 (set-cursor-color cursor))
854 (defun cua--pre-command-handler ()
856 (let ((movement (or (memq this-command cua--standard-movement-commands)
857 (memq this-command cua-movement-commands))))
859 ;; Cancel prefix key timeout if user enters another key.
860 (when cua--prefix-override-timer
861 (if (timerp cua--prefix-override-timer)
862 (cancel-timer cua--prefix-override-timer))
863 (setq cua--prefix-override-timer nil))
865 ;; Handle shifted cursor keys and other movement commands.
866 ;; If region is not active, region is activated if key is shifted.
867 ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC).
868 ;; If rectangle is active, expand rectangle in specified direction and ignore the movement.
871 ((memq 'shift (event-modifiers (aref (this-single-command-raw-keys) 0)))
874 (setq cua--last-region-shifted t)
875 (setq cua--explicit-region-start nil))
876 ((or cua--explicit-region-start cua--rectangle)
878 (push-mark nil nil t)))
880 ;; If we set mark-active to nil here, the region highlight will not be
881 ;; removed by the direct_output_ commands.
882 (setq deactivate-mark t)))
884 ;; Handle delete-selection property on other commands
885 (if (and mark-active (not deactivate-mark))
886 (let* ((ds (or (get this-command 'delete-selection)
887 (get this-command 'pending-delete)))
898 'cua-delete-rectangle ;; replace?
899 'cua-replace-region))
902 'cua-delete-rectangle
903 'cua-delete-region)))))
905 (setq this-original-command this-command
908 ;; Detect extension of rectangles by mouse or other movement
909 (setq cua--buffer-and-point-before-command
910 (if cua--rectangle (cons (current-buffer) (point))))
914 ;;; Post-command hook
916 (defun cua--post-command-handler ()
919 (when cua--global-mark-active
920 (cua--global-mark-post-command))
921 (when (fboundp 'cua--rectangle-post-command)
922 (cua--rectangle-post-command))
923 (setq cua--buffer-and-point-before-command nil)
924 (if (or (not mark-active) deactivate-mark)
925 (setq cua--explicit-region-start nil))
930 (cua--rectangle (cua--rectangle-assert))
931 (mark-active (message "Mark=%d Point=%d Expl=%s"
932 (mark t) (point) cua--explicit-region-start))))
934 ;; Disable transient-mark-mode if rectangle active in current buffer.
935 (if (not (window-minibuffer-p (selected-window)))
936 (setq transient-mark-mode (and (not cua--rectangle)
937 (if cua-highlight-region-shift-only
938 (not cua--explicit-region-start)
940 (if cua-enable-cursor-indications
941 (cua--update-indications))
943 (cua--select-keymaps)
951 (defun cua--M/H-key (map key fct)
952 ;; bind H-KEY or M-KEY to FCT in MAP
953 (if (eq key 'space) (setq key ? ))
954 (unless (listp key) (setq key (list key)))
955 (define-key map (vector (cons (if cua-use-hyper-key 'hyper 'meta) key)) fct))
957 (defvar cua-global-keymap (make-sparse-keymap)
958 "Global keymap for cua-mode; users may add to this keymap.")
960 (defvar cua--cua-keys-keymap (make-sparse-keymap))
961 (defvar cua--prefix-override-keymap (make-sparse-keymap))
962 (defvar cua--prefix-repeat-keymap (make-sparse-keymap))
963 (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
964 (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
965 (defvar cua--region-keymap (make-sparse-keymap))
967 (defvar cua--ena-cua-keys-keymap nil)
968 (defvar cua--ena-prefix-override-keymap nil)
969 (defvar cua--ena-prefix-repeat-keymap nil)
970 (defvar cua--ena-region-keymap nil)
971 (defvar cua--ena-global-mark-keymap nil)
973 (defvar cua--keymap-alist
974 `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap)
975 (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap)
976 (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap)
977 (cua--ena-global-mark-keymap . ,cua--global-mark-keymap)
978 (cua--rectangle . ,cua--rectangle-keymap)
979 (cua--ena-region-keymap . ,cua--region-keymap)
980 (cua-mode . ,cua-global-keymap)))
982 (defun cua--select-keymaps ()
983 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
984 (setq cua--ena-region-keymap
985 (and mark-active (not deactivate-mark)))
986 (setq cua--ena-prefix-override-keymap
987 (and cua--ena-region-keymap
989 (not cua-inhibit-cua-keys)
990 (or (eq cua-enable-cua-keys t)
991 (not cua--explicit-region-start))
992 (not executing-kbd-macro)
993 (not cua--prefix-override-timer)))
994 (setq cua--ena-prefix-repeat-keymap
995 (and cua--ena-region-keymap
996 (timerp cua--prefix-override-timer)))
997 (setq cua--ena-cua-keys-keymap
998 (and cua-enable-cua-keys
999 (not cua-inhibit-cua-keys)
1000 (or (eq cua-enable-cua-keys t)
1001 cua--last-region-shifted)))
1002 (setq cua--ena-global-mark-keymap
1003 (and cua--global-mark-active
1004 (not (window-minibuffer-p)))))
1006 (defvar cua--keymaps-initalized nil)
1008 (defun cua--init-keymaps ()
1009 (unless (eq cua-use-hyper-key 'only)
1010 (define-key cua-global-keymap [(shift return)] 'cua-set-rectangle-mark))
1011 (when cua-use-hyper-key
1012 (cua--M/H-key cua-global-keymap 'space 'cua-set-rectangle-mark)
1013 (define-key cua-global-keymap [(hyper mouse-1)] 'cua-mouse-set-rectangle-mark))
1015 (define-key cua-global-keymap [(shift control ? )] 'cua-toggle-global-mark)
1017 ;; replace region with rectangle or element on kill ring
1018 (define-key cua-global-keymap [remap yank] 'cua-paste)
1019 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1020 ;; replace current yank with previous kill ring element
1021 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1023 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
1025 (define-key cua-global-keymap [remap undo] 'cua-undo)
1026 (define-key cua-global-keymap [remap advertised-undo] 'cua-undo)
1028 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
1029 (define-key cua--cua-keys-keymap [(shift control x)] 'Control-X-prefix)
1030 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
1031 (define-key cua--cua-keys-keymap [(shift control c)] 'mode-specific-command-prefix)
1032 (define-key cua--cua-keys-keymap [(control z)] 'undo)
1033 (define-key cua--cua-keys-keymap [(control v)] 'yank)
1034 (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1036 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1037 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
1039 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1040 (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler)
1041 (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler)
1042 (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler)
1043 (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler)
1044 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1045 (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler)
1046 (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler)
1047 (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler)
1048 (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler)
1050 ;; replace current region
1051 (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region)
1052 (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region)
1053 (define-key cua--region-keymap [remap insert-register] 'cua-replace-region)
1054 (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region)
1055 (define-key cua--region-keymap [remap newline] 'cua-replace-region)
1056 (define-key cua--region-keymap [remap open-line] 'cua-replace-region)
1057 ;; delete current region
1058 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1059 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1060 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1061 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1063 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1065 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1066 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1067 ;; cancel current region/rectangle
1068 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1069 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1074 (defun cua-mode (&optional arg)
1075 "Toggle CUA key-binding mode.
1076 When enabled, using shifted movement keys will activate the region (and
1077 highlight the region using `transient-mark-mode'), and typed text replaces
1078 the active selection. C-z, C-x, C-c, and C-v will undo, cut, copy, and
1079 paste (in addition to the normal emacs bindings)."
1083 ((null arg) (not cua-mode))
1085 (t (> (prefix-numeric-value arg) 0))))
1087 (setq mark-even-if-inactive t)
1088 (setq highlight-nonselected-windows nil)
1089 (make-variable-buffer-local 'cua--explicit-region-start)
1090 (make-variable-buffer-local 'cua--status-string)
1092 (unless cua--keymaps-initalized
1094 (setq cua--keymaps-initalized t))
1098 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1099 (add-hook 'post-command-hook 'cua--post-command-handler)
1100 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1101 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
1103 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1104 (remove-hook 'post-command-hook 'cua--post-command-handler))
1107 (setq emulation-mode-map-alists (delq 'cua--keymap-alist emulation-mode-map-alists))
1108 (add-to-list 'emulation-mode-map-alists 'cua--keymap-alist)
1109 (cua--select-keymaps))
1111 (if (fboundp 'cua--rectangle-on-off)
1112 (cua--rectangle-on-off cua-mode))
1113 (setq transient-mark-mode (and cua-mode
1114 (if cua-highlight-region-shift-only
1115 (not cua--explicit-region-start)
1119 "Toggle cua debugging."
1121 (setq cua--debug (not cua--debug)))
1123 ;;; cua-base.el ends here