Function: markdown-ts-table-insert-table

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

Signature

(markdown-ts-table-insert-table &optional NROWS NCOLS HEADINGS BODY)

Documentation

Insert a Markdown pipe table.

NROWS is the number of rows in the new table and defaults to 3.

NCOLS is the number of columns and defaults to 3.

HEADINGS is a list of headings and defaults to left-aligned synthesized column names. If NCOLS is nil, use HEADINGS length. If both NCOLS and HEADINGS are non-nil, use up to NCOLS headings, synthesizing the remainder.

Each heading in the list can be a string column name or a cons of the form (name . alignment) where alignment is one of the symbols left, right, or center. If a string, default alignment is unspecified.

If BODY is non-nil, it is a list of NROWS rows where each row is a list of NCOLS body cells.

For convenience and aesthetics, pad each body cell to the width of its column's header, and accommodate alignment indicators. If a BODY cell is longer than its column header, insert it unchanged.

Note: A blank line is needed before the table for it to properly render. If the line preceding point is not blank, insert one.

Note: Due to an issue with the tree-sitter Markdown grammar, a row of empty cells causes a parsing error. To avoid this, an empty row will contain a single period in its first cell.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
;;;; Commands:

(defun markdown-ts-table-insert-table (&optional nrows ncols headings body)
  "Insert a Markdown pipe table.
NROWS is the number of rows in the new table and defaults to 3.

NCOLS is the number of columns and defaults to 3.

HEADINGS is a list of headings and defaults to left-aligned synthesized
column names.  If NCOLS is nil, use HEADINGS length.  If both NCOLS and
HEADINGS are non-nil, use up to NCOLS headings, synthesizing the
remainder.

Each heading in the list can be a string column name or a cons of the
form (name . alignment) where alignment is one of the symbols `left',
`right', or `center'.  If a string, default alignment is unspecified.

If BODY is non-nil, it is a list of NROWS rows where each row is a list
of NCOLS body cells.

For convenience and aesthetics, pad each body cell to the width of its
column's header, and accommodate alignment indicators.  If a BODY cell
is longer than its column header, insert it unchanged.

Note: A blank line is needed before the table for it to properly render.
If the line preceding `point' is not blank, insert one.

Note: Due to an issue with the tree-sitter Markdown grammar, a row of
empty cells causes a parsing error.  To avoid this, an empty row will
contain a single period in its first cell."
  (interactive)
  (setq nrows (or nrows 3))
  (setq ncols (or ncols (and headings (length headings)) 3))
  (setq headings
        (let (res)
          (dotimes (x ncols)
            (push (or (nth x headings)
                      (format "Column %d" (1+ x)))
                  res))
          (nreverse res)))
  (let ((heading-widths
         (let (res)
           (dolist (elt headings)
             (let ((str (if (consp elt) (car elt) elt)))
               (push (max markdown-ts-table-default-column-width
                          (length str))
                     res)))
           (nreverse res))))
    ;; If point is not at bol, insert a newline.
    (unless (eq (point) (pos-bol))
      (insert "\n"))
    ;; Insert an empty line if needed.
    (unless (save-excursion
              (forward-line -1)
              (eq (pos-bol) (pos-eol)))
      (insert "\n"))
    ;; Insert padded heading text.
    (insert "|")
    (dotimes (x ncols)
      (let* ((elt (nth x headings))
             (str (if (consp elt) (car elt) elt))
             (width (max 3 (nth x heading-widths)))
             (pad-width (- width (length str))))
        (insert str (make-string pad-width ?\s) "|")))
    (insert "\n")
    ;; Insert padded heading aligners.
    (insert "|")
    (dotimes (x ncols)
      (let* ((elt (nth x headings))
             (align (if (consp elt) (cdr elt) 'unspecified))
             (width (max 3 (nth x heading-widths))))
        (insert (markdown-ts--table-make-aligner width align)))
      (insert "|"))
    (insert "\n")
    ;; Insert BODY cells or empty cells.
    (dotimes (y nrows)
      (insert "|")
      (let ((row (nth y body)))
        (dotimes (x ncols)
          ;; TODO: Remove the placeholder character if the tree-sitter
          ;; grammar is repaired.
          (let* ((str (or (nth x row) (and (eq x 0) ".") ""))
                 (strlen (length str))
                 (width (nth x heading-widths))
                 (pad-width (max 0 (- width strlen))))
            (insert str
                    (make-string pad-width ?\s)
                    "|")))
        (insert "\n")))))