How to detect if the point is within a comment area?

share|improve this question
up vote 15 down vote accepted

Check the fourth* value in the list returned by syntax-ppss:

(nth 4 (syntax-ppss))

It's nil if point is outside any comment, t if inside a non-nestable comment, or an integer (the nesting depth) if inside a nestable comment. See the docstring for parse-partial-sexp for more details.

* Zero-based.

share|improve this answer
    
Perfect, is there a documentation for all other information thatsyntax-ppss provides? – Name Jul 27 '15 at 12:55
2  
Yes, it's in the docstring for parse-partial-sexp. – legoscia Jul 27 '15 at 12:59
2  
@Name: the docstring for syntax-ppss will point you to parse-partial-sexp, the latter of which will give you a description of all of the stuff returned by these functions. Hopefully that helps for a start. – Dan Jul 27 '15 at 13:01
1  
See also section 34.6 "Parsing Expressions" in the Emacs Lisp Manual. – Sue D. Nymme Jul 27 '15 at 13:43

use the font-face, this is the trick I learned from flyspell.

I tried syntax-ppss two years ago, it does not work for two reason:

  • not work on edge of comment (comment limit), for example, for comment like // this is comment in c++-mode, if you place the cursor over the / character, the result of (nth 4 (syntax-ppss)) is nil.

  • not work at all in major-mode like web-mode

Here is code I copied from flyspell:

(defun evilnc--in-comment-p (pos)
  (interactive)
  (let ((fontfaces (get-text-property pos 'face)))
    (when (not (listp fontfaces))
      (setf fontfaces (list fontfaces)))
    (delq nil
          (mapcar #'(lambda (f)
                      ;; learn this trick from flyspell
                      (or (eq f 'font-lock-comment-face)
                          (eq f 'font-lock-comment-delimiter-face)))
                  fontfaces))))

Please note the code could be extended to support new major modes by fuzz matching font face.

I've used this trick for about three years without failure. Besides, considering flyspell is widely used for so long, I could claim this method is reliable.

See Which keyboard shortcut to use for navigating out of a string for similar question.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.