<commit_before>;;; core/autoload/sandbox.el -*- lexical-binding: t; -*-

(defvar doom-sandbox-buffer-name "*doom:sandbox*"
  "Name of the Doom sandbox buffer.")

(defvar doom-sandbox-dir
  (expand-file-name "doom-sandbox" (temporary-file-directory))
  "TODO")

(defvar doom-sandbox-preamble
  ";; Welcome to the sandbox!
;;
;; This is a test bed for running Emacs Lisp in another instance of Emacs that
;; has varying amounts of Doom loaded:
;;
;; - vanilla Emacs (nothing loaded)         \\[doom--run-vanilla-emacs]
;; - vanilla Doom (only Doom core)          \\[doom--run-vanilla-doom]
;; - Doom + modules - your private config   \\[doom--run-vanilla-doom+]
;; - Doom + modules + your private config   \\[doom--run-full-doom]
;;
;; This is done without sacrificing access to installed packages. Use the sandbox
;; to reproduce bugs and determine if Doom is to blame.\n\n"
  "TODO")

(defun doom--sandbox-launch (args forms)
  (require 'package)
  (require 'restart-emacs)
  (let* ((sandbox-file (expand-file-name "init.el" doom-sandbox-dir))
         (args (append args (list "-l" sandbox-file))))
    (delete-directory doom-sandbox-dir 'recursive)
    (make-directory doom-sandbox-dir 'parents)
    (with-temp-file sandbox-file
      (prin1 forms (current-buffer)))
    (condition-case-unless-debug e
        (cond ((display-graphic-p)
               (if (memq system-type '(windows-nt ms-dos))
                   (restart-emacs--start-gui-on-windows args)
                 (restart-emacs--start-gui-using-sh args)))
              ((memq system-type '(windows-nt ms-dos))
               (user-error "Cannot start another Emacs from Windows shell."))
              ((suspend-emacs
                (format "%s %s -nw; fg"
                        (shell-quote-argument (restart-emacs--get-emacs-binary))
                        (mapconcat #'shell-quote-argument args " ")))))
      (error
       (delete-directory doom-sandbox-dir 'recursive)
       (signal (car e) (cdr e))))))


(defun doom--sandbox-run (&optional mode)
  "TODO"
  (doom--sandbox-launch
   (unless (eq mode 'doom) '("-Q"))
   (let ((forms
          (read (format "(progn\n%s\n)"
                        (buffer-substring-no-properties
                         (point-min)
                         (point-max))))))
     (if (eq mode 'doom)
         forms
       `(progn
          ;; doom variables
          (setq doom-debug-p t
                doom-emacs-dir ,doom-emacs-dir
                doom-cache-dir ,(expand-file-name "cache/" doom-sandbox-dir)
                doom-etc-dir   ,(expand-file-name "etc/" doom-sandbox-dir))
          (defun doom--write-to-etc-dir-a (orig-fn &rest args)
            (let ((user-emacs-directory doom-etc-dir))
              (apply orig-fn args)))
          (advice-add #'locate-user-emacs-file :around #'doom--write-to-etc-dir-a)
          ;; emacs essential variables
          (setq before-init-time (current-time)
                after-init-time nil
                init-file-debug doom-debug-p
                noninteractive nil
                process-environment (get 'process-environment 'initial-value)
                exec-path (get 'exec-path 'initial-value)
                load-path ',load-path
                user-init-file load-file-name)
          ;; package.el
          (setq package--init-file-ensured t
                package-user-dir ,package-user-dir
                package-archives ',package-archives)
          ;; comp.el
          (setq comp-deferred-compilation nil
                native-comp-eln-load-path ',(bound-and-true-p native-comp-eln-load-path)
                native-comp-async-env-modifier-form ',(bound-and-true-p comp-async-env-modifier-form)
                comp-deferred-compilation-black-list ',(bound-and-true-p comp-deferred-compilation-black-list)
                ;; REVIEW Remove me after a couple weeks
                comp-eln-load-path ',(bound-and-true-p native-comp-eln-load-path))
          ;; (add-hook 'kill-emacs-hook
          ;;           (lambda ()<commit_msg>Fix #5038: renamed comp variables in sandbox loader<commit_after>;;; core/autoload/sandbox.el -*- lexical-binding: t; -*-

(defvar doom-sandbox-buffer-name "*doom:sandbox*"
  "Name of the Doom sandbox buffer.")

(defvar doom-sandbox-dir
  (expand-file-name "doom-sandbox" (temporary-file-directory))
  "TODO")

(defvar doom-sandbox-preamble
  ";; Welcome to the sandbox!
;;
;; This is a test bed for running Emacs Lisp in another instance of Emacs that
;; has varying amounts of Doom loaded:
;;
;; - vanilla Emacs (nothing loaded)         \\[doom--run-vanilla-emacs]
;; - vanilla Doom (only Doom core)          \\[doom--run-vanilla-doom]
;; - Doom + modules - your private config   \\[doom--run-vanilla-doom+]
;; - Doom + modules + your private config   \\[doom--run-full-doom]
;;
;; This is done without sacrificing access to installed packages. Use the sandbox
;; to reproduce bugs and determine if Doom is to blame.\n\n"
  "TODO")

(defun doom--sandbox-launch (args forms)
  (require 'package)
  (require 'restart-emacs)
  (let* ((sandbox-file (expand-file-name "init.el" doom-sandbox-dir))
         (args (append args (list "-l" sandbox-file))))
    (delete-directory doom-sandbox-dir 'recursive)
    (make-directory doom-sandbox-dir 'parents)
    (with-temp-file sandbox-file
      (prin1 forms (current-buffer)))
    (condition-case-unless-debug e
        (cond ((display-graphic-p)
               (if (memq system-type '(windows-nt ms-dos))
                   (restart-emacs--start-gui-on-windows args)
                 (restart-emacs--start-gui-using-sh args)))
              ((memq system-type '(windows-nt ms-dos))
               (user-error "Cannot start another Emacs from Windows shell."))
              ((suspend-emacs
                (format "%s %s -nw; fg"
                        (shell-quote-argument (restart-emacs--get-emacs-binary))
                        (mapconcat #'shell-quote-argument args " ")))))
      (error
       (delete-directory doom-sandbox-dir 'recursive)
       (signal (car e) (cdr e))))))


(defun doom--sandbox-run (&optional mode)
  "TODO"
  (doom--sandbox-launch
   (unless (eq mode 'doom) '("-Q"))
   (let ((forms
          (read (format "(progn\n%s\n)"
                        (buffer-substring-no-properties
                         (point-min)
                         (point-max))))))
     (if (eq mode 'doom)
         forms
       `(progn
          ;; doom variables
          (setq doom-debug-p t
                doom-emacs-dir ,doom-emacs-dir
                doom-cache-dir ,(expand-file-name "cache/" doom-sandbox-dir)
                doom-etc-dir   ,(expand-file-name "etc/" doom-sandbox-dir))
          (defun doom--write-to-etc-dir-a (orig-fn &rest args)
            (let ((user-emacs-directory doom-etc-dir))
              (apply orig-fn args)))
          (advice-add #'locate-user-emacs-file :around #'doom--write-to-etc-dir-a)
          ;; emacs essential variables
          (setq before-init-time (current-time)
                after-init-time nil
                init-file-debug doom-debug-p
                noninteractive nil
                process-environment (get 'process-environment 'initial-value)
                exec-path (get 'exec-path 'initial-value)
                load-path ',load-path
                user-init-file load-file-name)
          ;; package.el
          (setq package--init-file-ensured t
                package-user-dir ,package-user-dir
                package-archives ',package-archives)
          ;; comp.el
          (if (boundp 'comp-deferred-compilation)
              ;; REVIEW Remove me after a month
              (setq comp-deferred-compilation nil
                    comp-deferred-compilation-deny-list ',(bound-and-true-p native-comp-async-env-modifier-form)
                    comp-async-env-modifier-form ',(bound-and-true-p native-comp-async-env-modifier-form)
                    comp-eln-load-path ',(bound-and-true-p native-comp-eln-load-path))
            (setq native-comp-deferred-compilation nil
                  native-comp-deferred-compilation-deny-list ',(bound-and-true-p native-comp-async-env-modifier-form)
                  native-comp-async-env-modifier-form ',(bound-and-true-p native-comp-async-env-modifier-form)
                  native-comp-eln-load-path ',(bound-and-true-p native-comp-eln-load-path)))
          ;; (add-hook 'kill-emacs-hook
          ;;           (lambda ()
{'fraction_non_alphanumeric': 0.13980789754535752, 'fraction_numerical': 0.000711490572749911, 'mean_word_length': 1.9458609849807893, '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': '6505167', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2611, 'n_words': 580}