auctex
[Top][All Lists]

## Re: [AUCTeX] Listing and not listing regions inverted if \begin{lstlist

 From: Tassilo Horn Subject: Re: [AUCTeX] Listing and not listing regions inverted if \begin{lstlisting}' not followed by what is expected Date: Fri, 10 Apr 2015 11:36:53 +0200 User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (gnu/linux)

Denis Bitouzé <address@hidden> writes:

Hi Denis,

>>> Wouldn't it be possible for \end{...}' to never be considered as
>>> the /beginning/ of ...' environment?
>>
>> No, actually syntactic font-lock has no notion of beginning or end.
>
> Bad news! But good news: I'm smarter than AUCTeX! ;)

Humans write programs, so in general I hope we are all smarter than the
programs we produce.  At least in the middle term.  And then comes the
Terminator.  ;-)

Anyway, keen to elaborate?  Did you manage to work around the issue?

>> Basically, what AUCTeX' font-latex does is telling emacs that the
>> contents of verbatim environments are a kind of string which should
>> be highlighted using the verbatim face.  For that purpose, it has to
>> tell emacs where the "quotes" (delimiters) of that string are.  For
>> that, there's the "generic string fence" syntax class (|).  As an
>> example, in
>>
>> \begin{lstlisting}
>> foo bar
>> \end{lstlisting}
>>
>> the newline character after \begin{lstlisting} and the \ of
>> \end{lstlisting} have that |-syntax.  When the emacs encounters a char
>> with that syntax, it *toggles* string highlighting.
>
> If I understand well, emacs knows:
>
> - \begin{lstlisting} is the opening,
> - \ of \end{lstlisting} is the closing,
>
> "quote". Isn't it a kind of beginning or end notion?

No, it just notes here is a "quote" and there is a "quote".  You only
know if its an opening or closing quote when you start counting all
quotes from the beginning of the buffer, i.e., all oddly numbered quotes
are opening, all evenly numbered quotes are closing.

>> So long story short: I think we can get either argument highlighting
>> right or we can get nested verbatim environments right but not both.
>> And since your use-case, i.e., documenting how to use verbatim
>> environments, is the only one which requires nested verbatim
>> environments, I think the current implementation which fontifies
>> arguments correctly, is more useful for the majority of users.
>
> Indeed.

As a workaround for your use-case: how about putting the contents of the
top-level listings into separate files and then use \lstinputlisting?

>>> Meanwhile, is it possible to stop a listing region highlight with
>>
>> Yes, but not so easily.  You have to remove "lstlisting" from the
>> verbatim environments and then re-initiate font-locking.  You can do
>> so by writing a custom listings style and placing that in
>> style/listings.el (that path is relative to your master file; don't
>> replace the existing listings.el that ships with AUCTeX).
>>
>>  "listings"
>>  (lambda ()
>>    (setq LaTeX-verbatim-environments-local
>>          (remove "lstlisting" LaTeX-verbatim-environments-local))
>>    (font-latex-set-syntactic-keywords)
>>    (setq font-lock-set-defaults nil)
>>    (font-lock-set-defaults)))
>>
>> However, you need the current Git master for that to work.  Until the
>> last commit, AUCTeX would have called your custom style hook before
>> the style hook that ships with AUCTeX.  Now I do that in reversed
>> order, so a local (or private) listings.el style runs after the
>> global listings.el style and may undo what the former did like we do.
>
> OK, thanks. Unfortunately, I've no time for such test right now. But,
> when a new AUCTeX release will be available, I'll give feedback.

Cool, thanks.

Bye,
Tassilo