ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* getting line numbers
@ 2021-09-03 10:58 Thierry Horsin via ntg-context
  2021-09-03 12:16 ` Hans Hagen via ntg-context
  2021-09-03 13:53 ` Aditya Mahajan via ntg-context
  0 siblings, 2 replies; 9+ messages in thread
From: Thierry Horsin via ntg-context @ 2021-09-03 10:58 UTC (permalink / raw)
  To: ntg-context; +Cc: Thierry Horsin

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





___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
  2021-09-03 10:58 getting line numbers Thierry Horsin via ntg-context
@ 2021-09-03 12:16 ` Hans Hagen via ntg-context
  2021-09-03 12:20   ` Thierry Horsin via ntg-context
  2021-09-03 13:53 ` Aditya Mahajan via ntg-context
  1 sibling, 1 reply; 9+ messages in thread
From: Hans Hagen via ntg-context @ 2021-09-03 12:16 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Hans Hagen, Thierry Horsin

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
  2021-09-03 12:16 ` Hans Hagen via ntg-context
@ 2021-09-03 12:20   ` Thierry Horsin via ntg-context
  0 siblings, 0 replies; 9+ messages in thread
From: Thierry Horsin via ntg-context @ 2021-09-03 12:20 UTC (permalink / raw)
  To: Hans Hagen; +Cc: Thierry Horsin, mailing list for ConTeXt users

Many thanks Hans.
Thierry

Le Friday 03 Sep 2021 à 14:16:40 (+0200), Hans Hagen a écrit :


> 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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
  2021-09-03 10:58 getting line numbers Thierry Horsin via ntg-context
  2021-09-03 12:16 ` Hans Hagen via ntg-context
@ 2021-09-03 13:53 ` Aditya Mahajan via ntg-context
  2021-09-03 16:26   ` Hans Hagen via ntg-context
  1 sibling, 1 reply; 9+ messages in thread
From: Aditya Mahajan via ntg-context @ 2021-09-03 13:53 UTC (permalink / raw)
  To: Thierry Horsin via ntg-context; +Cc: Aditya Mahajan

On Fri, 3 Sep 2021, 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.

Also look at the filter module. 

    https://github.com/adityam/filter

Behind the scenes, it works by saving the content in an external file and then
running it through any external program. It also allows for caching the
result, and has many options for customizing the behavior.

Aditya
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
  2021-09-03 13:53 ` Aditya Mahajan via ntg-context
@ 2021-09-03 16:26   ` Hans Hagen via ntg-context
  2021-09-05 10:26     ` Fabrice Couvreur via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen via ntg-context @ 2021-09-03 16:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Hans Hagen

On 9/3/2021 3:53 PM, Aditya Mahajan via ntg-context wrote:
> On Fri, 3 Sep 2021, 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.
> 
> Also look at the filter module.
> 
>      https://github.com/adityam/filter
> 
> Behind the scenes, it works by saving the content in an external file and then
> running it through any external program. It also allows for caching the
> result, and has many options for customizing the behavior.
you forgot to mention that it also makes sure that you only process when 
there has been a change (after all calling gcc comes at a price)

Hans

-----------------------------------------------------------------
                                           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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
  2021-09-03 16:26   ` Hans Hagen via ntg-context
@ 2021-09-05 10:26     ` Fabrice Couvreur via ntg-context
  0 siblings, 0 replies; 9+ messages in thread
From: Fabrice Couvreur via ntg-context @ 2021-09-05 10:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Fabrice Couvreur


[-- Attachment #1.1: Type: text/plain, Size: 1991 bytes --]

Hi,
I am trying to test this file but without success.
I just do context test.tex but I suspect that is not it.
When you say

> We compile and run:
>

Which command is it with ?
Thanks
Fabrice

Le ven. 3 sept. 2021 à 18:26, Hans Hagen via ntg-context <ntg-context@ntg.nl>
a écrit :

> On 9/3/2021 3:53 PM, Aditya Mahajan via ntg-context wrote:
> > On Fri, 3 Sep 2021, 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.
> >
> > Also look at the filter module.
> >
> >      https://github.com/adityam/filter
> >
> > Behind the scenes, it works by saving the content in an external file
> and then
> > running it through any external program. It also allows for caching the
> > result, and has many options for customizing the behavior.
> you forgot to mention that it also makes sure that you only process when
> there has been a change (after all calling gcc comes at a price)
>
> Hans
>
> -----------------------------------------------------------------
>                                            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
>
> ___________________________________________________________________________________
>

[-- Attachment #1.2: Type: text/html, Size: 4724 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
  2021-09-04  8:35   ` Hans Hagen via ntg-context
