branch: elpa/rainbow-delimiters
commit 1305bca532f482f248814f0b918b0989a6c12dd4
Author: Fanael Linithien <[email protected]>
Commit: Fanael Linithien <[email protected]>
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.
---
rainbow-delimiters.el | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/rainbow-delimiters.el b/rainbow-delimiters.el
index 940ee1bb30..0e8a08b267 100644
--- a/rainbow-delimiters.el
+++ b/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)