From 30bfa6be0134b24d02896e4db70963c11fc49ec9 Mon Sep 17 00:00:00 2001 From: BirDt Date: Wed, 24 Sep 2025 22:48:56 +0800 Subject: [PATCH] Add csvivify --- elisp/csvivify.el | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 elisp/csvivify.el 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