summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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])