Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-13 Thread Georg Baum
Uwe Stöhr wrote:

 Am 10.03.2012 13:53, schrieb Georg Baum:
 
 Yes. Unfortunately this is not done, since special properties of
 characters like % are already handled in the tokenize phase. For example,

 \begin{verbatim}
 x
 %\end{verbatim}

 is not handled correctly, since tex2lyx believes that %\end{verbatim} is
 a comment.
 
 OK, but these cases seem a bit constructed. Who will uses such things in
 practice? For example in a listings inset I bet nobody ever.

Of course it is constructed. Again, it was only an example. I am pretty sure 
that I would find other cases if I would dive a bit deeper in the code. Is 
it really needed to demonstrate all possible problems? For me it is enough 
to know that problems exist, and that a clean solution needs to attack the 
problem at another level (cat codes or character by character parsing, or 
maybe even a third alternative) to stop thinking about the current one.

 But sure, if we can handle such cases, we should do so. I'm technically
 too limited in respect to catcodes and would leave this to you. OK?

Yes, but I won't promise any time frame (the third time I write about time 
today;-/)


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-13 Thread Georg Baum
Uwe Stöhr wrote:

> Am 10.03.2012 13:53, schrieb Georg Baum:
> 
>> Yes. Unfortunately this is not done, since special properties of
>> characters like % are already handled in the tokenize phase. For example,
>>
>> \begin{verbatim}
>> x
>> %\end{verbatim}
>>
>> is not handled correctly, since tex2lyx believes that %\end{verbatim} is
>> a comment.
> 
> OK, but these cases seem a bit constructed. Who will uses such things in
> practice? For example in a listings inset I bet nobody ever.

Of course it is constructed. Again, it was only an example. I am pretty sure 
that I would find other cases if I would dive a bit deeper in the code. Is 
it really needed to demonstrate all possible problems? For me it is enough 
to know that problems exist, and that a clean solution needs to attack the 
problem at another level (cat codes or character by character parsing, or 
maybe even a third alternative) to stop thinking about the current one.

> But sure, if we can handle such cases, we should do so. I'm technically
> too limited in respect to catcodes and would leave this to you. OK?

Yes, but I won't promise any time frame (the third time I write about time 
today;-/)


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-12 Thread Uwe Stöhr

Am 10.03.2012 13:53, schrieb Georg Baum:


Yes. Unfortunately this is not done, since special properties of characters
like % are already handled in the tokenize phase. For example,

\begin{verbatim}
x
%\end{verbatim}

is not handled correctly, since tex2lyx believes that %\end{verbatim} is a
comment.


OK, but these cases seem a bit constructed. Who will uses such things in practice? For example in a 
listings inset I bet nobody ever.
But sure, if we can handle such cases, we should do so. I'm technically too limited in respect to 
catcodes and would leave this to you. OK?


thanks and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-12 Thread Uwe Stöhr

Am 10.03.2012 14:03, schrieb Georg Baum:


The special language option handling looks fragile. I believe it will work
without the special code if you use p.verbatimOption() instead of
p.getArg().


Thanks, this works. I will commit as soon as the git transformation is ready and committing is 
possible again.


regards Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-12 Thread Uwe Stöhr

Am 10.03.2012 13:53, schrieb Georg Baum:


Yes. Unfortunately this is not done, since special properties of characters
like % are already handled in the tokenize phase. For example,

\begin{verbatim}
x
%\end{verbatim}

is not handled correctly, since tex2lyx believes that %\end{verbatim} is a
comment.


OK, but these cases seem a bit constructed. Who will uses such things in practice? For example in a 
listings inset I bet nobody ever.
But sure, if we can handle such cases, we should do so. I'm technically too limited in respect to 
catcodes and would leave this to you. OK?


thanks and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-12 Thread Uwe Stöhr

Am 10.03.2012 14:03, schrieb Georg Baum:


The special language option handling looks fragile. I believe it will work
without the special code if you use p.verbatimOption() instead of
p.getArg().


Thanks, this works. I will commit as soon as the git transformation is ready and committing is 
possible again.


regards Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-10 Thread Georg Baum
Uwe Stöhr wrote:

 Am 04.03.2012 22:47, schrieb Jean-Marc Lasgouttes:
 
 The point that Georg wanted to make is that we know what the correct
 solution is: reset all catcodes in the parser when we are in verbatim, or
 just read character by character until we find \end{verbatim}.
 
 I'm not sure about the catcodes. What we need to do is to parse verbatim
 as plain text without any change except for backslashes.

