Skip to content

Instantly share code, notes, and snippets.

@syohex
Created December 18, 2011 14:32
Show Gist options
  • Save syohex/1493582 to your computer and use it in GitHub Desktop.
Save syohex/1493582 to your computer and use it in GitHub Desktop.
emacs server focus in elisp
(defun my/emacs-serverstart ()
(let ((pid (emacs-pid))
(wid-file (expand-file-name
(concat user-emacs-directory "server_wid"))))
(let ((wid (if window-system
(my/emacs-window-id pid))))
(with-temp-file wid-file
(insert wid))
wid)))
(defun my/emacs-window-id (emacs-pid)
(with-temp-buffer
(call-process "wmctrl" nil t nil "-p" "-l")
(goto-char (point-min))
(let (ret)
(while (and (null ret) (re-search-forward "^\\(\\w+\\) +-?[0-9]+ +\\([0-9]+\\)" nil t))
(let ((wid (buffer-substring-no-properties (match-beginning 1)
(match-end 1)))
(pid (string-to-number
(buffer-substring-no-properties (match-beginning 2)
(match-end 2)))))
(if (= emacs-pid pid)
(setq ret wid))))
ret)))
(my/emacs-serverstart)
#!/bin/sh
if ! which wmctrl > /dev/null 2>&1
then
echo "Please install wmctrl"
exit 1
fi
# get current window id
CURRENT_WID=`wmctrl -a :ACTIVE: -v 2>&1 | \
perl -wln -e 'm{Using window: (\w+)$} and print $1'` 2> /dev/null
if [ "$CURRENT_WID" = "" ]
then
echo "Faild getting current window id"
exit 1
fi
WID_FILE=${HOME}/.emacs.d/server_wid
if [ -e $WID_FILE ]
then
# server is running on GUI Emacs
WID=`cat $WID_FILE`
# forcus emacs server window
wmctrl -i -a $WID
else
# server is running emacs in terminal
SERVER_TERM_TITLE='emacsserver_run'
wmctrl -F -a $SERVER_TERM_TITLE
fi
args=`getopt -o n -l long: -- "$@"`
RETURN_ORIGNAL_WINDOW=1
while getopts n OPT
do
case $OPT in
"n" )
RETURN_ORIGNAL_WINDOW=0
;;
esac
done
emacsclient $@
# return to origin window
if [ "$RETURN_ORIGNAL_WINDOW" -eq 1 ]
then
wmctrl -i -a $CURRENT_WID
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment