From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/51556 Path: news.gmane.org!not-for-mail From: Hans Hagen Newsgroups: gmane.comp.tex.context 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 Message-ID: <4A4B720E.6030806@wxs.nl> References: <4A4B41F6.2040005@cyberdude.com> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; Format="flowed" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1246458501 18952 80.91.229.12 (1 Jul 2009 14:28:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 1 Jul 2009 14:28:21 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Wed Jul 01 16:28:14 2009 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from ronja.vet.uu.nl ([131.211.172.88] helo=ronja.ntg.nl) by lo.gmane.org with esmtp (Exim 4.50) id 1MM0mJ-0000fY-RL for gctc-ntg-context-518@m.gmane.org; Wed, 01 Jul 2009 16:27:16 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 499851FB82; Wed, 1 Jul 2009 16:27:12 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 18488-01-3; Wed, 1 Jul 2009 16:26:35 +0200 (CEST) Original-Received: from ronja.vet.uu.nl (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 6366B1FB60; Wed, 1 Jul 2009 16:26:35 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id D216C1FB60 for ; Wed, 1 Jul 2009 16:26:33 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 18951-01-5 for ; Wed, 1 Jul 2009 16:26:25 +0200 (CEST) Original-Received: from filter4-nij.mf.surf.net (filter4-nij.mf.surf.net [195.169.124.155]) by ronja.ntg.nl (Postfix) with ESMTP id 0D3231FB5A for ; Wed, 1 Jul 2009 16:26:25 +0200 (CEST) Original-Received: from mail.pragma-ade.net (dsl-083-247-100-017.solcon.nl [83.247.100.17]) by filter4-nij.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id n61EQN4E015545 for ; Wed, 1 Jul 2009 16:26:24 +0200 Original-Received: from [10.100.1.100] (unverified [10.100.1.100]) by controller-1 (SurgeMail 3.9e) with ESMTP id 22814-1840426 for ; Wed, 01 Jul 2009 16:26:23 +0200 User-Agent: Thunderbird 2.0.0.22 (Windows/20090605) In-Reply-To: <4A4B41F6.2040005@cyberdude.com> X-Originating-IP: 10.100.1.100 X-Authenticated-User: hagen@controller-1 X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) X-CanIt-Geo: ip=83.247.100.17; country=NL; region=15; city=Almelo; latitude=52.3500; longitude=6.6667; http://maps.google.com/maps?q=52.3500,6.6667&z=6 X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 253682183 - f76474f5c1f6 - 20090701 X-Scanned-By: CanIt (www . roaringpenguin . com) on 195.169.124.155 X-Virus-Scanned: amavisd-new at ntg.nl X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.11 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: ntg-context-bounces@ntg.nl Errors-To: ntg-context-bounces@ntg.nl X-Virus-Scanned: amavisd-new at ntg.nl Xref: news.gmane.org gmane.comp.tex.context:51556 Archived-At: 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: ","# line: " .. l.line + 2) end end end \stopluacode \ctxlua{document.set_number("oeps")} \startbuffer[oeps] # line: 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 ___________________________________________________________________________________