Re: [Fish-users] Completion problem with regexp and string concatenation
On Wed, Mar 6, 2019 at 10:41 PM Kurtis Rader wrote: > > On Wed, Mar 6, 2019 at 1:29 PM SanskritFritz wrote: >> >> Hi and thanks for your answer. >> Indeed this way it works, but I don't understand why, because when I >> test that expression this works: >> string match --regex ' diff .*::[^ ]+ 'aaa'$' "borg diff ::aaa aaa" >> and this doesn't because (commandline) can contain spaces: >> string match --regex ' diff .*::[^ ]+ 'aaa'$' borg diff ::aaa aaa > > > You seem to be under the misapprehension that fish behaves like POSIX shells > (e.g., bash) with respect to command substitution. That is, that the output > of `(commandline)` is split on whitespace. The POSIX equivalent, > `$(commandline)` does split on whitespace. But fish only splits (i.e., > tokenizes) on newlines. So in fish, assuming the command line has a single > line, the output of `(commandline)` is equivalent to the double-quoted string > in your first example. There are various experiments you can do to show this. > For example: > > set var (commandline) > set --show var > > Note that this is also true for var expansion. Try this: > > set var "borg diff ::aaa aaa" > string match --regex ' diff .*::[^ ]+ 'aaa'$' $var > > Notice that `$var` doesn't need to be enclosed in double-quotes. Unlike a > POSIX shell where you do need to quote the var expansion to keep it from > being split on $IFS. Thank you for this explanation, much appreciated. Now I understand the difference. Cheers. ___ Fish-users mailing list Fish-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/fish-users
Re: [Fish-users] Completion problem with regexp and string concatenation
On Wed, Mar 6, 2019 at 1:29 PM SanskritFritz wrote: > Hi and thanks for your answer. > Indeed this way it works, but I don't understand why, because when I > test that expression this works: > string match --regex ' diff .*::[^ ]+ 'aaa'$' "borg diff ::aaa aaa" > and this doesn't because (commandline) can contain spaces: > string match --regex ' diff .*::[^ ]+ 'aaa'$' borg diff ::aaa aaa > You seem to be under the misapprehension that fish behaves like POSIX shells (e.g., bash) with respect to command substitution. That is, that the output of `(commandline)` is split on whitespace. The POSIX equivalent, `$(commandline)` does split on whitespace. But fish only splits (i.e., tokenizes) on newlines. So in fish, assuming the command line has a single line, the output of `(commandline)` is equivalent to the double-quoted string in your first example. There are various experiments you can do to show this. For example: set var (commandline) set --show var Note that this is also true for var expansion. Try this: set var "borg diff ::aaa aaa" string match --regex ' diff .*::[^ ]+ 'aaa'$' $var Notice that `$var` doesn't need to be enclosed in double-quotes. Unlike a POSIX shell where you do need to quote the var expansion to keep it from being split on $IFS. -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank ___ Fish-users mailing list Fish-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/fish-users
Re: [Fish-users] Completion problem with regexp and string concatenation
On Wed, Mar 6, 2019 at 3:05 PM David Adam wrote: > > On Sun, 3 Mar 2019, SanskritFritz wrote: > > I'm writing completions for borg. > > There is a special case where I want to achieve the following: > > Fish should give a second archive list when the following scenario is > > present (the cursor is now at the end of the line after a space): > > borg diff repo::archive1 > > yielding this: > > borg diff repo::archive1 archive2 > > I have found a solution with this code (with some debugging help as > > well for now): > > > > function __fish_borg_is_diff_second_archive > > echo (commandline) >> ~/temp/debug.txt > > return (string match --regex ' diff .*::[^ ]+ '(commandline > > --current-token) '"'(commandline)'"' >> ~/temp/debug.txt) > > end > > complete -c borg -f -n __fish_borg_is_diff_second_archive -a 'archive1 > > archive2 archive3' > > > > This works well, with one little problem, particularly that after > > borg diff repo::archive1 archive2 > > pressing tab again lists the archives which I don't want. The reason > > is that the regexp still matches the commandline. So I thought I try > > to modify the regexp like this: > > > > function __fish_borg_is_diff_second_archive > > echo (commandline) >> ~/temp/debug.txt > > return (string match --regex ' diff .*::[^ ]+ '(commandline > > --current-token)'$' '"'(commandline)'"' >> ~/temp/debug.txt) > > end > > > > The only difference to the previous one is the '$' after the > > --current-token. With that I want to achieve a more strict matching. > > However this somehow doesn't match even the second archive, I have no > > idea why. Issuing the following in the shell actually works well (this > > is how I try to test the matching part): > > > > ~> string match --regex ' .*::[^ ]+ ''$' "borg diff repo::archive1 " > > diff repo::archive1 > > ~> string match --regex ' .*::[^ ]+ 'archi'$' "borg diff repo::archive1 > > archi" > > diff repo::archive1 archi > > ~> string match --regex ' .*::[^ ]+ ''$' "borg diff repo::archive1 archive2 > > " > > ~> echo $status > > 1 > > > > Now I don't know what is going on and I need your help please. > > Hi SanskritFritz, > > I asked our resident completions expert, faho@, and he has suggested the > following: > > `'"'(commandline)'"'` is wrong. There's no need to add quotes, and that > last quote will make it so `$` isn't directly after the current token > anymore. Hi and thanks for your answer. Indeed this way it works, but I don't understand why, because when I test that expression this works: string match --regex ' diff .*::[^ ]+ 'aaa'$' "borg diff ::aaa aaa" and this doesn't because (commandline) can contain spaces: string match --regex ' diff .*::[^ ]+ 'aaa'$' borg diff ::aaa aaa May I contact faho@ on github or is he reading this as well? ___ Fish-users mailing list Fish-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/fish-users
Re: [Fish-users] Completion problem with regexp and string concatenation
On Sun, 3 Mar 2019, SanskritFritz wrote: > I'm writing completions for borg. > There is a special case where I want to achieve the following: > Fish should give a second archive list when the following scenario is > present (the cursor is now at the end of the line after a space): > borg diff repo::archive1 > yielding this: > borg diff repo::archive1 archive2 > I have found a solution with this code (with some debugging help as > well for now): > > function __fish_borg_is_diff_second_archive > echo (commandline) >> ~/temp/debug.txt > return (string match --regex ' diff .*::[^ ]+ '(commandline > --current-token) '"'(commandline)'"' >> ~/temp/debug.txt) > end > complete -c borg -f -n __fish_borg_is_diff_second_archive -a 'archive1 > archive2 archive3' > > This works well, with one little problem, particularly that after > borg diff repo::archive1 archive2 > pressing tab again lists the archives which I don't want. The reason > is that the regexp still matches the commandline. So I thought I try > to modify the regexp like this: > > function __fish_borg_is_diff_second_archive > echo (commandline) >> ~/temp/debug.txt > return (string match --regex ' diff .*::[^ ]+ '(commandline > --current-token)'$' '"'(commandline)'"' >> ~/temp/debug.txt) > end > > The only difference to the previous one is the '$' after the > --current-token. With that I want to achieve a more strict matching. > However this somehow doesn't match even the second archive, I have no > idea why. Issuing the following in the shell actually works well (this > is how I try to test the matching part): > > ~> string match --regex ' .*::[^ ]+ ''$' "borg diff repo::archive1 " > diff repo::archive1 > ~> string match --regex ' .*::[^ ]+ 'archi'$' "borg diff repo::archive1 archi" > diff repo::archive1 archi > ~> string match --regex ' .*::[^ ]+ ''$' "borg diff repo::archive1 archive2 " > ~> echo $status > 1 > > Now I don't know what is going on and I need your help please. Hi SanskritFritz, I asked our resident completions expert, faho@, and he has suggested the following: `'"'(commandline)'"'` is wrong. There's no need to add quotes, and that last quote will make it so `$` isn't directly after the current token anymore. David Adam zanc...@ucc.gu.uwa.edu.au ___ Fish-users mailing list Fish-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/fish-users