From 1442e694fbb36173b9c4408a4ad0dd7d59bd5f49 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 29 Apr 2020 20:19:34 -0400 Subject: [PATCH] Move core helpers to core-lib Since they can be generally useful. --- core/core-lib.el | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ core/core.el | 47 +------------------------------------------ 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/core/core-lib.el b/core/core-lib.el index 1bf435167..b981da875 100644 --- a/core/core-lib.el +++ b/core/core-lib.el @@ -85,6 +85,58 @@ Accepts the same arguments as `message'." format-string) ,@args)))) +(defun doom-try-run-hook (hook) + "Run HOOK (a hook function) with better error handling. +Meant to be used with `run-hook-wrapped'." + (doom-log "Running doom hook: %s" hook) + (condition-case e + (funcall hook) + ((debug error) + (signal 'doom-hook-error (list hook e)))) + ;; return nil so `run-hook-wrapped' won't short circuit + nil) + +(defun doom-load-autoloads-file (file &optional noerror) + "Tries to load FILE (an autoloads file). +Return t on success, nil otherwise (but logs a warning)." + (condition-case e + ;; Avoid `file-name-sans-extension' for premature optimization reasons. + ;; `string-remove-suffix' is much cheaper (because it does no file sanity + ;; checks during or after; just plain ol' string manipulation). + (load (string-remove-suffix ".el" file) noerror 'nomessage) + ((debug error) + (message "Autoload file error: %s -> %s" (file-name-nondirectory file) e) + nil))) + +(defun doom-load-envvars-file (file &optional noerror) + "Read and set envvars from FILE. +If NOERROR is non-nil, don't throw an error if the file doesn't exist or is +unreadable. Returns the names of envvars that were changed." + (if (null (file-exists-p file)) + (unless noerror + (signal 'file-error (list "No envvar file exists" file))) + (when-let + (env + (with-temp-buffer + (save-excursion + (insert "\0\n") ; to prevent off-by-one + (insert-file-contents file)) + (save-match-data + (when (re-search-forward "\0\n *\\([^#= \n]*\\)=" nil t) + (setq + env (split-string (buffer-substring (match-beginning 1) (point-max)) + "\0\n" + 'omit-nulls)))))) + (setq process-environment (append (nreverse env) process-environment) + exec-path (append (split-string (getenv "PATH") path-separator t) + (list exec-directory)) + shell-file-name (or (getenv "SHELL") shell-file-name)) + env))) + + +;; +;;; Functional library + (defalias 'doom-partial #'apply-partially) (defun doom-rpartial (fn &rest args) diff --git a/core/core.el b/core/core.el index 437779996..814a1682a 100644 --- a/core/core.el +++ b/core/core.el @@ -427,17 +427,6 @@ If this is a daemon session, load them all immediately instead." ;; ;;; Bootstrap helpers -(defun doom-try-run-hook (hook) - "Run HOOK (a hook function) with better error handling. -Meant to be used with `run-hook-wrapped'." - (doom-log "Running doom hook: %s" hook) - (condition-case e - (funcall hook) - ((debug error) - (signal 'doom-hook-error (list hook e)))) - ;; return nil so `run-hook-wrapped' won't short circuit - nil) - (defun doom-display-benchmark-h (&optional return-p) "Display a benchmark including number of packages and modules loaded. @@ -450,40 +439,6 @@ If RETURN-P, return the message as a string instead of displaying it." (setq doom-init-time (float-time (time-subtract (current-time) before-init-time)))))) -(defun doom-load-autoloads-file (file &optional noerror) - "Tries to load FILE (an autoloads file). -Return t on success, nil otherwise (but logs a warning)." - (condition-case e - (load (substring file 0 -3) noerror 'nomessage) - ((debug error) - (message "Autoload file error: %s -> %s" (file-name-nondirectory file) e) - nil))) - -(defun doom-load-envvars-file (file &optional noerror) - "Read and set envvars from FILE. -If NOERROR is non-nil, don't throw an error if the file doesn't exist or is -unreadable. Returns the names of envvars that were changed." - (if (not (file-readable-p file)) - (unless noerror - (signal 'file-error (list "No envvar file exists" file))) - (when-let - (env - (with-temp-buffer - (save-excursion - (insert "\0\n") ; to prevent off-by-one - (insert-file-contents file)) - (save-match-data - (when (re-search-forward "\0\n *\\([^#= \n]*\\)=" nil t) - (setq - env (split-string (buffer-substring (match-beginning 1) (point-max)) - "\0\n" - 'omit-nulls)))))) - (setq process-environment (append (nreverse env) process-environment) - exec-path (append (split-string (getenv "PATH") path-separator t) - (list exec-directory)) - shell-file-name (or (getenv "SHELL") shell-file-name)) - env))) - (defun doom-initialize (&optional force-p noerror) "Bootstrap Doom, if it hasn't already (or if FORCE-P is non-nil). @@ -502,7 +457,7 @@ The overall load order of Doom is as follows: Module config.el files ~/.doom.d/config.el `doom-init-modules-hook' - `doom-after-init-hook' (`after-init-hook') + `doom-after-init-modules-hook' (`after-init-hook') `emacs-startup-hook' `doom-init-ui-hook' `window-setup-hook'