@ 2021-09-04 10:18     ` Thierry Horsin via ntg-context
  0 siblings, 0 replies; 9+ messages in thread
From: Thierry Horsin via ntg-context @ 2021-09-04 10:18 UTC (permalink / raw)
  To: Hans Hagen; +Cc: Thierry Horsin, mailing list for ConTeXt users

Thank you Hans.
For what I had in mind (compiling some c++ inside a tex file) it seems to work. Anyway for this I am currently using the buffer and typebuffer solution you provide.
T.

Le Saturday 04 Sep 2021 à 10:35:34 (+0200), Hans Hagen a écrit :

> there has always been
> 
> \the\inputlineno
> 
> but it is not always accurate (because content can come from anyplace, e.g.
> lua)
> 
> 
> -----------------------------------------------------------------
>                                           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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
  2021-09-04  7:01 ` thierry horsin via ntg-context
@ 2021-09-04  8:35   ` Hans Hagen via ntg-context
  2021-09-04 10:18     ` Thierry Horsin via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: Hans Hagen via ntg-context @ 2021-09-04  8:35 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Hans Hagen, thierry horsin

On 9/4/2021 9:01 AM, thierry horsin via ntg-context wrote:
> Hi and thank you Aditaya and again Hans.
> 
> By the way, just for fun , is there a(n easy) way to get the linenumber 
> in the tex file ?
there has always been

\the\inputlineno

but it is not always accurate (because content can come from anyplace, 
e.g. lua)


-----------------------------------------------------------------
                                           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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getting line numbers
       [not found] <mailman.418.1630685069.1197.ntg-context@ntg.nl>
@ 2021-09-04  7:01 ` thierry horsin via ntg-context
  2021-09-04  8:35   ` Hans Hagen via ntg-context
  0 siblings, 1 reply; 9+ messages in thread
From: thierry horsin via ntg-context @ 2021-09-04  7:01 UTC (permalink / raw)
  To: ntg-context; +Cc: thierry horsin

Hi and thank you Aditaya and again Hans.

By the way, just for fun , is there a(n easy) way to get the linenumber 
in the tex file ?

Thanks


On 03/09/2021 18:04, ntg-context-request@ntg.nl wrote:
>
> ------------------------------
>
> Message: 4
> Date: Fri, 3 Sep 2021 14:16:40 +0200
> From: Hans Hagen <j.hagen@xs4all.nl>
> To: mailing list for ConTeXt users <ntg-context@ntg.nl>
> Cc: Thierry Horsin <horsin@free.fr>
> Subject: Re: [NTG-context] getting line numbers
> Message-ID: <807e4b7c-97ae-e20e-3207-28393544c5e5@xs4all.nl>
> Content-Type: text/plain; charset=utf-8; format=flowed
>
> 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
> -----------------------------------------------------------------
>
>
> ------------------------------
>
> ------------------------------
>
> Message: 6
> Date: Fri, 3 Sep 2021 09:53:58 -0400 (EDT)
> From: Aditya Mahajan <adityam@umich.edu>
> To: Thierry Horsin via ntg-context <ntg-context@ntg.nl>
> Subject: Re: [NTG-context] getting line numbers
> Message-ID:
> 	<nycvar.YAK.7.77.849.2109030949370.477524@ervasbepr.pvz.zptvyy.pn>
> Content-Type: text/plain; charset=US-ASCII
>
> On Fri, 3 Sep 2021, Thierry Horsin via ntg-context wrote:
>
>
>      https://github.com/adityam/filter
>
> Behind the scenes, it works by saving the content in an external file and then
> running it through any external program. It also allows for caching the
> result, and has many options for customizing the behavior.
>
> Aditya
>
>
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-09-05 10:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-03 10:58 getting line numbers Thierry Horsin via ntg-context
2021-09-03 12:16 ` Hans Hagen via ntg-context
2021-09-03 12:20   ` Thierry Horsin via ntg-context
2021-09-03 13:53 ` Aditya Mahajan via ntg-context
2021-09-03 16:26   ` Hans Hagen via ntg-context
2021-09-05 10:26     ` Fabrice Couvreur via ntg-context
     [not found] <mailman.418.1630685069.1197.ntg-context@ntg.nl>
2021-09-04  7:01 ` thierry horsin via ntg-context
2021-09-04  8:35   ` Hans Hagen via ntg-context
2021-09-04 10:18     ` Thierry Horsin via ntg-context

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).