Skip to content

Support command line options in EDITOR/GIT_EDITOR #163

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

Closed
wants to merge 3 commits into from
Closed

Support command line options in EDITOR/GIT_EDITOR #163

wants to merge 3 commits into from

Conversation

nirs
Copy link
Contributor

@nirs nirs commented Jul 21, 2017

On Windows users may like to configure command line options for the
editor[1]. Support this configuration.

Fixes python/blurb#2.

[1] https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup

@nirs
Copy link
Contributor Author

nirs commented Jul 21, 2017

@zware, @larryhastings please review.

@nirs
Copy link
Contributor Author

nirs commented Jul 21, 2017

Here an example with command with spaces, and command line options:

ln -s /usr/bin/vim 'my_editor'
$ EDITOR="$PWD/'my editor' +25" ../../core-workflow/blurb/blurb.py

It opens vim and jump to line 25.

Not tested on Windows.

@larryhastings
Copy link
Contributor

Yes, but you use quotes around the spaces. If the path isn't quoted, it won't run the editor properly. I don't know whether or not people put unquoted paths with spaces in environment variables, but I think it's possible to handle it--please see my previous pseudocode.

@nirs
Copy link
Contributor Author

nirs commented Jul 22, 2017

I have seen your example in #156, but it will also not handle path with spaces and command line arguments. If you don't quote the path, we get:

>>> p = "C:/Program Files (x86)/Notepad++/notepad++.exe -multiInst -nosession"
>>> shlex.split(p, posix=False)
['C:/Program', 'Files', '(x86)/Notepad++/notepad++.exe', '-multiInst', '-nosession']

We can use this hack for windows:

>>> p[:p.index('.exe') + 4]
'C:/Program Files (x86)/Notepad++/notepad++.exe'
>>> shlex.split(p[p.index('.exe') + 4:])
['-multiInst', '-nosession']

@larryhastings
Copy link
Contributor

It's impossible** to handle a string that contains both a path with unquoted spaces and additional command-line arguments. My point was, I think we need to handle these two scenarios:

  1. $EDITOR is a unquoted path to an editor, containing spaces
  2. $EDITOR is a quoted path to an editor, followed by arguments

My proposed approach would handle both those cases; IIUC your current approach only handles the latter.

I think it's reasonable to handle both these scenarios. After all, there are plenty of examples of environment variables that can contain unquoted paths containing spaces (e.g. $PATH, or %APPDATA% on Windows). And there are also examples specifically of $EDITOR containing command-line arguments, as you have demonstrated.

Therefore, I don't want to merge your PR with its current approach. If you'd like to handle this more complicated logic, go ahead and try; otherwise we should abandon this PR and I'll do the more complicated thing.

** well, not impossible to handle such a string. but it would be ridiculous to try. let's not go there.

@methane
Copy link
Member

methane commented Jul 22, 2017

Mercurial seems doing like tihs:

os.system('%s "%s"' % (cmd, path))

See https://www.mercurial-scm.org/repo/hg/file/tip/mercurial/ui.py#l1379

@nirs
Copy link
Contributor Author

nirs commented Jul 22, 2017

@larryhastings next patch implements what you suggested, supporting both "unquoted path" and "'quoted apth' --with --options".

@methane Mercurial solution is elegant, but it will not work with unquoted path.

nirs added 2 commits July 22, 2017 23:51
On Windows users may like to configure command line options for the
editor[1]. Support this configuration.

Fixes #162.

[1] https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
EDITOR environment variable value may be:

- "'quoted path' -with -options"
- "unquoted path"

This previous patch supported only the first; this patch adds support to
for both options.

If an environment value is set, but the value cannot be resolved, the
user has to fix the configuration. There are two possible failures:

The environment variable value does not resolve to a file:

    Error: Invalid 'EDITOR' environment variable "foo bar": Cannot find editor.

Splitting the environment variable failed:

    Error: Invalid 'EDITOR' environment variable "foo bar' -with -options": No closing quotation.
@Mariatta Mariatta added the blurb label Jul 28, 2017
@nirs
Copy link
Contributor Author

nirs commented Aug 24, 2017

@larryhastings, can you review the latest version again?

Copy link
Contributor

@larryhastings larryhastings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, thanks for the PR! I think you forgot to do one thing though ;-)

if os.path.exists(s):
return [s]
# "'quoted path to editor' -with -options"?
args = shlex.split(s, posix=os.name == 'posix')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learned two new things today! The "posix" argument to shlex.split, and os.name is usually "posix".

@@ -864,7 +882,7 @@ def find_editor():
else:
found_path = shutil.which(fallback)
if found_path and os.path.exists(found_path):
return found_path
return [found_path]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bug fix folded in with the rest of the PR? I appreciate it, I just want to double-check. It doesn't look like the rest of the PR touches found_path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks like a bug fix, I can split it to another PR if you like.


handle, tmp_path = tempfile.mkstemp(".rst")
os.close(handle)
atexit.register(lambda : os.unlink(tmp_path))
editor_command.append(tmp_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like your new code is doing nice things, but then doesn't use them. editor_command is ignored after this line; I was assuming it should replace "args".

Also, the code now appends tmp_path twice. This line adds one, and then line 913 in the original (line 932 after your patch is applied) appends tmp_path to "args".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

editor_command is used in line 837. I don't see the append you mention. I'll need to rebase and test this again, lot of time has passed since I sent this :-)

@larryhastings
Copy link
Contributor

This PR seems to be languishing. That's fine, but I would like the return [found_path] bug fix on line 885 to get merged. As you suggest it should arguably be a separate PR anyway.

If you have the time I'd be happy for you to make a one-line PR to get that fix in, so you can get credit for it. Or I can just do it if you don't care about getting credit.

@nirs
Copy link
Contributor Author

nirs commented Sep 19, 2018

I'm sorry but I don't have the time to work on this.

@nirs nirs closed this Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

blurb does not support command line options in EDITOR/GIT_EDITOR
5 participants