[英]Regex search in Emacs for line not starting with semicolon
I am trying to search forward in the current buffer for the first elisp function definition that is not a comment. 我正在尝试在当前缓冲区中向前搜索不是注释的第一个elisp函数定义。 I tried this:
我尝试了这个:
(re-search-forward "[^;] *\(defun ")
but it matches comments anyway. 但还是会与评论匹配。 Like the following line:
像下面这样:
;; (defun test ()
You can use: 您可以使用:
(catch 'found
(let (match)
(while (setq match (search-forward "(defun "))
(when (null (syntax-ppss-context (syntax-ppss)))
(throw 'found match)))
nil))
It relies on the internal parser and the language syntax definition. 它依赖于内部解析器和语言语法定义。 It returns only the result of
search-forward
if point is not in a comment and not in a string. 如果point不在注释中,也不在字符串中,则仅返回
search-forward
结果。 If you do not like the error in the case of a search without hits you can add nil t
to the arguments of the search-forward
command. 如果您不喜欢搜索没有命中的情况下的错误,可以将
nil t
添加到search-forward
命令的参数中。 Search with re-search-forward
is also fine. 用
re-search-forward
也可以。
This also works for cases like: 这也适用于以下情况:
(defun test (args)
"This function is defined with (defun test (args) ...)"
)
(defun test ()
的空格实际上与[^;]
匹配。由于您具有*
,因此不需要另一个空格。您可能需要使用[^;] +
。但是,可以在
\(?<!;;)
This seems to work nicely for me (tested in *scratch*): 这对我来说似乎很好(在* scratch *中测试):
(re-search-forward "^ *\(defun " nil t) ; hit C-x C-e after that
; closing parenthesis
;; (defun hi () )
(defun hola () )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.