all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alexis Roda <alexis.roda.villalonga@gmail.com>
To: help-gnu-emacs@gnu.org
Cc: Brian Merchant <bhmerchant@gmail.com>
Subject: Re: How to pass messages between emacs and a Python program? (goal: trying to use emacs as a UI)
Date: Fri, 26 Aug 2016 13:51:09 +0200	[thread overview]
Message-ID: <a9d32929-e7a2-9727-0a45-3100c18c51f9@gmail.com> (raw)
In-Reply-To: <CAMt_P4=LFb6dLS=Yv4o6zX_+VMPJ-MjjVbKq8+t81d2NNOsxNA@mail.gmail.com>

El 26/08/16 a les 00:18, Brian Merchant ha escrit:
> I don't want the Python program to be constantly polling the file for
> changes (using a `while` loop), and I probably don't want emacs to be
> constantly polling the file for updates (which I know how to do using the
> `auto-revert` command).

Hi,
not sure what you mean by "polling", for me it means:

while True:
     if file_changed():
         do_something()
     sleep(some_time)

pynofify (https://github.com/seb-m/pyinotify/wiki) can help with that. 
It's event driven, it monitors a set of files and reacts to changes on 
them. Unfortunately, AFAIK, it's not portable, it only works on linux.

Anyway, depending on your use case, this may not be the right approach.

> Maybe I press some key combination, and then that sends a message to a
> Python script that its time to read the file and make updates and then the
> Python script would message emacs and ask it to update what it is
> displaying in its buffer.
>
> Could this be done?

Yes, but you'll need to learn some emacs lisp.

The simplest solution I can think of that does not require a lot of 
emacs lisp is pymacs (https://github.com/pinard/Pymacs). In short, it 
starts a python interpreter in the background and exposes a set of 
python functions to emacs so that you can use them as if they where 
native emacs functions. From memory:

mymodule.py:

def do_something(path):
     # do something on file 'path'
     return changed  # return True or False

emacs:

(require 'pymacs)
(pymacs-load "mymodule.py" "mm")

 From now on you can call functions defined in mymodule.py prefixing 
them with "mm-":

(mm-do-something "/path/to/buffer")

Pymacs will take care of "encoding" the function call, pass it to 
python, get back the return value and "decode" it.

If required you can define a command that operates on the current buffer:

(defun do-something-on-current-buffer ()
   "call do_something on current buffer"
   (interactive)
   ;; TODO: check if buffer has changes not saved to disk and
   ;; prompt the user to save them.
   (if (mm-do-something (buffer-file-name))
     ;; if content changed reload buffer contents
     (revert-buffer t t)))

and bind it to a key:

(global-set-key (kbd "<f12>") 'do-something-on-current-buffer)

The pros are that you do all your coding in python and write a little 
bit of elisp glue code, the cons are that it's seem unmaintained and I'm 
not sure if it will work with python 3.


Packages like elpy or jedi start an RPC server. The idea is pretty much 
the same as pymacs, take care of forwarding function calls to python and 
getting back the result, but using a different communication 
channel/protocol.



HTH




  parent reply	other threads:[~2016-08-26 11:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-25 22:18 How to pass messages between emacs and a Python program? (goal: trying to use emacs as a UI) Brian Merchant
2016-08-26  8:05 ` Michael Albinus
2016-08-26  8:14 ` Thien-Thi Nguyen
2016-08-26 11:51 ` Alexis Roda [this message]
2016-08-28 17:10 ` Pascal J. Bourguignon
2016-08-28 19:50 ` Tomas Nordin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a9d32929-e7a2-9727-0a45-3100c18c51f9@gmail.com \
    --to=alexis.roda.villalonga@gmail.com \
    --cc=bhmerchant@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.