From: ·········@gmail.com
Subject: Extending forward-list to include multi-letter tags
Date: 
Message-ID: <1192855886.072329.192610@k35g2000prh.googlegroups.com>
I am sure this is a trivial problem for many of you, and it is one of
those problems that is like a mathematical puzzle with definite
solutions. I am interested in seeing multiple solutions to it.

Here is the problem:

The pair of functions forward-list and backward-list allow moving
the cursor to the next matching paren, bracket or brace. So it
works for characters like
{[(  )]}

The main utility of this function is mainly due to the nesting that
may be there, so a simple forward search wont take care of it.

Here, I would like to find out how one can find matching multi
letter tags.

For example, I have a tag like

<tag> and its matching tag is </tag>

there may be nested tags like this.

Essentially, the solution involves first finding the algorithm for
the forward-list and then extending it to multi-letter tags.

How can I get the lisp definition of a function inside emacs ?

I have posted this in cll and cls because their solutions may
be modified to work in emacs lisp.

Thanks to the star who can solve it.
gnuist
From: Thomas A. Russ
Subject: Re: Extending forward-list to include multi-letter tags
Date: 
Message-ID: <ymid4v63m89.fsf@blackcat.isi.edu>
·········@gmail.com writes:

> I am sure this is a trivial problem for many of you, and it is one of
> those problems that is like a mathematical puzzle with definite
> solutions. I am interested in seeing multiple solutions to it.
> 
> Here is the problem:
> 
> The pair of functions forward-list and backward-list allow moving
> the cursor to the next matching paren, bracket or brace. So it
> works for characters like
> {[(  )]}
> 
> The main utility of this function is mainly due to the nesting that
> may be there, so a simple forward search wont take care of it.

Well, not exactly, but a fairly simple counting verison of forward
search works just fine.  The only fly in the ointment, which afflicts
the Emacs versions, BTW, is if you have the possibility of encountering
escape or comment contexts where you don't want the matching to happen.

This is particularly a problem for the backward movement verison, since
the forward verison can typically manage to do the parsing correctly.

But for a quick outline of what you want to do, this is the basic
algorithm:

  1.  Set open counter = 1
  2.  Search character by character.
  3.  If an open character is encountered increment open counter.
  4.  If a close character is encountered increment close counter.
  5.  If the open counter = close counter value, you are done.

> Here, I would like to find out how one can find matching multi
> letter tags.

It works the same way, except that there is more lookahead in the
matching function.  You might also want to consider something like a
Boyer-Moore search algorithm for the multi-tag case.  Or even some
other, syntax-aware parsing of the content.

-- 
Thomas A. Russ,  USC/Information Sciences Institute