zk-phi / magic-latex-buffer

Magical syntax highlighting for LaTeX-mode buffers
152 stars 13 forks source link

Different highlighting when called from LaTeX-mode-hook #36

Open mjkramer opened 6 years ago

mjkramer commented 6 years ago

Edit: Diagnosis isn't quite right; see next comment.

When I include magic-latex-buffer in LaTeX-mode-hook (whether at the end or beginning, doesn't matter), the result is what I expect that MLB works its magic in the right a few places, and the rest of the highlighting is left alone. On the other hand, without MLB in the hook, if I open a file and then activate MLB manually, the highlighting changes drastically (e.g. highlighting all commands indiscriminately, removing the highlighting from command arguments, changing the colors of sections, etc.).

In the screenshot below, the left column shows the "stock" highlighting without MLB. The middle one shows the result when MLB is included in the hook. On the right is the undesirable result I get upon manually activating MLB. (I also get this result if MLB is in the hook and I manually toggle it off and then back on.)

For what it's worth, my LaTeX-mode-hook looks like (spacemacs/load-yasnippet smartparens-mode evil-matchit-mode mjk-latex//LaTeX-mode-hook turn-on-reftex magic-latex-buffer TeX-PDF-mode TeX-source-correlate-mode LaTeX-math-mode TeX-fold-mode latex/auto-fill-mode spacemacs//init-company-LaTeX-mode company-mode). This does not seem to be a Spacemacs-specific issue, but I can try it on a vanilla Emacs.

image

mjkramer commented 6 years ago

Upon preliminary inspection of magic-latex-buffer.el, it looks like the righthand column shows the intended result. It's supposed to change those faces.

So now the issue is that MLB is not fully re-fontifying the buffer when activated in LaTeX-mode-hook. Will investigate further.

mjkramer commented 6 years ago

The problem seems to be that find-file-hook is calling TeX-update-style after MLB has initialized, and TeX-update-style clears font-lock-keywords (by setting font-lock-set-defaults to nil before calling (font-lock-set-defaults). So the effect of MLB's

(font-lock-add-keywords nil ml/keywords 'set)

is being cancelled out. Manually enabling magic-latex-buffer works, because at that point find-file-hook has already been run.

I will try to think of a non-hacky way to fix this.

mjkramer commented 6 years ago

I can confirm that the problem goes away when I put latex-magic-buffer in TeX-update-style-hook instead of LaTeX-mode-hook. Should this be documented in the README? Anyway I'll go ahead and submit a PR to Spacemacs.