Add csvivify

This commit is contained in:
BirDt 2025-09-24 22:48:56 +08:00
parent 04d1f4e50a
commit 30bfa6be01

26
elisp/csvivify.el Normal file
View file

@ -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