Skip to content

Commit 266135b

Browse files
committed
Fix doom command execution
1 parent 7938e17 commit 266135b

File tree

1 file changed

+124
-116
lines changed

1 file changed

+124
-116
lines changed

mace

+124-116
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ set -euo pipefail
2121

2222
# Help message
2323
_help() {
24-
cat <<EOF
24+
cat <<EOF
2525
mace [subcommand]
2626
2727
-h, --help Show this help message.
@@ -49,169 +49,177 @@ EOF
4949

5050
# Error and message printing
5151
print_err() {
52-
printf "mace: [error]: %s\n" "$1" >&2 # Print errors to stderr
52+
printf "mace: [error]: %s\n" "$1" >&2 # Print errors to stderr
5353
}
5454

5555
print_msg() {
56-
printf "mace: %s\n" "$1"
56+
printf "mace: %s\n" "$1"
5757
}
5858

5959
# Dependency checking
6060
check_depends() {
61-
for cmd in emacs doom; do
62-
if ! command -v "$cmd" &>/dev/null; then
63-
print_err "$cmd does not exist in your path."
64-
exit 1
65-
fi
66-
done
61+
for cmd in emacs doom; do
62+
if ! command -v "$cmd" &>/dev/null; then
63+
print_err "$cmd does not exist in your path."
64+
exit 1
65+
fi
66+
done
6767
}
6868

6969
# Emacs status checking
7070
check_active_emacs() {
71-
pidof -q emacs &>/dev/null
71+
pidof -q emacs &>/dev/null
7272
}
7373

7474
# Core functions
7575
kill_daemon() {
76-
if check_active_emacs; then
77-
emacsclient -e '(save-some-buffers 1 nil)' 2>/dev/null || print_err "Could not save buffers."
78-
emacsclient -e '(kill-emacs)' 2>/dev/null || print_err "Could not kill daemon."
79-
else
80-
print_err "No daemon running."
81-
fi
76+
if check_active_emacs; then
77+
emacsclient -e '(save-some-buffers 1 nil)' 2>/dev/null || print_err "Could not save buffers."
78+
emacsclient -e '(kill-emacs)' 2>/dev/null || print_err "Could not kill daemon."
79+
else
80+
print_err "No daemon running."
81+
fi
8282
}
8383

8484
start_daemon() {
85-
if check_active_emacs; then
86-
print_err "A daemon is already running."
87-
return 1
88-
fi
89-
print_msg "Starting daemon."
90-
emacs --daemon || { print_err "Could not start Emacs?..." && return 1; }
91-
92-
print_msg "Finished launching daemon."
93-
run_eclient "$@"
94-
return 0
85+
if check_active_emacs; then
86+
print_err "A daemon is already running."
87+
return 1
88+
fi
89+
print_msg "Starting daemon."
90+
emacs --daemon || { print_err "Could not start Emacs?..." && return 1; }
91+
92+
print_msg "Finished launching daemon."
93+
run_eclient "$@"
94+
return 0
9595
}
9696

9797
restart_daemon() {
98-
kill_daemon
99-
start_daemon "$@"
100-
notify-send -a "mace" "Emacs daemon restarted!"
98+
kill_daemon
99+
start_daemon "$@"
100+
notify-send -a "mace" "Emacs daemon restarted!"
101101
}
102102

103103
# Helper functions (simplified)
104104
status_daemon() {
105-
if check_active_emacs; then
106-
print_msg "Emacs daemon is ACTIVE."
107-
else
108-
print_msg "Emacs daemon is not currently running."
109-
fi
105+
if check_active_emacs; then
106+
print_msg "Emacs daemon is ACTIVE."
107+
else
108+
print_msg "Emacs daemon is not currently running."
109+
fi
110110
}
111111

112112
run_eclient() {
113-
emacsclient -c -n --alternate-editor='' "$@"
113+
emacsclient -c -n --alternate-editor='' "$@"
114114
}
115115

116116
run_gui() {
117-
emacsclient -nc --alternate-editor='' "$@"
117+
emacsclient -nc --alternate-editor='' "$@"
118118
}
119119

120120
doom_capture() {
121-
org-capture || notify-send "Something went wrong..."
122-
exit 0
121+
org-capture || notify-send "Something went wrong..."
122+
exit 0
123123
}
124124

125125
# Doom command handling
126126
handle_doom() {
127-
local doom_subcommand="$1"
128-
local proc_count=$(($(nproc) - 2))
129-
local proc_cmd="-j $proc_count"
130-
131-
case "$doom_subcommand" in
132-
s | sync) doom sync --aot $proc_cmd ;;
133-
sb | sync-rebuild) doom sync --rebuild --aot $proc_cmd ;;
134-
u | upgrade) doom upgrade --aot $proc_cmd ;;
135-
gc) doom gc ;;
136-
d | doctor) doom doctor ;;
137-
*)
138-
print_err "Cannot recognise the subcommand!"
139-
_help
140-
exit 1
141-
;;
142-
esac
127+
local doom_subcommand="$1"
128+
local proc_count=$(($(nproc) - 2))
129+
local proc_cmd="-j $proc_count"
130+
local gc_cmd="--gc"
131+
132+
case "$doom_subcommand" in
133+
s | sync) eval "doom sync --aot $proc_cmd $gc_cmd" ;;
134+
su) eval "doom sync -u --aot $proc_cmd $gc_cmd" ;;
135+
sb | sync-rebuild) eval "doom sync --rebuild --aot $proc_cmd $gc_cmd" ;;
136+
u | upgrade) eval "doom upgrade --aot $proc_cmd" ;;
137+
gc) doom gc ;;
138+
d | doctor) doom doctor ;;
139+
*)
140+
print_err "Cannot recognise the subcommand!"
141+
_help
142+
exit 1
143+
;;
144+
esac
143145

