From: Michael Bohn
Subject: Howto find the enclosing top-level-form
Date: 
Message-ID: <45508efe$0$5725$9b4e6d93@newsspool3.arcor-online.net>
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

From: Greg Buchholz
Subject: Re: Howto find the enclosing top-level-form
Date: 
Message-ID: <1162920852.984416.97720@m73g2000cwd.googlegroups.com>
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
From: Thomas A. Russ
Subject: Re: Howto find the enclosing top-level-form
Date: 
Message-ID: <ymi64dr9i40.fsf@sevak.isi.edu>
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
From: Larry Clapp
Subject: Re: Howto find the enclosing top-level-form
Date: 
Message-ID: <slrnel18iv.6ob.larry@theclapp.ddts.net>
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
From: Michael Bohn
Subject: Re: Howto find the enclosing top-level-form
Date: 
Message-ID: <4550b51b$0$5719$9b4e6d93@newsspool3.arcor-online.net>
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.
From: Pascal Bourguignon
Subject: Re: Howto find the enclosing top-level-form
Date: 
Message-ID: <87ac33wdi1.fsf@thalassa.informatimago.com>
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.