waiting-for-dev / vim-www

Toolbox to open & search URLs from vim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Escaping characters from search terms

DancingQuanta opened this issue · comments

Search terms containing special characters such as " quotes, \ will be an issue as these characters are not escaped.
The quotes will prematurely end the url string so

"Searching google"

The first word (before space) is included in search url but the words after spaces are not and are individual url themselves (eg for chrome I get two tabs, one for google.com with Searching as term and second tab google is in url box).

i have tried :set shellslash or ;set noshellslash and observed no difference between them.

I am usinig Cygwin with vim 8 (2016 Sept 12).

Hey @DancingQuanta . Thanks for reporting. I'll push today or tomorrow a refactor about the escaping stuff. Let's see then if your issue persists.

@DancingQuanta , please, could you try again with the last version in master. I added a commit with changes in shell escape and dispatching stuff. Let's see if now it works for you.

Thanks

Okay I think cygstart is the real issue here.
Using the example "Testing here" which is then passed to cygstart in rhe current HEAD as

cygstart 'https://google.co.uk/search?q="Testing here"'

This launches Chrome with Google with Testing with no quotes.
I found that by repeating the above command in console (Cygwin) with the quotes escaped

cygstart 'https://google.co.uk/search?q=\"Testing here\"'

launched Chrome with Google with "Testing here".

So the quotes needs to be escaped properly before passing to cygstart which must be hard work to get, right?
Do start or xdg-open treats these quotes just fine?

It works well with xdg-open (I can't test with start now) and it makes sense. Between ' there should be no need to escape ", but I don't know how cygwin shell works...

Maybe the following post can help you: Use_cygwin_shell

This stackoverflow answer explains what cygstart does, it call Windows' ShellExecute

ShellExecute(NULL, "(null)", "https://google.co.uk/search?q="Testing here"", "(null)", "(null)", 1)

This may explain why the quotes are lost from the url... Seems that the quotes needs to be escaped.
Now I recommand this to be low priority as I do not want to take up too much of your time and I hardly encounter quotes in my searching.

Don't worry @DancingQuanta , I will be happy if we solve your issue.

Maybe you should set in your ~/.vimrc:

set shellxquote=\"

I think now your issue is more related with your shell dispatch configuration than with this plugin implementation. If cygwin ends up adding extra ", you should change it for any shell dispatch, and not keeping escaping at each :! invocation.

There are also some tips in the post I shared above that I think could be useful.

That post you talked about is mostly about gvim installed in Windows using cygwin as a shell while I am using vim within cygwin.
Using set shellzquote=\" causes an error in the shell

/bin/bash: line 1:  cygstart 'https://google.co.uk/search?q=Testing here': No such file or directory

So no dice.
However I created a pull request #7 that escapes the quotes in a way that Windows understands.
This could be improved further once we understand how Windows behaves in this case as we cannot avoid cygstart adding quotes as this is a way of making a string that Windows understands.

Hey @DancingQuanta , thanks for your feedback and your PR.

Prior to merging it I would like to understand what is going on... I still think that, if possible, it should be dealt globally in your vim configuration. I mean, it seems that you always need to escape the " character when passed to !:. Guess that another user has configured it accordingly (again, if it's possible), if this plugin escapes it a second time it would break the feature.

About your PR, why are you substituting " for ""? Shouldn't it be \"?

I will do some research about the subject, even if it is quite difficult to test because I don't have any Windows at hand... Have you look at shellxscape, maybe combining it with ( value in shellxquote?

The thing is, I don't think vim is aware that is I am using cygwin and that cygwin sits on top of cmd.exe in windows.
So using shellxquote and shellxescape to my understanding is limited to cygwin layer and does not affect what goes between cygwin and cmd.exe.
I completely forgot why I use "" to escape quotes, so don't merge yet as this is just an idea and simply want a place for people to see.
I will try combination of shellx and friends in the meantime and get back to you.

According to this page, cmd.exe escaping character is ^.

From :help shellxescape:

When 'shellxquote' is set to "(" then the characters listed in this option will be escaped with a '^' character. This makes it possible to execute most external commands with cmd.exe.

So, maybe having:

set shellxquote = "("
set shellxescape = '"'

should do the trick?

In fact, shellxescape defaults to: \"&|<>()@^ in Windows, so if it doesn't have this value by default maybe you should add all those characters.

I afaid that these two settings

set shellxquote = "("
set shellxescape = "\""

doesn't seems to change anything, vim still cannot see windows through cygwin.
By the way, '"' is a mistake because vim throws an error as it doesn't recognise ' so I believe you meant "\"".

Hmmm, in fact it should be:

set shellxquote = "("
set shellxescape = "\"&|<>()@^"

Also, what is the output of :set shell ?

Going to close it now. Feel free to reopen if you come with more info.