Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customise default search style without needing to rebind default keys #83

Closed
rileyrg opened this issue Jan 28, 2021 · 15 comments
Closed

Comments

@rileyrg
Copy link

rileyrg commented Jan 28, 2021

There doesn't seem to be a way to set the default search style. I find literal pretty limiting after getting lazy using other search tools. Ideally, I could set the default in a :custom form . Currently, I'm rebinding C-s which isn't nice.

(use-package ctrlf
      :custom
      (ctrlf-auto-recenter t)
       (ctrlf-mode-bindings
        '(("C-s" . ctrlf-forward-fuzzy-regexp)
          ("C-r" . ctrlf-backward-fuzzy-regexp)
          ("C-M-s" . ctrlf-forward-literal)
          ("C-M-r" . ctrlf-backward-literal)
          ("M-s _" . ctrlf-forward-regexp)
         ))
      :config
      (ctrlf-mode +1))
  
@glucas
Copy link

glucas commented Jan 29, 2021

Agreed, the first thing I went looking for after trying ctrlf was how to make fuzzy my default search style.

@raxod502
Copy link
Member

raxod502 commented Feb 7, 2021

I think a simple way to add this functionality would be to add new commands ctrlf-forward-default and ctrlf-backward-default which are like ctrlf-forward-* and ctrlf-backward-* but would pass the value of a new user option ctrlf-default-search-style to ctrlf-forward and ctrlf-backward respectively.

You'd probably also want a default way to adjust the "alternate" search style (i.e. the one on C-M-s / C-M-r). That could be done analogously, i.e. commands ctrlf-forward-alternate and ctrlf-backward-alternate and user option ctrlf-alternate-search-style.

@haji-ali
Copy link
Contributor

haji-ali commented Mar 22, 2021

It seems that even rebinding the keys doesn't work. After rebinding C-s to ctrlf-forward-fuzzy-regexp, if I change the search style during the CTRLF session (using M-s s), the style is changed but is reverted back to fuzzy regexp the next time I press C-s.

This is because only ctrlf-forward-literal and ctrlf-backward-literal allow for the optional argument that conserves the search style. Should I make a pull request adding that argument to other search functions?
Or is a better solution needed/coming?

EDIT: I implemented @raxod502 suggestions which handles this issue.

@rileyrg
Copy link
Author

rileyrg commented Apr 2, 2021

It seems that even rebinding the keys doesn't work. After rebinding C-s to ctrlf-forward-fuzzy-regexp, if I change the search style during the CTRLF session (using M-s s), the style is changed but is reverted back to fuzzy regexp the next time I press C-s.

This is because only ctrlf-forward-literal and ctrlf-backward-literal allow for the optional argument that conserves the search style. Should I make a pull request adding that argument to other search functions?
Or is a better solution needed/coming?

EDIT: I implemented @raxod502 suggestions which handles this issue.

How did you get the search style to stick? (after changing with m-s s?)

@haji-ali
Copy link
Contributor

haji-ali commented Apr 2, 2021

How did you get the search style to stick? (after changing with m-s s?)

I didn’t. I had to implement the solution suggested by @raxod502 in #88, which hopefully will be merged soon.

@raxod502
Copy link
Member

raxod502 commented Apr 4, 2021

I believe with the aforementioned pull request (thank you!), this issue should be solved in a nice way.

@raxod502 raxod502 closed this as completed Apr 4, 2021
@rileyrg
Copy link
Author

rileyrg commented Apr 4, 2021

Sorry to be slow, but does this address the search style sticking? If I switch to literal I really want it to stay in literal until I switch back ;)

@haji-ali
Copy link
Contributor

haji-ali commented Apr 5, 2021

Sorry to be slow, but does this address the search style sticking? If I switch to literal I really want it to stay in literal until I switch back ;)

Yes, the new implementation should work for this. But you should set ctrlf-default-search-style instead of rebinding keys.

@rileyrg
Copy link
Author

rileyrg commented Apr 9, 2021

Maybe we're at odds here in our understanding. I just removed my bindings, the default search is literal, I alt-s r to change to regexp. I finish my search. A little later, I do a continued search with C-s C-s and it's back to defaulting to literal. Is it only me? That this drives me insane? lol. I want it to stick to the last format I selected. I don't want it permanent over emacs sessions just that session. I switched to regexp from literal in that search session for a reason: I find the first occurrence of something, do some work there and want to continue my search using the same search style I had set it to on the last search.