144146
}
145147

146148
# Option parsing
147149
while getopts "hSARKGTDpCd:" opt; do
148150

149-
case "$opt" in
150-
h)
151-
_help
152-
exit 0
153-
;;
154-
S)
155-
start_daemon
156-
exit 0
157-
;;
158-
A)
159-
if check_active_emacs; then
160-
print_msg "Emacs daemon is ACTIVE."
161-
exit 0
162-
else
163-
print_msg "Emacs daemon is not currently running."
164-
exit 1
165-
fi
166-
;;
167-
R)
168-
restart_daemon "$@"
169-
exit 0
170-
;;
171-
K)
172-
kill_daemon
173-
exit 0
174-
;;
175-
D)
176-
emacs --debug-init
177-
exit 0
178-
;;
179-
p)
180-
pkill -USR2 emacs
181-
exit 0
182-
;;
183-
G)
184-
run_gui "$@" # Open in GUI mode with optional arguments
185-
exit 0
186-
;;
187-
T)
188-
emacsclient -t -nw -a "" "$@" 2>/dev/null # Open in terminal
189-
exit 0
190-
;;
191-
C) # Doom capture
192-
emacsclient -e '(+org-capture/open-frame)' || notify-send "Something went wrong..."
193-
exit 0
194-
;;
195-
d) # Handle Doom subcommands directly
196-
handle_doom "$OPTARG"
197-
exit 0
198-
;;
199-
*)
200-
_help
201-
exit 1
202-
;;
203-
esac
151+
case "$opt" in
152+
h)
153+
_help
154+
exit 0
155+
;;
156+
S)
157+
start_daemon
158+
exit 0
159+
;;
160+
A)
161+
if check_active_emacs; then
162+
print_msg "Emacs daemon is ACTIVE."
163+
exit 0
164+
else
165+
print_msg "Emacs daemon is not currently running."
166+
exit 1
167+
fi
168+
;;
169+
R)
170+
restart_daemon "$@"
171+
exit 0
172+
;;
173+
K)
174+
kill_daemon
175+
exit 0
176+
;;
177+
D)
178+
emacs --debug-init
179+
exit 0
180+
;;
181+
p)
182+
pkill -USR2 emacs
183+
exit 0
184+
;;
185+
G)
186+
run_gui "$@" # Open in GUI mode with optional arguments
187+
exit 0
188+
;;
189+
T)
190+
notify-send "We do not currently handle terminal emacs properly."
191+
192+
# if [ "$#" -eq 0 ]; then
193+
# If no files
194+
# emacsclient -t -a emacs
195+
# else
196+
# exec emacsclient -t -a=emacs "$@" 2>/dev/null
197+
# fi
198+
;;
199+
C) # Doom capture
200+
emacsclient -e '(+org-capture/open-frame)' -a 'emacs' >/dev/null || notify-send "Something went wrong opening capture window..."
201+
exit 0
202+
;;
203+
d) # Handle Doom subcommands directly
204+
handle_doom "$OPTARG"
205+
exit 0
206+
;;
207+
*)
208+
_help
209+
exit 1
210+
;;
211+
esac
204212
done
205213
shift $((OPTIND - 1)) # Remove parsed options
206214

207215
# Automatic start logic
208216
if [ "$#" -eq 0 ] || [ -z "$DISPLAY" ] || [ -n "${TMUX:-0}" ]; then
209-
if ! check_active_emacs; then
210-
echo "No Emacs daemon, launching one now..."
211-
emacs
212-
# start_daemon && run_eclient
213-
exit 0
214-
fi
215-
run_eclient
217+
if ! check_active_emacs; then
218+
echo "No Emacs daemon, launching one now..."
219+
emacs
220+
# start_daemon && run_eclient
216221
exit 0
222+
fi
223+
run_eclient
224+
exit 0
217225
fi

0 commit comments

Comments
 (0)