dotfiles/bin/emacsremote
2022-03-18 15:00:33 +01:00

48 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Open file on a remote Emacs server.
# https://andy.wordpress.com/2013/01/03/automatic-emacsclient/ with added sudo.
params=()
sudo=0
local=0
for p in "${@}"; do
if [[ "${p}" == "-n" ]]; then
params+=( "${p}" )
elif [[ "${p}" == "-c" ]]; then
params+=( "${p}" )
elif [[ "${p}" == "-cn" ]]; then
params+=( "${p}" )
elif [[ "${p:0:1}" == "+" ]]; then
params+=( "${p}" )
elif [[ "${p}" == "--sudo" ]]; then
sudo=1
elif [[ "${p}" == "--local" ]]; then
# Use local server, for use with --sudo.
local=1
else
# Setting field separator to newline so that filenames with spaces will
# not be split up into 2 array elements.
OLDIFS=${IFS}
IFS=$'\n'
if [[ $(id -u) -eq 0 || ${sudo} -eq 1 ]]; then
if [[ ${local} -eq 0 ]]; then
params+=( "/ssh:$(hostname -f)|sudo:$(hostname -f):"$(realpath -m "${p}") )
else
params+=( "/sudo:localhost:"$(realpath -m "${p}") )
fi
else
params+=( "/ssh:$(hostname -f):"$(realpath "${p}") )
fi
IFS=${OLDIFS}
fi
done
emacsclient_tcp=""
if [[ -f "${HOME}/.emacs.d/server/server" ]]; then
emacsclient_tcp="-f ~/.emacs.d/server/server"
fi
emacsclient ${emacsclient_tcp} "${params[@]}"