Yes. Unfortunately this is not done, since special properties of characters 
like % are already handled in the tokenize phase. For example,

\begin{verbatim}
x
%\end{verbatim}

is not handled correctly, since tex2lyx believes that %\end{verbatim} is a 
comment. Jean-Marc is right: The input must either be read with changed 
catcodes (probably more difficult), or character by character, not token by 
token (probably more easy).


Georg




Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-10 Thread Georg Baum
Uwe Stöhr wrote:

 While doing this I stumbled of a tex2lyx bug with listings:
 http://www.lyx.org/trac/ticket/8065
 
 The solution would be the same as I now did for verbatim.
 What do you think?

I agree (once the verbatim support is improved).

 I also found out that listings are currently not parsed if they have some
 settings: http://www.lyx.org/trac/ticket/8066

The special language option handling looks fragile. I believe it will work 
without the special code if you use p.verbatimOption() instead of 
p.getArg(). That would also take care if other options than language use 
brackets.


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-10 Thread Georg Baum
Uwe Stöhr wrote:

> Am 04.03.2012 22:47, schrieb Jean-Marc Lasgouttes:
> 
>> The point that Georg wanted to make is that we know what the correct
>> solution is: reset all catcodes in the parser when we are in verbatim, or
>> just read character by character until we find \end{verbatim}.
> 
> I'm not sure about the catcodes. What we need to do is to parse verbatim
> as plain text without any change except for backslashes.

Yes. Unfortunately this is not done, since special properties of characters 
like % are already handled in the tokenize phase. For example,

\begin{verbatim}
x
%\end{verbatim}

is not handled correctly, since tex2lyx believes that %\end{verbatim} is a 
comment. Jean-Marc is right: The input must either be read with changed 
catcodes (probably more difficult), or character by character, not token by 
token (probably more easy).


Georg




Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-10 Thread Georg Baum
Uwe Stöhr wrote:

> While doing this I stumbled of a tex2lyx bug with listings:
> http://www.lyx.org/trac/ticket/8065
> 
> The solution would be the same as I now did for verbatim.
> What do you think?

I agree (once the verbatim support is improved).

> I also found out that listings are currently not parsed if they have some
> settings: http://www.lyx.org/trac/ticket/8066

The special language option handling looks fragile. I believe it will work 
without the special code if you use p.verbatimOption() instead of 
p.getArg(). That would also take care if other options than language use 
brackets.


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-05 Thread Uwe Stöhr

Am 05.03.2012 03:07, schrieb Uwe Stöhr:


