From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Luke Gorrie Newsgroups: gmane.emacs.help Subject: Re: Elegance in elisp, need advices Date: 03 Feb 2003 01:30:40 +0100 Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: <874r7r7v1d.fsf@noos.fr> <5ly9537p5w.fsf@rum.cs.yale.edu> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1044232514 21563 80.91.224.249 (3 Feb 2003 00:35:14 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 3 Feb 2003 00:35:14 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18fUZr-0005bW-00 for ; Mon, 03 Feb 2003 01:35:12 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 18fUaK-0008D8-01 for gnu-help-gnu-emacs@m.gmane.org; Sun, 02 Feb 2003 19:35:40 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!logbridge.uoregon.edu!news-FFM2.ecrc.net!news.ecrc.de!not-for-mail Original-Newsgroups: gnu.emacs.help X-Sincerity: 14% (approx.) Original-Lines: 64 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Original-NNTP-Posting-Host: 195.149.129.26 Original-X-Complaints-To: news@ecrc.de Original-X-Trace: news.ecrc.de 1044232324 195.149.129.26 (Mon, 03 Feb 2003 01:32:04 MET) Original-NNTP-Posting-Date: Mon, 03 Feb 2003 01:32:04 MET Original-Xref: shelby.stanford.edu gnu.emacs.help:109707 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:6220 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:6220 "Stefan Monnier " writes: > [ redirecting follow-ups to gnu.emacs.help ] > > > For instance, the connection is set up with open-network-stream and > > set-process-filter. I keep a buffer-like string (buffer as in C not as > > in emacs), to which I concatenate every received string. When I am > [...] > > What would be the 'good' way to do that ? > > Other than details of the layout of your code (more than 80 cols, docstring > on the same line as the `defun', ...) it looks OK. I don't know of any way > to make it much better. You could of course use a real Emacs buffer rather > than a string, so you can use `insert / delete-region' rather than > `concat / substring', but fundamentally, it can't be very different. > > Admittedly, many process filters do such things, so it might be worth it > to provide a generic way to solve it, but I don't know of anybody who's > done that yet. I have something like this, it's a simple library for writing network-attached state machines. It basically gives your process buffer a "state", which is the name of a function to call when something happens (initialization, data arrives, disconnection.) Like a sentinel+filter, but I also accumulate data in the buffer itself (optionally.) The state machine also has a function to call if it terminates successfully and one to call if it fails (explicitly or via lisp error.) This is nice if you want to deliver the final result of your connection to some other code. When data arrives, the state function will typically check the buffer to see if a whole message is there, and if not just return (to be reinvoked when more data arrives), otherwise consume the message and possibly change states. It also has some debugging support, via an *fsm-debug* buffer that logs events and state changes. It's not properly documented, but the code is here (net-fsm.el): http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/distel/distel/elisp/net-fsm.el?rev=1.6&content-type=text/vnd.viewcvs-markup It was mostly written to support an implementation of the Distributed Erlang state machine (derl.el): http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/distel/distel/elisp/derl.el?rev=1.16&content-type=text/vnd.viewcvs-markup A simpler machine talks to the Erlang Port Mapper Daemon (epmd.el): http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/distel/distel/elisp/epmd.el?rev=1.1&content-type=text/vnd.viewcvs-markup If this sounds interesting but the code is too opaque, let me know and I'll document it when I have a chance. BTW, Emacs buffers (rather than strings) make exquisite network buffers I reckon, since you can interactively switch into them and call your data-reading functions via M-: or the debugger, and see the point advance through the input, etc. I love it a lot for debugging. Cheers, Luke