aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-02 17:00:29 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-02 17:00:29 +0100
commit9ce4eebcb615f5ce7df9bd23e3ff20e2eab38d82 (patch)
tree4559b09a7508a28d6e577242d5fb7083467e782b
parentb2d8d7f2e936c4862ca5554bb5ffe853a65c04e7 (diff)
Try to use multiple editors to edit files
-rw-r--r--lesana/command.py64
1 files changed, 47 insertions, 17 deletions
diff --git a/lesana/command.py b/lesana/command.py
index 513a5b2..4937ccf 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -13,12 +13,54 @@ from . import Collection, Entry
def edit_file_in_external_editor(filepath):
+ # First we try to use $EDITOR
+ try:
+ editor = os.environ['EDITOR']
+ subprocess.call([editor, filepath])
+ except FileNotFoundError as e:
+ if editor in str(e):
+ logging.info(
+ 'Could not open file {} with $EDITOR (currently {})'.format(
+ filepath,
+ editor
+ )
+ )
+ else:
+ logging.warning("Could not open file {}".format(filepath))
+ return False
+ else:
+ return True
+ # then we try to use sensible-editor (which should be available on
+ # debian and derivatives)
try:
subprocess.call(['sensible-editor', filepath])
except FileNotFoundError as e:
- logging.warning(
- "Could not open new file with editor: {}".format(str(e))
- )
+ if 'sensible-editor' in e.strerror:
+ logging.debug(
+ "Could not open file {} with editor: sensible-editor".format(filepath)
+ )
+ else:
+ logging.warning("Could not open file {}".format(filepath))
+ return False
+ else:
+ return True
+ # and finally we fallback to vi, because ed is the standard editor,
+ # but that would be way too cruel, and vi is also in posix
+ try:
+ subprocess.call(['vi', filepath])
+ except FileNotFoundError as e:
+ if 'vi' in e.strerror:
+ logging.warning(
+ "Could not open file {} with any known editor".format(filepath)
+ )
+ return False
+ else:
+ logging.warning("Could not open file {}".format(filepath))
+ return False
+ else:
+ return True
+
+
class New(gadona.Command):
@@ -37,13 +79,7 @@ class New(gadona.Command):
collection.itemdir,
new_entry.fname
)
- try:
- subprocess.call(['sensible-editor', filepath])
- except FileNotFoundError as e:
- logging.warning(
- "Could not open new file with editor: {}".format(str(e))
- )
- else:
+ if edit_file_in_external_editor(filepath):
collection.update_cache([filepath])
print(new_entry.fname)
@@ -67,13 +103,7 @@ class Edit(gadona.Command):
collection.itemdir,
entry.fname
)
- try:
- subprocess.call(['sensible-editor', filepath])
- except FileNotFoundError as e:
- logging.warning(
- "Could not open new file with editor: {}".format(str(e))
- )
- else:
+ if edit_file_in_external_editor(filepath):
collection.update_cache([filepath])