Discussion:
How to yank a regexp in isearch-mode?
Zhang Haijun
2018-11-11 00:46:47 UTC
Permalink
Is there a functions like isearch-yank-string but yank a regexp?

(defun my-isearch-foobar ()
(interactive)
(isearch-mode t)
(isearch-yank-string “ foobar "))
Drew Adams
2018-11-11 02:22:19 UTC
Permalink
Post by Zhang Haijun
Is there a functions like isearch-yank-string but yank a regexp?
(defun my-isearch-foobar ()
(interactive)
(isearch-mode t)
(isearch-yank-string " foobar "))
`isearch-yank-string' can yank a regexp string as well.

If you use Isearch+ then you have option
`isearchp-regexp-quote-yank-flag':

Non-nil means escape special chars in text yanked for a regexp isearch.
You can toggle this using `isearchp-toggle-regexp-quote-yank', bound
to `C-`' during Isearch.

IOW, if you turn off this escaping then regexp search
interprets yanked regexps normally. If you turn it on
then a yanked regexp string has its special chars
quoted (escaped).

With this escaping turned off, you can yank text such
as `^\*.*' without it being transformed to `\^\\\*\.\*'.

Isearch+:

https://www.emacswiki.org/emacs/IsearchPlus
Drew Adams
2018-11-11 17:13:59 UTC
Permalink
Post by Drew Adams
`isearch-yank-string' can yank a regexp string as well.
But it escape the string if isearch-regexp is not nil.
Yes. That's why Isearch+ adds `isearchp-regexp-quote-yank-flag'
(and a key to toggle that during Isearch): to give you a choice
(control).
Post by Drew Adams
If you use Isearch+ then you have option
`isearchp-regexp-quote-yank-flag':...
With this escaping turned off, you can yank text such
as `^\*.*' without it being transformed to `\^\\\*\.\*'.
https://www.emacswiki.org/emacs/IsearchPlus
I see it. It is a big package. It is too big for my use case. I just
want to yank a regexp.
It's not very big, if you remove the Commentary. But
yes, it's purpose is not just to let you yank regexp.

If you don't want to use Isearch+ then just advise or
redefine `isearch-yank-string', like Isearch+ does.
It's trivial to do. E.g., change this:

(if isearch-regexp (setq string (regexp-quote string)))

to this:

(when (and isearch-regexp your-variable)
(setq string (regexp-quote string)))

Or define a separate command that yanks without doing
`regexp-quote' - same defun but without that line
(if regexp-quote...). And bind that command to a different
key.

IOW, either use a variable with two values (and maybe a
toggle command) or two different keys. Pretty simple.
Zhang Haijun
2018-11-12 10:43:01 UTC
Permalink
Post by Drew Adams
Post by Drew Adams
`isearch-yank-string' can yank a regexp string as well.
But it escape the string if isearch-regexp is not nil.
Yes. That's why Isearch+ adds `isearchp-regexp-quote-yank-flag'
(and a key to toggle that during Isearch): to give you a choice
(control).
Post by Drew Adams
If you use Isearch+ then you have option
`isearchp-regexp-quote-yank-flag':...
With this escaping turned off, you can yank text such
as `^\*.*' without it being transformed to `\^\\\*\.\*'.
https://www.emacswiki.org/emacs/IsearchPlus
I see it. It is a big package. It is too big for my use case. I just
want to yank a regexp.
It's not very big, if you remove the Commentary. But
yes, it's purpose is not just to let you yank regexp.
If you don't want to use Isearch+ then just advise or
redefine `isearch-yank-string', like Isearch+ does.
(if isearch-regexp (setq string (regexp-quote string)))
(when (and isearch-regexp your-variable)
(setq string (regexp-quote string)))
Or define a separate command that yanks without doing
`regexp-quote' - same defun but without that line
(if regexp-quote...). And bind that command to a different
key.
IOW, either use a variable with two values (and maybe a
toggle command) or two different keys. Pretty simple.
Loading...