regex - AND operator for text search in Emacs -
i new emacs. can search text , show lines in separate buffer using "m-x occur". can search multiple text items using or operator : one\|two , find lines "one" or "two" (as explained on emacs occur mode search multiple strings). how can search lines both "one" , "two"? tried using \& , \&& not work. need create macro or function this?
edit:
i tried writing function above in racket (a scheme derivative). following works:
#lang racket (define text '("this line number one" "this line contains 2 keyword" "this line has both 1 , 2 keywords" "this line contains neither" "another 2 & 1 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 1 , 2 keywords" "another 2 & 1 words line")
but how can put in emacs lisp?
regex doesn't support "and" because has limited usefulness , weird semantics when try use in nontrivial regex. usual fix search one.*two\|two.*one
... or in case of *occur*
maybe search one
, m-x delete-non-matching-lines two
.
(you have mark *occur*
buffer writable before can this. read-only-mode
toggle; default keybinding c-x c-q
. @ least in emacs, have move cursor away first line or you'll "text read-only".)
(defun occur2 (regex1 regex2) "search lines matching both regex1 , regex2 way of `occur'. first (occur regex1) , (delete-non-matching-lines regex2) in *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
, other-window
bit of wart seemed easier hardcoding name of *occur*
buffer (which won't true; can have several occur buffers) or switching there fetch buffer name, doing right thing set-buffer
etc.
Comments
Post a Comment