7

I use python-mode for my Python code, and every once in a while the syntax highlighter gets confused and thinks everything in the buffer is a string literal. This typically seems to happen when I create a docstring; for a few seconds after I type the quotes, everything after the cursor is highlighted as a string.

Is there any way to prevent this entirely? Or, if not, is there some way to make emacs/python-mode recover more quickly?

ed: To be clear, the incorrect highlighting persists even after the quotes have been closed and the buffer saved. But again, this is not every time, just sometimes.

abingham
  • 927
  • 6
  • 18
  • 5
    Do you mean after you type the opening quotes? Because, in that case, the rest of the buffer *is* a (doc)string. You could solve this by having Emacs automatically insert the closing quotes after your cursor or something. – Tikhon Jelvis Oct 23 '14 at 20:20
  • No, the highlighting persists sometimes for several seconds after the quotes have been closed (though I don't currently have emacs insert those for me.) – abingham Oct 24 '14 at 05:48
  • Ugh, something similar happens to me all the time when I'm in a REPL and I accidentally print a bunch of text. Not sure if it's related, but maybe this issue is more general than just `python-mode`. – purple_arrows Oct 25 '14 at 05:02
  • Interesting point. I tested out c++-mode a bit, and it seems to have the same behavior. If I leave a string literal "open" long enough, the remainder of the buffer gets highlighted as part of the string (which makes perfect sense.) After closing the literal, it can take several seconds for the highlighting to recover. It seems, then, that there's some timer telling emacs to re-parse/-highlight the buffer. – abingham Oct 25 '14 at 11:45
  • Have you tried reporting this bug? – Dmitry Nov 09 '14 at 10:03
  • Not yet. I was hoping to narrow it down a bit before tossing it on the emacs bug tracker. I guess it's about time I did that now, though. And at the same time, I'm not sure it's so much a bug as subtle user error. – abingham Nov 09 '14 at 13:44
  • I think the answer by Stefan explains why this is happening. I find that using electric-pair-mode or paredit-mode makes this problem go away, even though it is an indirect solution to the problem. – nispio May 11 '15 at 23:54

1 Answers1

6

When you modify the buffer, only the lines you modified get re-highlighted right away. If the modification affects highlighting of lines futher down, these get re-highlighted later via an idle timer (i.e. it happens after you stop typing).

This delay is controlled by jit-lock-context-time and defaults to 0.5s. Note that it really means "Emacs has been idle for half a second", so as long as you keep giving Emacs commands without leaving more than half a second between commands, the re-highlighting does not happen.

If you think it takes more than 0.5s and can come up with a recipe that triggers this excessive delay, then you'll want to M-x report-emacs-bug.

Stefan
  • 26,154
  • 3
  • 46
  • 84