help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to search and replace in nested expressions


From: Pascal Bourguignon
Subject: Re: how to search and replace in nested expressions
Date: Mon, 13 Feb 2006 07:01:43 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

"JAMES FUNDERBURK" <funderburk1@verizon.net> writes:

> Is there a way to search and replace in nested expressions?
>   The particular form of my nested expressions is
>      form[x,y,...z]  where each 'argument' may be either
>     a word (simple character string) or else
>     another nested expression.  Note that the arguments are
>     separated by commas, but there may be only one argument.
>     For example, it might be desired to change expressions of the form
>        simple[x,y] to simple[y,x]  in such a way that
>        simple[apple[u],pear[v,w]] is changed to
>        simple[pear[v,w],apple[u]].
>    I am familiar with Emacs-Lisp, so am looking for a function to do
>    such changes on a buffer.  I have used the regular expression search  and 
> replace functions quite a bit, but don't see how to apply them in this 
> situation.

Regular expressions are not powerfull enough to handle nested
expressions.  You need a parser.  That's why lisp (lack of) syntax is
best: you only need one simple parser to parse any language and any
data.

To change:
(simple (apple u) (pear v w))
into:
(simple (pear v w) (apple u))

I only need to type:

(replace-sexps (buffer-file-name (current-buffer))
               (lambda (sexp) (if (and (consp sexp)
                                  (eq 'simple (first sexp))
                                  (= 3 (length sexp)))
                           `(simple ,(third sexp) ,(second sexp))
                           sexp)) :deeply t)


http://www.informatimago.com/develop/emacs
cvs -z3 -d 
:pserver:anonymous@cvs.informatimago.com:/usr/local/cvs/public/chrooted-cvs/cvs 
co emacs
(see pjb-sources.el)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

ADVISORY: There is an extremely small but nonzero chance that,
through a process known as "tunneling," this product may
spontaneously disappear from its present location and reappear at
any random place in the universe, including your neighbor's
domicile. The manufacturer will not be responsible for any damages
or inconveniences that may result.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]