Re: An idiom for :include {once}
Bram Moolenaar <[email protected]>
| Newsgroups | gmane.comp.tools.aap.user |
|---|---|
| Message-ID | <[email protected]> |
Adriaan de Groot wrote:
> When you use :include, you can specify {once} as an attribute, to
> prevent AAP from including the file more than once. This is useful
> when the included file contains :route commands, which cannot be
> redefined.
I already explained the intend of using {once} in another message.
> Unfortunately, this places the burden of deciding what to include once
> _at the point of inclusion_, not in the included file. And it's the
> included file, after all, that knows whether it can be included more
> than once or not. Hence, {once} isn't a very useful mechanism for
> including things once because the recipe doing the :including pretty
> much needs to specify {once} for _every_ include, just to be safe.
>
> Once workaround is to make {once} default for include, and add a
> {noonce} to allow multiple inclusion explicitly.
That would not be backwards compatible. I don't like it anyway.
":include recipe" should include the recipe without surprises.
> Or, here's an idiom comparable to C's #ifndef #define idiom to prevent
> multiple inclusion in the file that is being included.
>
> filename_aap_seen?=1
> @if _no.filename_aap_seen == "1":
> # Normal content of file
> filename_aap_seen=2
>
> This assumes - just like the C idiom - that noone else messes with the
> ..._seen variables. Because the filename_aap_seen variable is given a value,
> the first assignment is done exactly once, and the bottom assignment prevents
> the @if statement from being executed a second time.
>
> Another form of the same idiom is
>
> @if not globals().get("_no.filename_aap_seen"):
> # Normal contents
> filename_aap_seen=1
Yes, this is a good mechanism to avoid defining something twice when you
include a recipe in several places. A bit simpler:
@if not _top.get("seen_this_recipe"):
_top.seen_this_recipe = 1
:route something # define route only once
# more commands A
MyVar = something # do this in every scope
# more commands B
Hmm, the indenting doesn't look that nice if the block of "commands A"
once gets longer. Perhaps we should add a ":finish" command, that stops
processing the recipe? People using Vim will recognize it.
MyVar = something # do this in every scope
# more commands B
@if _top.get("seen_this_recipe"):
:finish
_top.seen_this_recipe = 1
:route something # define route only once
# more commands A
--
hundred-and-one symptoms of being an internet addict:
153. You find yourself staring at your "inbox" waiting for new e-mail
to arrive.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// Creator of Vim - Vi IMproved -- http://www.Vim.org \\\
\\\ Project leader for A-A-P -- http://www.A-A-P.org ///
\\\ Help AIDS victims, buy here: http://ICCF-Holland.org/click1.html ///
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01