cua-rect.el 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. ;;; cua-rect.el --- CUA unified rectangle support
  2. ;; Copyright (C) 1997-2017 Free Software Foundation, Inc.
  3. ;; Author: Kim F. Storm <storm@cua.dk>
  4. ;; Keywords: keyboard emulations convenience CUA
  5. ;; Package: cua-base
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Acknowledgments
  18. ;; The rectangle handling and display code borrows from the standard
  19. ;; GNU emacs rect.el package and the rect-mark.el package by Rick
  20. ;; Sladkey <jrs@world.std.com>.
  21. ;;; Commentary:
  22. ;;; Code:
  23. (require 'cua-base)
  24. ;;; Rectangle support
  25. (require 'rect)
  26. ;; If non-nil, restrict current region to this rectangle.
  27. ;; Value is a vector [top bot left right corner ins virt select].
  28. ;; CORNER specifies currently active corner 0=t/l 1=t/r 2=b/l 3=b/r.
  29. ;; INS specifies whether to insert on left(nil) or right(t) side.
  30. ;; If VIRT is non-nil, virtual straight edges are enabled.
  31. ;; If SELECT is a regexp, only lines starting with that regexp are affected.")
  32. (defvar cua--rectangle nil)
  33. (make-variable-buffer-local 'cua--rectangle)
  34. ;; Most recent rectangle geometry. Note: car is buffer.
  35. (defvar cua--last-rectangle nil)
  36. ;; Rectangle restored by undo.
  37. (defvar cua--restored-rectangle nil)
  38. ;; Last rectangle copied/killed; nil if last kill was not a rectangle.
  39. (defvar cua--last-killed-rectangle nil)
  40. ;; List of overlays used to display current rectangle.
  41. (defvar cua--rectangle-overlays nil)
  42. (make-variable-buffer-local 'cua--rectangle-overlays)
  43. (put 'cua--rectangle-overlays 'permanent-local t)
  44. (defvar cua--overlay-keymap
  45. (let ((map (make-sparse-keymap)))
  46. (define-key map "\r" 'cua-rotate-rectangle)))
  47. (defvar cua--virtual-edges-debug nil)
  48. ;; Undo rectangle commands.
  49. (defvar cua--rect-undo-set-point nil)
  50. (defun cua--rectangle-undo-boundary ()
  51. (when (listp buffer-undo-list)
  52. (let ((s (cua--rect-start-position))
  53. (e (cua--rect-end-position)))
  54. (undo-boundary)
  55. (push (list 'apply 0 s e
  56. 'cua--rect-undo-handler
  57. (copy-sequence cua--rectangle) t s e)
  58. buffer-undo-list))))
  59. (defun cua--rect-undo-handler (rect on s e)
  60. (if (setq on (not on))
  61. (setq cua--rect-undo-set-point s)
  62. (setq cua--restored-rectangle (copy-sequence rect))
  63. (setq cua--buffer-and-point-before-command nil))
  64. (push (list 'apply 0 s (if on e s)
  65. 'cua--rect-undo-handler rect on s e)
  66. buffer-undo-list))
  67. ;;;###autoload
  68. (define-minor-mode cua-rectangle-mark-mode
  69. "Toggle the region as rectangular.
  70. Activates the region if needed. Only lasts until the region is deactivated."
  71. :keymap cua--rectangle-keymap
  72. (cond
  73. (cua-rectangle-mark-mode
  74. (add-hook 'deactivate-mark-hook
  75. (lambda () (cua-rectangle-mark-mode -1)))
  76. (add-hook 'post-command-hook #'cua--rectangle-post-command nil t)
  77. (cua-set-rectangle-mark))
  78. (t
  79. (cua--deactivate-rectangle)
  80. (remove-hook 'post-command-hook #'cua--rectangle-post-command t))))
  81. ;;; Rectangle geometry
  82. (defun cua--rectangle-top (&optional val)
  83. ;; Top of CUA rectangle (buffer position on first line).
  84. (if (not val)
  85. (aref cua--rectangle 0)
  86. (setq val (line-beginning-position))
  87. (if (<= val (aref cua--rectangle 1))
  88. (aset cua--rectangle 0 val)
  89. (aset cua--rectangle 1 val)
  90. (cua--rectangle-corner 2))))
  91. (defun cua--rectangle-bot (&optional val)
  92. ;; Bot of CUA rectangle (buffer position on last line).
  93. (if (not val)
  94. (aref cua--rectangle 1)
  95. (setq val (line-end-position))
  96. (if (>= val (aref cua--rectangle 0))
  97. (aset cua--rectangle 1 val)
  98. (aset cua--rectangle 0 val)
  99. (cua--rectangle-corner 2))))
  100. (defun cua--rectangle-left (&optional val)
  101. ;; Left column of CUA rectangle.
  102. (if (integerp val)
  103. (if (<= val (aref cua--rectangle 3))
  104. (aset cua--rectangle 2 val)
  105. (aset cua--rectangle 3 val)
  106. (cua--rectangle-corner (if (cua--rectangle-right-side) -1 1)))
  107. (aref cua--rectangle 2)))
  108. (defun cua--rectangle-right (&optional val)
  109. ;; Right column of CUA rectangle.
  110. (if (integerp val)
  111. (if (>= val (aref cua--rectangle 2))
  112. (aset cua--rectangle 3 val)
  113. (aset cua--rectangle 2 val)
  114. (cua--rectangle-corner (if (cua--rectangle-right-side) -1 1)))
  115. (aref cua--rectangle 3)))
  116. (defun cua--rectangle-corner (&optional advance)
  117. ;; Currently active corner of rectangle.
  118. (let ((c (aref cua--rectangle 4)))
  119. (if (not (integerp advance))
  120. c
  121. (aset cua--rectangle 4
  122. (if (= advance 0)
  123. (- 3 c) ; opposite corner
  124. (mod (+ c 4 advance) 4)))
  125. (aset cua--rectangle 5 0))))
  126. (defun cua--rectangle-right-side (&optional topbot)
  127. ;; t if point is on right side of rectangle.
  128. (if (and topbot (= (cua--rectangle-left) (cua--rectangle-right)))
  129. (< (cua--rectangle-corner) 2)
  130. (= (mod (cua--rectangle-corner) 2) 1)))
  131. (defun cua--rectangle-column ()
  132. (if (cua--rectangle-right-side)
  133. (cua--rectangle-right)
  134. (cua--rectangle-left)))
  135. (defun cua--rectangle-insert-col (&optional col)
  136. ;; Currently active corner of rectangle.
  137. (if (integerp col)
  138. (aset cua--rectangle 5 col)
  139. (if (cua--rectangle-right-side t)
  140. (if (= (aref cua--rectangle 5) 0)
  141. (1+ (cua--rectangle-right))
  142. (aref cua--rectangle 5))
  143. (cua--rectangle-left))))
  144. (defun cua--rectangle-virtual-edges (&optional set val)
  145. ;; Current setting of rectangle virtual-edges
  146. (if set
  147. (aset cua--rectangle 6 val))
  148. (and ;(not buffer-read-only)
  149. (aref cua--rectangle 6)))
  150. (defun cua--rectangle-restriction (&optional val bounded negated)
  151. ;; Current rectangle restriction
  152. (if val
  153. (aset cua--rectangle 7
  154. (and (stringp val)
  155. (> (length val) 0)
  156. (list val bounded negated)))
  157. (aref cua--rectangle 7)))
  158. (defun cua--rectangle-assert ()
  159. (message "%S (%d)" cua--rectangle (point))
  160. (if (< (cua--rectangle-right) (cua--rectangle-left))
  161. (message "rectangle right < left"))
  162. (if (< (cua--rectangle-bot) (cua--rectangle-top))
  163. (message "rectangle bot < top")))
  164. (defun cua--rectangle-get-corners ()
  165. ;; Calculate the rectangular region represented by point and mark,
  166. ;; putting start in the upper left corner and end in the
  167. ;; bottom right corner.
  168. (let ((top (point)) (bot (mark)) r l corner)
  169. (save-excursion
  170. (goto-char top)
  171. (setq l (current-column))
  172. (goto-char bot)
  173. (setq r (current-column))
  174. (if (<= top bot)
  175. (setq corner (if (<= l r) 0 1))
  176. (setq top (prog1 bot (setq bot top)))
  177. (setq corner (if (<= l r) 2 3)))
  178. (if (<= l r)
  179. (if (< l r)
  180. (setq r (1- r)))
  181. (setq l (prog1 r (setq r l)))
  182. (goto-char top)
  183. (move-to-column l)
  184. (setq top (point))
  185. (goto-char bot)
  186. (move-to-column r)
  187. (setq bot (point))))
  188. (vector top bot l r corner 0 cua-virtual-rectangle-edges nil)))
  189. (defun cua--rectangle-set-corners ()
  190. ;; Set mark and point in opposite corners of current rectangle.
  191. (let (pp pc mp mc (c (cua--rectangle-corner)))
  192. (cond
  193. ((= c 0) ; top/left -> bot/right
  194. (setq pp (cua--rectangle-top) pc (cua--rectangle-left)
  195. mp (cua--rectangle-bot) mc (cua--rectangle-right)))
  196. ((= c 1) ; top/right -> bot/left
  197. (setq pp (cua--rectangle-top) pc (cua--rectangle-right)
  198. mp (cua--rectangle-bot) mc (cua--rectangle-left)))
  199. ((= c 2) ; bot/left -> top/right
  200. (setq pp (cua--rectangle-bot) pc (cua--rectangle-left)
  201. mp (cua--rectangle-top) mc (cua--rectangle-right)))
  202. ((= c 3) ; bot/right -> top/left
  203. (setq pp (cua--rectangle-bot) pc (cua--rectangle-right)
  204. mp (cua--rectangle-top) mc (cua--rectangle-left))))
  205. (goto-char mp)
  206. (move-to-column mc)
  207. (set-mark (point))
  208. (goto-char pp)
  209. ;; Move cursor inside rectangle, except if char at right edge is a tab.
  210. (if (and (if (cua--rectangle-right-side)
  211. (and (= (move-to-column pc) (- pc tab-width))
  212. (not (eolp)))
  213. (> (move-to-column pc) pc))
  214. (not (bolp)))
  215. (backward-char 1))
  216. ))
  217. (defun cua--rect-start-position ()
  218. ;; Return point of top left corner
  219. (save-excursion
  220. (goto-char (cua--rectangle-top))
  221. (and (> (move-to-column (cua--rectangle-left))
  222. (cua--rectangle-left))
  223. (not (bolp))
  224. (backward-char 1))
  225. (point)))
  226. (defun cua--rect-end-position ()
  227. ;; Return point of bottom right cornet
  228. (save-excursion
  229. (goto-char (cua--rectangle-bot))
  230. (and (= (move-to-column (cua--rectangle-right))
  231. (- (cua--rectangle-right) tab-width))
  232. (not (eolp))
  233. (not (bolp))
  234. (backward-char 1))
  235. (point)))
  236. ;;; Rectangle resizing
  237. (defun cua--forward-line (n)
  238. ;; Move forward/backward one line. Returns t if movement.
  239. (let ((pt (point)))
  240. (and (= (forward-line n) 0)
  241. ;; Deal with end of buffer
  242. (or (not (eobp))
  243. (goto-char pt)))))
  244. (defun cua--rectangle-resized ()
  245. ;; Refresh state after resizing rectangle
  246. (setq cua--buffer-and-point-before-command nil)
  247. (cua--rectangle-insert-col 0)
  248. (cua--rectangle-set-corners)
  249. (cua--keep-active))
  250. (defun cua-resize-rectangle-right (n)
  251. "Resize rectangle to the right."
  252. (interactive "p")
  253. (let ((resized (> n 0)))
  254. (while (> n 0)
  255. (setq n (1- n))
  256. (cond
  257. ((cua--rectangle-right-side)
  258. (cua--rectangle-right (1+ (cua--rectangle-right)))
  259. (move-to-column (cua--rectangle-right)))
  260. (t
  261. (cua--rectangle-left (1+ (cua--rectangle-left)))
  262. (move-to-column (cua--rectangle-right)))))
  263. (if resized
  264. (cua--rectangle-resized))))
  265. (defun cua-resize-rectangle-left (n)
  266. "Resize rectangle to the left."
  267. (interactive "p")
  268. (let (resized)
  269. (while (> n 0)
  270. (setq n (1- n))
  271. (if (or (= (cua--rectangle-right) 0)
  272. (and (not (cua--rectangle-right-side)) (= (cua--rectangle-left) 0)))
  273. (setq n 0)
  274. (cond
  275. ((cua--rectangle-right-side)
  276. (cua--rectangle-right (1- (cua--rectangle-right)))
  277. (move-to-column (cua--rectangle-right)))
  278. (t
  279. (cua--rectangle-left (1- (cua--rectangle-left)))
  280. (move-to-column (cua--rectangle-right))))
  281. (setq resized t)))
  282. (if resized
  283. (cua--rectangle-resized))))
  284. (defun cua-resize-rectangle-down (n)
  285. "Resize rectangle downwards."
  286. (interactive "p")
  287. (let (resized)
  288. (while (> n 0)
  289. (setq n (1- n))
  290. (cond
  291. ((>= (cua--rectangle-corner) 2)
  292. (goto-char (cua--rectangle-bot))
  293. (when (cua--forward-line 1)
  294. (move-to-column (cua--rectangle-column))
  295. (cua--rectangle-bot t)
  296. (setq resized t)))
  297. (t
  298. (goto-char (cua--rectangle-top))
  299. (when (cua--forward-line 1)
  300. (move-to-column (cua--rectangle-column))
  301. (cua--rectangle-top t)
  302. (setq resized t)))))
  303. (if resized
  304. (cua--rectangle-resized))))
  305. (defun cua-resize-rectangle-up (n)
  306. "Resize rectangle upwards."
  307. (interactive "p")
  308. (let (resized)
  309. (while (> n 0)
  310. (setq n (1- n))
  311. (cond
  312. ((>= (cua--rectangle-corner) 2)
  313. (goto-char (cua--rectangle-bot))
  314. (when (cua--forward-line -1)
  315. (move-to-column (cua--rectangle-column))
  316. (cua--rectangle-bot t)
  317. (setq resized t)))
  318. (t
  319. (goto-char (cua--rectangle-top))
  320. (when (cua--forward-line -1)
  321. (move-to-column (cua--rectangle-column))
  322. (cua--rectangle-top t)
  323. (setq resized t)))))
  324. (if resized
  325. (cua--rectangle-resized))))
  326. (defun cua-resize-rectangle-eol ()
  327. "Resize rectangle to end of line."
  328. (interactive)
  329. (unless (eolp)
  330. (end-of-line)
  331. (if (> (current-column) (cua--rectangle-right))
  332. (cua--rectangle-right (current-column)))
  333. (if (not (cua--rectangle-right-side))
  334. (cua--rectangle-corner 1))
  335. (cua--rectangle-resized)))
  336. (defun cua-resize-rectangle-bol ()
  337. "Resize rectangle to beginning of line."
  338. (interactive)
  339. (unless (bolp)
  340. (beginning-of-line)
  341. (cua--rectangle-left (current-column))
  342. (if (cua--rectangle-right-side)
  343. (cua--rectangle-corner -1))
  344. (cua--rectangle-resized)))
  345. (defun cua-resize-rectangle-bot ()
  346. "Resize rectangle to bottom of buffer."
  347. (interactive)
  348. (goto-char (point-max))
  349. (move-to-column (cua--rectangle-column))
  350. (cua--rectangle-bot t)
  351. (cua--rectangle-resized))
  352. (defun cua-resize-rectangle-top ()
  353. "Resize rectangle to top of buffer."
  354. (interactive)
  355. (goto-char (point-min))
  356. (move-to-column (cua--rectangle-column))
  357. (cua--rectangle-top t)
  358. (cua--rectangle-resized))
  359. (defun cua-resize-rectangle-page-up ()
  360. "Resize rectangle upwards by one scroll page."
  361. (interactive)
  362. (scroll-down)
  363. (move-to-column (cua--rectangle-column))
  364. (if (>= (cua--rectangle-corner) 2)
  365. (cua--rectangle-bot t)
  366. (cua--rectangle-top t))
  367. (cua--rectangle-resized))
  368. (defun cua-resize-rectangle-page-down ()
  369. "Resize rectangle downwards by one scroll page."
  370. (interactive)
  371. (scroll-up)
  372. (move-to-column (cua--rectangle-column))
  373. (if (>= (cua--rectangle-corner) 2)
  374. (cua--rectangle-bot t)
  375. (cua--rectangle-top t))
  376. (cua--rectangle-resized))
  377. ;;; Mouse support
  378. ;; This is pretty simplistic, but it does the job...
  379. (defun cua-mouse-resize-rectangle (event)
  380. "Set rectangle corner at mouse click position."
  381. (interactive "e")
  382. (mouse-set-point event)
  383. ;; FIX ME -- need to calculate virtual column.
  384. (if (cua--rectangle-virtual-edges)
  385. (move-to-column (car (posn-col-row (event-end event))) t))
  386. (if (cua--rectangle-right-side)
  387. (cua--rectangle-right (current-column))
  388. (cua--rectangle-left (current-column)))
  389. (if (>= (cua--rectangle-corner) 2)
  390. (cua--rectangle-bot t)
  391. (cua--rectangle-top t))
  392. (cua--rectangle-resized))
  393. (defvar cua--mouse-last-pos nil)
  394. (defun cua-mouse-set-rectangle-mark (event)
  395. "Start rectangle at mouse click position."
  396. (interactive "e")
  397. (when cua--rectangle
  398. (cua--deactivate-rectangle)
  399. (cua--deactivate t))
  400. (setq cua--last-rectangle nil)
  401. (mouse-set-point event)
  402. ;; FIX ME -- need to calculate virtual column.
  403. (cua-set-rectangle-mark)
  404. (setq cua--buffer-and-point-before-command nil)
  405. (setq cua--mouse-last-pos nil))
  406. (defun cua-mouse-save-then-kill-rectangle (event arg)
  407. "Expand rectangle to mouse click position and copy rectangle.
  408. If command is repeated at same position, delete the rectangle."
  409. (interactive "e\nP")
  410. (if (and (eq this-command last-command)
  411. (eq (point) (car-safe cua--mouse-last-pos))
  412. (eq cua--last-killed-rectangle (cdr-safe cua--mouse-last-pos)))
  413. (progn
  414. (unless buffer-read-only
  415. (cua--delete-rectangle))
  416. (cua--deactivate))
  417. (cua-mouse-resize-rectangle event)
  418. (let ((cua-keep-region-after-copy t))
  419. (cua-copy-region arg)
  420. (setq cua--mouse-last-pos (cons (point) cua--last-killed-rectangle)))))
  421. (defun cua--mouse-ignore (_event)
  422. (interactive "e")
  423. (setq this-command last-command))
  424. (defun cua--rectangle-move (dir)
  425. (let ((moved t)
  426. (top (cua--rectangle-top))
  427. (bot (cua--rectangle-bot))
  428. (l (cua--rectangle-left))
  429. (r (cua--rectangle-right)))
  430. (cond
  431. ((eq dir 'up)
  432. (goto-char top)
  433. (when (cua--forward-line -1)
  434. (cua--rectangle-top t)
  435. (goto-char bot)
  436. (forward-line -1)
  437. (cua--rectangle-bot t)))
  438. ((eq dir 'down)
  439. (goto-char bot)
  440. (when (cua--forward-line 1)
  441. (cua--rectangle-bot t)
  442. (goto-char top)
  443. (cua--forward-line 1)
  444. (cua--rectangle-top t)))
  445. ((eq dir 'left)
  446. (when (> l 0)
  447. (cua--rectangle-left (1- l))
  448. (cua--rectangle-right (1- r))))
  449. ((eq dir 'right)
  450. (cua--rectangle-right (1+ r))
  451. (cua--rectangle-left (1+ l)))
  452. (t
  453. (setq moved nil)))
  454. (when moved
  455. (setq cua--buffer-and-point-before-command nil)
  456. (cua--rectangle-set-corners)
  457. (cua--keep-active))))
  458. ;;; Operations on current rectangle
  459. (defun cua--tabify-start (start end)
  460. ;; Return position where auto-tabify should start (or nil if not required).
  461. (save-excursion
  462. (save-restriction
  463. (widen)
  464. (and (not buffer-read-only)
  465. cua-auto-tabify-rectangles
  466. (if (or (not (integerp cua-auto-tabify-rectangles))
  467. (= (point-min) (point-max))
  468. (progn
  469. (goto-char (max (point-min)
  470. (- start cua-auto-tabify-rectangles)))
  471. (search-forward "\t" (min (point-max)
  472. (+ end cua-auto-tabify-rectangles)) t)))
  473. start)))))
  474. (defun cua--rectangle-operation (keep-clear visible undo pad tabify &optional fct post-fct)
  475. ;; Call FCT for each line of region with 4 parameters:
  476. ;; Region start, end, left-col, right-col
  477. ;; Point is at start when FCT is called
  478. ;; Call fct with (s,e) = whole lines if VISIBLE non-nil.
  479. ;; Only call fct for visible lines if VISIBLE==t.
  480. ;; Set undo boundary if UNDO is non-nil.
  481. ;; Rectangle is padded if PAD = t or numeric and (cua--rectangle-virtual-edges)
  482. ;; Perform auto-tabify after operation if TABIFY is non-nil.
  483. ;; Mark is kept if keep-clear is 'keep and cleared if keep-clear is 'clear.
  484. (let* ((inhibit-field-text-motion t)
  485. (start (cua--rectangle-top))
  486. (end (cua--rectangle-bot))
  487. (l (cua--rectangle-left))
  488. (r (1+ (cua--rectangle-right)))
  489. (m (make-marker))
  490. (tabpad (and (integerp pad) (= pad 2)))
  491. (sel (cua--rectangle-restriction))
  492. (tabify-start (and tabify (cua--tabify-start start end))))
  493. (if undo
  494. (cua--rectangle-undo-boundary))
  495. (if (integerp pad)
  496. (setq pad (cua--rectangle-virtual-edges)))
  497. (save-excursion
  498. (save-restriction
  499. (widen)
  500. (when (> (cua--rectangle-corner) 1)
  501. (goto-char end)
  502. (and (bolp) (not (eolp)) (not (eobp))
  503. (setq end (1+ end))))
  504. (when (eq visible t)
  505. (setq start (max (window-start) start))
  506. (setq end (min (window-end) end)))
  507. (goto-char end)
  508. (setq end (line-end-position))
  509. (if (and visible (bolp) (not (eobp)))
  510. (setq end (1+ end)))
  511. (goto-char start)
  512. (setq start (line-beginning-position))
  513. (narrow-to-region start end)
  514. (goto-char (point-min))
  515. (while (< (point) (point-max))
  516. (move-to-column r pad)
  517. (and (not pad) (not visible) (> (current-column) r)
  518. (backward-char 1))
  519. (if (and tabpad (not pad) (looking-at "\t"))
  520. (forward-char 1))
  521. (set-marker m (point))
  522. (move-to-column l pad)
  523. (if (and fct (or visible (and (>= (current-column) l) (<= (current-column) r))))
  524. (let ((v t) (p (point)))
  525. (when sel
  526. (if (car (cdr sel))
  527. (setq v (looking-at (car sel)))
  528. (setq v (re-search-forward (car sel) m t))
  529. (goto-char p))
  530. (if (car (cdr (cdr sel)))
  531. (setq v (null v))))
  532. (if visible
  533. (funcall fct p m l r v)
  534. (if v
  535. (funcall fct p m l r)))))
  536. (set-marker m nil)
  537. (forward-line 1))
  538. (if (not visible)
  539. (cua--rectangle-bot t))
  540. (if post-fct
  541. (funcall post-fct l r))
  542. (when tabify-start
  543. (tabify tabify-start (point)))))
  544. (cond
  545. ((eq keep-clear 'keep)
  546. (cua--keep-active))
  547. ((eq keep-clear 'clear)
  548. (cua--deactivate))
  549. ((eq keep-clear 'corners)
  550. (cua--rectangle-set-corners)
  551. (cua--keep-active)))
  552. (setq cua--buffer-and-point-before-command nil)))
  553. (put 'cua--rectangle-operation 'lisp-indent-function 4)
  554. (defun cua--delete-rectangle ()
  555. (let ((lines 0))
  556. (if (not (cua--rectangle-virtual-edges))
  557. (cua--rectangle-operation nil nil t 2 t
  558. (lambda (s e _l _r _v)
  559. (setq lines (1+ lines))
  560. (if (and (> e s) (<= e (point-max)))
  561. (delete-region s e))))
  562. (cua--rectangle-operation nil 1 t nil t
  563. (lambda (s e _l _r _v)
  564. (setq lines (1+ lines))
  565. (when (and (> e s) (<= e (point-max)))
  566. (delete-region s e)))))
  567. lines))
  568. (defun cua--extract-rectangle ()
  569. (let (rect)
  570. (if (not (cua--rectangle-virtual-edges))
  571. (cua--rectangle-operation nil nil nil nil nil ; do not tabify
  572. (lambda (s e _l _r)
  573. (setq rect (cons (cua--filter-buffer-noprops s e) rect))))
  574. (cua--rectangle-operation nil 1 nil nil nil ; do not tabify
  575. (lambda (s e l r _v)
  576. (let ((copy t) (bs 0) (as 0) row)
  577. (if (= s e) (setq e (1+ e)))
  578. (goto-char s)
  579. (move-to-column l)
  580. (if (= (point) (line-end-position))
  581. (setq bs (- r l)
  582. copy nil)
  583. (skip-chars-forward "\s\t" e)
  584. (setq bs (- (min r (current-column)) l)
  585. s (point))
  586. (move-to-column r)
  587. (skip-chars-backward "\s\t" s)
  588. (setq as (- r (max (current-column) l))
  589. e (point)))
  590. (setq row (if (and copy (> e s))
  591. (cua--filter-buffer-noprops s e)
  592. ""))
  593. (when (> bs 0)
  594. (setq row (concat (make-string bs ?\s) row)))
  595. (when (> as 0)
  596. (setq row (concat row (make-string as ?\s))))
  597. (setq rect (cons row rect))))))
  598. (nreverse rect)))
  599. (defun cua--extract-rectangle-bounds ()
  600. (let (rect)
  601. (if (not (cua--rectangle-virtual-edges))
  602. (cua--rectangle-operation nil nil nil nil nil ; do not tabify
  603. (lambda (s e _l _r)
  604. (setq rect (cons (cons s e) rect))))
  605. (cua--rectangle-operation nil 1 nil nil nil ; do not tabify
  606. (lambda (s e l r _v)
  607. (goto-char s)
  608. (move-to-column l)
  609. (setq s (point))
  610. (move-to-column r)
  611. (setq e (point))
  612. (setq rect (cons (cons s e) rect)))))
  613. (nreverse rect)))
  614. (defun cua--insert-rectangle (rect &optional below paste-column line-count)
  615. ;; Insert rectangle as insert-rectangle, but don't set mark and exit with
  616. ;; point at either next to top right or below bottom left corner
  617. ;; Notice: In overwrite mode, the rectangle is inserted as separate text lines.
  618. (if (eq below 'auto)
  619. (setq below (and (bolp)
  620. (or (eolp) (eobp) (= (1+ (point)) (point-max))))))
  621. (unless paste-column
  622. (setq paste-column (current-column)))
  623. (let ((lines rect)
  624. (first t)
  625. (tabify-start (cua--tabify-start (point) (point)))
  626. last-column
  627. p)
  628. (while (or lines below)
  629. (or first
  630. (if overwrite-mode
  631. (insert ?\n)
  632. (forward-line 1)
  633. (or (bolp) (insert ?\n))))
  634. (unless overwrite-mode
  635. (move-to-column paste-column t))
  636. (if (not lines)
  637. (setq below nil)
  638. (insert-for-yank (car lines))
  639. (unless last-column
  640. (setq last-column (current-column)))
  641. (setq lines (cdr lines))
  642. (and first (not below)
  643. (setq p (point))))
  644. (setq first nil)
  645. (if (and line-count (= (setq line-count (1- line-count)) 0))
  646. (setq lines nil)))
  647. (when (and line-count last-column (not overwrite-mode))
  648. (while (> line-count 0)
  649. (forward-line 1)
  650. (or (bolp) (insert ?\n))
  651. (move-to-column paste-column t)
  652. (insert-char ?\s (- last-column paste-column -1))
  653. (setq line-count (1- line-count))))
  654. (when (and tabify-start
  655. (not overwrite-mode))
  656. (tabify tabify-start (point)))
  657. (and p (not overwrite-mode)
  658. (goto-char p))))
  659. (defun cua--copy-rectangle-as-kill (&optional ring)
  660. (if cua--register
  661. (set-register cua--register (cua--extract-rectangle))
  662. (setq killed-rectangle (cua--extract-rectangle))
  663. (setq cua--last-killed-rectangle (cons (and kill-ring (car kill-ring)) killed-rectangle))
  664. (if ring
  665. (kill-new (mapconcat
  666. (function (lambda (row) (concat row "\n")))
  667. killed-rectangle "")))))
  668. (defun cua--activate-rectangle ()
  669. ;; Set cua--rectangle to indicate we're marking a rectangle.
  670. ;; Be careful if we are already marking a rectangle.
  671. (setq cua--rectangle
  672. (or (and cua--last-rectangle
  673. (eq (car cua--last-rectangle) (current-buffer))
  674. (eq (car (cdr cua--last-rectangle)) (point))
  675. (cdr (cdr cua--last-rectangle)))
  676. (cua--rectangle-get-corners))
  677. cua--status-string (if (cua--rectangle-virtual-edges) " [R]" "")
  678. cua--last-rectangle nil)
  679. (activate-mark))
  680. ;; (defvar cua-save-point nil)
  681. (defun cua--deactivate-rectangle ()
  682. ;; This is used to clean up after `cua--activate-rectangle'.
  683. (mapc #'delete-overlay cua--rectangle-overlays)
  684. (setq cua--last-rectangle (cons (current-buffer)
  685. (cons (point) ;; cua-save-point
  686. cua--rectangle))
  687. cua--rectangle nil
  688. cua--rectangle-overlays nil
  689. cua--status-string nil
  690. cua--mouse-last-pos nil)
  691. ;; FIXME: This call to cua-rectangle-mark-mode is a workaround.
  692. ;; Deactivation can happen in various different ways, and we
  693. ;; currently don't handle them all in a coherent way.
  694. (if cua-rectangle-mark-mode (cua-rectangle-mark-mode -1)))
  695. (defun cua--highlight-rectangle ()
  696. ;; This function is used to highlight the rectangular region.
  697. ;; We do this by putting an overlay on each line within the rectangle.
  698. ;; Each overlay extends across all the columns of the rectangle.
  699. ;; We try to reuse overlays where possible because this is more efficient
  700. ;; and results in less flicker.
  701. ;; If cua--rectangle-virtual-edges is nil and the buffer contains tabs or short lines,
  702. ;; the highlighted region may not be perfectly rectangular.
  703. (let ((deactivate-mark deactivate-mark)
  704. (old cua--rectangle-overlays)
  705. (new nil)
  706. (left (cua--rectangle-left))
  707. (right (1+ (cua--rectangle-right))))
  708. (when (/= left right)
  709. (sit-for 0) ; make window top/bottom reliable
  710. (cua--rectangle-operation nil t nil nil nil ; do not tabify
  711. (lambda (s e l r v)
  712. (let ((rface (if v 'cua-rectangle 'cua-rectangle-noselect))
  713. overlay bs ms as)
  714. (when (cua--rectangle-virtual-edges)
  715. (let ((lb (line-beginning-position))
  716. (le (line-end-position))
  717. cl cl0 pl cr cr0 pr)
  718. (goto-char s)
  719. (setq cl (move-to-column l)
  720. pl (point))
  721. (setq cr (move-to-column r)
  722. pr (point))
  723. (if (= lb pl)
  724. (setq cl0 0)
  725. (goto-char (1- pl))
  726. (setq cl0 (current-column)))
  727. (if (= lb le)
  728. (setq cr0 0)
  729. (goto-char (1- pr))
  730. (setq cr0 (current-column)))
  731. (unless (and (= cl l) (= cr r))
  732. (when (/= cl l)
  733. (setq bs (propertize
  734. (make-string
  735. (- l cl0 (if (and (= le pl) (/= le lb)) 1 0))
  736. (if cua--virtual-edges-debug ?. ?\s))
  737. 'face (or (get-text-property (max (1- s) (point-min)) 'face) 'default)))
  738. (if (/= pl le)
  739. (setq s (1- s))))
  740. (cond
  741. ((= cr r)
  742. (if (and (/= pr le)
  743. (/= cr0 (1- cr))
  744. (or bs (/= cr0 (- cr tab-width)))
  745. (/= (mod cr tab-width) 0))
  746. (setq e (1- e))))
  747. ((= cr cl)
  748. (setq ms (propertize
  749. (make-string
  750. (- r l)
  751. (if cua--virtual-edges-debug ?, ?\s))
  752. 'face rface))
  753. (if (cua--rectangle-right-side)
  754. (put-text-property (1- (length ms)) (length ms) 'cursor 2 ms)
  755. (put-text-property 0 1 'cursor 2 ms))
  756. (setq bs (concat bs ms))
  757. (setq rface nil))
  758. (t
  759. (setq as (propertize
  760. (make-string
  761. (- r cr0 (if (= le pr) 1 0))
  762. (if cua--virtual-edges-debug ?~ ?\s))
  763. 'face rface))
  764. (if (cua--rectangle-right-side)
  765. (put-text-property (1- (length as)) (length as) 'cursor 2 as)
  766. (put-text-property 0 1 'cursor 2 as))
  767. (if (/= pr le)
  768. (setq e (1- e))))))))
  769. ;; Trim old leading overlays.
  770. (while (and old
  771. (setq overlay (car old))
  772. (< (overlay-start overlay) s)
  773. (/= (overlay-end overlay) e))
  774. (delete-overlay overlay)
  775. (setq old (cdr old)))
  776. ;; Reuse an overlay if possible, otherwise create one.
  777. (if (and old
  778. (setq overlay (car old))
  779. (or (= (overlay-start overlay) s)
  780. (= (overlay-end overlay) e)))
  781. (progn
  782. (move-overlay overlay s e)
  783. (setq old (cdr old)))
  784. (setq overlay (make-overlay s e)))
  785. (overlay-put overlay 'before-string bs)
  786. (overlay-put overlay 'after-string as)
  787. (overlay-put overlay 'face rface)
  788. (overlay-put overlay 'keymap cua--overlay-keymap)
  789. (overlay-put overlay 'window (selected-window))
  790. (setq new (cons overlay new))))))
  791. ;; Trim old trailing overlays.
  792. (mapc (function delete-overlay) old)
  793. (setq cua--rectangle-overlays (nreverse new))))
  794. (defun cua--indent-rectangle (&optional ch to-col clear)
  795. ;; Indent current rectangle.
  796. (let ((col (cua--rectangle-insert-col))
  797. (pad (cua--rectangle-virtual-edges))
  798. indent)
  799. (cua--rectangle-operation (if clear 'clear 'corners) nil t pad nil
  800. (lambda (_s _e l _r)
  801. (move-to-column col pad)
  802. (if (and (eolp)
  803. (< (current-column) col))
  804. (move-to-column col t))
  805. (cond
  806. (to-col (indent-to to-col))
  807. ((and ch (not (eq ch ?\t))) (insert ch))
  808. (t (tab-to-tab-stop)))
  809. (if (cua--rectangle-right-side t)
  810. (cua--rectangle-insert-col (current-column))
  811. (setq indent (- (current-column) l))))
  812. (lambda (l r)
  813. (when (and indent (> indent 0))
  814. (aset cua--rectangle 2 (+ l indent))
  815. (aset cua--rectangle 3 (+ r indent -1)))))))
  816. ;;
  817. ;; rectangle functions / actions
  818. ;;
  819. (defvar cua--rectangle-initialized nil)
  820. (defun cua-set-rectangle-mark (&optional reopen)
  821. "Set mark and start in CUA rectangle mode.
  822. With prefix argument, activate previous rectangle if possible."
  823. (interactive "P")
  824. (unless cua--rectangle-initialized
  825. (cua--init-rectangles))
  826. (when (not cua--rectangle)
  827. (if (and reopen
  828. cua--last-rectangle
  829. (eq (car cua--last-rectangle) (current-buffer)))
  830. (goto-char (car (cdr cua--last-rectangle)))
  831. (if (not mark-active)
  832. (push-mark nil nil t)))
  833. (cua--activate-rectangle)
  834. (cua--rectangle-set-corners)
  835. (if cua-enable-rectangle-auto-help
  836. (cua-help-for-rectangle t))))
  837. (defun cua-clear-rectangle-mark ()
  838. "Cancel current rectangle."
  839. (interactive)
  840. (when cua--rectangle
  841. (setq mark-active nil)
  842. (cua--deactivate-rectangle)))
  843. (defun cua-toggle-rectangle-mark ()
  844. (interactive)
  845. (if cua--rectangle
  846. (cua--deactivate-rectangle)
  847. (unless cua--rectangle-initialized
  848. (cua--init-rectangles))
  849. (cua--activate-rectangle))
  850. (if cua--rectangle
  851. (if cua-enable-rectangle-auto-help
  852. (cua-help-for-rectangle t))
  853. (if cua-enable-region-auto-help
  854. (cua-help-for-region t))))
  855. (defun cua-restrict-regexp-rectangle (arg)
  856. "Restrict rectangle to lines (not) matching regexp.
  857. With prefix argument, toggle restriction."
  858. (interactive "P")
  859. (let ((r (cua--rectangle-restriction)))
  860. (if (and r (null (car (cdr r))))
  861. (if arg
  862. (cua--rectangle-restriction (car r) nil (not (car (cdr (cdr r)))))
  863. (cua--rectangle-restriction "" nil nil))
  864. (cua--rectangle-restriction
  865. (read-from-minibuffer "Restrict rectangle (regexp): "
  866. nil nil nil nil) nil arg))))
  867. (defun cua-restrict-prefix-rectangle (arg)
  868. "Restrict rectangle to lines (not) starting with CHAR.
  869. With prefix argument, toggle restriction."
  870. (interactive "P")
  871. (let ((r (cua--rectangle-restriction)))
  872. (if (and r (car (cdr r)))
  873. (if arg
  874. (cua--rectangle-restriction (car r) t (not (car (cdr (cdr r)))))
  875. (cua--rectangle-restriction "" nil nil))
  876. (cua--rectangle-restriction
  877. (format "[%c]"
  878. (read-char "Restrictive rectangle (char): ")) t arg))))
  879. (defun cua-move-rectangle-up ()
  880. (interactive)
  881. (cua--rectangle-move 'up))
  882. (defun cua-move-rectangle-down ()
  883. (interactive)
  884. (cua--rectangle-move 'down))
  885. (defun cua-move-rectangle-left ()
  886. (interactive)
  887. (cua--rectangle-move 'left))
  888. (defun cua-move-rectangle-right ()
  889. (interactive)
  890. (cua--rectangle-move 'right))
  891. (defun cua-rotate-rectangle ()
  892. (interactive)
  893. (cua--rectangle-corner (if (= (cua--rectangle-left) (cua--rectangle-right)) 0 1))
  894. (cua--rectangle-set-corners)
  895. (if (cua--rectangle-virtual-edges)
  896. (setq cua--buffer-and-point-before-command nil)))
  897. (defun cua-toggle-rectangle-virtual-edges ()
  898. (interactive)
  899. (cua--rectangle-virtual-edges t (not (cua--rectangle-virtual-edges)))
  900. (cua--rectangle-set-corners)
  901. (setq cua--status-string (and (cua--rectangle-virtual-edges) " [R]"))
  902. (cua--keep-active))
  903. (defun cua-do-rectangle-padding ()
  904. (interactive)
  905. (if buffer-read-only
  906. (message "Cannot do padding in read-only buffer")
  907. (cua--rectangle-operation nil nil t t t)
  908. (cua--rectangle-set-corners))
  909. (cua--keep-active))
  910. (defun cua-open-rectangle ()
  911. "Blank out CUA rectangle, shifting text right.
  912. The text previously in the region is not overwritten by the blanks,
  913. but instead winds up to the right of the rectangle."
  914. (interactive)
  915. (cua--rectangle-operation 'corners nil t 1 nil
  916. (lambda (_s _e l r)
  917. (skip-chars-forward " \t")
  918. (let ((ws (- (current-column) l))
  919. (p (point)))
  920. (skip-chars-backward " \t")
  921. (delete-region (point) p)
  922. (indent-to (+ r ws))))))
  923. (defun cua-close-rectangle (arg)
  924. "Delete all whitespace starting at left edge of CUA rectangle.
  925. On each line in the rectangle, all continuous whitespace starting
  926. at that column is deleted.
  927. With prefix arg, also delete whitespace to the left of that column."
  928. (interactive "P")
  929. (cua--rectangle-operation 'clear nil t 1 nil
  930. (lambda (s _e _l _r)
  931. (when arg
  932. (skip-syntax-backward " " (line-beginning-position))
  933. (setq s (point)))
  934. (skip-syntax-forward " " (line-end-position))
  935. (delete-region s (point)))))
  936. (defun cua-blank-rectangle ()
  937. "Blank out CUA rectangle.
  938. The text previously in the rectangle is overwritten by the blanks."
  939. (interactive)
  940. (cua--rectangle-operation 'keep nil nil 1 nil
  941. (lambda (s e _l _r)
  942. (goto-char e)
  943. (skip-syntax-forward " " (line-end-position))
  944. (setq e (point))
  945. (let ((column (current-column)))
  946. (goto-char s)
  947. (skip-syntax-backward " " (line-beginning-position))
  948. (delete-region (point) e)
  949. (indent-to column)))))
  950. (defun cua-align-rectangle ()
  951. "Align rectangle lines to left column."
  952. (interactive)
  953. (cua--rectangle-operation 'clear nil t t nil
  954. (lambda (s _e l _r)
  955. (let ((b (line-beginning-position)))
  956. (skip-syntax-backward "^ " b)
  957. (skip-syntax-backward " " b)
  958. (setq s (point)))
  959. (skip-syntax-forward " " (line-end-position))
  960. (delete-region s (point))
  961. (indent-to l))
  962. (lambda (l _r)
  963. (move-to-column l)
  964. ;; (setq cua-save-point (point))
  965. )))
  966. (declare-function cua--cut-rectangle-to-global-mark "cua-gmrk" (as-text))
  967. (declare-function cua--copy-rectangle-to-global-mark "cua-gmrk" (as-text))
  968. (defun cua-copy-rectangle-as-text (&optional arg delete)
  969. "Copy rectangle, but store as normal text."
  970. (interactive "P")
  971. (if cua--global-mark-active
  972. (if delete
  973. (cua--cut-rectangle-to-global-mark t)
  974. (cua--copy-rectangle-to-global-mark t))
  975. (let* ((rect (cua--extract-rectangle))
  976. (text (mapconcat
  977. (function (lambda (row) (concat row "\n")))
  978. rect "")))
  979. (setq arg (cua--prefix-arg arg))
  980. (if cua--register
  981. (set-register cua--register text)
  982. (kill-new text)))
  983. (if delete
  984. (cua--delete-rectangle))
  985. (cua--deactivate)))
  986. (defun cua-cut-rectangle-as-text (arg)
  987. "Kill rectangle, but store as normal text."
  988. (interactive "P")
  989. (cua-copy-rectangle-as-text arg (not buffer-read-only)))
  990. (defun cua-string-rectangle (string)
  991. "Replace CUA rectangle contents with STRING on each line.
  992. The length of STRING need not be the same as the rectangle width."
  993. (interactive "sString rectangle: ")
  994. (cua--rectangle-operation 'keep nil t t nil
  995. (lambda (s e l _r)
  996. (delete-region s e)
  997. (skip-chars-forward " \t")
  998. (let ((ws (- (current-column) l)))
  999. (delete-region s (point))
  1000. (insert string)
  1001. (indent-to (+ (current-column) ws))))
  1002. (unless (cua--rectangle-restriction)
  1003. (lambda (l _r)
  1004. (cua--rectangle-right (max l (+ l (length string) -1)))))))
  1005. (defun cua-fill-char-rectangle (character)
  1006. "Replace CUA rectangle contents with CHARACTER."
  1007. (interactive "cFill rectangle with character: ")
  1008. (cua--rectangle-operation 'clear nil t 1 nil
  1009. (lambda (s e l r)
  1010. (delete-region s e)
  1011. (move-to-column l t)
  1012. (insert-char character (- r l)))))
  1013. (defun cua-replace-in-rectangle (regexp newtext)
  1014. "Replace REGEXP with NEWTEXT in each line of CUA rectangle."
  1015. (interactive "sReplace regexp: \nsNew text: ")
  1016. (if buffer-read-only
  1017. (message "Cannot replace in read-only buffer")
  1018. (cua--rectangle-operation 'keep nil t 1 nil
  1019. (lambda (_s e _l _r)
  1020. (if (re-search-forward regexp e t)
  1021. (replace-match newtext nil nil))))))
  1022. (defun cua-incr-rectangle (increment)
  1023. "Increment each line of CUA rectangle by prefix amount."
  1024. (interactive "p")
  1025. (cua--rectangle-operation 'keep nil t 1 nil
  1026. (lambda (_s e _l _r)
  1027. (cond
  1028. ((re-search-forward "0x\\([0-9a-fA-F]+\\)" e t)
  1029. (let* ((txt (cua--filter-buffer-noprops (match-beginning 1) (match-end 1)))
  1030. (n (string-to-number txt 16))
  1031. (fmt (format "0x%%0%dx" (length txt))))
  1032. (replace-match (format fmt (+ n increment)))))
  1033. ((re-search-forward "\\( *-?[0-9]+\\)" e t)
  1034. (let* ((txt (cua--filter-buffer-noprops (match-beginning 1) (match-end 1)))
  1035. (prefix (if (= (aref txt 0) ?0) "0" ""))
  1036. (n (string-to-number txt 10))
  1037. (fmt (format "%%%s%dd" prefix (length txt))))
  1038. (replace-match (format fmt (+ n increment)))))
  1039. (t nil)))))
  1040. (defvar cua--rectangle-seq-format "%d"
  1041. "Last format used by `cua-sequence-rectangle'.")
  1042. (defun cua-sequence-rectangle (first incr format)
  1043. "Resequence each line of CUA rectangle starting from FIRST.
  1044. The numbers are formatted according to the FORMAT string."
  1045. (interactive
  1046. (list (if current-prefix-arg
  1047. (prefix-numeric-value current-prefix-arg)
  1048. (string-to-number
  1049. (read-string "Start value: (0) " nil nil "0")))
  1050. (string-to-number
  1051. (read-string "Increment: (1) " nil nil "1"))
  1052. (read-string (concat "Format: (" cua--rectangle-seq-format ") "))))
  1053. (if (= (length format) 0)
  1054. (setq format cua--rectangle-seq-format)
  1055. (setq cua--rectangle-seq-format format))
  1056. (cua--rectangle-operation 'clear nil t 1 nil
  1057. (lambda (s e _l _r)
  1058. (delete-region s e)
  1059. (insert (format format first))
  1060. (setq first (+ first incr)))))
  1061. (defmacro cua--convert-rectangle-as (command tabify)
  1062. `(cua--rectangle-operation 'clear nil nil nil ,tabify
  1063. (lambda (s e _l _r)
  1064. (,command s e))))
  1065. (defun cua-upcase-rectangle ()
  1066. "Convert the rectangle to upper case."
  1067. (interactive)
  1068. (cua--convert-rectangle-as upcase-region nil))
  1069. (defun cua-downcase-rectangle ()
  1070. "Convert the rectangle to lower case."
  1071. (interactive)
  1072. (cua--convert-rectangle-as downcase-region nil))
  1073. (defun cua-upcase-initials-rectangle ()
  1074. "Convert the rectangle initials to upper case."
  1075. (interactive)
  1076. (cua--convert-rectangle-as upcase-initials-region nil))
  1077. (defun cua-capitalize-rectangle ()
  1078. "Convert the rectangle to proper case."
  1079. (interactive)
  1080. (cua--convert-rectangle-as capitalize-region nil))
  1081. ;;; Replace/rearrange text in current rectangle
  1082. (defun cua--rectangle-aux-replace (width adjust keep replace pad format-fct &optional setup-fct)
  1083. ;; Process text inserted by calling SETUP-FCT or current rectangle if nil.
  1084. ;; Then call FORMAT-FCT on text (if non-nil); takes two args: start and end.
  1085. ;; Fill to WIDTH characters if > 0 or fill to current width if == 0.
  1086. ;; Don't fill if WIDTH < 0.
  1087. ;; Replace current rectangle by filled text if REPLACE is non-nil
  1088. (let ((auxbuf (get-buffer-create "*CUA temp*"))
  1089. (w (if (> width 1) width
  1090. (- (cua--rectangle-right) (cua--rectangle-left) -1)))
  1091. (r (or setup-fct (cua--extract-rectangle)))
  1092. y z (tr 0))
  1093. (with-current-buffer auxbuf
  1094. (erase-buffer)
  1095. (if setup-fct
  1096. (funcall setup-fct)
  1097. (cua--insert-rectangle r))
  1098. (if format-fct
  1099. (let ((fill-column w))
  1100. (funcall format-fct (point-min) (point-max))))
  1101. (when replace
  1102. (goto-char (point-min))
  1103. (while (not (eobp))
  1104. (setq z (cons (filter-buffer-substring (point) (line-end-position)) z))
  1105. (forward-line 1))))
  1106. (if (not cua--debug)
  1107. (kill-buffer auxbuf))
  1108. (when replace
  1109. (setq z (reverse z))
  1110. (if cua--debug
  1111. (print z auxbuf))
  1112. (cua--rectangle-operation nil nil t pad nil
  1113. (lambda (s e l _r)
  1114. (let (cc)
  1115. (goto-char e)
  1116. (skip-chars-forward " \t")
  1117. (setq cc (current-column))
  1118. (if cua--debug
  1119. (print (list cc s e) auxbuf))
  1120. (delete-region s (point))
  1121. (if (not z)
  1122. (setq y 0)
  1123. (move-to-column l t)
  1124. (insert (car z))
  1125. (when (> (current-column) (+ l w))
  1126. (setq y (point))
  1127. (move-to-column (+ l w) t)
  1128. (delete-region (point) y)
  1129. (setq tr (1+ tr)))
  1130. (setq z (cdr z)))
  1131. (if cua--debug
  1132. (print (list (current-column) cc) auxbuf))
  1133. (just-one-space 0)
  1134. (indent-to cc))))
  1135. (if (> tr 0)
  1136. (message "Warning: Truncated %d row%s" tr (if (> tr 1) "s" "")))
  1137. (if adjust
  1138. (cua--rectangle-right (+ (cua--rectangle-left) w -1)))
  1139. (if keep
  1140. (cua--rectangle-resized)))))
  1141. (put 'cua--rectangle-aux-replace 'lisp-indent-function 4)
  1142. (defun cua--left-fill-rectangle (_start _end)
  1143. (beginning-of-line)
  1144. (while (< (point) (point-max))
  1145. (delete-horizontal-space nil)
  1146. (forward-line 1))
  1147. (fill-region-as-paragraph (point-min) (point-max) 'left nil)
  1148. (untabify (point-min) (point-max)))
  1149. (defun cua-text-fill-rectangle (width text)
  1150. "Replace rectangle with filled TEXT read from minibuffer.
  1151. A numeric prefix argument is used a new width for the filled rectangle."
  1152. (interactive (list
  1153. (prefix-numeric-value current-prefix-arg)
  1154. (read-from-minibuffer "Enter text: "
  1155. nil nil nil nil)))
  1156. (cua--rectangle-aux-replace width t t t 1
  1157. 'cua--left-fill-rectangle
  1158. (lambda () (insert text))))
  1159. (defun cua-refill-rectangle (width)
  1160. "Fill contents of current rectangle.
  1161. A numeric prefix argument is used as new width for the filled rectangle."
  1162. (interactive "P")
  1163. (cua--rectangle-aux-replace
  1164. (if width (prefix-numeric-value width) 0)
  1165. t t t 1 'cua--left-fill-rectangle))
  1166. (defun cua-shell-command-on-rectangle (replace command)
  1167. "Run shell command on rectangle like `shell-command-on-region'.
  1168. With prefix arg, replace rectangle with output from command."
  1169. (interactive (list
  1170. current-prefix-arg
  1171. (read-from-minibuffer "Shell command on rectangle: "
  1172. nil nil nil
  1173. 'shell-command-history)))
  1174. (cua--rectangle-aux-replace -1 t t replace 1
  1175. (lambda (s e)
  1176. (shell-command-on-region s e command
  1177. replace replace nil))))
  1178. (defun cua-reverse-rectangle ()
  1179. "Reverse the lines of the rectangle."
  1180. (interactive)
  1181. (cua--rectangle-aux-replace 0 t t t t 'reverse-region))
  1182. (defun cua-scroll-rectangle-up ()
  1183. "Remove the first line of the rectangle and scroll remaining lines up."
  1184. (interactive)
  1185. (cua--rectangle-aux-replace 0 t t t t
  1186. (lambda (s _e)
  1187. (if (= (forward-line 1) 0)
  1188. (delete-region s (point))))))
  1189. (defun cua-scroll-rectangle-down ()
  1190. "Insert a blank line at the first line of the rectangle.
  1191. The remaining lines are scrolled down, losing the last line."
  1192. (interactive)
  1193. (cua--rectangle-aux-replace 0 t t t t
  1194. (lambda (s _e)
  1195. (goto-char s)
  1196. (insert "\n"))))
  1197. ;;; Insert/delete text to left or right of rectangle
  1198. (defun cua-insert-char-rectangle (&optional ch)
  1199. (interactive)
  1200. (if buffer-read-only
  1201. (ding)
  1202. (cua--indent-rectangle (or ch (aref (this-single-command-keys) 0)))
  1203. (cua--keep-active))
  1204. t)
  1205. (defun cua-indent-rectangle (column)
  1206. "Indent rectangle to next tab stop.
  1207. With prefix arg, indent to that column."
  1208. (interactive "P")
  1209. (if (null column)
  1210. (cua-insert-char-rectangle ?\t)
  1211. (cua--indent-rectangle nil (prefix-numeric-value column))))
  1212. (defun cua-delete-char-rectangle ()
  1213. "Delete char to left or right of rectangle."
  1214. (interactive)
  1215. (let ((col (cua--rectangle-insert-col))
  1216. (pad (cua--rectangle-virtual-edges))
  1217. indent)
  1218. (cua--rectangle-operation 'corners nil t pad nil
  1219. (lambda (_s _e l r)
  1220. (move-to-column
  1221. (if (cua--rectangle-right-side t)
  1222. (max (1+ r) col) l)
  1223. pad)
  1224. (if (bolp)
  1225. nil
  1226. (delete-char -1)
  1227. (if (cua--rectangle-right-side t)
  1228. (cua--rectangle-insert-col (current-column))
  1229. (setq indent (- l (current-column))))))
  1230. (lambda (l r)
  1231. (when (and indent (> indent 0))
  1232. (aset cua--rectangle 2 (- l indent))
  1233. (aset cua--rectangle 3 (- r indent 1)))))))
  1234. (defun cua-help-for-rectangle (&optional help)
  1235. (interactive)
  1236. (let ((M (cond ((eq cua--rectangle-modifier-key 'hyper) " H-")
  1237. ((eq cua--rectangle-modifier-key 'super) " s-")
  1238. ((eq cua--rectangle-modifier-key 'alt) " A-")
  1239. (t " M-"))))
  1240. (message
  1241. (concat (if help "C-?:help" "")
  1242. M "p:pad" M "o:open" M "c:close" M "b:blank"
  1243. M "s:string" M "f:fill" M "i:incr" M "n:seq"))))
  1244. ;;; CUA-like cut & paste for rectangles
  1245. (defun cua--cancel-rectangle ()
  1246. ;; Cancel rectangle
  1247. (if cua--rectangle
  1248. (cua--deactivate-rectangle))
  1249. (setq cua--last-rectangle nil))
  1250. (defun cua--rectangle-post-command ()
  1251. (if cua--restored-rectangle
  1252. (progn
  1253. (setq cua--rectangle cua--restored-rectangle
  1254. cua--restored-rectangle nil
  1255. mark-active t
  1256. deactivate-mark nil)
  1257. (cua--rectangle-set-corners))
  1258. (when (and cua--rectangle cua--buffer-and-point-before-command
  1259. (equal (car cua--buffer-and-point-before-command) (current-buffer))
  1260. (not (= (cdr cua--buffer-and-point-before-command) (point))))
  1261. (if (cua--rectangle-right-side)
  1262. (cua--rectangle-right (current-column))
  1263. (cua--rectangle-left (current-column)))
  1264. (if (>= (cua--rectangle-corner) 2)
  1265. (cua--rectangle-bot t)
  1266. (cua--rectangle-top t))))
  1267. (if cua--rectangle
  1268. (if (and mark-active
  1269. (not deactivate-mark))
  1270. (cua--highlight-rectangle)
  1271. (cua--deactivate-rectangle))
  1272. (when cua--rectangle-overlays
  1273. ;; clean-up after revert-buffer
  1274. (mapc (function delete-overlay) cua--rectangle-overlays)
  1275. (setq cua--rectangle-overlays nil)
  1276. (setq deactivate-mark t)))
  1277. (when cua--rect-undo-set-point
  1278. (goto-char cua--rect-undo-set-point)
  1279. (setq cua--rect-undo-set-point nil)))
  1280. (add-function :around region-extract-function
  1281. #'cua--rectangle-region-extract)
  1282. (add-function :around region-insert-function
  1283. #'cua--insert-rectangle)
  1284. (add-function :around redisplay-highlight-region-function
  1285. #'cua--rectangle-highlight-for-redisplay)
  1286. (defun cua--rectangle-highlight-for-redisplay (orig &rest args)
  1287. (if (not cua--rectangle) (apply orig args)
  1288. ;; When cua--rectangle is active, just don't highlight at all, since we
  1289. ;; already do it elsewhere.
  1290. (funcall redisplay-unhighlight-region-function (nth 3 args))))
  1291. (defun cua--rectangle-region-extract (orig &optional delete)
  1292. (cond
  1293. ((not cua--rectangle)
  1294. (funcall orig delete))
  1295. ((eq delete 'bounds)
  1296. (cua--extract-rectangle-bounds))
  1297. ((eq delete 'delete-only)
  1298. (cua--delete-rectangle))
  1299. (t
  1300. (let* ((strs (cua--extract-rectangle))
  1301. (str (mapconcat #'identity strs "\n")))
  1302. (if delete (cua--delete-rectangle))
  1303. (setq killed-rectangle strs)
  1304. (setq cua--last-killed-rectangle
  1305. (cons (and kill-ring (car kill-ring)) killed-rectangle))
  1306. (when (eq last-command 'kill-region)
  1307. ;; Try to prevent kill-region from appending this to some
  1308. ;; earlier element.
  1309. (setq last-command 'kill-region-dont-append))
  1310. (when strs
  1311. (put-text-property 0 (length str) 'yank-handler
  1312. `(rectangle--insert-for-yank ,strs t)
  1313. str)
  1314. str)))))
  1315. ;;; Initialization
  1316. (defun cua--rect-M/H-key (key cmd)
  1317. (cua--M/H-key cua--rectangle-keymap key cmd))
  1318. (defun cua--init-rectangles ()
  1319. (define-key cua--rectangle-keymap cua-rectangle-mark-key 'cua-clear-rectangle-mark)
  1320. (define-key cua--region-keymap cua-rectangle-mark-key 'cua-toggle-rectangle-mark)
  1321. (unless (eq cua--rectangle-modifier-key 'meta)
  1322. (cua--rect-M/H-key ?\s 'cua-clear-rectangle-mark)
  1323. (cua--M/H-key cua--region-keymap ?\s 'cua-toggle-rectangle-mark))
  1324. (define-key cua--rectangle-keymap [remap set-mark-command] 'cua-toggle-rectangle-mark)
  1325. (define-key cua--rectangle-keymap [remap forward-char] 'cua-resize-rectangle-right)
  1326. (define-key cua--rectangle-keymap [remap right-char] 'cua-resize-rectangle-right)
  1327. (define-key cua--rectangle-keymap [remap backward-char] 'cua-resize-rectangle-left)
  1328. (define-key cua--rectangle-keymap [remap left-char] 'cua-resize-rectangle-left)
  1329. (define-key cua--rectangle-keymap [remap next-line] 'cua-resize-rectangle-down)
  1330. (define-key cua--rectangle-keymap [remap previous-line] 'cua-resize-rectangle-up)
  1331. (define-key cua--rectangle-keymap [remap end-of-line] 'cua-resize-rectangle-eol)
  1332. (define-key cua--rectangle-keymap [remap beginning-of-line] 'cua-resize-rectangle-bol)
  1333. (define-key cua--rectangle-keymap [remap end-of-buffer] 'cua-resize-rectangle-bot)
  1334. (define-key cua--rectangle-keymap [remap beginning-of-buffer] 'cua-resize-rectangle-top)
  1335. (define-key cua--rectangle-keymap [remap scroll-down] 'cua-resize-rectangle-page-up)
  1336. (define-key cua--rectangle-keymap [remap scroll-up] 'cua-resize-rectangle-page-down)
  1337. (define-key cua--rectangle-keymap [remap scroll-down-command] 'cua-resize-rectangle-page-up)
  1338. (define-key cua--rectangle-keymap [remap scroll-up-command] 'cua-resize-rectangle-page-down)
  1339. (define-key cua--rectangle-keymap [remap delete-backward-char] 'cua-delete-char-rectangle)
  1340. (define-key cua--rectangle-keymap [remap backward-delete-char] 'cua-delete-char-rectangle)
  1341. (define-key cua--rectangle-keymap [remap backward-delete-char-untabify] 'cua-delete-char-rectangle)
  1342. (define-key cua--rectangle-keymap [remap self-insert-command] 'cua-insert-char-rectangle)
  1343. ;; Catch self-inserting characters which are "stolen" by other modes
  1344. (define-key cua--rectangle-keymap [t]
  1345. '(menu-item "sic" cua-insert-char-rectangle :filter cua--self-insert-char-p))
  1346. (define-key cua--rectangle-keymap "\r" 'cua-rotate-rectangle)
  1347. (define-key cua--rectangle-keymap "\t" 'cua-indent-rectangle)
  1348. (define-key cua--rectangle-keymap [(control ??)] 'cua-help-for-rectangle)
  1349. (define-key cua--rectangle-keymap [mouse-1] 'cua-mouse-set-rectangle-mark)
  1350. (define-key cua--rectangle-keymap [down-mouse-1] 'cua--mouse-ignore)
  1351. (define-key cua--rectangle-keymap [drag-mouse-1] 'cua--mouse-ignore)
  1352. (define-key cua--rectangle-keymap [mouse-3] 'cua-mouse-save-then-kill-rectangle)
  1353. (define-key cua--rectangle-keymap [down-mouse-3] 'cua--mouse-ignore)
  1354. (define-key cua--rectangle-keymap [drag-mouse-3] 'cua--mouse-ignore)
  1355. (cua--rect-M/H-key 'up 'cua-move-rectangle-up)
  1356. (cua--rect-M/H-key 'down 'cua-move-rectangle-down)
  1357. (cua--rect-M/H-key 'left 'cua-move-rectangle-left)
  1358. (cua--rect-M/H-key 'right 'cua-move-rectangle-right)
  1359. (cua--rect-M/H-key '(control up) 'cua-scroll-rectangle-up)
  1360. (cua--rect-M/H-key '(control down) 'cua-scroll-rectangle-down)
  1361. (cua--rect-M/H-key ?a 'cua-align-rectangle)
  1362. (cua--rect-M/H-key ?b 'cua-blank-rectangle)
  1363. (cua--rect-M/H-key ?c 'cua-close-rectangle)
  1364. (cua--rect-M/H-key ?f 'cua-fill-char-rectangle)
  1365. (cua--rect-M/H-key ?i 'cua-incr-rectangle)
  1366. (cua--rect-M/H-key ?k 'cua-cut-rectangle-as-text)
  1367. (cua--rect-M/H-key ?l 'cua-downcase-rectangle)
  1368. (cua--rect-M/H-key ?m 'cua-copy-rectangle-as-text)
  1369. (cua--rect-M/H-key ?n 'cua-sequence-rectangle)
  1370. (cua--rect-M/H-key ?o 'cua-open-rectangle)
  1371. (cua--rect-M/H-key ?p 'cua-toggle-rectangle-virtual-edges)
  1372. (cua--rect-M/H-key ?P 'cua-do-rectangle-padding)
  1373. (cua--rect-M/H-key ?q 'cua-refill-rectangle)
  1374. (cua--rect-M/H-key ?r 'cua-replace-in-rectangle)
  1375. (cua--rect-M/H-key ?R 'cua-reverse-rectangle)
  1376. (cua--rect-M/H-key ?s 'cua-string-rectangle)
  1377. (cua--rect-M/H-key ?t 'cua-text-fill-rectangle)
  1378. (cua--rect-M/H-key ?u 'cua-upcase-rectangle)
  1379. (cua--rect-M/H-key ?| 'cua-shell-command-on-rectangle)
  1380. (cua--rect-M/H-key ?' 'cua-restrict-prefix-rectangle)
  1381. (cua--rect-M/H-key ?/ 'cua-restrict-regexp-rectangle)
  1382. (setq cua--rectangle-initialized t))
  1383. (provide 'cua-rect)
  1384. ;;; cua-rect.el ends here