Merge pull request #1 from plexus/early-init

Early-init support, aka chemacs 2.0
This commit is contained in:
Arne Brasseur 2020-10-05 09:09:04 +02:00 committed by GitHub
commit f10c90d5c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 184 deletions

View file

@ -62,48 +62,27 @@ various edge cases and use cases that come up.
** Installation
Clone the Git repository, and run =install.sh=
Clone the Chemacs 2 repository as =$HOME/.emacs.d=. Note that if you already
have an Emacs setup in =~/.emacs.d= you need to move it out of the way first. If
you have an =~/.emacs= startup script then move that out of the way as well.
#+BEGIN_SRC shell
$ git clone https://github.com/plexus/chemacs.git
$ cd chemacs
$ ./install.sh
OK Creating symlink ~/.emacs -> /home/arne/chemacs/.emacs
[ -f ~/.emacs ] && mv ~/.emacs ~/.emacs.bak
[ -d ~/.emacs.d ] && mv ~/.emacs.d ~/.emacs.default
git clone https://github.com/plexus/chemacs2.git ~/.emacs.d
#+END_SRC
The install script will symlink =~/.emacs= to the Chemacs script. If you
already have a =~/.emacs= you need to move it out of the way first
Note that this is different from Chemacs 1. Before Chemacs installed itself as
=~/.emacs= and you could have your own default setup in =~/.emacs.d=. This
approach no longer works because of =~/.emacs.d/early-init.el=, so Chemacs 2
needs to be installed as =~/.emacs.d=.
#+BEGIN_SRC shell
$ ./install.sh
WARN chemacs can't be installed, ~/.emacs is in the way
#+END_SRC
Next you will need to create a =~/.emacs-profiles.el= file, for details see
below.
By symlinking you can easily update Chemacs with a =git pull=. Chemacs is not
available on ELPA/MELPA because its special position in the Emacs boot process
would cause a chicken and egg problem.
There's a similar script for Windows called =install.ps1=.
#+BEGIN_SRC powershell
$ git clone https://github.com/plexus/chemacs.git
$ cd chemacs
$ .\install.ps1
OK copying file to ~/.emacs
#+END_SRC
The Windows script copies the ~/.emacs file rather than symlinking it, as
symlinks in Windows require elevated privileges. To update the file, run
=install.ps1= a second time, if necessary - it uses hashing to detect
changes and prompts for conformation before overwriting
#+BEGIN_SRC powershell
$ .\install.ps1
WARN content is different between C:\Users\Me\.emacs and C:\Users\Me\repos\chemacs\.emacs
WARN chemacs may already be installed OR something else may be in the way
Do you want to overwrite C:\Users\Me\.emacs?: y
OK updated chemacs files successfully.
#+END_SRC
#+begin_src emacs-lisp
(("default" . ((user-emacs-directory . "~/.emacs.default"))))
#+end_src
** Usage
@ -127,7 +106,7 @@ names, and the values/cdrs their configuration.
The main thing to configure is the =user-emacs-directory=
#+BEGIN_SRC emacs-lisp
(("default" . ((user-emacs-directory . "~/.emacs.d")))
(("default" . ((user-emacs-directory . "~/.emacs.default")))
("spacemacs" . ((user-emacs-directory . "~/spacemacs"))))
#+END_SRC

View file

@ -1,4 +1,4 @@
;;; chemacs-common.el --- -*- lexical-binding: t; -*-
;;; chemacs.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;;
@ -16,27 +16,9 @@
;;
;; ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;;
;; Chemacs - Emacs Profile Switcher v0.1
;; Chemacs - Emacs Profile Switcher
;;
;; INSTALLATION
;;
;; Install this file as ~/.emacs . Next time you start Emacs it will create a
;; ~/.emacs-profiles.el , with a single "default" profile
;;
;; (("default" . ((user-emacs-directory . "~/.emacs.d"))))
;;
;; Now you can start Emacs with `--with-profile' to pick a specific profile. A
;; more elaborate example:
;;
;; (("default" . ((user-emacs-directory . "~/emacs-profiles/plexus")))
;; ("spacemacs" . ((user-emacs-directory . "~/github/spacemacs")
;; (server-name . "spacemacs")
;; (custom-file . "~/.spacemacs.d/custom.el")
;; (env . (("SPACEMACSDIR" . "~/.spacemacs.d"))))))
;;
;; If you want to change the default profile used (so that, for example, a
;; GUI version of Emacs uses the profile you want), you can also put the name
;; of that profile in a ~/.emacs-profile file
;; See README.md for instructions.
;; ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;; this must be here to keep the package system happy, normally you do
@ -49,8 +31,7 @@
(defvar chemacs-default-profile-path "~/.emacs-profile")
(when (not (file-exists-p chemacs-profiles-path))
(with-temp-file chemacs-profiles-path
(insert "((\"default\" . ((user-emacs-directory . \"~/.emacs.d\"))))")))
(error "[chemacs] %s does not exist." chemacs-profiles-path))
(defvar chemacs-emacs-profiles
(with-temp-buffer
@ -91,7 +72,7 @@
(alist-get key (chemacs-get-emacs-profile chemacs-current-emacs-profile)
default))
(defun chemacs-load-profile (profile early-init-p)
(defun chemacs-load-profile (profile)
(when (not (chemacs-get-emacs-profile profile))
(error "No profile `%s' in %s" profile chemacs-profiles-path))
(setq chemacs-current-emacs-profile profile)
@ -100,8 +81,7 @@
(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))
(early-init-file (expand-file-name "early-init.el" emacs-directory))
)
(early-init-file (expand-file-name "early-init.el" emacs-directory)))
(setq user-emacs-directory emacs-directory)
;; Allow multiple profiles to each run their server
@ -114,9 +94,10 @@
(setenv (car env) (cdr env)))
(chemacs-emacs-profile-key 'env))
(if early-init-p
(if (and (boundp 'chemacs-early-init) chemacs-early-init)
(when (file-exists-p early-init-file)
(load early-init-file))
(load early-init-file)
(setq chemacs-early-init nil))
(when (chemacs-emacs-profile-key 'straight-p)
(chemacs-load-straight))
@ -130,7 +111,7 @@
(unless (equal custom-file init-file)
(load custom-file))))))
(defun chemacs-check-command-line-args (args early-init-p)
(defun chemacs-check-command-line-args (args)
(if args
;; Handle either `--with-profile profilename' or
;; `--with-profile=profilename'
@ -144,14 +125,16 @@
'("--with-profile" .
(lambda (_) (pop command-line-args-left))))
;; Load the profile
(chemacs-load-profile (cadr args) early-init-p))
(chemacs-load-profile (cadr args)))
;; Similar handling for `--with-profile=profilename'
((equal (car s) "--with-profile")
(add-to-list 'command-switch-alist `(,(car args) . (lambda (_))))
(chemacs-load-profile (mapconcat 'identity (cdr s) "=") early-init-p))
(chemacs-load-profile (mapconcat 'identity (cdr s) "=")))
(t (chemacs-check-command-line-args (cdr args) early-init-p))))
(t (chemacs-check-command-line-args (cdr args)))))
;; If no profile given, load the "default" profile
(chemacs-load-profile (chemacs-detect-default-profile) early-init-p)))
(chemacs-load-profile (chemacs-detect-default-profile))))
(chemacs-check-command-line-args command-line-args)

