From d024fe96ad889097d025a87dae3316acc44299f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= Date: Sun, 25 Jun 2023 13:38:21 +0200 Subject: [PATCH] org-src--contents-for-write-back: simplify the case of LaTeX fragments * lisp/org-src.el (org-src--contents-for-write-back): Extract special case logic for LaTeX fragments. (org-edit-latex-fragment): Trim the contents to be inserted in the original buffer. --- lisp/org-src.el | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lisp/org-src.el b/lisp/org-src.el index f15ba8e99..121e59241 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -466,7 +466,6 @@ Assume point is in the corresponding edit buffer." org-src--content-indentation 0)))) (use-tabs? (and (> org-src--tab-width 0) t)) - (preserve-fl (eq org-src--source-type 'latex-fragment)) (source-tab-width org-src--tab-width) (contents (org-with-wide-buffer (let ((eol (line-end-position))) @@ -479,16 +478,13 @@ Assume point is in the corresponding edit buffer." ;; Reproduce indentation parameters from source buffer. (setq indent-tabs-mode use-tabs?) (when (> source-tab-width 0) (setq tab-width source-tab-width)) - ;; Apply WRITE-BACK function on edit buffer contents. (insert (org-no-properties (car contents))) (setq marker (point-marker)) (insert (org-no-properties (car (cdr contents)))) (goto-char (point-min)) - (when (functionp write-back) (save-excursion (funcall write-back))) ;; Add INDENTATION-OFFSET to every line in buffer, ;; unless indentation is meant to be preserved. (when (> indentation-offset 0) - (when preserve-fl (forward-line)) (while (not (eobp)) (skip-chars-forward " \t") (when (or (not (eolp)) ; not a blank line @@ -498,6 +494,9 @@ Assume point is in the corresponding edit buffer." (delete-region (line-beginning-position) (point)) (indent-to (+ i indentation-offset)))) (forward-line))) + ;; Apply WRITE-BACK function on edit buffer contents. + (goto-char (point-min)) + (when (functionp write-back) (funcall write-back)) (set-marker marker nil)))) (defun org-src--edit-element @@ -1150,7 +1149,12 @@ Throw an error when not at such a table." (lambda () ;; Blank lines break things, replace with a single newline. (while (re-search-forward "\n[ \t]*\n" nil t) (replace-match "\n")) - ;; If within a table a newline would disrupt the structure, + ;; Trim contents: `org-src--contents-for-write-back' may have + ;; added indentation at the beginning, which we remove. + (goto-char (point-min)) + (skip-chars-forward " \t") + (delete-region (point-min) (point)) + ;; If within a table a newline would disrupt the structure, ;; so remove newlines. (goto-char (point-min)) (when (org-element-lineage context '(table-cell)) -- 2.41.0