ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen via ntg-context <ntg-context@ntg.nl>
To: ntg-context@ntg.nl
Cc: Hans Hagen <j.hagen@freedom.nl>
Subject: [NTG-context] Re: How can a macro check how many items are in a list?
Date: Mon, 4 Nov 2024 18:52:43 +0100	[thread overview]
Message-ID: <9ce0b651-44c8-4c34-beec-5da7de645d53@freedom.nl> (raw)
In-Reply-To: <544F4782-4007-4CBC-8F2F-30299E93B906@scorecrow.com>

On 11/3/2024 5:53 PM, Bruce Horrocks wrote:
> \definecounter [MyItemsCounter]
> \definecounter [MyItemsUniqueID] [way=bytext]
> \setcounter    [MyItemsUniqueID] [1]
> 
> \definedataset [MyItems] [delay=yes]
> 
> \defineitemgroup [MyItems]
> \setupitemgroup  [MyItems]
>    [ before={\setcounter[MyItemsCounter][1]},
>      inbetween={\incrementcounter[MyItemsCounter]},
>      after={\setdataset[MyItems][\rawcountervalue[MyItemsUniqueID]][count=\rawcountervalue[MyItemsCounter]]%
>             \incrementcounter[MyItemsUniqueID]},
>    ]
> 
> \define\listLen{\datasetvariable{MyItems}{\rawcountervalue[MyItemsUniqueID]}{count}}
> \define\itemOrItems{\doifelse{\datasetvariable{MyItems}{\rawcountervalue[MyItemsUniqueID]}{count}}{1}{item}{items}}
> 
> \starttext
> 
> This list has \listLen\ \itemOrItems.
> \startMyItems
> \item cat
> \item dog
> \item tree
> \stopMyItems
> 
> \blank
> 
> This list has \listLen\ \itemOrItems.
> \startMyItems
> \item cow
> \stopMyItems
> 
> \stoptext

There is actually some info when you are in an itemize:

\currentitemindex
\currentnofitems

So it's not too hard to get the upcoming value of \currentnofitems:

(1) an extra helper in strc-itm.mkxl

\permanent\tolerant\protected\def\upcomingnofitems[#1]%
   {\the\numexpr
      \clf_analyzeupcomingitemgroup
        {\ifempty{#1}\v!itemize\else#1\fi}%
        \numexpr\c_strc_itemgroups_nesting+\plusone\relax
      \c_strc_itemgroups_max_items
    \relax}

(2) a few line patch in strc-itm.lmt

local function analyzeitemgroup(name,level,upcoming)
     local n = counts[name]
     local u = false
     if level == 1 then
         n = n + 1
         counts[name] = n
         u = upcoming and true
     end
     local items = 0
     local width = 0
     local itemgroup = collected[name]
     if itemgroup then
         local entry = itemgroup[n]
         if entry then
             local l = entry[level]
             if l then
                 items = l[1] or 0
                 width = l[2] or 0
             end
         end
     end
     if u then
         counts[name] = n - 1
     end
     texsetcount(c_strc_itemgroups_max_items,items)
     texsetdimen(d_strc_itemgroups_max_width,width)
end

plus extra:

implement {
     name      = "analyzeupcomingitemgroup",
     actions   = analyzeitemgroup,
     arguments = { "string", "integer", true }
}

And then for Bruce to come up with the singular / plural variant for

This list has \upcomingnofitems[MyItems] entries

\startMyItems
\item cat
\item dog
\item tree
\stopMyItems

This list has \upcomingnofitems\ entries

\startitemize
\item cat
\item dog
\item tree
\stopitemize


Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

  reply	other threads:[~2024-11-04 17:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <692582251.5119511.1730577725919.ref@mail.yahoo.com>
2024-11-02 20:02 ` [NTG-context] " Joel via ntg-context
2024-11-03  7:19   ` [NTG-context] " Wolfgang Schuster
2024-11-03 11:04     ` vm via ntg-context
2024-11-03 16:53   ` Bruce Horrocks
2024-11-04 17:52     ` Hans Hagen via ntg-context [this message]
2024-11-05 18:21       ` Bruce Horrocks

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9ce0b651-44c8-4c34-beec-5da7de645d53@freedom.nl \
    --to=ntg-context@ntg.nl \
    --cc=j.hagen@freedom.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).