-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot-tangle
executable file
·85 lines (72 loc) · 2.38 KB
/
dot-tangle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/env bash
# Following script installs and tangles literate dotfiles written in Org mode.
# Directory which contains dotfiles (same even with =sudo=):
if [ -v SUDO_USER ]; then
HOME=$(eval echo "~$SUDO_USER")
fi
DOTFILES_HOME=$HOME/.dotfiles
# Following function Exports Org file to another Org file.
# Exporting is needed because =#+INCLUDE= isn't executed during tangling.
emacs_export_to_org() {
emacs -Q --batch --eval "
(progn
(setq make-backup-files nil)
(dolist (file command-line-args-left)
(with-current-buffer
(find-file-noselect file)
(org-org-export-to-org)
)
)
)" "$1"
}
# Following function decrypts entries encrypted by =org-crypt=.
# It is required to set =<<crypt_key>>= in file secrets.org;
# same key must be used for encryption of Org entries.
emacs_decrypt_entries() {
emacs -Q --batch --eval "
(progn
(require 'org-crypt)
(require 'epa-file)
(epa-file-enable)
(setq org-crypt-key \"9C4E20813A852D1F\")
(setf epa-pinentry-mode 'ask)
(setq make-backup-files nil)
(dolist (file command-line-args-left)
(with-current-buffer
(find-file-noselect file)
(org-decrypt-entries)
(save-buffer)
)
)
)" "$1"
}
# Following function tangles all specified Org files.
emacs_tangle() {
emacs -Q --batch --eval "
(progn
(setq make-backup-files nil)
(dolist (file command-line-args-left)
(with-current-buffer
(find-file-noselect file)
(org-babel-tangle)
)
)
)" "$1"
}
# Export all Org files matching passed arguments except the files in the root directory:
FILES=$(find "$DOTFILES_HOME/$*" -path "$DOTFILES_HOME/[!.]*/*.org")
emacs_export_to_org "$FILES"
# Decrypt exported files.
# It is required to drop =sudo= privileges; otherwise files are not decrypted correctly.
EXPORTED_FILES=${FILES//'.org'/.org.org}
if [ -v SUDO_USER ]; then
chown "$SUDO_USER" "$EXPORTED_FILES"
sudo -u "$SUDO_USER" bash -c "$(declare -f emacs_decrypt_entries); emacs_decrypt_entries $EXPORTED_FILES"
else
emacs_decrypt_entries "$EXPORTED_FILES"
fi
# Tangle exported files.
emacs_tangle "$EXPORTED_FILES"
# Delete exported files.
rm "$EXPORTED_FILES"
exit 0