cua-base.el 52 KB

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