5

So I have a long papers.bib bibtex file that I edit with emacs (spacemacs bibtex layer), but the syntax highlighting only ever goes about halfway down. Here it is at about line 790:

screenshot

Other times this happens elsewhere: line 10,000, line 900, etc. At first I thought this happens because of some incorrect syntax in the Bibtex, but I can't see any errors, actually. What's going on here, and how can I get it to syntax highlight the whole file?

Jonathan
  • 567
  • 5
  • 20
  • This used to happen to me all the time, and it drove me nuts. I eventually sorted it out, and I think it was due to a bad interaction with one of my add-on packages. Unfortunately I can't remember which. Or it might have been resolved when moving to a newer Emacs. Sorry I can't be more specific, but I feel your pain! – Tyler Jul 17 '17 at 15:35
  • Can you post your init.el somewhere? I can take a look and see if I recognize the culprit – Tyler Nov 17 '17 at 20:09
  • My init.el is just the spacemacs init.el, which I think is on GitHub somewhere. – Jonathan Nov 24 '17 at 11:15
  • ok. I can confirm the problem using the default spacemacs install, so that's something – Tyler Nov 26 '17 at 15:01
  • Hadn't looked at spacemacs before - it's very slick! However, it's also enormous, and I'm not sure how to go about debugging it. Perhaps there's a spacemacs-specific forum for dealing with config bugs? – Tyler Nov 26 '17 at 15:36
  • I have occasionally had bibtex highlighting go awry because of some earlier thing in the file. And I've got a bibtex file with 100K lines that works fine. Some things to check: (1) if you remove the entry you showed, does the rest highlight properly? (2) If you remove all the entries above that entry, does it highlight properly? (3) If you remove, say, half of the entries above, does it highlight properly? – Win Dec 15 '18 at 01:36
  • I've encountered vaguely similar issues in other modes which turned out to be bugs triggered by rather non-obvious circumstances. My best advice is to come up with a minimal test case -- use @Win's approach recursively to narrow it down as far as possible, and then update the question with the text of the test case. – phils Dec 09 '19 at 04:42
  • Does `` `font-lock-fontify-buffer` `` fix the wrong fontification when this fontification bug happens? (Note, that this function will take a while to finish for such a large buffer.) – Tobias Nov 22 '22 at 15:31

1 Answers1

0

When that happens to me, this helps :

(defun my-find-file-check-make-large-file-read-only-hook ()
  "If a file is over a given size, make the buffer read only."
  (when (> (buffer-size) (* 30 1024 1024))
    (setq buffer-read-only t)
    (buffer-disable-undo)
    (fundamental-mode)))

(add-hook 'find-file-hooks 'my-find-file-check-make-large-file-read-only-hook)

OK, you can't edit the file, but at least you can read it without crashing Emacs.

Combine it with

(setq large-file-warning-threshold (* 35 1024 1024))

And finally, maybe vlfi can help.

yPhil
  • 963
  • 5
  • 22
  • 1
    If I understand this correctly, this is the opposite of what I want to do, since fundamental-mode will disable highlighting of the file altogether, whereas I want syntax highlighting to work. I'm also not having problems with emacs crashing. It's not that I want to view a file, (I can do that with less or something), but edit it, so disabling editing is a bad idea. – Jonathan Jul 23 '17 at 19:10