Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Created January 13, 2023 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pesterhazy/b954a7e7ee6d6a4c2bd08eabd41bd863 to your computer and use it in GitHub Desktop.
Save pesterhazy/b954a7e7ee6d6a4c2bd08eabd41bd863 to your computer and use it in GitHub Desktop.
Open file in emacsclient with optional line number. Start emacs if it's not running yet
#!/bin/bash
result=0
if [[ $# == 1 ]]; then
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n "$1" || result=$?
elif [[ $# == 2 ]]; then
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n +"$2" "$1" || result=$?
else
echo Unexpected args
exit 1
fi
# Didn't work? Let's start emacs first
if [[ $result != 0 ]]; then
echo Starting new Emacs...
open -a Emacs
for i in `seq 50`; do
result=0
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -e "(+ 1 2)" >/dev/null 2>&1 || result=$?
if [[ $result == 0 ]]; then
break
fi
sleep 0.1
done
fi
# Rerun initial command
result=0
if [[ $# == 1 ]]; then
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n "$1" || result=$?
elif [[ $# == 2 ]]; then
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n +"$2" "$1" || result=$?
else
echo Unexpected args
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment