]> code.delx.au - gnu-emacs/blob - lisp/emulation/cua-base.el
Merged in changes from CVS trunk. Plus added lisp/term tweaks.
[gnu-emacs] / lisp / emulation / cua-base.el
1 ;;; cua-base.el --- emulate CUA key bindings
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5
6 ;; Author: Kim F. Storm <storm@cua.dk>
7 ;; Keywords: keyboard emulation convenience cua
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26
27 ;;; Commentary:
28
29 ;; This is the CUA package which provides a complete emulation of the
30 ;; standard CUA key bindings (Motif/Windows/Mac GUI) for selecting and
31 ;; manipulating the region where S-<movement> is used to highlight &
32 ;; extend the region.
33
34 ;; CUA style key bindings for cut and paste
35 ;; ----------------------------------------
36
37 ;; This package allows the C-z, C-x, C-c, and C-v keys to be
38 ;; bound appropriately according to the Motif/Windows GUI, i.e.
39 ;; C-z -> undo
40 ;; C-x -> cut
41 ;; C-c -> copy
42 ;; C-v -> paste
43 ;;
44 ;; The tricky part is the handling of the C-x and C-c keys which
45 ;; are normally used as prefix keys for most of emacs' built-in
46 ;; commands. With CUA they still do!!!
47 ;;
48 ;; Only when the region is currently active (and highlighted since
49 ;; transient-mark-mode is used), the C-x and C-c keys will work as CUA
50 ;; keys
51 ;; C-x -> cut
52 ;; C-c -> copy
53 ;; When the region is not active, C-x and C-c works as prefix keys!
54 ;;
55 ;; This probably sounds strange and difficult to get used to - but
56 ;; based on my own experience and the feedback from many users of
57 ;; this package, it actually works very well and users adapt to it
58 ;; instantly - or at least very quickly. So give it a try!
59 ;; ... and in the few cases where you make a mistake and accidentally
60 ;; delete the region - you just undo the mistake (with C-z).
61 ;;
62 ;; If you really need to perform a command which starts with one of
63 ;; the prefix keys even when the region is active, you have three options:
64 ;; - press the prefix key twice very quickly (within 0.2 seconds),
65 ;; - press the prefix key and the following key within 0.2 seconds, or
66 ;; - use the SHIFT key with the prefix key, i.e. C-X or C-C
67 ;;
68 ;; This behaviour can be customized via the
69 ;; cua-prefix-override-inhibit-delay variable.
70
71 ;; In addition to using the shifted movement keys, you can also use
72 ;; [C-space] to start the region and use unshifted movement keys to extend
73 ;; it. To cancel the region, use [C-space] or [C-g].
74
75 ;; If you prefer to use the standard emacs cut, copy, paste, and undo
76 ;; bindings, customize cua-enable-cua-keys to nil.
77
78
79 ;; Typing text replaces the region
80 ;; -------------------------------
81
82 ;; When the region is active, i.e. highlighted, the text in region is
83 ;; replaced by the text you type.
84
85 ;; The replaced text is saved in register 0 which can be inserted using
86 ;; the key sequence M-0 C-v (see the section on register support below).
87
88 ;; If you have just replaced a highlighted region with typed text,
89 ;; you can repeat the replace with M-v. This will search forward
90 ;; for a streach of text identical to the previous contents of the
91 ;; region (i.e. the contents of register 0) and replace it with the
92 ;; text you typed to replace the original region. Repeating M-v will
93 ;; replace the next matching region and so on.
94 ;;
95 ;; Example: Suppose you have a line like this
96 ;; The redo operation will redo the last redoable command
97 ;; which you want to change into
98 ;; The repeat operation will repeat the last repeatable command
99 ;; This is done by highlighting the first occurrence of "redo"
100 ;; and type "repeat" M-v M-v.
101
102 ;; Note: Since CUA-mode duplicates the functionality of the
103 ;; delete-selection-mode, that mode is automatically disabled when
104 ;; CUA-mode is enabled.
105
106
107 ;; CUA mode indications
108 ;; --------------------
109 ;; You can choose to let CUA use different cursor colors to indicate
110 ;; overwrite mode and read-only buffers. For example, the following
111 ;; setting will use a RED cursor in normal (insertion) mode in
112 ;; read-write buffers, a YELLOW cursor in overwrite mode in read-write
113 ;; buffers, and a GREEN cursor read-only buffers:
114 ;;
115 ;; (setq cua-normal-cursor-color "red")
116 ;; (setq cua-overwrite-cursor-color "yellow")
117 ;; (setq cua-read-only-cursor-color "green")
118 ;;
119
120 ;; CUA register support
121 ;; --------------------
122 ;; Emacs' standard register support is also based on a separate set of
123 ;; "register commands".
124 ;;
125 ;; CUA's register support is activated by providing a numeric
126 ;; prefix argument to the C-x, C-c, and C-v commands. For example,
127 ;; to copy the selected region to register 2, enter [M-2 C-c].
128 ;; Or if you have activated the keypad prefix mode, enter [kp-2 C-c].
129 ;;
130 ;; And CUA will copy and paste normal region as well as rectangles
131 ;; into the registers, i.e. you use exactly the same command for both.
132 ;;
133 ;; In addition, the last highlighted text that is deleted (not
134 ;; copied), e.g. by [delete] or by typing text over a highlighted
135 ;; region, is automatically saved in register 0, so you can insert it
136 ;; using [M-0 C-v].
137
138 ;; CUA rectangle support
139 ;; ---------------------
140 ;; Emacs' normal rectangle support is based on interpreting the region
141 ;; between the mark and point as a "virtual rectangle", and using a
142 ;; completely separate set of "rectangle commands" [C-x r ...] on the
143 ;; region to copy, kill, fill a.s.o. the virtual rectangle.
144 ;;
145 ;; cua-mode's superior rectangle support uses a true visual
146 ;; representation of the selected rectangle, i.e. it highlights the
147 ;; actual part of the buffer that is currently selected as part of the
148 ;; rectangle. Unlike emacs' traditional rectangle commands, the
149 ;; selected rectangle always as straight left and right edges, even
150 ;; when those are in the middle of a TAB character or beyond the end
151 ;; of the current line. And it does this without actually modifying
152 ;; the buffer contents (it uses display overlays to visualize the
153 ;; virtual dimensions of the rectangle).
154 ;;
155 ;; This means that cua-mode's rectangles are not limited to the actual
156 ;; contents of the buffer, so if the cursor is currently at the end of a
157 ;; short line, you can still extend the rectangle to include more columns
158 ;; of longer lines in the same rectangle. And you can also have the
159 ;; left edge of a rectangle start in the middle of a TAB character.
160 ;; Sounds strange? Try it!
161 ;;
162 ;; To start a rectangle, use [C-return] and extend it using the normal
163 ;; movement keys (up, down, left, right, home, end, C-home,
164 ;; C-end). Once the rectangle has the desired size, you can cut or
165 ;; copy it using C-x and C-c (or C-w and M-w), and you can
166 ;; subsequently insert it - as a rectangle - using C-v (or C-y). So
167 ;; the only new command you need to know to work with cua-mode
168 ;; rectangles is C-return!
169 ;;
170 ;; Normally, when you paste a rectangle using C-v (C-y), each line of
171 ;; the rectangle is inserted into the existing lines in the buffer.
172 ;; If overwrite-mode is active when you paste a rectangle, it is
173 ;; inserted as normal (multi-line) text.
174 ;;
175 ;; If you prefer the traditional rectangle marking (i.e. don't want
176 ;; straight edges), [M-p] toggles this for the current rectangle,
177 ;; or you can customize cua-virtual-rectangle-edges.
178
179 ;; And there's more: If you want to extend or reduce the size of the
180 ;; rectangle in one of the other corners of the rectangle, just use
181 ;; [return] to move the cursor to the "next" corner. Or you can use
182 ;; the [M-up], [M-down], [M-left], and [M-right] keys to move the
183 ;; entire rectangle overlay (but not the contents) in the given
184 ;; direction.
185 ;;
186 ;; [C-return] cancels the rectangle
187 ;; [C-space] activates the region bounded by the rectangle
188
189 ;; If you type a normal (self-inserting) character when the rectangle is
190 ;; active, the character is inserted on the "current side" of every line
191 ;; of the rectangle. The "current side" is the side on which the cursor
192 ;; is currently located. If the rectangle is only 1 column wide,
193 ;; insertion will be performed to the left when the cursor is at the
194 ;; bottom of the rectangle. So, for example, to comment out an entire
195 ;; paragraph like this one, just place the cursor on the first character
196 ;; of the first line, and enter the following:
197 ;; C-return M-} ; ; <space> C-return
198
199 ;; cua-mode's rectangle support also includes all the normal rectangle
200 ;; functions with easy access:
201 ;;
202 ;; [M-a] aligns all words at the left edge of the rectangle
203 ;; [M-b] fills the rectangle with blanks (tabs and spaces)
204 ;; [M-c] closes the rectangle by removing all blanks at the left edge
205 ;; of the rectangle
206 ;; [M-f] fills the rectangle with a single character (prompt)
207 ;; [M-i] increases the first number found on each line of the rectangle
208 ;; by the amount given by the numeric prefix argument (default 1)
209 ;; It recognizes 0x... as hexadecimal numbers
210 ;; [M-k] kills the rectangle as normal multi-line text (for paste)
211 ;; [M-l] downcases the rectangle
212 ;; [M-m] copies the rectangle as normal multi-line text (for paste)
213 ;; [M-n] fills each line of the rectangle with increasing numbers using
214 ;; a supplied format string (prompt)
215 ;; [M-o] opens the rectangle by moving the highlighted text to the
216 ;; right of the rectangle and filling the rectangle with blanks.
217 ;; [M-p] toggles virtual straight rectangle edges
218 ;; [M-P] inserts tabs and spaces (padding) to make real straight edges
219 ;; [M-q] performs text filling on the rectangle
220 ;; [M-r] replaces REGEXP (prompt) by STRING (prompt) in rectangle
221 ;; [M-R] reverse the lines in the rectangle
222 ;; [M-s] fills each line of the rectangle with the same STRING (prompt)
223 ;; [M-t] performs text fill of the rectangle with TEXT (prompt)
224 ;; [M-u] upcases the rectangle
225 ;; [M-|] runs shell command on rectangle
226 ;; [M-'] restricts rectangle to lines with CHAR (prompt) at left column
227 ;; [M-/] restricts rectangle to lines matching REGEXP (prompt)
228 ;; [C-?] Shows a brief list of the above commands.
229
230 ;; [M-C-up] and [M-C-down] scrolls the lines INSIDE the rectangle up
231 ;; and down; lines scrolled outside the top or bottom of the rectangle
232 ;; are lost, but can be recovered using [C-z].
233
234 ;; CUA Global Mark
235 ;; ---------------
236 ;; The final feature provided by CUA is the "global mark", which
237 ;; makes it very easy to copy bits and pieces from the same and other
238 ;; files into the current text. To enable and cancel the global mark,
239 ;; use [S-C-space]. The cursor will blink when the global mark
240 ;; is active. The following commands behave differently when the global
241 ;; mark is set:
242 ;; <ch> All characters (including newlines) you type are inserted
243 ;; at the global mark!
244 ;; [C-x] If you cut a region or rectangle, it is automatically inserted
245 ;; at the global mark, and the global mark is advanced.
246 ;; [C-c] If you copy a region or rectangle, it is immediately inserted
247 ;; at the global mark, and the global mark is advanced.
248 ;; [C-v] Copies a single character to the global mark.
249 ;; [C-d] Moves (i.e. deletes and inserts) a single character to the
250 ;; global mark.
251 ;; [backspace] deletes the character before the global mark, while
252 ;; [delete] deltes the character after the global mark.
253
254 ;; [S-C-space] Jumps to and cancels the global mark.
255 ;; [C-u S-C-space] Cancels the global mark (stays in current buffer).
256
257 ;; [TAB] Indents the current line or rectangle to the column of the
258 ;; global mark.
259
260 ;;; Code:
261
262 ;;; Customization
263
264 (defgroup cua nil
265 "Emulate CUA key bindings including C-x and C-c."
266 :prefix "cua"
267 :group 'editing-basics
268 :group 'convenience
269 :group 'emulations
270 :version "22.1"
271 :link '(emacs-commentary-link :tag "Commentary" "cua-base.el")
272 :link '(emacs-library-link :tag "Lisp File" "cua-base.el"))
273
274 (defcustom cua-enable-cua-keys t
275 "*Enable using C-z, C-x, C-c, and C-v for undo, cut, copy, and paste.
276 If the value is t, these mappings are always enabled. If the value is
277 `shift', these keys are only enabled if the last region was marked with
278 a shifted movement key. If the value is nil, these keys are never
279 enabled."
280 :type '(choice (const :tag "Disabled" nil)
281 (const :tag "Shift region only" shift)
282 (other :tag "Enabled" t))
283 :group 'cua)
284
285 (defcustom cua-highlight-region-shift-only nil
286 "*If non-nil, only highlight region if marked with S-<move>.
287 When this is non-nil, CUA toggles `transient-mark-mode' on when the region
288 is marked using shifted movement keys, and off when the mark is cleared.
289 But when the mark was set using \\[cua-set-mark], transient-mark-mode
290 is not turned on."
291 :type 'boolean
292 :group 'cua)
293
294 (defcustom cua-prefix-override-inhibit-delay
295 (if (featurep 'lisp-float-type) (/ (float 1) (float 5)) nil)
296 "*If non-nil, time in seconds to delay before overriding prefix key.
297 If there is additional input within this time, the prefix key is
298 used as a normal prefix key. So typing a key sequence quickly will
299 inhibit overriding the prefix key.
300 As a special case, if the prefix keys repeated within this time, the
301 first prefix key is discarded, so typing a prefix key twice in quick
302 succession will also inhibit overriding the prefix key.
303 If the value is nil, use a shifted prefix key to inhibit the override."
304 :type '(choice (number :tag "Inhibit delay")
305 (const :tag "No delay" nil))
306 :group 'cua)
307
308 (defcustom cua-keep-region-after-copy nil
309 "If non-nil, don't deselect the region after copying."
310 :type 'boolean
311 :group 'cua)
312
313 (defcustom cua-enable-register-prefix 'not-ctrl-u
314 "*If non-nil, registers are supported via numeric prefix arg.
315 If the value is t, any numeric prefix arg in the range 0 to 9 will be
316 interpreted as a register number.
317 If the value is `not-ctrl-u', using C-u to enter a numeric prefix is not
318 interpreted as a register number.
319 If the value is `ctrl-u-only', only numeric prefix entered with C-u is
320 interpreted as a register number."
321 :type '(choice (const :tag "Disabled" nil)
322 (const :tag "Enabled, but C-u arg is not a register" not-ctrl-u)
323 (const :tag "Enabled, but only for C-u arg" ctrl-u-only)
324 (other :tag "Enabled" t))
325 :group 'cua)
326
327 (defcustom cua-delete-copy-to-register-0 t
328 "*If non-nil, save last deleted region or rectangle to register 0."
329 :type 'boolean
330 :group 'cua)
331
332 (defcustom cua-use-hyper-key nil
333 "*If non-nil, bind rectangle commands to H-... instead of M-....
334 If set to `also', toggle region command is also on C-return.
335 Must be set prior to enabling CUA."
336 :type '(choice (const :tag "Meta key and C-return" nil)
337 (const :tag "Hyper key only" only)
338 (const :tag "Hyper key and C-return" also))
339 :group 'cua)
340
341 (defcustom cua-enable-region-auto-help nil
342 "*If non-nil, automatically show help for active region."
343 :type 'boolean
344 :group 'cua)
345
346 (defcustom cua-enable-modeline-indications nil
347 "*If non-nil, use minor-mode hook to show status in mode line."
348 :type 'boolean
349 :group 'cua)
350
351 (defcustom cua-check-pending-input t
352 "*If non-nil, don't override prefix key if input pending.
353 It is rumoured that `input-pending-p' is unreliable under some window
354 managers, so try setting this to nil, if prefix override doesn't work."
355 :type 'boolean
356 :group 'cua)
357
358
359 ;;; Rectangle Customization
360
361 (defcustom cua-virtual-rectangle-edges t
362 "*If non-nil, rectangles have virtual straight edges.
363 Note that although rectangles are always DISPLAYED with straight edges, the
364 buffer is NOT modified, until you execute a command that actually modifies it.
365 M-p toggles this feature when a rectangle is active."
366 :type 'boolean
367 :group 'cua)
368
369 (defcustom cua-auto-tabify-rectangles 1000
370 "*If non-nil, automatically tabify after rectangle commands.
371 This basically means that `tabify' is applied to all lines that
372 are modified by inserting or deleting a rectangle. If value is
373 an integer, CUA will look for existing tabs in a region around
374 the rectangle, and only do the conversion if any tabs are already
375 present. The number specifies then number of characters before
376 and after the region marked by the rectangle to search."
377 :type '(choice (number :tag "Auto detect (limit)")
378 (const :tag "Disabled" nil)
379 (other :tag "Enabled" t))
380 :group 'cua)
381
382 (defcustom cua-enable-rectangle-auto-help t
383 "*If non-nil, automatically show help for region, rectangle and global mark."
384 :type 'boolean
385 :group 'cua)
386
387 (defface cua-rectangle
388 '((default :inherit region)
389 (((class color)) :foreground "white" :background "maroon"))
390 "*Font used by CUA for highlighting the rectangle."
391 :group 'cua)
392
393 (defface cua-rectangle-noselect
394 '((default :inherit region)
395 (((class color)) :foreground "white" :background "dimgray"))
396 "*Font used by CUA for highlighting the non-selected rectangle lines."
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
408 '((((min-colors 88)(class color)) :foreground "black" :background "yellow1")
409 (((class color)) :foreground "black" :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 (or (and (boundp 'initial-cursor-color) initial-cursor-color)
429 (and (boundp 'initial-frame-alist)
430 (assoc 'cursor-color initial-frame-alist)
431 (cdr (assoc 'cursor-color initial-frame-alist)))
432 (and (boundp 'default-frame-alist)
433 (assoc 'cursor-color default-frame-alist)
434 (cdr (assoc 'cursor-color default-frame-alist)))
435 (frame-parameter nil 'cursor-color)
436 "red")
437 "Normal (non-overwrite) cursor color.
438 Default is to load cursor color from initial or default frame parameters.
439
440 If the value is a COLOR name, then only the `cursor-color' attribute will be
441 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
442 then only the `cursor-type' property will be affected. If the value is
443 a cons (TYPE . COLOR), then both properties are affected."
444 :initialize 'custom-initialize-default
445 :type '(choice
446 (color :tag "Color")
447 (choice :tag "Type"
448 (const :tag "Filled box" box)
449 (const :tag "Vertical bar" bar)
450 (const :tag "Horizontal bar" hbar)
451 (const :tag "Hollow box" hollow))
452 (cons :tag "Color and Type"
453 (choice :tag "Type"
454 (const :tag "Filled box" box)
455 (const :tag "Vertical bar" bar)
456 (const :tag "Horizontal bar" hbar)
457 (const :tag "Hollow box" hollow))
458 (color :tag "Color")))
459 :group 'cua)
460
461 (defcustom cua-read-only-cursor-color "darkgreen"
462 "*Cursor color used in read-only buffers, if non-nil.
463 Only used when `cua-enable-cursor-indications' is non-nil.
464
465 If the value is a COLOR name, then only the `cursor-color' attribute will be
466 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
467 then only the `cursor-type' property will be affected. If the value is
468 a cons (TYPE . COLOR), then both properties are affected."
469 :type '(choice
470 (color :tag "Color")
471 (choice :tag "Type"
472 (const :tag "Filled box" box)
473 (const :tag "Vertical bar" bar)
474 (const :tag "Horizontal bar" hbar)
475 (const :tag "Hollow box" hollow))
476 (cons :tag "Color and Type"
477 (choice :tag "Type"
478 (const :tag "Filled box" box)
479 (const :tag "Vertical bar" bar)
480 (const :tag "Horizontal bar" hbar)
481 (const :tag "Hollow box" hollow))
482 (color :tag "Color")))
483 :group 'cua)
484
485 (defcustom cua-overwrite-cursor-color "yellow"
486 "*Cursor color used when overwrite mode is set, if non-nil.
487 Only used when `cua-enable-cursor-indications' is non-nil.
488
489 If the value is a COLOR name, then only the `cursor-color' attribute will be
490 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
491 then only the `cursor-type' property will be affected. If the value is
492 a cons (TYPE . COLOR), then both properties are affected."
493 :type '(choice
494 (color :tag "Color")
495 (choice :tag "Type"
496 (const :tag "Filled box" box)
497 (const :tag "Vertical bar" bar)
498 (const :tag "Horizontal bar" hbar)
499 (const :tag "Hollow box" hollow))
500 (cons :tag "Color and Type"
501 (choice :tag "Type"
502 (const :tag "Filled box" box)
503 (const :tag "Vertical bar" bar)
504 (const :tag "Horizontal bar" hbar)
505 (const :tag "Hollow box" hollow))
506 (color :tag "Color")))
507 :group 'cua)
508
509 (defcustom cua-global-mark-cursor-color "cyan"
510 "*Indication for active global mark.
511 Will change cursor color to specified color if string.
512 Only used when `cua-enable-cursor-indications' is non-nil.
513
514 If the value is a COLOR name, then only the `cursor-color' attribute will be
515 affected. If the value is a cursor TYPE (one of: box, hollow, bar, or hbar),
516 then only the `cursor-type' property will be affected. If the value is
517 a cons (TYPE . COLOR), then both properties are affected."
518 :type '(choice
519 (color :tag "Color")
520 (choice :tag "Type"
521 (const :tag "Filled box" box)
522 (const :tag "Vertical bar" bar)
523 (const :tag "Horizontal bar" hbar)
524 (const :tag "Hollow box" hollow))
525 (cons :tag "Color and Type"
526 (choice :tag "Type"
527 (const :tag "Filled box" box)
528 (const :tag "Vertical bar" bar)
529 (const :tag "Horizontal bar" hbar)
530 (const :tag "Hollow box" hollow))
531 (color :tag "Color")))
532 :group 'cua)
533
534
535 ;;; Rectangle support is in cua-rect.el
536
537 (autoload 'cua-set-rectangle-mark "cua-rect" nil t nil)
538
539 ;; Stub definitions until it is loaded
540
541 (when (not (featurep 'cua-rect))
542 (defvar cua--rectangle)
543 (setq cua--rectangle nil)
544 (defvar cua--last-killed-rectangle)
545 (setq cua--last-killed-rectangle nil))
546
547
548
549 ;;; Global Mark support is in cua-gmrk.el
550
551 (autoload 'cua-toggle-global-mark "cua-gmrk.el" nil t nil)
552
553 ;; Stub definitions until cua-gmrk.el is loaded
554
555 (when (not (featurep 'cua-gmrk))
556 (defvar cua--global-mark-active)
557 (setq cua--global-mark-active nil))
558
559
560 (provide 'cua-base)
561
562 (eval-when-compile
563 (require 'cua-rect)
564 (require 'cua-gmrk)
565 )
566
567
568 ;;; Low-level Interface
569
570 (defvar cua-inhibit-cua-keys nil
571 "Buffer-local variable that may disable the CUA keymappings.")
572 (make-variable-buffer-local 'cua-inhibit-cua-keys)
573
574 ;;; Aux. variables
575
576 ;; Current region was started using cua-set-mark.
577 (defvar cua--explicit-region-start nil)
578 (make-variable-buffer-local 'cua--explicit-region-start)
579
580 ;; Latest region was started using shifted movement command.
581 (defvar cua--last-region-shifted nil)
582
583 ;; buffer + point prior to current command when rectangle is active
584 ;; checked in post-command hook to see if point was moved
585 (defvar cua--buffer-and-point-before-command nil)
586
587 ;; status string for mode line indications
588 (defvar cua--status-string nil)
589 (make-variable-buffer-local 'cua--status-string)
590
591 (defvar cua--debug nil)
592
593
594 ;;; Prefix key override mechanism
595
596 ;; The prefix override (when mark-active) operates in three substates:
597 ;; [1] Before using a prefix key
598 ;; [2] Immediately after using a prefix key
599 ;; [3] A fraction of a second later
600
601 ;; In state [1], the cua--prefix-override-keymap is active.
602 ;; This keymap binds the C-x and C-c prefix keys to the
603 ;; cua--prefix-override-handler function.
604
605 ;; When a prefix key is typed in state [1], cua--prefix-override-handler
606 ;; will push back the keys already read to the event queue. If input is
607 ;; pending, it changes directly to state [3]. Otherwise, a short timer [T]
608 ;; is started, and it changes to state [2].
609
610 ;; In state [2], the cua--prefix-override-keymap is inactive. Instead the
611 ;; cua--prefix-repeat-keymap is active. This keymap binds C-c C-c and C-x
612 ;; C-x to the cua--prefix-repeat-handler function.
613
614 ;; If the prefix key is repeated in state [2], cua--prefix-repeat-handler
615 ;; will cancel [T], back the keys already read (except for the second prefix
616 ;; keys) to the event queue, and changes to state [3].
617
618 ;; The basic cua--cua-keys-keymap binds [C-x timeout] to kill-region and
619 ;; [C-c timeout] to copy-region-as-kill, so if [T] times out in state [2],
620 ;; the cua--prefix-override-timeout function will push a `timeout' event on
621 ;; the event queue, and changes to state [3].
622
623 ;; In state [3] both cua--prefix-override-keymap and cua--prefix-repeat-keymap
624 ;; are inactive, so the timeout in cua-global-keymap binding is used, or the
625 ;; normal prefix key binding from the global or local map will be used.
626
627 ;; The pre-command hook (executed as a consequence of the timeout or normal
628 ;; prefix key binding) will cancel [T] and change from state [3] back to
629 ;; state [1]. So cua--prefix-override-handler and cua--prefix-repeat-handler
630 ;; are always called with state reset to [1]!
631
632 ;; State [1] is recognized by cua--prefix-override-timer is nil,
633 ;; state [2] is recognized by cua--prefix-override-timer is a timer, and
634 ;; state [3] is recognized by cua--prefix-override-timer is t.
635
636 (defvar cua--prefix-override-timer nil)
637 (defvar cua--prefix-override-length nil)
638
639 (defun cua--prefix-override-replay (arg repeat)
640 (let* ((keys (this-command-keys))
641 (i (length keys))
642 (key (aref keys (1- i))))
643 (setq cua--prefix-override-length (- i repeat))
644 (setq cua--prefix-override-timer
645 (or
646 ;; In state [2], change to state [3]
647 (> repeat 0)
648 ;; In state [1], change directly to state [3]
649 (and cua-check-pending-input (input-pending-p))
650 ;; In state [1], [T] disabled, so change to state [3]
651 (not (numberp cua-prefix-override-inhibit-delay))
652 (<= cua-prefix-override-inhibit-delay 0)
653 ;; In state [1], start [T] and change to state [2]
654 (run-with-timer cua-prefix-override-inhibit-delay nil
655 'cua--prefix-override-timeout)))
656 ;; Don't record this command
657 (setq this-command last-command)
658 ;; Restore the prefix arg
659 (setq prefix-arg arg)
660 (reset-this-command-lengths)
661 ;; Push the key back on the event queue
662 (setq unread-command-events (cons key unread-command-events))))
663
664 (defun cua--prefix-override-handler (arg)
665 "Start timer waiting for prefix key to be followed by another key.
666 Repeating prefix key when region is active works as a single prefix key."
667 (interactive "P")
668 (cua--prefix-override-replay arg 0))
669
670 (defun cua--prefix-repeat-handler (arg)
671 "Repeating prefix key when region is active works as a single prefix key."
672 (interactive "P")
673 (cua--prefix-override-replay arg 1))
674
675 (defun cua--prefix-copy-handler (arg)
676 "Copy region/rectangle, then replay last key."
677 (interactive "P")
678 (if cua--rectangle
679 (cua-copy-rectangle arg)
680 (cua-copy-region arg))
681 (let ((keys (this-single-command-keys)))
682 (setq unread-command-events
683 (cons (aref keys (1- (length keys))) unread-command-events))))
684
685 (defun cua--prefix-cut-handler (arg)
686 "Cut region/rectangle, then replay last key."
687 (interactive "P")
688 (if cua--rectangle
689 (cua-cut-rectangle arg)
690 (cua-cut-region arg))
691 (let ((keys (this-single-command-keys)))
692 (setq unread-command-events
693 (cons (aref keys (1- (length keys))) unread-command-events))))
694
695 (defun cua--prefix-override-timeout ()
696 (setq cua--prefix-override-timer t)
697 (when (= (length (this-command-keys)) cua--prefix-override-length)
698 (setq unread-command-events (cons 'timeout unread-command-events))
699 (if prefix-arg
700 (reset-this-command-lengths)
701 (setq overriding-terminal-local-map nil))
702 (cua--select-keymaps)))
703
704
705 ;;; Aux. functions
706
707 (defun cua--fallback ()
708 ;; Execute original command
709 (setq this-command this-original-command)
710 (call-interactively this-command))
711
712 (defun cua--keep-active ()
713 (setq mark-active t
714 deactivate-mark nil))
715
716 (defun cua--deactivate (&optional now)
717 (setq cua--explicit-region-start nil)
718 (if (not now)
719 (setq deactivate-mark t)
720 (setq mark-active nil)
721 (run-hooks 'deactivate-mark-hook)))
722
723
724 ;; The current register prefix
725 (defvar cua--register nil)
726
727 (defun cua--prefix-arg (arg)
728 (setq cua--register
729 (and cua-enable-register-prefix
730 (integerp arg) (>= arg 0) (< arg 10)
731 (let* ((prefix (aref (this-command-keys) 0))
732 (ctrl-u-prefix (and (integerp prefix)
733 (= prefix ?\C-u))))
734 (cond
735 ((eq cua-enable-register-prefix 'not-ctrl-u)
736 (not ctrl-u-prefix))
737 ((eq cua-enable-register-prefix 'ctrl-u-only)
738 ctrl-u-prefix)
739 (t t)))
740 (+ arg ?0)))
741 (if cua--register nil arg))
742
743
744 ;;; Region specific commands
745
746 (defvar cua--last-deleted-region-pos nil)
747 (defvar cua--last-deleted-region-text nil)
748
749 (defun cua-delete-region ()
750 "Delete the active region.
751 Save a copy in register 0 if `cua-delete-copy-to-register-0' is non-nil."
752 (interactive)
753 (let ((start (mark)) (end (point)))
754 (or (<= start end)
755 (setq start (prog1 end (setq end start))))
756 (setq cua--last-deleted-region-text (buffer-substring start end))
757 (if cua-delete-copy-to-register-0
758 (set-register ?0 cua--last-deleted-region-text))
759 (delete-region start end)
760 (setq cua--last-deleted-region-pos
761 (cons (current-buffer)
762 (and (consp buffer-undo-list)
763 (car buffer-undo-list))))
764 (cua--deactivate)
765 (/= start end)))
766
767 (defun cua-replace-region ()
768 "Replace the active region with the character you type."
769 (interactive)
770 (let ((not-empty (cua-delete-region)))
771 (unless (eq this-original-command this-command)
772 (let ((overwrite-mode
773 (and overwrite-mode
774 not-empty
775 (not (eq this-original-command 'self-insert-command)))))
776 (cua--fallback)))))
777
778 (defun cua-copy-region (arg)
779 "Copy the region to the kill ring.
780 With numeric prefix arg, copy to register 0-9 instead."
781 (interactive "P")
782 (setq arg (cua--prefix-arg arg))
783 (setq cua--last-killed-rectangle nil)
784 (let ((start (mark)) (end (point)))
785 (or (<= start end)
786 (setq start (prog1 end (setq end start))))
787 (cond
788 (cua--register
789 (copy-to-register cua--register start end nil))
790 ((eq this-original-command 'clipboard-kill-ring-save)
791 (clipboard-kill-ring-save start end))
792 (t
793 (copy-region-as-kill start end)))
794 (if cua-keep-region-after-copy
795 (cua--keep-active)
796 (cua--deactivate))))
797
798 (defun cua-cut-region (arg)
799 "Cut the region and copy to the kill ring.
800 With numeric prefix arg, copy to register 0-9 instead."
801 (interactive "P")
802 (setq cua--last-killed-rectangle nil)
803 (if buffer-read-only
804 (cua-copy-region arg)
805 (setq arg (cua--prefix-arg arg))
806 (let ((start (mark)) (end (point)))
807 (or (<= start end)
808 (setq start (prog1 end (setq end start))))
809 (cond
810 (cua--register
811 (copy-to-register cua--register start end t))
812 ((eq this-original-command 'clipboard-kill-region)
813 (clipboard-kill-region start end))
814 (t
815 (kill-region start end))))
816 (cua--deactivate)))
817
818 ;;; Generic commands for regions, rectangles, and global marks
819
820 (defun cua-cancel ()
821 "Cancel the active region, rectangle, or global mark."
822 (interactive)
823 (setq mark-active nil)
824 (setq cua--explicit-region-start nil)
825 (if (fboundp 'cua--cancel-rectangle)
826 (cua--cancel-rectangle)))
827
828 (defun cua-paste (arg)
829 "Paste last cut or copied region or rectangle.
830 An active region is deleted before executing the command.
831 With numeric prefix arg, paste from register 0-9 instead.
832 If global mark is active, copy from register or one character."
833 (interactive "P")
834 (setq arg (cua--prefix-arg arg))
835 (let ((regtxt (and cua--register (get-register cua--register)))
836 (count (prefix-numeric-value arg))
837 paste-column paste-lines)
838 (cond
839 ((and cua--register (not regtxt))
840 (message "Nothing in register %c" cua--register))
841 (cua--global-mark-active
842 (if regtxt
843 (cua--insert-at-global-mark regtxt)
844 (when (not (eobp))
845 (cua--insert-at-global-mark (buffer-substring (point) (+ (point) count)))
846 (forward-char count))))
847 (buffer-read-only
848 (message "Cannot paste into a read-only buffer"))
849 (t
850 ;; Must save register here, since delete may override reg 0.
851 (if mark-active
852 ;; Before a yank command, make sure we don't yank
853 ;; the same region that we are going to delete.
854 ;; That would make yank a no-op.
855 (if cua--rectangle
856 (progn
857 (goto-char (min (mark) (point)))
858 (setq paste-column (cua--rectangle-left))
859 (setq paste-lines (cua--delete-rectangle))
860 (if (= paste-lines 1)
861 (setq paste-lines nil))) ;; paste all
862 (if (string= (buffer-substring (point) (mark))
863 (car kill-ring))
864 (current-kill 1))
865 (cua-delete-region)))
866 (cond
867 (regtxt
868 (cond
869 ((consp regtxt) (cua--insert-rectangle regtxt))
870 ((stringp regtxt) (insert-for-yank regtxt))
871 (t (message "Unknown data in register %c" cua--register))))
872 ((and cua--last-killed-rectangle
873 (eq (and kill-ring (car kill-ring)) (car cua--last-killed-rectangle)))
874 (let ((pt (point)))
875 (when (not (eq buffer-undo-list t))
876 (setq this-command 'cua--paste-rectangle)
877 (undo-boundary)
878 (setq buffer-undo-list (cons pt buffer-undo-list)))
879 (cua--insert-rectangle (cdr cua--last-killed-rectangle)
880 nil paste-column paste-lines)
881 (if arg (goto-char pt))))
882 ((eq this-original-command 'clipboard-yank)
883 (clipboard-yank))
884 (t (yank arg)))))))
885
886 (defun cua-paste-pop (arg)
887 "Replace a just-pasted text or rectangle with a different text.
888 See `yank-pop' for details."
889 (interactive "P")
890 (if (eq last-command 'cua--paste-rectangle)
891 (progn
892 (undo)
893 (yank arg))
894 (yank-pop (prefix-numeric-value arg))))
895
896 (defun cua-exchange-point-and-mark (arg)
897 "Exchanges point and mark, but don't activate the mark.
898 Activates the mark if a prefix argument is given."
899 (interactive "P")
900 (if arg
901 (setq mark-active t)
902 (let (mark-active)
903 (exchange-point-and-mark)
904 (if cua--rectangle
905 (cua--rectangle-corner 0)))))
906
907 ;; Typed text that replaced the highlighted region.
908 (defvar cua--repeat-replace-text nil)
909
910 (defun cua-repeat-replace-region (arg)
911 "Repeat replacing text of highlighted region with typed text.
912 Searches for the next stretch of text identical to the region last
913 replaced by typing text over it and replaces it with the same stretch
914 of text."
915 (interactive "P")
916 (when cua--last-deleted-region-pos
917 (save-excursion
918 (save-restriction
919 (set-buffer (car cua--last-deleted-region-pos))
920 (widen)
921 ;; Find the text that replaced the region via the undo list.
922 (let ((ul buffer-undo-list)
923 (elt (cdr cua--last-deleted-region-pos))
924 u s e)
925 (when elt
926 (while (consp ul)
927 (setq u (car ul) ul (cdr ul))
928 (cond
929 ((eq u elt) ;; got it
930 (setq ul nil))
931 ((and (consp u) (integerp (car u)) (integerp (cdr u)))
932 (if (and s (= (cdr u) s))
933 (setq s (car u))
934 (setq s (car u) e (cdr u)))))))
935 (setq cua--repeat-replace-text
936 (cond ((and s e (<= s e) (= s (mark t)))
937 (buffer-substring-no-properties s e))
938 ((and (null s) (eq u elt)) ;; nothing inserted
939 "")
940 (t
941 (message "Cannot locate replacement text")
942 nil))))))
943 (setq cua--last-deleted-region-pos nil))
944 (if (and cua--last-deleted-region-text
945 cua--repeat-replace-text
946 (search-forward cua--last-deleted-region-text nil t nil))
947 (replace-match cua--repeat-replace-text arg t)))
948
949 (defun cua-help-for-region (&optional help)
950 "Show region specific help in echo area."
951 (interactive)
952 (message
953 (concat (if help "C-?:help " "")
954 "C-z:undo C-x:cut C-c:copy C-v:paste S-ret:rect")))
955
956
957 ;;; Shift activated / extended region
958
959 (defun cua-set-mark (&optional arg)
960 "Set mark at where point is, clear mark, or jump to mark.
961
962 With no prefix argument, clear mark if already set. Otherwise, set
963 mark, and push old mark position on local mark ring; also push mark on
964 global mark ring if last mark was set in another buffer.
965
966 With argument, jump to mark, and pop a new position for mark off
967 the local mark ring \(this does not affect the global mark ring\).
968 Use \\[pop-global-mark] to jump to a mark off the global mark ring
969 \(see `pop-global-mark'\).
970
971 Repeating the command without the prefix jumps to the next position
972 off the local \(or global\) mark ring.
973
974 With a double \\[universal-argument] prefix argument, unconditionally set mark."
975 (interactive "P")
976 (cond
977 ((and (consp arg) (> (prefix-numeric-value arg) 4))
978 (push-mark-command nil))
979 ((eq last-command 'pop-to-mark-command)
980 (setq this-command 'pop-to-mark-command)
981 (pop-to-mark-command))
982 ((and (eq last-command 'pop-global-mark) (not arg))
983 (setq this-command 'pop-global-mark)
984 (pop-global-mark))
985 (arg
986 (setq this-command 'pop-to-mark-command)
987 (pop-to-mark-command))
988 (mark-active
989 (cua--deactivate)
990 (message "Mark Cleared"))
991 (t
992 (push-mark-command nil nil)
993 (setq cua--explicit-region-start t)
994 (setq cua--last-region-shifted nil)
995 (if cua-enable-region-auto-help
996 (cua-help-for-region t)))))
997
998 ;;; Scrolling commands which does not signal errors at top/bottom
999 ;;; of buffer at first key-press (instead moves to top/bottom
1000 ;;; of buffer).
1001
1002 (defun cua-scroll-up (&optional arg)
1003 "Scroll text of current window upward ARG lines; or near full screen if no ARG.
1004 If window cannot be scrolled further, move cursor to bottom line instead.
1005 A near full screen is `next-screen-context-lines' less than a full screen.
1006 Negative ARG means scroll downward.
1007 If ARG is the atom `-', scroll downward by nearly full screen."
1008 (interactive "P")
1009 (cond
1010 ((eq arg '-) (cua-scroll-down nil))
1011 ((< (prefix-numeric-value arg) 0)
1012 (cua-scroll-down (- (prefix-numeric-value arg))))
1013 ((eobp)
1014 (scroll-up arg)) ; signal error
1015 (t
1016 (condition-case nil
1017 (scroll-up arg)
1018 (end-of-buffer (goto-char (point-max)))))))
1019
1020 (put 'cua-scroll-up 'CUA 'move)
1021
1022 (defun cua-scroll-down (&optional arg)
1023 "Scroll text of current window downward ARG lines; or near full screen if no ARG.
1024 If window cannot be scrolled further, move cursor to top line instead.
1025 A near full screen is `next-screen-context-lines' less than a full screen.
1026 Negative ARG means scroll upward.
1027 If ARG is the atom `-', scroll upward by nearly full screen."
1028 (interactive "P")
1029 (cond
1030 ((eq arg '-) (cua-scroll-up nil))
1031 ((< (prefix-numeric-value arg) 0)
1032 (cua-scroll-up (- (prefix-numeric-value arg))))
1033 ((bobp)
1034 (scroll-down arg)) ; signal error
1035 (t
1036 (condition-case nil
1037 (scroll-down arg)
1038 (beginning-of-buffer (goto-char (point-min)))))))
1039
1040 (put 'cua-scroll-down 'CUA 'move)
1041
1042 ;;; Cursor indications
1043
1044 (defun cua--update-indications ()
1045 (let* ((cursor
1046 (cond
1047 ((and cua--global-mark-active
1048 cua-global-mark-cursor-color)
1049 cua-global-mark-cursor-color)
1050 ((and buffer-read-only
1051 cua-read-only-cursor-color)
1052 cua-read-only-cursor-color)
1053 ((and cua-overwrite-cursor-color overwrite-mode)
1054 cua-overwrite-cursor-color)
1055 (t cua-normal-cursor-color)))
1056 (color (if (consp cursor) (cdr cursor) cursor))
1057 (type (if (consp cursor) (car cursor) cursor)))
1058 (if (and color
1059 (stringp color)
1060 (not (equal color (frame-parameter nil 'cursor-color))))
1061 (set-cursor-color color))
1062 (if (and type
1063 (symbolp type)
1064 (not (eq type default-cursor-type)))
1065 (setq default-cursor-type type))))
1066
1067
1068 ;;; Pre-command hook
1069
1070 (defun cua--pre-command-handler-1 ()
1071 (let ((movement (eq (get this-command 'CUA) 'move)))
1072
1073 ;; Cancel prefix key timeout if user enters another key.
1074 (when cua--prefix-override-timer
1075 (if (timerp cua--prefix-override-timer)
1076 (cancel-timer cua--prefix-override-timer))
1077 (setq cua--prefix-override-timer nil))
1078
1079 ;; Handle shifted cursor keys and other movement commands.
1080 ;; If region is not active, region is activated if key is shifted.
1081 ;; If region is active, region is cancelled if key is unshifted (and region not started with C-SPC).
1082 ;; If rectangle is active, expand rectangle in specified direction and ignore the movement.
1083 (if movement
1084 (cond
1085 ((if window-system
1086 (memq 'shift (event-modifiers
1087 (aref (this-single-command-raw-keys) 0)))
1088 (or
1089 (memq 'shift (event-modifiers
1090 (aref (this-single-command-keys) 0)))
1091 ;; See if raw escape sequence maps to a shifted event, e.g. S-up or C-S-home.
1092 (and (boundp 'local-function-key-map)
1093 (terminal-local-value 'function-key-map nil)
1094 (let ((ev (lookup-key (terminal-local-value 'function-key-map nil)
1095 (this-single-command-raw-keys))))
1096 (and (vector ev)
1097 (symbolp (setq ev (aref ev 0)))
1098 (string-match "S-" (symbol-name ev)))))))
1099 (unless mark-active
1100 (push-mark-command nil t))
1101 (setq cua--last-region-shifted t)
1102 (setq cua--explicit-region-start nil))
1103 ((or cua--explicit-region-start cua--rectangle)
1104 (unless mark-active
1105 (push-mark-command nil nil)))
1106 (t
1107 ;; If we set mark-active to nil here, the region highlight will not be
1108 ;; removed by the direct_output_ commands.
1109 (setq deactivate-mark t)))
1110
1111 ;; Handle delete-selection property on other commands
1112 (if (and mark-active (not deactivate-mark))
1113 (let* ((ds (or (get this-command 'delete-selection)
1114 (get this-command 'pending-delete)))
1115 (nc (cond
1116 ((not ds) nil)
1117 ((eq ds 'yank)
1118 'cua-paste)
1119 ((eq ds 'kill)
1120 (if cua--rectangle
1121 'cua-copy-rectangle
1122 'cua-copy-region))
1123 ((eq ds 'supersede)
1124 (if cua--rectangle
1125 'cua-delete-rectangle
1126 'cua-delete-region))
1127 (t
1128 (if cua--rectangle
1129 'cua-delete-rectangle ;; replace?
1130 'cua-replace-region)))))
1131 (if nc
1132 (setq this-original-command this-command
1133 this-command nc)))))
1134
1135 ;; Detect extension of rectangles by mouse or other movement
1136 (setq cua--buffer-and-point-before-command
1137 (if cua--rectangle (cons (current-buffer) (point))))))
1138
1139 (defun cua--pre-command-handler ()
1140 (when cua-mode
1141 (condition-case nil
1142 (cua--pre-command-handler-1)
1143 (error nil))))
1144
1145 ;;; Post-command hook
1146
1147 (defun cua--post-command-handler-1 ()
1148 (when cua--global-mark-active
1149 (cua--global-mark-post-command))
1150 (when (fboundp 'cua--rectangle-post-command)
1151 (cua--rectangle-post-command))
1152 (setq cua--buffer-and-point-before-command nil)
1153 (if (or (not mark-active) deactivate-mark)
1154 (setq cua--explicit-region-start nil))
1155
1156 ;; Debugging
1157 (if cua--debug
1158 (cond
1159 (cua--rectangle (cua--rectangle-assert))
1160 (mark-active (message "Mark=%d Point=%d Expl=%s"
1161 (mark t) (point) cua--explicit-region-start))))
1162
1163 ;; Disable transient-mark-mode if rectangle active in current buffer.
1164 (if (not (window-minibuffer-p (selected-window)))
1165 (setq transient-mark-mode (and (not cua--rectangle)
1166 (if cua-highlight-region-shift-only
1167 (not cua--explicit-region-start)
1168 t))))
1169 (if cua-enable-cursor-indications
1170 (cua--update-indications))
1171
1172 (cua--select-keymaps))
1173
1174 (defun cua--post-command-handler ()
1175 (when cua-mode
1176 (condition-case nil
1177 (cua--post-command-handler-1)
1178 (error nil))))
1179
1180
1181 ;;; Keymaps
1182
1183 (defun cua--M/H-key (map key fct)
1184 ;; bind H-KEY or M-KEY to FCT in MAP
1185 (if (eq key 'space) (setq key ?\s))
1186 (unless (listp key) (setq key (list key)))
1187 (define-key map (vector (cons (if cua-use-hyper-key 'hyper 'meta) key)) fct))
1188
1189 (defun cua--self-insert-char-p (def)
1190 ;; Return DEF if current key sequence is self-inserting in
1191 ;; global-map.
1192 (if (memq (global-key-binding (this-single-command-keys))
1193 '(self-insert-command self-insert-iso))
1194 def nil))
1195
1196 (defvar cua-global-keymap (make-sparse-keymap)
1197 "Global keymap for cua-mode; users may add to this keymap.")
1198
1199 (defvar cua--cua-keys-keymap (make-sparse-keymap))
1200 (defvar cua--prefix-override-keymap (make-sparse-keymap))
1201 (defvar cua--prefix-repeat-keymap (make-sparse-keymap))
1202 (defvar cua--global-mark-keymap (make-sparse-keymap)) ; Initalized when cua-gmrk.el is loaded
1203 (defvar cua--rectangle-keymap (make-sparse-keymap)) ; Initalized when cua-rect.el is loaded
1204 (defvar cua--region-keymap (make-sparse-keymap))
1205
1206 (defvar cua--ena-cua-keys-keymap nil)
1207 (defvar cua--ena-prefix-override-keymap nil)
1208 (defvar cua--ena-prefix-repeat-keymap nil)
1209 (defvar cua--ena-region-keymap nil)
1210 (defvar cua--ena-global-mark-keymap nil)
1211
1212 (defvar cua--keymap-alist
1213 `((cua--ena-prefix-override-keymap . ,cua--prefix-override-keymap)
1214 (cua--ena-prefix-repeat-keymap . ,cua--prefix-repeat-keymap)
1215 (cua--ena-cua-keys-keymap . ,cua--cua-keys-keymap)
1216 (cua--ena-global-mark-keymap . ,cua--global-mark-keymap)
1217 (cua--rectangle . ,cua--rectangle-keymap)
1218 (cua--ena-region-keymap . ,cua--region-keymap)
1219 (cua-mode . ,cua-global-keymap)))
1220
1221 (defun cua--select-keymaps ()
1222 ;; Setup conditions for selecting the proper keymaps in cua--keymap-alist.
1223 (setq cua--ena-region-keymap
1224 (and mark-active (not deactivate-mark)))
1225 (setq cua--ena-prefix-override-keymap
1226 (and cua--ena-region-keymap
1227 cua-enable-cua-keys
1228 (not cua-inhibit-cua-keys)
1229 (or (eq cua-enable-cua-keys t)
1230 (not cua--explicit-region-start))
1231 (not executing-kbd-macro)
1232 (not cua--prefix-override-timer)))
1233 (setq cua--ena-prefix-repeat-keymap
1234 (and cua--ena-region-keymap
1235 (timerp cua--prefix-override-timer)))
1236 (setq cua--ena-cua-keys-keymap
1237 (and cua-enable-cua-keys
1238 (not cua-inhibit-cua-keys)
1239 (or (eq cua-enable-cua-keys t)
1240 cua--last-region-shifted)))
1241 (setq cua--ena-global-mark-keymap
1242 (and cua--global-mark-active
1243 (not (window-minibuffer-p)))))
1244
1245 (defvar cua--keymaps-initalized nil)
1246
1247 (defun cua--init-keymaps ()
1248 (unless (eq cua-use-hyper-key 'only)
1249 (define-key cua-global-keymap [(control return)] 'cua-set-rectangle-mark))
1250 (when cua-use-hyper-key
1251 (cua--M/H-key cua-global-keymap 'space 'cua-set-rectangle-mark)
1252 (define-key cua-global-keymap [(hyper mouse-1)] 'cua-mouse-set-rectangle-mark))
1253
1254 (define-key cua-global-keymap [(shift control ?\s)] 'cua-toggle-global-mark)
1255
1256 ;; replace region with rectangle or element on kill ring
1257 (define-key cua-global-keymap [remap yank] 'cua-paste)
1258 (define-key cua-global-keymap [remap clipboard-yank] 'cua-paste)
1259 ;; replace current yank with previous kill ring element
1260 (define-key cua-global-keymap [remap yank-pop] 'cua-paste-pop)
1261 ;; set mark
1262 (define-key cua-global-keymap [remap set-mark-command] 'cua-set-mark)
1263
1264 ;; scrolling
1265 (define-key cua-global-keymap [remap scroll-up] 'cua-scroll-up)
1266 (define-key cua-global-keymap [remap scroll-down] 'cua-scroll-down)
1267
1268 (define-key cua--cua-keys-keymap [(control x) timeout] 'kill-region)
1269 (define-key cua--cua-keys-keymap [(control c) timeout] 'copy-region-as-kill)
1270 (define-key cua--cua-keys-keymap [(control z)] 'undo)
1271 (define-key cua--cua-keys-keymap [(control v)] 'yank)
1272 (define-key cua--cua-keys-keymap [(meta v)] 'cua-repeat-replace-region)
1273 (define-key cua--cua-keys-keymap [remap exchange-point-and-mark] 'cua-exchange-point-and-mark)
1274
1275 (define-key cua--prefix-override-keymap [(control x)] 'cua--prefix-override-handler)
1276 (define-key cua--prefix-override-keymap [(control c)] 'cua--prefix-override-handler)
1277
1278 (define-key cua--prefix-repeat-keymap [(control x) (control x)] 'cua--prefix-repeat-handler)
1279 (define-key cua--prefix-repeat-keymap [(control x) up] 'cua--prefix-cut-handler)
1280 (define-key cua--prefix-repeat-keymap [(control x) down] 'cua--prefix-cut-handler)
1281 (define-key cua--prefix-repeat-keymap [(control x) left] 'cua--prefix-cut-handler)
1282 (define-key cua--prefix-repeat-keymap [(control x) right] 'cua--prefix-cut-handler)
1283 (define-key cua--prefix-repeat-keymap [(control c) (control c)] 'cua--prefix-repeat-handler)
1284 (define-key cua--prefix-repeat-keymap [(control c) up] 'cua--prefix-copy-handler)
1285 (define-key cua--prefix-repeat-keymap [(control c) down] 'cua--prefix-copy-handler)
1286 (define-key cua--prefix-repeat-keymap [(control c) left] 'cua--prefix-copy-handler)
1287 (define-key cua--prefix-repeat-keymap [(control c) right] 'cua--prefix-copy-handler)
1288
1289 ;; Enable shifted fallbacks for C-x and C-c when region is active
1290 (define-key cua--region-keymap [(shift control x)] 'Control-X-prefix)
1291 (define-key cua--region-keymap [(shift control c)] 'mode-specific-command-prefix)
1292 ;; replace current region
1293 (define-key cua--region-keymap [remap self-insert-command] 'cua-replace-region)
1294 (define-key cua--region-keymap [remap self-insert-iso] 'cua-replace-region)
1295 (define-key cua--region-keymap [remap insert-register] 'cua-replace-region)
1296 (define-key cua--region-keymap [remap newline-and-indent] 'cua-replace-region)
1297 (define-key cua--region-keymap [remap newline] 'cua-replace-region)
1298 (define-key cua--region-keymap [remap open-line] 'cua-replace-region)
1299 ;; delete current region
1300 (define-key cua--region-keymap [remap delete-backward-char] 'cua-delete-region)
1301 (define-key cua--region-keymap [remap backward-delete-char] 'cua-delete-region)
1302 (define-key cua--region-keymap [remap backward-delete-char-untabify] 'cua-delete-region)
1303 (define-key cua--region-keymap [remap delete-char] 'cua-delete-region)
1304 ;; kill region
1305 (define-key cua--region-keymap [remap kill-region] 'cua-cut-region)
1306 (define-key cua--region-keymap [remap clipboard-kill-region] 'cua-cut-region)
1307 ;; copy region
1308 (define-key cua--region-keymap [remap copy-region-as-kill] 'cua-copy-region)
1309 (define-key cua--region-keymap [remap kill-ring-save] 'cua-copy-region)
1310 (define-key cua--region-keymap [remap clipboard-kill-ring-save] 'cua-copy-region)
1311 ;; cancel current region/rectangle
1312 (define-key cua--region-keymap [remap keyboard-escape-quit] 'cua-cancel)
1313 (define-key cua--region-keymap [remap keyboard-quit] 'cua-cancel)
1314 )
1315
1316
1317 ;; Setup standard movement commands to be recognized by CUA.
1318
1319 (dolist (cmd
1320 '(forward-char backward-char
1321 next-line previous-line
1322 forward-word backward-word
1323 end-of-line beginning-of-line
1324 move-end-of-line move-beginning-of-line
1325 end-of-buffer beginning-of-buffer
1326 scroll-up scroll-down
1327 forward-sentence backward-sentence
1328 forward-paragraph backward-paragraph))
1329 (put cmd 'CUA 'move))
1330
1331 ;; State prior to enabling cua-mode
1332 ;; Value is a list with the following elements:
1333 ;; transient-mark-mode
1334 ;; delete-selection-mode
1335 ;; pc-selection-mode
1336
1337 (defvar cua--saved-state nil)
1338
1339 ;;;###autoload
1340 (define-minor-mode cua-mode
1341 "Toggle CUA key-binding mode.
1342 When enabled, using shifted movement keys will activate the
1343 region (and highlight the region using `transient-mark-mode'),
1344 and typed text replaces the active selection.
1345
1346 Also when enabled, you can use C-z, C-x, C-c, and C-v to undo,
1347 cut, copy, and paste in addition to the normal Emacs bindings.
1348 The C-x and C-c keys only do cut and copy when the region is
1349 active, so in most cases, they do not conflict with the normal
1350 function of these prefix keys.
1351
1352 If you really need to perform a command which starts with one of
1353 the prefix keys even when the region is active, you have three
1354 options:
1355 - press the prefix key twice very quickly (within 0.2 seconds),
1356 - press the prefix key and the following key within 0.2 seconds, or
1357 - use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c.
1358
1359 You can customize `cua-enable-cua-keys' to completely disable the
1360 CUA bindings, or `cua-prefix-override-inhibit-delay' to change
1361 the prefix fallback behavior."
1362 :global t
1363 :group 'cua
1364 :set-after '(cua-enable-modeline-indications cua-use-hyper-key)
1365 :require 'cua-base
1366 :link '(emacs-commentary-link "cua-base.el")
1367 (setq mark-even-if-inactive t)
1368 (setq highlight-nonselected-windows nil)
1369
1370 (unless cua--keymaps-initalized
1371 (cua--init-keymaps)
1372 (setq cua--keymaps-initalized t))
1373
1374 (if cua-mode
1375 (progn
1376 (add-hook 'pre-command-hook 'cua--pre-command-handler)
1377 (add-hook 'post-command-hook 'cua--post-command-handler)
1378 (if (and cua-enable-modeline-indications (not (assoc 'cua-mode minor-mode-alist)))
1379 (setq minor-mode-alist (cons '(cua-mode cua--status-string) minor-mode-alist)))
1380 (if cua-enable-cursor-indications
1381 (cua--update-indications)))
1382
1383 (remove-hook 'pre-command-hook 'cua--pre-command-handler)
1384 (remove-hook 'post-command-hook 'cua--post-command-handler))
1385
1386 (if (not cua-mode)
1387 (setq emulation-mode-map-alists (delq 'cua--keymap-alist emulation-mode-map-alists))
1388 (add-to-ordered-list 'emulation-mode-map-alists 'cua--keymap-alist 400)
1389 (cua--select-keymaps))
1390
1391 (cond
1392 (cua-mode
1393 (setq cua--saved-state
1394 (list
1395 transient-mark-mode
1396 (and (boundp 'delete-selection-mode) delete-selection-mode)
1397 (and (boundp 'pc-selection-mode) pc-selection-mode)))
1398 (if (and (boundp 'delete-selection-mode) delete-selection-mode)
1399 (delete-selection-mode -1))
1400 (if (and (boundp 'pc-selection-mode) pc-selection-mode)
1401 (pc-selection-mode -1))
1402 (cua--deactivate)
1403 (setq transient-mark-mode (and cua-mode
1404 (if cua-highlight-region-shift-only
1405 (not cua--explicit-region-start)
1406 t))))
1407 (cua--saved-state
1408 (setq transient-mark-mode (car cua--saved-state))
1409 (if (nth 1 cua--saved-state)
1410 (delete-selection-mode 1))
1411 (if (nth 2 cua--saved-state)
1412 (pc-selection-mode 1))
1413 (if (interactive-p)
1414 (message "CUA mode disabled.%s%s%s%s"
1415 (if (nth 1 cua--saved-state) " Delete-Selection" "")
1416 (if (and (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " and" "")
1417 (if (nth 2 cua--saved-state) " PC-Selection" "")
1418 (if (or (nth 1 cua--saved-state) (nth 2 cua--saved-state)) " enabled" "")))
1419 (setq cua--saved-state nil))))
1420
1421
1422 ;;;###autoload
1423 (defun cua-selection-mode (arg)
1424 "Enable CUA selection mode without the C-z/C-x/C-c/C-v bindings."
1425 (interactive "P")
1426 (setq-default cua-enable-cua-keys nil)
1427 (cua-mode arg))
1428
1429
1430 (defun cua-debug ()
1431 "Toggle CUA debugging."
1432 (interactive)
1433 (setq cua--debug (not cua--debug)))
1434
1435 ;; Install run-time check for older versions of CUA-mode which does not
1436 ;; work with GNU Emacs version 22.1 and newer.
1437 ;;
1438 ;; Except for version 1.2, all of the 1.x and 2.x version of cua-mode
1439 ;; provided the `CUA-mode' feature. Since this is no longer true,
1440 ;; we can warn the user if the `CUA-mode' feature is ever provided.
1441
1442 ;;;###autoload (eval-after-load 'CUA-mode
1443 ;;;###autoload '(error (concat "\n\n"
1444 ;;;###autoload "CUA-mode is now part of the standard GNU Emacs distribution,\n"
1445 ;;;###autoload "so you may now enable and customize CUA via the Options menu.\n\n"
1446 ;;;###autoload "You have loaded an older version of CUA-mode which does\n"
1447 ;;;###autoload "not work correctly with this version of GNU Emacs.\n\n"
1448 ;;;###autoload (if user-init-file (concat
1449 ;;;###autoload "To correct this, remove the loading and customization of the\n"
1450 ;;;###autoload "old version from the " user-init-file " file.\n\n")))))
1451
1452 (provide 'cua)
1453
1454 ;;; arch-tag: 21fb6289-ba25-4fee-bfdc-f9fb351acf05
1455 ;;; cua-base.el ends here