Re: \documentclass does not activate latex-mode

2022-08-19 Thread Ikumi Keita
> jfbu  writes:
>> For the time being, you can work around by
>> (let (entry)
>> (while (setq entry (rassq 'tex-mode auto-mode-alist))
>> (setcdr entry #'TeX-tex-mode)))
>> in your personal init file.

> wow it does work! Now that's quite magic to me. I have delayed for two
> decades learning e-Lisp...

It replaces all occurences of `tex-mode' in `auto-mode-alist' with
`TeX-tex-mode'. ;-)

> Thanks for your help!

You're welcome.

> Arash Esbati  writes:
> Ikumi Keita  writes:

>> [To developpers]
>> Why on earth does tex-mode.el use advice for its own function? The whole
>> above piece of code can just be composed up as
>> --
>> (defvar tex-mode--recursing nil)
>> (define-derived-mode tex-mode text-mode "generic-TeX"
>> "...doc string..."
>> 
>> ;; The file may have "mode: tex" in the local variable
>> ;; block, in which case we'll be called recursively
>> ;; infinitely.  Inhibit that.
>> (let ((tex-mode--recursing tex-mode--recursing))
>> (if (or delay-mode-hooks tex-mode--recursing)
>> ;; We're called from one of the children already.
>> (tex-common-initialization)
>> (setq tex-mode--recursing t)
>> (tex--guess-mode
>> --
>> without any advice, if I understand correctly.
>> 
>> I think we should file bug report for tex-mode.el. What do others
>> think?

> I didn't look closely, but I trust your assessment.  Would you file a
> bug report for tex-mode.el with your suggestion above?

I realized the necessity of around advice. My proposal calls the base
major mode function, `text-mode', unconditionally even when it delegates
the role to submode detected by `tex--guess-mode'. :-(

I'll reconsider this issue another day, probably November, since I don't
have much spare time now.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine



Re: \documentclass does not activate latex-mode

2022-08-18 Thread jfbu

Hi Keita,

Le 18/08/2022 à 09:40, Ikumi Keita a écrit :

Hi Jean, sorry for late response. I just came back from my summer
vacation.


no worries whatsoever
thanks for all your volunteer work on AUCTeX !




jfbu  writes:

when I open a buffer on this file




\def\foo{bar}
\documentclass{foobar}
\begin{document}
hello
\end{document}




there is no activation of latex-mode.



I have to manually type-in M-x latex-mode.


[...]


Any hint to make my life less miserable?


Actually, AUCTeX is able to activate latex mode correctly, but emacs
built-in tex-mode.el interferes with it, due to the change installed in
emacs 28. :-(




oh well... yes indeed I am currently using a build of GNU Emacs 28.1...

in retrospect it is easy for me to say that somewhere inside me a little voice 
was telling me that in the past I had been working under same circumstances and 
didn't experience the reported problems... but your detective work is much more 
accurate!


For the time being, you can work around by
(let (entry)
   (while (setq entry (rassq 'tex-mode auto-mode-alist))
 (setcdr entry #'TeX-tex-mode)))
in your personal init file.


wow it does work! Now that's quite magic to me. I have delayed for two decades 
learning e-Lisp...




But IMHO, should'nt \documentclass by itself trigger latex-mode?
It is relatively frequent to have to put code before the
\documentclass.


Yes. As I wrote above, there was no problem until emacs 27.



I knew it! (subconsciously ;-))

Thanks for your help!

Best,

Jean-François


[To developpers]
In emacs 28, tex-mode.el changed `tex-mode' activation like this:
--
(define-derived-mode tex-mode text-mode "generic-TeX"
   "...doc string..."
   (tex-common-initialization))

(advice-add 'tex-mode :around #'tex--redirect-to-submode)
(defvar tex-mode--recursing nil)
(defun tex--redirect-to-submode (orig-fun)
   "Redirect to one of the submodes when called directly."
   ;; The file may have "mode: tex" in the local variable
   ;; block, in which case we'll be called recursively
   ;; infinitely.  Inhibit that.
   (let ((tex-mode--recursing tex-mode--recursing))
 (funcall (if (or delay-mode-hooks tex-mode--recursing)
  ;; We're called from one of the children already.
  orig-fun
(setq tex-mode--recursing t)
(tex--guess-mode)
--
This around advice for `tex-mode' broke AUCTeX's intention. At emacs
startup, AUCTeX adds override advice `TeX-tex-mode' to `tex-mode' in
tex-site.el. However, when tex-mode.el is autoloaded, it futher adds
around advice `tex--redirect-to-submode'.
By the nature of around advice, it bypasses the `orig-fun' (which is
essentially `TeX-tex-mode' by the override advice) and calls
`tex--guess-mode'. `tex--guess-mode' in tex-mode.el is not smart enough
to guess that Jean's example is latex document, ending up with calling
`plain-tex-mode'.

Why on earth does tex-mode.el use advice for its own function? The whole
above piece of code can just be composed up as
--
(defvar tex-mode--recursing nil)
(define-derived-mode tex-mode text-mode "generic-TeX"
   "...doc string..."

   ;; The file may have "mode: tex" in the local variable
   ;; block, in which case we'll be called recursively
   ;; infinitely.  Inhibit that.
   (let ((tex-mode--recursing tex-mode--recursing))
 (if (or delay-mode-hooks tex-mode--recursing)
 ;; We're called from one of the children already.
 (tex-common-initialization)
   (setq tex-mode--recursing t)
   (tex--guess-mode
--
without any advice, if I understand correctly.

I think we should file bug report for tex-mode.el. What do others think?

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine







Re: \documentclass does not activate latex-mode

2022-08-18 Thread Arash Esbati
Hi Keita,

Ikumi Keita  writes:

> [To developpers]
> Why on earth does tex-mode.el use advice for its own function? The whole
> above piece of code can just be composed up as
> --
> (defvar tex-mode--recursing nil)
> (define-derived-mode tex-mode text-mode "generic-TeX"
>   "...doc string..."
>
>   ;; The file may have "mode: tex" in the local variable
>   ;; block, in which case we'll be called recursively
>   ;; infinitely.  Inhibit that.
>   (let ((tex-mode--recursing tex-mode--recursing))
> (if (or delay-mode-hooks tex-mode--recursing)
> ;; We're called from one of the children already.
> (tex-common-initialization)
>   (setq tex-mode--recursing t)
>   (tex--guess-mode
> --
> without any advice, if I understand correctly.
>
> I think we should file bug report for tex-mode.el. What do others
> think?

I didn't look closely, but I trust your assessment.  Would you file a
bug report for tex-mode.el with your suggestion above?

Best, Arash



Re: \documentclass does not activate latex-mode

2022-08-18 Thread Ikumi Keita
Hi Jean, sorry for late response. I just came back from my summer
vacation.

> jfbu  writes:
> when I open a buffer on this file

> 
> \def\foo{bar}
> \documentclass{foobar}
> \begin{document}
> hello
> \end{document}
> 

> there is no activation of latex-mode.

> I have to manually type-in M-x latex-mode.

[...]

> Any hint to make my life less miserable?

Actually, AUCTeX is able to activate latex mode correctly, but emacs
built-in tex-mode.el interferes with it, due to the change installed in
emacs 28. :-(

For the time being, you can work around by
(let (entry)
  (while (setq entry (rassq 'tex-mode auto-mode-alist))
(setcdr entry #'TeX-tex-mode)))
in your personal init file.

> But IMHO, should'nt \documentclass by itself trigger latex-mode?
> It is relatively frequent to have to put code before the
> \documentclass.

Yes. As I wrote above, there was no problem until emacs 27.

[To developpers]
In emacs 28, tex-mode.el changed `tex-mode' activation like this:
--
(define-derived-mode tex-mode text-mode "generic-TeX"
  "...doc string..."
  (tex-common-initialization))

(advice-add 'tex-mode :around #'tex--redirect-to-submode)
(defvar tex-mode--recursing nil)
(defun tex--redirect-to-submode (orig-fun)
  "Redirect to one of the submodes when called directly."
  ;; The file may have "mode: tex" in the local variable
  ;; block, in which case we'll be called recursively
  ;; infinitely.  Inhibit that.
  (let ((tex-mode--recursing tex-mode--recursing))
(funcall (if (or delay-mode-hooks tex-mode--recursing)
 ;; We're called from one of the children already.
 orig-fun
   (setq tex-mode--recursing t)
   (tex--guess-mode)
--
This around advice for `tex-mode' broke AUCTeX's intention. At emacs
startup, AUCTeX adds override advice `TeX-tex-mode' to `tex-mode' in
tex-site.el. However, when tex-mode.el is autoloaded, it futher adds
around advice `tex--redirect-to-submode'.
By the nature of around advice, it bypasses the `orig-fun' (which is
essentially `TeX-tex-mode' by the override advice) and calls
`tex--guess-mode'. `tex--guess-mode' in tex-mode.el is not smart enough
to guess that Jean's example is latex document, ending up with calling
`plain-tex-mode'.

Why on earth does tex-mode.el use advice for its own function? The whole
above piece of code can just be composed up as
--
(defvar tex-mode--recursing nil)
(define-derived-mode tex-mode text-mode "generic-TeX"
  "...doc string..."

  ;; The file may have "mode: tex" in the local variable
  ;; block, in which case we'll be called recursively
  ;; infinitely.  Inhibit that.
  (let ((tex-mode--recursing tex-mode--recursing))
(if (or delay-mode-hooks tex-mode--recursing)
;; We're called from one of the children already.
(tex-common-initialization)
  (setq tex-mode--recursing t)
  (tex--guess-mode
--
without any advice, if I understand correctly.

I think we should file bug report for tex-mode.el. What do others think?

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine



Re: \documentclass does not activate latex-mode

2022-08-17 Thread jfbu



Hi

Le 15/08/2022 à 14:41, Mandar Mitra a écrit :

jfbu wrote (Sun, Aug 14, 2022 at 06:14:12PM +0200):

I can not add any file local variable, the file is output
by some build.


You can't modify the build file that generates your .tex file? Something as 
simple as renaming haru.tex to haru.ltx might fix things for you.



I could modify some scripts which are put into the source directory of test 
projects and invoked to build, but I would have to repeat at each new feature 
testing directory
I would not modify my git clone of the upstream Makefile templates else I would 
risk committing and pushing accidentally


Some more questionable suggestions, while you wait for the experts to weigh in.

C-h v shows that I have the following entries in auto-mode-alist:

("\\.[tT]e[xX]\\'" . tex-mode)

("\\.ltx\\'" . latex-mode)



same here

Do you ever use plain-tex-mode at all? 


yes I do routinely...


If not, or only very infrequently, you could see if

(add-to-list 'auto-mode-alist '("\\[tT]e[xX]\\'" . latex-mode))

in your .emacs or init.el helps.



not an option...




But IMHO, should'nt \documentclass by itself trigger latex-mode?
It is relatively frequent to have to put code before the
\documentclass.


According to the variable TeX-format-list at tex.el:3640, a file will have to start with 
\documentclass, or \begin, \section, \subsection, etc. for auctex to go into latex-mode. 
The last entry in the list is a catchall regexp "." that invokes plain-tex-mode.



I am not sure from "3. A regexp typically matched in the beginning of the file." of the 
docstring what "in the beginning of the file means".  But anyhow, what I could see with 
my  sample file


\def\foo{bar}
\documentclass{foobar}
\begin{document}
hello
\end{document}


is that it would trigger latex-mode if the first line was commented.

So I now tried with \newcommand\foo{bar} in place of \def\foo{bar} and then yes 
latex-mode is triggered which is good as some LaTeX constructs require 
macro-code before \documentclass.  Notice that \newcommand is the same apart 
from existence check (and construction of macro with one optional argument) as 
\long\def and one could imagine (convoluted) situations where you really want 
\def and not \long\def here.

I even remember that there might

Now, indeed this is problematic as the \def in first line could indicate the 
author is a TeX fanatic and the presence of \documentclass could simply be a 
mirage.

But, sincerely, if you have \documentclass at start of one the first 5 lines of 
your document, reasonable bets at 99,9% is that this a LaTeX2e document.

Now the \def in my use case is inserted there by the LaTeX templates of the 
build system I am using.  I could possible make a PR there to ask them (aka me 
for latex) to use \newcommand...

On further investigation already

\foo{bar}
\documentclass{foobar}

triggers tex-mode not latex-mode
it is not especially the \def token

Now if \foo is replaced by \RequirePackage or something latexian auctex 
accurately chooses latex mode.

with


\input{bar}
\documentclass{article}
\begin{document}
hello
\end{document}


latex-mode is **not** triggered.  But this is quite LaTeXian in spirit.

I would argue that \documentclass at start of anyone of the first 5 lines of a 
document should trigger latex-mode.

Jean-François


-mandar








Re: \documentclass does not activate latex-mode

2022-08-15 Thread Mandar Mitra
jfbu wrote (Sun, Aug 14, 2022 at 06:14:12PM +0200):
> I can not add any file local variable, the file is output
> by some build.

You can't modify the build file that generates your .tex file? Something as 
simple as renaming haru.tex to haru.ltx might fix things for you.

Some more questionable suggestions, while you wait for the experts to weigh in.

C-h v shows that I have the following entries in auto-mode-alist:

("\\.[tT]e[xX]\\'" . tex-mode)

("\\.ltx\\'" . latex-mode)

Do you ever use plain-tex-mode at all? If not, or only very infrequently, you 
could see if

(add-to-list 'auto-mode-alist '("\\[tT]e[xX]\\'" . latex-mode))

in your .emacs or init.el helps. 


> But IMHO, should'nt \documentclass by itself trigger latex-mode?
> It is relatively frequent to have to put code before the
> \documentclass.

According to the variable TeX-format-list at tex.el:3640, a file will have to 
start with \documentclass, or \begin, \section, \subsection, etc. for auctex to 
go into latex-mode. The last entry in the list is a catchall regexp "." that 
invokes plain-tex-mode.

-mandar