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

Suggestion: extended regex mode for "hints" kitten #2124

Closed
artemave opened this issue Nov 10, 2019 · 9 comments
Closed

Suggestion: extended regex mode for "hints" kitten #2124

artemave opened this issue Nov 10, 2019 · 9 comments

Comments

@artemave
Copy link

I am using "hints" kitten to open file links in terminal vim (in a different tmux pane) and it works really well. In fact, that's the sole reason I switched to kitty (though it's awesome in every other way too).

There are few bits though that I find "hints" is missing:

  • regex is never 100% accurate: there are either false positives, or not enough match
  • can't group multiple regexps under a single shortcut (I'd like to open file links and web links with the same shortcut)
  • can't hint on a text that doesn't look like a file path, but nonetheless maps onto one (e.g., in rails log, PostsController#create maps onto a particular line in app/controllers/posts_controller.rb

So I was thinking to have a go at extending "hints" kitten, such that it supports the following contrived config:

map kitty_mod+f kitten hints --type regex-plus \
  --regex1 "FILE_WITH_LINE_NUMBER_REGEX" --transform1 ~/return_input_if_file_exists.sh --program1 ~/bin/open_in_vim.sh
  --regex2 "RAILS_CONTROLLER_ACTION_REGEX" --transform2 ~/rails_controller_action_to_path.sh --program2 ~/bin/open_in_vim.sh
  --regex3 "WEB_URL_REGEX"

If the command specified in --transformX returns an empty string: no hint is shown for the --regexX. Otherwise, the hint is shown and --transformX output is passed into --programX command.

Any thoughts on the above? What are the chances of this getting merged?

@kovidgoyal
Copy link
Owner

Regexes support the OR operator. Simply create a regex that matches all
the things you want to match then pass it to a program that can perform
further filtering/actions as required. This can be made a bit more
ergonomic by changing the hints kitten so that if the regex has named
groups it will pass the name of the group as an additional argument to
the invoked program.

@artemave
Copy link
Author

OR operator definitely helps, but some files are difficult to match without going too inclusive (e.g. files without extensions, files in the current directory). One way to solve this could be to pass a match through an arbitrary test - e.g. "does this file exist?" - to weed out false positives and only show the hints that pass. That's what the proposed --transform option is about.

@kovidgoyal
Copy link
Owner

Running a script on every match is way too slow/CPU intensive. If you
want to do this a better way would be to just allow the user to pass in
the path to a python script that defines a match_postprocess() function
the hints kitten can then import that function and use it adjust the
match as needed. The hints kitten already has a framework for this, see
post_processors.

And really might as well go the whole hog, let the user pass in a python
module that defines two entry points.

  1. iter_matches(text, args, context_dict)

which yields pairs of start and end positions for all matches.

  1. execute_match(match_text, args, context_dict)

which can perform arbitrary actions on the selected match.

@artemave
Copy link
Author

Thanks a lot @kovidgoyal for making this happen!

A friend of my is interested in using it, but he's not the kind of guy who builds his own terminals :) When can we expect this change to make it in a release?

For the reference, working example: https://github.com/artemave/myrcs/blob/fe1a29d17750cb55f828de163a29403b94c2fdeb/.config/kitty/tmux_send_to_vim.py

@kovidgoyal
Copy link
Owner

soonish, I am suposed to be migrting the kitty binary build system to
use bypy, once that is dne I will make the next reease.

@ysftaha
Copy link

ysftaha commented Dec 15, 2019

Thanks a lot @kovidgoyal for making this happen!

A friend of my is interested in using it, but he's not the kind of guy who builds his own terminals :) When can we expect this change to make it in a release?

For the reference, working example: https://github.com/artemave/myrcs/blob/fe1a29d17750cb55f828de163a29403b94c2fdeb/.config/kitty/tmux_send_to_vim.py

What if I wanted to copy the whole screen to a new buffer in vim?

@kovidgoyal
Copy link
Owner

kovidgoyal commented Dec 15, 2019 via email

@artemave
Copy link
Author

I summed it all up in a blog post - https://featurist.co.uk/blog/file-links-in-terminal/ (reddit thread: https://www.reddit.com/r/programming/comments/empexg/file_links_in_the_terminal/ )

@kovidgoyal
Copy link
Owner

Cool :)

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

No branches or pull requests

3 participants