Function: markdown-ts-table-transpose-table
markdown-ts-table-transpose-table is an interactive and byte-compiled
function defined in markdown-ts-mode.el.gz.
Signature
(markdown-ts-table-transpose-table)
Documentation
Transpose the rows and columns of the Markdown table at point.
Use unspecified column alignment in the transposed table. If table's rows are not uniform with those on the delimiter row, signal an error. 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-transpose-table ()
"Transpose the rows and columns of the Markdown table at point.
Use unspecified column alignment in the transposed table.
If table's rows are not uniform with those on the delimiter row, signal
an error.
If not in a table, do nothing."
(interactive)
(markdown-ts--barf-if-not-mode 'markdown-ts-table-transpose-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))
(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))
(cell-text (make-hash-table :size (* nrows ncols) :test 'equal)))
(without-restriction
;; Collect cell text and invert coordinates.
(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))))
(puthash (cons y x) text cell-text)))))
(let ((source-buffer (current-buffer)))
(with-temp-buffer
(dotimes (x ncols)
;; Synthesize a delimiter row.
(when (eq x 1)
(dotimes (_y (1- nrows))
(insert "|"
(markdown-ts--table-make-aligner
(max 5 markdown-ts-table-default-column-width)
'unspecified)))
(insert "|" "\n"))
(dotimes (y nrows)
;; Skip the original delimiter row.
(unless (eq y 1)
;; Confused cells have nil text.
(let ((text (or (gethash (cons x y) cell-text) "")))
(insert "|" text))))
(insert "|" "\n"))
(let ((temp-buffer (current-buffer)))
(with-current-buffer source-buffer
(replace-region-contents beg end temp-buffer))))))
(when (or (eq markdown-ts-table-auto-align t)
(memq 'transpose markdown-ts-table-auto-align))
(markdown-ts-table-align-table))))