rect.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. ;;; rect.el --- rectangle functions for GNU Emacs
  2. ;; Copyright (C) 1985, 1999-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: Didier Verna <didier@xemacs.org>
  4. ;; Keywords: internal
  5. ;; Package: emacs
  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. ;;; Commentary:
  18. ;; This package provides the operations on rectangles that are documented
  19. ;; in the Emacs manual.
  20. ;; ### NOTE: this file was almost completely rewritten by Didier Verna
  21. ;; <didier@xemacs.org> in July 1999.
  22. ;;; Global key bindings
  23. ;;;###autoload (define-key ctl-x-r-map "c" 'clear-rectangle)
  24. ;;;###autoload (define-key ctl-x-r-map "k" 'kill-rectangle)
  25. ;;;###autoload (define-key ctl-x-r-map "d" 'delete-rectangle)
  26. ;;;###autoload (define-key ctl-x-r-map "y" 'yank-rectangle)
  27. ;;;###autoload (define-key ctl-x-r-map "o" 'open-rectangle)
  28. ;;;###autoload (define-key ctl-x-r-map "t" 'string-rectangle)
  29. ;;;###autoload (define-key ctl-x-r-map "N" 'rectangle-number-lines)
  30. ;;; Code:
  31. ;; FIXME: this function should be replaced by `apply-on-rectangle'
  32. (defun operate-on-rectangle (function start end coerce-tabs)
  33. "Call FUNCTION for each line of rectangle with corners at START, END.
  34. If COERCE-TABS is non-nil, convert multi-column characters
  35. that span the starting or ending columns on any line
  36. to multiple spaces before calling FUNCTION.
  37. FUNCTION is called with three arguments:
  38. position of start of segment of this line within the rectangle,
  39. number of columns that belong to rectangle but are before that position,
  40. number of columns that belong to rectangle but are after point.
  41. Point is at the end of the segment of this line within the rectangle."
  42. (let (startcol startlinepos endcol endlinepos)
  43. (save-excursion
  44. (goto-char start)
  45. (setq startcol (current-column))
  46. (beginning-of-line)
  47. (setq startlinepos (point)))
  48. (save-excursion
  49. (goto-char end)
  50. (setq endcol (current-column))
  51. (forward-line 1)
  52. (setq endlinepos (point-marker)))
  53. (if (< endcol startcol)
  54. (setq startcol (prog1 endcol (setq endcol startcol))))
  55. (save-excursion
  56. (goto-char startlinepos)
  57. (while (< (point) endlinepos)
  58. (let (startpos begextra endextra)
  59. (if coerce-tabs
  60. (move-to-column startcol t)
  61. (move-to-column startcol))
  62. (setq begextra (- (current-column) startcol))
  63. (setq startpos (point))
  64. (if coerce-tabs
  65. (move-to-column endcol t)
  66. (move-to-column endcol))
  67. ;; If we overshot, move back one character
  68. ;; so that endextra will be positive.
  69. (if (and (not coerce-tabs) (> (current-column) endcol))
  70. (backward-char 1))
  71. (setq endextra (- endcol (current-column)))
  72. (if (< begextra 0)
  73. (setq endextra (+ endextra begextra)
  74. begextra 0))
  75. (funcall function startpos begextra endextra))
  76. (forward-line 1)))
  77. (- endcol startcol)))
  78. (defun apply-on-rectangle (function start end &rest args)
  79. "Call FUNCTION for each line of rectangle with corners at START, END.
  80. FUNCTION is called with two arguments: the start and end columns of the
  81. rectangle, plus ARGS extra arguments. Point is at the beginning of line when
  82. the function is called.
  83. The final point after the last operation will be returned."
  84. (let (startcol startpt endcol endpt final-point)
  85. (save-excursion
  86. (goto-char start)
  87. (setq startcol (current-column))
  88. (beginning-of-line)
  89. (setq startpt (point))
  90. (goto-char end)
  91. (setq endcol (current-column))
  92. (forward-line 1)
  93. (setq endpt (point-marker))
  94. ;; ensure the start column is the left one.
  95. (if (< endcol startcol)
  96. (let ((col startcol))
  97. (setq startcol endcol endcol col)))
  98. ;; start looping over lines
  99. (goto-char startpt)
  100. (while (< (point) endpt)
  101. (apply function startcol endcol args)
  102. (setq final-point (point))
  103. (forward-line 1)))
  104. final-point))
  105. (defun delete-rectangle-line (startcol endcol fill)
  106. (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
  107. (delete-region (point)
  108. (progn (move-to-column endcol 'coerce)
  109. (point)))))
  110. (defun delete-extract-rectangle-line (startcol endcol lines fill)
  111. (let ((pt (point-at-eol)))
  112. (if (< (move-to-column startcol (if fill t 'coerce)) startcol)
  113. (setcdr lines (cons (spaces-string (- endcol startcol))
  114. (cdr lines)))
  115. ;; else
  116. (setq pt (point))
  117. (move-to-column endcol t)
  118. (setcdr lines (cons (filter-buffer-substring pt (point) t) (cdr lines))))
  119. ))
  120. ;; This is actually the only function that needs to do complicated
  121. ;; stuff like what's happening in `operate-on-rectangle', because the
  122. ;; buffer might be read-only.
  123. (defun extract-rectangle-line (startcol endcol lines)
  124. (let (start end begextra endextra line)
  125. (move-to-column startcol)
  126. (setq start (point)
  127. begextra (- (current-column) startcol))
  128. (move-to-column endcol)
  129. (setq end (point)
  130. endextra (- endcol (current-column)))
  131. (setq line (buffer-substring start (point)))
  132. (if (< begextra 0)
  133. (setq endextra (+ endextra begextra)
  134. begextra 0))
  135. (if (< endextra 0)
  136. (setq endextra 0))
  137. (goto-char start)
  138. (while (search-forward "\t" end t)
  139. (let ((width (- (current-column)
  140. (save-excursion (forward-char -1)
  141. (current-column)))))
  142. (setq line (concat (substring line 0 (- (point) end 1))
  143. (spaces-string width)
  144. (substring line (+ (length line)
  145. (- (point) end)))))))
  146. (if (or (> begextra 0) (> endextra 0))
  147. (setq line (concat (spaces-string begextra)
  148. line
  149. (spaces-string endextra))))
  150. (setcdr lines (cons line (cdr lines)))))
  151. (defconst spaces-strings
  152. '["" " " " " " " " " " " " " " " " "])
  153. (defun spaces-string (n)
  154. "Return a string with N spaces."
  155. (if (<= n 8) (aref spaces-strings n)
  156. (make-string n ?\s)))
  157. ;;;###autoload
  158. (defun delete-rectangle (start end &optional fill)
  159. "Delete (don't save) text in the region-rectangle.
  160. The same range of columns is deleted in each line starting with the
  161. line where the region begins and ending with the line where the region
  162. ends.
  163. When called from a program the rectangle's corners are START and END.
  164. With a prefix (or a FILL) argument, also fill lines where nothing has
  165. to be deleted."
  166. (interactive "*r\nP")
  167. (apply-on-rectangle 'delete-rectangle-line start end fill))
  168. ;;;###autoload
  169. (defun delete-extract-rectangle (start end &optional fill)
  170. "Delete the contents of the rectangle with corners at START and END.
  171. Return it as a list of strings, one for each line of the rectangle.
  172. When called from a program the rectangle's corners are START and END.
  173. With an optional FILL argument, also fill lines where nothing has to be
  174. deleted."
  175. (let ((lines (list nil)))
  176. (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
  177. (nreverse (cdr lines))))
  178. ;;;###autoload
  179. (defun extract-rectangle (start end)
  180. "Return the contents of the rectangle with corners at START and END.
  181. Return it as a list of strings, one for each line of the rectangle."
  182. (let ((lines (list nil)))
  183. (apply-on-rectangle 'extract-rectangle-line start end lines)
  184. (nreverse (cdr lines))))
  185. (defvar killed-rectangle nil
  186. "Rectangle for `yank-rectangle' to insert.")
  187. ;;;###autoload
  188. (defun kill-rectangle (start end &optional fill)
  189. "Delete the region-rectangle and save it as the last killed one.
  190. When called from a program the rectangle's corners are START and END.
  191. You might prefer to use `delete-extract-rectangle' from a program.
  192. With a prefix (or a FILL) argument, also fill lines where nothing has to be
  193. deleted.
  194. If the buffer is read-only, Emacs will beep and refrain from deleting
  195. the rectangle, but put it in the kill ring anyway. This means that
  196. you can use this command to copy text from a read-only buffer.
  197. \(If the variable `kill-read-only-ok' is non-nil, then this won't
  198. even beep.)"
  199. (interactive "r\nP")
  200. (condition-case nil
  201. (setq killed-rectangle (delete-extract-rectangle start end fill))
  202. ((buffer-read-only text-read-only)
  203. (setq killed-rectangle (extract-rectangle start end))
  204. (if kill-read-only-ok
  205. (progn (message "Read only text copied to kill ring") nil)
  206. (barf-if-buffer-read-only)
  207. (signal 'text-read-only (list (current-buffer)))))))
  208. ;;;###autoload
  209. (defun yank-rectangle ()
  210. "Yank the last killed rectangle with upper left corner at point."
  211. (interactive "*")
  212. (insert-rectangle killed-rectangle))
  213. ;;;###autoload
  214. (defun insert-rectangle (rectangle)
  215. "Insert text of RECTANGLE with upper left corner at point.
  216. RECTANGLE's first line is inserted at point, its second
  217. line is inserted at a point vertically under point, etc.
  218. RECTANGLE should be a list of strings.
  219. After this command, the mark is at the upper left corner
  220. and point is at the lower right corner."
  221. (let ((lines rectangle)
  222. (insertcolumn (current-column))
  223. (first t))
  224. (push-mark)
  225. (while lines
  226. (or first
  227. (progn
  228. (forward-line 1)
  229. (or (bolp) (insert ?\n))
  230. (move-to-column insertcolumn t)))
  231. (setq first nil)
  232. (insert-for-yank (car lines))
  233. (setq lines (cdr lines)))))
  234. ;;;###autoload
  235. (defun open-rectangle (start end &optional fill)
  236. "Blank out the region-rectangle, shifting text right.
  237. The text previously in the region is not overwritten by the blanks,
  238. but instead winds up to the right of the rectangle.
  239. When called from a program the rectangle's corners are START and END.
  240. With a prefix (or a FILL) argument, fill with blanks even if there is
  241. no text on the right side of the rectangle."
  242. (interactive "*r\nP")
  243. (apply-on-rectangle 'open-rectangle-line start end fill)
  244. (goto-char start))
  245. (defun open-rectangle-line (startcol endcol fill)
  246. (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
  247. (unless (and (not fill)
  248. (= (point) (point-at-eol)))
  249. (indent-to endcol))))
  250. (defun delete-whitespace-rectangle-line (startcol _endcol fill)
  251. (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
  252. (unless (= (point) (point-at-eol))
  253. (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
  254. ;;;###autoload
  255. (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
  256. ;;;###autoload
  257. (defun delete-whitespace-rectangle (start end &optional fill)
  258. "Delete all whitespace following a specified column in each line.
  259. The left edge of the rectangle specifies the position in each line
  260. at which whitespace deletion should begin. On each line in the
  261. rectangle, all continuous whitespace starting at that column is deleted.
  262. When called from a program the rectangle's corners are START and END.
  263. With a prefix (or a FILL) argument, also fill too short lines."
  264. (interactive "*r\nP")
  265. (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
  266. (defvar string-rectangle-history nil)
  267. (defun string-rectangle-line (startcol endcol string delete)
  268. (move-to-column startcol t)
  269. (if delete
  270. (delete-rectangle-line startcol endcol nil))
  271. (insert string))
  272. ;;;###autoload
  273. (defun string-rectangle (start end string)
  274. "Replace rectangle contents with STRING on each line.
  275. The length of STRING need not be the same as the rectangle width.
  276. Called from a program, takes three args; START, END and STRING."
  277. (interactive
  278. (progn (barf-if-buffer-read-only)
  279. (list
  280. (region-beginning)
  281. (region-end)
  282. (read-string (format "String rectangle (default %s): "
  283. (or (car string-rectangle-history) ""))
  284. nil 'string-rectangle-history
  285. (car string-rectangle-history)))))
  286. (goto-char
  287. (apply-on-rectangle 'string-rectangle-line start end string t)))
  288. ;;;###autoload
  289. (defalias 'replace-rectangle 'string-rectangle)
  290. ;;;###autoload
  291. (defun string-insert-rectangle (start end string)
  292. "Insert STRING on each line of region-rectangle, shifting text right.
  293. When called from a program, the rectangle's corners are START and END.
  294. The left edge of the rectangle specifies the column for insertion.
  295. This command does not delete or overwrite any existing text."
  296. (interactive
  297. (progn (barf-if-buffer-read-only)
  298. (list
  299. (region-beginning)
  300. (region-end)
  301. (read-string (format "String insert rectangle (default %s): "
  302. (or (car string-rectangle-history) ""))
  303. nil 'string-rectangle-history
  304. (car string-rectangle-history)))))
  305. (apply-on-rectangle 'string-rectangle-line start end string nil))
  306. ;;;###autoload
  307. (defun clear-rectangle (start end &optional fill)
  308. "Blank out the region-rectangle.
  309. The text previously in the region is overwritten with blanks.
  310. When called from a program the rectangle's corners are START and END.
  311. With a prefix (or a FILL) argument, also fill with blanks the parts of the
  312. rectangle which were empty."
  313. (interactive "*r\nP")
  314. (apply-on-rectangle 'clear-rectangle-line start end fill))
  315. (defun clear-rectangle-line (startcol endcol fill)
  316. (let ((pt (point-at-eol)))
  317. (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
  318. (if (and (not fill)
  319. (<= (save-excursion (goto-char pt) (current-column)) endcol))
  320. (delete-region (point) pt)
  321. ;; else
  322. (setq pt (point))
  323. (move-to-column endcol t)
  324. (setq endcol (current-column))
  325. (delete-region pt (point))
  326. (indent-to endcol)))))
  327. ;; Line numbers for `rectangle-number-line-callback'.
  328. (defvar rectangle-number-line-counter)
  329. (defun rectangle-number-line-callback (start _end format-string)
  330. (move-to-column start t)
  331. (insert (format format-string rectangle-number-line-counter))
  332. (setq rectangle-number-line-counter
  333. (1+ rectangle-number-line-counter)))
  334. (defun rectange--default-line-number-format (start end start-at)
  335. (concat "%"
  336. (int-to-string (length (int-to-string (+ (count-lines start end)
  337. start-at))))
  338. "d "))
  339. ;;;###autoload
  340. (defun rectangle-number-lines (start end start-at &optional format)
  341. "Insert numbers in front of the region-rectangle.
  342. START-AT, if non-nil, should be a number from which to begin
  343. counting. FORMAT, if non-nil, should be a format string to pass
  344. to `format' along with the line count. When called interactively
  345. with a prefix argument, prompt for START-AT and FORMAT."
  346. (interactive
  347. (if current-prefix-arg
  348. (let* ((start (region-beginning))
  349. (end (region-end))
  350. (start-at (read-number "Number to count from: " 1)))
  351. (list start end start-at
  352. (read-string "Format string: "
  353. (rectange--default-line-number-format
  354. start end start-at))))
  355. (list (region-beginning) (region-end) 1 nil)))
  356. (unless format
  357. (setq format (rectange--default-line-number-format start end start-at)))
  358. (let ((rectangle-number-line-counter start-at))
  359. (apply-on-rectangle 'rectangle-number-line-callback
  360. start end format)))
  361. (provide 'rect)
  362. ;;; rect.el ends here