restore: original org-auto-tabularx with inhibit-modification-hooks (worked on Mac1)

Restore the hook-based approach from acb3875 (original working version) + the
inhibit-modification-hooks fix from 457b9d3 that made it work on Emacs 31.

457b9d3 confirmed working by Martin (PDF exported) — only issue was the syntax
warning from the stray paren. This commit has correct paren balance.

Also keep pdf-tools config and corfu-terminal Emacs 31 guard from 98b53cb.
This commit is contained in:
2026-02-23 15:06:38 +01:00
parent 98b53cb181
commit 2c2823a62d

View File

@@ -325,13 +325,49 @@ Bound to cmd+v in org-mode and markdown-mode."
;;; ORG MODE — LATEX EXPORT
;;; ============================================================
;; LaTeX table export: use tabularx as default environment.
;; Org natively supports tabularx: when environment is "tabularx" or "tabulary",
;; it automatically inserts the required width argument (\linewidth).
;; No buffer modification, no advice, no filter needed.
(after! ox-latex
(setq org-latex-default-table-environment "tabularx")
(add-to-list 'org-latex-packages-alist '("" "tabularx")))
;; Count data columns in an Org table line
(defun my/org-count-table-columns (line)
"Count the number of data columns in Org table LINE."
(length (cl-remove-if
(lambda (s) (string-match-p "^-*$" (string-trim s)))
(cdr (butlast (split-string line "|"))))))
;; Generate tabularx column spec: first column left-aligned, rest Y (auto-width)
(defun my/org-table-attr-latex-spec (ncols)
"Return tabularx column spec for NCOLS columns: first l, rest Y."
(concat "l" (make-string (max 0 (1- ncols)) ?Y)))
;; Automatically insert #+ATTR_LATEX tabularx before tables on LaTeX export.
;; inhibit-modification-hooks prevents org-element-cache from recursing
;; during the insert (which caused max-lisp-eval-depth on Emacs 31).
(defun my/org-auto-tabularx (backend)
"Insert #+ATTR_LATEX tabularx before each table when exporting to LaTeX."
(when (org-export-derived-backend-p backend 'latex)
(let ((inhibit-modification-hooks t))
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(cond
((looking-at "^|")
(let ((prev-line (save-excursion
(forward-line -1)
(buffer-substring-no-properties
(line-beginning-position) (line-end-position)))))
(when (not (string-match-p "^|" prev-line))
(when (not (string-match-p "^#\\+ATTR_LATEX" prev-line))
(let* ((table-line (buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(ncols (my/org-count-table-columns table-line))
(spec (my/org-table-attr-latex-spec ncols))
(attr (format "#+ATTR_LATEX: :environment tabularx :width \\textwidth :align %s\n"
spec)))
(when (> ncols 0)
(insert attr)))))
(forward-line))
(t
(forward-line)))))))))
(add-hook 'org-export-before-processing-hook #'my/org-auto-tabularx)
;; Optional: enable booktabs style (horizontal rules in tables)
;; (setq org-latex-tables-booktabs t)