]> code.delx.au - gnu-emacs/blob - lisp/emulation/cua-base.el
* emulation/cua-base.el: Add support for changing cursor types;
[gnu-emacs] / lisp / emulation / cua-base.el
1 ;;; cua-base.el --- emulate CUA key bindings
2
3 ;; Copyright (C) 1997,98,99,200,01,02,03,04 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 (defcustom cua-enable-cua-keys t
264 "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste.
265 If the value is t, these mappings are always enabled. If the value is
266 'shift, these keys are only enabled if the last region was marked with
267 a shifted movement key. If the value is nil, these keys are never
268 enabled."
269 :type '(choice (const :tag "Disabled" nil)
270 (const :tag "Shift region only" shift)
271 (other :tag "Enabled" t))
272 :group 'cua)
273
274 (defcustom cua-highlight-region-shift-only nil
275 "*If non-nil, only highlight region if marked with S-<move>.
276 When this is non-nil, CUA toggles `transient-mark-mode' on when the region
277 is marked using shifted movement keys, and off when the mark is cleared.
278 But when the mark was set using \\[cua-set-mark], transient-mark-mode
279 is not turned on."
280 :type 'boolean
281 :group 'cua)
282
283 (defcustom cua-prefix-override-inhibit-delay
284 (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) nil)
285 "*If non-nil, time in seconds to delay before overriding prefix key.
286 If there is additional input within this time, the prefix key is
287 used as a normal prefix key. So typing a key sequence quickly will
288 inhibit overriding the prefix key.
289 As a special case, if the prefix keys repeated within this time, the
290 first prefix key is discarded, so typing a prefix key twice in quick
291 succession will also inhibit overriding the prefix key.
292 If the value is nil, use a shifted prefix key to inhibit the override."
293 :type '(choice (number :tag "Inhibit delay")
294 (const :tag "No delay" nil))
295 :group 'cua)
296
297 (defcustom cua-keep-region-after-copy nil
298 "If non-nil, don't deselect the region after copying."
299 :type 'boolean
300 :group 'cua)
301
302 (defcustom cua-enable-register-prefix 'not-ctrl-u
303 "*If non-nil, registers are supported via numeric prefix arg.
304 If the value is t, any numeric prefix arg in the range 0 to 9 will be
305 interpreted as a register number.
306 If the value is not-ctrl-u, using C-u to enter a numeric prefix is not
307 interpreted as a register number.
308 If the value is ctrl-u-only, only numeric prefix entered with C-u is
309 interpreted as a register number."
310 :type '(choice (const :tag "Disabled" nil)
311 (const :tag "Enabled, but C-u arg is not a register" not-ctrl-u)
312 (const :tag "Enabled, but only for C-u arg" ctrl-u-only)
313 (other :tag "Enabled" t))
314 :group 'cua)
315
316 (defcustom cua-delete-copy-to-register-0 t
317 "*If non-nil, save last deleted region or rectangle to register 0."
318 :type 'boolean
319 :group 'cua)
320
321 (defcustom cua-use-hyper-key nil
322 "*If non-nil, bind rectangle commands to H-? instead of M-?.
323 If set to 'also, toggle region command is also on S-return.
324 Must be set prior to enabling CUA."
325 :type '(choice (const :tag "Meta key and S-return" nil)
326 (const :tag "Hyper key only" only)
327 (const :tag "Hyper key and S-return" also))
328 :group 'cua)
329
330 (defcustom cua-enable-region-auto-help nil
331 "*If non-nil, automatically show help for active region."
332 :type 'boolean
333 :group 'cua)
334
335 (defcustom cua-enable-modeline-indications nil
336 "*If non-nil, use minor-mode hook to show status in mode line."
337 :type 'boolean
338 :group 'cua)
339
340 (defcustom cua-check-pending-input t
341 "*If non-nil, don't override prefix key if input pending.
342 It is rumoured that input-pending-p is unreliable under some window
343 managers, so try setting this to nil, if prefix override doesn't work."
344 :type 'boolean
345 :group 'cua)
346
347
348 ;;; Rectangle Customization
349
350 (defcustom cua-auto-expand-rectangles nil
351 "*If non-nil, rectangles are padded with spaces to make straight edges.
352 This implies modifying buffer contents by expanding tabs and inserting spaces.
353 Consequently, this is inhibited in read-only buffers.
354 Can be toggled by [M-p] while the rectangle is active,"
355 :type 'boolean
356 :group 'cua)
357
358 (defcustom cua-enable-rectangle-auto-help t
359 "*If non-nil, automatically show help for region, rectangle and global mark."
360 :type 'boolean
361 :group 'cua)
362
363 (defface cua-rectangle-face 'nil
364 "*Font used by CUA for highlighting the rectangle."
365 :group 'cua)
366
367 (defface cua-rectangle-noselect-face 'nil
368 "*Font used by CUA for highlighting the non-selected rectangle lines."
369 :group 'cua)
370
371 (defcustom cua-undo-max 64
372 "*Max no of undoable CUA rectangle changes (including undo)."
373 :type 'integer
374 :group 'cua)
375
376
377 ;;; Global Mark Customization
378
379 (defcustom cua-global-mark-keep-visible t
380 "*If non-nil, always keep global mark visible in other window."
381 :type 'boolean
382 :group 'cua)
383
384 (defface cua-global-mark-face '((((class color))
385 :foreground "black"
386 :background "yellow")
387 (t :bold t))
388 "*Font used by CUA for highlighting the global mark."
389 :group 'cua)
390
391 (defcustom cua-global-mark-blink-cursor-interval 0.20
392 "*Blink cursor at this interval when global mark is active."
393 :type '(choice (number :tag "Blink interval")
394 (const :tag "No blink" nil))
395 :group 'cua)
396
397
398 ;;; Cursor Indication Customization
399
400 (defcustom cua-enable-cursor-indications nil
401 "*If non-nil, use different cursor colors for indications."
402 :type 'boolean
403 :group 'cua)
404
405 (defcustom cua-normal-cursor-color (or (and (boundp 'initial-cursor-color) initial-cursor-color)
406 (and (boundp 'initial-frame-alist)
407 (assoc 'cursor-color initial-frame-alist)
408 (cdr (assoc 'cursor-color initial-frame-alist)))
409 (and (boundp 'default-frame-alist)
410 (assoc 'cursor-color default-frame-alist)
411 (cdr (assoc 'cursor-color default-frame-alist)))
412 (frame-parameter nil 'cursor-color)
413 "red")
414 "Normal (non-overwrite) cursor color.
415 Also used to indicate that rectangle padding is not in effect.
416 Default is to load cursor color from initial or default frame parameters.
417
418 If the value is a COLOR name, then only the `cursor-color' attribute will be
419 affected. If the value is a cursor TYPE (one of: box, block, bar, or hbar),
420 then only the `cursor-type' property will be affected. If the value is
421 a cons (TYPE . COLOR), then both properties are affected."
422 :initialize 'custom-initialize-default
423 :type '(choice
424 (color :tag "Color")
425 (choice :tag "Type"
426 (const :tag "Filled box" box)
427 (const :tag "Vertical bar" bar)
428 (const :tag "Horisontal bar" hbar)
429 (const :tag "Hollow box" block))
430 (cons :tag "Color and Type"
431 (choice :tag "Type"
432 (const :tag "Filled box" box)
433 (const :tag "Vertical bar" bar)
434 (const :tag "Horisontal bar" hbar)
435 (const :tag "Hollow box" block))
436 (color :tag "Color")))
437 :group 'cua)
438
439 (defcustom cua-read-only-cursor-color "darkgreen"
440 "*Cursor color used in read-only buffers, if non-nil.
441 Only used when `cua-enable-cursor-indications' is non-nil.
442
443 If the value is a COLOR name, then only the `cursor-color' attribute will be
444 affected. If the value is a cursor TYPE (one of: box, block, bar, or hbar),
445 then only the `cursor-type' property will be affected. If the value is
446 a cons (TYPE . COLOR), then both properties are affected."
447 :type '(choice
448 (color :tag "Color")
449 (choice :tag "Type"
450 (const :tag "Filled box" box)
451 (const :tag "Vertical bar" bar)
452 (const :tag "Horisontal bar" hbar)
453 (const :tag "Hollow box" block))
454 (cons :tag "Color and Type"
455 (choice :tag "Type"
456 (const :tag "Filled box" box)
457 (const :tag "Vertical bar" bar)
458 (const :tag "Horisontal bar" hbar)
459 (const :tag "Hollow box" block))
460 (color :tag "Color")))
461 :group 'cua)
462
463 (defcustom cua-overwrite-cursor-color "yellow"
464 "*Cursor color used when overwrite mode is set, if non-nil.
465 Also used to indicate that rectangle padding is in effect.
466 Only used when `cua-enable-cursor-indications' is non-nil.
467
468 If the value is a COLOR name, then only the `cursor-color' attribute will be
469 affected. If the value is a cursor TYPE (one of: box, block, bar, or hbar),
470 then only the `cursor-type' property will be affected. If the value is
471 a cons (TYPE . COLOR), then both properties are affected."
472 :type '(choice
473 (color :tag "Color")
474 (choice :tag "Type"
475 (const :tag "Filled box" box)
476 (const :tag "Vertical bar" bar)
477 (const :tag "Horisontal bar" hbar)
478 (const :tag "Hollow box" block))
479 (cons :tag "Color and Type"
480 (choice :tag "Type"
481 (const :tag "Filled box" box)
482 (const :tag "Vertical bar" bar)
483 (const :tag "Horisontal bar" hbar)
484 (const :tag "Hollow box" block))
485 (color :tag "Color")))
486 :group 'cua)
487
488 (defcustom cua-global-mark-cursor-color "cyan"
489 "*Indication for active global mark.
490 Will change cursor color to specified color if string.
491 Only used when `cua-enable-cursor-indications' is non-nil.
492
493 If the value is a COLOR name, then only the `cursor-color' attribute will be
494 affected. If the value is a cursor TYPE (one of: box, block, bar, or hbar),
495 then only the `cursor-type' property will be affected. If the value is
496 a cons (TYPE . COLOR), then both properties are affected."
497 :type '(choice
498 (color :tag "Color")
499 (choice :tag "Type"
500 (const :tag "Filled box" box)
501 (const :tag "Vertical bar" bar)
502 (const :tag "Horisontal bar" hbar)
503 (const :tag "Hollow box" block))
504 (cons :tag "Color and Type"
505 (choice :tag "Type"
506 (const :tag "Filled box" box)
507 (const :tag "Vertical bar" bar)
508 (const :tag "Horisontal bar" hbar)
509 (const :tag "Hollow box" block))
510 (color :tag "Color")))
511 :group 'cua)
512
513
514 ;;; Rectangle support is in cua-rect.el
515
516 (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
517
518 ;; Stub definitions until it is loaded
519
520 (when (not (featurep 'cua-rect))
521 (defvar cua--rectangle)
522 (setq cua--rectangle nil)
523 (defvar cua--last-killed-rectangle)
524 (setq cua--last-killed-rectangle nil))
525
526
527
528 ;;; Global Mark support is in cua-gmrk.el
529
530 (autoload 'cua-toggle-global-mark "cua-gmrk.el" nil t nil)
531
532 ;; Stub definitions until cua-gmrk.el is loaded
533
534 (when (not (featurep 'cua-gmrk))
535 (defvar cua--global-mark-active)
536 (setq cua--global-mark-active nil))
537
538
539 (provide 'cua-base)
540
541 (eval-when-compile
542 (require 'cua-rect)
543 (require 'cua-gmrk)
544 )
545
546
547 ;;; Low-level Interface
548
549 (defvar cua-inhibit-cua-keys nil
550 "Buffer-local variable that may disable the cua keymappings.")
551 (make-variable-buffer-local 'cua-inhibit-cua-keys)
552
553 ;;; Aux. variables
554
555 ;; Current region was started using cua-set-mark.
556 (defvar cua--explicit-region-start nil)
557
558 ;; Latest region was started using shifted movement command.
559 (defvar cua--last-region-shifted nil)
560
561 ;; buffer + point prior to current command when rectangle is active
562 ;; checked in post-command hook to see if point was moved
563 (defvar cua--buffer-and-point-before-command nil)
564
565 ;; status string for mode line indications
566 (defvar cua--status-string nil)
567
568 (defvar cua--debug nil)
569
570
571 ;;; Prefix key override mechanism
572
573 ;; The prefix override (when mark-active) operates in three substates:
574 ;; [1] Before using a prefix key
575 ;; [2] Immediately after using a prefix key
576 ;; [3] A fraction of a second later
577
578 ;; In state [1], the cua--prefix-override-keymap is active.
579 ;; This keymap binds the C-x and C-c prefix keys to the
580 ;; cua--prefix-override-handler function.
581
582 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
583 ;; will push back the keys already read to the event queue. If input is
584 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
585 ;; is started, and it changes to state [2].
586
587 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
588 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
589 ;; C-x to the cua--prefix-repeat-handler function.
590
591 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
592 ;; will cancel [T], back the keys already read (except for the second prefix
593 ;; keys) to the event queue, and changes to state [3].
594
595 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
596 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
597 ;; the cua--prefix-override-timeout function will push a `timeout' event on
598 ;; the event queue, and changes to state [3].
599
600 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
601 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
602 ;; normal prefix key binding from the global or local map will be used.
603
604 ;; The pre-command hook (executed as a consequence of the timeout or normal
605 ;; prefix key binding) will cancel [T] and change from state [3] back to
606 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
607 ;; are always called with state reset to [1]!
608
609 ;; State [1] is recognized by cua--prefix-override-timer is nil,
610 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
611 ;; state [3] is recognized by cua--prefix-override-timer is t.
612
613 (defvar cua--prefix-override-timer nil)
614 (defvar cua--prefix-override-length nil)
615
616 (defun cua--prefix-override-replay (arg repeat)
617 (let* ((keys (this-command-keys))
618 (i (length keys))
619 (key (aref keys (1- i))))
620 (setq cua--prefix-override-length (- i repeat))
621 (setq cua--prefix-override-timer
622 (or
623 ;; In state [2], change to state [3]
624 (> repeat 0)
625 ;; In state [1], change directly to state [3]
626 (and cua-check-pending-input (input-pending-p))
627 ;; In state [1], [T] disabled, so change to state [3]
628 (not (numberp cua-prefix-override-inhibit-delay))
629 (<= cua-prefix-override-inhibit-delay 0)
630 ;; In state [1], start [T] and change to state [2]
631 (run-with-timer cua-prefix-override-inhibit-delay nil
632 'cua--prefix-override-timeout)))
633 ;; Don't record this command
634 (setq this-command last-command)
635 ;; Restore the prefix arg
636 (setq prefix-arg arg)
637 (reset-this-command-lengths)
638 ;; Push the key back on the event queue
639 (setq unread-command-events (cons key unread-command-events))))
640
641 (defun cua--prefix-override-handler (arg)
642 "Start timer waiting for prefix key to be followed by another key.
643 Repeating prefix key when region is active works as a single prefix key."
644 (interactive "P")
645 (cua--prefix-override-replay arg 0))
646
647 (defun cua--prefix-repeat-handler (arg)
648 "Repeating prefix key when region is active works as a single prefix key."
649 (interactive "P")
650 (cua--prefix-override-replay arg 1))
651
652 (defun cua--prefix-copy-handler (arg)
653 "Copy region/rectangle, then replay last key."
654 (interactive "P")
655 (if cua--rectangle
656 (cua-copy-rectangle arg)
657 (cua-copy-region arg))
658 (let ((keys (this-single-command-keys)))
659 (setq unread-command-events
660 (cons (aref keys (1- (length keys))) unread-command-events))))
661
662 (defun cua--prefix-cut-handler (arg)
663 "Cut region/rectangle, then replay last key."
664 (interactive "P")
665 (if cua--rectangle
666 (cua-cut-rectangle arg)
667 (cua-cut-region arg))
668 (let ((keys (this-single-command-keys)))
669 (setq unread-command-events
670 (cons (aref keys (1- (length keys))) unread-command-events))))
671
672 (defun cua--prefix-override-timeout ()
673 (setq cua--prefix-override-timer t)
674 (when (= (length (this-command-keys)) cua--prefix-override-length)
675 (setq unread-command-events (cons 'timeout unread-command-events))
676 (if prefix-arg
677 (reset-this-command-lengths)
678 (setq overriding-terminal-local-map nil))
679 (cua--select-keymaps)))
680
681
682 ;;; Aux. functions
683
684 (defun cua--fallback ()
685 ;; Execute original command
686 (setq this-command this-original-command)
687 (call-interactively this-command))
688
689 (defun cua--keep-active ()
690 (setq mark-active t
691 deactivate-mark nil))
692
693 (defun cua--deactivate (&optional now)
694 (setq cua--explicit-region-start nil)
695 (if (not now)
696 (setq deactivate-mark t)
697 (setq mark-active nil)
698 (run-hooks 'deactivate-mark-hook)))
699
700
701 ;; The current register prefix
702 (defvar cua--register nil)
703
704 (defun cua--prefix-arg (arg)
705 (setq cua--register
706 (and cua-enable-register-prefix
707 (integerp arg) (>= arg 0) (< arg 10)
708 (let* ((prefix (aref (this-command-keys) 0))
709 (ctrl-u-prefix (and (integerp prefix)
710 (= prefix ?\C-u))))
711 (cond
712 ((eq cua-enable-register-prefix 'not-ctrl-u)
713 (not ctrl-u-prefix))
714 ((eq cua-enable-register-prefix 'ctrl-u-only)
715 ctrl-u-prefix)
716 (t t)))
717 (+ arg ?0)))
718 (if cua--register nil arg))
719
720 ;;; Enhanced undo - restore rectangle selections
721
722 (defun cua-undo (&optional arg)
723 "Undo some previous changes.
724 Knows about CUA rectangle highlighting in addition to standard undo."
725 (interactive "*P")
726 (if (fboundp 'cua--rectangle-undo)
727 (cua--rectangle-undo arg)
728 (undo arg)))
729
730 ;;; Region specific commands
731
732 (defvar cua--last-deleted-region-pos nil)
733 (defvar cua--last-deleted-region-text nil)
734
735 (defun cua-delete-region ()
736 "Delete the active region.
737 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
738 (interactive)
739 (let ((start (mark)) (end (point)))
740 (or (<= start end)
741 (setq start (prog1 end (setq end start))))
742 (setq cua--last-deleted-region-text (buffer-substring start end))
743 (if cua-delete-copy-to-register-0
744 (set-register ?0 cua--last-deleted-region-text))
745 (delete-region start end)
746 (setq cua--last-deleted-region-pos
747 (cons (current-buffer)
748 (and (consp buffer-undo-list)
749 (car buffer-undo-list))))
750 (cua--deactivate)))
751
752 (defun cua-replace-region ()
753 "Replace the active region with the character you type."
754 (interactive)
755 (cua-delete-region)
756 (unless (eq this-original-command this-command)
757 (cua--fallback)))
758
759 (defun cua-copy-region (arg)
760 "Copy the region to the kill ring.
761 With numeric prefix arg, copy to register 0-9 instead."
762 (interactive "P")
763 (setq arg (cua--prefix-arg arg))
764 (setq cua--last-killed-rectangle nil)
765 (let ((start (mark)) (end (point)))
766 (or (<= start end)
767 (setq start (prog1 end (setq end start))))
768 (if cua--register
769 (copy-to-register cua--register start end nil)
770 (copy-region-as-kill start end))
771 (if cua-keep-region-after-copy
772 (cua--keep-active)
773 (cua--deactivate))))
774
775 (defun cua-cut-region (arg)
776 "Cut the region and copy to the kill ring.
777 With numeric prefix arg, copy to register 0-9 instead."
778 (interactive "P")
779 (setq cua--last-killed-rectangle nil)
780 (if buffer-read-only
781 (cua-copy-region arg)
782 (setq arg (cua--prefix-arg arg))
783 (let ((start (mark)) (end (point)))
784 (or (<= start end)
785 (setq start (prog1 end (setq end start))))
786 (if cua--register
787 (copy-to-register cua--register start end t)
788 (kill-region start end)))
789 (cua--deactivate)))
790
791 ;;; Generic commands for regions, rectangles, and global marks
792
793 (defun cua-cancel ()
794 "Cancel the active region, rectangle, or global mark."
795 (interactive)
796 (setq mark-active nil)
797 (setq cua--explicit-region-start nil)
798 (if (fboundp 'cua--cancel-rectangle)
799 (cua--cancel-rectangle)))
800
801 (defun cua-paste (arg)
802 "Paste last cut or copied region or rectangle.
803 An active region is deleted before executing the command.
804 With numeric prefix arg, paste from register 0-9 instead.
805 If global mark is active, copy from register or one character."
806 (interactive "P")
807 (setq arg (cua--prefix-arg arg))
808 (let ((regtxt (and cua--register (get-register cua--register)))
809 (count (prefix-numeric-value arg)))
810 (cond
811 ((and cua--register (not regtxt))
812 (message "Nothing in register %c" cua--register))
813 (cua--global-mark-active
814 (if regtxt
815 (cua--insert-at-global-mark regtxt)
816 (when (not (eobp))
817 (cua--insert-at-global-mark (buffer-substring (point) (+ (point) count)))
818 (forward-char count))))
819 (buffer-read-only
820 (message "Cannot paste into a read-only buffer"))
821 (t
822 ;; Must save register here, since delete may override reg 0.
823 (if mark-active
824 ;; Before a yank command, make sure we don't yank
825 ;; the same region that we are going to delete.
826 ;; That would make yank a no-op.
827 (if cua--rectangle
828 (cua--delete-rectangle)
829 (if (string= (buffer-substring (point) (mark))
830 (car kill-ring))
831 (current-kill 1))
832 (cua-delete-region)))
833 (cond
834 (regtxt
835 (cond
836 ((consp regtxt) (cua--insert-rectangle regtxt))
837 ((stringp regtxt) (insert-for-yank regtxt))
838 (t (message "Unknown data in register %c" cua--register))))
839 ((and cua--last-killed-rectangle
840 (eq (and kill-ring (car kill-ring)) (car cua--last-killed-rectangle)))
841 (let ((pt (point)))
842 (when (not (eq buffer-undo-list t))
843 (setq this-command 'cua--paste-rectangle)
844 (undo-boundary)
845 (setq buffer-undo-list (cons pt buffer-undo-list)))
846 (cua--insert-rectangle (cdr cua--last-killed-rectangle))
847 (if arg (goto-char pt))))
848 (t (yank arg)))))))
849
850 (defun cua-paste-pop (arg)
851 "Replace a just-pasted text or rectangle with a different text.
852 See `yank-pop' for details."
853 (interactive "P")
854 (if (eq last-command 'cua--paste-rectangle)
855 (progn
856 (undo)
857 (yank arg))
858 (yank-pop (prefix-numeric-value arg))))
859
860 (defun cua-exchange-point-and-mark (arg)
861 "Exchanges point and mark, but don't activate the mark.
862 Activates the mark if a prefix argument is given."
863 (interactive "P")
864 (if arg
865 (setq mark-active t)
866 (let (mark-active)
867 (exchange-point-and-mark)
868 (if cua--rectangle
869 (cua--rectangle-corner 0)))))
870
871 ;; Typed text that replaced the highlighted region.
872 (defvar cua--repeat-replace-text nil)
873
874 (defun cua-repeat-replace-region (arg)
875 "Repeat replacing text of highlighted region with typed text.
876 Searches for the next streach of text identical to the region last
877 replaced by typing text over it and replaces it with the same streach
878 of text."
879 (interactive "P")
880 (when cua--last-deleted-region-pos
881 (save-excursion
882 (save-restriction
883 (set-buffer (car cua--last-deleted-region-pos))
884 (widen)
885 ;; Find the text that replaced the region via the undo list.
886 (let ((ul buffer-undo-list)
887 (elt (cdr cua--last-deleted-region-pos))
888 u s e)
889 (when elt
890 (while (consp ul)
891 (setq u (car ul) ul (cdr ul))
892 (cond
893 ((eq u elt) ;; got it
894 (setq ul nil))
895 ((and (consp u) (integerp (car u)) (integerp (cdr u)))
896 (if (and s (= (cdr u) s))
897 (setq s (car u))
898 (setq s (car u) e (cdr u)))))))
899 (setq cua--repeat-replace-text
900 (cond ((and s e (<= s e) (= s (mark t)))
901 (buffer-substring-no-properties s e))
902 ((and (null s) (eq u elt)) ;; nothing inserted
903 "")
904 (t
905 (message "Cannot locate replacement text")
906 nil))))))
907 (setq cua--last-deleted-region-pos nil))
908 (if (and cua--last-deleted-region-text
909 cua--repeat-replace-text
910 (search-forward cua--last-deleted-region-text nil t nil))
911 (replace-match cua--repeat-replace-text arg t)))
912
913 (defun cua-help-for-region (&optional help)
914 "Show region specific help in echo area."
915 (interactive)
916 (message
917 (concat (if help "C-?:help " "")
918 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
919
920
921 ;;; Shift activated / extended region
922
923 (defun cua-set-mark (&optional arg)
924 "Set mark at where point is, clear mark, or jump to mark.
925
926 With no prefix argument, clear mark if already set. Otherwise, set
927 mark, and push old mark position on local mark ring; also push mark on
928 global mark ring if last mark was set in another buffer.
929
930 With argument, jump to mark, and pop a new position for mark off
931 the local mark ring \(this does not affect the global mark ring\).
932 Use \\[pop-global-mark] to jump to a mark off the global mark ring
933 \(see `pop-global-mark'\).
934
935 Repeating the command without the prefix jumps to the next position
936 off the local \(or global\) mark ring.
937
938 With a double \\[universal-argument] prefix argument, unconditionally set mark."
939 (interactive "P")
940 (cond
941 ((and (consp arg) (> (prefix-numeric-value arg) 4))
942 (push-mark-command nil))
943 ((eq last-command 'pop-to-mark-command)
944 (setq this-command 'pop-to-mark-command)
945 (pop-to-mark-command))
946 ((and (eq last-command 'pop-global-mark) (not arg))
947 (setq this-command 'pop-global-mark)
948 (pop-global-mark))
949 (arg
950 (setq this-command 'pop-to-mark-command)
951 (pop-to-mark-command))
952 (mark-active
953 (cua--deactivate)
954 (message "Mark Cleared"))
955 (t
956 (push-mark-command nil nil)
957 (setq cua--explicit-region-start t)
958 (setq cua--last-region-shifted nil)
959 (if cua-enable-region-auto-help
960 (cua-help-for-region t)))))
961
962 (defvar cua--standard-movement-commands
963 '(forward-char backward-char
964 next-line previous-line
965 forward-word backward-word
966 end-of-line beginning-of-line
967 end-of-buffer beginning-of-buffer
968 scroll-up scroll-down cua-scroll-up cua-scroll-down
969 forward-sentence backward-sentence
970 forward-paragraph backward-paragraph)
971 "List of standard movement commands.
972 Extra commands should be added to `cua-movement-commands'")
973
974 (defvar cua-movement-commands nil
975 "User may add additional movement commands to this list.")
976
977
978 ;;; Scrolling commands which does not signal errors at top/bottom
979 ;;; of buffer at first key-press (instead moves to top/bottom
980 ;;; of buffer).
981
982 (defun cua-scroll-up (&optional arg)
983 "Scroll text of current window upward ARG lines; or near full screen if no ARG.
984 If window cannot be scrolled further, move cursor to bottom line instead.
985 A near full screen is `next-screen-context-lines' less than a full screen.
986 Negative ARG means scroll downward.
987 If ARG is the atom `-', scroll downward by nearly full screen."
988 (interactive "P")
989 (cond
990 ((eq arg '-) (cua-scroll-down nil))
991 ((< (prefix-numeric-value arg) 0)
992 (cua-scroll-down (- (prefix-numeric-value arg))))
993 ((eobp)
994 (scroll-up arg)) ; signal error
995 (t
996 (condition-case nil
997 (scroll-up arg)
998 (end-of-buffer (goto-char (point-max)))))))
999
1000 (defun cua-scroll-down (&optional arg)
1001 "Scroll text of current window downward ARG lines; or near full screen if no ARG.
1002 If window cannot be scrolled further, move cursor to top line instead.
1003 A near full screen is `next-screen-context-lines' less than a full screen.
1004 Negative ARG means scroll upward.
1005 If ARG is the atom `-', scroll upward by nearly full screen."
1006 (interactive "P")
1007 (cond
1008 ((eq arg '-) (cua-scroll-up nil))
1009 ((< (prefix-numeric-value arg) 0)
1010 (cua-scroll-up (- (prefix-numeric-value arg))))
1011 ((bobp)
1012 (scroll-down arg)) ; signal error
1013 (t
1014 (condition-case nil
1015 (scroll-down arg)
1016 (beginning-of-buffer (goto-char (point-min)))))))
1017
1018 ;;; Cursor indications
1019
1020 (defun cua--update-indications ()
1021 (let* ((cursor
1022 (cond
1023 ((and cua--global-mark-active
1024 cua-global-mark-cursor-color)
1025 cua-global-mark-cursor-color)
1026 ((and buffer-read-only
1027 cua-read-only-cursor-color)
1028 cua-read-only-cursor-color)
1029 ((and cua-overwrite-cursor-color
1030 (or overwrite-mode
1031 (and cua--rectangle (cua--rectangle-padding))))
1032 cua-overwrite-cursor-color)
1033 (t cua-normal-cursor-color)))
1034 (color (if (consp cursor) (cdr cursor) cursor))
1035 (type (if (consp cursor) (car cursor) cursor)))
1036 (if (and color
1037 (stringp color)
1038 (not (equal color (frame-parameter nil 'cursor-color))))
1039 (set-cursor-color color))
1040 (if (and type
1041 (symbolp type)
1042 (not (eq type (frame-parameter nil 'cursor-type))))
1043 (setq default-cursor-type type))))
1044
1045
1046 ;;; Pre-command hook
1047
1048 (defun cua--pre-command-handler ()
1049 (condition-case nil
1050 (let ((movement (or (memq this-command cua--standard-movement-commands)
1051 (memq this-command cua-movement-commands))))
1052
1053 ;; Cancel prefix key timeout if user enters another key.
1054 (when cua--prefix-override-timer
1055 (if (timerp cua--prefix-override-timer)
1056 (cancel-timer cua--prefix-override-timer))
1057 (setq cua--prefix-override-timer nil))
1058
1059 ;; Handle shifted cursor keys and other movement commands.
1060 ;; If region is not active, region is activated if key is shifted.
1061 ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC).
1062 ;; If rectangle is active, expand rectangle in specified direction and ignore the movement.
1063 (if movement
1064 (cond
1065 ((memq 'shift (event-modifiers (aref (this-single-command-raw-keys) 0)))
1066 (unless mark-active
1067 (push-mark-command nil t))
1068 (setq cua--last-region-shifted t)
1069 (setq cua--explicit-region-start nil))
1070 ((or cua--explicit-region-start cua--rectangle)
1071 (unless mark-active
1072 (push-mark-command nil nil)))
1073 (t
1074 ;; If we set mark-active to nil here, the region highlight will not be
1075 ;; removed by the direct_output_ commands.
1076 (setq deactivate-mark t)))
1077
1078 ;; Handle delete-selection property on other commands
1079 (if (and mark-active (not deactivate-mark))
1080 (let* ((ds (or (get this-command 'delete-selection)
1081 (get this-command 'pending-delete)))
1082 (nc (cond
1083 ((not ds) nil)
1084 ((eq ds 'yank)
1085 'cua-paste)
1086 ((eq ds 'kill)
1087 (if cua--rectangle
1088 'cua-copy-rectangle
1089 'cua-copy-region))
1090 ((eq ds 'supersede)
1091 (if cua--rectangle
1092 'cua-delete-rectangle
1093 'cua-delete-region))
1094 (t
1095 (if cua--rectangle
1096 'cua-delete-rectangle ;; replace?
1097 'cua-replace-region)))))
1098 (if nc
1099 (setq this-original-command this-command
1100 this-command nc)))))
1101
1102 ;; Detect extension of rectangles by mouse or other movement
1103 (setq cua--buffer-and-point-before-command
1104 (if cua--rectangle (cons (current-buffer) (point))))
1105 )
1106 (error nil)))
1107
1108 ;;; Post-command hook
1109
1110 (defun cua--post-command-handler ()
1111 (condition-case nil
1112 (progn
1113 (when cua--global-mark-active
1114 (cua--global-mark-post-command))
1115 (when (fboundp 'cua--rectangle-post-command)
1116 (cua--rectangle-post-command))
1117 (setq cua--buffer-and-point-before-command nil)
1118 (if (or (not mark-active) deactivate-mark)
1119 (setq cua--explicit-region-start nil))
1120
1121 ;; Debugging
1122 (if cua--debug
1123 (cond
1124 (cua--rectangle (cua--rectangle-assert))
1125 (mark-active (message "Mark=%d Point=%d Expl=%s"
1126 (mark t) (point) cua--explicit-region-start))))
1127
1128 ;; Disable transient-mark-mode if rectangle active in current buffer.
1129 (if (not (window-minibuffer-p (selected-window)))
1130 (setq transient-mark-mode (and (not cua--rectangle)
1131 (if cua-highlight-region-shift-only
1132 (not cua--explicit-region-start)
1133 t))))
1134 (if cua-enable-cursor-indications
1135 (cua--update-indications))
1136
1137 (cua--select-keymaps)
1138 )
1139
1140 (error nil)))
1141
1142
1143 ;;; Keymaps
1144
1145 (defun cua--M/H-key (map key fct)
1146 ;; bind H-KEY or M-KEY to FCT in MAP
1147 (if (eq key 'space) (setq key ? ))
1148 (unless (listp key) (setq key (list key)))
1149 (define-key map (vector (cons (if cua-use-hyper-key 'hyper 'meta) key)) fct))
1150
1151 (defun cua--self-insert-char-p (def)
1152 ;; Return DEF if current key sequence is self-inserting in
1153 ;; global-map.
1154 (if (memq (global-key-binding (this-single-command-keys))
1155 '(self-insert-command self-insert-iso))
1156 def nil))
1157
1158 (defvar cua-global-keymap (make-sparse-keymap)
1159 "Global keymap for cua-mode; users may add to this keymap.")
1160
1161 (defvar cua--cua-keys-keymap (make-sparse-keymap))
1162 (defvar cua--prefix-override-keymap (make-sparse-keymap))
1163 (defvar cua--prefix-repeat-keymap (make-sparse-keymap))
1164 (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
1165 (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
1166 (defvar cua--region-keymap (make-sparse-keymap))
1167
1168 (defvar cua--ena-cua-keys-keymap nil)
1169 (defvar cua--ena-prefix-override-keymap nil)
1170 (defvar cua--ena-prefix-repeat-keymap nil)
1171 (defvar cua--ena-region-keymap nil)
1172 (defvar cua--ena-global-mark-keymap nil)
1173
1174 (defvar cua--keymap-alist
1175 `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap)
1176 (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap)
1177 (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap)
1178 (cua--ena-global-mark-keymap . ,cua--global-mark-keymap)
1179 (cua--rectangle . ,cua--rectangle-keymap)
1180 (cua--ena-region-keymap . ,cua--region-keymap)
1181 (cua-mode . ,cua-global-keymap)))
1182
1183 (defun cua--select-keymaps ()
1184 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
1185 (setq cua--ena-region-keymap
1186 (and mark-active (not deactivate-mark)))
1187 (setq cua--ena-prefix-override-keymap
1188 (and cua--ena-region-keymap
1189 cua-enable-cua-keys
1190 (not cua-inhibit-cua-keys)
1191 (or (eq cua-enable-cua-keys t)
1192 (not cua--explicit-region-start))
1193 (not executing-kbd-macro)
1194 (not cua--prefix-override-timer)))
1195 (setq cua--ena-prefix-repeat-keymap
1196 (and cua--ena-region-keymap
1197 (timerp cua--prefix-override-timer)))
1198 (setq cua--ena-cua-keys-keymap
1199 (and cua-enable-cua-keys
1200 (not cua-inhibit-cua-keys)
1201 (or (eq cua-enable-cua-keys t)
1202 cua--last-region-shifted)))
1203 (setq cua--ena-global-mark-keymap
1204 (and cua--global-mark-active
1205 (not (window-minibuffer-p)))))
1206
1207 (defvar cua--keymaps-initalized nil)
1208
1209 (defun cua--init-keymaps ()
1210 (unless (eq cua-use-hyper-key 'only)
1211 (define-key cua-global-keymap [(shift return)] 'cua-set-rectangle-mark))
1212 (when cua-use-hyper-key
1213 (cua--M/H-key cua-global-keymap 'space 'cua-set-rectangle-mark)
1214 (define-key cua-global-keymap [(hyper mouse-1)] 'cua-mouse-set-rectangle-mark))
1215
1216 (define-key cua-global-keymap [(shift control ? )] 'cua-toggle-global-mark)
1217
1218 ;; replace region with rectangle or element on kill ring
1219 (define-key cua-global-keymap [remap yank] 'cua-paste)
1220 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1221 ;; replace current yank with previous kill ring element
1222 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1223 ;; set mark
1224 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
1225 ;; undo
1226 (define-key cua-global-keymap [remap undo] 'cua-undo)
1227 (define-key cua-global-keymap [remap advertised-undo] 'cua-undo)
1228
1229 ;; scrolling
1230 (define-key cua-global-keymap [remap scroll-up] 'cua-scroll-up)
1231 (define-key cua-global-keymap [remap scroll-down] 'cua-scroll-down)
1232
1233 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
1234 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
1235 (define-key cua--cua-keys-keymap [(control z)] 'undo)
1236 (define-key cua--cua-keys-keymap [(control v)] 'yank)
1237 (define-key cua--cua-keys-keymap [(meta v)] 'cua-repeat-replace-region)
1238 (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1239
1240 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1241 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
1242
1243 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1244 (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler)
1245 (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler)
1246 (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler)
1247 (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler)
1248 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1249 (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler)
1250 (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler)
1251 (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler)
1252 (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler)
1253
1254 ;; Enable shifted fallbacks for C-x and C-c when region is active
1255 (define-key cua--region-keymap [(shift control x)] 'Control-X-prefix)
1256 (define-key cua--region-keymap [(shift control c)] 'mode-specific-command-prefix)
1257 ;; replace current region
1258 (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region)
1259 (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region)
1260 (define-key cua--region-keymap [remap insert-register] 'cua-replace-region)
1261 (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region)
1262 (define-key cua--region-keymap [remap newline] 'cua-replace-region)
1263 (define-key cua--region-keymap [remap open-line] 'cua-replace-region)
1264 ;; delete current region
1265 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1266 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1267 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1268 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1269 ;; kill region
1270 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1271 ;; copy region
1272 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1273 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1274 ;; cancel current region/rectangle
1275 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1276 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1277 )
1278
1279 ;; State prior to enabling cua-mode
1280 ;; Value is a list with the following elements:
1281 ;; transient-mark-mode
1282 ;; delete-selection-mode
1283 ;; pc-selection-mode
1284
1285 (defvar cua--saved-state nil)
1286
1287 ;;;###autoload
1288 (define-minor-mode cua-mode
1289 "Toggle CUA key-binding mode.
1290 When enabled, using shifted movement keys will activate the region (and
1291 highlight the region using `transient-mark-mode'), and typed text replaces
1292 the active selection. C-z, C-x, C-c, and C-v will undo, cut, copy, and
1293 paste (in addition to the normal emacs bindings)."
1294 :global t
1295 :set-after '(cua-enable-modeline-indications cua-use-hyper-key)
1296 :require 'cua-base
1297 :link '(emacs-commentary-link "cua-base.el")
1298 :version "21.4"
1299 (setq mark-even-if-inactive t)
1300 (setq highlight-nonselected-windows nil)
1301 (make-variable-buffer-local 'cua--explicit-region-start)
1302 (make-variable-buffer-local 'cua--status-string)
1303
1304 (unless cua--keymaps-initalized
1305 (cua--init-keymaps)
1306 (setq cua--keymaps-initalized t))
1307
1308 (if cua-mode
1309 (progn
1310 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1311 (add-hook 'post-command-hook 'cua--post-command-handler)
1312 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1313 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
1314 (if cua-enable-cursor-indications
1315 (cua--update-indications)))
1316
1317 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1318 (remove-hook 'post-command-hook 'cua--post-command-handler))
1319
1320 (if (not cua-mode)
1321 (setq emulation-mode-map-alists (delq 'cua--keymap-alist emulation-mode-map-alists))
1322 (add-to-list 'emulation-mode-map-alists 'cua--keymap-alist)
1323 (cua--select-keymaps))
1324
1325 (if (fboundp 'cua--rectangle-on-off)
1326 (cua--rectangle-on-off cua-mode))
1327
1328 (cond
1329 (cua-mode
1330 (setq cua--saved-state
1331 (list
1332 transient-mark-mode
1333 (and (boundp 'delete-selection-mode) delete-selection-mode)
1334 (and (boundp 'pc-selection-mode) pc-selection-mode)))
1335 (if (and (boundp 'delete-selection-mode) delete-selection-mode)
1336 (delete-selection-mode -1))
1337 (if (and (boundp 'pc-selection-mode) pc-selection-mode)
1338 (pc-selection-mode -1))
1339 (setq transient-mark-mode (and cua-mode
1340 (if cua-highlight-region-shift-only
1341 (not cua--explicit-region-start)
1342 t))))
1343 (cua--saved-state
1344 (setq transient-mark-mode (car cua--saved-state))
1345 (if (nth 1 cua--saved-state)
1346 (delete-selection-mode 1))
1347 (if (nth 2 cua--saved-state)
1348 (pc-selection-mode 1))
1349 (if (interactive-p)
1350 (message "CUA mode disabled.%s%s%s%s"
1351 (if (nth 1 cua--saved-state) " Delete-Selection" "")
1352 (if (and (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " and" "")
1353 (if (nth 2 cua--saved-state) " PC-Selection" "")
1354 (if (or (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " enabled" "")))
1355 (setq cua--saved-state nil))))
1356
1357 (defun cua-debug ()
1358 "Toggle cua debugging."
1359 (interactive)
1360 (setq cua--debug (not cua--debug)))
1361
1362 ;; Install run-time check for older versions of CUA-mode which does not
1363 ;; work with GNU Emacs version 21.4 and newer.
1364 ;;
1365 ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
1366 ;; provided the `CUA-mode' feature. Since this is no longer true,
1367 ;; we can warn the user if the `CUA-mode' feature is ever provided.
1368
1369 ;;;###autoload (eval-after-load 'CUA-mode
1370 ;;;###autoload '(error (concat "\n\n"
1371 ;;;###autoload "CUA-mode is now part of the standard GNU Emacs distribution,\n"
1372 ;;;###autoload "so you may now enable and customize CUA via the Options menu.\n\n"
1373 ;;;###autoload "Your " (file-name-nondirectory user-init-file) " loads an older version of CUA-mode which does\n"
1374 ;;;###autoload "not work correctly with this version of GNU Emacs.\n"
1375 ;;;###autoload "To correct this, remove the loading and customization of the\n"
1376 ;;;###autoload "old version from the " user-init-file " file.\n\n")))
1377
1378 (provide 'cua)
1379
1380 ;;; arch-tag: 21fb6289-ba25-4fee-bfdc-f9fb351acf05
1381 ;;; cua-base.el ends here