Skip to content
Snippets Groups Projects
Commit 9b51fb09 authored by Chris Green's avatar Chris Green Committed by Peter Scheibel
Browse files

Support VISUAL environment variable for editing. (#10898)

If the user has set the environment variable VISUAL, it will be used
in preference to EDITOR for all Spack editing activities. If VISUAL
is not set or fails (perhaps due to a lack of graphical editing
capabilities),EDITOR will be used instead. We fall back to one of
several common editors if neither bears fruit.

This feature has been tailored to:

* Provide identical behavior to the previous implementation in the
  case that VISUAL is not set.
* Not require any change to code utilizing the editor feature.
* Follow usual UNIX behavior concerning VISUAL and EDITOR.
parent 1d738683
Branches
Tags
No related merge requests found
...@@ -5,31 +5,37 @@ ...@@ -5,31 +5,37 @@
"""Module for finding the user's preferred text editor. """Module for finding the user's preferred text editor.
Defines one variable: ``editor``, which is a Defines one function, editor(), which invokes the editor defined by the
``spack.util.executable.Executable`` object that can be called to invoke user's VISUAL environment variable if set. We fall back to the editor
the editor. defined by the EDITOR environment variable if VISUAL is not set or the
specified editor fails (e.g. no DISPLAY for a graphical editor). If
If no ``editor`` is found, an ``EnvironmentError`` is raised when neither variable is set, we fall back to one of several common editors,
``editor`` is invoked. raising an EnvironmentError if we are unable to find one.
""" """
import copy
import os import os
from spack.util.executable import Executable, which from spack.util.executable import Executable, which
# Set up the user's editor _visual_exe\
# $EDITOR environment variable has the highest precedence = Executable(os.environ['VISUAL']) if 'VISUAL' in os.environ else None
editor = os.environ.get('EDITOR') _editor_exe\
= Executable(os.environ['EDITOR']) \
if 'EDITOR' in os.environ else which('vim', 'vi', 'emacs', 'nano')
# if editor is not set, use some sensible defaults # Invoke the user's editor.
if editor is not None: def editor(*args, **kwargs):
editor = Executable(editor) if _visual_exe:
else: visual_kwargs = copy.copy(kwargs)
editor = which('vim', 'vi', 'emacs', 'nano') visual_kwargs['fail_on_error'] = False
_visual_exe(*args, **visual_kwargs)
if _visual_exe.returncode == 0:
return # Otherwise, fall back to EDITOR.
# If there is no editor, only raise an error if we actually try to use it. if _editor_exe:
if not editor: _editor_exe(*args, **kwargs)
def editor_not_found(*args, **kwargs): else:
raise EnvironmentError( raise EnvironmentError(
'No text editor found! Please set the EDITOR environment variable ' 'No text editor found! Please set the VISUAL and/or EDITOR '
'to your preferred text editor.') 'environment variable(s) to your preferred text editor.')
editor = editor_not_found
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment