From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alexis Roda Newsgroups: gmane.emacs.help 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 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1472218781 2061 195.159.176.226 (26 Aug 2016 13:39:41 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 26 Aug 2016 13:39:41 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 Cc: Brian Merchant To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Aug 26 15:39:35 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bdHM6-0007yR-7U for geh-help-gnu-emacs@m.gmane.org; Fri, 26 Aug 2016 15:39:34 +0200 Original-Received: from localhost ([::1]:60590 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bdHLy-0002fZ-Ew for geh-help-gnu-emacs@m.gmane.org; Fri, 26 Aug 2016 09:39:26 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38351) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bdHBL-0004gd-HA for help-gnu-emacs@gnu.org; Fri, 26 Aug 2016 09:28:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bdHBG-0005ty-Mj for help-gnu-emacs@gnu.org; Fri, 26 Aug 2016 09:28:27 -0400 Original-Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:38306) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bdHBG-0005tR-Fq for help-gnu-emacs@gnu.org; Fri, 26 Aug 2016 09:28:22 -0400 Original-Received: by mail-wm0-x22e.google.com with SMTP id o80so119480448wme.1 for ; Fri, 26 Aug 2016 06:28:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:cc:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=x/RCkdDmDli6g7u2i2b7v/4cHcJEnwtfgaD9oIPVZMU=; b=oBvBKTtm0d7xQlUJXRxpFfQNDdcshYoWLPM/H5nKDBG7NJN8XXoW+DgGqZ/f5iWjvG 8UBg3IPoTvIRQ0gLsBNbt9dqjL9Iu+mJWUKswEdaPVvG7I5vKyA8zhB7Fu6lqxmv7YB7 BbiFVNLvxWm0qdSucj1ckBIGKPE2+vmBgS1EZZE1icPziJR0IM9iit3Qw7wy7+oxBwTm uqC4wGPcniXsj7xHX1wTjXlnaXiGzIA41xd0Oa3Qk3NAfywVBtTLR1hDbTc7IfDKFy6o kyKiZx24oV5anxE+xn0tlQ/Gz/Vhsrj50jrTLOGBpDTlgqjiUQFJG4MzaCmVNXxTBjFv Fwmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:cc:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=x/RCkdDmDli6g7u2i2b7v/4cHcJEnwtfgaD9oIPVZMU=; b=LDTn00OmN5JMVEM0zLzQDDZp2UFsd1FmMXd7+BCr+J/RM/ShsShCDpncXWD3dgoadw vPdEl2FvPGNC+94n66k85DqmP+wT0lQhRM0d9olDPOegtuLVJJyc45aPHrQDrcrmTOIt e8PhRssto7sZ66EG8kR7g+WHFWEkIgbSIPkKrJmP0XsFinNq33mgqBKAs/FL8I9Nb1XU 3yCDnvmyGTjo8kNWtHqOkujEOPESkWoleDQ9ki2PmP/yWeVdATNj46JEJqg//L5jh8bc lZvYdtCfooiYbYLyyyF3L/Ebm3HaaVnkJE+4GfbDCnRhRE6uD9Wu92AqWonoCOiUQOgj Eokg== X-Gm-Message-State: AEkoouvutKqosbuAfE0rcmEo9ZHPaoFXCMLjMZu74p5+2ohG0eI6Ianpsk4xaddbWz9yCg== X-Received: by 10.28.37.71 with SMTP id l68mr34246787wml.50.1472212275263; Fri, 26 Aug 2016 04:51:15 -0700 (PDT) Original-Received: from [192.168.1.10] (219.red-88-12-231.dynamicip.rima-tde.net. [88.12.231.219]) by smtp.gmail.com with ESMTPSA id ya1sm19697542wjb.23.2016.08.26.04.51.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Aug 2016 04:51:14 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22e X-Mailman-Approved-At: Fri, 26 Aug 2016 09:38:46 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:111206 Archived-At: 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 "") '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