[英]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.