Emacs lisp and c-mode: when I'm in the comments area

I would like to search for regular expressions in the c / C ++ buffer, but I want to avoid the expression corresponding to the comment area. Is there a way using c mode to find out if a bunch of text is in the comments area (or a dot is in the comments area)?

+8
emacs elisp
source share
2 answers

A way to figure this out is with syntax-ppss , which works in C / C ++ and most major modes. For example. (null (nth 8 (syntax-ppss))) will be non-zero if and only if you are not in a line or comment.

+10
source share
 (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)))))) 
0
source share

All Articles