<commit_before>;;; config/literate/autoload.el -*- lexical-binding: t; -*-

(defvar +literate-config-file
  (concat doom-private-dir "config.org")
  "The file path of your literate config file.")

(defvar +literate-config-cache-file
  (concat doom-cache-dir "literate-last-compile")
  "The file path that `+literate-config-file' will be tangled to, then
byte-compiled from.")

;;;###autoload
(defun +literate-tangle-h ()
  "Tangles `+literate-config-file' if it has changed."
  (print! (start "Compiling your literate config..."))
  (print-group!
   (let* ((default-directory doom-private-dir)
          (org    (expand-file-name +literate-config-file))
          (dest   (concat (file-name-sans-extension +literate-config-file) ".el"))
          (backup (make-temp-file "config.org.backup")))
     (and (require 'ox)
          (require 'ob-tangle)
          (unwind-protect
              (letf! ((defun message (msg &rest args)
                        (when msg
                          (print! (info "%s") (apply #'format msg args))))
                      ;; Prevent infinite recursion due to recompile-on-save
                      ;; hooks later.
                      (org-mode-hook nil))
                ;; Do the ol' switcheroo because `org-babel-tangle' writes
                ;; changes to the user's literate config, which would impose on
                ;; the user.
                (copy-file org backup t)
                (with-current-buffer (find-file-noselect org)
                  ;; Tangling won't ordinarily expand #+INCLUDE directives, so
                  ;; we do it ourselves.
                  (org-export-expand-include-keyword)
                  (org-babel-tangle nil dest))
                t)
            (ignore-errors (copy-file backup org t))
            (ignore-errors (delete-file backup)))
          ;; Write an empty file to serve as our mtime cache
          (with-temp-file +literate-config-cache-file)))))

;;;###autoload
(add-hook 'org-mode-hook #'+literate-enable-recompile-h)

;;;###autoload
(defalias '+literate/reload #'doom/reload)

;;;###autoload
(defun +literate-enable-recompile-h ()
  "Enable literate-compiling-on-save in the current buffer."
  (add-hook 'after-save-hook #'+literate-recompile-maybe-h nil 'local))

;;;###autoload
(defun +literate-recompile-maybe-h ()
  "Recompile literate config to `doom-private-dir'.

We assume any org file in `doom-private-dir' is connected to your literate
config, and should trigger a recompile if changed."
  (when (file-in-directory-p buffer-file-name doom-private-dir)
    (+literate-tangle-h)))
<commit_msg>Fix #3685: don't mutate org buffer when tangling<commit_after>;;; config/literate/autoload.el -*- lexical-binding: t; -*-

(defvar +literate-config-file
  (concat doom-private-dir "config.org")
  "The file path of your literate config file.")

(defvar +literate-config-cache-file
  (concat doom-cache-dir "literate-last-compile")
  "The file path that `+literate-config-file' will be tangled to, then
byte-compiled from.")

;;;###autoload
(defun +literate-tangle-h ()
  "Tangles `+literate-config-file' if it has changed."
  (print! (start "Compiling your literate config..."))
  (print-group!
   (let* ((default-directory doom-private-dir)
          (org    (expand-file-name +literate-config-file))
          (dest   (concat (file-name-sans-extension +literate-config-file) ".el")))
     (and (require 'ox)
          (require 'ob-tangle)
          (unwind-protect
              (letf! ((defun message (msg &rest args)
                        (when msg
                          (print! (info "%s") (apply #'format msg args))))
                      ;; Prevent infinite recursion due to recompile-on-save
                      ;; hooks later.
                      (org-mode-hook nil)
                      ;; Operate on a copy because `org-babel-tangle' has
                      ;; side-effects we don't want to impose on the User's
                      ;; config permanently.
                      (backup (make-temp-file (concat (file-name-nondirectory org) "."))))
                (copy-file org backup t)
                (with-current-buffer (find-file-noselect backup)
                  ;; Tangling won't ordinarily expand #+INCLUDE directives
                  (org-export-expand-include-keyword)
                  (org-babel-tangle nil dest)
                  (kill-buffer (current-buffer)))
                t)
            (ignore-errors (delete-file backup)))
          ;; Write an empty file to serve as our mtime cache
          (with-temp-file +literate-config-cache-file)))))

;;;###autoload
(add-hook 'org-mode-hook #'+literate-enable-recompile-h)

;;;###autoload
(defalias '+literate/reload #'doom/reload)

;;;###autoload
(defun +literate-enable-recompile-h ()
  "Enable literate-compiling-on-save in the current buffer."
  (add-hook 'after-save-hook #'+literate-recompile-maybe-h nil 'local))

;;;###autoload
(defun +literate-recompile-maybe-h ()
  "Recompile literate config to `doom-private-dir'.

We assume any org file in `doom-private-dir' is connected to your literate
config, and should trigger a recompile if changed."
  (when (file-in-directory-p buffer-file-name doom-private-dir)
    (+literate-tangle-h)))

{'fraction_non_alphanumeric': 0.13702290076335877, 'fraction_numerical': 0.0007633587786259542, 'mean_word_length': 2.2962264150943397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7641347', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1560, 'n_words': 424}