On 9/3/2021 12:58 PM, Thierry Horsin via ntg-context wrote:
Hi everybody

Is there a way to have an automatic access to the current line number of a 
specific place inside a tex file ? To be more precise, here is what I would 
like to do.

I am currently trying to have some automatic external compilation by g++ of 
some portion of my tex file as in the following example below. \startC \stopC 
defines a framed part of text written between lines 538 and 549. Of course, if 
I add something before or inbetween my framed code, 538 and/or 549 will change. 
Thus I would like to get these two numbers automatically.

Thank you.
Thierry

\startC
#include <iostream>

using namespace std;
int mysum(int,int); int a; int b;
int main () { a=2; b=9;
   cout << mysum(a,b) << endl;
   return 0;
         }
int mysum(int a, int b)
{ return a+b; }
\stopC

\startluacode
myout = io.open("test.cpp", "w")
myin = io.open(tex.jobname..".tex", "r")
local n=0
for line in myin:lines() do
n=n+1
if n>539 and n<550 then
     myout:write(line.."\n")
  end
end
myout:close()
myin:close()
os.execute("g++ -o test test.cpp")
os.execute("./test > tmp")
local file=assert(io.open("tmp","r"))
local t = file:read("*all")
tex.print(t)
\stopluacode
in this case, thinking a bit different is easier ...

\starttext

\startbuffer[MyBuffer]
#include <iostream>

using namespace std;
int mysum(int,int);
int a; int b;
int main () {
    a=2; b=9;
    cout << mysum(a,b) << endl;
    return 0;
}
int mysum(int a, int b) {
    return a+b;
}
\stopbuffer

We compile and run:

\typebuffer[MyBuffer][option=C]

\startluacode
io.savedata("test.cpp",buffers.getcontent("MyBuffer"))
--os.execute("g++ -o test test.cpp")
--os.execute("./test > test.tmp")
os.execute("echo done > test.tmp")
buffers.assign("MyBuffer",io.loaddata("test.tmp"))
\stopluacode

And get:

\typebuffer[MyBuffer]

Or just:

\typefile{test.tmp}

\stoptext


-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
       tel: 038 477 53 69 | www.pragma-ade.nl | 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to