From e4a6b3ac3421762a868c4979d242f390c0e0fdee Mon Sep 17 00:00:00 2001
From: Marius <mariausol@gmail.com>
Date: Thu, 30 Sep 2010 19:58:31 +0300
Subject: beta 2010.09.29 23:37

---
 scripts/context/lua/mtx-interface.lua       |   8 +-
 tex/context/base/buff-ver.mkiv              |   2 +-
 tex/context/base/catc-ctx.tex               |  10 +
 tex/context/base/cont-new.tex               |   2 +-
 tex/context/base/context.tex                |   2 +-
 tex/context/base/grph-fig.mkiv              |   1 +
 tex/context/base/grph-inc.lua               | 308 +++++++++++++++-------------
 tex/context/base/grph-inc.mkiv              |  39 ++--
 tex/context/base/lang-lab.mkiv              |  11 +-
 tex/context/base/lxml-tex.lua               |   3 +-
 tex/context/base/strc-lev.lua               |   8 +-
 tex/context/base/strc-lev.mkiv              |  16 +-
 tex/generic/context/luatex-fonts-merged.lua |   2 +-
 13 files changed, 243 insertions(+), 169 deletions(-)

diff --git a/scripts/context/lua/mtx-interface.lua b/scripts/context/lua/mtx-interface.lua
index d774019a9..986183ffe 100644
--- a/scripts/context/lua/mtx-interface.lua
+++ b/scripts/context/lua/mtx-interface.lua
@@ -261,7 +261,10 @@ if environment.argument("context") then
     scripts.interface.context()
 elseif environment.argument("messages") then
     scripts.interface.messages()
-elseif environment.argument("scite") or environment.argument("bbedit") or environment.argument("jedit") then
+elseif environment.argument("scite")  or
+       environment.argument("bbedit") or
+       environment.argument("jedit")  or
+       environment.argument("raw")    then
     if environment.argument("scite") then
         scripts.interface.editor("scite")
     end
@@ -271,6 +274,9 @@ elseif environment.argument("scite") or environment.argument("bbedit") or enviro
     if environment.argument("jedit") then
         scripts.interface.editor("jedit")
     end
+    if environment.argument("raw") then
+        scripts.interface.editor("raw")
+    end
 elseif environment.argument("check") then
     scripts.interface.check()
 else
diff --git a/tex/context/base/buff-ver.mkiv b/tex/context/base/buff-ver.mkiv
index 9fdc19ca4..d20c5a2a5 100644
--- a/tex/context/base/buff-ver.mkiv
+++ b/tex/context/base/buff-ver.mkiv
@@ -392,7 +392,7 @@
   {\initializetype
    \verbatimcolor
    \dosetverbatimfont
-   \setcatcodetable \typcatcodesa
+   \setcatcodetable \typcatcodesc % was a
    \def\dodotypeDD##1#1{\dodotypeAA{##1}}%
    \dodotypeDD}
 
diff --git a/tex/context/base/catc-ctx.tex b/tex/context/base/catc-ctx.tex
index e2c35c42b..77e8fe88c 100644
--- a/tex/context/base/catc-ctx.tex
+++ b/tex/context/base/catc-ctx.tex
@@ -22,6 +22,7 @@
 % \ifdefined \xmlcatcodesr \else \newcatcodetable \xmlcatcodesr \fi % reduce
 \ifdefined \typcatcodesa \else \newcatcodetable \typcatcodesa \fi % { }
 \ifdefined \typcatcodesb \else \newcatcodetable \typcatcodesb \fi % < >
+\ifdefined \typcatcodesc \else \newcatcodetable \typcatcodesc \fi % delimited
 
 \startcatcodetable \ctxcatcodes
     \catcode`\^^I = 10
@@ -164,6 +165,15 @@
     \catcode`\>   =  2
 \stopcatcodetable
 
