fix(org-latex+corfu): filter-based tabularx, disable corfu-terminal on Emacs 31+

org-latex: replace before-processing-hook+insert with org-export-filter-table-functions.
  - Old approach (insert in hook) triggered org-element-cache after-change hooks
    causing infinite recursion / max-lisp-eval-depth even with inhibit-modification-hooks.
  - New: my/org-latex-table-to-tabularx filter transforms rendered LaTeX string,
    no buffer modification, no hook recursion possible.
  - Removed dead helpers: my/org-count-table-columns, my/org-table-attr-latex-spec.

corfu-terminal: add (< emacs-major-version 31) guard.
  - Emacs 31 supports corfu natively in terminal; corfu-terminal causes
    popup positioning issues (jumping) on Emacs 31+.
This commit is contained in:
2026-02-23 14:53:41 +01:00
parent f36e515501
commit 46c9327dd0

View File

@@ -325,48 +325,27 @@ Bound to cmd+v in org-mode and markdown-mode."
;;; ORG MODE — LATEX EXPORT ;;; ORG MODE — LATEX EXPORT
;;; ============================================================ ;;; ============================================================
;; Count data columns in an Org table line ;; LaTeX table export: replace tabular → tabularx{\textwidth}
(defun my/org-count-table-columns (line) ;; Filter approach: works on rendered LaTeX string, never touches the org buffer,
"Count the number of data columns in Org table LINE." ;; so no hook recursion (the old before-processing-hook + insert approach caused
(length (cl-remove-if ;; 'max-lisp-eval-depth' via org-element-cache after-change hooks).
(lambda (s) (string-match-p "^-*$" (string-trim s))) (defun my/org-latex-table-to-tabularx (table _backend _info)
(cdr (butlast (split-string line "|")))))) "Replace \\begin{tabular} with \\begin{tabularx}{\\textwidth} in LaTeX output."
(when (and (stringp table) (string-match "\\\\begin{tabular}" table))
(setq table (replace-regexp-in-string
"\\\\begin{tabular}{\\([^}]*\\)}"
"\\\\begin{tabularx}{\\\\textwidth}{\\1}"
table))
(setq table (replace-regexp-in-string
"\\\\end{tabular}"
"\\\\end{tabularx}"
table)))
table)
;; Generate tabularx column spec: first column left-aligned, rest Y (auto-width) (after! ox-latex
(defun my/org-table-attr-latex-spec (ncols) (add-to-list 'org-export-filter-table-functions
"Return tabularx column spec for NCOLS columns: first l, rest Y." #'my/org-latex-table-to-tabularx)
(concat "l" (make-string (max 0 (1- ncols)) ?Y))) (add-to-list 'org-latex-packages-alist '("" "tabularx")))
;; Automatically insert #+ATTR_LATEX tabularx before tables on LaTeX export
(defun my/org-auto-tabularx (backend)
"Insert #+ATTR_LATEX tabularx before each table when exporting to LaTeX.
Uses inhibit-modification-hooks to prevent recursive hook calls during insert."
(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) ;; Optional: enable booktabs style (horizontal rules in tables)
;; (setq org-latex-tables-booktabs t) ;; (setq org-latex-tables-booktabs t)
@@ -561,9 +540,12 @@ and optional priority indicator [#A]."
(add-hook 'prog-mode-hook #'martin/cape-capf-setup) (add-hook 'prog-mode-hook #'martin/cape-capf-setup)
(add-hook 'text-mode-hook #'martin/cape-capf-setup)) (add-hook 'text-mode-hook #'martin/cape-capf-setup))
;; Corfu popup in terminal (iTerm2 / SSH / tmux) ;; Corfu popup in terminal — only needed for Emacs < 31 without child-frame support.
;; Emacs 31 handles corfu natively even in terminal; loading corfu-terminal
;; there causes popup positioning issues ("jumping").
(use-package! corfu-terminal (use-package! corfu-terminal
:when (not (display-graphic-p)) :when (and (not (display-graphic-p))
(< emacs-major-version 31))
:after corfu :after corfu
:config :config
(corfu-terminal-mode +1)) (corfu-terminal-mode +1))