Wednesday, March 27, 2013

Emacs in Daemon mode

When I started to use emacs as Java IDE, I have installed CEDET and JDEE. This make start time of emacs longer, even when I am not opening any Java file. Therefore, it is required to start Emacs in daemon mode. So that only one time startup is longer, and subsequent startup of emacs (emacs client to be precise) will be much faster.

I have written the following shell script (em, please place it in your $PATH), which starts the emacs daemon on first time only, and connects emacsclient to it.
#!/bin/bash

usr=$(whoami)
emacs_daemon="emacs --daemon"
cmd="ps auxww | grep \"$usr.*$emacs_daemon\" | grep -v grep"
#echo "$cmd"
is_running=$(eval "$cmd")
if [ -z "$is_running" ]
then
    echo "Starting emacs daemon"
    eval "$emacs_daemon"
    if [ $? -ne 0 ]
    then
        echo "$0: Cannot start $emacs_daemon" >&2
        exit
    fi
fi
emacsclient -a "" -t "$@"
exit $?

First Run:
$ em Hello.java
Starting emacs daemon

Warning: due to a long standing Gtk+ bug
http://bugzilla.gnome.org/show_bug.cgi?id=85715
Emacs might crash when run in daemon mode and the X11 connection is unexpectedly lost.
Using an Emacs configured with --with-x-toolkit=lucid does not have this problem.
("emacs")
Loading /usr/share/emacs/site-lisp/site-start.d/auto-complete-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/auto-complete-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-color-theme-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-color-theme-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-goodies-loaddefs.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-goodies-loaddefs.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/focus-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/focus-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/gnuplot-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/gnuplot-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/gnus-bonus-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/gnus-bonus-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/rpm-spec-mode-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/rpm-spec-mode-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/rpmdev-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/rpmdev-init.el (source)...done
Loading /usr/share/emacs/site-lisp/cedet-1.1/common/cedet.el (source)...
Setting up CEDET packages...
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Setting up CEDET packages...done
Loading /usr/share/emacs/site-lisp/cedet-1.1/common/cedet.el (source)...done
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1                                                                                                  
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1                                                                                                  
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
jde-java-font-lock: building names cache...
jde-java-font-lock: building names cache...empty
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Starting Emacs daemon.

Second and Subsequent Runs:
$ em Hello.java

4 comments:

Ajay said...

NICE BLOG!!! Thanks for sharing useful information about Touraeas and being one of best Tourism Website agree that this blog is very useful for the Tourism are Searching for best tourist Places , I would really like to come back again right here for likewise good articles or blog posts. Thanks for sharing...Cheap tour packages Bangalore.

Raghav Kumar Gautam said...

What version of emacs, cedet & jdee are you using ? Does it work reliably ?

Raghav Kumar Gautam said...
This comment has been removed by the author.
mitesh.singh.jat said...

Hi Raghav,


I have used
cedet 1.1
jdee 2.4.1

for any issue related to versions, please refer to
this site
http://www.emacswiki.org/emacs/JavaDevelopmentEnvironment

Regards,
Mitesh