Function: markdown-table-convert-region
markdown-table-convert-region is an interactive and byte-compiled
function defined in markdown-mode.el.
Signature
(markdown-table-convert-region BEGIN END &optional SEPARATOR)
Documentation
Convert region from BEGIN to END to table with SEPARATOR.
If every line contains at least one TAB character, the function assumes that the material is tab separated (TSV). If every line contains a comma, comma-separated values (CSV) are assumed. If not, lines are split at whitespace into cells.
You can use a prefix argument to force a specific separator:
C-u (universal-argument) once forces CSV, C-u (universal-argument)
twice forces TAB, and C-u (universal-argument) three times will
prompt for a regular expression to match the separator, and a
numeric argument N indicates that at least N consecutive
spaces, or alternatively a TAB should be used as the separator.
Key Bindings
Source Code
;; Defined in ~/.emacs.d/elpa/markdown-mode-20260321.143/markdown-mode.el
(defun markdown-table-convert-region (begin end &optional separator)
"Convert region from BEGIN to END to table with SEPARATOR.
If every line contains at least one TAB character, the function
assumes that the material is tab separated (TSV). If every line
contains a comma, comma-separated values (CSV) are assumed. If
not, lines are split at whitespace into cells.
You can use a prefix argument to force a specific separator:
\\[universal-argument] once forces CSV, \\[universal-argument]
twice forces TAB, and \\[universal-argument] three times will
prompt for a regular expression to match the separator, and a
numeric argument N indicates that at least N consecutive
spaces, or alternatively a TAB should be used as the separator."
(interactive "r\nP")
(let* ((begin (min begin end)) (end (max begin end)) re)
(goto-char begin) (beginning-of-line 1)
(setq begin (point-marker))
(goto-char end)
(if (bolp) (backward-char 1) (end-of-line 1))
(setq end (point-marker))
(when (equal separator '(64))
(setq separator (read-regexp "Regexp for cell separator: ")))
(unless separator
;; Get the right cell separator
(goto-char begin)
(setq separator
(cond
((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
((not (re-search-forward "^[^\n,]+$" end t)) '(4))
(t 1))))
(goto-char begin)
(if (equal separator '(4))
;; Parse CSV
(while (< (point) end)
(cond
((looking-at "^") (insert "| "))
((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
(replace-match "\\1") (if (looking-at "\"") (insert "\"")))
((looking-at "[^,\n]+") (goto-char (match-end 0)))
((looking-at "[ \t]*,") (replace-match " | "))
(t (beginning-of-line 2))))
(setq re
(cond
((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
((equal separator '(16)) "^\\|\t")
((integerp separator)
(if (< separator 1)
(user-error "Cell separator must contain one or more spaces")
(format "^ *\\| *\t *\\| \\{%d,\\}\\|$" separator)))
((stringp separator) (format "^ *\\|%s" separator))
(t (error "Invalid cell separator"))))
(let (finish)
(while (and (not finish) (re-search-forward re end t))
(if (eolp)
(progn
(replace-match "|" t t)
(forward-line 1)
(when (eobp)
(setq finish t)))
(replace-match "| " t t)))))
(goto-char begin)
(when markdown-table-align-p
(markdown-table-align))))