Function: markdown-table-align

markdown-table-align is an interactive and byte-compiled function defined in markdown-mode.el.

Signature

(markdown-table-align)

Documentation

Align table at point.

This function assumes point is on a table.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/markdown-mode-20260321.143/markdown-mode.el
(defun markdown-table-align ()
  "Align table at point.
This function assumes point is on a table."
  (interactive)
  (let ((begin (markdown-table-begin))
        (end (copy-marker (markdown-table-end))))
    (markdown-table-save-cell
     (goto-char begin)
     (let* (fmtspec
            ;; Store table indent
            (indent (progn (looking-at "[ \t]*") (match-string 0)))
            ;; Split table in lines and save column format specifier
            (lines (mapcar (lambda (line)
                             (if (markdown--is-delimiter-row line)
                                 (progn (setq fmtspec (or fmtspec line)) nil)
                               line))
                           (markdown--split-string (buffer-substring begin end) "\n")))
            ;; Split lines in cells
            (cells (mapcar (lambda (l) (markdown--table-line-to-columns l))
                           (remq nil lines)))
            ;; Calculate maximum number of cells in a line
            (maxcells (if cells
                          (apply #'max (mapcar #'length cells))
                        (user-error "Empty table")))
            maxwidths)
       ;; Calculate maximum width for each column
       (dotimes (i maxcells)
         (let ((column (mapcar (lambda (x) (or (nth i x) "")) cells)))
           (push (apply #'max 1 (mapcar #'markdown--string-width column))
                 maxwidths)))
       (setq maxwidths (nreverse maxwidths))
       ;; Process column format specifier
       (setq fmtspec (markdown-table-colfmt fmtspec))
       ;; Compute formats needed for output of table lines
       (let ((hfmt (concat indent "|"))
             hfmt1 fmt (fmts fmtspec))
         (dolist (width maxwidths)
           (setq fmt (car fmts) fmts (cdr fmts))
           (cond ((equal fmt 'l) (setq hfmt1 ":%s-|"))
                 ((equal fmt 'r) (setq hfmt1 "-%s:|"))
                 ((equal fmt 'c) (setq hfmt1 ":%s:|"))
                 (t              (setq hfmt1 "-%s-|")))
           (setq hfmt (concat hfmt (format hfmt1 (make-string width ?-)))))
         ;; Replace modified lines only
         (dolist (line lines)
           (let ((line (if line
                           (concat indent "|"
                                   (markdown-table-align-raw (pop cells) fmtspec maxwidths)
                                   "|")
                         hfmt))
                 (previous (buffer-substring (point) (line-end-position))))
             (if (equal previous line)
                 (forward-line)
               (insert line "\n")
               (delete-region (point) (line-beginning-position 2))))))
       (set-marker end nil)))))