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

;;
;;; Helpers

(defun doom-template-insert (template)
  "TODO"
  (let ((file (expand-file-name (format "templates/%s" template) doom-core-dir)))
    (when (file-exists-p file)
      (insert-file-contents file))))

;;;###autoload
(defun doom-info ()
  "Returns diagnostic information about the current Emacs session in markdown,
ready to be pasted in a bug report on github."
  (require 'vc-git)
  (let ((default-directory doom-emacs-dir)
        (doom-modules (doom-modules)))
    (format
     (concat "- OS: %s (%s)\n"
             "- Shell: %s\n"
             "- Emacs: %s (%s)\n"
             "- Doom: %s (%s)\n"
             "- Graphic display: %s (daemon: %s)\n"
             "- System features: %s\n"
             "- Details:\n"
             "  ```elisp\n"
             "  env bootstrapper: %s\n"
             "  elc count: %s\n"
             "  uname -a:  %s\n"
             "  modules:   %s\n"
             "  packages:  %s\n"
             "  exec-path: %s\n"
             "  ```")
     system-type system-configuration
     shell-file-name
     emacs-version (format-time-string "%b %d, %Y" emacs-build-time)
     doom-version
     (or (string-trim (shell-command-to-string "git log -1 --format=\"%D %h %ci\""))
         "n/a")
     (display-graphic-p) (daemonp)
     (bound-and-true-p system-configuration-features)
     (cond ((file-exists-p doom-env-file) 'envvar-file)
           ((featurep 'exec-path-from-shell) 'exec-path-from-shell))
     ;; details
     (length (doom-files-in `(,@doom-modules-dirs
                              ,doom-core-dir
                              ,doom-private-dir)
                            :type 'files :match "\\.elc$" :sort nil))
     (if IS-WINDOWS
         "n/a"
       (with-temp-buffer
         (unless (zerop (call-process "uname" nil t nil "-msrv"))
           (insert (format "%s" system-type)))
         (string-trim (buffer-string))))
     (or (cl-loop with cat = nil
                  for key being the hash-keys of doom-modules
                  if (or (not cat) (not (eq cat (car key))))
                  do (setq cat (car key))
                  and collect cat
                  and collect (cdr key)
                  else collect
                  (let ((flags (doom-module-get cat (cdr key) :flags)))
                    (if flags
                        `(,(cdr key) ,@flags)
                      (cdr key))))
         "n/a")
     (or (ignore-errors
           (require 'use-package)
           (cl-loop for (name . plist) in (doom-find-packages :private t)
                    if (use-package-plist-delete (copy-sequence plist) :modules)
                    collect (format "%s" (cons name it))
                    else
                    collect (symbol-name name)))
         "n/a")
     ;; abbreviate $HOME to hide username
     (mapcar #'abbreviate-file-name exec-path))))


;;
;;; Commands

;;;###autoload
(defun doom/version ()
  "Display the current version of Doom & Emacs, including the current Doom
branch and commit."
  (interactive)
  (require 'vc-git)
  (print! "Doom v%s (Emacs v%s)\nBranch: %s\nCommit: %s"
          doom-version
          emacs-version
          (or (vc-git--symbolic-ref doom-core-dir)
              "n/a")
          (or (vc-git-working-revision doom-core-dir)
              "n/a")))

;;;###autoload
(defun doom/info ()
  "Collects some debug information about your Emacs session, formats it into
markdown and copies it to your clipboard, ready to be pasted into bug reports!"
  (interactive)
  (message "Generating Doom info...")
  (if noninteractive
      (print! (doom-info))
    (kill-new (doom-info))
    (message "Done! Copied to clipboard.")))

;;;###autoload
(defun doom/am-i-secure ()
  "Test to see if your root certificates are securely configured in emacs."
  (declare (interactive-only t))
  (interactive)
  (unless (string-match-p "\\_<GNUTLS\\_>" system-configuration-features)
    (warn "gnutls support isn't built into Emacs, there may be problems"))
  (if-let* ((bad-hosts
             (cl-loop for bad
                      in '("https://wrong.host.badssl.com/"
                           "https://self-signed.badssl.com/")
                      if (condition-case _e
                             (url-retrieve-synchronously bad)
                           (error nil))
                      collect bad)))
      (error "tls seems to be misconfigured (it got %s)."
             bad-hosts)
    (url-retrieve "https://badssl.com"
                  (lambda (status)
                    (if (or (not status) (plist-member status :error))
                        (warn "Something went wrong.\n\n%s" (pp-to-string status))
                      (message "Your trust roots are set up properly.\n\n%s" (pp-to-string status))
                      t)))))


;;
;;; Vanilla sandbox

(defun doom--run-sandbox (&optional mode)
  (interactive)
  (let ((contents (buffer-string))
        (file (make-temp-file "doom-sandbox-")))
    (require 'package)
    (with-temp-file file
      (insert
       (prin1-to-string
        (macroexp-progn
         (append `((setq debug-on-error t
                         package--init-file-ensured t
                         package-user-dir ,package-user-dir
                         package-archives ',package-archives
                         user-emacs-directory ,doom-emacs-dir
                         doom-modules ,doom-modules))
                 (pcase mode
                   (`vanilla-doom+ ; Doom core + modules - private config
                    `((setq doom-private-dir "/tmp/does/not/exist")
                      (load-file ,user-init-file)
                      (doom|run-all-startup-hooks)))
                   (`vanilla-doom  ; only Doom core
                    `((setq doom-private-dir "/tmp/does/not/exist"
                            doom-init-modules-p t)
                      (load-file ,user-init-file)
                      (doom|run-all-startup-hooks)))
                   (`vanilla       ; nothing loaded
                    `((package-initialize)))))))
       "\n(unwind-protect (progn\n" contents "\n)\n"
       (format "(delete-file %S))" file)))
    (let ((args (if (eq mode 'doom)
                    (list "-l" file)
                  (list "-Q" "-l" file))))
      (require 'restart-emacs)
      (condition-case 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<commit_msg>Fix vanilla-doom+ launcher in sandbox (C-c C-p)<commit_after>;;; core/autoload/debug.el -*- lexical-binding: t; -*-

;;
;;; Helpers

(defun doom-template-insert (template)
  "TODO"
  (let ((file (expand-file-name (format "templates/%s" template) doom-core-dir)))
    (when (file-exists-p file)
      (insert-file-contents file))))

;;;###autoload
(defun doom-info ()
  "Returns diagnostic information about the current Emacs session in markdown,
ready to be pasted in a bug report on github."
  (require 'vc-git)
  (let ((default-directory doom-emacs-dir)
        (doom-modules (doom-modules)))
    (format
     (concat "- OS: %s (%s)\n"
             "- Shell: %s\n"
             "- Emacs: %s (%s)\n"
             "- Doom: %s (%s)\n"
             "- Graphic display: %s (daemon: %s)\n"
             "- System features: %s\n"
             "- Details:\n"
             "  ```elisp\n"
             "  env bootstrapper: %s\n"
             "  elc count: %s\n"
             "  uname -a:  %s\n"
             "  modules:   %s\n"
             "  packages:  %s\n"
             "  exec-path: %s\n"
             "  ```")
     system-type system-configuration
     shell-file-name
     emacs-version (format-time-string "%b %d, %Y" emacs-build-time)
     doom-version
     (or (string-trim (shell-command-to-string "git log -1 --format=\"%D %h %ci\""))
         "n/a")
     (display-graphic-p) (daemonp)
     (bound-and-true-p system-configuration-features)
     (cond ((file-exists-p doom-env-file) 'envvar-file)
           ((featurep 'exec-path-from-shell) 'exec-path-from-shell))
     ;; details
     (length (doom-files-in `(,@doom-modules-dirs
                              ,doom-core-dir
                              ,doom-private-dir)
                            :type 'files :match "\\.elc$" :sort nil))
     (if IS-WINDOWS
         "n/a"
       (with-temp-buffer
         (unless (zerop (call-process "uname" nil t nil "-msrv"))
           (insert (format "%s" system-type)))
         (string-trim (buffer-string))))
     (or (cl-loop with cat = nil
                  for key being the hash-keys of doom-modules
                  if (or (not cat) (not (eq cat (car key))))
                  do (setq cat (car key))
                  and collect cat
                  and collect (cdr key)
                  else collect
                  (let ((flags (doom-module-get cat (cdr key) :flags)))
                    (if flags
                        `(,(cdr key) ,@flags)
                      (cdr key))))
         "n/a")
     (or (ignore-errors
           (require 'use-package)
           (cl-loop for (name . plist) in (doom-find-packages :private t)
                    if (use-package-plist-delete (copy-sequence plist) :modules)
                    collect (format "%s" (cons name it))
                    else
                    collect (symbol-name name)))
         "n/a")
     ;; abbreviate $HOME to hide username
     (mapcar #'abbreviate-file-name exec-path))))


;;
;;; Commands

;;;###autoload
(defun doom/version ()
  "Display the current version of Doom & Emacs, including the current Doom
branch and commit."
  (interactive)
  (require 'vc-git)
  (print! "Doom v%s (Emacs v%s)\nBranch: %s\nCommit: %s"
          doom-version
          emacs-version
          (or (vc-git--symbolic-ref doom-core-dir)
              "n/a")
          (or (vc-git-working-revision doom-core-dir)
              "n/a")))

;;;###autoload
(defun doom/info ()
  "Collects some debug information about your Emacs session, formats it into
markdown and copies it to your clipboard, ready to be pasted into bug reports!"
  (interactive)
  (message "Generating Doom info...")
  (if noninteractive
      (print! (doom-info))
    (kill-new (doom-info))
    (message "Done! Copied to clipboard.")))

;;;###autoload
(defun doom/am-i-secure ()
  "Test to see if your root certificates are securely configured in emacs."
  (declare (interactive-only t))
  (interactive)
  (unless (string-match-p "\\_<GNUTLS\\_>" system-configuration-features)
    (warn "gnutls support isn't built into Emacs, there may be problems"))
  (if-let* ((bad-hosts
             (cl-loop for bad
                      in '("https://wrong.host.badssl.com/"
                           "https://self-signed.badssl.com/")
                      if (condition-case _e
                             (url-retrieve-synchronously bad)
                           (error nil))
                      collect bad)))
      (error "tls seems to be misconfigured (it got %s)."
             bad-hosts)
    (url-retrieve "https://badssl.com"
                  (lambda (status)
                    (if (or (not status) (plist-member status :error))
                        (warn "Something went wrong.\n\n%s" (pp-to-string status))
                      (message "Your trust roots are set up properly.\n\n%s" (pp-to-string status))
                      t)))))


;;
;;; Vanilla sandbox

(defun doom--run-sandbox (&optional mode)
  (interactive)
  (let ((contents (buffer-string))
        (file (make-temp-file "doom-sandbox-")))
    (require 'package)
    (with-temp-file file
      (insert
       (prin1-to-string
        (macroexp-progn
         (append `((setq noninteractive nil
                         debug-on-error t
                         package--init-file-ensured t
                         package-user-dir ,package-user-dir
                         package-archives ',package-archives
                         user-emacs-directory ,doom-emacs-dir
                         doom-modules-cache nil
                         doom-modules ',doom-modules))
                 (pcase mode
                   (`vanilla-doom+ ; Doom core + modules - private config
                    `((setq doom-private-dir "/tmp/does/not/exist")
                      (load-file ,user-init-file)
                      (after! undo-tree
                        ;; undo-tree throws errors because `buffer-undo-tree'
                        ;; isn't corrrectly initialized
                        (setq-default buffer-undo-tree (make-undo-tree)))
                      (maphash (lambda (key plist)
                                 (let ((doom--current-module key)
                                       (doom--current-flags (plist-get plist :flags)))
                                   (load! "init" (plist-get plist :path) t)))
                               doom-modules)
                      (maphash (lambda (key plist)
                                 (let ((doom--current-module key)
                                       (doom--current-flags (plist-get plist :flags)))
                                   (load! "config" (plist-get plist :path) t)))
                               doom-modules)
                      (run-hook-wrapped 'doom-init-modules-hook #'doom-try-run-hook)
                      (doom|run-all-startup-hooks)))
                   (`vanilla-doom  ; only Doom core
                    `((setq doom-private-dir "/tmp/does/not/exist"
                            doom-init-modules-p t)
                      (load-file ,user-init-file)
                      (doom|run-all-startup-hooks)))
                   (`vanilla       ; nothing loaded
                    `((package-initialize)))))))
       "\n(unwind-protect (progn\n" contents "\n)\n"
       (format "(delete-file %S))" file)))
    (let ((args (if (eq mode 'doom)
                    (list "-l" file)
                  (list "-Q" "-l" file))))
      (require 'restart-emacs)
      (condition-case 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
{'fraction_non_alphanumeric': 0.1387941617787478, 'fraction_numerical': 0.00027281407720638383, 'mean_word_length': 1.4814689456760872, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1421550', 'n_tokens_mistral': 4531, 'n_tokens_neox': 4335, 'n_words': 1103}