branch: externals/org
commit c0ae2983939179edf815443a0874279a807618b9
Author: Morgan Smith <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    Preserve "Local Variables" block without preserving following text
    
    * lisp/org-macs.el (org-preserve-local-variables): Extract "Local
    Variables" block exactly to prevent preserving text that follows the
    block.
    
    Reported-by: Rens Oliemans <[email protected]>
    Link: https://list.orgmode.org/[email protected]
---
 lisp/org-macs.el | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index aabaf13d96..74d7902ba6 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -252,18 +252,29 @@ This function is only useful when called from Agenda 
buffer."
               (org-with-wide-buffer
                (goto-char (point-max))
                (let ((case-fold-search t))
-                 (and (re-search-backward "^[ \t]*# +Local Variables:"
-                                          (max (- (point) 3000) 1)
-                                          t)
-                      (delete-and-extract-region (point) (point-max))))))
+                 (and (re-search-backward
+                       ,(rx-let ((prefix
+                                  (seq line-start (zero-or-more whitespace)
+                                       "#" (one-or-more whitespace))))
+                          (rx prefix "Local Variables:"
+                              (one-or-more anychar)
+                              prefix "End:"
+                              (zero-or-more whitespace) (optional "\n")))
+                       (max (- (point) 3000) 1)
+                       t)
+                      (cons (match-beginning 0)
+                            (delete-and-extract-region (match-beginning 0)
+                                                         (match-end 0)))))))
              (,tick-counter-before (buffer-modified-tick)))
          (unwind-protect (progn ,@body)
            (when ,local-variables
              (org-with-wide-buffer
-              (goto-char (point-max))
-              (unless (bolp) (insert "\n"))
               (let ((modified (< ,tick-counter-before (buffer-modified-tick))))
-                (insert ,local-variables)
+                (if (not modified)
+                    (goto-char (car ,local-variables))
+                  (goto-char (point-max))
+                  (unless (bolp) (insert "\n")))
+               (insert (cdr ,local-variables))
                 (unless modified
                   (restore-buffer-modified-p nil))))))))))
 

Reply via email to