8

I'm wondering whether it is possible, and if so how, to save the contents of a ConTeXt buffer to file. Well, I know that the command \savebuffer[<buffername>][<filename>] exists, but it saves the file in the same location as the .tex file. I was hoping I could save the file in a subdirectory, say ./temp, to avoid cluttering up the main working directory. But that doesn't work. MWE:

\starttext

\startbuffer[code]
-- some lua code for examples sake
local x = 2
local y = math.sqrt(x)
\stopbuffer

% works
\savebuffer
  [code]
  [code.lua]

% doesn't work
\savebuffer
  [code]
  [/temp/code.lua]

\stoptext
9tTn9B
  • 830
  • 1
  • 6
  • 10
  • Have you tried without the forward slash at the beginning? Like \savebuffer[code][temp/code.lua]? – TeXnician May 28 '17 at 16:28
  • I did try that, and a couple of variations, unsuccessfully. But you mentioning it now has given me a brainwave! I'll post a self-answer as such. – 9tTn9B May 28 '17 at 16:46

1 Answers1

7

The problem is that, by default, \savebuffer attaches a prefix <jobname>- to the saved file. (So, for example, the call \savebuffer[code][code.lua] in a file foo.tex saves the buffer as foo-code.lua.) Thus, doing the naive thing

\savebuffer
  [code]
  [temp/code.lua]

is no good, as it would attempt to save the buffer as <jobname>-temp/code.lua. Instead, make use the other way of calling \savebuffer, which is:

\savebuffer
  [list=<buffer name(s)>, prefix=<'yes' or 'no'>, file=<filename>]

Then you can do

\savebuffer
  [prefix=no, file={temp/code.lua}, list=code]

successfully.

9tTn9B
  • 830
  • 1
  • 6
  • 10
  • Thanks! This made me realize that the code in t-filter module could be improved: https://github.com/adityam/filter/commit/b718a46997ccb309655837c60462d1ad8242cd98 Earlier I was moving the file after it was being written. – Aditya May 29 '17 at 16:00
  • @Aditya that's funny, the very reason I was looking into this is because I was experimenting with the filter module, and got to playing around with buffers. – 9tTn9B May 29 '17 at 17:03