in cperl-mode, here docs are colored with a font-lock-string-face. However if you used any function that inserts a newline on a blank line (e.g. M-: (insert "\n")) it will cause all code after the newline to be highlighted with font-lock-comment-face. The only way to fix it is try and "mess with" here doc tags so that cperl re-parses and corrects the font-lock. I have a feeling this may have something to do with this issue since it is also dealing with multi-line syntax highlighting. Is there an expert out there who can help me find a way to fix this?

#!/usr/bin/env perl

my $here_doc = <<'_HERE_';
this is here doc line
<--- call (insert "\n") when cursor is here
  • That's a bug in cperl-mode. I recommend you M-x report-emacs-bug (BTW, it's not just on empty lines: it seems to apply whenever you insert the newline at BOL). In the mean time, you might like to use perl-mode which doesn't seem to suffer from this bug.
    – Stefan
    Commented Feb 18, 2017 at 23:02

2 Answers 2


Eventually this bug has been fixed in the Emacs repository. You can use cperl-mode.el directly from that repository with Emacs 26.1 or newer.

The fix does what could have been done in font-lock-extend-region-functions as suggested by wasamasa and now uses the same method as perl-mode.el to mark here-documents.


The package highlight-refontification visualizes how font-lock refontifies a buffer. In this case, it starts at the line where the point is.

One way to handle this is to add a function to font-lock-extend-region-functions to expand the region to include the full heredoc comment.

One thing that is curious is that the highlighting becomes correct when pressing return but not when evaluating (insert "\n"), something I have no explanation for.

  • Hitting RET will run at the very least newline which does a lot more than (insert "\n"), so this doesn't surprise me at all.
    – wasamasa
    Commented Feb 17, 2017 at 21:55
  • How do I add my font-lock-extend-region-function that I wrote to the font-lock-extend-region-functions? Every example I could find was of someone creating a new major mode. There is no cperl specific hook. Commented Feb 27, 2017 at 20:40
  • You can use (add-to-list 'font-lock-extend-region-functions #'my-extend-something). If can call this from a function in cperl-mode-hook. Commented Mar 1, 2017 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.