bin/em

89 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s globstar nullglob
function show_help ()
{
cat >&2 <<EOF
usage: $(basename "$0") [OPTION...]
OPTIONS
-v Verbosity. Repeat for more verbosity.
-h This help.
Examples
\$ $(basename "$0")
\$ $(basename "$0") -vv
EOF
exit 1
}
#------------------------------------------------------------------------------
declare -i VERBOSITY=0
while getopts ":hv" opt
do
case $opt in
v ) ((++VERBOSITY)) ;;
* ) show_help ;;
esac
done
shift $((OPTIND - 1))
#------------------------------------------------------------------------------
function squawk ()
{
local -i NVOL=$1; shift
if ((0 == NVOL)); then
echo >&2 "$(basename "$0") $(date +'%T %Z')" "$@"
exit 1
elif ((NVOL <= VERBOSITY)); then
echo "$(basename "$0") $(date +'%T %Z')" "$@"
fi
return 0
}
#------------------------------------------------------------------------------
# Express the VERBOSITY as an option string.
function vopt ()
{
local str=""
if ((0 < VERBOSITY)); then
str="-v"
fi
for ((i = 1; i < VERBOSITY; i++)); do
str="${str}v"
done
echo "$str"
return 0
}
#------------------------------------------------------------------------------
squawk 1 "$(date --iso) started."
if [[ -n "${INSIDE_EMACS:-}" ]]; then
squawk 1 "I'm inside emacs..."
emacsclient "$@"
else
squawk 1 "Not inside emacs..."
# Attempt to clean up the possible messes that emacs makes.
if ! pgrep --uid "$LOGNAME" --full 'emacs --daemon' >/dev/null
then
squawk 1 "No daemon, clearing lock..."
rm -f ~/.emacs.d/.emacs.desktop.lock
elif ! [[ -S /run/user/$UID/emacs/server ]]
then
squawk 1 "Killing daemon that lost its socket..."
pkill --signal SIGKILL --uid "$LOGNAME" --full 'emacs --daemon'
squawk 1 "Clearing lock..."
rm -f ~/.emacs.d/.emacs.desktop.lock
fi
squawk 1 "daemon state should be good, so starting client..."
exec emacsclient --tty --alternate-editor="" "$@"
fi
squawk 1 "done."
exit 0