diff --git a/config.el b/config.el index 2fc4ba0..84a264e 100644 --- a/config.el +++ b/config.el @@ -339,30 +339,33 @@ Bound to cmd+v in org-mode and markdown-mode." ;; 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." + "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) - (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)))))))) + (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)