seankhl / fzf-mksh

mksh files for junegunn's fzf
13 stars 1 forks source link

fzf tab completion #1

Closed siduck closed 3 years ago

siduck commented 3 years ago

hi @seankhl , I tried your fzf completion with mksh and it works very well . Could you add some functionality for tab completion with fzf?

https://github.com/lincheney/fzf-tab-completion

seankhl commented 3 years ago

Hi, thanks for your suggestion. Built-in tab completion with mksh is a lot simpler than it is with bash (http://www.mirbsd.org/mksh-faq.htm#completion), so this should be fairly easy to do though may not provide as many benefits as it does with bash. One main benefit I can see is that it avoids cluttering the terminal with large lists of completion candidates, something that has bothered me before. I'll take a look at doing this, and PRs (including incomplete ones that we can collaborate on) are welcome.

siduck commented 3 years ago

agreed! btw can we make tab completions case insensitive? if fzf is made to do the tab completion then its possible I think.

seankhl commented 3 years ago

I believe if we use fzf for the selection process, then choosing the corresponding completion would be case-insensitive; whether this case-insensitivity would automatically apply to the already-input text I suppose would depend on our implementation. I haven't looked closely enough at the link you posted to see how it works in their implementation. I agree personally with making the already-input text case insensitive, but it might be best to make this an option as I can see arguments for both. Adding a few env variables for some options would be reasonable IMO.

siduck commented 3 years ago

I would suggest doing the fzf tab completion , since fzf itself will manage the case insensitive stuff itself .

bash tab completion with fzf

image

seankhl commented 3 years ago

It's the least of our worries, but my point was that say you do

cd a<TAB>

Even using the project you linked, folders beginning with a captial "A" won't be listed. Likewise, typing cd A<TAB> won't list folders starting with a lowercase "a".

I've looked through the project and it uses quite a bit of nuttiness to achieve the desired result. The tricky part is obtaining the list of completion terms in a programmatic manner. Usually this is a completely interactive thing: you have to press tab twice to get your list of completion candidates, and this cannot be easily replicated by an actual command. Just because it's not easy doesn't mean it's not possible though.

I'm not sure if mksh has analogous functionality, but I don't think so. I looked through the manpage but didn't find anything that could quite be used, and some hackish ways of trying to get the output from a command (by piping tab characters to mksh -i or something like that) weren't successful. That's not even the proper solution anyway. I will probably end up asking on the mksh IRC channel to see if there is a way.

In the meantime, it is actually possible to use the bash completion provided in that repo from within mksh by binding tab to something like

bind -m '^I'='^a^[ __fzf_select_tmux__ $(bash -c ". $HOME/fzf-bash-completion.sh; fzf_bash_completion_query ^e)^[^e^a^e' 

where fzf_bash_completion_query takes as an argument the current text in your prompt and returns the list of completion candidates (this functionality is already in that repo, just needs to be pulled out as a function which I was mostly able to do). By the way that command is just pseudocode, it's just to get the idea across and I haven't tested it at all, so it's almost certain to not work even if you did provide a working fzf_bash_completion_query function.

Then, that just gets passed to your fzf selector which behaves as it would in any other case. Of course this spawns a bash subshell so it's going to perform quite poorly compared to a pure mksh solution, but it's an example of some of the pieces that would need to be put in place to get a full mksh solution working. That would basically just involve doing the same thing but writing a fzf-mskh-completion.sh script with a corresponding fzf_mksh_completion_query function instead.

siduck commented 3 years ago

Sounds complex to me, also could you separate the tmux part from functions? it would be helpful for those not using tmux.

siduck commented 3 years ago

Idk much about mksh too , I've been using it since 3 days and it had nice fzf history completion made by you , which is why I'm staying on it! :D

seankhl commented 3 years ago

I made some progress on this, as when I asked about obtaining the completion lists programamatically in the mksh server, mirabilos (the mksh maintainer) told me that glob canditates work the same way as completion list candidates. This allowed me to obtain the completion lists that mksh generates and pass them to fzf. There are a few outstanding tasks, but this is looking pretty doable and much simpler overall than the bash solution.

siduck commented 3 years ago

I made some progress on this, as when I asked about obtaining the completion lists programamatically in the mksh server, mirabilos (the mksh maintainer) told me that glob canditates work the same way as completion list candidates. This allowed me to obtain the completion lists that mksh generates and pass them to fzf. There are a few outstanding tasks, but this is looking pretty doable and much simpler overall than the bash solution.

Sounds very great! Thanks for actually working on fzf + mksh :'( . I asked help for this and my friends told me stop using mksh XD , but luckily found your repo :D

seankhl commented 3 years ago

I committed a WIP that should mostly do the job. Give it a shot and let me know if you find any issues. Check the commit message for caveats.

This was absolute madness to get right! The bind macro is 170 charaters long! It was good fun though, like doing a puzzle.

siduck commented 3 years ago

I committed a WIP that should mostly do the job. Give it a shot and let me know if you find any issues. Check the commit message for caveats.

This was absolute madness to get right! The bind macro is 170 charaters long! It was good fun though, like doing a puzzle.

fzf history works perfect , But the tab fzf completion doesnt work.

seankhl commented 3 years ago

I think it might be because I was testing with fzf-tmux. I still need to add all the fzf-based options back in, it's still a WIP and pretty bare-bones right now in terms of options.

I committed a change to have it use plain fzf. If you want to try again, let me know what you tried, and if there's any problems, describe it and send any error messages or other incorrect output you saw.

seankhl commented 3 years ago

Hey @siduck76, I added back all the fzf and fzf-tmux auto-selection and options. So, the tmux stuff is fully separated out, and it should auto-detect that you're not using tmux and use the regular fzf executable as desired. Also, the environment variables $FZF_DEFAULT_OPTS, $FZF_TMUX, $FZF_TMUX_OPTS, and $FZF_TMUX_HEIGHT are respected for the tab completion implementation as well as the rest. I also cleaned up the other implementations, i.e. for history search (ctrl+r), file-select (ctrl-t), and cd (alt+c).

If you have the bandwidth I would definitely be interested in having you test the completion implementation again. Also, let me know if the cleaned up version of the history search or others has any regressions for you.

siduck commented 3 years ago

@seankhl sorry for late response , I added the fzf bindings in my .mkshrc and the history completion still works. do I need to source the completion.mksh?

also could you provide some instructions in the readme about using mksh + fzf ? Maybe something like this

image

Its very useful for newbies like me!

seankhl commented 3 years ago

Yes sorry, for both key-bindings.mksh and completion.mksh, you can just source them from your mkshrc file or even just on the command line to get it for your current shell prompt.

There is no need to do any bind commands, as they are already in the files you source.

You're totally right that I should add a readme, I will write one with full instructions tonight!

seankhl commented 3 years ago

also could you provide some instructions in the readme about using mksh + fzf ?

Done

siduck commented 3 years ago

cool! btw this occurs when I press TAB to do tab completion

image

seankhl commented 3 years ago

Oof. Can you show me the output of set -o?

seankhl commented 3 years ago

Also, is this from pressing TAB after typing just cd, like at the end of that line? Just trying to make sure I understand.

I think some of our hotkey bindings are not the same. Do you have alt+shift+F or alt+shift+B bound to anything, or ctrl+X maybe?

seankhl commented 3 years ago

I'm pretty sure I know what the issue is: I'm using quote-region in the bind macro which is a feature from R59. My guess is that you're on R58 or earlier... can you update to mksh R59?

I have a solution that will work on previous versions, but it will be slower and less consistent with the builtin tab completion, and would take some time to implement.

siduck commented 3 years ago

set -o

image

siduck commented 3 years ago

Also, is this from pressing TAB after typing just cd, like at the end of that line? Just trying to make sure I understand.

I think some of our hotkey bindings are not the same. Do you have alt+shift+F or alt+shift+B bound to anything, or ctrl+X maybe?

this occurs when I press TAB , whether it be after cd or anytime . And no I dont have those binds!

siduck commented 3 years ago

I'm pretty sure I know what the issue is: I'm using quote-region in the bind macro which is a feature from R59. My guess is that you're on R58 or earlier... can you update to mksh R59?

I have a solution that will work on previous versions, but it will be slower and less consistent with the builtin tab completion, and would take some time to implement.

My mksh version image

seankhl commented 3 years ago

OK, I took the exact same version you're using, and it seems like quote-region feature still doesn't work properly even in that version (must have been a bug that was later fixed).

If you'll bear with me, can you try building and installing mksh from source? Instructions are here: http://www.mirbsd.org/mksh.htm#getcvs but I'll summarize them for you. You can use cvs or the git mirror:

env CVS_RSH=ssh cvs -qd _anoncvs@anoncvs.mirbsd.org:/cvs co -PA mksh

or

git clone https://github.com/MirBSD/mksh.git

Then to build and install, it's easy (no dependencies):

cd mksh
sh Build.sh -r
install -c -s -o root -g bin -m 555 mksh /bin/mksh
# optional (for our needs): add mksh to the system's available shells
grep -x /bin/mksh /etc/shells >/dev/null || echo /bin/mksh >>/etc/shells
# optional (for our needs): install some example mkshrc configs, and the manpages
install -c -o root -g bin -m 444 dot.mkshrc /usr/share/doc/mksh/examples/
install -c -o root -g bin -m 444 lksh.1 mksh.1 /usr/share/man/man1/

I just tested with the same set -o configs as you with this latest checkout, and everything worked as desired, so hopefully this will fix the problem.

siduck commented 3 years ago

yes thix fixed it! btw you should mention this on the readme

siduck commented 3 years ago

btw any way we could make mksh case insensitive ( while pressing tab)

suppose I type this

image

and I press tab , fzf doesnt popup

siduck commented 3 years ago

but the same works in bash , I added this in ~/.inputrc and this makes bash case insensitive

image

seankhl commented 3 years ago

btw any way we could make mksh case insensitive ( while pressing tab)

I do want to support this, but can you open a new issue about it? While it does need more work to to considered totally implemented, I think the basic functionality of tab completion is now done. Unless you disagree, I'm going to close this issue after making a note in the readme about the version needed for tab completion.

edit: b6dd5e72abf12d8181fdc08502e762c42cbfc478

siduck commented 3 years ago

yeah this works well :)