3

您好我正在尝试编写一个 Python 程序来保存 Emacs 的文件,以防止失去窗口焦点。

为此,我编写了一个 Python 程序,它创建了一个完整的 gtk 应用程序并使用了 wnck 模块:

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 = ''

不幸的是,Python 程序冻结了 Emacs;可能是因为 Emacs 等待程序完成。有没有办法让程序在后台运行?

任何帮助都非常感谢。

4

2 回答 2

1

Pymacs 主要只是意味着您可以在 python 中编写 emacs 扩展。它不是 IPC 方法。

如果你想让两个程序同时运行并在外部事件发生时互相发送消息,你需要 IPC。

现代 Linux 系统上一种非常常见的 IPC 形式是(可怕的无证)dbus。Emacs 支持 dbus(这似乎也没有很好的文档记录)。

但是,here(rakete 的回答)是一个非常好的示例,说明如何为 emacs 函数提供 dbus 接口

因此,您可能想要做的是在 emacs 中创建一个“安全缓冲区”方法,将其注册为可从 dbus 访问,启动程序以监视未聚焦事件并通过 dbus 调用该“安全缓冲区”方法。

于 2012-08-06T08:50:29.470 回答
0

我制作了Python-EPC,一个用 Python 实现的 EPC 服务器。EPC 是一个 RPC 堆栈,专为 Emacs Lisp 设计。使用 Python-EPC,您可以从 Emacs 调用 Python 函数,从 Python 调用 Emacs Lisp 函数。这是与 GTK 集成的示例:https ://github.com/tkf/python-epc/blob/master/examples/gtk/server.py

于 2012-11-11T19:52:50.547 回答