Hello Newsgroup,
I want to find the start and end of the enclosing toplevel-form.
For example:
() ; Top-Level-1
---
( ; Top-Level-2
()
((()))
)
---
This is easy if I'm scanning the document from the beginning. But
what if I'm in the middle of an toplevel-form.
For example:
() ; Top-Level-1
---
( ; Top-Level-2
()
((,()))
)
---
If I'm on the position of the comma, I want to find the start and
endposition of Top-Level-2. But without scanning the whole document.
How can this be done?
Thanks for any hints
Michael
Michael Bohn wrote:
...snip...
> If I'm on the position of the comma, I want to find the start and
> endposition of Top-Level-2. But without scanning the whole document.
> How can this be done?
Take a look at "Zipper"-like data structures...
http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf
http://okmij.org/ftp/Scheme/misc.html#zipper
http://www.informatik.uni-bonn.de/~ralf/publications/TheWeb.ps.gz
Michael Bohn <············@gmx.de> writes:
> Hello Newsgroup,
>
> I want to find the start and end of the enclosing toplevel-form.
For the start, type C-M-A and from there C-M-F.
--
Thomas A. Russ, USC/Information Sciences Institute
On 2006-11-07, Michael Bohn <············@gmx.de> wrote:
> I want to find the start and end of the enclosing toplevel-form.
> For example:
>
> () ; Top-Level-1
> ---
> ( ; Top-Level-2
> ()
> ((()))
> )
> ---
>
> This is easy if I'm scanning the document from the beginning. But
> what if I'm in the middle of an toplevel-form.
>
> For example:
>
> () ; Top-Level-1
> ---
> ( ; Top-Level-2
> ()
> ((,()))
> )
> ---
>
> If I'm on the position of the comma, I want to find the start and
> endposition of Top-Level-2. But without scanning the whole document.
> How can this be done?
It can't. But for most files on most computers Vim (for example) can
do it pretty quickly, even so. Try 99[( or to make it permanent add
nmap [\ 99[(
to your $HOME/.vimrc file.
This is really more of an editor question than a Lisp question.
Unless you want to do it in a program somewhere. In that case, the
simple answer is, you can't.
For a more complex answer, you could pre-parse the file and keep
pointers to the beginning each top-level expression. Then you have
the headache of keeping your pointers up-to-date during
inserts/deletes/etc. And it probably wouldn't save you that much time
over just scanning the file from the beginning, and it seems like it
would add a lot of complexity (comparitively speaking).
-- L
Larry Clapp wrote:
...
>>
>> If I'm on the position of the comma, I want to find the start and
>> endposition of Top-Level-2. But without scanning the whole document.
>> How can this be done?
>
> It can't. But for most files on most computers Vim (for example) can
> do it pretty quickly, even so. Try 99[( or to make it permanent add
>
> nmap [\ 99[(
>
> to your $HOME/.vimrc file.
>
> This is really more of an editor question than a Lisp question.
> Unless you want to do it in a program somewhere. In that case, the
> simple answer is, you can't.
>
> For a more complex answer, you could pre-parse the file and keep
> pointers to the beginning each top-level expression. Then you have
> the headache of keeping your pointers up-to-date during
> inserts/deletes/etc. And it probably wouldn't save you that much time
> over just scanning the file from the beginning, and it seems like it
> would add a lot of complexity (comparitively speaking).
>
> -- L
>
Ok, thanks.
I need this for my Eclipse-Plugin (a Lisp-Editor for Eclipse).
So I could really manage the positions via the
IDocument.addPosition(...) mechanism. But that's now Java :).
I think I will scan the whole file for the current toplevel-form.
Michael Bohn <············@gmx.de> writes:
> Hello Newsgroup,
>
> I want to find the start and end of the enclosing toplevel-form.
> For example:
>
> () ; Top-Level-1
> ---
> ( ; Top-Level-2
> ()
> ((()))
> )
> ---
>
> This is easy if I'm scanning the document from the beginning. But
> what if I'm in the middle of an toplevel-form.
>
> For example:
>
> () ; Top-Level-1
> ---
> ( ; Top-Level-2
> ()
> ((,()))
> )
> ---
>
> If I'm on the position of the comma, I want to find the start and
> endposition of Top-Level-2. But without scanning the whole document.
> How can this be done?
(defun go-to-begin-of-toplevel-sexp ()
(interactive)
(while (ignore-errors (progn (up-list -1) t))))
--
__Pascal Bourguignon__ http://www.informatimago.com/
CAUTION: The mass of this product contains the energy equivalent of
85 million tons of TNT per net ounce of weight.