From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: Spaces please! Date: Tue, 01 Dec 2009 18:06:57 +0100 Organization: Informatimago Message-ID: <874ooa39ou.fsf@galatea.local> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1259689279 4819 80.91.229.12 (1 Dec 2009 17:41:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 1 Dec 2009 17:41:19 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Dec 01 18:41:12 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1NFWis-0000E5-Kb for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Dec 2009 18:41:10 +0100 Original-Received: from localhost ([127.0.0.1]:39984 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NFWir-00073V-UG for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Dec 2009 12:41:09 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 69 Original-X-Trace: individual.net skk84zkUZQBGIpn9nv/tsQUlO2n+3R0RIJLz4pkZBvxNPrpqhp Cancel-Lock: sha1:ZjI1ZDhkNGE3MTA0ZTliMWM1Y2JiNTk5MGMwNzE5MDdiYjUxZjUwNQ== sha1:AxmFEl20XPCCmMT4hEvtOKraNXo= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin) Original-Xref: news.stanford.edu gnu.emacs.help:175209 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:70283 Archived-At: andrea writes: > I get sick sometimes to read something like > x=y+1*(2-3*a^2) > but I get even more sick to add spaces manually every time. Funny. I get sick reading something like x = y+1 * ( 2-3 * a ^2) ;-) Perhaps the best form would be: (= x (+ y (* 1 (- 2 (* 3 (^ a 2)))))) but that would be a different syntax. Notice that it could be the work of an editor to read a source in whatever "syntax" it may happen to be, to present it to the user in whatever form the user prefers, allowing it to do structural editing instead of textual edition, and saving it back to whatever "syntax" is required. A little like it is done for character encodings, where you can load a file in iso-2022, edit it in emacs unicode, and save it in utf-8, only for language syntaxes. But that'd be a whole project. > I ask help to you elisp gurus, should I use a regexp No, you cannot parse arithmetic expressions with regular expressions, they are too weak! You need a parser. > or a macro-like > function (go-here, do-this)? > > I think I should define some rules like: > - which operators want spaces (and if before or after) > - what is the arity of them > > One problem for example could arise from things like > > (-2)^10 > but we could have some operators that could be binary or unary and check > that somehow. > > Someone has done it already maybe? Well, you could hack something half working yourself, or perhaps you could use a predefined parser for the language in question. http://cedet.sourceforget.net has a parser generator (the "bovinator", and parsers predefined for a number of programming languages, that you could use to implement your "formula beautifier". Once you have the parse tree, it is indeed trivial to generate the expression with the number of spaces you want. -- __Pascal Bourguignon__