I'm trying to write a major-mode that highlights triple-quoted strings. Here's a minimal reproducible example:
(defconst demo-triple-quoted-string-regex
(rx "\"\"\""
;; After the delimiter, we're a sequence of
;; non-backslashes or blackslashes paired with something.
(*? (or (not (any "\\"))
(seq "\\" anything)))
"\"\"\""))
(defun demo-stringify-triple-quote ()
"Put `syntax-table' property on triple-quoted strings."
(let* ((string-literal (match-string 0))
(string-start-pos (- (point) (length string-literal)))
(string-end-pos (point)))
(unless (nth 4 (syntax-ppss)) ;; not inside comment
(put-text-property string-start-pos string-end-pos
'syntax-table (string-to-syntax "|")))))
(defconst demo-syntax-propertize-function
(syntax-propertize-rules
(demo-triple-quoted-string-regex
(0 (ignore (demo-stringify-triple-quote))))))
(define-derived-mode demo-mode prog-mode "Demo"
"Major mode showing stack overflow question."
(set (make-local-variable 'font-lock-defaults) '(()))
(set (make-local-variable 'syntax-propertize-function)
demo-syntax-propertize-function))
However, this leads to really bizarre behaviour when modifying the buffer. Here's my buffer contents:
dodgy when we put a newline after babel
"""
a
"
babel
"""
x = 1
M-x demo-mode
gives correct highlighting:
but pressing enter suddenly gives this:
What am I doing wrong?
forward-sexp
.syntax-ppss
keeps track of this. Take a look how it's done inpython.el
.