+
+\startcatcodetable \typcatcodesc
+    \catcode`\^^I = 12
+    \catcode`\^^M = 12
+    \catcode`\^^L = 12
+    \catcode`\    = 12
+    \catcode`\^^Z = 12
+\stopcatcodetable
+
 \startcatcodetable \txtcatcodes
     \catcode`\^^I = 10
     \catcode`\^^M =  5
diff --git a/tex/context/base/cont-new.tex b/tex/context/base/cont-new.tex
index 7b2b86610..db575e475 100644
--- a/tex/context/base/cont-new.tex
+++ b/tex/context/base/cont-new.tex
@@ -11,7 +11,7 @@
 %C therefore copyrighted by \PRAGMA. See mreadme.pdf for
 %C details.
 
-\newcontextversion{2010.09.29 10:46}
+\newcontextversion{2010.09.29 23:37}
 
 %D This file is loaded at runtime, thereby providing an
 %D excellent place for hacks, patches, extensions and new
diff --git a/tex/context/base/context.tex b/tex/context/base/context.tex
index 48d039a4d..cc3a28b77 100644
--- a/tex/context/base/context.tex
+++ b/tex/context/base/context.tex
@@ -20,7 +20,7 @@
 %D your styles an modules.
 
 \edef\contextformat {\jobname}
-\edef\contextversion{2010.09.29 10:46}
+\edef\contextversion{2010.09.29 23:37}
 
 %D For those who want to use this:
 
diff --git a/tex/context/base/grph-fig.mkiv b/tex/context/base/grph-fig.mkiv
index 5064e5c43..856821b45 100644
--- a/tex/context/base/grph-fig.mkiv
+++ b/tex/context/base/grph-fig.mkiv
@@ -591,6 +591,7 @@
    \c!background=, % new
    \c!splitcolor=\s!white,
    \c!conversion=,
+   \c!resolution=,
    \c!prefix=,
    \c!cache=,
 %  \c!grid=,
diff --git a/tex/context/base/grph-inc.lua b/tex/context/base/grph-inc.lua
index 0a3346846..95a465b4d 100644
--- a/tex/context/base/grph-inc.lua
+++ b/tex/context/base/grph-inc.lua
@@ -248,105 +248,108 @@ end
 
 -- interfacing to tex
 
-do
-
-    local figuredata = { }
-    local callstack  = { }
-
-    function figures.new() -- we could use metatables status -> used -> request but it needs testing
-        local request = {
-            name       = false,
-            label      = false,
-            format     = false,
-            page       = false,
-            width      = false,
-            height     = false,
-            preview    = false,
-            ["repeat"] = false,
-            controls   = false,
-            display    = false,
-            conversion = false,
-            cache      = false,
-            prefix     = false,
-            size       = false,
-        }
-        local used = {
-            fullname   = false,
-            format     = false,
-            name       = false,
-            path       = false,
-            suffix     = false,
-            width      = false,
-            height     = false,
-        }
-        local status = {
-            status     = 0,
-            converted  = false,
-            cached     = false,
-            fullname   = false,
-            format     = false,
-        }
-     -- setmetatable(status, { __index = used })
-     -- setmetatable(used,   { __index = request })
-        figuredata = {
-            request = request,
-            used    = used,
-            status  = status,
-        }
-        return figuredata
-    end
 
-    function figures.push(request)
-        local ncs = #callstack + 1
-        if ncs == 1 then
-            statistics.starttiming(figures)
-        end
-        local figuredata = figures.new()
-        if request then
-        local iv = interfaces.variables
-        --  request.width/height are strings and are only used when no natural dimensions
-        --  can be determined; at some point the handlers might set them to numbers instead
-        --  local w, h = tonumber(request.width), tonumber(request.height)
-            request.page      = math.max(tonumber(request.page) or 1,1)
-            request.size      = img.checksize(request.size)
-            request.object    = iv[request.object] == variables.yes
-            request["repeat"] = iv[request["repeat"]] == variables.yes
-            request.preview   = iv[request.preview] == variables.yes
-            request.cache     = request.cache  ~= "" and request.cache
-            request.prefix    = request.prefix ~= "" and request.prefix
-            request.format    = request.format ~= "" and request.format
-        --  request.width     = (w and w > 0) or false
-        --  request.height    = (h and h > 0) or false
-            table.merge(figuredata.request,request)
-        end
-        callstack[ncs] = figuredata
-        return figuredata
-    end
-    function figures.pop()
-        local ncs = #callstack
-        figuredata = callstack[ncs]
-        callstack[ncs] = nil
-        if ncs == 1 then
-            statistics.stoptiming(figures)
-        end
+local figuredata = { }
+local callstack  = { }
+
+function figures.new() -- we could use metatables status -> used -> request but it needs testing
+    local request = {
+        name       = false,
+        label      = false,
+        format     = false,
+        page       = false,
+        width      = false,
+        height     = false,
+        preview    = false,
+        ["repeat"] = false,
+        controls   = false,
+        display    = false,
+        conversion = false,
+        resolution = false,
+        cache      = false,
+        prefix     = false,
+        size       = false,
+    }
+    local used = {
+        fullname   = false,
+        format     = false,
+        name       = false,
+        path       = false,
+        suffix     = false,
+        width      = false,
+        height     = false,
+    }
+    local status = {
+        status     = 0,
+        converted  = false,
+        cached     = false,
+        fullname   = false,
+        format     = false,
+    }
+ -- setmetatable(status, { __index = used })
+ -- setmetatable(used,   { __index = request })
+    figuredata = {
+        request = request,
+        used    = used,
+        status  = status,
+    }
+    return figuredata
+end
+
+function figures.push(request)
+    local ncs = #callstack + 1
+    if ncs == 1 then
+        statistics.starttiming(figures)
     end
-    -- maybe move texsprint to tex
-    function figures.get(category,tag,default)
-        local value = figuredata[category]
-        value = value and value[tag]
-        if not value or value == "" or value == true then
-            return default or ""
-        else
-            return value
-        end
+    local figuredata = figures.new()
+    if request then
+    local iv = interfaces.variables
+    --  request.width/height are strings and are only used when no natural dimensions
+    --  can be determined; at some point the handlers might set them to numbers instead
+    --  local w, h = tonumber(request.width), tonumber(request.height)
+        request.page       = math.max(tonumber(request.page) or 1,1)
+        request.size       = img.checksize(request.size)
+        request.object     = iv[request.object] == variables.yes
+        request["repeat"]  = iv[request["repeat"]] == variables.yes
+        request.preview    = iv[request.preview] == variables.yes
+        request.cache      = request.cache ~= "" and request.cache
+        request.prefix     = request.prefix ~= "" and request.prefix
+        request.format     = request.format ~= "" and request.format
+    --  request.width      = (w and w > 0) or false
+    --  request.height     = (h and h > 0) or false
+        table.merge(figuredata.request,request)
     end
-    function figures.tprint(category,tag,default)
-        texsprint(ctxcatcodes,figures.get(category,tag,default))
+    callstack[ncs] = figuredata
+    return figuredata
+end
+
+function figures.pop()
+    local ncs = #callstack
+    figuredata = callstack[ncs]
+    callstack[ncs] = nil
+    if ncs == 1 then
+        statistics.stoptiming(figures)
     end
-    function figures.current()
-        return callstack[#callstack]
+end
+
+-- maybe move texsprint to tex
+
+function figures.get(category,tag,default)
+    local value = figuredata[category]
+    value = value and value[tag]
+    if not value or value == "" or value == true then
+        return default or ""
+    else
+        return value
     end
+end
 
+function figures.tprint(category,tag,default)
+    texsprint(ctxcatcodes,figures.get(category,tag,default))
+end
+
+function figures.current()
+    return callstack[#callstack]
 end
 
 local defaultformat = "pdf"
@@ -357,16 +360,20 @@ local function register(askedname,specification)
         local format = specification.format
         if format then
             local conversion = specification.conversion
+            local resolution = specification.resolution
             if conversion == "" then
                 conversion = nil
             end
+            if resolution == "" then
+                resolution = nil
+            end
             local newformat = conversion
             if not newformat or newformat == "" then
                 newformat = defaultformat
             end
             if trace_conversion then
-                report_graphics("checking conversion of '%s': old format '%s', new format '%s', conversion '%s'",
-                    askedname,format,newformat,conversion or "default")
+                report_graphics("checking conversion of '%s': old format '%s', new format '%s', conversion '%s', resolution '%s'",
+                    askedname,format,newformat,conversion or "default",resolution or "default")
             end
             local converter = (newformat ~= format) and converters[format]
             if converter then
@@ -387,7 +394,7 @@ local function register(askedname,specification)
                 local oldname = specification.fullname
                 local newpath = file.dirname(oldname)
                 local oldbase = file.basename(oldname)
-                local newbase = file.replacesuffix(oldbase,newformat)
+                local newbase = file.removesuffix(oldbase)
                 local fc = specification.cache or figures.cachepaths.path
                 if fc and fc ~= "" and fc ~= "." then
                     newpath = fc
@@ -402,6 +409,10 @@ local function register(askedname,specification)
                 if prefix and prefix ~= "" then
                     newbase = prefix .. newbase
                 end
+                if resolution and resolution ~= "" then -- the order might change
+                    newbase = newbase .. "_" .. resolution
+                end
+                local newbase = file.addsuffix(newbase,newformat)
                 local newname = file.join(newpath,newbase)
                 dir.makedirs(newpath)
                 oldname = file.collapse_path(oldname)
@@ -412,7 +423,7 @@ local function register(askedname,specification)
                     if trace_conversion then
                         report_graphics("converting '%s' from '%s' to '%s'",askedname,format,newformat)
                     end
-                    converter(oldname,newname)
+                    converter(oldname,newname,resolution or "")
                 else
                     if trace_conversion then
                         report_graphics("no need to convert '%s' from '%s' to '%s'",askedname,format,newformat)
@@ -451,7 +462,7 @@ local function register(askedname,specification)
         specification = { }
     end
     specification.foundname = specification.foundname or specification.fullname
-    figures.found[askedname .. "->" .. (specification.conversion or "default")] = specification
+    figures.found[askedname .. "->" .. (specification.conversion or "default") .. "->" .. (specification.resolution or "default")] = specification
     return specification
 end
 
@@ -459,7 +470,7 @@ local resolve_too = true -- urls
 
 local function locate(request) -- name, format, cache
     local askedname = resolvers.cleanpath(request.name)
-    local foundname = figures.found[askedname .. "->" .. (request.conversion or "default")]
+    local foundname = figures.found[askedname .. "->" .. (request.conversion or "default") .. "->" .. (request.resolution or "default")]
     if foundname then
         return foundname
     end
@@ -484,6 +495,7 @@ local function locate(request) -- name, format, cache
     local askedformat = (request.format ~= "" and request.format ~= "unknown" and request.format) or file.extname(askedname) or ""
     local askedcache = request.cache
     local askedconversion = request.conversion
+    local askedresolution = request.resolution
     if askedformat ~= "" then
         if trace_figures then
             commands.writestatus("figures","strategy: forced format")
@@ -504,12 +516,13 @@ local function locate(request) -- name, format, cache
             local foundname = figures.exists(askedname,format,resolve_too) -- not askedformat
             if foundname then
                 return register(askedname, {
-                    askedname = askedname,
-                    fullname = askedname,
-                    format = format,
-                    cache = askedcache,
-                    foundname = foundname,
+                    askedname  = askedname,
+                    fullname   = askedname,
+                    format     = format,
+                    cache      = askedcache,
+                    foundname  = foundname,
                     conversion = askedconversion,
+                    resolution = askedresolution,
                 })
             end
         end
@@ -517,11 +530,12 @@ local function locate(request) -- name, format, cache
             -- path and type given, todo: strip pieces of path
             if figures.exists(askedname,askedformat,resolve_too) then
                 return register(askedname, {
-                    askedname = askedname,
-                    fullname = askedname,
-                    format = askedformat,
-                    cache = askedcache,
+                    askedname  = askedname,
+                    fullname   = askedname,
+                    format     = askedformat,
+                    cache      = askedcache,
                     conversion = askedconversion,
+                    resolution = askedresolution,
                 })
             end
         else
@@ -534,11 +548,12 @@ local function locate(request) -- name, format, cache
              -- is given we don't waste much time
                 if figures.exists(check,askedformat,resolve_too) then
                     return register(check, {
-                        askedname = askedname,
-                        fullname = check,
-                        format = askedformat,
-                        cache = askedcache,
+                        askedname  = askedname,
+                        fullname   = check,
+                        format     = askedformat,
+                        cache      = askedcache,
                         conversion = askedconversion,
+                        resolution = askedresolution,
                     })
                 end
             end
@@ -546,11 +561,12 @@ local function locate(request) -- name, format, cache
                 local check = resolvers.findfile(askedname)
                 if check and check ~= "" then
                     return register(askedname, {
-                        askedname = askedname,
-                        fullname = check,
-                        format = askedformat,
-                        cache = askedcache,
+                        askedname  = askedname,
+                        fullname   = check,
+                        format     = askedformat,
+                        cache      = askedcache,
                         conversion = askedconversion,
+                        resolution = askedresolution,
                     })
                 end
             end
@@ -568,11 +584,12 @@ local function locate(request) -- name, format, cache
                 local check = file.addsuffix(askedname,suffix)
                 if figures.exists(check,format,resolve_too) then
                     return register(askedname, {
-                        askedname = askedname,
-                        fullname = check,
-                        format = format,
-                        cache = askedcache,
+                        askedname  = askedname,
+                        fullname   = check,
+                        format     = format,
+                        cache      = askedcache,
                         conversion = askedconversion,
+                        resolution = askedresolution,
                     })
                 end
             end
@@ -601,11 +618,12 @@ local function locate(request) -- name, format, cache
                             end
                         elseif figures.exists(check,format,true) then
                             return register(askedname, {
-                                askedname = askedname,
-                                fullname = check,
-                                format = format,
-                                cache = askedcache,
+                                askedname  = askedname,
+                                fullname   = check,
+                                format     = format,
+                                cache      = askedcache,
                                 conversion = askedconversion,
+                                resolution = askedresolution,
                             })
                         end
                     end
@@ -627,11 +645,12 @@ local function locate(request) -- name, format, cache
                         local check = path .. "/" .. file.replacesuffix(askedbase,suffix)
                         if figures.exists(check,format,resolve_too) then
                             return register(askedname, {
-                                askedname = askedname,
-                                fullname = check,
-                                format = format,
-                                cache = askedcache,
+                                askedname  = askedname,
+                                fullname   = check,
+                                format     = format,
+                                cache      = askedcache,
                                 conversion = askedconversion,
+                                resolution = askedresolution,
                             })
                         end
                     end
@@ -651,11 +670,12 @@ local function locate(request) -- name, format, cache
                     local check = resolvers.findfile(file.replacesuffix(askedname,suffix))
                     if check and check ~= "" then
                         return register(askedname, {
-                            askedname = askedname,
-                            fullname = check,
-                            format = format,
-                            cache = askedcache,
+                            askedname  = askedname,
+                            fullname   = check,
+                            format     = format,
+                            cache      = askedcache,
                             conversion = askedconversion,
+                            resolution = askedresolution,
                         })
                     end
                 end
@@ -764,10 +784,14 @@ function checkers.generic(data)
     local dr, du, ds = data.request, data.used, data.status
     local name, page, size, color = du.fullname or "unknown generic", du.page or dr.page, dr.size or "crop", dr.color or "natural"
     local conversion = dr.conversion
+    local resolution = dr.resolution
     if not conversion or conversion == "" then
         conversion = "unknown"
     end
-    local hash = name .. "->" .. page .. "->" .. size .. "->" .. color .. "->" .. conversion
+    if not resolution or resolution == "" then
+        resolution = "unknown"
+    end
+    local hash = name .. "->" .. page .. "->" .. size .. "->" .. color .. "->" .. conversion .. "->" .. resolution
     local figure = figures.loaded[hash]
     if figure == nil then
         figure = img.new { filename = name, page = page, pagebox = dr.size }
@@ -928,7 +952,7 @@ includers.tex = includers.nongeneric
 
 -- -- -- converters -- -- --
 
-local function makeoptions(program)
+local function makeoptions(options)
     local to = type(options)
     return (to == "table" and concat(options," ")) or (to == "string" and options) or ""
 end
@@ -947,19 +971,27 @@ local epsconverter     = { }
 converters.eps = epsconverter
 
 programs.gs = {
+    resolutions = {
+        [variables.low]    = "screen",
+        [variables.medium] = "ebook",
+        [variables.high]   = "prepress",
+    },
     options = {
         "-dAutoRotatePages=/None",
-        "-dPDFSETTINGS=/prepress",
+        "-dPDFSETTINGS=/%s",
         "-dEPSCrop",
     },
-    command = (os.type == "windows" and "gswin32") or "gs"
+    command = (os.type == "windows" and "gswin32c") or "gs"
 }
 
-function epsconverter.pdf(oldname,newname)
+function epsconverter.pdf(oldname,newname,resolution) -- the resolution interface might change
     local gs = programs.gs
     runprogram (
         '%s -q -sDEVICE=pdfwrite -dNOPAUSE -dNOCACHE -dBATCH %s -sOutputFile="%s" "%s" -c quit',
-        gs.command, makeoptions(gs.options), newname, oldname
+        gs.command,
+        format(makeoptions(gs.options),gs.resolutions[resolution or ""] or "prepress"),
+        newname,
+        oldname
     )
 end
 
diff --git a/tex/context/base/grph-inc.mkiv b/tex/context/base/grph-inc.mkiv
index 639ca7d75..b456cadf8 100644
--- a/tex/context/base/grph-inc.mkiv
+++ b/tex/context/base/grph-inc.mkiv
@@ -123,7 +123,8 @@
    \let\@@efforegroundcolor\empty
    \let\@@efcolor          \empty
    \let\@@efconversion     \empty
-   \let\@@efbackground     \empty}
+   \let\@@efbackground     \empty
+   \let\@@efresolution     \empty}
 
 \appendtoks \resetexternalfigures \to \everyoverlay
 \appendtoks \resetexternalfigures \to \everybeforepagebody % not really needed
@@ -134,6 +135,7 @@
    \let\@@eflabel     \empty
    \let\@@efsize      \empty
    \let\@@efconversion\@@exconversion
+   \let\@@efresolution\@@exresolution
    \let\@@efprefix    \@@exprefix
    \let\@@efcache     \@@excache
    \let\@@efpage      \!!zerocount
@@ -230,23 +232,24 @@
 %    \color[\@@efcolor]{\xdef\globcolorattr{\internalspotcolorname}}
 %    \endgroup
    \ctxlua{figures.push {
-        name="#3",
-        label="#2", % todo: \@eflabel
-        page="\@@efpage",
-        size="\@@efsize",
-        object="\@@efobject",
-        prefix="\@@efprefix",
-        cache="\@@efcache",
-        format="\@@efmethod",
-        preset="\@@efprefix",
-        controls="\@@efcontrols",
-        preview="\@@efpreview",
-        display="\@@efdisplay",
-        conversion="\@@efconversion",
-        color="\internalspotcolorparent\@@efcolor", % hack is needed
-        ["repeat"]="\@@efrepeat",
-        width="\@@efwidth", % can be crap
-        height="\@@efheight", % can be crap
+        name       = "#3",
+        label      = "#2", % todo: \@eflabel
+        page       = "\@@efpage",
+        size       = "\@@efsize",
+        object     = "\@@efobject",
+        prefix     = "\@@efprefix",
+        cache      = "\@@efcache",
+        format     = "\@@efmethod",
+        preset     = "\@@efprefix",
+        controls   = "\@@efcontrols",
+        preview    = "\@@efpreview",
+        display    = "\@@efdisplay",
+        conversion = "\@@efconversion",
+        resolution = "\@@efresolution",
+        color      = "\internalspotcolorparent\@@efcolor", % hack is needed
+        ["repeat"] = "\@@efrepeat",
+        width      = "\@@efwidth", % can be crap
+        height     = "\@@efheight", % can be crap
     } }%
    \ctxlua{figures.identify()}%
    \ifconditional\testexternalfigureonly
diff --git a/tex/context/base/lang-lab.mkiv b/tex/context/base/lang-lab.mkiv
index 1ef27c6c3..86311e77f 100644
--- a/tex/context/base/lang-lab.mkiv
+++ b/tex/context/base/lang-lab.mkiv
@@ -73,12 +73,12 @@
         \expandafter\noexpand\csname              #1text\endcsname}}
 
 \def\dododefinelabelclass#1#2#3#4#5#6#7#8#9%
-  {\unexpanded\def#3{#5#4}%
-              \def#4{\reallanguagetag{\defaultlanguage\currentmainlanguage}}%
-   \setuvalue{setup#1text}{\protecttextprefixes#2\def\currenttextprefixclass{#1}\dodoubleempty\dosetupsometextprefix}%
+  {\setuvalue{setup#1text}{\protecttextprefixes#2\def\currenttextprefixclass{#1}\dodoubleempty\dosetupsometextprefix}%
    \setuvalue{preset#1text}{\protecttextprefixes1\def\currenttextprefixclass{#1}\dodoubleempty\dosetupsometextprefix}%
+   \def#4{\reallanguagetag{\defaultlanguage\currentmainlanguage}}%
    \ifnum#2=\plustwo
-     \unexpanded\def#5##1##2% ##1=language
+     \def#3{#5#4}%
+     \def#5##1##2% ##1=language
        {\ifcsname\??ml:#1:##1:##2\endcsname
           \csname\??ml:#1:##1:##2\endcsname
         \else\ifcsname\??la#4\s!default\endcsname
@@ -88,13 +88,14 @@
         \else\ifcsname\??ml:#1:\s!en:##2\endcsname
           \csname\??ml:#1:\s!en:##2\endcsname
         \else
-          #2%
+          ##2%
         \fi\fi\fi\fi}%
      \let#6\gobbleoneargument
      \let#7\gobbleoneargument
      \let#8\gobbletwoarguments
      \let#9#3%
    \else
+     \unexpanded\def#3{#5#4}%
      \unexpanded\def#5##1##2%
        {\ifcsname\??ml:#1:##1:##2\endcsname
           \expandafter\let\expandafter\thetextprefix\csname\??ml:#1:##1:##2\endcsname
diff --git a/tex/context/base/lxml-tex.lua b/tex/context/base/lxml-tex.lua
index a0a34cb0d..a90844d2e 100644
--- a/tex/context/base/lxml-tex.lua
+++ b/tex/context/base/lxml-tex.lua
@@ -914,7 +914,8 @@ local function position(collected,n)
 end
 
 local function match(collected) -- is match in preceding collected, never change, see bibxml
-    texwrite((collected and collected[1].mi) or 0)
+    local m = collected and collected[1]
+    texwrite(m and m.mi or 0)
 end
 
 local function index(collected,n)
diff --git a/tex/context/base/strc-lev.lua b/tex/context/base/strc-lev.lua
index cacbc9788..ff30c3f91 100644
--- a/tex/context/base/strc-lev.lua
+++ b/tex/context/base/strc-lev.lua
@@ -10,14 +10,20 @@ local format = string.format
 local insert, remove = table.insert, table.remove
 
 local sections = structures.sections
+local default  = interfaces.variables.default
 
-local level, levels, categories = 0, { }, { }
+sections.levels = sections.levels or { }
+
+local level, levels, categories = 0, sections.levels, { }
+
+storage.register("structures/sections/levels", levels, "structures.sections.levels")
 
 function sections.defineautolevels(category,list)
     levels[category] = utilities.parsers.settings_to_array(list)
 end
 
 function sections.startautolevel(category)
+    category = category ~= "" and category or default
     level = level + 1
     local lc = levels[category]
     if not lc or level > #lc then
diff --git a/tex/context/base/strc-lev.mkiv b/tex/context/base/strc-lev.mkiv
index 4f4745e9b..4c4605bcd 100644
--- a/tex/context/base/strc-lev.mkiv
+++ b/tex/context/base/strc-lev.mkiv
@@ -24,7 +24,7 @@
 \unprotect
 
 \unexpanded\def\definestructurelevels{\dodoubleargument\dodefinestructurelevels}
-\unexpanded\def\startstructurelevel  {\dosingleargument\dostartstructurelevel}
+\unexpanded\def\startstructurelevel  {\dosingleempty\dostartstructurelevel}
 
 \unexpanded\def\dodefinestructurelevels[#1][#2]{\ctxlua{structures.sections.defineautolevels("#1","#2")}}
 \unexpanded\def\dostartstructurelevel      [#1]{\ctxlua{structures.sections.startautolevel("#1")}}
@@ -33,6 +33,11 @@
 \unexpanded\def\nostartstructurehead{\dotripleargument\nonostartstructurehead}
 \unexpanded\def\nostopstructurehead {\dosingleargument\nonostopstructurehead }
 
+\unexpanded\def\dostartstructurelevel[#1]%
+  {\doifassignmentelse{#1}
+     {\ctxlua{structures.sections.startautolevel("\v!default")}[#1]}
+     {\ctxlua{structures.sections.startautolevel("#1")}}}
+
 \unexpanded\def\nonostartstructurehead[#1][#2][#3]%
   {\blank
    \noindentation{\tttf[start missing structure level #1]}
@@ -43,6 +48,15 @@
    \noindentation{\tttf[stop missing structure level #1]}
    \blank}
 
+\definestructurelevels
+  [\v!default]
+  [\v!chapter,
+   \v!section,
+   \v!subsection,
+   \v!subsubsection,
+   \v!subsubsubsection,
+   \v!subsubsubsubsection]
+
 \protect \endinput
 
 % \starttext
diff --git a/tex/generic/context/luatex-fonts-merged.lua b/tex/generic/context/luatex-fonts-merged.lua
index 9ee8f7aee..dfba8c7c5 100644
--- a/tex/generic/context/luatex-fonts-merged.lua
+++ b/tex/generic/context/luatex-fonts-merged.lua
@@ -1,6 +1,6 @@
 -- merged file : luatex-fonts-merged.lua
 -- parent file : luatex-fonts.lua
--- merge date  : 09/29/10 10:46:09
+-- merge date  : 09/29/10 23:37:44
 
 do -- begin closure to overcome local limits and interference
 
-- 
cgit v1.2.3