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

Proposal: Symbol at point as default search pattern for `counsel-*g` and `counsel-git-grep` and all other search tools #1867

Open
Alexander-Shukaev opened this issue Dec 20, 2018 · 8 comments

Comments

@Alexander-Shukaev
Copy link

commented Dec 20, 2018

Would it be possible to input symbol-at-point by default whenever a counsel-based search tool is invoked? That is try the stock grep-recursive. I think it's very cleverly implemented in that it displays symbol-at-point in parentheses as a default pattern, so if you hit RET without typing anything, this will become your pattern. Similar feature could be implemented for counsel-based search tools. That is by default the input field remains empty as it is now but there would be symbol-at-point in parentheses displayed in a similar manner to grep-recursive and the invoked search tool would immediately search for that default pattern already. In other words, you could immediately trigger ivy-occur without even typing anything because there are (probably) matches already for that default pattern. However, if you start typing anything, then your pattern of course overtakes, while as soon as you delete your input completely, again the default pattern should kick in. To be reasonable, a defcustom to enable this behavior should be introduced.

@mookid

This comment has been minimized.

Copy link
Contributor

commented Dec 20, 2018

agreed. I have some code that does works around that limitation in my config for a while now.

@vishesh

This comment has been minimized.

Copy link

commented Dec 21, 2018

This is actually useful as reasonable number of times I wish to search what's under the cursor. I borrowed this[1] which does exactly same, however the annoying thing about this is that its not uncommon to search something you are not under, in which case you have manually clear the input and that I've found to be more annoying. Perhaps that problem can be mitigated to some extent by keeping input selected?

[1] https://github.com/seagle0128/.emacs.d/blob/master/lisp/init-ivy.el#L299

@Alexander-Shukaev

This comment has been minimized.

Copy link
Author

commented Dec 21, 2018

If you read my proposal, you will see that I deliberately mention grep-recursive as an example and reiterate the desired behavior in words. That is, there should be no initial input (which would be indeed annoying to clear). The symbol at point should rather be displayed as part of the ivy prompt (similar to how grep-recursive does) just for information purposes and it should of course be already applied as a pattern. At the same time, the input field should still remain empty so that if one wants to search for something else, one just starts typing it and that custom input pattern should overtake the default symbol at point pattern, while as soon as the input field is empty again, the default symbol at point pattern should become active again. In this case, the existing workflow is not disturbed at all, it remains backwards-compatible, while the only feature is that we make use of (currently useless) empty input field to designate the default symbol at point pattern activation.

@Alexander-Shukaev

This comment has been minimized.

Copy link
Author

commented Dec 21, 2018

Indeed, this requires slightly more effort to implement than just inject initial input (which is unacceptable due to annoyance of clearing as you already mentioned).

@basil-conto

This comment has been minimized.

Copy link
Collaborator

commented Dec 21, 2018

@Alexander-Shukaev The closest existing solution to what you describe is specifying the symbol at point as the DEF keyword/argument to ivy-read/completing-read. IIRC, Ivy currently adds such default items to the list of candidates unless already present, and preselects them. AIUI, the only difference between this and what you describe is that the prompt does not automatically change depending on whether a DEF argument was specified. By convention, it's up to the caller to modify the prompt accordingly, so I'm not sure we could successfully be smart about this in ivy without becoming intrusive. Besides, the reason (default: foo) is needed in vanilla Emacs completion is because there is no realtime visualisation of the candidate list, whereas Ivy makes it clear that the default is preselected.

@Alexander-Shukaev

This comment has been minimized.

Copy link
Author

commented Dec 21, 2018

That's fine, I can live with that, whatever is "native" for the framework should stay so. Then, all what's left to implement this now is to add :def ... all over the place?

@Alexander-Shukaev

This comment has been minimized.

Copy link
Author

commented Dec 21, 2018

I suppose to be more flexible, what could be done is a variable similar to ivy-initial-inputs-alist, e.g. ivy-read-def-alist, where one could add whatever they want in a custom fashion. I'd appreciate a symbol-at-point for search commands as a default example though. On a side note, why ivy-initial-inputs-alist is a defvar rather than defcustom?

@basil-conto

This comment has been minimized.

Copy link
Collaborator

commented Dec 21, 2018

I suppose to be more flexible, what could be done is a variable similar to ivy-initial-inputs-alist, e.g. ivy-read-def-alist, where one could add whatever they want in a custom fashion. I'd appreciate a symbol-at-point for search commands as a default example though.

SGTM. The values of the alist would need to optionally be functions so as to determine the symbol at point dynamically.

On a side note, why ivy-initial-inputs-alist is a defvar rather than defcustom?

I don't know; @abo-abo should. I'm guessing oversight / historical accident.

abo-abo added a commit that referenced this issue Dec 23, 2018
ivy.el: allow to make the initial-input dynamic.
This allows for instance to use customizations such as
(lambda ()
  (thing-at-point 'symbol))

(ivy-initial-inputs-alist): cdrs of the alist can now be functions.
(ivy--reset-state): adapt.

Fixes #1868
Re #1867
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.