Function: markdown-ts-table-insert-row

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

Signature

(markdown-ts-table-insert-row &optional ABOVE CLONE)

Documentation

Insert a table row below point.

If point is in the header, insert below the first row of the table. If ABOVE is non-nil and is not below, or with a prefix argument, insert the row above point. If CLONE is non-nil, clone the current row, otherwise insert empty cells. If point is not at a table, do nothing.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts-table-insert-row (&optional above clone)
  "Insert a table row below point.
If point is in the header, insert below the first row of the table.
If ABOVE is non-nil and is not `below', or with a prefix argument,
insert the row above point.
If CLONE is non-nil, clone the current row, otherwise insert empty
cells.
If point is not at a table, do nothing."
  (interactive "P")
  (markdown-ts--barf-if-not-mode 'markdown-ts-table-insert-row)
  (setq above (cond ((eq above 'below) nil)
                    (above above)
                    (current-prefix-arg t)))
  (when-let* ((at-table (markdown-ts-at-table-p nil t))
              (pos (car at-table))
              (table (cdr at-table))
              (delimiter-row (treesit-search-subtree
                              table
                              "\\`pipe_table_delimiter_row\\'"))
              (ncols (treesit-node-child-count delimiter-row 'named))
              (heading-widths
               (let (res)
                 (dolist (elt (treesit-node-children delimiter-row 'named))
                   (push (length (treesit-node-text elt)) res))
                 (nreverse res))))
    (let* ((near (markdown-ts--table-body-row-near-pos pos))
           (row (car near))
           (current-column (current-column)))
      ;; If row is nil, there are zero rows so add one below the
      ;; delimiter-row.
      (unless row
        (setq clone nil)
        (setq row delimiter-row)
        (setq above nil))
      (save-excursion
        (cond (above
               (goto-char (treesit-node-start row)))
              (t
               (goto-char (treesit-node-end row))
               (insert "\n")))
        (cond (clone
               ;; The clone strategy is WYSIWYG.  Just copy the text
               ;; and don't fuss with cell content and pipe symbols.
               ;; The grammar includes the final newline (and
               ;; sometimes not).
               (insert (string-trim-right
                        (treesit-node-text row 'no-property) "\n")))
              (t
               (insert "|")
               (dotimes (x ncols)
                 (let ((pad-width (nth x heading-widths)))
                   ;; TODO: Remove the placeholder character if the
                   ;; tree-sitter grammar is repaired.
                   (insert (if (eq x 0) "." "")
                           (make-string (- pad-width (if (eq x 0) 1 0)) ?\s)
                           "|")))))
        (when above
          (insert "\n")))
      (cond (above
             (when (> current-column 0)
               (with-suppressed-warnings ((interactive-only previous-line))
                 (previous-line))))
            (t (with-suppressed-warnings ((interactive-only next-line))
                 (next-line)))))))