1

I am new to Emacs and I am trying to search for all words entered one by one using 'occur'. I am trying following code:

(defun occurlist (ss)
    (interactive "s\Enter list of search items: ")
    (let (lst (split-string ss))
        (message "In let block; lst=%s" lst)
        (while lst
            (occur (pop lst)))))

I enter the search words, e.g.: world bank report. It should search for these words one by one sequentially.

However, it is not working. There is no error message. Starting emacs with command "emacs --debug-init" also does not show any error message.

Where is the error and how can it be solved? Thanks for your help.

Edit: I added message in let block. It reveals that lst=nil at that location. Hence it is not entering the while loop. Why is lst not getting value of (split-string ss) in let block?

Correcting (let (lst (split-string ss)) to (let ((lst (split-string ss))) leads to function showing search for last word only.

2
  • Pl see the edit in my question above.
    – rnso
    Commented Apr 9, 2017 at 2:58
  • I want to sequentially find words, hence I want to search for next word in output of previous occur command. It is like 'and' operation for search.
    – rnso
    Commented Apr 9, 2017 at 3:00

1 Answer 1

1

Your let is malformed, for one thing. It should be (let ((lst (split-string ss)))...). What you wrote instead binds variable lst to nil and variable split-string to ss.

Your second problem is that subsequent invocations of occur still search the original buffer, not the output Occur buffer. So all you see is the result of the last search of the original buffer. You need to switch to the output buffer after each search.

Something like this:

(defun occurlist (ss)
  (interactive "s\Enter list of search items: ")
  (let ((lst (split-string ss)))
    (while lst
      (occur (pop lst))
      (set-buffer next-error-last-buffer))))

But subsequent occur invocations use new buffers *Occur<N>*. If you don't want to keep the intermediate output buffers, then this will kill them:

(defun occurlist (ss)
  (interactive "s\Enter list of search items: ")
  (let ((lst  (split-string ss))
        lastbuf)
    (while lst
      (occur (pop lst))
      (when lastbuf (kill-buffer lastbuf))
      (set-buffer (setq lastbuf  next-error-last-buffer)))))
3
  • However, correcting it also does not work fully. It runs but shows lines containing last word only, not lines with all words.
    – rnso
    Commented Apr 9, 2017 at 4:10
  • Because the original buffer is searched again.
    – Drew
    Commented Apr 9, 2017 at 4:11
  • How do I change to new buffer?
    – rnso
    Commented Apr 9, 2017 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.