Here's an alternative "quick-fix" solution using the built-in hi-lock
package.
The solution is to put special Hi-Lock
keywords in a comment at the top of the file (just like file-local variable comments starting with -*-
). But first we need to configure hi-lock
to automatically detect these special keywords without asking any questions.
Step 1: Minor config for hi-lock-mode
Add the below to your config.
(require 'hi-lock)
;; Don't ask before highlighting any Hi-Lock: pattern found in a file
;; Below, (lambda (pattern) t) simply always returns `t' regardless of
;; what the `pattern' input is.
(setq hi-lock-file-patterns-policy (lambda (pattern) t))
(global-hi-lock-mode 1) ; Enable the detection of the Hi-Lock keywords globally
Step 2: Put those keywords in your file
Provide the format for the regexp to match and the face to set the regexp groups to, using the font-lock-keywords
format. You can learn more about this format by doing C-h v font-lock-keywords
.
Here's an example that I tried out:
# Hi-lock: (("\\(^[^&\n].*$\\)" (0 '(:inherit font-lock-comment-face) prepend)))
# Hi-Lock: end
this is a comment
& this is not a comment
& this is not a comment even if there's a blank line above this line
# this is a comment
& not a comment
& this is a comment too as the line is preceded with spaces
! blah
% blah
12312312
Outcome
Here is how the comment face is rendered (font face is based on the theme you use):

Addendum
Here is a gist of the font-lock-keywords
format from my personal notes:
;; The Hi-Lock regexp forms are in the form of font lock keywords. Do
;; `C-h v font-lock-keywords' to learn more.
;; Hi-Lock: (("<REGEXP>" (<SUBEXP-0> '<FACE-0> [<OVERRIDE> [<LAXMATCH>]])
;; (<SUBEXP-1> '<FACE-1> [<OVERRIDE> [<LAXMATCH>]])
;; .. ))
;; Hi-Lock: end
;; OVERRIDE and LAXMATCH are flags.
;; If OVERRIDE is t, existing fontification can be overwritten.
;; If `keep', only parts not already fontified are highlighted.
;; If `prepend', existing fontification is merged with the new, in
;; which the new fontification takes precedence.
;; If `append', existing fontification is merged with the new, in
;; which the existing fontification takes precedence.
;; If LAXMATCH is non-nil, that means don't signal an error if there is
;; no match for SUBEXP in REGEXP.
;; Examples of Hi-Lock patterns:
;; Highlight outshine headers in `shell-script-mode':
;; # Hi-lock: (("\\(^\\s< *\\**\\)\\(\\* *.*\\)" (1 'org-hide prepend) (2 '(:inherit org-level-1 :height 1.3 :weight bold :overline t :underline t) prepend)))
;; Highlight outshine headers in `emacs-lisp-mode':
;; ;; Hi-lock: (("\\(^;\\{3,\\}\\)\\( *.*\\)" (1 'org-hide prepend) (2 '(:inherit org-level-1 :height 1.3 :weight bold :overline t :underline t) prepend)))
As bonus, you can also set the hi-lock-file-patterns
variable directly using .dir-locals.el
files if you do not want to put the keywords in the file itself. But for that to work, you would need to put this in your emacs config:
(put 'hi-lock-file-patterns 'safe-local-variable 'identity)
&
character at the beginning. – WJahn Feb 16 '16 at 13:42