leo-arch / clifm

The shell-like, command line terminal file manager: simple, fast, extensible, and lightweight as hell.
https://github.com/leo-arch/clifm/wiki
GNU General Public License v2.0
1.29k stars 41 forks source link

Problem using eln with file names containing spaces with xonsh #244

Closed tkossak closed 11 months ago

tkossak commented 11 months ago

Describe the bug Problem using files containing spaces in their names, when using clifm with xonsh shell.

To Reproduce Run clifm using CLIFM_SHELL=/home/kossak/.local/bin/xonsh clifm (enter proper path to xonsh) and then:

<0> $ n 'a - a'
<0> $ vim 4   # 4 is my ELN of above file - change it to proper ELN on your computer
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Jul 08 2023 13:06:53)
Unknown option argument: "-\"
More info with: "vim -h"

I also tried expanding ELN (by entering vim 4 and hitting tab) but the same error appears:

<0> $ vim a\ -\ a
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Jul 08 2023 13:06:53)
Unknown option argument: "-\"
More info with: "vim -h"

In xonsh vim a\ -\ a also returns this error but vim 'a - a' works. Is it possible to configure eln expansion to use quotes instead of escaping spaces with \?

Expected behavior After running vim 4 vim opens the file a - a

Desktop (please complete the following information):

tkossak commented 11 months ago

It actually is a problem with xonsh shell. It would work if eln was expanded using quotes, not slashes. Maybe I will just stop using xonsh inside clifm, and switch back to bash...

leo-arch commented 11 months ago

Hi @tkossak. First, the obvious thing: xonsh is quite unorthodox in this regard. Escaping (using backslashes), just as quoting (using either single or double quotes), is a standard method (POSIX indeed) to disable special meanings.

However, providing an option to choose among these escaping mechanisms (backslash, single, or double quotes) when auto-expanding file names, might be an interesting feature.

For the time being, you can still make it work using the ; character, which instructs clifm to pass the current command line verbatim to the system shell (in this case, xonsh). Example:

;vim 'a - a'

Far from perfect, but it does the trick.

leo-arch commented 11 months ago

A little improvement: vim 'a - a' should work (no need for leading semicolon anymore).

Btw, there are several open issues in the xonsh repo complaining about this: it cannot handle backslashes. See for example https://github.com/xonsh/xonsh/issues/3595 and https://github.com/xonsh/xonsh/issues/1432. Sadly, the devs do not seem to like the idea of supporting backslashes at all, despite the fact that it is a quite standard feature (and indeed the default quoting mechanism for most shells). As specified by POSIX, all the three quoting mechanisms (backslash, single, and double quotes) shall be supported by a compliant shell. Of course, xonsh do not need to be POSIX compliant (and probably it is not even intended to be so); but there are some drawbacks, mostly portability. This issue seems to be a good example.

tkossak commented 11 months ago

Yes, xonsh does not try to be 100% POSIX compatible, instead it wants to be more PYTHON compatible.

vim 'a - a' works, but I need to write file name manually instead of expanding ELN (or manually remove all slashes and enclose it in quotes)

So it's feature request ticket :). If we could configure clifm to use single single (or double) quotes instead of slashes it would be awesome. I would use it even with bash, because slashes look ugly, especially when file name has more spaces and is long - it's difficult to find at first glance where file name ends, and where command arguments are.

Thank you very much for your hard work!

leo-arch commented 11 months ago

I have been playing around with this (cause I think is a feature to have) and have been able to make it work pretty nice, except for directories: TAB completion/suggestions is complex machinery. The thing is that I don't like to provide a non-complete feature because it looks weak (and it is it), but I could provide you with temporary snapshots hidden behind a non-documented option to make some tests.

EDIT: non-complete means that alternative quoting mechanisms (both single and double quotes) will work only for ELN expansions provided they do not point to directories.

tkossak commented 11 months ago

Sure, If you tell me commit hash to install clifm from, I can try it

leo-arch commented 11 months ago

The new feature (though highly experimental) is ready. This is how it works:

  1. Select the quoting style setting QuotingStyle in the config file (the COMMAND LINE section feels like the right place) to any of these values: backslash (default), single, or double (single would be the most natural choice in this case).
  2. Regular files (not dirs) expanded/completed from ELN's will use the desired quoting style.

Needles to say, this feature is completely undocumented (hidden) and might be modified (or even completely removed) in the (near) future.

tkossak commented 11 months ago

Expanding eln works as it should (tried QuotingStyle=single), but using eln directly doesn't (because of xonsh), eg inside clifm:

$ n 'a - a'
$ vim 1<TAB>  # works, it changes into: vim 'a - a'
$ vim 1
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Jul 08 2023 13:06:53)
Unknown option argument: "-\"
More info with: "vim -h"

clifm v1.13.9 with xonsh as $CLIFM_SHELL.

In bash both eln and expanded eln works.

leo-arch commented 11 months ago

Should be working now.

EDIT: A little tip: vim 1 (runs the command via the system shell, or whatever shell specified via CLIFM_SHELL); 1 vim, by contrast, runs the command bypassing the shell, directly via execv(3), in which case the quoting style doesn't make any difference: whatever 1 points to is passed to vim as is. The second command works no matter the quoting style.

tkossak commented 11 months ago

Works, thank you (also for the tip).

tkossak commented 11 months ago

Got another improvement for this feature: it would nice if quoting expansion worked also for tab completion! eg:

$ n 'a - a'
$ ls a<TAB>

Last command is expanded into: ls a\ -\ a, but should be ls 'a - a'

leo-arch commented 11 months ago

Hi @tkossak. Yes, I'm totally aware of this, and this is why I said that this feature is not complete. The thing is that 1<TAB> is one thing and a<TAB> is a completely different thing (or at least quite more complex): in the first case, we know we have an ELN, which could be either a directory or a file name (always in the current directory); in the second case, by contrast, none of these conditions can be given for granted, cause it may be anything, from a simple file name, to a directory name, to a path or any other completion type. This is why I limited this feature to ELN's in the first place.

I'll bear this in mind however.

freijon commented 10 months ago

btw. same issue with nushell. Nushell also supports back-ticks (`) as quote style, so maybe this could easily be added as well?

leo-arch commented 10 months ago

Hi @freijon. Thanks for pointing this out. For the time being, the same trick used for xonsh can be used for nushell.

As to the backticks, that's quite unorthodox, and more importantly, dangerous: most shells (following the POSIX specification) use backticks for command substitution : now, if you're not running nushell and your quote style is backtick, the underlying shell will try to execute a command named as your file, which is clearly not what you intended, and also a security concern.

I'll take a look at it, however.