0

I am using highlight-regexp with a regexp pattern that includes the elisp comment characters ;;. How can I extend the pattern so that it adequately handle the general comment character for each language?

 (highlight-regexp
   "^;;\s+\\(\\[.+\\]\\).*$" 'kmface 1))

I have found comment-start but do not know how to use it in the regexp

"^;;\s+\\(\\[.+\\]\\).*$"

Have started with format to try getting two comment characters next to each other to construct a regexp.

(let ((fm (format "%s%s" comment-start comment-start)))
    (message "fm: %s" fm))
Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

1

You asked the same question on help-gnu-emacs@gnu.org. Thibaut Verron gave you the answer there. Repeating it here, for the benefit of others:

Use \s< to match the comment-start character, whatever it may be in the current mode. If you want two successive comment-start chars, use \s<\{2\}.

So your example would be:

(highlight-regexp "^\\s<+\\(\\[.+\\]\\).*$" 'kmface 1))

Or if you want only two comment-start chars, as you later stipulated, then:

(highlight-regexp "^\\s<\\{2\\}\\(\\[.+\\]\\).*$" 'kmface 1))

(You need to double backslash chars in Lisp strings.)

See the Elisp manual, node Regexp Backslash, which tells you that \sC matches a character with syntax class C. And see node Syntax Class Table, which tells you that < designates the comment-start syntax class.

So \s< matches a comment-start character.

Drew
  • 75,699
  • 9
  • 109
  • 225