Rockiger
Rockiger

Reputation: 422

How to load a GTK program in Pymacs

Hello I am trying to write a Python program to save Emacs' files on the loss of window focus.

For that I wrote a Python programm that creates a full gtk application and uses the wnck module:

from Pymacs import lisp

import wnck
import gtk

class AutoSaver(object):
    """This class watches if Emacs looses focus and if Emacs looses
    focus saves all buffers with files
    """

    def __init__(self):
        """
        """
        self.screen = wnck.screen_get_default()
        self.screen.force_update()
        self.screen.connect("active_window_changed", self.watch_for_emacs)

    def watch_for_emacs(self, screen, data=None):
        screen.force_update()
        win_list = screen.get_windows()
        for win in win_list:
            if win.get_application().get_name().startswith("emacs"):
                self.save_all_buffers()

    def save_all_buffers(self):
        lisp.save_some_buffers(True, None)

    def main(self):
        """
        Starts GTK's main loop.
        """
        gtk.main()

def start():
    autosaver = AutoSaver()
    autosaver.main()

start.interaction = ''

Unfortunately the Python programm freezes Emacs; probably because Emacs waits for the program to finish. Is there a way to let run the program in the background?

Any help really appreciated.

Upvotes: 3

Views: 178

Answers (2)

tkf
tkf

Reputation: 3020

I made Python-EPC, an EPC server implementation in Python. EPC is an RPC stack, which is designed for Emacs Lisp. Using Python-EPC, you can call Python functions from Emacs and Emacs Lisp functions from Python. Here is an example for integration with GTK: https://github.com/tkf/python-epc/blob/master/examples/gtk/server.py

Upvotes: 0

xubuntix
xubuntix

Reputation: 2333

Pymacs mainly just means that you can programm emacs extension in python. It is not an IPC method.

If you want to have two programs running at the same time and sending each other messages when external events occur, you need IPC.

A very common form of IPC on modern Linux systems is (the horribly undocumented) dbus. Emacs has support for dbus (which seems also not well documented).

However, here (the answer by rakete) is a very nice example on how to provide a dbus interface to an emacs function.

So, what you probably want to do is create a "safe-buffer" method in emacs, register it to be accessible from dbus, start your program to watch for unfocus events and call that "safe-buffer" method via dbus.

Upvotes: 1

Related Questions