Function: markdown-ts-table-convert-region

markdown-ts-table-convert-region is an interactive and byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts-table-convert-region BEG END &optional HEADER-LINE REPLACE FORMAT)

Documentation

Convert the comma-separated region BEG to END to a Markdown table.

If HEADER-LINE is non-nil, use the first line of the region as the table column header. If nil, infer the number of columns and synthesize column names from the first line.

Note: Body columns beyond the provided or inferred number of columns are dropped.

If REPLACE is non-nil, overwrite the region BEG END with the Markdown table, otherwise insert the table after END.

FORMAT is one of the symbols csv for comma-separated values, or tsv for tab-separated values. If any other value, use csv.

With a single prefix argument, HEADER-LINE is non-nil. With a double prefix argument, REPLACE is non-nil. With a triple prefix argument, both are non-nil.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts-table-convert-region ( beg end
                                          &optional
                                          header-line replace format)
  "Convert the comma-separated region BEG to END to a Markdown table.
If HEADER-LINE is non-nil, use the first line of the region as the table
column header.  If nil, infer the number of columns and synthesize
column names from the first line.

Note: Body columns beyond the provided or inferred number of columns are
dropped.

If REPLACE is non-nil, overwrite the region BEG END with the Markdown
table, otherwise insert the table after END.

FORMAT is one of the symbols `csv' for comma-separated values, or `tsv'
for tab-separated values.  If any other value, use `csv'.

With a single prefix argument, HEADER-LINE is non-nil.
With a double prefix argument, REPLACE is non-nil.
With a triple prefix argument, both are non-nil."
  (interactive "R")
  (markdown-ts--barf-if-not-mode 'markdown-ts-table-convert-region)

  (cond ((equal current-prefix-arg '(4))
         (setq header-line t))
        ((equal current-prefix-arg '(16))
         (setq replace t))
        ((equal current-prefix-arg '(64))
         (setq header-line t
               replace t)))
  (let (rows)
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (< (point) (point-max))
        (pcase format
          ('tsv
             (push
              (split-string
               (buffer-substring-no-properties (point) (pos-eol)) "\t")
              rows))
           (_
            (let (row)
              (while (re-search-forward
                      "\"\\(?:[^\"]\\|\"\"\\)*\"\\|[^,]+\\|\"\""
                      (pos-eol) t)
                (push
                 (replace-regexp-in-string
                  "\"\"" "\""
                  (replace-regexp-in-string
                   "\\`\"\\(\.*\\)\"\\'"
                   "\\1"
                   (substring-no-properties (match-string 0))))
                 row))
              (push (nreverse row) rows))))
        (forward-line 1))
        (setq rows (nreverse rows))
        (cond (replace (delete-region beg end))
              (t (goto-char end)))
        (if header-line
            (markdown-ts-table-insert-table (1- (length rows))
                                            (length (car rows))
                                            (car rows)
                                            (cdr rows))
          (markdown-ts-table-insert-table (length rows)
                                          (length (car rows))
                                          nil
                                          rows)))))