From c69d929538f760937e7d5c2fb7083e065e61c0f9 Mon Sep 17 00:00:00 2001 From: Tyler Ware Date: Wed, 9 Dec 2020 09:16:17 -0700 Subject: [PATCH] Enable the loading a profile via CLI Currently a profile must first be specified in `~/.emacs-profiles.el` in order to be used. In most day-to-day usage this is adequate, but on occasion it is nice to have a 'throw-away' profile. The rationale for such a profile is trying someone else's config out for a day / hour or some such, but it's not something you're committed to. This change enables the following to work: ```bash emacs --with-profile '((user-emacs-directory . "/tmp/throw-away-config"))' ``` --- CHANGELOG.md | 1 + README.org | 6 ++++++ chemacs.el | 54 +++++++++++++++++++++++++++++++++++----------------- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61deacf..4ef510f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Add support for early-init.el - Change installation from `~/.emacs` to `~/.emacs.d` - Allow .emacs-profiles and .emacs-profile to be stored in $XDG\_CONFIG\_HOME/chemacs +- Allow loading a literal profile from the cli (e.g. `emacs --with-profile '((user-emacs-directory . "/path/to/config"))'` works) # 1.0 (2020-10-01 / 4dad0684) diff --git a/README.org b/README.org index f7199e4..5c3a12e 100644 --- a/README.org +++ b/README.org @@ -95,6 +95,12 @@ If no profile is given at the command line then the =default= profile is used. $ emacs --with-profile my-profile #+END_SRC +There is an option for using profile that is not preconfigured in =~/.emacs-profiles.el=. To accomplish that you can directly provide the profile via the command line, like so +#+BEGIN_SRC shell +$ emacs --with-profile '((user-emacs-directory . "/path/to/config"))' +#+END_SRC +This method supports all the profile options given below. + ** .emacs-profiles.el This file contains an association list, with the keys/cars being the profile diff --git a/chemacs.el b/chemacs.el index fcf42ec..e3e2ac0 100644 --- a/chemacs.el +++ b/chemacs.el @@ -31,18 +31,6 @@ (defvar chemacs-profiles-path (or (car (seq-filter 'file-exists-p chemacs-profiles-paths)) (car chemacs-profiles-paths))) (defvar chemacs-default-profile-path (or (car (seq-filter 'file-exists-p chemacs-default-profile-paths)) (car chemacs-default-profile-paths))) -(when (not (file-exists-p chemacs-profiles-path)) - (error "[chemacs] %s does not exist." chemacs-profiles-path)) - -(defvar chemacs-default-profile-name - (if (file-exists-p chemacs-default-profile-path) - (with-temp-buffer - (insert-file-contents chemacs-default-profile-path) - (goto-char (point-min)) - ;; (buffer-string)) - (symbol-name (read (current-buffer)) )) - "default")) - (defun chemacs-handle-command-line (args) (when args ;; Handle either --with-profile profilename or @@ -65,20 +53,52 @@ (t (chemacs-handle-command-line (cdr args))))))) +(defvar chemacs--with-profile-value + (let* ((value (chemacs-handle-command-line command-line-args)) + (read-value (read value))) + (when value + (if (listp read-value) + read-value + value)))) + +(defvar chemacs-literal-profile-provided + (and chemacs--with-profile-value + (listp chemacs--with-profile-value))) + +(unless (or (file-exists-p chemacs-profiles-path) + (and chemacs--with-profile-value + (listp chemacs--with-profile-value))) + (error "[chemacs] %s does not exist." chemacs-profiles-path)) + +(defvar chemacs-default-profile-name + (if (file-exists-p chemacs-default-profile-path) + (with-temp-buffer + (insert-file-contents chemacs-default-profile-path) + (goto-char (point-min)) + ;; (buffer-string)) + (symbol-name (read (current-buffer)) )) + "default")) + + (defvar chemacs-profile-name - (let ((name (chemacs-handle-command-line command-line-args))) - (if name name chemacs-default-profile-name))) + (if (and chemacs--with-profile-value + (stringp chemacs--with-profile-value)) + chemacs--with-profile-value + chemacs-default-profile-name)) (defvar chemacs-profile - (let ((profiles + (if (and chemacs--with-profile-value + (listp chemacs--with-profile-value)) + chemacs--with-profile-value + (let ((profiles (with-temp-buffer (insert-file-contents chemacs-profiles-path) (goto-char (point-min)) (read (current-buffer))))) - (cdr (assoc chemacs-profile-name profiles)))) + (cdr (assoc chemacs-profile-name profiles))))) (unless chemacs-profile - (error "No profile `%s' in %s" profile chemacs-profiles-path)) + (error "No profile `%s' in %s" chemacs-profile-name chemacs-profiles-path)) (defun chemacs-profile-get (key &optional default) (alist-get key chemacs-profile default))