Commits

Wolfgang Schuster  committed 098f1fb

Add buffer method

  • Participants
  • Parent commits 9ab3369

Comments (0)

Files changed (3)

File files/annotation-14.tex

 
 \starttext
 
-\placelist[annotation]
+\placelist[annotation][headnumber=always]
 
 \blank
 

File files/t-annotation.lua

 local v_command    = variables.command
 local v_vertical   = variables.vertical
 local v_horizontal = variables.horizontal
+local v_annotation = variables.annotation
+local v_buffer     = variables.buffer
 
 local ctxcatcodes  = tex.ctxcatcodes
 local txtcatcodes  = tex.txtcatcodes
     local list        = t.list
     local number      = t.number
     local text        = t.text
+    local reference   = t.reference
     context.unprotect()
     -- alternative
     if alternative == v_paragraph then
         tex.settrue('c_annotation_title')
     end
     -- list
-    if list ~= "" or tex.conditionals['c_annotation_number'] then
+--~ if list ~= "" or tex.conditionals['c_annotation_number'] then
+--~     tex.settrue('c_annotation_caption')
+--~ end
+    if list ~= "" then
         tex.settrue('c_annotation_caption')
     end
     -- text
     if tex.conditionals['c_annotation_header'] and text == "" then
         tex.setfalse('c_annotation_header')
     end
+    -- reference
+    if reference ~= "" then
+        tex.settrue('c_annotation_reference')
+    end
     -- export
     --~ if export == v_yes then
     --~     annotation.write(title,content)
 
 -- Collect the content of the environment
 
-annotation.data = { }
+local data = { }
 
-function annotation.erasedata()
-    annotation.data = nil
+function annotation.erasedata(name)
+    data[name] = nil
 end
 
-function annotation.getdata()
-    return annotation.data or ""
+function annotation.getdata(name)
+    local data = data[name]
+    return data and data.content or ""
 end
 
-annotation.catcodes = ctxcatcodes
-
-function annotation.printdata()
-    if annotation.catcodes == txtcatcodes then
-        tex.sprint(txtcatcodes,annotation.data)
+function annotation.printdata(name)
+    local data = data[name]
+    local m_content  = data and data.content  or ""
+    local m_catcodes = data and data.catcodes or ""
+    if m_catcodes == txtcatcodes then
+        tex.sprint(txtcatcodes,m_content)
     else
-        tex.sprint(ctxcatcodes,annotation.data)
+        tex.sprint(ctxcatcodes,m_content)
     end
 end
 
-function annotation.collectdata(content,begintag,endtag,catcodes)
-    local data    = annotation.getdata()
-    local nesting = false
-    if data == "" then
-        annotation.data     = content
-        annotation.catcodes = catcodes
+function annotation.collectdata(name,content,begintag,endtag,catcodes)
+    local m_data     = annotation.getdata(name)
+    local m_catcodes = catcodes
+    local m_nesting  = false
+    if m_data == "" then
+        m_data = content
     else
-        annotation.data = data .. "\\" .. endtag .. " " .. content
+        m_data = m_data .. "\\" .. endtag .. " " .. content
     end
-    if select(2,string.gsub(annotation.data,begintag,begintag)) > select(2,string.gsub(annotation.data,endtag,endtag)) then
-        nesting = true
+    if select(2,string.gsub(m_data,begintag,begintag)) > select(2,string.gsub(m_data,endtag,endtag)) then
+        m_nesting = true
     else
-        nesting = false
+        m_nesting = false
     end
-    if not nesting then annotation.data = annotation.data:strip() end
-    commands.testcase(nesting)
+    if not m_nesting then
+        m_data = m_data:strip()
+        context.setdataset( { v_annotation }, { content = m_data, catcodes = m_catcodes } )
+    end
+    data[name] = { content = m_data, catcodes = m_catcodes }
+    commands.testcase(m_nesting)
 end
 
