-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmace
executable file
·216 lines (189 loc) · 4.7 KB
/
mace
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env bash
# // mace
# // ~~~~
# Copyright (C) 2024 Sau P
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -euox pipefail
# Help message
_help() {
cat <<EOF
mace [subcommand]
-h, --help Show this help message.
-S, --start Start Emacs daemon.
-A, --active Check for active Emacs processes.
-R, --restart Restart Emacs daemon.
-K, --kill Kill any Emacs daemon instances.
-G, --gui Open Emacs in GUI mode.
-T, --terminal Open Emacs in the current terminal.
-D, --debug Alias for 'emacs --debug-init'.
-p, --pkill Alias for 'pkill -USR2 emacs' Allows for debugging Emacs while
it is frozen.
-C, --capture Open Doom capture mode in new frame.
-ds 'doom sync --aot'.
-dsu 'doom sync -u --aot'. Use this if you change an installed package recipe.
-dsb 'doom sync -b --aot'. Rebuild all packages.
-du 'doom upgrade --aot'.
-dd 'doom doctor'.
-dgc 'doom gc'.
EOF
}
# parent_proc=$(ps -o comm= $PPID)
# Error and message printing
print_err() {
printf "mace: [error]: %s\n" "$1" >&2 # Print errors to stderr
}
print_msg() {
printf "mace: %s\n" "$1"
}
# Dependency checking
check_depends() {
for cmd in emacs doom; do
if ! command -v "$cmd" &>/dev/null; then
print_err "$cmd does not exist in your path."
exit 1
fi
done
}
# Emacs status checking
check_active_emacs() {
pidof -q emacs &>/dev/null
}
# Core functions
kill_daemon() {
if check_active_emacs; then
emacsclient -e '(save-some-buffers 1 nil)' 2>/dev/null || print_err "Could not save buffers."
emacsclient -e '(kill-emacs)' 2>/dev/null || print_err "Could not kill daemon."
else
print_err "No daemon running."
fi
}
start_daemon() {
if check_active_emacs; then
print_err "A daemon is already running."
return 1
fi
print_msg "Starting daemon."
emacs --daemon || { print_err "Could not start Emacs?..." && return 1; }
print_msg "Finished launching daemon."
run_eclient "$@"
return 0
}
restart_daemon() {
kill_daemon
start_daemon "$@"
notify-send -a "mace" "Emacs daemon restarted!"
}
# Helper functions (simplified)
status_daemon() {
if check_active_emacs; then
print_msg "Emacs daemon is ACTIVE."
else
print_msg "Emacs daemon is not currently running."
fi
}
run_eclient() {
emacsclient -c -n --alternate-editor='' "$@"
}
run_gui() {
emacsclient -nc --alternate-editor='' "$@"
}
doom_capture() {
org-capture || notify-send "Something went wrong..."
exit 0
}
# Doom command handling
handle_doom() {
local doom_subcommand="$1"
local proc_count=$(( $(nproc) - 2 ))
local proc_cmd="-j $proc_count"
case "$doom_subcommand" in
s | sync) doom sync --aot $proc_cmd ;;
sb | sync-rebuild) doom sync --rebuild --aot $proc_cmd ;;
u | upgrade) doom upgrade --aot $proc_cmd ;;
gc) doom gc ;;
d | doctor) doom doctor ;;
*)
print_err "Cannot recognise the subcommand!"
_help
exit 1
;;
esac
}
# Option parsing
while getopts "hSARKGTDpCd:" opt; do
case "$opt" in
h)
_help
exit 0
;;
S)
start_daemon
exit 0
;;
A)
if check_active_emacs; then
print_msg "Emacs daemon is ACTIVE."
exit 0
else
print_msg "Emacs daemon is not currently running."
exit 1
fi
;;
R)
restart_daemon "$@"
exit 0
;;
K)
kill_daemon
exit 0
;;
D)
emacs --debug-init
exit 0
;;
p)
pkill -USR2 emacs
exit 0
;;
G)
run_gui "$@" # Open in GUI mode with optional arguments
exit 0
;;
T)
emacsclient -t -nw -a "" "$@" 2>/dev/null # Open in terminal
exit 0
;;
C) # Doom capture
emacsclient -e '(+org-capture/open-frame)' || notify-send "Something went wrong..."
exit 0
;;
d) # Handle Doom subcommands directly
handle_doom "$OPTARG"
exit 0
;;
*)
_help
exit 1
;;
esac
done
shift $((OPTIND - 1)) # Remove parsed options
# Automatic start logic
if [ "$#" -eq 0 ] || [ -z "$DISPLAY" ] || [ -n "${TMUX:-0}" ]; then
if ! check_active_emacs; then
echo "No Emacs daemon, launching one now..."
start_daemon && run_eclient
exit 0
fi
run_eclient
exit 0
fi