Function: markdown-ts-table-delete-column

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

Signature

(markdown-ts-table-delete-column)

Documentation

Delete the table column at point.

Point can be in the table header or body. 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-delete-column ()
  "Delete the table column at point.
Point can be in the table header or body.
If point is not at a table, do nothing."
  (interactive)
  (markdown-ts--barf-if-not-mode 'markdown-ts-table-delete-column)
  (when-let* ((at-table (markdown-ts-at-table-p nil t))
              (pos (car at-table))
              (table (cdr at-table))
              (cell (markdown-ts--table-node-cell nil pos))
              (row (markdown-ts--table-node-row cell))
              (cols (treesit-node-children row 'named))
              (ncols (length cols)))
    (let ((table-column (markdown-ts--table-compute-node-column row cell)))
      (unless table-column
        (error "Could not compute the table column"))
      ;; NOTE: GFM tables allow non-uniform table rows.  The current row
      ;; could have a differing number of columns from other rows.  We
      ;; silently do nothing to a row that does not extend to the
      ;; computed column.
      ;;
      ;; NOTE: The grammar annoyingly ignores cell leading whitespace
      ;; but does include trailing whitespace so cells are bounded by
      ;; the first graph character up to the last character just before
      ;; the pipe symbol, including whitespace.
      ;;
      ;; The grammar also considers the trailing pipe symbol to be a
      ;; part of its preceding cell so that's what we delete when we
      ;; delete a column's cell; i.e., if there is a pipe before the
      ;; first column, we delete it.
      (let* ((adj 0) ; Adjust node position offsets by deleted text.
             (rows (treesit-node-children table 'named))
             (nrows (length rows)))
        (save-excursion
          (without-restriction
            (dotimes (x nrows)
              ;; If cell is nil, it is beyond the table-column and its
              ;; row is ignored.
              (when-let* ((row (nth x rows))
                          (cols (treesit-node-children row 'named))
                          (ncols (length cols))
                          (cell (nth table-column cols))
                          (cell-beg (treesit-node-start cell))
                          (cell-end (treesit-node-end cell))
                          ;; Adjust beg to trim the grammar
                          ;; unaccounted-for leading whitespace.
                          (beg (cond
                                ;; If this is the sole column, delete from bol.
                                ((eq ncols 1)
                                 (goto-char (- cell-beg adj))
                                 (pos-bol))
                                ;; If this is the first column, delete
                                ;; from the pipe, if there is one,
                                ;; otherwise cell-beg.
                                ((eq table-column 0)
                                 (let ((p (- cell-beg adj)))
                                   (goto-char p)
                                   (goto-char (pos-bol))
                                   (if (search-forward "|" p t)
                                       (1- (point))
                                     p)))
                                ;; If this is the final column, delete from the prior pipe symbol.
                                ((eq table-column (1- ncols))
                                 (let ((p (- (treesit-node-end
                                              (nth (1- table-column) cols)) adj)))
                                   (goto-char p)
                                   (if (search-forward "|" (- cell-beg adj) t)
                                       (point)
                                     p)))
                                ;; Otherwise, start at the end of the
                                ;; prior cell.
                                (t
                                 (- (treesit-node-end
                                         (nth (1- table-column) cols))
                                    adj))))
                          ;; Adjust end to trim the trailing pipe symbol
                          ;; considered by the grammar to be part of its
                          ;; preceding cell.
                          (end (cond
                                ;; If this is the sole column, delete until eol.
                                ((eq ncols 1)
                                 (goto-char (- cell-beg adj))
                                 (pos-eol))
                                ;; If this is the final column, delete until eol.
                                ((eq table-column (1- ncols))
                                 (goto-char (- cell-end adj))
                                 (pos-eol))
                                ;; Otherwise, look forward from cell-end
                                ;; (which includes the cell's trailing
                                ;; whitespace) for a pipe symbol.
                                (t
                                 (goto-char (- cell-end adj))
                                 (if (re-search-forward "|+?" (pos-eol) t)
                                     (1- (point))
                                   cell-end)))))
                (delete-region beg end)
                (setq adj (+ adj (- end beg)))))))))))