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

余生长醉 提交于 2019-12-23 07:56:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!