View file

@ -2,8 +2,9 @@
(let ((chemacs-directory
(file-name-directory (file-truename load-file-name))))
(load (expand-file-name "chemacs-common.el" chemacs-directory)))
(load (expand-file-name "chemacs.el" chemacs-directory)))
;; Check for a --with-profile flag and honor it; otherwise load the
;; default profile.
(chemacs-check-command-line-args command-line-args 'early-init)
(defvar chemacs-early-init t)
(chemacs-check-command-line-args command-line-args)

View file

@ -2,8 +2,8 @@
(let ((chemacs-directory
(file-name-directory (file-truename load-file-name))))
(load (expand-file-name "chemacs-common.el" chemacs-directory)))
(load (expand-file-name "chemacs.el" chemacs-directory)))
;; Check for a --with-profile flag and honor it; otherwise load the
;; default profile.
(chemacs-check-command-line-args command-line-args nil)
(chemacs-check-command-line-args command-line-args)

View file

@ -1,57 +0,0 @@
param(
[switch] $Confirm = $True
)
$ErrorActionPreference = "Stop"
$ChemacsPath = Join-Path (Split-Path $PSCommandPath) ".emacs"
Try {
Set-Variable -Scope global EmacsPath $(
Join-Path $(Get-Variable -Scope global HOME).Value ".emacs"
)
} Catch {
# If $HOME hasn't been defined by the user, then emacs will default to this
# directory on modern versions of Windows. Older versions of Windows probably
# don't even have PowerShell installed, so I make no effort to support them.
Set-Variable -Scope global EmacsPath $(
Resolve-Path "~/AppData/roaming/.emacs"
)
}
If (-Not (Test-Path -Path $EmacsPath)) {
# In Windows, symlinks require admin privileges, so we copy the
# file wholesale instead.
Write-Host "OK copying file to ~/.emacs..."
Copy-Item $ChemacsPath $EmacsPath
} Else {
# Because we copied the file instead of symlinking it, we have to try to
# detect changes with hashing instead...
If ((Get-FileHash $ChemacsPath).Hash -Eq (Get-FileHash $EmacsPath).Hash) {
Write-Host "OK chemacs appears to already be copied over, you're all good."
} Else {
Write-Host ("WARN content is different between {0} and {1}" -f @($EmacsPath, $ChemacsPath))
$should_write = $False
If ($Confirm) {
Write-Host "WARN chemacs may already be installed OR something else may be in the way"
$ans = Read-Host ("Do you want to overwrite {0}?" -f $EmacsPath)
If (@("y", "yes", "Y", "YES").Contains($ans)) {
$should_write = $True
}
} Else {
$should_write = $True
}
If ($should_write) {
Copy-Item $ChemacsPath $EmacsPath
Write-Host "OK updated chemacs files successfully."
} Else {
Write-Host "WARN took no action."
}
}
}

