问题
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)?
回答1:
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.
回答2:
(defun re-search-forward-not-in-comment (regexp)
"Search forward first regexp not inside a comment. "
(interactive
(list (read-from-minibuffer "Regexp: ")))
(while (and (re-search-forward regexp nil t 1)
(and (nth 8 (syntax-ppss))(nth 4 (syntax-ppss))))))
来源:https://stackoverflow.com/questions/12815781/emacs-lisp-and-c-mode-when-am-i-in-a-comment-region