In SPARQL, comments starts with a # and follows to the end of the line. The problem is that URI's, which is a big part of SPARQL, can also contain #, but then they are not the start of a comment. Like so <http://example.com/foo#bar>. URI's are always surrounded by <> and never have any spaces in them.

Normally, we would write a syntax-table like this

(defvar sparql-mode-syntax-table
  (modify-syntax-entry ?# "<" syntax-table)
  (modify-syntax-entry ?\n ">" syntax-table))

but this would include the # of a URI. Now when the buffer is font-locked, or we try to move using the defined syntax, from the part from the URI that contains a # is now considered a comment.

How can I make syntax-propertize skip marking # in URI's as beginning of comments?

  • I would like to add syntax-propertize as a tag, but I don't have the reputation yet.
    – ljos
    Nov 16, 2017 at 13:39

1 Answer 1


The way to make syntax-propertize skip part of a buffer is to use syntax-propertize-function.

(defvar syntax-mode-syntax-table
  (let ((syntax-table (make-syntax-table)))
    (modify-syntax-entry ?\n ">")

(defvar sparql-syntax-propertize-function
    ("<\\S-*>" (0 "@"))
    ("#" (0 "<"))))

Since syntax-propertize-rules only propertizes a part of the buffer once and it matches from top to bottom, this will set the syntax-property for a URI (<\\S-*>, a string without space within <>) to inherit and therefore skip any # inside the URI. If it is outside it matches the second rule and is marked as a comment. The syntax table takes care of the comment ending.

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.