diff --git a/init/multi-emacs-config.el b/init/multi-emacs-config.el index b0234cc..41dd93b 100644 --- a/init/multi-emacs-config.el +++ b/init/multi-emacs-config.el @@ -3,7 +3,7 @@ ;; Author: Rongsong Shen ;; ;;; Commentary: - +;; ;; This package will enable us to use multiple EMACS startup ;; configuration(startkit in this package) and switch between ;; these configuration just by change a single environment @@ -23,134 +23,153 @@ (require 'cl-lib) (require 'comint) -(defvar emacs-start-kit "The name of emacs startkit we will use") -(defvar emacs-config-name "The directory name of emacs startkit") -(defvar emacs-config-directory "The based directory of emacs start configuration") -(defvar emacs-init-file "The full path of emacs start file") -(defvar startkit-info "Information to install emacs startkit") +(defvar emacs-start-kit + (split-string + (let ((start-kit (getenv "EMACS_STARTKIT"))) + (if start-kit + start-kit + "easy-emacs")) + ":") + "The name of emacs startkit we will use") -(setq emacs-start-kit - (let ((start-kit (getenv "EMACS_STARTKIT"))) - (if start-kit - start-kit - "spacemacs"))) +(defvar startkit-name + (car emacs-start-kit) + "The name of Emacs startkit") -(setq emacs-config-name emacs-start-kit) +(defvar startkit-branch + (let ((branch (cadr emacs-start-kit))) + (if branch + branch + "master")) + "The branch of emacs startkit") + +(defvar emacs-config-name + startkit-name + "The directory name of emacs startkit") + +(defvar emacs-config-directory + (let ((config-dir (getenv "EMACS_CONFIG_DIR"))) + (if config-dir + (format "%s/%s.%s" + config-dir + startkit-name + startkit-branch) + (format "%s/.config/emacs/%s.%s" + (getenv "HOME") + startkit-name + startkit-branch))) + "The based directory of emacs start configuration") + +(defvar startkit-info nil + "Information to install emacs startkit") + +(defvar emacs-base-directory + (format "%s/Emacs/%s.%s" + (getenv "HOME") + startkit-name + startkit-branch) + "The base directory which Emacs startkit will be put") + +(defvar emacs-init-file + (concat emacs-base-directory "/" + "/init.el") + "The full path of emacs start file") -(setq emacs-config-directory - (let ((config-dir (getenv "EMACS_CONFIG_DIR"))) - (if config-dir - (concat config-dir "/" - emacs-config-name "/") - (concat (substitute-in-file-name "~/.config/emacs/") - emacs-config-name "/")))) (unless (file-exists-p emacs-config-directory) (make-directory emacs-config-directory t)) -(setq user-emacs-directory emacs-config-directory) - -(setq emacs-init-file - (expand-file-name "init.el" user-emacs-directory)) - ;; setup customization directory for emacs (custom.el) (unless custom-file - (setq custom-file (expand-file-name "custom.el" user-emacs-directory))) + (setq custom-file (expand-file-name "custom.el" emacs-config-directory))) ;; Setup default directory which package will be installed (setq package-user-dir - (concat user-emacs-directory "/elpa")) + (concat emacs-config-directory "/elpa")) ;; setup directory of spacemacs customization -(when (or (string= emacs-start-kit "spacemacs") - (string= emacs-start-kit "spacemacs.develop")) +(when (string= startkit-name "spacemacs") (let ((spacemacs-dir (concat emacs-config-directory "/.local/etc"))) (progn (setenv "SPACEMACSDIR" spacemacs-dir)))) -(setq startkit-info - '((doom-emacs . - (lambda (dir) - (list - (concat "git clone git@github.com:shen390s/doom-emacs.git " dir) - (concat "cd " dir) - "cp init.example.el init.el" - "make install" - "y"))) - (frontmacs . - (lambda (dir) - (list - (concat "mkdir -p " dir ) - (concat "cd " dir) - "wget https://raw.githubusercontent.com/shen390s/frontmacs/master/scripts/init-frontmacs.el" - "echo \"(load (expand-file-name \\\"init-frontmacs.el\\\" user-emacs-directory))\" >>init.el"))) - (graphene . - (lambda (dir) - (list - (concat "git clone git@github.com:shen390s/dotfiles-emacs.git " dir)))) - (ohai-emacs . - (lambda (dir) - (list - (concat "git clone git@github.com:shen390s/ohai-emacs.git " dir)))) - (prelude . - (lambda (dir) - (list - (concat "git clone git@github.com:shen390s/prelude.git " dir)))) - (spacemacs . - (lambda (dir) - (list - (concat "git clone https://github.com/syl20bnr/spacemacs.git " dir)))) - (spacemacs.develop . - (lambda (dir) - (list - (concat "git clone -b develop https://github.com/syl20bnr/spacemacs.git " dir)))) - (radian-emacs . - (lambda (dir) - (list - (concat "git clone -b radian https://github.com/shen390s/straight-emacs.git " dir)))) - (hell-emacs . - (lambda (dir) - (list - (concat "git clone -b doom-emacs https://github.com/shen390s/straight-emacs.git " dir)))) - (doom-emacs.develop . - (lambda (dir) - (list - (concat "git clone -b develop https://github.com/hlissner/doom-emacs.git " dir) - (concat "cd " dir) - "cp init.example.el init.el" - "make install" - "y"))) - (easy-emacs . - (lambda (dir) - (list - (concat "git clone git@shenrs.eu:rshen/easy-emacs.git " dir)))) - (castlemacs . - (lambda (dir) - (list - (concat "git clone https://github.com/freetonik/castlemacs.git " dir)))))) +(defun gen-doom-install-script (branch install-dir config-dir) + (if (string= branch "master") + (list + (format + "git clone https://github.com/hlissner/doom-emacs.git %s" + install-dir) + (format "cd %s" install-dir) + (format "DOOMDIR=%s ./bin/doom install" + config-dir)) + (list + (format + "git clone -b %s https://github.com/hlissner/doom-emacs.git %s" + branch install-dir) + (format "cd %s" install-dir) + (format "DOOMDIR=%s ./bin/doom install" + config-dir)))) -(defun install-emacs-startkit (name location) +(defun doom-load (install-dir config-dir) + (progn + (setenv "DOOMDIR" config-dir) + (load-file emacs-init-file))) + +(defun gen-easy-install-script (branch install-dir config-dir) + (if (string= branch "master") + (list + (format + "git clone git@shenrs.eu:rshen/easy-emacs.git %s" + install-dir)) + (list + (format + "git clone -b %s git@shenrs.eu:rshen/easy-emacs.git %s" + branch install-dir)))) + +(defun easy-emacs-load (install-dir config-dir) + (progn + (setenv "EASYEMACSDATA" config-dir) + (setenv "EASYEMACSDIR" install-dir) + (load-file emacs-init-file))) + +(setq startkit-info + '((doom-emacs . (gen-doom-install-script . doom-load)) + (easy-emacs . (gen-easy-install-script . easy-emacs-load)))) + +(defun get-startkit-installer-gen-fn (name startkit-info) + (car (cdr (assoc name startkit-info)))) + +(defun get-startkit-loader (name startkit-info) + (cdr (cdr (assoc name startkit-info)))) + +(defun load-emacs () + ;; use emacs config directory as user-emacs-directory + (setq user-emacs-directory emacs-config-directory) + (funcall (get-startkit-loader (intern startkit-name) + startkit-info) + emacs-base-directory + emacs-config-directory)) + +(defun on-installer-finished (process event) + "Callback function when uinstaller process has finished. +We use this function to load startkit initialization file" + (cond + ((string= event "finished\n") (load-emacs)) + (t (message "Event %s has been ignored" event)))) + + +(defun install-emacs-startkit (name branch location config-dir) "Function to install EMACS startkit. NAME is the name of EMACS startkit. LOCATION is the target directory which startkit will be installed." (progn (let* ((shell-buffer (make-comint-in-buffer "installer" nil "/bin/sh")) (shell-process (get-buffer-process shell-buffer))) (progn - (defun on-installer-finished (process event) - "Callback function when uinstaller process has finished. -We use this function to load startkit initialization file" - (cond - ((string= event "finished\n") (load-file emacs-init-file)) - (t (message "Event %s has been ignored" event)))) - (set-process-sentinel shell-process 'on-installer-finished) (display-buffer shell-buffer) - (comint-simple-send shell-process - (concat "PS1=\">\"\n" - "PS2=\">\"\n" - "PROMPT=\">\"\n" - "export PS1 PS2 PROMPT\n")) - (cl-loop for cmd in (funcall (cdr (assoc (intern name) startkit-info)) - location) + (cl-loop for cmd in + (funcall (get-startkit-installer-gen-fn (intern name) + startkit-info) + branch location config-dir) do (comint-simple-send shell-process (concat cmd "\n"))) (comint-simple-send shell-process @@ -158,9 +177,11 @@ We use this function to load startkit initialization file" (if (not (file-exists-p emacs-init-file)) (progn - (install-emacs-startkit emacs-start-kit + (install-emacs-startkit startkit-name + startkit-branch + emacs-base-directory emacs-config-directory)) - (load-file emacs-init-file)) + (load-emacs)) (provide 'multi-emacs-config) ;;; multi-emacs-config.el ends here