benma / visual-regexp-steroids.el

Extends visual-regexp to support other regexp engines
255 stars 14 forks source link

default case insensitive search #12

Closed Xparx closed 9 years ago

Xparx commented 9 years ago

Hi, Is it possible to make vr/isearch-forward and vr/isearch-backward to by default be case insensitive? auto insert (?i) or something similar when the search is initiated?

benma commented 9 years ago

Take a look at case-fold-search. Setting this variable to nil disables case insensitive search.

Xparx commented 9 years ago

Sorry, Maybe I misunderstand you.

What I was wondering was for emacs built in isearch-forwards, for example, I do get the case insensitive search (which I want). What I'm wondering is if for visual-regexp-steroids forward search I could get the same behaviour. How it works now is it that it does not seem to follow the rule based on the case-fold-search variable (I have it set to t so it searches as case insensitive).

When I invoke vr/isearch-forward I have to enter (?i)search term to get case insensitive search. If I understand it correctly it's becouse the search is passed to a python regex enterpriter function that parses it. However I see no flag to control this.

My question was if there is a flag like the case-fold-search but for vr/isearch-forward and backward. For example inserting (?i) automatically at the prompt when searching.

Thanks for your time.

benma commented 9 years ago

I see, thanks for elaborating.

When you do vr/replace with visual-regexp-steroids, you can see the modifiers in the minibuffer in front of the query, like (?im). Those can be toggled with C-c i, C-c m etc, and their defaults can also be configured.

In isearch, I have no control over displaying that part in the minibuffer, so I decided not to include those modifiers in isearch. It would be weird to have the modifiers do their thing but not actually see them in the minibuffer.

In an early version of steroids, I actually did prepend (?i) when case-fold-search was on. See here: https://github.com/benma/visual-regexp-steroids.el/commit/240970f76dfe9bda56b8bb31fcf49c87e8884b7d

 (regexp string ;; (if case-fold-search (concat "(?i)" string) string)

I can't remember why I removed it, but I must assume there was a reason ;)

In any case, you could simply defadvice vr--isearch and prepend (?i) to the regexp if case-fold-search is enabled, to restore that behavior. What do you think?

Xparx commented 9 years ago

Ok, I see. As far as I know isearch has this special feature that it is case insensitive if you type in common letters, however if you type any Capital letters it becomes case sensitive. Might be that that was the reason not to include (?i) by default as the behaviour is not exactly the same in either case.

For me the use case of having (?i) is more commonly used so your suggestion seems like a great addition. Usually I search for something I'm not really sure how I entered it in the buffer. I'm not really an elisp litterate but I think I can figure out how to use your suggestion. defadvice seems like it was made for just such an occasion.

benma commented 9 years ago

I think that just might have been the reason :laughing:

Something like this should work:

(defadvice vr--isearch (around add-case-insensitive (forward string &optional bound noerror count) activate)
  (when (and (eq vr/engine 'python) case-fold-search)
    (setq string (concat "(?i)" string)))
  ad-do-it)
Xparx commented 9 years ago

Nice! Works like a charm =), even the native isearch functionality seems to be working the same with this solution.

Thank you so much for your time and solution.