your section{ example stangely fails.


It failed of course as I explicitly handled this case incorrectly. Now 
everything should be in shape.

regards Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-05 Thread Uwe Stöhr

Am 05.03.2012 03:07, schrieb Uwe Stöhr:


your section{ example stangely fails.


It failed of course as I explicitly handled this case incorrectly. Now 
everything should be in shape.

regards Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Uwe Stöhr

Am 01.03.2012 00:48, schrieb Uwe Stöhr:


Paragraph ended in line 1032
Missing \end_layout.


Strange that I don't get this.


I now had a closer look and fixed this now.


If you want a more fancy test, try this:

\begin{verbatim}
\begin{foo}
\end{verbatim}

This will convert everything what follows to verbatim. If you look how
verbatimEnvironment() is implemented, you can also see why this is the case.



I now created a new function where \begin and \end inside verbatim environments 
are no longer parsed.
This must maybe be optimized a bit for other cases.
Could you please give it a try? I also now updated the verbatim example in our 
test file.

While doing this I stumbled of a tex2lyx bug with listings:
http://www.lyx.org/trac/ticket/8065

The solution would be the same as I now did for verbatim.
What do you think?

I also found out that listings are currently not parsed if they have some 
settings:
http://www.lyx.org/trac/ticket/8066

thanks for your help and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Uwe Stöhr

Am 01.03.2012 21:18, schrieb Georg Baum:


If you can tell me what exactly fails I can have a look, but I want to avoid
doing duplicate work.


I tried to use this

Context context(true, parent_context.textclass);
context.layout = parent_context.textclass.plainLayout();
context.check_layout(os);

But it must be Verbatim as layout, not plainLayout. I failed to get this.

thanks and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Jean-Marc Lasgouttes

Le 04/03/2012 15:12, Uwe Stöhr a écrit :

I now created a new function where \begin and \end inside verbatim
environments are no longer parsed.
This must maybe be optimized a bit for other cases.
Could you please give it a try? I also now updated the verbatim example
in our test file.


You realize that Georg only wanted to give an example, don't you?
What about this one?
\begin{verbatim}
\section{
\end{verbatim}

[caveat, I did not try it, so I may end up ridiculed]

The point that Georg wanted to make is that we know what the correct 
solution is: reset all catcodes in the parser when we are in verbatim, 
or just read character by character until we find \end{verbatim}.


JMarc


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Uwe Stöhr

Am 04.03.2012 22:47, schrieb Jean-Marc Lasgouttes:


The point that Georg wanted to make is that we know what the correct solution 
is: reset all catcodes
in the parser when we are in verbatim, or just read character by character 
until we find
\end{verbatim}.


I'm not sure about the catcodes. What we need to do is to parse verbatim as plain text without any 
change except for backslashes.


your section{ example stangely fails. I will have a look the next days.

regards Uwe



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Uwe Stöhr

Am 01.03.2012 00:48, schrieb Uwe Stöhr:


Paragraph ended in line 1032
Missing \end_layout.


Strange that I don't get this.


I now had a closer look and fixed this now.


If you want a more fancy test, try this:

\begin{verbatim}
\begin{foo}
\end{verbatim}

This will convert everything what follows to verbatim. If you look how
verbatimEnvironment() is implemented, you can also see why this is the case.



I now created a new function where \begin and \end inside verbatim environments 
are no longer parsed.
This must maybe be optimized a bit for other cases.
Could you please give it a try? I also now updated the verbatim example in our 
test file.

While doing this I stumbled of a tex2lyx bug with listings:
http://www.lyx.org/trac/ticket/8065

The solution would be the same as I now did for verbatim.
What do you think?

I also found out that listings are currently not parsed if they have some 
settings:
http://www.lyx.org/trac/ticket/8066

thanks for your help and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Uwe Stöhr

Am 01.03.2012 21:18, schrieb Georg Baum:


If you can tell me what exactly fails I can have a look, but I want to avoid
doing duplicate work.


I tried to use this

Context context(true, parent_context.textclass);
context.layout = _context.textclass.plainLayout();
context.check_layout(os);

But it must be "Verbatim" as layout, not "plainLayout". I failed to get this.

thanks and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Jean-Marc Lasgouttes

Le 04/03/2012 15:12, Uwe Stöhr a écrit :

I now created a new function where \begin and \end inside verbatim
environments are no longer parsed.
This must maybe be optimized a bit for other cases.
Could you please give it a try? I also now updated the verbatim example
in our test file.


You realize that Georg only wanted to give an example, don't you?
What about this one?
\begin{verbatim}
\section{
\end{verbatim}

[caveat, I did not try it, so I may end up ridiculed]

The point that Georg wanted to make is that we know what the correct 
solution is: reset all catcodes in the parser when we are in verbatim, 
or just read character by character until we find \end{verbatim}.


JMarc


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-04 Thread Uwe Stöhr

Am 04.03.2012 22:47, schrieb Jean-Marc Lasgouttes:


The point that Georg wanted to make is that we know what the correct solution 
is: reset all catcodes
in the parser when we are in verbatim, or just read character by character 
until we find
\end{verbatim}.


I'm not sure about the catcodes. What we need to do is to parse verbatim as plain text without any 
change except for backslashes.


your section{ example stangely fails. I will have a look the next days.

regards Uwe



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-01 Thread Georg Baum
Uwe Stöhr wrote:

 Am 28.02.2012 22:11, schrieb Georg Baum:
 
 Your test code in test-structure.tex fails already. LyX complains like
 this:

 Paragraph ended in line 1032
 Missing \end_layout.
 
 Strange that I don't get this. All my examples are exported as expected.
 Looking in the LyX-code I can also don't see a problem, but I'll have a
 closer look the next days.

Look in the console output. The generated file is invalid, but LyX tries to 
read it nevertheless, and succeeds, because it guesses right where the 
paragraph ends.

 The reason for this is probably that you did not follow Jean-Marcs advice
 to use the context machinery.
 
 I tried this but failed. It doesn't seem to work in this case but I'm not
 an expert in this.

If you can tell me what exactly fails I can have a look, but I want to avoid 
doing duplicate work.


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-03-01 Thread Georg Baum
Uwe Stöhr wrote:

> Am 28.02.2012 22:11, schrieb Georg Baum:
> 
>> Your test code in test-structure.tex fails already. LyX complains like
>> this:
>>
>> Paragraph ended in line 1032
>> Missing \end_layout.
> 
> Strange that I don't get this. All my examples are exported as expected.
> Looking in the LyX-code I can also don't see a problem, but I'll have a
> closer look the next days.

Look in the console output. The generated file is invalid, but LyX tries to 
read it nevertheless, and succeeds, because it guesses right where the 
paragraph ends.

>> The reason for this is probably that you did not follow Jean-Marcs advice
>> to use the context machinery.
> 
> I tried this but failed. It doesn't seem to work in this case but I'm not
> an expert in this.

If you can tell me what exactly fails I can have a look, but I want to avoid 
doing duplicate work.


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-29 Thread Uwe Stöhr

Am 28.02.2012 22:11, schrieb Georg Baum:


Your test code in test-structure.tex fails already. LyX complains like this:

Paragraph ended in line 1032
Missing \end_layout.


Strange that I don't get this. All my examples are exported as expected. Looking in the LyX-code I 
can also don't see a problem, but I'll have a closer look the next days.



The reason for this is probably that you did not follow Jean-Marcs advice to
use the context machinery.


I tried this but failed. It doesn't seem to work in this case but I'm not an 
expert in this.


If you want a more fancy test, try this:

\begin{verbatim}
\begin{foo}
\end{verbatim}

This will convert everything what follows to verbatim. If you look how
verbatimEnvironment() is implemented, you can also see why this is the case.


OK. I wasn't aware of these cases.


skip_braces() is wrong.


Why? We are now behind \end{verbatim} so if there are braces we can eat
then, no?


No. They must be eaten iff LyX writes them.


Fixed now.

thanks and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-29 Thread Uwe Stöhr

Am 28.02.2012 22:11, schrieb Georg Baum:


Your test code in test-structure.tex fails already. LyX complains like this:

Paragraph ended in line 1032
Missing \end_layout.


Strange that I don't get this. All my examples are exported as expected. Looking in the LyX-code I 
can also don't see a problem, but I'll have a closer look the next days.



The reason for this is probably that you did not follow Jean-Marcs advice to
use the context machinery.


I tried this but failed. It doesn't seem to work in this case but I'm not an 
expert in this.


If you want a more fancy test, try this:

\begin{verbatim}
\begin{foo}
\end{verbatim}

This will convert everything what follows to verbatim. If you look how
verbatimEnvironment() is implemented, you can also see why this is the case.


OK. I wasn't aware of these cases.


skip_braces() is wrong.


Why? We are now behind \end{verbatim} so if there are braces we can eat
then, no?


No. They must be eaten iff LyX writes them.


Fixed now.

thanks and regards
Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-28 Thread Georg Baum
Uwe Stöhr wrote:

 Am 21.02.2012 22:09, schrieb Georg Baum:
 
 this is far more complicated than what you implemented. Believe me, if it
 was that easy I would have added verbatim support a long time ago.
 
 I think we could not support verbatim before as we had no feature to
 convert to.

OK, to be more precise: I would have implemented a correct conversion of 
verbatim to ERT. But even that is much more complicated.

 +   os  \n\\begin_layout Verbatim\n;
 +   string const s = p.verbatimEnvironment(verbatim);

 The environment nesting in verbatimEnvironment() is not suitable for
 verbatim environments. Instead, you need to implement exactly the same
 algorithm as used by TeX for detecting the environment end. In order to
 do this, you probably also need to temporarily change the cat codes in
 the parser.
 
 Why not. I played around with all constructs that came into my mind and
 verbatimEnvironment does what I want.
 Can you give ma an example TeX code where my code fails?

Your test code in test-structure.tex fails already. LyX complains like this:

Paragraph ended in line 1032
Missing \end_layout.

The reason for this is probably that you did not follow Jean-Marcs advice to 
use the context machinery.

If you want a more fancy test, try this:

\begin{verbatim}
\begin{foo}
\end{verbatim}

This will convert everything what follows to verbatim. If you look how 
verbatimEnvironment() is implemented, you can also see why this is the case. 
Andf if this works, there is more funny stuff, e.g.

\begin{verbatim}
foo
\end{verbatim} some stuff on the same line

which needs to be parsed differently, depending whether verbatim.sty is 
loaded or not.

 \n\\end_layout\n\\begin_layout Verbatim\n;
 +   } else
 +   os  *it;

 This test for consecutive space looks wrong. In a verbatim block you
 should be able to write as many consecutive spaces (even at the end), and
 LyX should output them as is.
 
 But it does. I only had to add this special code for the case it is at the
 end because of http://www.lyx.org/trac/ticket/8049

But this is exactly what I was telling! There is obviously a bug in LyX 
which should be fixed in LyX, not in tex2lyx.

 If this special treatment is really needed the
 verbatim implementation in LyX looks wrong.
 
 What exactly?

Bug 8049 (but I did not know that it has a number).

 +   os  \n\\end_layout\n\n;
 +   p.skip_spaces();
 +   skip_braces(p); // eat {} that might by set by LyX
 behind comments

 skip_braces() is wrong.
 
 Why? We are now behind \end{verbatim} so if there are braces we can eat
 then, no?

No. They must be eaten iff LyX writes them.


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-28 Thread Georg Baum
Uwe Stöhr wrote:

> Am 21.02.2012 22:09, schrieb Georg Baum:
> 
>> this is far more complicated than what you implemented. Believe me, if it
>> was that easy I would have added verbatim support a long time ago.
> 
> I think we could not support verbatim before as we had no feature to
> convert to.

OK, to be more precise: I would have implemented a correct conversion of 
verbatim to ERT. But even that is much more complicated.

>>> +   os<<  "\n\\begin_layout Verbatim\n";
>>> +   string const s = p.verbatimEnvironment("verbatim");
>>
>> The environment nesting in verbatimEnvironment() is not suitable for
>> verbatim environments. Instead, you need to implement exactly the same
>> algorithm as used by TeX for detecting the environment end. In order to
>> do this, you probably also need to temporarily change the cat codes in
>> the parser.
> 
> Why not. I played around with all constructs that came into my mind and
> verbatimEnvironment does what I want.
> Can you give ma an example TeX code where my code fails?

Your test code in test-structure.tex fails already. LyX complains like this:

Paragraph ended in line 1032
Missing \end_layout.

The reason for this is probably that you did not follow Jean-Marcs advice to 
use the context machinery.

If you want a more fancy test, try this:

\begin{verbatim}
\begin{foo}
\end{verbatim}

This will convert everything what follows to verbatim. If you look how 
verbatimEnvironment() is implemented, you can also see why this is the case. 
Andf if this works, there is more funny stuff, e.g.

\begin{verbatim}
foo
\end{verbatim} some stuff on the same line

which needs to be parsed differently, depending whether verbatim.sty is 
loaded or not.

>> "\n\\end_layout\n\\begin_layout Verbatim\n";
>>> +   } else
>>> +   os<<  *it;
>>
>> This test for consecutive space looks wrong. In a verbatim block you
>> should be able to write as many consecutive spaces (even at the end), and
>> LyX should output them as is.
> 
> But it does. I only had to add this special code for the case it is at the
> end because of http://www.lyx.org/trac/ticket/8049

But this is exactly what I was telling! There is obviously a bug in LyX 
which should be fixed in LyX, not in tex2lyx.

>> If this special treatment is really needed the
>> verbatim implementation in LyX looks wrong.
> 
> What exactly?

Bug 8049 (but I did not know that it has a number).

>>> +   os<<  "\n\\end_layout\n\n";
>>> +   p.skip_spaces();
>>> +   skip_braces(p); // eat {} that might by set by LyX
>> behind comments
>>
>> skip_braces() is wrong.
> 
> Why? We are now behind \end{verbatim} so if there are braces we can eat
> then, no?

No. They must be eaten iff LyX writes them.


Georg



Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-26 Thread Uwe Stöhr

Am 21.02.2012 22:09, schrieb Georg Baum:


this is far more complicated than what you implemented. Believe me, if it
was that easy I would have added verbatim support a long time ago.


I think we could not support verbatim before as we had no feature to convert to.


+   else if (name == verbatim) {
+   eat_whitespace(p, os, parent_context, false);


eat_whitespace is wrong here.


Sorry. Fixed now.


+   os  \n\\begin_layout Verbatim\n;
+   string const s = p.verbatimEnvironment(verbatim);


The environment nesting in verbatimEnvironment() is not suitable for
verbatim environments. Instead, you need to implement exactly the same
algorithm as used by TeX for detecting the environment end. In order to do
this, you probably also need to temporarily change the cat codes in the
parser.


Why not. I played around with all constructs that came into my mind and verbatimEnvironment does 
what I want.

Can you give ma an example TeX code where my code fails?


\n\\end_layout\n\\begin_layout Verbatim\n;

+   } else
+   os  *it;


This test for consecutive space looks wrong. In a verbatim block you should
be able to write as many consecutive spaces (even at the end), and LyX
should output them as is.


But it does. I only had to add this special code for the case it is at the end 
because of
http://www.lyx.org/trac/ticket/8049


If this special treatment is really needed the
verbatim implementation in LyX looks wrong.


What exactly?


+   os  \n\\end_layout\n\n;
+   p.skip_spaces();
+   skip_braces(p); // eat {} that might by set by LyX

behind comments

skip_braces() is wrong.


Why? We are now behind \end{verbatim} so if there are braces we can eat then, 
no?


Note that you do not escape backslashes, but this is needed in the .lyx file
format.


Thanks for the hint. I did this now.


Please add a test case as well.


Also done now.

regards Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-26 Thread Uwe Stöhr

Am 21.02.2012 22:09, schrieb Georg Baum:


this is far more complicated than what you implemented. Believe me, if it
was that easy I would have added verbatim support a long time ago.


I think we could not support verbatim before as we had no feature to convert to.


+   else if (name == "verbatim") {
+   eat_whitespace(p, os, parent_context, false);


eat_whitespace is wrong here.


Sorry. Fixed now.


+   os<<  "\n\\begin_layout Verbatim\n";
+   string const s = p.verbatimEnvironment("verbatim");


The environment nesting in verbatimEnvironment() is not suitable for
verbatim environments. Instead, you need to implement exactly the same
algorithm as used by TeX for detecting the environment end. In order to do
this, you probably also need to temporarily change the cat codes in the
parser.


Why not. I played around with all constructs that came into my mind and verbatimEnvironment does 
what I want.

Can you give ma an example TeX code where my code fails?


"\n\\end_layout\n\\begin_layout Verbatim\n";

+   } else
+   os<<  *it;


This test for consecutive space looks wrong. In a verbatim block you should
be able to write as many consecutive spaces (even at the end), and LyX
should output them as is.


But it does. I only had to add this special code for the case it is at the end 
because of
http://www.lyx.org/trac/ticket/8049


If this special treatment is really needed the
verbatim implementation in LyX looks wrong.


What exactly?


+   os<<  "\n\\end_layout\n\n";
+   p.skip_spaces();
+   skip_braces(p); // eat {} that might by set by LyX

behind comments

skip_braces() is wrong.


Why? We are now behind \end{verbatim} so if there are braces we can eat then, 
no?


Note that you do not escape backslashes, but this is needed in the .lyx file
format.


Thanks for the hint. I did this now.


Please add a test case as well.


Also done now.

regards Uwe


Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-21 Thread Georg Baum
uwesto...@lyx.org wrote:

 Author: uwestoehr
 Date: Tue Feb 21 00:38:13 2012
 New Revision: 40785
 URL: http://www.lyx.org/trac/changeset/40785
 
 Log:
 support for verbatim: step 2: tex2lyx support

Uwe,

this is far more complicated than what you implemented. Believe me, if it 
was that easy I would have added verbatim support a long time ago.

 Modified:
lyx-devel/trunk/src/tex2lyx/text.cpp
lyx-devel/trunk/src/version.h
 
 Modified: lyx-devel/trunk/src/tex2lyx/text.cpp
 
==
 --- lyx-devel/trunk/src/tex2lyx/text.cpp  Mon Feb 20 03:10:33 2012
(r40784)
 +++ lyx-devel/trunk/src/tex2lyx/text.cpp  Tue Feb 21 00:38:13 2012
(r40785)
 @@ -1343,6 +1343,29 @@
  preamble.registerAutomaticallyLoadedPackage(verbatim);
  }
  
 + else if (name == verbatim) {
 + eat_whitespace(p, os, parent_context, false);

eat_whitespace is wrong here.

 + os  \n\\begin_layout Verbatim\n;
 + string const s = p.verbatimEnvironment(verbatim);

The environment nesting in verbatimEnvironment() is not suitable for 
verbatim environments. Instead, you need to implement exactly the same 
algorithm as used by TeX for detecting the environment end. In order to do 
this, you probably also need to temporarily change the cat codes in the 
parser.

 + string::const_iterator it2 = s.begin();
 + for (string::const_iterator it = s.begin(), et = 
s.end(); it != et;
 ++it) {
 + if (*it == '\n') {
 + it2 = it + 1;
 + // avoid adding an empty paragraph 
at the end
 + // if there are 2 consecutive spaces 
at the end ignore it
 + // because LyX will re-add a \n
 + if ((it + 1 != et)  (it + 2 != et 
|| *it2 != '\n'))
 + os  
\n\\end_layout\n\\begin_layout Verbatim\n;
 + } else
 + os  *it;

This test for consecutive space looks wrong. In a verbatim block you should 
be able to write as many consecutive spaces (even at the end), and LyX 
should output them as is. If this special treatment is really needed the 
verbatim implementation in LyX looks wrong.

 + }
 + os  \n\\end_layout\n\n;
 + p.skip_spaces();
 + skip_braces(p); // eat {} that might by set by LyX 
behind comments

skip_braces() is wrong.

 + // reset to Standard layout
 + os  \n\\begin_layout Standard\n;
 + }
 +
  else if (name == lyxgreyedout) {
  eat_whitespace(p, os, parent_context, false);
  parent_context.check_layout(os);

Note that you do not escape backslashes, but this is needed in the .lyx file 
format. Please add a test case as well.


Georg




Re: r40785 - in lyx-devel/trunk/src: . tex2lyx

2012-02-21 Thread Georg Baum
uwesto...@lyx.org wrote:

> Author: uwestoehr
> Date: Tue Feb 21 00:38:13 2012
> New Revision: 40785
> URL: http://www.lyx.org/trac/changeset/40785
> 
> Log:
> support for verbatim: step 2: tex2lyx support

Uwe,

this is far more complicated than what you implemented. Believe me, if it 
was that easy I would have added verbatim support a long time ago.

> Modified:
>lyx-devel/trunk/src/tex2lyx/text.cpp
>lyx-devel/trunk/src/version.h
> 
> Modified: lyx-devel/trunk/src/tex2lyx/text.cpp
> 
==
> --- lyx-devel/trunk/src/tex2lyx/text.cpp  Mon Feb 20 03:10:33 2012
(r40784)
> +++ lyx-devel/trunk/src/tex2lyx/text.cpp  Tue Feb 21 00:38:13 2012
(r40785)
> @@ -1343,6 +1343,29 @@
>  preamble.registerAutomaticallyLoadedPackage("verbatim");
>  }
>  
> + else if (name == "verbatim") {
> + eat_whitespace(p, os, parent_context, false);

eat_whitespace is wrong here.

> + os << "\n\\begin_layout Verbatim\n";
> + string const s = p.verbatimEnvironment("verbatim");

The environment nesting in verbatimEnvironment() is not suitable for 
verbatim environments. Instead, you need to implement exactly the same 
algorithm as used by TeX for detecting the environment end. In order to do 
this, you probably also need to temporarily change the cat codes in the 
parser.

> + string::const_iterator it2 = s.begin();
> + for (string::const_iterator it = s.begin(), et = 
s.end(); it != et;
> ++it) {
> + if (*it == '\n') {
> + it2 = it + 1;
> + // avoid adding an empty paragraph 
at the end
> + // if there are 2 consecutive spaces 
at the end ignore it
> + // because LyX will re-add a \n
> + if ((it + 1 != et) && (it + 2 != et 
|| *it2 != '\n'))
> + os << 
"\n\\end_layout\n\\begin_layout Verbatim\n";
> + } else
> + os << *it;

This test for consecutive space looks wrong. In a verbatim block you should 
be able to write as many consecutive spaces (even at the end), and LyX 
should output them as is. If this special treatment is really needed the 
verbatim implementation in LyX looks wrong.

> + }
> + os << "\n\\end_layout\n\n";
> + p.skip_spaces();
> + skip_braces(p); // eat {} that might by set by LyX 
behind comments

skip_braces() is wrong.

> + // reset to Standard layout
> + os << "\n\\begin_layout Standard\n";
> + }
> +
>  else if (name == "lyxgreyedout") {
>  eat_whitespace(p, os, parent_context, false);
>  parent_context.check_layout(os);

Note that you do not escape backslashes, but this is needed in the .lyx file 
format. Please add a test case as well.


Georg