Function: markdown-ts-table-align-table
markdown-ts-table-align-table is an interactive and byte-compiled
function defined in markdown-ts-mode.el.gz.
Signature
(markdown-ts-table-align-table &optional ALIGN-CELLS)
Documentation
Align the Markdown table at point.
Adjust each column's width to accommodate its widest constituent cell.
If ALIGN-CELLS is non-nil, or with a prefix argument, apply the
delimiter row's alignment to cell content. ALIGN-CELLS is non-nil if
the user option markdown-ts-table-align-features includes
justify-cells.
Anchor the columns to the delimiter row. Ignore rows that contain
columns beyond the delimiter row.
If not in a table, do nothing.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts-table-align-table (&optional align-cells)
"Align the Markdown table at point.
Adjust each column's width to accommodate its widest constituent cell.
If ALIGN-CELLS is non-nil, or with a prefix argument, apply the
delimiter row's alignment to cell content. ALIGN-CELLS is non-nil if
the user option `markdown-ts-table-align-features' includes
`justify-cells'.
Anchor the columns to the delimiter row. Ignore rows that contain
columns beyond the delimiter row.
If not in a table, do nothing."
(interactive "P")
(markdown-ts--barf-if-not-mode 'markdown-ts-table-align-table)
(when-let* ((at-table (markdown-ts-at-table-p nil t))
(table (cdr at-table))
(beg (treesit-node-start table))
(end (treesit-node-end table))
(_ (markdown-ts--table-tick-stale-p beg))
(rows (treesit-node-children table 'named))
(nrows (length rows))
(delimiter-row (treesit-search-subtree
table
"\\`pipe_table_delimiter_row\\'"))
(aligners (markdown-ts--table-aligners table))
(cols (treesit-node-children delimiter-row 'named))
(ncols (length cols))
(col-widths (make-hash-table :size ncols :test 'equal))
(cell-text (make-hash-table :size (* nrows ncols) :test 'equal)))
(setq align-cells (or align-cells
current-prefix-arg
(memq 'justify-cells markdown-ts-table-align-features)))
(without-restriction
;; Collect cell text and compute max column widths.
;; Do not use the delimiter row for width computation.
(dotimes (x nrows)
(let* ((row (nth x rows))
(cells (treesit-node-children row 'named)))
(dotimes (y ncols)
;; Skip nil cells--the parser got confused.
(when-let* ((cell (nth y cells))
(text (string-trim-right
(treesit-node-text cell 'no-property)))
(w (length text))
(w0 (or (gethash y col-widths) 0)))
;; Elide delimiter widths.
(unless (eq x 1)
(when (> w w0)
(puthash y w col-widths)))
(puthash (cons x y) text cell-text)))))
(let ((source-buffer (current-buffer)))
(with-temp-buffer
(dotimes (x nrows)
(dotimes (y ncols)
(let* ((width (or (gethash y col-widths)
markdown-ts-table-default-column-width))
(align (or (plist-get (nth y aligners) :align)
'unspecified))
;; Confused cells have nil text.
(text (or (gethash (cons x y) cell-text) ""))
(text0 (markdown-ts--table-align-cell
text width
(if align-cells
align
'unspecified))))
(cond
;; Header row.
((eq x 0)
(insert "|" text0))
;; Delimiter row. Use unaligned text.
((eq x 1)
(insert "|"
(markdown-ts--table-make-aligner width align)))
;; Body row.
(t
(insert "|" text0)))))
(insert "|" "\n"))
(let ((temp-buffer (current-buffer)))
(with-current-buffer source-buffer
(replace-region-contents beg end temp-buffer))))))
(markdown-ts--table-tick-update beg)))