benma / visual-regexp.el

A regexp/replace command for Emacs with interactive visual feedback
392 stars 28 forks source link

Use of ,(elisp) in replacement? #30

Closed ddoherty03 closed 8 years ago

ddoherty03 commented 8 years ago

Will visual-regexp allow ,(elisp) constructs in the replacement string? Could not find answer in docs, and experiments indicate no, but maybe I'm doing something wrong? Does the use of pcre2el make a difference? That's what I'm using.

Thanks for a great package, by the way.

benma commented 8 years ago

The first line of the README says

visual-regexp for Emacs is like replace-regex

This is the goal. The elisp replacement works, but you have to use \,(elisp), not ,(elisp), just like in replace-regexp. It also works with pcre2el (because it converts the pcre-like regexp format to Emacs-style regexp). If you use the Python engine, you can use Python expressions.

ddoherty03 commented 8 years ago

Ah. Thanks, I'll play with it.

ddoherty03 commented 8 years ago

OK. I've tried to get this to work, but no luck yet. I tried working on a file with a set of numbers, one to a line, to use ([0-9]+) => \,(1+ #1), which should add one to each number found. For vr/query-replace, I used ([0-9]+) => \,(1+ #1) (grouping parens not escaped).

It worked with replace-regexp, but with vr/query-replace, it matched the numbers, but replaces each with the literal elisp expression, i.e., \,(1+ #1).

Am I missing something?

benma commented 8 years ago

Works fine for me, using the pcre2el engine: http://i.imgur.com/wR8ovQ8.png

Does it work if you use the plain Emacs engine? Use vr/select-replace, choose emacs.

ddoherty03 commented 8 years ago

Yes. It works with the emacs engine, but with pcre I get the literal replacement. With the emacs engine, it even previews the result of the elisp call. Nice.

Also seem to get the same thing with the python engine.

benma commented 8 years ago

Which Emacs, visual-regexp, pcre2el versions are you using? Which operating system?

As I have shown in my screenshot, it works as expected for me, so it is hard for me to help.

With the python engine, you can't use emacs expressions. You can use Python expressions, though (hit C-c C-c when entering the replacement, C-c ? for more options).

ddoherty03 commented 8 years ago

OK. I keep investigating. When I invoke vr/select-query-replace, and select pcre2el it works. With just vr/query-replace it fails. I have my keys bound to the latter, which explains why I was getting the behaviour.

I have Ubuntu and am using emacs 24.5.50.1. Version 0.8 of visual-regexp.

benma commented 8 years ago

I think you might have been using the Python engine all along, which is the default engine.

ddoherty03 commented 8 years ago

OK. That nailed it. The customization system appears to set that as the default. I had it set to pcre2el in my init, but the customizations were apparently loaded later. Set it to pcre2el in customize and all is well.

Sorry for the fire drill and thanks for your patience.