]> code.delx.au - gnu-emacs/blob - lisp/emulation/cua-base.el
(viper-smart-suffix-list, viper-ask-level): Fix typos.
[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 ;; CUA style key bindings for cut and paste
34 ;; ----------------------------------------
35
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.
38 ;; C-z -> undo
39 ;; C-x -> cut
40 ;; C-c -> copy
41 ;; C-v -> paste
42 ;;
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!!!
46 ;;
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
49 ;; keys
50 ;; C-x -> cut
51 ;; C-c -> copy
52 ;; When the region is not active, C-x and C-c works as prefix keys!
53 ;;
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).
60 ;;
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
66 ;;
67 ;; This behaviour can be customized via the
68 ;; cua-prefix-override-inhibit-delay variable.
69
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].
73
74 ;; If you prefer to use the standard emacs cut, copy, paste, and undo
75 ;; bindings, customize cua-enable-cua-keys to nil.
76
77
78 ;; Typing text replaces the region
79 ;; -------------------------------
80
81 ;; When the region is active, i.e. highlighted, the text in region is
82 ;; replaced by the text you type.
83
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).
86
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.
93 ;;
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.
100
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.
104
105
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:
113 ;;
114 ;; (setq cua-normal-cursor-color "red")
115 ;; (setq cua-overwrite-cursor-color "yellow")
116 ;; (setq cua-read-only-cursor-color "green")
117 ;;
118
119 ;; CUA register support
120 ;; --------------------
121 ;; Emacs' standard register support is also based on a separate set of
122 ;; "register commands".
123 ;;
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].
128 ;;
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.
131 ;;
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
135 ;; using [M-0 C-v].
136
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.
143 ;;
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!
152 ;;
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.
157 ;;
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!
162 ;;
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.
168
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
174 ;; direction.
175 ;;
176 ;; [S-return] cancels the rectangle
177 ;; [C-space] activates the region bounded by the rectangle
178
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
188
189 ;; cua-mode's rectangle support also includes all the normal rectangle
190 ;; functions with easy access:
191 ;;
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
195 ;; of the rectangle
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.
219
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].
223
224 ;; CUA Global Mark
225 ;; ---------------
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
231 ;; mark is set:
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
240 ;; global mark.
241 ;; [backspace] deletes the character before the global mark, while
242 ;; [delete] deltes the character after the global mark.
243
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).
246
247 ;; [TAB] Indents the current line or rectangle to the column of the
248 ;; global mark.
249
250 ;;; Code:
251
252 ;;; Customization
253
254 (defgroup cua nil
255 "Emulate CUA key bindings including C-x and C-c."
256 :prefix "cua"
257 :group 'editing-basics
258 :group 'convenience
259 :group 'emulations
260 :link '(emacs-commentary-link :tag "Commentary" "cua-base.el")
261 :link '(emacs-library-link :tag "Lisp File" "cua-base.el"))
262
263 ;;;###autoload
264 (defcustom cua-mode nil
265 "Non-nil means that CUA emulation mode is enabled.
266 In CUA mode, shifted movement keys highlight and extend the region.
267 When a region is highlighted, the binding of the C-x and C-c keys are
268 temporarily changed to work as Motif, MAC or MS-Windows cut and paste.
269 Also, insertion commands first delete the region and then insert.
270 This mode enables Transient Mark mode and it provides a superset of the
271 PC Selection Mode and Delete Selection Modes.
272
273 Setting this variable directly does not take effect;
274 use either \\[customize] or the function `cua-mode'."
275 :set (lambda (symbol value)
276 (cua-mode (or value 0)))
277 :initialize 'custom-initialize-default
278 :set-after '(cua-enable-modeline-indications cua-use-hyper-key)
279 :require 'cua-base
280 :link '(emacs-commentary-link "cua-base.el")
281 :version "21.4"
282 :type 'boolean
283 :group 'cua)
284
285
286 (defcustom cua-enable-cua-keys t
287 "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste.
288 If the value is t, these mappings are always enabled. If the value is
289 'shift, these keys are only enabled if the last region was marked with
290 a shifted movement key. If the value is nil, these keys are never
291 enabled."
292 :type '(choice (const :tag "Disabled" nil)
293 (const :tag "Shift region only" shift)
294 (other :tag "Enabled" t))
295 :group 'cua)
296
297 (defcustom cua-highlight-region-shift-only nil
298 "*If non-nil, only highlight region if marked with S-<move>.
299 When this is non-nil, CUA toggles `transient-mark-mode' on when the region
300 is marked using shifted movement keys, and off when the mark is cleared.
301 But when the mark was set using \\[cua-set-mark], transient-mark-mode
302 is not turned on."
303 :type 'boolean
304 :group 'cua)
305
306 (defcustom cua-prefix-override-inhibit-delay
307 (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) nil)
308 "*If non-nil, time in seconds to delay before overriding prefix key.
309 If there is additional input within this time, the prefix key is
310 used as a normal prefix key. So typing a key sequence quickly will
311 inhibit overriding the prefix key.
312 As a special case, if the prefix keys repeated within this time, the
313 first prefix key is discarded, so typing a prefix key twice in quick
314 succession will also inhibit overriding the prefix key.
315 If the value is nil, use a shifted prefix key to inhibit the override."
316 :type '(choice (number :tag "Inhibit delay")
317 (const :tag "No delay" nil))
318 :group 'cua)
319
320 (defcustom cua-keep-region-after-copy nil
321 "If non-nil, don't deselect the region after copying."
322 :type 'boolean
323 :group 'cua)
324
325 (defcustom cua-enable-register-prefix 'not-ctrl-u
326 "*If non-nil, registers are supported via numeric prefix arg.
327 If the value is t, any numeric prefix arg in the range 0 to 9 will be
328 interpreted as a register number.
329 If the value is not-ctrl-u, using C-u to enter a numeric prefix is not
330 interpreted as a register number.
331 If the value is ctrl-u-only, only numeric prefix entered with C-u is
332 interpreted as a register number."
333 :type '(choice (const :tag "Disabled" nil)
334 (const :tag "Enabled, but C-u arg is not a register" not-ctrl-u)
335 (const :tag "Enabled, but only for C-u arg" ctrl-u-only)
336 (other :tag "Enabled" t))
337 :group 'cua)
338
339 (defcustom cua-delete-copy-to-register-0 t
340 "*If non-nil, save last deleted region or rectangle to register 0."
341 :type 'boolean
342 :group 'cua)
343
344 (defcustom cua-use-hyper-key nil
345 "*If non-nil, bind rectangle commands to H-? instead of M-?.
346 If set to 'also, toggle region command is also on S-return.
347 Must be set prior to enabling CUA."
348 :type '(choice (const :tag "Meta key and S-return" nil)
349 (const :tag "Hyper key only" only)
350 (const :tag "Hyper key and S-return" also))
351 :group 'cua)
352
353 (defcustom cua-enable-region-auto-help nil
354 "*If non-nil, automatically show help for active region."
355 :type 'boolean
356 :group 'cua)
357
358 (defcustom cua-enable-modeline-indications nil
359 "*If non-nil, use minor-mode hook to show status in mode line."
360 :type 'boolean
361 :group 'cua)
362
363 (defcustom cua-check-pending-input t
364 "*If non-nil, don't override prefix key if input pending.
365 It is rumoured that input-pending-p is unreliable under some window
366 managers, so try setting this to nil, if prefix override doesn't work."
367 :type 'boolean
368 :group 'cua)
369
370
371 ;;; Rectangle Customization
372
373 (defcustom cua-auto-expand-rectangles nil
374 "*If non-nil, rectangles are padded with spaces to make straight edges.
375 This implies modifying buffer contents by expanding tabs and inserting spaces.
376 Consequently, this is inhibited in read-only buffers.
377 Can be toggled by [M-p] while the rectangle is active,"
378 :type 'boolean
379 :group 'cua)
380
381 (defcustom cua-enable-rectangle-auto-help t
382 "*If non-nil, automatically show help for region, rectangle and global mark."
383 :type 'boolean
384 :group 'cua)
385
386 (defface cua-rectangle-face 'nil
387 "*Font used by CUA for highlighting the rectangle."
388 :group 'cua)
389
390 (defface cua-rectangle-noselect-face 'nil
391 "*Font used by CUA for highlighting the non-selected rectangle lines."
392 :group 'cua)
393
394 (defcustom cua-undo-max 64
395 "*Max no of undoable CUA rectangle changes (including undo)."
396 :type 'integer
397 :group 'cua)
398
399
400 ;;; Global Mark Customization
401
402 (defcustom cua-global-mark-keep-visible t
403 "*If non-nil, always keep global mark visible in other window."
404 :type 'boolean
405 :group 'cua)
406
407 (defface cua-global-mark-face '((((class color))
408 (:foreground "black")
409 (:background "yellow"))
410 (t (:bold t)))
411 "*Font used by CUA for highlighting the global mark."
412 :group 'cua)
413
414 (defcustom cua-global-mark-blink-cursor-interval 0.20
415 "*Blink cursor at this interval when global mark is active."
416 :type '(choice (number :tag "Blink interval")
417 (const :tag "No blink" nil))
418 :group 'cua)
419
420
421 ;;; Cursor Indication Customization
422
423 (defcustom cua-enable-cursor-indications nil
424 "*If non-nil, use different cursor colors for indications."
425 :type 'boolean
426 :group 'cua)
427
428 (defcustom cua-normal-cursor-color nil
429 "Normal (non-overwrite) cursor color.
430 Also used to indicate that rectangle padding is not in effect.
431 Automatically loaded from frame parameters, if nil."
432 :initialize (lambda (symbol value)
433 (set symbol (or value
434 (and (boundp 'initial-cursor-color) initial-cursor-color)
435 (and (boundp 'initial-frame-alist)
436 (assoc 'cursor-color initial-frame-alist)
437 (cdr (assoc 'cursor-color initial-frame-alist)))
438 (and (boundp 'default-frame-alist)
439 (assoc 'cursor-color default-frame-alist)
440 (cdr (assoc 'cursor-color default-frame-alist)))
441 (frame-parameter nil 'cursor-color))))
442 :type 'color
443 :group 'cua)
444
445 (defcustom cua-read-only-cursor-color "darkgreen"
446 "*Cursor color used in read-only buffers, if non-nil."
447 :type 'color
448 :group 'cua)
449
450 (defcustom cua-overwrite-cursor-color "yellow"
451 "*Cursor color used when overwrite mode is set, if non-nil.
452 Also used to indicate that rectangle padding is in effect."
453 :type 'color
454 :group 'cua)
455
456 (defcustom cua-global-mark-cursor-color "cyan"
457 "*Indication for active global mark.
458 Will change cursor color to specified color if string."
459 :type 'color
460 :group 'cua)
461
462
463 ;;; Rectangle support is in cua-rect.el
464
465 (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
466
467 ;; Stub definitions until it is loaded
468
469 (when (not (featurep 'cua-rect))
470 (defvar cua--rectangle)
471 (setq cua--rectangle nil)
472 (defvar cua--last-killed-rectangle)
473 (setq cua--last-killed-rectangle nil))
474
475
476
477 ;;; Global Mark support is in cua-gmrk.el
478
479 (autoload 'cua-toggle-global-mark "cua-gmrk.el" nil t nil)
480
481 ;; Stub definitions until cua-gmrk.el is loaded
482
483 (when (not (featurep 'cua-gmrk))
484 (defvar cua--global-mark-active)
485 (setq cua--global-mark-active nil))
486
487
488 (provide 'cua-base)
489
490 (eval-when-compile
491 (require 'cua-rect)
492 (require 'cua-gmrk)
493 )
494
495
496 ;;; Low-level Interface
497
498 (defvar cua-inhibit-cua-keys nil
499 "Buffer-local variable that may disable the cua keymappings.")
500 (make-variable-buffer-local 'cua-inhibit-cua-keys)
501
502 ;;; Aux. variables
503
504 ;; Current region was started using cua-set-mark.
505 (defvar cua--explicit-region-start nil)
506
507 ;; Latest region was started using shifted movement command.
508 (defvar cua--last-region-shifted nil)
509
510 ;; buffer + point prior to current command when rectangle is active
511 ;; checked in post-command hook to see if point was moved
512 (defvar cua--buffer-and-point-before-command nil)
513
514 ;; status string for mode line indications
515 (defvar cua--status-string nil)
516
517 (defvar cua--debug nil)
518
519
520 ;;; Prefix key override mechanism
521
522 ;; The prefix override (when mark-active) operates in three substates:
523 ;; [1] Before using a prefix key
524 ;; [2] Immediately after using a prefix key
525 ;; [3] A fraction of a second later
526
527 ;; In state [1], the cua--prefix-override-keymap is active.
528 ;; This keymap binds the C-x and C-c prefix keys to the
529 ;; cua--prefix-override-handler function.
530
531 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
532 ;; will push back the keys already read to the event queue. If input is
533 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
534 ;; is started, and it changes to state [2].
535
536 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
537 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
538 ;; C-x to the cua--prefix-repeat-handler function.
539
540 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
541 ;; will cancel [T], back the keys already read (except for the second prefix
542 ;; keys) to the event queue, and changes to state [3].
543
544 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
545 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
546 ;; the cua--prefix-override-timeout function will push a `timeout' event on
547 ;; the event queue, and changes to state [3].
548
549 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
550 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
551 ;; normal prefix key binding from the global or local map will be used.
552
553 ;; The pre-command hook (executed as a consequence of the timeout or normal
554 ;; prefix key binding) will cancel [T] and change from state [3] back to
555 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
556 ;; are always called with state reset to [1]!
557
558 ;; State [1] is recognized by cua--prefix-override-timer is nil,
559 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
560 ;; state [3] is recognized by cua--prefix-override-timer is t.
561
562 (defvar cua--prefix-override-timer nil)
563 (defvar cua--prefix-override-length nil)
564
565 (defun cua--prefix-override-replay (arg repeat)
566 (let* ((keys (this-command-keys))
567 (i (length keys))
568 (key (aref keys (1- i))))
569 (setq cua--prefix-override-length (- i repeat))
570 (setq cua--prefix-override-timer
571 (or
572 ;; In state [2], change to state [3]
573 (> repeat 0)
574 ;; In state [1], change directly to state [3]
575 (and cua-check-pending-input (input-pending-p))
576 ;; In state [1], [T] disabled, so change to state [3]
577 (not (numberp cua-prefix-override-inhibit-delay))
578 (<= cua-prefix-override-inhibit-delay 0)
579 ;; In state [1], start [T] and change to state [2]
580 (run-with-timer cua-prefix-override-inhibit-delay nil
581 'cua--prefix-override-timeout)))
582 ;; Don't record this command
583 (setq this-command last-command)
584 ;; Restore the prefix arg
585 (setq prefix-arg arg)
586 (reset-this-command-lengths)
587 ;; Push the key back on the event queue
588 (setq unread-command-events (cons key unread-command-events))))
589
590 (defun cua--prefix-override-handler (arg)
591 "Start timer waiting for prefix key to be followed by another key.
592 Repeating prefix key when region is active works as a single prefix key."
593 (interactive "P")
594 (cua--prefix-override-replay arg 0))
595
596 (defun cua--prefix-repeat-handler (arg)
597 "Repeating prefix key when region is active works as a single prefix key."
598 (interactive "P")
599 (cua--prefix-override-replay arg 1))
600
601 (defun cua--prefix-copy-handler (arg)
602 "Copy region/rectangle, then replay last key."
603 (interactive "P")
604 (if cua--rectangle
605 (cua-copy-rectangle arg)
606 (cua-copy-region arg))
607 (let ((keys (this-single-command-keys)))
608 (setq unread-command-events
609 (cons (aref keys (1- (length keys))) unread-command-events))))
610
611 (defun cua--prefix-cut-handler (arg)
612 "Cut region/rectangle, then replay last key."
613 (interactive "P")
614 (if cua--rectangle
615 (cua-cut-rectangle arg)
616 (cua-cut-region arg))
617 (let ((keys (this-single-command-keys)))
618 (setq unread-command-events
619 (cons (aref keys (1- (length keys))) unread-command-events))))
620
621 (defun cua--prefix-override-timeout ()
622 (setq cua--prefix-override-timer t)
623 (when (= (length (this-command-keys)) cua--prefix-override-length)
624 (setq unread-command-events (cons 'timeout unread-command-events))
625 (if prefix-arg
626 (reset-this-command-lengths)
627 (setq overriding-terminal-local-map nil))
628 (cua--select-keymaps)))
629
630
631 ;;; Aux. functions
632
633 (defun cua--fallback ()
634 ;; Execute original command
635 (setq this-command this-original-command)
636 (call-interactively this-command))
637
638 (defun cua--keep-active ()
639 (setq mark-active t
640 deactivate-mark nil))
641
642 (defun cua--deactivate (&optional now)
643 (setq cua--explicit-region-start nil)
644 (if (not now)
645 (setq deactivate-mark t)
646 (setq mark-active nil)
647 (run-hooks 'deactivate-mark-hook)))
648
649
650 ;; The current register prefix
651 (defvar cua--register nil)
652
653 (defun cua--prefix-arg (arg)
654 (setq cua--register
655 (and cua-enable-register-prefix
656 (integerp (this-command-keys))
657 (cond ((eq cua-enable-register-prefix 'not-ctrl-u)
658 (not (= (aref (this-command-keys) 0) ?\C-u)))
659 ((eq cua-enable-register-prefix 'ctrl-u-only)
660 (= (aref (this-command-keys) 0) ?\C-u))
661 (t t))
662 (integerp arg) (>= arg 0) (< arg 10)
663 (+ arg ?0)))
664 (if cua--register nil arg))
665
666
667 ;;; Enhanced undo - restore rectangle selections
668
669 (defun cua-undo (&optional arg)
670 "Undo some previous changes.
671 Knows about CUA rectangle highlighting in addition to standard undo."
672 (interactive "*P")
673 (if (fboundp 'cua--rectangle-undo)
674 (cua--rectangle-undo arg)
675 (undo arg)))
676
677 ;;; Region specific commands
678
679 (defun cua-delete-region ()
680 "Delete the active region.
681 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
682 (interactive)
683 (let ((start (mark)) (end (point)))
684 (or (<= start end)
685 (setq start (prog1 end (setq end start))))
686 (if cua-delete-copy-to-register-0
687 (copy-to-register ?0 start end nil))
688 (delete-region start end)
689 (cua--deactivate)))
690
691 (defun cua-replace-region ()
692 "Replace the active region with the character you type."
693 (interactive)
694 (cua-delete-region)
695 (if (not (eq this-original-command this-command))
696 (cua--fallback)))
697
698 (defun cua-copy-region (arg)
699 "Copy the region to the kill ring.
700 With numeric prefix arg, copy to register 0-9 instead."
701 (interactive "P")
702 (setq arg (cua--prefix-arg arg))
703 (setq cua--last-killed-rectangle nil)
704 (let ((start (mark)) (end (point)))
705 (or (<= start end)
706 (setq start (prog1 end (setq end start))))
707 (if cua--register
708 (copy-to-register cua--register start end nil)
709 (copy-region-as-kill start end))
710 (if cua-keep-region-after-copy
711 (cua--keep-active)
712 (cua--deactivate))))
713
714 (defun cua-cut-region (arg)
715 "Cut the region and copy to the kill ring.
716 With numeric prefix arg, copy to register 0-9 instead."
717 (interactive "P")
718 (setq cua--last-killed-rectangle nil)
719 (if buffer-read-only
720 (cua-copy-region arg)
721 (setq arg (cua--prefix-arg arg))
722 (let ((start (mark)) (end (point)))
723 (or (<= start end)
724 (setq start (prog1 end (setq end start))))
725 (if cua--register
726 (copy-to-register cua--register start end t)
727 (kill-region start end)))
728 (cua--deactivate)))
729
730 ;;; Generic commands for regions, rectangles, and global marks
731
732 (defun cua-cancel ()
733 "Cancel the active region, rectangle, or global mark."
734 (interactive)
735 (setq mark-active nil)
736 (setq cua--explicit-region-start nil)
737 (if (fboundp 'cua--cancel-rectangle)
738 (cua--cancel-rectangle)))
739
740 (defun cua-paste (arg)
741 "Paste last cut or copied region or rectangle.
742 An active region is deleted before executing the command.
743 With numeric prefix arg, paste from register 0-9 instead.
744 If global mark is active, copy from register or one character."
745 (interactive "P")
746 (setq arg (cua--prefix-arg arg))
747 (let ((regtxt (and cua--register (get-register cua--register)))
748 (count (prefix-numeric-value arg)))
749 (cond
750 ((and cua--register (not regtxt))
751 (message "Nothing in register %c" cua--register))
752 (cua--global-mark-active
753 (if regtxt
754 (cua--insert-at-global-mark regtxt)
755 (when (not (eobp))
756 (cua--insert-at-global-mark (buffer-substring (point) (+ (point) count)))
757 (forward-char count))))
758 (buffer-read-only
759 (message "Cannot paste into a read-only buffer"))
760 (t
761 ;; Must save register here, since delete may override reg 0.
762 (if mark-active
763 ;; Before a yank command, make sure we don't yank
764 ;; the same region that we are going to delete.
765 ;; That would make yank a no-op.
766 (if cua--rectangle
767 (cua--delete-rectangle)
768 (if (string= (buffer-substring (point) (mark))
769 (car kill-ring))
770 (current-kill 1))
771 (cua-delete-region)))
772 (cond
773 (regtxt
774 (cond
775 ((consp regtxt) (cua--insert-rectangle regtxt))
776 ((stringp regtxt) (insert-for-yank regtxt))
777 (t (message "Unknown data in register %c" cua--register))))
778 ((and cua--last-killed-rectangle
779 (eq (and kill-ring (car kill-ring)) (car cua--last-killed-rectangle)))
780 (let ((pt (point)))
781 (when (not (eq buffer-undo-list t))
782 (setq this-command 'cua--paste-rectangle)
783 (undo-boundary)
784 (setq buffer-undo-list (cons pt buffer-undo-list)))
785 (cua--insert-rectangle (cdr cua--last-killed-rectangle))
786 (if arg (goto-char pt))))
787 (t (yank arg)))))))
788
789 (defun cua-paste-pop (arg)
790 "Replace a just-pasted text or rectangle with a different text.
791 See `yank-pop' for details."
792 (interactive "P")
793 (if (eq last-command 'cua--paste-rectangle)
794 (progn
795 (undo)
796 (yank arg))
797 (yank-pop (prefix-numeric-value arg))))
798
799 (defun cua-exchange-point-and-mark (arg)
800 "Exchanges point and mark, but don't activate the mark.
801 Activates the mark if a prefix argument is given."
802 (interactive "P")
803 (if arg
804 (setq mark-active t)
805 (let (mark-active)
806 (exchange-point-and-mark)
807 (if cua--rectangle
808 (cua--rectangle-corner 0)))))
809
810 ;; Typed text that replaced the highlighted region.
811 (defvar cua--repeat-replace-text nil)
812
813 (defun cua-repeat-replace-region (arg)
814 "Repeat replacing text of highlighted region with typed text.
815 Searches for the next streach of text identical to the region last
816 replaced by typing text over it and replaces it with the same streach
817 of text.
818 Note: Works only when used immediately after typing the last character.
819 After that, it can be repeated (fairly) reliable until a buffer is
820 modified in any other way than repeating this command."
821 (interactive "P")
822 (unless (or (eq this-command last-command)
823 (not cua--repeat-replace-text)
824 (not (eq last-command 'self-insert-command)))
825 (setq cua--repeat-replace-text
826 (and (mark t)
827 (/= (point) (mark t))
828 (buffer-substring-no-properties (point) (mark t)))))
829 (let ((old (get-register ?0)))
830 (if (and old
831 cua--repeat-replace-text
832 (search-forward old nil t nil))
833 (replace-match cua--repeat-replace-text arg t))))
834
835 (defun cua-help-for-region (&optional help)
836 "Show region specific help in echo area."
837 (interactive)
838 (message
839 (concat (if help "C-?:help " "")
840 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
841
842
843 ;;; Shift activated / extended region
844
845 (defun cua-set-mark (&optional arg)
846 "Set mark at where point is, clear mark, or jump to mark.
847 With no prefix argument, set mark, push old mark position on local mark
848 ring, and push mark on global mark ring, or if mark is already set, clear mark.
849 With argument, jump to mark, and pop a new position for mark off the ring;
850 then it jumps to the next mark off the ring if repeated with no argument, or
851 sets the mark at the new position if repeated with argument."
852 (interactive "P")
853 (cond
854 ((eq last-command 'pop-to-mark-command)
855 (if (and (consp arg) (> (prefix-numeric-value arg) 4))
856 (push-mark-command nil)
857 (setq this-command 'pop-to-mark-command)
858 (pop-to-mark-command)))
859 (arg
860 (setq this-command 'pop-to-mark-command)
861 (pop-to-mark-command))
862 (mark-active
863 (cua--deactivate)
864 (message "Mark Cleared"))
865 (t
866 (push-mark-command nil nil)
867 (setq cua--explicit-region-start t)
868 (setq cua--last-region-shifted nil)
869 (if cua-enable-region-auto-help
870 (cua-help-for-region t)))))
871
872 (defvar cua--standard-movement-commands
873 '(forward-char backward-char
874 next-line previous-line
875 forward-word backward-word
876 end-of-line beginning-of-line
877 end-of-buffer beginning-of-buffer
878 scroll-up scroll-down forward-paragraph backward-paragraph)
879 "List of standard movement commands.
880 Extra commands should be added to `cua-user-movement-commands'")
881
882 (defvar cua-movement-commands nil
883 "User may add additional movement commands to this list.")
884
885
886 ;;; Cursor indications
887
888 (defun cua--update-indications ()
889 (let ((cursor
890 (cond
891 ((and cua--global-mark-active
892 (stringp cua-global-mark-cursor-color))
893 cua-global-mark-cursor-color)
894 ((and buffer-read-only
895 (stringp cua-read-only-cursor-color))
896 cua-read-only-cursor-color)
897 ((and (stringp cua-overwrite-cursor-color)
898 (or overwrite-mode
899 (and cua--rectangle (cua--rectangle-padding))))
900 cua-overwrite-cursor-color)
901 (t cua-normal-cursor-color))))
902 (if (and cursor
903 (not (equal cursor (frame-parameter nil 'cursor-color))))
904 (set-cursor-color cursor))
905 cursor))
906
907
908 ;;; Pre-command hook
909
910 (defun cua--pre-command-handler ()
911 (condition-case nil
912 (let ((movement (or (memq this-command cua--standard-movement-commands)
913 (memq this-command cua-movement-commands))))
914
915 ;; Cancel prefix key timeout if user enters another key.
916 (when cua--prefix-override-timer
917 (if (timerp cua--prefix-override-timer)
918 (cancel-timer cua--prefix-override-timer))
919 (setq cua--prefix-override-timer nil))
920
921 ;; Handle shifted cursor keys and other movement commands.
922 ;; If region is not active, region is activated if key is shifted.
923 ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC).
924 ;; If rectangle is active, expand rectangle in specified direction and ignore the movement.
925 (if movement
926 (cond
927 ((memq 'shift (event-modifiers (aref (this-single-command-raw-keys) 0)))
928 (unless mark-active
929 (push-mark-command nil t))
930 (setq cua--last-region-shifted t)
931 (setq cua--explicit-region-start nil))
932 ((or cua--explicit-region-start cua--rectangle)
933 (unless mark-active
934 (push-mark-command nil nil)))
935 (t
936 ;; If we set mark-active to nil here, the region highlight will not be
937 ;; removed by the direct_output_ commands.
938 (setq deactivate-mark t)))
939
940 ;; Handle delete-selection property on other commands
941 (if (and mark-active (not deactivate-mark))
942 (let* ((ds (or (get this-command 'delete-selection)
943 (get this-command 'pending-delete)))
944 (nc (cond
945 ((not ds) nil)
946 ((eq ds 'yank)
947 'cua-paste)
948 ((eq ds 'kill)
949 (if cua--rectangle
950 'cua-copy-rectangle
951 'cua-copy-region))
952 ((eq ds 'supersede)
953 (if cua--rectangle
954 'cua-delete-rectangle ;; replace?
955 'cua-replace-region))
956 (t
957 (if cua--rectangle
958 'cua-delete-rectangle
959 'cua-delete-region)))))
960 (if nc
961 (setq this-original-command this-command
962 this-command nc)))))
963
964 ;; Detect extension of rectangles by mouse or other movement
965 (setq cua--buffer-and-point-before-command
966 (if cua--rectangle (cons (current-buffer) (point))))
967 )
968 (error nil)))
969
970 ;;; Post-command hook
971
972 (defun cua--post-command-handler ()
973 (condition-case nil
974 (progn
975 (when cua--global-mark-active
976 (cua--global-mark-post-command))
977 (when (fboundp 'cua--rectangle-post-command)
978 (cua--rectangle-post-command))
979 (setq cua--buffer-and-point-before-command nil)
980 (if (or (not mark-active) deactivate-mark)
981 (setq cua--explicit-region-start nil))
982
983 ;; Debugging
984 (if cua--debug
985 (cond
986 (cua--rectangle (cua--rectangle-assert))
987 (mark-active (message "Mark=%d Point=%d Expl=%s"
988 (mark t) (point) cua--explicit-region-start))))
989
990 ;; Disable transient-mark-mode if rectangle active in current buffer.
991 (if (not (window-minibuffer-p (selected-window)))
992 (setq transient-mark-mode (and (not cua--rectangle)
993 (if cua-highlight-region-shift-only
994 (not cua--explicit-region-start)
995 t))))
996 (if cua-enable-cursor-indications
997 (cua--update-indications))
998
999 (cua--select-keymaps)
1000 )
1001
1002 (error nil)))
1003
1004
1005 ;;; Keymaps
1006
1007 (defun cua--M/H-key (map key fct)
1008 ;; bind H-KEY or M-KEY to FCT in MAP
1009 (if (eq key 'space) (setq key ? ))
1010 (unless (listp key) (setq key (list key)))
1011 (define-key map (vector (cons (if cua-use-hyper-key 'hyper 'meta) key)) fct))
1012
1013 (defun cua--self-insert-char-p (def)
1014 ;; Return DEF if current key sequence is self-inserting in
1015 ;; global-map.
1016 (if (memq (global-key-binding (this-single-command-keys))
1017 '(self-insert-command self-insert-iso))
1018 def nil))
1019
1020 (defvar cua-global-keymap (make-sparse-keymap)
1021 "Global keymap for cua-mode; users may add to this keymap.")
1022
1023 (defvar cua--cua-keys-keymap (make-sparse-keymap))
1024 (defvar cua--prefix-override-keymap (make-sparse-keymap))
1025 (defvar cua--prefix-repeat-keymap (make-sparse-keymap))
1026 (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
1027 (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
1028 (defvar cua--region-keymap (make-sparse-keymap))
1029
1030 (defvar cua--ena-cua-keys-keymap nil)
1031 (defvar cua--ena-prefix-override-keymap nil)
1032 (defvar cua--ena-prefix-repeat-keymap nil)
1033 (defvar cua--ena-region-keymap nil)
1034 (defvar cua--ena-global-mark-keymap nil)
1035
1036 (defvar cua--keymap-alist
1037 `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap)
1038 (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap)
1039 (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap)
1040 (cua--ena-global-mark-keymap . ,cua--global-mark-keymap)
1041 (cua--rectangle . ,cua--rectangle-keymap)
1042 (cua--ena-region-keymap . ,cua--region-keymap)
1043 (cua-mode . ,cua-global-keymap)))
1044
1045 (defun cua--select-keymaps ()
1046 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
1047 (setq cua--ena-region-keymap
1048 (and mark-active (not deactivate-mark)))
1049 (setq cua--ena-prefix-override-keymap
1050 (and cua--ena-region-keymap
1051 cua-enable-cua-keys
1052 (not cua-inhibit-cua-keys)
1053 (or (eq cua-enable-cua-keys t)
1054 (not cua--explicit-region-start))
1055 (not executing-kbd-macro)
1056 (not cua--prefix-override-timer)))
1057 (setq cua--ena-prefix-repeat-keymap
1058 (and cua--ena-region-keymap
1059 (timerp cua--prefix-override-timer)))
1060 (setq cua--ena-cua-keys-keymap
1061 (and cua-enable-cua-keys
1062 (not cua-inhibit-cua-keys)
1063 (or (eq cua-enable-cua-keys t)
1064 cua--last-region-shifted)))
1065 (setq cua--ena-global-mark-keymap
1066 (and cua--global-mark-active
1067 (not (window-minibuffer-p)))))
1068
1069 (defvar cua--keymaps-initalized nil)
1070
1071 (defun cua--init-keymaps ()
1072 (unless (eq cua-use-hyper-key 'only)
1073 (define-key cua-global-keymap [(shift return)] 'cua-set-rectangle-mark))
1074 (when cua-use-hyper-key
1075 (cua--M/H-key cua-global-keymap 'space 'cua-set-rectangle-mark)
1076 (define-key cua-global-keymap [(hyper mouse-1)] 'cua-mouse-set-rectangle-mark))
1077
1078 (define-key cua-global-keymap [(shift control ? )] 'cua-toggle-global-mark)
1079
1080 ;; replace region with rectangle or element on kill ring
1081 (define-key cua-global-keymap [remap yank] 'cua-paste)
1082 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1083 ;; replace current yank with previous kill ring element
1084 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1085 ;; set mark
1086 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
1087 ;; undo
1088 (define-key cua-global-keymap [remap undo] 'cua-undo)
1089 (define-key cua-global-keymap [remap advertised-undo] 'cua-undo)
1090
1091 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
1092 (define-key cua--cua-keys-keymap [(shift control x)] 'Control-X-prefix)
1093 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
1094 (define-key cua--cua-keys-keymap [(shift control c)] 'mode-specific-command-prefix)
1095 (define-key cua--cua-keys-keymap [(control z)] 'undo)
1096 (define-key cua--cua-keys-keymap [(control v)] 'yank)
1097 (define-key cua--cua-keys-keymap [(meta v)] 'cua-repeat-replace-region)
1098 (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1099
1100 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1101 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
1102
1103 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1104 (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler)
1105 (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler)
1106 (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler)
1107 (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler)
1108 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1109 (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler)
1110 (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler)
1111 (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler)
1112 (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler)
1113
1114 ;; replace current region
1115 (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region)
1116 (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region)
1117 (define-key cua--region-keymap [remap insert-register] 'cua-replace-region)
1118 (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region)
1119 (define-key cua--region-keymap [remap newline] 'cua-replace-region)
1120 (define-key cua--region-keymap [remap open-line] 'cua-replace-region)
1121 ;; delete current region
1122 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1123 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1124 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1125 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1126 ;; kill region
1127 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1128 ;; copy region
1129 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1130 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1131 ;; cancel current region/rectangle
1132 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1133 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1134 )
1135
1136 ;; State prior to enabling cua-mode
1137 ;; Value is a list with the following elements:
1138 ;; transient-mark-mode
1139 ;; delete-selection-mode
1140 ;; pc-selection-mode
1141
1142 (defvar cua--saved-state nil)
1143
1144 ;;;###autoload
1145 (defun cua-mode (&optional arg)
1146 "Toggle CUA key-binding mode.
1147 When enabled, using shifted movement keys will activate the region (and
1148 highlight the region using `transient-mark-mode'), and typed text replaces
1149 the active selection. C-z, C-x, C-c, and C-v will undo, cut, copy, and
1150 paste (in addition to the normal emacs bindings)."
1151 (interactive "P")
1152 (setq cua-mode
1153 (cond
1154 ((null arg) (not cua-mode))
1155 ((symbolp arg) t)
1156 (t (> (prefix-numeric-value arg) 0))))
1157
1158 (setq mark-even-if-inactive t)
1159 (setq highlight-nonselected-windows nil)
1160 (make-variable-buffer-local 'cua--explicit-region-start)
1161 (make-variable-buffer-local 'cua--status-string)
1162
1163 (unless cua--keymaps-initalized
1164 (cua--init-keymaps)
1165 (setq cua--keymaps-initalized t))
1166
1167 (if cua-mode
1168 (progn
1169 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1170 (add-hook 'post-command-hook 'cua--post-command-handler)
1171 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1172 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
1173 )
1174 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1175 (remove-hook 'post-command-hook 'cua--post-command-handler))
1176
1177 (if (not cua-mode)
1178 (setq emulation-mode-map-alists (delq 'cua--keymap-alist emulation-mode-map-alists))
1179 (add-to-list 'emulation-mode-map-alists 'cua--keymap-alist)
1180 (cua--select-keymaps))
1181
1182 (if (fboundp 'cua--rectangle-on-off)
1183 (cua--rectangle-on-off cua-mode))
1184
1185 (cond
1186 (cua-mode
1187 (setq cua--saved-state
1188 (list
1189 transient-mark-mode
1190 (and (boundp 'delete-selection-mode) delete-selection-mode)
1191 (and (boundp 'pc-selection-mode) pc-selection-mode)))
1192 (if (and (boundp 'delete-selection-mode) delete-selection-mode)
1193 (delete-selection-mode))
1194 (if (and (boundp 'pc-selection-mode) pc-selection-mode)
1195 (pc-selection-mode))
1196 (setq transient-mark-mode (and cua-mode
1197 (if cua-highlight-region-shift-only
1198 (not cua--explicit-region-start)
1199 t)))
1200 (if (interactive-p)
1201 (message "CUA mode enabled")))
1202 (cua--saved-state
1203 (setq transient-mark-mode (car cua--saved-state))
1204 (if (nth 1 cua--saved-state)
1205 (delete-selection-mode 1))
1206 (if (nth 2 cua--saved-state)
1207 (pc-selection-mode 1))
1208 (if (interactive-p)
1209 (message "CUA mode disabled.%s%s%s%s"
1210 (if (nth 1 cua--saved-state) " Delete-Selection" "")
1211 (if (and (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " and" "")
1212 (if (nth 2 cua--saved-state) " PC-Selection" "")
1213 (if (or (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " enabled" "")))
1214 (setq cua--saved-state nil))
1215
1216 (t
1217 (if (interactive-p)
1218 (message "CUA mode disabled")))))
1219
1220 (defun cua-debug ()
1221 "Toggle cua debugging."
1222 (interactive)
1223 (setq cua--debug (not cua--debug)))
1224
1225 ;;; cua-base.el ends here