On Mon, 14 Nov 2011, Hans Hagen wrote:

On 14-11-2011 09:47, Aditya Mahajan wrote:
 On Fri, 11 Nov 2011, Steffen Wolfrum wrote:

>  quite often I wish we could have a kind of in line comment:
>  Something like ¿this is my comment¡ this.
> > I assume it is not trivial due to catcode issues, to say the least.
>  But nevertheless, as it is getting close to christmas, I just wanted
>  to mention this idea here.

 As long as there is no line break in the comment, the following lua
 based solution works (inspired by m-translate)

or ...


Why not go all the way with full multiline comments then? See attached.

Aditya
\startluacode
     thirddata          = thirddata or { }
     local comments     = { filters = { } }
     thirddata.comments = comments

     local textlineactions = resolvers.openers.helpers.textlineactions
     local sequencers      = utilities.sequencers
     local constants       = interfaces.constants

     local gsub, find, format = string.gsub, string.find, string.format

     function comments.install(name,settings)
         local settings = utilities.parsers.settings_to_hash(settings)
         local start = string.escapedpattern(settings[constants.start] or "")
         local stop  = string.escapedpattern(settings[constants.stop] or "")
         local any   = ".-"
         if start ~= "" and stop ~= "" then
            local strip_inline = function(s) return gsub(s, start .. any .. stop, "", 1) end
            local strip_start  = function(s) return gsub(s, start .. any,         "", 1) end
            local strip_stop   = function(s) return gsub(s, "^"   .. any .. stop, "", 1) end 
            local insideComment = false 
            comments.filters[name] = function(s)
                local filter = comments.filters[name]
                if s == "" then
                    return s
                elseif insideComment then
                    if find(s,stop) then
                        insideComment = false
                        return filter(strip_stop(s))
                    else
                        return ""
                    end
                elseif find(s,start) then
                    if find(s, stop) then
                        return filter(strip_inline(s))
                    else
                        insideComment = true 
                        return strip_start(s)
                    end
                else
                    return s
                end
             end
             sequencers.appendaction(textlineactions,"after",format("thirddata.comments.filters.%s",name))
             comments.disable(name)
         end
     end

     function comments.enable(name)
        sequencers.enableaction(textlineactions,format("thirddata.comments.filters.%s",name))
     end

     function comments.disable(name)
        sequencers.disableaction(textlineactions,format("thirddata.comments.filters.%s",name))
     end

\stopluacode

\unprotect

\unexpanded\def\installcomments{\dodoubleargument\doinstallcomments}

\def\doinstallcomments[#1][#2]%
    {\ctxlua{thirddata.comments.install(\!!bs#1\!!es,\!!bs\detokenize{#2}\!!es)}}

\unexpanded\def\enablecomments[#1]%
   {\ctxlua{thirddata.comments.enable (\!!bs#1\!!es)}}

\unexpanded\def\disablecomments[#1]%
   {\ctxlua{thirddata.comments.disable(\!!bs#1\!!es)}}

\protect

\installcomments[test] [start=/*,stop=*/]

\starttext
\enablecomments[test]

\section{Test of /*inline*/ comments}
/* Test for multiline comments
 * to see if they work
 */

Another test for /* inline comments */ with multiple /* commented lines
that span /* false nested comments as well */ to see if it works

\stoptext
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to