Search code examples
emacselisp

emacs lisp and c-mode: when am I in a comment region


I'd like to search for regular expressions within a c/c++ buffer, but I want to avoid expression matching a comment region. Is there a way using the c mode to know if a bunch of text is within a comment region (or a point is within a comment region)?


Solution

  • The way to figure that out is with syntax-ppss which works in C/C++ and most major modes. E.g. (null (nth 8 (syntax-ppss))) will be non-nil if and only if you're not within a string-or-comment.