#970 Latex folding and better syntax highlighting (code done)

Completed
closed
Neil Hodgson
None
5
2013-01-17
2013-01-09
tarlou
No

I noticed there is a latex lexer in LexOthers.cxx, which is much better than LexTeX.cxx :)
However, this is still not perfect.
1. It has no folding function.
2. It colors everything with the same color in math environment. However, we know there can be comments and others in math environments.

I would like to share my modifications on the latex lexer. I've done a folding function that recognizes \part, \chapter, \section, ..., \subparagraph as well as \begin...\end, and also changed the behaviors of syntax coloring.

I'm now using this myself. Hope this is helpful for improving the lexer.

For non-developers who want to use it: Put the file in \lexers; delete the code about latex in LexOthers.cxx; add LexLaTeX.cxx to makefile; and rebuild.

1 Attachments

Discussion

  • Neil Hodgson
    Neil Hodgson
    2013-01-09

    There are some compiler warnings:

    ../lexers/LexLaTeX.cxx: In function 'bool latexLastWordIsMathEnv(int, Accessor&)':
    ../lexers/LexLaTeX.cxx:171:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    ../lexers/LexLaTeX.cxx: In member function 'virtual void LexerLaTeX::Fold(unsigned int, int, int, IDocument*)':
    ../lexers/LexLaTeX.cxx:483:26: warning: variable 'allSpace' set but not used [-Wunused-but-set-variable]
    ../lexers/LexLaTeX.cxx:530:46: warning: 'lev' may be used uninitialized in this function [-Wmaybe-uninitialized]

     
  • tarlou
    tarlou
    2013-01-09

    I've fixed these warnings.

    Unluckily neither cl.exe nor gcc produced any warning on my computer, so I'm not quite sure if there are still warnings.

     
  • Neil Hodgson
    Neil Hodgson
    2013-01-10

    Arghh! I wrote a note here yesterday and its disappeared.

    The warnings were visible with Mingw gcc 4.7.0 on Windows.

    The new version has no warnings from gcc or cl but there is a warning from Code Analysis in Visual Studio Express 2012 in the latexLastWordIsMathEnv function:
    lexlatex.cxx(170): warning : C6385: Reading invalid data from 's': the readable size is '32' bytes, but '-1' bytes may be read.
    lexlatex.cxx(170): warning : C6386: Buffer overrun while writing to 's': the writable size is '32' bytes, but '-1' bytes might be written.
    This can be worked around with a check "if (j == 0) return false;" after the \0 termination of s. I'll accept code that produces Code Analysis warnings since there is a limit to the context it examines, but you should check that the code can not fail.
    Xcode doesn't like that code much either: http://www.scintilla.org/XcodeLaTeX.png

    It is better to remove code in contributions than to comment it out. It may be there as a reminder to you about future plans but to anyone else maintaining the code, it is just confusing.

     
  • tarlou
    tarlou
    2013-01-11

    The code won't fail, because the possibility of j==0 at 170 is ruled out by the check (i==pos-1) at line 165.

    But I agree this is not a good style. I have added "if (j==0) return false;" after 169, and removed the commented code in Fold(...) function.

     
    Attachments
  • Neil Hodgson
    Neil Hodgson
    2013-01-11

    Committed as [b68497], [e3ad39] and [381e3a] (SciTE).

    There may be some way of writing the code so it is more obvious both humans and code analysers that its OK. That would be good style. I expect that, in a couple of years, the code analysers will have advanced enough to more deeply understand that code and warn "expression (j == 0) always false".

     

    Related

    Commit: [381e3a]
    Commit: [b68497]
    Commit: [e3ad39]

  • Neil Hodgson
    Neil Hodgson
    2013-01-11

    • assigned_to: Neil Hodgson
     
  • Neil Hodgson
    Neil Hodgson
    2013-01-17

    • status: open --> closed