Emacs support in Pida

Embedding an editor is only the matter of two problems:

  • Let the editor receive Pida commands (for example, open or save a file).
  • Make Pida listen the editor events so that the editor can be used the usual way with pida synchronizing its views accordingly.

One more task should be addressed. It is purely cosmetic, but it provides the feel you like to have when using an IDE.

  • Physically embed the editor window in Pida.

Installing Emacs

Emacs 22 provides the choice to use a GTK interface. But frame embedding patches were only introduced in early February 2008. So only an Emacs snapshot can work with Pida by today (March 2008).

Johan Dahlin has built ubuntu feisty packages of a snapshot from an old Emacs CVS snapshot with the necessary patches applied. These packages can be found at http://dragon.async.com.br/~jdahlin/emacs-snapshot/

Romain Francoise provides non official builds of Emacs trunk for Debian (which should work for Ubuntu). See http://emacs.orebokech.com/

Below are two ways to build Emacs. You are welcome to provide other proper ways to achieve the same result. Please take care that Pida only searches for emacs in the path. So, if there are more than one version installed on your system, take care of your path before calling Pida.

Compiling Emacs

From now, I'll assume you know the basic stuff about compiling.

Go through the usual steps:

  • Get Emacs from CVS.
  • Compile Emacs with GTK support. All other compilation options are up to you.
  • Install Emacs the way you like. Remember Pida takes the first Emacs binary in the path.

Compiling Emacs... The Debian way

Actually, these is more than one way to do it. I'll make use of apt-src here, with the snapshot sources provided by Romain.

  • First, update your sources.list file and your archive keyring as described by Romain. Also add unstable archive for dependencies and build dependencies of the emacs-snapshot package.
  • Install apt-src and type apt-src update to synchronize its database.
  • Type apt-src install emacs-snapshot to get the last emacs snapshot sources and all build dependencies.
  • Run apt-src build emacs-snapshot to get the Debian packages from this source.

You should then have some new packages in the same directory. Now, as root or using sudo, try:

  dpkg -i emacs-snapshot-gtk_20070223-1_i386.deb
    emacs-snapshot-bin-common_20070223-1_i386.deb
    emacs-snapshot-common_20070223-1_all.deb
    emacs-snapshot-el_20070223-1_all.deb

Note that there is a timestamp in the package names, so the real package names you get shall differ a bit. Also note you will need the emacsen-common package from unstable achive.

The previous steps were successfully achieved with Debian testing (once when it was still called Etch, and now that it is Lenny).

Pida and Emacs

The rest of this document describes the communication between Emacs and Pida.

Sending messages to Emacs

The latest version of emacsclient provides a way to send Emacs Lisp shunks to an Emacs instance. Since Pida already relies on the latest Emacs, it can make use of emacsclient too without any problems. The emacsclient binary is installed along with the rest of the standard emacs installation.

emacsclient needs that a server is first started inside the Emacs instance to communicate with. Pida starts this server when it spawns Emacs. Pida also takes care of configuring this server with a unique number so that multiple instances of Pida/Emacs can work together.

What Emacs says can be interesting too

Emacs provides hooks to add customized behaviour to the editor. Actually, many modes of Emacs simply rely on one or more hooks to achieve their work.

Pida provides a small Emacs Lisp script that installs some callbacks for the hooks Pida is interested in. The main job of these callbacks is to contact Pida to forward the event that has just popped in Emacs. The communication from Emacs to Pida is made through a socket that is created at Emacs creation time.

The Emacs Lisp script is stored in Pida user directory (for example ~/.pida2 on Unices) if it cannot be found. You can hack it as you want but beware that you can severely disturb Emacs doing this (like breaking the 'kill-emacs' function as a side effect, which is problematic to close Pida). In case you are in trouble, you can delete the script so that Pida will reinitialize it at next launch.