diff --git a/elisp/csvivify.el b/elisp/csvivify.el new file mode 100644 index 0000000..48da652 --- /dev/null +++ b/elisp/csvivify.el @@ -0,0 +1,26 @@ +;;; csvivify.el --- Turn org or markdown tables into csv in-place -*- lexical-binding: t -*- + +;;; Commentary: + +;; For most use-cases, this is unnecessary, as org tables can already convert +;; to and from markdown and CSV, but it was a fun exercise to write. + +;;; Code: + +(defun csvivify () + "Make org or markdown table into csv in-place." + (interactive) + (when (region-active-p) + (let* ((from (region-beginning)) + (to (region-end)) + (remainder (- (point-max) to))) + (flush-lines "\|[-\\+]+\|" from to) + (flush-lines "^|[\s|:-]+|$" from to) + (save-excursion + (while (re-search-forward "[\s]+|[\s]+" (- (point-max) remainder) t) + (replace-match ","))) + (save-excursion + (while (re-search-forward "^|\s+\\|\s+|$" (- (point-max) remainder) t) + (replace-match "")))))) + +;;; csvivify.el ends here