See https://lists.gnu.org/archive/html/emacs-devel/2015-12/msg01046.html for more background.
YAML defines a comment as a #
either preceded by the beginning of the line or whitespace (more specifically, TAB
or SPC
). This means that foo#bar
is a normal token, not foo
with a #bar
comment. You can get most of the way there with the traditional syntax rules:
(modify-syntax-entry ?# "<")
(modify-syntax-entry ?\n ">")
Unfortunately this will highlight foo#bar
incorrectly. I've been pointed to syntax-propertize-function
which allows for more powerful syntax rules, but behaves weirdly. Depending on the specific code I write, either insertion of new characters or deletion invalidates the existing fontification.
Example 1 (marking #
preceded by non-whitespace as _
):
(modify-syntax-entry ?# "<")
(modify-syntax-entry ?\n ">")
;; invalidated by deletion
(syntax-propertize-rules
("[^ \t]#" (0 "_")))
Example 2 (marking #
preceded by whitespace as <
):
(modify-syntax-entry ?\n ">")
;; invalidated by insertion
(syntax-propertize-rules
("^#" (0 "<")))
("[ \t]#" (0 "<")))
If I rewrite either of these in terms of a function scanning for appliable parts and edebug it, edebug does not take me through the function for the invalidating case. Any idea why?
Bonus - Syntax propertize functions:
Example 1:
(defun yaml-mode-syntax-propertize-function (beg end)
(save-excursion
(goto-char beg)
(while (search-forward "#" end t)
(save-excursion
(forward-char -2) ;; move to char preceding #
(when (looking-at "[^ \t]")
(forward-char 1) ;; move to # again
(put-text-property (point) (1+ (point))
'syntax-table (string-to-syntax "_"))))))
Example 2:
(defun yaml-mode-syntax-propertize-function (beg end)
(save-excursion
(goto-char beg)
(while (search-forward "#" end t)
(save-excursion
(forward-char -1)
(if (bolp)
(put-text-property (point) (1+ (point))
'syntax-table (string-to-syntax "<"))
(forward-char -1)
(when (looking-at "[ \t]")
(forward-char 1)
(put-text-property (point) (1+ (point))
'syntax-table (string-to-syntax "<"))))))))
Test file:
keyword: token#token
#comment comment
token #comment