* Isearch - Incremental search with predefined initial character?
@ 2014-10-04 17:57 BobD
2014-10-04 21:58 ` Drew Adams
2014-10-08 20:49 ` BobD
0 siblings, 2 replies; 4+ messages in thread
From: BobD @ 2014-10-04 17:57 UTC (permalink / raw)
To: help-gnu-emacs
Using 24.3.1 (i386-mingw-nt6.1.7601)
I wish to do incremental searches with the first character pre-defined, i.e. have emacs prepend a character to the characters I enter.
E.g., if I start an i-search, and then enter "abc", emacs will pretend I entered "Xabc".
If I continue the i-search, emacs will seek "Xabc".
I fiddled with isearch-mode-hook, but I couldn't see how to continue searching with the prepended character.
Do I need to fiddle with the "isearch-repeat-..." code?
Do I need to fiddle with other code?
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: Isearch - Incremental search with predefined initial character?
2014-10-04 17:57 Isearch - Incremental search with predefined initial character? BobD
@ 2014-10-04 21:58 ` Drew Adams
2014-10-08 20:49 ` BobD
1 sibling, 0 replies; 4+ messages in thread
From: Drew Adams @ 2014-10-04 21:58 UTC (permalink / raw)
To: BobD, help-gnu-emacs
> I wish to do incremental searches with the first character pre-
> defined, i.e. have emacs prepend a character to the characters I
> enter.
>
> E.g., if I start an i-search, and then enter "abc", emacs will
> pretend I entered "Xabc". If I continue the i-search, emacs will
> seek "Xabc".
Just how do you want to tell Emacs to do this?
1. Do you want to hit a key during Isearch to prepend this prefix?
IOW, on-demand insertion of your string?
2. Do you want a substitute for the normal `C-s', so this prefix is
prepended each time you start Isearch, i.e., for every search?
Is the prefix to prepend always the same (a constant string)?
Your request is not specific. Try to specify it better.
---
1. Did you know that you can yank the last kill into the current
search string, using `C-y'? Based on the code that does that,
here is some code that you can use to yank a constant string
at the end of the search string, when you hit `C-o' - #1, above.
If you use it before typing anything then your prefix will be
inserted before whatever you type.
(defvar my-prefix "XYZ" "My Isearch prefix.")
(defun my-yank-prefix (&optional prefix)
"Yank PREFIX into current search string.
Default is the value of `my-prefix'."
(interactive)
(unless prefix (setq prefix my-prefix))
(isearch-yank-string my-prefix))
(define-key isearch-mode-map "\C-o" 'my-yank-prefix)
---
2. If you want to automatically prepend your prefix each time
you start Isearch - #2, above, then you can do this:
Copy the code for function `isearch-mode' (from isearch.el),
and change the "" binding of `isearch-string' in your copy, so
that that variable is bound instead to your prefix. For example:
(setq isearch-forward forward ; Initialize global vars.
...
isearch-string my-prefix ; <======= Value changed here
...)
It is not usually a great idea to redefine a standard function
this way, but (a) I don't see how advising the function would
get this job done, and (b) I don't think there is a hook that
you can use to accomplish the same thing.
With luck, someone else will suggest a simpler, cleaner solution.
(I would anyway question why you would want to do this.)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Isearch - Incremental search with predefined initial character?
2014-10-04 17:57 Isearch - Incremental search with predefined initial character? BobD
2014-10-04 21:58 ` Drew Adams
@ 2014-10-08 20:49 ` BobD
2014-10-08 22:32 ` Drew Adams
1 sibling, 1 reply; 4+ messages in thread
From: BobD @ 2014-10-08 20:49 UTC (permalink / raw)
To: help-gnu-emacs
I want a conditional substitute for the normal isearch, depending on which key I use to start the search.
Always using the same prefix is OK; I use "{".
After fiddling a while, I came up with this, which does what I want.
I bind [lwindow] to isearch-forward and isearch-repeat-forward.
In the isearch-mode-hook function, I yank a "{" if [lwindow] started the search.
([lwindow] is the left-side Windows key on keyboards made for Windows.)
(defvar my-prefix "{" "My Isearch prefix") ;My one-char prefix
(defun rjd-prebrace () ;Function for isearch-mode-hook
(if (equal (this-command-keys) [lwindow]) ;If invoked by [lwindow]
(isearch-yank-string my-prefix) ; yank the prefix
)
)
(add-hook 'isearch-mode-hook 'rjd-prebrace) ;Plant function in hook list
(global-set-key [lwindow] 'isearch-forward) ;Plant key in maps (Only forward direction)
(define-key isearch-mode-map [lwindow] 'isearch-repeat-forward)
Thank you very much for your help. Pointing me at (isearch-yank-string ...) made this work.
I wish to do this so that I can search for "{abc" without typing the "{" first. Typing something like "\C-s{abc" is exhausting.
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: Isearch - Incremental search with predefined initial character?
2014-10-08 20:49 ` BobD
@ 2014-10-08 22:32 ` Drew Adams
0 siblings, 0 replies; 4+ messages in thread
From: Drew Adams @ 2014-10-08 22:32 UTC (permalink / raw)
To: BobD, help-gnu-emacs
> After fiddling a while, I came up with this, which does what I want.
> ...
> Thank you very much for your help. Pointing me at (isearch-yank-
> string ...) made this work.
You're welcome.
> I wish to do this so that I can search for "{abc" without typing the
> "{" first. Typing something like "\C-s{abc" is exhausting.
You have what you want. And the string you want as a prefix each
time is just one char in this case (`{`). Things would be a lot
more exhausting if the commonly used prefix were a complex string!
Anyway, just FYI, in case you have other things that you sometimes
want to yank into the search string, here are two tips (they require
libraries `isearch+.el' and `second-sel.el'):
1. You can use `C-M-y' to yank the secondary selection to the search
string.
The secondary selection does not change when the region changes,
so it is a good place to hold onto a string that you might use
fairly often but intermittently.
For example, it can be handy for a kind of on-demand replacement:
select something, then `C-M-y' to yank the secondary selection
in place of it (`C-w' first, if you don't use `delete-selection-mode').
You can't yank the region text to replace the region text (!), but
you can yank the secondary selection. And it remains as the same
text until you explicitly change it.
And unlike the region, point is generally nowhere near the secondary
selection, which means the s.s. can be off-window.
Most Emacs users never use the secondary selection. I use it all
the time. My guess is that people don't use it because they have
never bound it to a keyboard key (like `C-M-y'). The only default
key for yanking the s.s. is a mouse key (`M-mouse-2':
`mouse-yank-secondary').
2. You can use `C-x r g' to append the contents of a register to the
search string. This is the same key that is bound globally to
`insert-register'. You are prompted for the register (a character).
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2014-10-08 22:32 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-04 17:57 Isearch - Incremental search with predefined initial character? BobD
2014-10-04 21:58 ` Drew Adams
2014-10-08 20:49 ` BobD
2014-10-08 22:32 ` Drew Adams
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).