all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to get a fixed search string into the editable area of an isearch.
@ 2016-05-31 19:13 Robert Weiner
  2016-06-01  5:07 ` Tom
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Robert Weiner @ 2016-05-31 19:13 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1186 bytes --]

Hi:

I have a need for a command that starts an isearch with a specific string
in the editable area that I can add and subtract characters from
interactively during the search.  I have things working with the following
code except the string does not appear in the editable area even though the
search is done for the fixed string.  Can anyone solve this?  I couldn't
get any of the isearch-yank-* commands to do the right thing here either.
Please help.  Thanks.

It would be much simpler if the user-level isearch commands like
isearch-forward could take a string/regexp as an argument and then jump
into the middle of a search as if the characters had been typed
interactively.  One might think setting isearch-string would do something
similar.

(defun isearch-for-string ()
  "Interactively search forward for next occurrence of a fixed string.
Then add characters to further narrow the search."
  (interactive)
  (let* ((match-str "fixed-string")
         (isearch-mode-hook
 (append '((lambda () (interactive) (setq isearch-string match-str))
   isearch-mode-hook))))
    (if (not (equal match-str (car search-ring)))
(isearch-update-ring match-str nil))
    (isearch-forward)))

[-- Attachment #2: Type: text/html, Size: 1585 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to get a fixed search string into the editable area of an isearch.
  2016-05-31 19:13 How to get a fixed search string into the editable area of an isearch Robert Weiner
@ 2016-06-01  5:07 ` Tom
  2016-06-01 22:21 ` Michael Heerdegen
  2016-06-04 21:46 ` Juri Linkov
  2 siblings, 0 replies; 5+ messages in thread
From: Tom @ 2016-06-01  5:07 UTC (permalink / raw)
  To: emacs-devel

Robert Weiner <rswgnu <at> gmail.com> writes:

> I have a need for a command that starts an isearch with
> a specific string in the editable area 

How about this:

(progn
  (setq unread-command-events
        (append unread-command-events '(?t ?e ?s ?t)))
  (isearch-forward))





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to get a fixed search string into the editable area of an isearch.
  2016-05-31 19:13 How to get a fixed search string into the editable area of an isearch Robert Weiner
  2016-06-01  5:07 ` Tom
@ 2016-06-01 22:21 ` Michael Heerdegen
  2016-06-04 21:46 ` Juri Linkov
  2 siblings, 0 replies; 5+ messages in thread
From: Michael Heerdegen @ 2016-06-01 22:21 UTC (permalink / raw)
  To: emacs-devel; +Cc: Robert Weiner

Robert Weiner <rswgnu@gmail.com> writes:

> I have a need for a command that starts an isearch with a specific
> string in the editable area that I can add and subtract characters
> from interactively during the search. I have things working with the
> following code except the string does not appear in the editable area
> even though the search is done for the fixed string. Can anyone solve
> this? I couldn't get any of the isearch-yank-* commands to do the
> right thing here either. Please help. Thanks.

I'm just a user like you, but figured out some missing elements I think.
Try this:

#+begin_src emacs-lisp
(defun my-isearch-for-string ()
  (interactive)
  (let ((isearch-mode-hook
         (cons (lambda ()
                 (setq isearch-string "test"
                       isearch-message "test"
                       isearch-yank-flag t)
                 (isearch-search-and-update))
               isearch-mode-hook)))
    (isearch-forward)))
#+end_src

You probably want to add your updating of the search ring and other
things.

AFAICT, the text displayed in the minibuffer is (based on) the current
binding of the variable `isearch-message', and it is up to you to update
it properly (it doesn't happen automatically).

`isearch-search-and-update' triggers, yes, what it says.

I'm not sure why isearch-yank-flag -> t was useful, a comment in my code
says

|    ;; Don't move cursor in reverse search.


BTW, in your code:

#+begin_src emacs-lisp
(let* ((match-str "fixed-string")
       (isearch-mode-hook
        (append '((lambda () (interactive) (setq isearch-string match-str)))
                isearch-mode-hook))))
#+end_src

there is a quoted lambda; it's better to avoid quoted lambdas for
several reasons.


HTH,

Michael.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to get a fixed search string into the editable area of an isearch.
  2016-05-31 19:13 How to get a fixed search string into the editable area of an isearch Robert Weiner
  2016-06-01  5:07 ` Tom
  2016-06-01 22:21 ` Michael Heerdegen
@ 2016-06-04 21:46 ` Juri Linkov
  2016-06-04 22:42   ` Robert Weiner
  2 siblings, 1 reply; 5+ messages in thread
From: Juri Linkov @ 2016-06-04 21:46 UTC (permalink / raw)
  To: Robert Weiner; +Cc: emacs-devel

> I have a need for a command that starts an isearch with a specific string
> in the editable area that I can add and subtract characters from
> interactively during the search.  I have things working with the following
> code except the string does not appear in the editable area even though the
> search is done for the fixed string.  Can anyone solve this?  I couldn't
> get any of the isearch-yank-* commands to do the right thing here either.
> Please help.  Thanks.
>
> It would be much simpler if the user-level isearch commands like
> isearch-forward could take a string/regexp as an argument and then jump
> into the middle of a search as if the characters had been typed
> interactively.  One might think setting isearch-string would do something
> similar.
>
> (defun isearch-for-string ()
>   "Interactively search forward for next occurrence of a fixed string.
> Then add characters to further narrow the search."
>   (interactive)
>   (let* ((match-str "fixed-string")
>          (isearch-mode-hook
>  (append '((lambda () (interactive) (setq isearch-string match-str))
>    isearch-mode-hook))))
>     (if (not (equal match-str (car search-ring)))
> (isearch-update-ring match-str nil))
>     (isearch-forward)))

Maybe this is what you need?

  (defun isearch-for-string ()
    (interactive)
    (isearch-forward nil 1)
    (isearch-yank-string "test"))



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to get a fixed search string into the editable area of an isearch.
  2016-06-04 21:46 ` Juri Linkov
@ 2016-06-04 22:42   ` Robert Weiner
  0 siblings, 0 replies; 5+ messages in thread
From: Robert Weiner @ 2016-06-04 22:42 UTC (permalink / raw)
  To: Juri Linkov; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 898 bytes --]

On Sat, Jun 4, 2016 at 5:46 PM, Juri Linkov <juri@linkov.net> wrote:

>
> Maybe this is what you need?
>
>   (defun isearch-for-string ()
>     (interactive)
>     (isearch-forward nil 1)
>     (isearch-yank-string "test"))
>

Thank you.  That is a nice clean-looking solution.  I had tried
isearch-yank-string but missed the recursive-minibuffer argument to the
isearch-forward, without which it doesn't work at all which led me to think
that isearch-yank-string was not the right approach.  The only limitation
with this solution is that if you start to delete search characters, the
entire search string provided to the yank is deleted at once rather than
character by character.  A pretty minor limitation which is resolved by the
first solution, using unread-command-events.

Thanks to all, these have been very helpful and definitely should find some
place in the isearch documentation.

Bob

[-- Attachment #2: Type: text/html, Size: 1337 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-06-04 22:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-31 19:13 How to get a fixed search string into the editable area of an isearch Robert Weiner
2016-06-01  5:07 ` Tom
2016-06-01 22:21 ` Michael Heerdegen
2016-06-04 21:46 ` Juri Linkov
2016-06-04 22:42   ` Robert Weiner

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.