Wolfgang Schuster wrote:
Am 08.01.2010 um 17:27 schrieb Tad Ashlock:Hi All,
======================== \startluacode function move_end_punctuation (text, punc, cmd_start, cmd_mid, cmd_end) context(cmd_start .. text .. cmd_mid) if string.find('.,!?', punc, 1, true) then context(punc .. cmd_end) else context(cmd_end .. ' ' .. punc) end end \stopluacode
\def\Var#1#2{\ctxlua{move_end_punctuation([=#1]==],[==[#2]==], '\\quote{\\type{','}','}')}}
\starttext
This is \Var{var_name}, a variable.
\stoptext ========================
This works, until the \Var{} macro appears in the argument of another macro. For example, make the text:
\framed{This is \Var{var_name}, a variable.}
\nonknuthmode % make '_' a normal character in text mode
\define[1]\Var {\doifnextcharelse{,}{\doVar{#1,}\gobbleoneargument}{\noVar{#1}}}
\define[1]\doVar {\mono{#1}}
\define[1]\noVar {\mono{#1}% \doifnextcharelse{;} % \autoinsertnextspace is no longer available in MkIV :( {\donothing} {\doifnextcharelse{.} {\donothing} {\space}}}
\starttext
This is \Var{var_name}, a variable.
This is \Var{var_name,} a variable.
\framed{This is \Var{var_name}, a variable.}
\stoptext
Wolfgang
Thanks for responding Wolfgang. But this solution has some problems. The first problem is that it only works for commas. I'm sure I can figure out how to next the \doifnextcharelse commands to add periods, question marks, and exclamation points, but then the command becomes rather unwieldy. Not really a problem for a single command, but this leads to... Problem number 2 is that I'm going to need many of these commands for different types of things. I'd like to abstract out the "punctuation swapping" so that it doesn't have to be repeated for each command. But the show-stopper is problem number 3: this solution still breaks in other places, like captions: ======================== \starttext \placefigure [left] {This is \Var{var_name}, a variable.} {\framed{And \Var{another_var_name} is yet another variable.}} \stoptext ======================== results in: ========================
! Argument of \gobbleoneargument has an extra }. <inserted text> \par <to be read again> } \doifnextcharelse ...token =#1\def \!!stringa {#2} \def \!!stringb {#3}\futur... \@@kjfiguretitle ->This is \Var {var_name} , a variable. \doif #1#2->\edef \!!stringa {#1 }\edef \!!stringb {#2}\ifx \!!stringa \!!str... \dostructurecountercomponent ...\c!title }\v!none {\setfalse \hasstructureco... ... l.47 ...other_var_name} is yet another variable.}}
? ========================
This is the sort of problem I was flailing around with. I'd get a definition that worked in one case, but broke in another. One attempt broke when I included a table of contents. Thanks again! Tad