简体   繁体   English

Emacs中用于文本搜索的AND运算符

[英]AND operator for text search in Emacs

I am new to Emacs. 我是Emacs的新手。 I can search for text and show all lines in a separate buffer using "Mx occur". 我可以搜索文本,并使用“ Mx when”在单独的缓冲区中显示所有行。 I can also search for multiple text items using OR operator as : one\\|two , which will find lines with "one" or "two" (as explained on Emacs occur mode search for multiple strings ). 我还可以使用OR运算符来搜索多个文本项,例如:one \\ | two,这将找到带有“一”或“二”的行(如Emacs上的出现模式搜索多个字符串所述 )。 How can I search for lines with both "one" and "two"? 如何搜索同时带有“一个”和“两个”的行? I tried using \\& and \\&& but they do not work. 我尝试使用\\&和\\ &&,但是它们不起作用。 Will I need to create a macro or function for this? 我是否需要为此创建宏或函数?

Edit: 编辑:

I tried writing a function for above in Racket (a Scheme derivative). 我试过在Racket(Scheme派生)中为上述函数编写函数。 Following works: 以下作品:

#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")

Ouput: 输出:

'("this line has both one and two keywords" "another two & one words line")

But how can I put this in Emacs Lisp? 但是如何将其放入Emacs Lisp?

Regex doesn't support "and" because it has very limited usefulness and weird semantics when you try to use it in any nontrivial regex. 正则表达式不支持“和”,因为当您尝试在任何非平凡的正则表达式中使用它时,它的用途和有限的语义非常有限。 The usual fix is to just search for one.*two\\|two.*one ... or in the case of *Occur* maybe just search for one and then Mx delete-non-matching-lines two . 通常的解决方法是只搜索one.*two\\|two.*one ...,或者在*Occur*的情况下,可能只搜索one ,然后Mx delete-non-matching-lines two

(You have to mark the *Occur* buffer as writable before you can do this. read-only-mode is a toggle; the default keybinding is Cx Cq . At least in my Emacs, you have to move the cursor away from the first line or you'll get "Text is read-only".) (在执行此操作之前,您必须将*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))))

The save-excursion and other-window is a bit of a wart but it seemed easier than hardcoding the name of the *Occur* buffer (which won't always be true; you can have several occur buffers) or switching there just to fetch the buffer name, then Doing the Right Thing with set-buffer etc. save-excursionother-window有点麻烦,但是似乎比硬编码*Occur*缓冲区的名称(这并不总是正确的;您可以有多个出现的缓冲区)或切换到那里来获取来得容易。缓冲区名称,然后使用set-buffer做正确的事情等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM