%D \module
%D   [       file=mtx-context-meaning,
%D        version=2018.01.12,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Show Meanings,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% begin help
%
% usage: context --extra=meaning [options] list-of-commands
%
% --pattern : match given pattern
%
% example: context --extra=meaning --pattern=*paper*
%          context --extra=meaning setuplayout
%          context --extra=meaning setup_layout
%          context --extra=meaning --once --noconsole --nostatistics setuplayout
%          mtxrun --silent --script context --extra=meaning --once  --noconsole --nostatistics setuplayout
%          mtxrun --script interface --meaning setuplayout
%
% end help

\input mtx-context-common.tex

\setupbodyfont
  [dejavu]

\unexpanded\def\ShowMeaning#1%
  {\ctxlua{logs.pushtarget("both")}%
   \writestatus{meaning}{\strippedcsname#1}%
   \writestring{}%
   \writestring{\expandafter\meaning\begincsname#1\endcsname}%
   \ctxlua{logs.poptarget()}}

\unexpanded\def\ShowTokens#1%
  {\ctxlua{logs.pushtarget("both")}%
   \writestatus{tokens}{\strippedcsname#1}%
   \writestring{}%
   \ifcase\contextlmtxmode
      \writestring{only supported in lmtx}%
   \else
      \expandafter\showluatokens\begincsname#1\endcsname
   \fi
   \ctxlua{logs.poptarget()}}

\starttext

\usemodule[setups-macros]

\startluacode
    local h = tex.hashtokens()
    local t = environment.arguments.tokens

    local function showmeaning(str)
        local h = interfaces.macros.collect(str)
        context("pattern: ")
        context.type(str)
        context.blank()
        for i=1,#h do
            local hi = h[i]
            context.type(hi)
            context.par()
            if t then
                context.ShowTokens(hi)
            else
                context.ShowMeaning(hi)
            end
        end
        context.page()
    end

    local done    = false
    local pattern = document.arguments.pattern

    if pattern then
        pattern = { pattern}
    else
        pattern = document.files
    end

    if type(pattern) == "table" then
        table.sort(pattern)
        for i=1,#pattern do
            local p = pattern[i]
            if not string.find(p,"^mtx%-context%-") then
                done = true
                showmeaning(p)
            end
        end
    end

    if not done then
        context("no search pattern given")
    end
\stopluacode

\stoptext
