all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Spaces please!
Date: Tue, 01 Dec 2009 18:06:57 +0100	[thread overview]
Message-ID: <874ooa39ou.fsf@galatea.local> (raw)
In-Reply-To: mailman.11955.1259684648.2239.help-gnu-emacs@gnu.org

andrea <andrea.crotti.0@gmail.com> 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__


       reply	other threads:[~2009-12-01 17:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.11955.1259684648.2239.help-gnu-emacs@gnu.org>
2009-12-01 17:06 ` Pascal J. Bourguignon [this message]
2009-12-01 17:42   ` Spaces please! Harald Hanche-Olsen
2009-12-01 17:46   ` Lennart Borgman
2009-12-01 18:21 ` Burkhard Schultheis
2009-12-01 18:26   ` Burkhard Schultheis
2009-12-01 16:19 andrea
2009-12-02 13:33 ` Thien-Thi Nguyen

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=874ooa39ou.fsf@galatea.local \
    --to=pjb@informatimago.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.