-function annotation.savedata(content,catcodes)
-    annotation.data     = content
-    annotation.catcodes = catcodes
+function annotation.savedata(name,content,catcodes)
+    data[name] = { content = content, catcodes = catcodes }
 end
 
 -- Write the content of the environment to a file

File files/t-annotation.mkvi

 %D \module
 %D   [     file=t-annotation,
-%D      version=2012.04.03,
+%D      version=2012.04.07,
 %D        title=\CONTEXT\ User Module,
 %D     subtitle=Annotations,
 %D       author=Wolfgang Schuster,
 %M
 %M \setupbodyfont[calluna]
 %M
-%M \definedescription[description][location=margin]
+%M \definedescription[description][alternative=margin]
 
 %D \subject{\textbackslash defineannotation}
 %D
 \setnewconstant \c_annotation_title     \plusone
 \setnewconstant \c_annotation_paragraph \plusone
 \setnewconstant \c_annotation_caption   \plusone
+\setnewconstant \c_annotation_reference \plusone
+\setnewconstant \c_annotation_method    \plusone
+
+\newcount\c_annotation_n
+
+\definedataset[\v!annotation]
+
+\installnamespace {annotation}
+\installnamespace {annotationalternative}
+\installnamespace {annotationmethod}
+
+\installcommandhandler \????annotation {annotation} \????annotation
 
 \ctxloadluafile{t-annotation}
 
-\installnamespace {annotation}
-\installnamespace {annotationalternative}
-
-\installcommandhandler \????annotation {annotation} \????annotation
+% new feature, save the text and flush it later
 
 \appendtoks
-  \letannotationparameter\s!counter\currentannotation
-\to \everypresetannotation
+  \pushmacro\setupannotation
+  \definesorting[\currentannotation][\currentannotation][\v!none]%
+  \popmacro \setupannotation
+\to \everydefineannotation
+
+% commands are created after the definition of the sorting because
+% I use \definesort and don’t need a command to save the text
 
 \appendtoks
   \setuevalue        {\currentannotation}{\annotation_cmd[\currentannotation]}%
   \setuevalue{\e!start\currentannotation}{\annotation_beg[\currentannotation]}%
   \setuevalue{\e!stop \currentannotation}{\annotation_end[\currentannotation]}%
+  \setuevalue{\e!get  \currentannotation}{\annotation_get[\currentannotation]}%
 \to \everydefineannotation
 
 % each new environment creates a list and a counter with the same,
 % case it has to be set with the “counter” key
 
 \appendtoks
+  \letannotationparameter\s!counter\currentannotation
+\to \everypresetannotation
+
+\appendtoks
   \edef\p_annotation_counter{\annotationparameter\s!counter}%
   \ifx \p_annotation_counter \currentannotation
     \definelist   [\currentannotation]%
 % to do this a single line is necessary which takes care of this
 
 \appendtoks
- %\dostructurecountersetup\currentannotation\annotationparameter
   \strc_counter_setup_using_parameter\currentannotation\annotationparameter
 \to \everysetupannotation
 
+% Code for the annotation environment and command
+
+\let\m_annotation_name      \empty
+\let\m_annotation_parameters\empty
+
 \unexpanded\def\annotation_beg[#environment]%
   {\begingroup
    \edef\currentannotation{#environment}%
-   \dosingleempty\annotation_beg_parameters}
+   \dodoubleempty\annotation_beg_parameters}
 
-\def\annotation_beg_parameters[#parameters]%
-  {\dosinglegroupempty{\annotation_beg_title[#parameters]}}
+\def\annotation_beg_parameters[#name][#parameters]%
+  {\ifsecondargument
+     \edef\m_annotation_name      {#name}%
+     \edef\m_annotation_parameters{#parameters}%
+   \else
+     \doifassignmentelse{#name}
+       {\edef\m_annotation_parameters{#name}}
+       {\edef\m_annotation_name      {#name}}%
+   \fi
+   \dosinglegroupempty\annotation_beg_title}
 
-\def\annotation_beg_title[#parameters]#title%
-  {\ctxlua{thirddata.annotation.erasedata()}%
-   \annotation_parameters[\c!reference=,\c!list=,\c!title={#title},#parameters]%
+\def\annotation_beg_title#title%
+  {\ctxlua{thirddata.annotation.erasedata("\m_annotation_name")}%
+   \edef\m_annotation_parameters{\c!reference=,\c!list=,\c!title={#title},\m_annotation_parameters}%
    \expandafter\def\expandafter\annotation_collect\expandafter##\expandafter1\csname\e!stop\currentannotation\endcsname
-     {\ctxlua{thirddata.annotation.collectdata(\!!bs\detokenize{##1}\!!es,"\e!start\currentannotation","\e!stop\currentannotation",\number\catcodetable)}%
+     {\ctxlua{thirddata.annotation.collectdata("\m_annotation_name",\!!bs\detokenize{##1}\!!es,"\e!start\currentannotation","\e!stop\currentannotation",\number\catcodetable)}%
       \annotation_collect
-      \annotation_process}%
+      \annotation_method}%
    \annotation_collect}
 
 \unexpanded\def\annotation_end[#environment]%
 \unexpanded\def\annotation_cmd[#environment]%
   {\begingroup
    \edef\currentannotation{#environment}%
-   \dosingleempty\annotation_cmd_parameters}
+   \dodoubleempty\annotation_cmd_parameters}
 
-\def\annotation_cmd_parameters[#parameters]%
-  {\dodoublegroupempty{\annotation_cmd_title[#parameters]}}
+\def\annotation_cmd_parameters[#name][#parameters]%
+  {\ifsecondargument
+     \edef\m_annotation_name      {#name}%
+     \edef\m_annotation_parameters{#parameters}%
+   \else
+     \doifassignmentelse{#name}
+       {\edef\m_annotation_parameters{#name}}
+       {\edef\m_annotation_name      {#name}}%
+   \fi
+   \dodoublegroupempty\annotation_cmd_title}
 
-\def\annotation_cmd_title[#parameters]#first#second%
+\def\annotation_cmd_title#first#second%
   {\ifsecondargument
-     \def\annotation_next{\annotation_cmd_content[#parameters]{#first}{#second}}%
+     \def\annotation_next{\annotation_cmd_content{#first}{#second}}%
    \else
-     \def\annotation_next{\annotation_cmd_content[#parameters]{}{#first}}%
+     \def\annotation_next{\annotation_cmd_content{}{#first}}%
    \fi\annotation_next}
 
-\def\annotation_cmd_content[#parameters]#title#content%
-  {\ctxlua{thirddata.annotation.erasedata()}%
-   \annotation_parameters[\c!reference=,\c!list=,\c!title={#title},#parameters]%
-   \ctxlua{thirddata.annotation.savedata(\!!bs\detokenize{#content}\!!es,\number\catcodetable)}%
-   \annotation_process}
+\def\annotation_cmd_content#title#content%
+  {\ctxlua{thirddata.annotation.erasedata("\m_annotation_name")}%
+   \normalexpanded{\annotation_parameters[\c!reference=,\c!list=,\c!title={#title},\m_annotation_parameters]}%
+   \ctxlua{thirddata.annotation.savedata("\m_annotation_name", \!!bs\detokenize{#content}\!!es,\number\catcodetable)}%
+   \annotation_method}
 
-\def\annotation_process
-  {\expandcheckedcsname\????annotationalternative{\annotationparameter\c!alternative}\v!none
+\unexpanded\def\annotation_get[#environment]%
+  {\begingroup
+   \edef\currentannotation{#environment}%
+   \dodoubleempty\annotation_get_content}
+
+\def\annotation_get_content[#name][#parameters]%
+  {\ifsecondargument
+     \edef\m_annotation_name      {#name}%
+     \edef\m_annotation_parameters{#parameters}%
+   \else
+     \doifassignmentelse{#name}
+       {\edef\m_annotation_parameters{#name}}
+       {\edef\m_annotation_name      {#name}}%
+   \fi
+   \annotation_parameters[\m_annotation_parameters]%
+   \expandcheckedcsname\????annotationalternative{\annotationparameter\c!alternative}\v!none
+   \endgroup}
+
+
+% methods
+
+\def\annotation_method
+  {\expandcheckedcsname\????annotationmethod{\annotationparameter\c!method}\s!default}
+
+\setvalue{\????annotationmethod\s!default}%
+  {\normalexpanded{\annotation_parameters[\m_annotation_parameters]}%
+   \expandcheckedcsname\????annotationalternative{\annotationparameter\c!alternative}\v!none
    \annotation_export
    \ifconditional\c_annotation_display
      \checknextindentation[\annotationparameter\c!indentnext]%
    \fi
    \normalexpanded{\endgroup\annotation_aftergroup}}
 
+\setvalue{\????annotationmethod\v!buffer}%
+  {\endgroup}
+
+% processing
+
 \unexpanded\def\annotation_parameters[#parameters]%
-  {\doifelse{\annotationparameter\c!title}\v!yes
+  {\doifelse{\annotationparameter\c!header}\v!yes
      {\c_annotation_header\zerocount}
      {\c_annotation_header\plusone  }%
-   \setupannotation[\currentannotation][#parameters]%
+   \setupcurrentannotation[#parameters]%
    \ctxlua{thirddata.annotation.save{
      alternative = "\annotationparameter\c!alternative",
      display     = "\annotationparameter\c!display",
      list        = "\annotationparameter\c!list",
      number      = "\annotationparameter\c!number",
      text        = "\annotationparameter\c!text",
+     reference   = "\annotationparameter\c!reference",
      mode        = "\ifvmode\v!vertical\else\v!horizontal\fi",
    }}%
    \annotation_structure}
 
-\installstructurelistprocessor{annotation}{\usestructurelistprocessor{number+title}}
-
 \unexpanded\def\annotation_structure
   {\begingroup
-   \incrementcounter[\annotationparameter\s!counter]%
-  %\dostructurecountercomponent
-   \strc_counters_register_component
-     {annotation}%
-     \setupcurrentannotation
-     \annotationparameter
-     \detokenizedannotationparameter
-     \relax
-     \relax
-     \relax
-     [      \c!name=\currentannotation,%
-      \s!hascaption=\ifconditional\c_annotation_caption\v!yes\else\v!no\fi,%
-       \s!hasnumber=\ifconditional\c_annotation_number \v!yes\else\v!no\fi,%
-        \s!hastitle=\ifconditional\c_annotation_title  \v!yes\else\v!no\fi,%
-        \c!bookmark=]%
-     []%
-  %\laststructurecountersynchronize
-  %\iflocation\ifx\laststructurecounterattribute\relax\else\ifx\laststructurecounterattribute\empty\else
-  %  \attribute\destinationattribute\laststructurecounterattribute\emptyhbox
-  %\fi\fi\fi
-   \m_strc_counters_last_registered_synchronize
-   \iflocation\ifx\m_strc_counters_last_registered_attribute\relax\else\ifx\m_strc_counters_last_registered_attribute\empty\else
-     \attribute\destinationattribute\m_strc_counters_last_registered_attribute\emptyhbox
-   \fi\fi\fi
+   \ifconditional\c_annotation_number
+     \incrementcounter[\annotationparameter\s!counter]%
+   \fi
+   \ifconditional\c_annotation_caption
+     \ifconditional\c_annotation_number
+       \normalexpanded{\writetolist[\currentannotation]{\rawcountervalue[\annotationparameter\s!counter]}{\annotationparameter\c!list}}%
+     \else
+       \normalexpanded{\writetolist[\currentannotation]{}{\annotationparameter\c!list}}%
+     \fi
+   \fi
+   \ifconditional\c_annotation_reference
+     \ifconditional\c_annotation_number
+       \normalexpanded{\textreference[\annotationparameter\c!reference]{\rawcountervalue[\annotationparameter\s!counter]}}%
+     \else
+       \normalexpanded{\textreference[\annotationparameter\c!reference]{{\tttf ??}}}%
+     \fi
+   \fi
    \endgroup}
 
 \unexpanded\def\annotation_export
      \useannotationstyleandcolor\c!style\c!color
      \useannotationstyleandcolor\c!textstyle\c!textcolor
      \ifconditional\c_annotation_paragraph\setupinterlinespace\fi
-     \ctxlua{thirddata.annotation.printdata()}%
+     \ctxlua{thirddata.annotation.printdata("\m_annotation_name")}%
      \ifconditional\c_annotation_paragraph\par\fi
    \endgroup}
 
    \annotationparameter\c!stopper}
 
 \def\annotation_content_raw
-  {\ctxlua{thirddata.annotation.printdata()}}
+  {\ctxlua{thirddata.annotation.printdata("\m_annotation_name")}}
 
 % With the command alternative one can access the formatted content and title
 % with the two arguments but it’s also possible to use the raw content of
 \def\placeannotationtext   {\annotationparameter\c!text}
 \def\placeannotationnumber {\convertedcounter[\annotationparameter\s!counter]}
 \def\placeannotationtitle  {\annotationparameter\c!title}
-\def\placeannotationcontent{\ctxlua{thirddata.annotation.printdata()}}
+\def\placeannotationcontent{\ctxlua{thirddata.annotation.printdata("\m_annotation_name")}}
 
 % All layouts for the annotation commands are accessible with the alternative key,
 % the relation between the key values and the internal layout is done with the
    \dostoptagged
    \doifsomethingelse{\annotationparameter\c!spaceafter}{\blank[\annotationparameter\c!spaceafter]}{\endgraf}}
 
+\def\annotation_placement_narrow
+  {\doifsomethingelse{\annotationparameter\c!spacebefore}{\blank[\annotationparameter\c!spacebefore]}{\endgraf}%
+   \dostarttagged\t!division\currentannotation
+   \annotationparameter\c!before
+   \doifelse{\annotationparameter\c!doublesided}\v!yes
+     {\doifoddpageelse
+        {\doadaptleftskip {\annotationparameter\c!leftmargin }%
+         \doadaptrightskip{\annotationparameter\c!rightmargin}}%
+        {\doadaptleftskip {\annotationparameter\c!rightmargin}%
+         \doadaptrightskip{\annotationparameter\c!leftmargin }}}%
+        {\doadaptleftskip {\annotationparameter\c!leftmargin }%
+         \doadaptrightskip{\annotationparameter\c!rightmargin}}%
+   \annotation_content_formatted
+   \annotationparameter\c!after
+   \dostoptagged
+   \doifsomethingelse{\annotationparameter\c!spaceafter}{\blank[\annotationparameter\c!spaceafter]}{\endgraf}}
+
 \def\annotation_placement_comment
   {\begingroup\simplifycommands
    \doifsomething{\annotationparameter\c!color}{\setupcomment[\c!color=\annotationparameter\c!color]}%
 
 \setvalue{\????annotationalternative\v!text     }{\annotation_placement_text     }
 \setvalue{\????annotationalternative\v!paragraph}{\annotation_placement_paragraph}
+\setvalue{\????annotationalternative\v!narrow   }{\annotation_placement_narrow   }
 \setvalue{\????annotationalternative\v!comment  }{\annotation_placement_comment  }
 \setvalue{\????annotationalternative\v!tooltip  }{\annotation_placement_tooltip  }
 
      \c!paragraph=\v!no,
        \c!command=,
         \c!number=\v!no,
-         \c!title=\v!yes,
+       \c!header=\v!yes,
         \c!export=\v!no,
      \c!indenting=\v!never,
     \c!indentnext=\v!yes,