On 8/18/2025 7:34 PM, Henning Hraban Ramm wrote:
Am 18.08.25 um 18:31 schrieb Hans Hagen via ntg-context:
On 8/18/2025 3:10 PM, Henning Hraban Ramm wrote:
How would you resolve shell/environment variables like $TEXMFHOME or ~ ($HOME) in Lua code?

dir.expandname doesn’t do that.

Search and replace iterating over os.env?

These tex related variables are often encoded in a special way so you need to do more (and also know how that coding works). Anyway, it's not the kind of code one sees / uses everyday (but it's nearly 20 years old now so quite stable):

\startluacode
     local function whatever(v)
         local p = resolvers.expandedpathlistfromvariable(v)
         inspect(p)
         for i=1,#p do
             print(resolvers.resolve(p[i]))
         end
         for i=1,#p do
             print(resolvers.cleanpath(resolvers.resolve(p[i])))
         end
     end
     whatever("TEXMFHOME")
     whatever("TEXMF")
\stopluacode

Thank you!

I guess that was what I was asking for, but not what I needed.

I didn’t think of TeX variables containing lists of paths in opposite to environment/shell variables that resolve to one value (even if it might be a function).

I can’t iterate over os.env like I hoped, the table is empty. I can only ask for single values with os.env[] or os.getenv().

This works (in bash), even if the code is ugly:


function os.resolveenv(path)
   -- resolve environment variables
   path, _ = string.gsub(path, "~", "${HOME}")
   -- are there other special characters to resolve?
   local patt = "%${?[%a_-]+}?" -- $BLA or ${BLA}
   local inpatt = "[%a_-]+" -- just the name
   local found = true
   while found do
     -- there might be several variables in a path
     local var = string.match(path, patt)
     if var then
       local value = os.getenv(string.match(var, inpatt))
       if value then
         path, _ = string.gsub(path, var, value)
       else
        logs.report("resolve env", "Couldn’t resolve variable " .. var .. " in " .. path)
         -- if we don’t break here, we get an endless loop;
         -- otherwise we would need to track the finds
         found = false
       end
     else
       found = false
     end
   end
   logs.report("resolve env", path)
   return path
end

os.resolveenv("~/context/")
os.resolveenv("${JAVA_HOME}/something")

>set WHATEVERNAME=foo
>set WHATEVERMORE=oof
>set WHATEVERELSE=oof/{$WHATEVERNAME}


    local p = resolvers.expandedpathlistfromvariable("WHATEVERNAME")
    inspect(p)
    local p = resolvers.expandedpathlistfromvariable("WHATEVERELSE")
    inspect(p)


table={
 "foo",
}
table={
 "oof/foo",
}

so to some extend ...

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 : [email protected] / 
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 via email to