Function: org-preserve-local-variables

org-preserve-local-variables is a macro defined in org-macs.el.

Signature

(org-preserve-local-variables &rest BODY)

Documentation

Execute BODY while preserving local variables.

Source Code

;; Defined in ~/.emacs.d/elpa/org-9.8.2/org-macs.el
(defmacro org-preserve-local-variables (&rest body)
  "Execute BODY while preserving local variables."
  (declare (debug (body)))
  (org-with-gensyms (local-variables tick-counter-before)
    `(org-with-undo-amalgamate
       (let ((,local-variables
              (org-with-wide-buffer
               (goto-char (point-max))
               (let ((case-fold-search t))
                 (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
              (let ((modified (< ,tick-counter-before (buffer-modified-tick))))
                (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))))))))))