6

I am trying to set isearch-string from the command line. The purpose is to have a Perl script start Emacs with a given search string, and go directly to the first line of the string and enter search mode. For example:

$ emacs +2 t.txt -l srep.el &

with t.txt the text file to search in, for example here is a dummy file:

a bb c
aa
aa bb c d

The Lisp file srep.el (generated by the Perl script) sets the isearch-string, for example:

(setq isearch-string "aa")
(isearch-search-and-update)

However, this does not work. I get a wrong-type-argument error from the debugger. I then modified srep.el to :

(defun my-set-search-word()
  (interactive)
  (setq isearch-string "aa"))

(add-hook 'isearch-mode-hook 'my-set-search-word)

This works in the beginning. I can press C-s to search for the string aa, but if I try to escape isearch-mode (press arrow up key for example) and do some editing and later try to search for another string (by pressing C-s again) for example the string b. I get error Failing I-search.

I tried to solve this by changing srep.el to:

(defun my-set-search-word()
  (interactive)
  (if *my-search-str-flag*
      (setq isearch-string "aa")
    (setq *my-search-str-flag* nil)))

(defun my-set-search-word()
  (interactive)
  (setq isearch-string "aa"))

(defvar *my-search-str-flag* t)
(add-hook 'isearch-mode-hook 'my-set-search-word)

But I still get same behavior. I am using Emacs 24.3 on Ubuntu 14.04.

1 Answer 1

7

What I recommend you to try is to start Isearch first and then yank your word to the search string, effectively doing the same what you would do manually to add words in isearch-mode:

(defun my-search-word ()
  (interactive)
  (isearch-forward nil 1)
  (isearch-yank-string "aa"))

(my-search-word)

If you need to match word boundaries, then try this:

(defun my-search-word ()
  (interactive)
  (isearch-forward-word nil 1)
  (isearch-yank-string "aa"))

(my-search-word)
3
  • Thanks this seems to work! (I tried to look up the documentation for isearch-forward using M-x describe-function. It says (isearch-forward &optional REGEXP-P NO-RECURSIVE-EDIT) but I could not find a description of REGEX-P and NO-RECURSIVE-EDIT in the documentation. What is the purpose of these arguments?) Oct 28, 2014 at 11:38
  • 1
    REGEX-P and NO-RECURSIVE-EDIT are documented in the docstring of isearch-mode in the recently released version 24.4. The non-nil arg REGEXP-P does an incremental regular expression search. When the arg NO-RECURSIVE-EDIT is nil, this function behaves modally and does not return to the calling function until the search is completed. To behave this way it enters a recursive-edit and exits it when done isearching.
    – link0ff
    Oct 28, 2014 at 11:42
  • Thanks! I am using version 24.3. I also found that I could run sudo apt-get install emacs24-el. Then there will be a link to the source code of isearch-forward when you run describe-function on it.. Oct 28, 2014 at 12:40

Your Answer

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

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