Move EDITOR discovery to a util function

This commit is contained in:
Adrian Sampson 2015-11-14 13:05:06 -08:00
parent 9c968456c1
commit d5b7ce64da
2 changed files with 26 additions and 18 deletions

View file

@ -1473,7 +1473,7 @@ def config_edit():
An empty config file is created if no existing config file exists.
"""
path = config.user_config_path()
editor = os.environ.get('EDITOR')
editor = util.editor_command()
try:
if not os.path.isfile(path):
open(path, 'w+').close()

View file

@ -736,28 +736,36 @@ def open_anything():
return base_cmd
def interactive_open(targets, command=None):
"""Open the files in `targets` by `exec`ing a new command. (The new
program takes over, and Python execution ends: this does not fork a
subprocess.)
def editor_command():
"""Get a command for opening a text file.
If `command` is provided, use it. Otherwise, use an OS-specific
command (from `open_anything`) to open the file.
Use the `EDITOR` environment variable by default. If it is not
present, fall back to `open_anything()`, the platform-specific tool
for opening files in general.
"""
editor = os.environ.get('EDITOR')
if editor:
return editor
return open_anything()
def interactive_open(targets, command):
"""Open the files in `targets` by `exec`ing a new `command`, given
as a Unicode string. (The new program takes over, and Python
execution ends: this does not fork a subprocess.)
Can raise `OSError`.
"""
if command:
command = command.encode('utf8')
try:
command = [c.decode('utf8')
for c in shlex.split(command)]
except ValueError: # Malformed shell tokens.
command = [command]
command.insert(0, command[0]) # for argv[0]
else:
base_cmd = open_anything()
command = [base_cmd, base_cmd]
# Split the command string into its arguments.
command = command.encode('utf8')
try:
command = [c.decode('utf8')
for c in shlex.split(command)]
except ValueError: # Malformed shell tokens.
command = [command]
command.insert(0, command[0]) # for argv[0]
# Add the explicit arguments.
command += targets
return os.execlp(*command)