[英]AND operator for text search in Emacs
我是Emacs的新手。 我可以搜索文本,并使用“ Mx when”在单独的缓冲区中显示所有行。 我还可以使用OR运算符来搜索多个文本项,例如:one \\ | two,这将找到带有“一”或“二”的行(如Emacs上的出现模式搜索多个字符串所述 )。 如何搜索同时带有“一个”和“两个”的行? 我尝试使用\\&和\\ &&,但是它们不起作用。 我是否需要为此创建宏或函数?
我试过在Racket(Scheme派生)中为上述函数编写函数。 以下作品:
#lang racket
(define text '("this is line number one"
"this line contains two keyword"
"this line has both one and two keywords"
"this line contains neither"
"another two & one words line"))
(define (srch . lst) ; takes variable number of arguments
(for ((i lst))
(set! text (filter (λ (x) (string-contains? x i)) text)))
text)
(srch "one" "two")
输出:
'("this line has both one and two keywords" "another two & one words line")
但是如何将其放入Emacs Lisp?
正则表达式不支持“和”,因为当您尝试在任何非平凡的正则表达式中使用它时,它的用途和有限的语义非常有限。 通常的解决方法是只搜索one.*two\\|two.*one
...,或者在*Occur*
的情况下,可能只搜索one
,然后Mx delete-non-matching-lines two
。
(在执行此操作之前,您必须将*Occur*
缓冲区标记为可写。 read-only-mode
是一个切换;默认键绑定是Cx Cq
。至少在我的Emacs中,您必须将光标从第一个移开行,否则您将获得“文本为只读”。)
(defun occur2 (regex1 regex2)
"Search for lines matching both REGEX1 and REGEX2 by way of `occur'.
We first (occur regex1) and then do (delete-non-matching-lines regex2) in the
*Occur* buffer."
(interactive "sFirst term: \nsSecond term: ")
(occur regex1)
(save-excursion
(other-window 1)
(let ((buffer-read-only nil))
(forward-line 1)
(delete-non-matching-lines regex2))))
save-excursion
和other-window
有点麻烦,但是似乎比硬编码*Occur*
缓冲区的名称(这并不总是正确的;您可以有多个出现的缓冲区)或切换到那里来获取来得容易。缓冲区名称,然后使用set-buffer
做正确的事情等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.