diff --git a/config.el b/config.el index 53ed171..2fb5b6d 100644 --- a/config.el +++ b/config.el @@ -325,48 +325,27 @@ Bound to cmd+v in org-mode and markdown-mode." ;;; ORG MODE — LATEX EXPORT ;;; ============================================================ -;; 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 "|")))))) +;; LaTeX table export: replace tabular → tabularx{\textwidth} +;; Filter approach: works on rendered LaTeX string, never touches the org buffer, +;; so no hook recursion (the old before-processing-hook + insert approach caused +;; 'max-lisp-eval-depth' via org-element-cache after-change hooks). +(defun my/org-latex-table-to-tabularx (table _backend _info) + "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) -(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 -(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) +(after! ox-latex + (add-to-list 'org-export-filter-table-functions + #'my/org-latex-table-to-tabularx) + (add-to-list 'org-latex-packages-alist '("" "tabularx"))) ;; Optional: enable booktabs style (horizontal rules in tables) ;; (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 '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 - :when (not (display-graphic-p)) + :when (and (not (display-graphic-p)) + (< emacs-major-version 31)) :after corfu :config (corfu-terminal-mode +1))