From 2c2823a62d61e71e1b952015630bc1497c394b73 Mon Sep 17 00:00:00 2001 From: Daneel Date: Mon, 23 Feb 2026 15:06:38 +0100 Subject: [PATCH] restore: original org-auto-tabularx with inhibit-modification-hooks (worked on Mac1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- config.el | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/config.el b/config.el index 6a8eb99..ac8fc5e 100644 --- a/config.el +++ b/config.el @@ -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)