@haji-ali
Copy link
Contributor

haji-ali commented Apr 9, 2021

I finish my search

When you finish your search the state is lost. This is by design and I think it's the correct behavior.
That being said, you could probably achieve what you want with the following advice (not fully tested):

(define-advice
    ctrlf-change-search-style
    (:after (&rest _))
  (setq ctrlf-default-search-style ctrlf--style))

This will change the behaviour globally (in all buffers). You can probably make it buffer specific by using setq-local instead of setq.

EDIT: a buffer-local setting is slightly more complicated

(define-advice
    ctrlf-change-search-style
    (:after (&rest _))
  (with-current-buffer (window-buffer (minibuffer-selected-window))
    (setq-local ctrlf-default-search-style ctrlf--style)))

I actually like this and might include it in my setup :)

@rileyrg
Copy link
Author

rileyrg commented Apr 9, 2021

Thanks for taking the time to reply and look into it. I shall test it and let you know. (Just one disagreement ;) : I can't think of any case where I use a search style once or twice with C-s then hit enter and work on the result and resume the search with C-s C-s to find the search style reset is desirable - Id want the continuing search to continue with the same search type!

EDIT : didn't work I'm afraid. After I M-r to change literal to regexp and then C-s followed by enter, when I continue the search with C-s C-s it is literal again.

(use-package ctrlf
  :custom-face
  (ctrlf-highlight-active ((t (:inherit nil :background "gold" :foreground "dim gray"))))
  (ctrlf-highlight-passive ((t (:inherit nil :background "red4" :foreground "white"))))
  :custom
  (ctrlf-auto-recenter t nil nil "Customized with use-package ctrlf")
  (ctrlf-highlight-current-line t)
  (ctrlf-auto-recenter t)
  :config
  (define-advice
      ctrlf-change-search-style
      (:after (&rest _))
    (with-current-buffer (window-buffer (minibuffer-selected-window))
      (setq-local ctrlf-default-search-style ctrlf--style)))
  (ctrlf-mode +1))

EDIT EDIT

Does work with M-s to change style but not with quick-change hot keys eg M-r

So, works with M-s s and this is great. Thank you!

rileyrg added a commit to rileyrg/Emacs-Customisations that referenced this issue Apr 9, 2021
@rileyrg
Copy link
Author

rileyrg commented Apr 9, 2021

Hey @haji-ali , again thanks for your help. If you don't mind I shall add a custom and make this a PR (I already did it on my clone here: https://github.com/rileyrg/ctrlf/tree/sticky-search-style . I need to practice my PR submissions anyway ;) @raxod502 is your README.md generated from some meta data or do you write it manually? edit for posterity : https://github.com/raxod502/contributor-guide

@haji-ali
Copy link
Contributor

haji-ali commented Apr 9, 2021

Does work with M-s to change style but not with quick-change hot keys eg M-r

You would need to add the same code to ctrlf-toggle-regexp and ctrlf-toggle-symbol or other functions called by the hot keys.

If you don't mind I shall add a custom and make this a PR

Feel free to adapt the idea into a PR.

Perhaps you should check with @raxod502 or others to see if such a behaviour is desirable; including the behaviour that ctrlf-default-search-style is made buffer-local. Also, this solution does not work well with the alternative search (the one called with ctrlf-forward-alternate and ctrlf-backward-alternate) and your PR should handle that.

@rileyrg
Copy link
Author

rileyrg commented Apr 10, 2021

Personally I'm happy with it only working with the primary search forward/backwards. But happy to extend it - to the other cases if @raxod502 is happy with it - not sure it makes sense with the "alternate" as that's a fixed style anyway set up to be that fixed style. As I mentioned before, I can't imagine a case of search/edit/continue search where I wouldn't want this behaviour - I'm already very happy with it ;) buffer local seems the common-sense approach - certainly I wouldn't want it persisting across multiple unrelated buffers. https://github.com/rileyrg/ctrlf/tree/sticky-search-style . Anyway to get the ball running and better localise discussion : #89

@raxod502
Copy link
Member

@raxod502 is your README.md generated from some meta data or do you write it manually?

It's written manually.

Anyway to get the ball running and better localise discussion

Great, will continue discussion there. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants