How to Determine the Current File Name and Line Number in the ConTeXt Source?
Is there a convenient way within a Lua block to determine the current file name and line number of the source file being processed by ConTeXt? For example: (test.tex) --------------------------------- \def\ShowLineNumber{% \ctxlua{print('current line number:', tex.current_line_number()) print('current file:', tex.current_file_name())}} \starttext a line \ShowLineNumber another line \ShowLineNumber \stoptext --------------------------------- With the following displayed: --------------------------------- current line number: 6 current file: test.tex current line number: 9 current file: test.tex --------------------------------- I've considered hooking into the open_read_file callback and tracking the file name and line number (via reader) myself. But that seems a bit heavy handed if the file name and line number information are available more directly. And since that information is needed for error reporting, I'm thinking there's a good chance it is accessible. (Just in case you're interested, the purpose of this is to be able to assemble code segments during the ConTeXt processing into external files with #line directives ala the C preprocessor. That way if the subsequent compilation of the external file results in an error, the error message can refer back to the line of code in the ConTeXt source file. This is part of the literate programming module that Kevin Robbins has been describing in another thread.) Thanks, Tad
Tad Ashlock wrote:
Is there a convenient way within a Lua block to determine the current file name and line number of the source file being processed by ConTeXt?
For example: (test.tex) --------------------------------- \def\ShowLineNumber{% \ctxlua{print('current line number:', tex.current_line_number()) print('current file:', tex.current_file_name())}} \starttext a line \ShowLineNumber another line
\ShowLineNumber \stoptext ---------------------------------
With the following displayed: --------------------------------- current line number: 6 current file: test.tex current line number: 9 current file: test.tex ---------------------------------
I've considered hooking into the open_read_file callback and tracking the file name and line number (via reader) myself. But that seems a bit heavy handed if the file name and line number information are available more directly. And since that information is needed for error reporting, I'm thinking there's a good chance it is accessible.
(Just in case you're interested, the purpose of this is to be able to assemble code segments during the ConTeXt processing into external files with #line directives ala the C preprocessor. That way if the subsequent compilation of the external file results in an error, the error message can refer back to the line of code in the ConTeXt source file. This is part of the literate programming module that Kevin Robbins has been describing in another thread.)
i have no time now to figure out while filenames are not known but here's a (wikifyable) hack: \starttext % needs to be added to buff-ini.lua % % function buffers.raw(name) % return data[name] or { } % end \startluacode local locations = { } function document.set_number(name) locations[name] = { line = status.linenumber, file = status.filename } end function document.add_number(name) local b, l = buffers.raw(name), locations[name] if b and l then for i=1,#b do b[i] = string.gsub(b[i],"# line: <number>","# line: " .. l.line + 2) end end end \stopluacode \ctxlua{document.set_number("oeps")} \startbuffer[oeps] # line: <number> test test \stopbuffer \ctxlua{document.add_number("oeps")} \typebuffer[oeps] \stoptext when i have time (or reason) i'll think about a more integrated approach with hooks ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
participants (2)
-
Hans Hagen
-
Tad Ashlock