Skip to content

Instantly share code, notes, and snippets.

@plexus
Created May 18, 2018 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plexus/b7b3fbef780351414dba69f05c6cc781 to your computer and use it in GitHub Desktop.
Save plexus/b7b3fbef780351414dba69f05c6cc781 to your computer and use it in GitHub Desktop.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (package-initialize)
(setq chemacs-profiles-path "~/.emacs-profiles.el")
(when (not (file-exists-p chemacs-profiles-path))
(with-temp-file chemacs-profiles-path
(insert "((\"default\" . ((user-emacs-directory . \"~/.emacs.d\"))))")))
(setq chemacs-emacs-profiles
(with-temp-buffer
(insert-file-contents chemacs-profiles-path)
(goto-char (point-min))
(read (current-buffer))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun chemacs-get-emacs-profile (profile)
(alist-get profile chemacs-emacs-profiles nil nil #'equal))
(defun chemacs-emacs-profile-key (key &optional default)
(alist-get key (chemacs-get-emacs-profile chemacs-current-emacs-profile)
default))
(defun chemacs-load-profile (profile)
(setq chemacs-current-emacs-profile profile)
(let* ((emacs-directory (file-name-as-directory
(chemacs-emacs-profile-key 'user-emacs-directory)))
(init-file (expand-file-name "init.el" emacs-directory))
(custom-file- (chemacs-emacs-profile-key 'custom-file init-file))
(server-name- (chemacs-emacs-profile-key 'server-name)))
(setq user-emacs-directory emacs-directory)
;; Allow multiple profiles to each run their server
;; use `emacsclient -s profile_name' to connect
(when server-name-
(setq server-name server-name-))
;; Set environment variables, these are visible to init-file with getenv
(mapcar (lambda (env)
(setenv (car env) (cdr env)))
(chemacs-emacs-profile-key 'env))
;; Start the actual initialization
(load init-file)
;; Prevent customize from changing ~/.emacs (this file)
(when (not custom-file)
(setq custom-file custom-file-)
(load custom-file))))
(defun chemacs-check-command-line-args (args)
(when args
(if (equal (car args) "--with-profile")
(chemacs-load-profile (cadr args))
(chemacs-check-command-line-args (cdr args)))))
;; Check for a --with-profile flag and honor it
(chemacs-check-command-line-args command-line-args)
;; TODO a sensible fallback?
;; If we wait for command-switch-alist then after-init-hook has already run,
;; this is just a no-op so Emacs knows --with-profile is a valid option
(add-to-list 'command-switch-alist '("--with-profile" .
(lambda (_) (pop command-line-args-left))))
(provide '.emacs)
(("plexus" . ((user-emacs-directory . "~/emacs-profiles/plexus")))
("lambdaisland" . ((user-emacs-directory . "~/emacs-profiles/spacemacs-master")
(custom-file . "~/emacs-profiles/lambdaisland/custom.el")
(env . (("SPACEMACSDIR" . "~/emacs-profiles/lambdaisland")))))
("spacemacs" . ((user-emacs-directory . "~/emacs-profiles/spacemacs-master")
(env . (("SPACEMACSDIR" . "~/emacs-profiles/spacemacs-dir")))))
("spacemacs-develop" . ((user-emacs-directory . "~/emacs-profiles/spacemacs-develop")
(env . (("SPACEMACSDIR" . "~/emacs-profiles/spacemacs-dir")))))
("nextjournal" . ((user-emacs-directory . "~/emacs-profiles/spacemacs-master")
(env . (("SPACEMACSDIR" . "~/Nextjournal/nextjournal/spacemacs.d")))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment