cua-base.el 61 KB

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