View file

@ -1,53 +0,0 @@
#!/bin/sh
chemacs_realpath() {
origpwd="$PWD"
cd "$(dirname "$1")"
link=$(readlink "$(basename "$1")")
while [ "$link" ]; do
cd "$(dirname "$link")"
link=$(readlink "$(basename "$1")")
done
realpath="$PWD/$(basename "$1")"
cd "$origpwd"
echo "$realpath"
}
chemacs_path=$(chemacs_realpath "$(dirname "$0")")
main() {
if [ -L "$HOME/.emacs" ]; then
check_existing_symlink
else
create_symlink_if_possible
fi
}
check_existing_symlink() {
target=$(readlink -f "$HOME/.emacs")
if [ "$target" != "$chemacs_path/.emacs" ]; then
warn "~/.emacs symlink points elsewhere -> $target"
else
ok "chemacs already linked, you're all good."
fi
}
create_symlink_if_possible() {
if [ -e "$HOME/.emacs" ]; then
warn "chemacs can't be installed, ~/.emacs is in the way"
else
ok "Creating symlink ~/.emacs -> $chemacs_path/.emacs"
ln -s "$chemacs_path/.emacs" "$HOME"
fi
}
warn() {
printf "WARN\t%s\n" "$1"
}
ok() {
printf "OK\t%s\n" "$1"
}
main