adityam / filter

ConTeXt module to process contents of a start-stop environment through an external program
45 stars 10 forks source link

how to pipe output of filter to vim syntax highlighter? #18

Closed jmarca closed 9 years ago

jmarca commented 9 years ago

This is a question, not a bug or issue.

I want to process R, and then have the commands piped to the vimtyping filter.

So, how to I "wrap" the output from the filter module in another set of \startR \stopR commands that will trigger the vimtyping filter module?

Here is what I tried, without success:

First, the base case (largely based on your test case for R)

\definevimtyping [R][syntax=r]

\defineexternalfilter
[Rcode]
[filtercommand={R CMD BATCH -q --save --restore \externalfilterinputfile\space \externalfilteroutputfile},
  output=\externalfilterbasefile.out,
readcommand=\typefile,
directory=output,
continue=yes]

...(from your r test case)...

First a test of whether the workspace is persistent:
bla
\startRcode
a <- "bla"
b <- "blabla"
ls()
\stopRcode

One R run ends, another begins.

\startR
ls()
\stopR

The R code is executed and formatted as plain text. The "ls()" command is not executed, but is syntax colored. What I want to do is to put \startR and \stopR in the filter as follows:

\defineexternalfilter
[Rcode]
[filtercommand={R CMD BATCH -q --save --restore \externalfilterinputfile\space \externalfilteroutputfile},
  output=\externalfilterbasefile.out,
readcommand=\typefile,
directory=output,
before=\startR,
after=\stopR,
continue=yes]

However, that dies, with context hanging up on the stopR of the next bit of code:


tex error       > error on line 14 in file output/testfile.md.tex: ! Emergency stop

<to be read again> 
\else 
\externalfilterparameter ...rentexternalfilter :#1
                                                  \endcsname \????externalfi...
\directsetup ...me \??setup :#1\endcsname #1\else 
                                                  \letterpercent \fi \fi \en...
<argument> ...!setups ]\directsetup \externalfilterparameter 
                                                  \c!readcommand \externalfi...
\ctxcommand #1p-\directlua {commands.#1
                                       }
\buff_gobble ...ne =\plusone true\else false\fi )}
                                                  \buff_gobble \buff_finish 
l.14 \stopR

 4     \startRcode
 5     a <- "bla"
 6     b <- "blabla"
 7     ls()
 8     \stopRcode
 9     
10     One R run ends, another begins.
11     
12     \startR
13     ls()
14 >>  \stopR
15     
16     Now follows a hidden R run which cleans the R workspace
17     
18     \startRhidden
19     rm(list=ls())
20     save.image()
21     \stopRhidden
22     
23     What is in the workspace now?
24     

I don't really understand tex/context macros, but it just seems like there should be some way to "protect" the macro so that it is not executed, but rather is written out as a command first and then interpreted in a second pass?

Any help would be appreciated, or redirection to a more appropriate channel.

adityam commented 9 years ago

Sorry, I did not see this before.

Define a vimtyping environment, say Rtyping and then set readcommand=\typeRtypingfile.

jmarca commented 9 years ago

Ah cool. I will try that today.

adityam commented 9 years ago

@jmarca Could you confirm whether my suggestion works? If so, I will like to close this issue.

jmarca commented 9 years ago

I'll check. I honestly don't remember how I solved it, but I will dig up the report and check.

jmarca commented 9 years ago

Yes it works. Details:

My test.tex file reads:

\usetypescript[termes]
\setupbodyfont[termes,14pt]

\usecolors[xwi]

\defineframedtext
  [framedcode]
  [strut=yes,
    offset=2mm,
    leftframe=on,
    foreground=color,
    foregroundcolor=blue,
    width=\textwidth,
    align=right,
    background=color,
    backgroundcolor=yellow,
    frame=off]

\usemodule[vim]

\definevimtyping [R][syntax=r,
  before={\startframedcode},
  after={\stopframedcode},
  margin=0.5cm,
  directory=output/]

\definevimtyping [RBLOCKOUTPUT][syntax=r,
  before={\startframedcode[backgroundcolor=beige]},
  after={\stopframedcode},
  margin=0.5cm,
  directory=output/]

\defineexternalfilter
  [markdown]
    [filtercommand={ \space
        echo 'SECFILTER s/^\externalfilterparameter{markdownbasenest}/\externalfilterparameter{markdownsectionnest}\externalfilterparameter{markdownbasenest}/g'; \space
        sed 's/^\externalfilterparameter{markdownbasenest}/\externalfilterparameter{markdownsectionnest}\externalfilterparameter{markdownbasenest}/g' \space
                  \externalfilterinputfile\space > \externalfilterinputfile.tmp.md; \space
        pandoc -t context --filter=filters/context-float-refs.js \space
               \externalfilterinputfile.tmp.md -o \externalfilteroutputfile; \space
        rm tmp.md
      },
      directory=output]

\defineexternalfilter
  [Rcode]
  [
    filtercommand={R CMD BATCH -q --restore --slave  --save --no-timing \externalfilterinputfile\space \externalfilteroutputfile},
    output=\externalfilterbasefile.out,
    readcommand=\typefile,
    cache=yes,
    directory=output,
    continue=yes]

%% this one defaults to readfile, not typefile
\defineexternalfilter
  [Recho]
  [
    filtercommand={Rscript --restore \externalfilterinputfile\space |\space pandoc -t context\space --filter=filters/context-float-refs.js\space -o \externalfilteroutputfile},
    %%output=\externalfilterbasefile.out,
    %%cache=yes,
    directory=output,
    continue=yes]

%% this one hides output
\defineexternalfilter
  [Rhidden]
  [
    filtercommand={R CMD BATCH -q --save --restore --no-timing
      \externalfilterinputfile\space \externalfilteroutputfile},
    output=\externalfilterbasefile.out,
    read=no,
    cache=yes,
    directory=output,
    continue=yes]

\defineexternalfilter
  [RcodeFormatted]
  [
    filtercommand={R CMD BATCH -q --restore --slave  --save --no-timing \externalfilterinputfile\space \externalfilteroutputfile},
    output=\externalfilterbasefile.out,
    readcommand=\typeRBLOCKOUTPUTfile,
    cache=yes,
    directory=output,
    continue=yes]

\starttext

\startbodymatter

\processmarkdownfile{simpleR.md}

\stopbodymatter

\stoptext

Then in my markdown file, I have:

# testing

This is a test.  First execute R hidden from output.

\startRhidden
a <- 2
b <- 3
\stopRhidden

Execute R code, but it is not piped through vim module:

\startRcode
print(paste(a,'and',b))
result <- print(paste(a,'and',b))
cat (result)
\stopRcode

Can format but not execute R using the vimtyping `R` environment.
\startR
print(paste(a,'and',b))
result <- print(paste(a,'and',b))
cat (result)
\stopR

And can get executed R plus inline echo: \inlineRecho{cat(paste(a,'and',b))} using
`Recho` environment.

And finally, can execute R and format the output through the vim
typing module by using `RCodeFormatted`.  Note the beige background.

\startRcodeFormatted
print(paste(a,'and',b))
result <- print(paste(a,'and',b))
cat(result)
\stopRcodeFormatted

The end

Output looks like:

pdfscreenshot