From: Hans Hagen <pragma@wxs.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: How to Determine the Current File Name and Line Number in the ConTeXt Source?
Date: Wed, 01 Jul 2009 16:26:22 +0200 [thread overview]
Message-ID: <4A4B720E.6030806@wxs.nl> (raw)
In-Reply-To: <4A4B41F6.2040005@cyberdude.com>
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
-----------------------------------------------------------------
___________________________________________________________________________________
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 : https://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
next prev parent reply other threads:[~2009-07-01 14:26 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-01 11:01 Tad Ashlock
2009-07-01 14:26 ` Hans Hagen [this message]
2009-07-03 14:22 Tad Ashlock
2009-07-03 15:23 ` Hans Hagen
2009-07-06 12:52 ` Taco Hoekwater
2009-07-07 18:09 Tad Ashlock
2009-07-08 8:27 ` Taco Hoekwater
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4A4B720E.6030806@wxs.nl \
--to=pragma@wxs.nl \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).