Skip to content

Instantly share code, notes, and snippets.

@doitian
Created August 28, 2009 05:42
Show Gist options
  • Save doitian/176795 to your computer and use it in GitHub Desktop.
Save doitian/176795 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Author: Ian Yang
_is_emacs_daemon_started () {
netstat -nlpx 2> /dev/null | awk '{print $NF}' | grep -qF "/tmp/emacs$UID/server"
}
_is_emacs_window_exist () {
wmctrl -lx 2> /dev/null | awk '{print $3}' | grep -qF 'emacs.Emacs'
}
main () {
if ! _is_emacs_daemon_started; then
echo -n 'starting emacs daemon'
if /usr/bin/emacs --daemon &> /tmp/emacs$UID.log; then
echo ' [sucess]'
else
echo ' [faild]'
return 1
fi
fi
if [ -n "$DISPLAY" ]; then
local desk=$(wmctrl -d 2> /dev/null |\
sed -n 's/^\([0-9]*\)[[:space:]]*\*.*/\1/p')
if _is_emacs_window_exist; then
wmctrl -x -a emacs.Emacs &> /dev/null
if [ -n "$1" ]; then
/usr/bin/emacsclient "$@"
[ -n "$desk" ] && wmctrl -s "$desk"
fi
else
if [ -n "$1" ]; then
/usr/bin/emacsclient -c "$@" &
else
/usr/bin/emacsclient -n -c &
fi
local -i tries=3
while ! wmctrl -x -a emacs.Emacs &> /dev/null &&\
[ $tries -gt 0 ]; do
sleep 1
tries=tries-1
done
wait $!
[ -n "$1" -a -n "$desk" ] && wmctrl -s "$desk"
fi
else
/usr/bin/emacsclient "$@"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment