Use comment-search-forward to see if a delimiter starts a comment.

(looking-at comment-start-skip) is not always enough, because some
major modes set `comment-start-skip' to not exactly what we expect.

Fixes #35.
master
Fanael Linithien 12 years ago
parent df4976a965
commit 1305bca532
  1. 18
      rainbow-delimiters.el

@ -96,6 +96,7 @@
;; - Intelligent support for other languages: Ruby, LaTeX tags, et al.
;;; Code:
(require 'newcomment)
;;; Customize interface:
@ -437,8 +438,19 @@ Returns t if char at loc meets one of the following conditions:
(nth 4 ppss) ; inside comment?
(save-excursion ; starting a comment?
(goto-char loc)
(let ((inhibit-changing-match-data t))
(looking-at comment-start-skip)))
(and
;; Fast path: if this test fails, it's not a comment, and we avoid a
;; costly `comment-search-forward' call.
(let ((inhibit-changing-match-data t))
(looking-at comment-start-skip))
;; Some major modes set `comment-start-skip' to not exactly what we
;; expect, catching more than actual comments. Use
;; `comment-search-forward' to see if it's really a comment.
;; NOTE: is lookahead of five characters enough for all languages? I hope
;; there's no language with 6-character comment delimiters.
(save-match-data
(let ((comment-start-pos (comment-search-forward (min (+ 5 loc) (point-max)) t)))
(and comment-start-pos (= loc comment-start-pos))))))
(and rainbow-delimiters-escaped-char-predicate
(funcall rainbow-delimiters-escaped-char-predicate loc))))
@ -531,6 +543,8 @@ Used by font-lock for dynamic highlighting."
(defun rainbow-delimiters-mode-turn-on ()
"Set up `rainbow-delimiters-mode'."
;; Necessary for our use of `comment-search-forward'.
(comment-normalize-vars t)
;; Flush the ppss cache now in case there's something left in there.
(setq rainbow-delimiters-parse-partial-sexp-cache nil)
(add-hook 'before-change-functions 'rainbow-delimiters-syntax-ppss-flush-cache t t)

Loading…
Cancel
Save