cua-rect.el 51 KB

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