]> code.delx.au - gnu-emacs/blob - lisp/emulation/cua-base.el
(cua-inhibit-cua-keys): New buffer-local variable.
[gnu-emacs] / lisp / emulation / cua-base.el
1 ;;; cua-base.el --- emulate CUA key bindings
2
3 ;; Copyright (C) 1997-2002 Free Software Foundation, Inc.
4
5 ;; Author: Kim F. Storm <storm@cua.dk>
6 ;; Keywords: keyboard emulation 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 2, or (at your option)
13 ;; 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; 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.
24
25
26 ;;; Commentary:
27
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 &
31 ;; extend the region.
32
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.
35 ;; C-z -> undo
36 ;; C-x -> cut
37 ;; C-c -> copy
38 ;; C-v -> paste
39 ;;
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!!!
43 ;;
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
46 ;; keys
47 ;; C-x -> cut
48 ;; C-c -> copy
49 ;; When the region is not active, C-x and C-c works as prefix keys!
50 ;;
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).
57 ;;
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
63 ;;
64 ;; This behaviour can be customized via the
65 ;; cua-prefix-override-inhibit-delay variable.
66
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].
70
71 ;; If you prefer to use the standard emacs cut, copy, paste, and undo
72 ;; bindings, customize cua-enable-cua-keys to nil.
73
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:
81 ;;
82 ;; (setq cua-normal-cursor-color "red")
83 ;; (setq cua-overwrite-cursor-color "yellow")
84 ;; (setq cua-read-only-cursor-color "green")
85 ;;
86
87 ;; CUA register support
88 ;; --------------------
89 ;; Emacs' standard register support is also based on a separate set of
90 ;; "register commands".
91 ;;
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].
96 ;;
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.
99 ;;
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
103 ;; using [M-0 C-v].
104
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.
111 ;;
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!
120 ;;
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.
125 ;;
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!
130 ;;
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.
136
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
142 ;; direction.
143 ;;
144 ;; [S-return] cancels the rectangle
145 ;; [C-space] activates the region bounded by the rectangle
146
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
156
157 ;; cua-mode's rectangle support also includes all the normal rectangle
158 ;; functions with easy access:
159 ;;
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
163 ;; of the rectangle
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.
187
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].
191
192 ;; CUA Global Mark
193 ;; ---------------
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
199 ;; mark is set:
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
208 ;; global mark.
209 ;; [backspace] deletes the character before the global mark, while
210 ;; [delete] deltes the character after the global mark.
211
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).
214
215 ;; [TAB] Indents the current line or rectangle to the column of the
216 ;; global mark.
217
218 ;;; Code:
219
220 ;;; Customization
221
222 (defgroup cua nil
223 "Emulate CUA key bindings including C-x and C-c."
224 :prefix "cua"
225 :group 'editing-basics
226 :group 'convenience
227 :group 'emulations
228 :link '(emacs-commentary-link :tag "Commentary" "cua-base.el")
229 :link '(emacs-library-link :tag "Lisp File" "cua-base.el"))
230
231 ;;;###autoload
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.
240
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)
247 :require 'cua-base
248 :link '(emacs-commentary-link "cua-base.el")
249 :version "21.4"
250 :type 'boolean
251 :group 'cua)
252
253
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
259 enabled."
260 :type '(choice (const :tag "Disabled" nil)
261 (const :tag "Shift region only" shift)
262 (other :tag "Enabled"))
263 :group 'cua)
264
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
270 is not turned on."
271 :type 'boolean
272 :group 'cua)
273
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))
286 :group 'cua)
287
288 (defcustom cua-keep-region-after-copy nil
289 "If non-nil, don't deselect the region after copying."
290 :type 'boolean
291 :group 'cua)
292
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"))
305 :group 'cua)
306
307 (defcustom cua-delete-copy-to-register-0 t
308 "*If non-nil, save last deleted region or rectangle to register 0."
309 :type 'boolean
310 :group 'cua)
311
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))
319 :group 'cua)
320
321 (defcustom cua-enable-region-auto-help nil
322 "*If non-nil, automatically show help for active region."
323 :type 'boolean
324 :group 'cua)
325
326 (defcustom cua-enable-modeline-indications nil
327 "*If non-nil, use minor-mode hook to show status in mode line."
328 :type 'boolean
329 :group 'cua)
330
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."
335 :type 'boolean
336 :group 'cua)
337
338
339 ;;; Rectangle Customization
340
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,"
346 :type 'boolean
347 :group 'cua)
348
349 (defcustom cua-enable-rectangle-auto-help t
350 "*If non-nil, automatically show help for region, rectangle and global mark."
351 :type 'boolean
352 :group 'cua)
353
354 (defface cua-rectangle-face 'nil
355 "*Font used by CUA for highlighting the rectangle."
356 :group 'cua)
357
358 (defface cua-rectangle-noselect-face 'nil
359 "*Font used by CUA for highlighting the non-selected rectangle lines."
360 :group 'cua)
361
362 (defcustom cua-undo-max 64
363 "*Max no of undoable CUA rectangle changes (including undo)."
364 :type 'integer
365 :group 'cua)
366
367
368 ;;; Global Mark Customization
369
370 (defcustom cua-global-mark-keep-visible t
371 "*If non-nil, always keep global mark visible in other window."
372 :type 'boolean
373 :group 'cua)
374
375 (defface cua-global-mark-face '((((class color))
376 (:foreground "black")
377 (:background "yellow"))
378 (t (:bold t)))
379 "*Font used by CUA for highlighting the global mark."
380 :group 'cua)
381
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))
386 :group 'cua)
387
388
389 ;;; Cursor Indication Customization
390
391 (defcustom cua-enable-cursor-indications t
392 "*If non-nil, use different cursor colors for indications."
393 :type 'boolean
394 :group 'cua)
395
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))))
410 :type 'color
411 :group 'cua)
412
413 (defcustom cua-read-only-cursor-color "darkgreen"
414 "*Cursor color used in read-only buffers, if non-nil."
415 :type 'color
416 :group 'cua)
417
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."
421 :type 'color
422 :group 'cua)
423
424 (defcustom cua-global-mark-cursor-color "cyan"
425 "*Indication for active global mark.
426 Will change cursor color to specified color if string."
427 :type 'color
428 :group 'cua)
429
430
431 ;;; Rectangle support is in cua-rect.el
432
433 (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
434
435 ;; Stub definitions until it is loaded
436
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))
442
443
444
445 ;;; Global Mark support is in cua-gmrk.el
446
447 (autoload 'cua-toggle-global-mark "cua-gmrk.el" nil t nil)
448
449 ;; Stub definitions until cua-gmrk.el is loaded
450
451 (when (not (featurep 'cua-gmrk))
452 (defvar cua--global-mark-active)
453 (setq cua--global-mark-active nil))
454
455
456 (provide 'cua-base)
457
458 (eval-when-compile
459 (require 'cua-rect)
460 (require 'cua-gmrk)
461 )
462
463
464 ;;; Low-level Interface
465
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)
469
470 ;;; Aux. variables
471
472 ;; Current region was started using cua-set-mark.
473 (defvar cua--explicit-region-start nil)
474
475 ;; Latest region was started using shifted movement command.
476 (defvar cua--last-region-shifted nil)
477
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)
481
482 ;; status string for mode line indications
483 (defvar cua--status-string nil)
484
485 (defvar cua--debug nil)
486
487
488 ;;; Prefix key override mechanism
489
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
494
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.
498
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].
503
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.
507
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].
511
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].
516
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.
520
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]!
525
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.
529
530 (defvar cua--prefix-override-timer nil)
531 (defvar cua--prefix-override-length nil)
532
533 (defun cua--prefix-override-replay (arg repeat)
534 (let* ((keys (this-command-keys))
535 (i (length keys))
536 (key (aref keys (1- i))))
537 (setq cua--prefix-override-length (- i repeat))
538 (setq cua--prefix-override-timer
539 (or
540 ;; In state [2], change to state [3]
541 (> repeat 0)
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))))
557
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."
561 (interactive "P")
562 (cua--prefix-override-replay arg 0))
563
564 (defun cua--prefix-repeat-handler (arg)
565 "Repeating prefix key when region is active works as a single prefix key."
566 (interactive "P")
567 (cua--prefix-override-replay arg 1))
568
569 (defun cua--prefix-copy-handler (arg)
570 "Copy region/rectangle, then replay last key."
571 (interactive "P")
572 (if cua--rectangle
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))))
578
579 (defun cua--prefix-cut-handler (arg)
580 "Cut region/rectangle, then replay last key."
581 (interactive "P")
582 (if cua--rectangle
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))))
588
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))
593 (if prefix-arg
594 (reset-this-command-lengths)
595 (setq overriding-terminal-local-map nil))
596 (cua--fix-keymaps nil)))
597
598
599 ;;; Aux. functions
600
601 (defun cua--fallback ()
602 ;; Execute original command
603 (setq this-command this-original-command)
604 (call-interactively this-command))
605
606 (defun cua--keep-active ()
607 (setq mark-active t
608 deactivate-mark nil))
609
610 (defun cua--deactivate (&optional now)
611 (setq cua--explicit-region-start nil)
612 (if (not now)
613 (setq deactivate-mark t)
614 (setq mark-active nil)
615 (run-hooks 'deactivate-mark-hook)))
616
617
618 ;; The current register prefix
619 (defvar cua--register nil)
620
621 (defun cua--prefix-arg (arg)
622 (setq cua--register
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))
629 (t t))
630 (integerp arg) (>= arg 0) (< arg 10)
631 (+ arg ?0)))
632 (if cua--register nil arg))
633
634
635 ;;; Enhanced undo - restore rectangle selections
636
637 (defun cua-undo (&optional arg)
638 "Undo some previous changes.
639 Knows about CUA rectangle highlighting in addition to standard undo."
640 (interactive "*P")
641 (if (fboundp 'cua--rectangle-undo)
642 (cua--rectangle-undo arg)
643 (undo arg)))
644
645 ;;; Region specific commands
646
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."
650 (interactive)
651 (let ((start (mark)) (end (point)))
652 (or (<= start end)
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)
657 (cua--deactivate)))
658
659 (defun cua-replace-region ()
660 "Replace the active region with the character you type."
661 (interactive)
662 (cua-delete-region)
663 (if (not (eq this-original-command this-command))
664 (cua--fallback)))
665
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."
669 (interactive "P")
670 (setq arg (cua--prefix-arg arg))
671 (setq cua--last-killed-rectangle nil)
672 (let ((start (mark)) (end (point)))
673 (or (<= start end)
674 (setq start (prog1 end (setq end start))))
675 (if cua--register
676 (copy-to-register cua--register start end nil)
677 (copy-region-as-kill start end))
678 (if cua-keep-region-after-copy
679 (cua--keep-active)
680 (cua--deactivate))))
681
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."
685 (interactive "P")
686 (setq cua--last-killed-rectangle nil)
687 (if buffer-read-only
688 (cua-copy-region arg)
689 (setq arg (cua--prefix-arg arg))
690 (let ((start (mark)) (end (point)))
691 (or (<= start end)
692 (setq start (prog1 end (setq end start))))
693 (if cua--register
694 (copy-to-register cua--register start end t)
695 (kill-region start end)))
696 (cua--deactivate)))
697
698 ;;; Generic commands for regions, rectangles, and global marks
699
700 (defun cua-cancel ()
701 "Cancel the active region, rectangle, or global mark."
702 (interactive)
703 (setq mark-active nil)
704 (setq cua--explicit-region-start nil)
705 (if (fboundp 'cua--cancel-rectangle)
706 (cua--cancel-rectangle)))
707
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."
713 (interactive "P")
714 (setq arg (cua--prefix-arg arg))
715 (let ((regtxt (and cua--register (get-register cua--register)))
716 (count (prefix-numeric-value arg)))
717 (cond
718 ((and cua--register (not regtxt))
719 (message "Nothing in register %c" cua--register))
720 (cua--global-mark-active
721 (if regtxt
722 (cua--insert-at-global-mark regtxt)
723 (when (not (eobp))
724 (cua--insert-at-global-mark (buffer-substring (point) (+ (point) count)))
725 (forward-char count))))
726 (buffer-read-only
727 (message "Cannot paste into a read-only buffer"))
728 (t
729 ;; Must save register here, since delete may override reg 0.
730 (if mark-active
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.
734 (if cua--rectangle
735 (cua--delete-rectangle)
736 (if (string= (buffer-substring (point) (mark))
737 (car kill-ring))
738 (current-kill 1))
739 (cua-delete-region)))
740 (cond
741 (regtxt
742 (cond
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)))
748 (let ((pt (point)))
749 (when (not (eq buffer-undo-list t))
750 (setq this-command 'cua--paste-rectangle)
751 (undo-boundary)
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))))
755 (t (yank arg)))))))
756
757 (defun cua-paste-pop (arg)
758 "Replace a just-pasted text or rectangle with a different text.
759 See `yank-pop' for details."
760 (interactive "P")
761 (if (eq last-command 'cua--paste-rectangle)
762 (progn
763 (undo)
764 (yank arg))
765 (yank-pop (prefix-numeric-value arg))))
766
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."
770 (interactive "P")
771 (if arg
772 (setq mark-active t)
773 (let (mark-active)
774 (exchange-point-and-mark)
775 (if cua--rectangle
776 (cua--rectangle-corner 0)))))
777
778 (defun cua-help-for-region (&optional help)
779 "Show region specific help in echo area."
780 (interactive)
781 (message
782 (concat (if help "C-?:help " "")
783 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
784
785
786 ;;; Shift activated / extended region
787
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."
795 (interactive "P")
796 (if (and (eq this-command last-command)
797 last-prefix-arg)
798 (setq arg (if arg nil last-prefix-arg)
799 current-prefix-arg arg))
800 (cond
801 (arg
802 (if (null (mark t))
803 (error "No mark set in this buffer")
804 (goto-char (mark t))
805 (pop-mark)))
806 (mark-active
807 (cua--deactivate)
808 (message "Mark Cleared"))
809 (t
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)))))
815
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'")
825
826 (defvar cua-movement-commands nil
827 "User may add additional movement commands to this list.")
828
829
830 ;;; Cursor indications
831
832 (defun cua--update-indications ()
833 (let ((cursor
834 (cond
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)
842 (or overwrite-mode
843 (and cua--rectangle (cua--rectangle-padding))))
844 cua-overwrite-cursor-color)
845 (t cua-normal-cursor-color))))
846 (if (and cursor
847 (not (equal cursor (frame-parameter nil 'cursor-color))))
848 (set-cursor-color cursor))
849 cursor))
850
851
852 ;;; Pre-command hook
853
854 (defun cua--pre-command-handler ()
855 (condition-case nil
856 (let ((movement (or (memq this-command cua--standard-movement-commands)
857 (memq this-command cua-movement-commands))))
858
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))
864
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.
869 (if movement
870 (cond
871 ((memq 'shift (event-modifiers (aref (this-single-command-raw-keys) 0)))
872 (unless mark-active
873 (push-mark nil t t))
874 (setq cua--last-region-shifted t)
875 (setq cua--explicit-region-start nil))
876 ((or cua--explicit-region-start cua--rectangle)
877 (unless mark-active
878 (push-mark nil nil t)))
879 (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)))
883
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)))
888 (nc (cond
889 ((not ds) nil)
890 ((eq ds 'yank)
891 'cua-paste)
892 ((eq ds 'kill)
893 (if cua--rectangle
894 'cua-copy-rectangle
895 'cua-copy-region))
896 ((eq ds 'supersede)
897 (if cua--rectangle
898 'cua-delete-rectangle ;; replace?
899 'cua-replace-region))
900 (t
901 (if cua--rectangle
902 'cua-delete-rectangle
903 'cua-delete-region)))))
904 (if nc
905 (setq this-original-command this-command
906 this-command nc)))))
907
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))))
911 )
912 (error nil)))
913
914 ;;; Post-command hook
915
916 (defun cua--post-command-handler ()
917 (condition-case nil
918 (progn
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))
926
927 ;; Debugging
928 (if cua--debug
929 (cond
930 (cua--rectangle (cua--rectangle-assert))
931 (mark-active (message "Mark=%d Point=%d Expl=%s"
932 (mark t) (point) cua--explicit-region-start))))
933
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)
939 t))))
940 (if cua-enable-cursor-indications
941 (cua--update-indications))
942
943 (cua--fix-keymaps nil)
944 )
945
946 (error nil)))
947
948
949 ;;; Keymaps
950
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))
956
957 (defvar cua-global-keymap (make-sparse-keymap))
958 (defvar cua--cua-keys-keymap (make-sparse-keymap))
959 (defvar cua--prefix-override-keymap (make-sparse-keymap))
960 (defvar cua--prefix-repeat-keymap (make-sparse-keymap))
961 (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
962 (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
963 (defvar cua--region-keymap (make-sparse-keymap))
964
965 (defvar cua--ena-cua-keys-keymap nil)
966 (defvar cua--ena-prefix-override-keymap nil)
967 (defvar cua--ena-prefix-repeat-keymap nil)
968 (defvar cua--ena-region-keymap nil)
969 (defvar cua--ena-global-mark-keymap nil)
970
971 (defvar cua--mmap-prefix-override-keymap (cons 'cua--ena-prefix-override-keymap cua--prefix-override-keymap))
972 (defvar cua--mmap-prefix-repeat-keymap (cons 'cua--ena-prefix-repeat-keymap cua--prefix-repeat-keymap))
973 (defvar cua--mmap-cua-keys-keymap (cons 'cua--ena-cua-keys-keymap cua--cua-keys-keymap))
974 (defvar cua--mmap-global-mark-keymap (cons 'cua--ena-global-mark-keymap cua--global-mark-keymap))
975 (defvar cua--mmap-rectangle-keymap (cons 'cua--rectangle cua--rectangle-keymap))
976 (defvar cua--mmap-region-keymap (cons 'cua--ena-region-keymap cua--region-keymap))
977 (defvar cua--mmap-global-keymap (cons 'cua-mode cua-global-keymap))
978
979 (defvar cua--mmap-list
980 (list cua--mmap-prefix-override-keymap
981 cua--mmap-prefix-repeat-keymap
982 cua--mmap-cua-keys-keymap
983 cua--mmap-global-mark-keymap
984 cua--mmap-rectangle-keymap
985 cua--mmap-region-keymap
986 cua--mmap-global-keymap))
987
988 (defun cua--fix-keymaps (disable)
989 ;; Ensure that cua's keymaps are in minor-mode-map-alist and
990 ;; in the correct order.
991 (let (fix
992 (mmap minor-mode-map-alist)
993 (ml cua--mmap-list))
994 (while (and (not fix) mmap ml)
995 (if (not (eq (car mmap) (car ml)))
996 (setq fix t)
997 (setq mmap (cdr mmap)
998 ml (cdr ml))))
999 (if ml
1000 (setq fix t))
1001 (when (or fix disable)
1002 (setq ml cua--mmap-list)
1003 (while ml
1004 (setq minor-mode-map-alist (delq (car ml) minor-mode-map-alist))
1005 (setq ml (cdr ml))))
1006 (when (and fix (not disable))
1007 (setq minor-mode-map-alist
1008 (append (copy-sequence cua--mmap-list) minor-mode-map-alist))))
1009 (setq cua--ena-region-keymap
1010 (and mark-active (not deactivate-mark)))
1011 (setq cua--ena-prefix-override-keymap
1012 (and cua--ena-region-keymap
1013 cua-enable-cua-keys
1014 (not cua-inhibit-cua-keys)
1015 (or (eq cua-enable-cua-keys t)
1016 (not cua--explicit-region-start))
1017 (not executing-kbd-macro)
1018 (not cua--prefix-override-timer)))
1019 (setq cua--ena-prefix-repeat-keymap
1020 (and cua--ena-region-keymap
1021 (timerp cua--prefix-override-timer)))
1022 (setq cua--ena-cua-keys-keymap
1023 (and cua-enable-cua-keys
1024 (not cua-inhibit-cua-keys)
1025 (or (eq cua-enable-cua-keys t)
1026 cua--last-region-shifted)))
1027 (setq cua--ena-global-mark-keymap
1028 (and cua--global-mark-active
1029 (not (window-minibuffer-p)))))
1030
1031 (defvar cua--keymaps-initalized nil)
1032
1033 (defun cua--init-keymaps ()
1034 (unless (eq cua-use-hyper-key 'only)
1035 (define-key cua-global-keymap [(shift return)] 'cua-set-rectangle-mark))
1036 (when cua-use-hyper-key
1037 (cua--M/H-key cua-global-keymap 'space 'cua-set-rectangle-mark)
1038 (define-key cua-global-keymap [(hyper mouse-1)] 'cua-mouse-set-rectangle-mark))
1039
1040 (define-key cua-global-keymap [(shift control ? )] 'cua-toggle-global-mark)
1041
1042 ;; replace region with rectangle or element on kill ring
1043 (define-key cua-global-keymap [remap yank] 'cua-paste)
1044 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1045 ;; replace current yank with previous kill ring element
1046 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1047 ;; set mark
1048 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
1049 ;; undo
1050 (define-key cua-global-keymap [remap undo] 'cua-undo)
1051 (define-key cua-global-keymap [remap advertised-undo] 'cua-undo)
1052
1053 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
1054 (define-key cua--cua-keys-keymap [(shift control x)] 'Control-X-prefix)
1055 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
1056 (define-key cua--cua-keys-keymap [(shift control c)] 'mode-specific-command-prefix)
1057 (define-key cua--cua-keys-keymap [(control z)] 'undo)
1058 (define-key cua--cua-keys-keymap [(control v)] 'yank)
1059 (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1060
1061 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1062 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
1063
1064 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1065 (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler)
1066 (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler)
1067 (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler)
1068 (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler)
1069 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1070 (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler)
1071 (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler)
1072 (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler)
1073 (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler)
1074
1075 ;; replace current region
1076 (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region)
1077 (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region)
1078 (define-key cua--region-keymap [remap insert-register] 'cua-replace-region)
1079 (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region)
1080 (define-key cua--region-keymap [remap newline] 'cua-replace-region)
1081 (define-key cua--region-keymap [remap open-line] 'cua-replace-region)
1082 ;; delete current region
1083 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1084 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1085 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1086 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1087 ;; kill region
1088 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1089 ;; copy region
1090 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1091 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1092 ;; cancel current region/rectangle
1093 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1094 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1095 )
1096
1097
1098 ;;;###autoload
1099 (defun cua-mode (&optional arg)
1100 "Toggle CUA key-binding mode.
1101 When enabled, using shifted movement keys will activate the region (and
1102 highlight the region using `transient-mark-mode'), and typed text replaces
1103 the active selection. C-z, C-x, C-c, and C-v will undo, cut, copy, and
1104 paste (in addition to the normal emacs bindings)."
1105 (interactive "P")
1106 (setq cua-mode
1107 (cond
1108 ((null arg) (not cua-mode))
1109 ((symbolp arg) t)
1110 (t (> (prefix-numeric-value arg) 0))))
1111
1112 (setq mark-even-if-inactive t)
1113 (setq highlight-nonselected-windows nil)
1114 (make-variable-buffer-local 'cua--explicit-region-start)
1115 (make-variable-buffer-local 'cua--status-string)
1116
1117 (unless cua--keymaps-initalized
1118 (cua--init-keymaps)
1119 (setq cua--keymaps-initalized t))
1120
1121 (if cua-mode
1122 (progn
1123 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1124 (add-hook 'post-command-hook 'cua--post-command-handler)
1125 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1126 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
1127 )
1128 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1129 (remove-hook 'post-command-hook 'cua--post-command-handler))
1130 (cua--fix-keymaps (not cua-mode))
1131 (if (fboundp 'cua--rectangle-on-off)
1132 (cua--rectangle-on-off cua-mode))
1133 (setq transient-mark-mode (and cua-mode
1134 (if cua-highlight-region-shift-only
1135 (not cua--explicit-region-start)
1136 t))))
1137
1138 (defun cua-debug ()
1139 "Toggle cua debugging."
1140 (interactive)
1141 (setq cua--debug (not cua--debug)))
1142
1143 ;;; cua-base.el ends here