From 356fe582b8d64163fb84f662b37f534633b9c2a7 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Fri, 29 Aug 2014 16:38:04 +0200 Subject: [PATCH] fish: edit: centralize the EDITOR handling Allow null as value for the editor parameter of edit_file_editor, which will then get it from the EDITOR envvar (falling back on vi). This is basically code motion from the two edit_file_editor users to it. --- edit/edit.c | 12 +++--------- fish/edit.c | 7 ++----- fish/file-edit.c | 6 ++++++ fish/file-edit.h | 3 +++ 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/edit/edit.c b/edit/edit.c index f1c3b9175..821f28098 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -333,15 +333,9 @@ edit (const char *filename, const char *root) if (perl_expr != NULL) { r = edit_file_perl (g, filename, perl_expr, backup_extension, verbose); - } else { - const char *editor; - - editor = getenv ("EDITOR"); - if (editor == NULL) - editor = "vi"; - - r = edit_file_editor (g, filename, editor, backup_extension, verbose); - } + } else + r = edit_file_editor (g, filename, NULL /* use $EDITOR */, + backup_extension, verbose); switch (r) { case -1: diff --git a/fish/edit.c b/fish/edit.c index 0f41a8886..013306ee6 100644 --- a/fish/edit.c +++ b/fish/edit.c @@ -47,11 +47,8 @@ run_edit (const char *cmd, size_t argc, char *argv[]) editor = "vi"; else if (STRCASEEQ (cmd, "emacs")) editor = "emacs -nw"; - else { - editor = getenv ("EDITOR"); - if (editor == NULL) - editor = "vi"; /* could be cruel here and choose ed(1) */ - } + else + editor = NULL; /* use $EDITOR */ /* Handle 'win:...' prefix. */ remotefilename = win_prefix (argv[0]); diff --git a/fish/file-edit.c b/fish/file-edit.c index 74cb89b59..e6d25f5ab 100644 --- a/fish/file-edit.c +++ b/fish/file-edit.c @@ -52,6 +52,12 @@ edit_file_editor (guestfs_h *g, const char *filename, const char *editor, int r; struct utimbuf times; + if (editor == NULL) { + editor = getenv ("EDITOR"); + if (editor == NULL) + editor = "vi"; + } + /* Download the file and write it to a temporary. */ if (do_download (g, filename, &tmpfilename) == -1) return -1; diff --git a/fish/file-edit.h b/fish/file-edit.h index 3f8b95e5e..16352542d 100644 --- a/fish/file-edit.h +++ b/fish/file-edit.h @@ -25,6 +25,9 @@ * Edit 'filename' using the specified 'editor' application. * If 'backup_extension' is not null, then a copy of 'filename' is saved * with 'backup_extension' appended to its file name. + * If 'editor' is null, then the EDITOR environment variable + * will be queried for the editor application, leaving "vi" as fallback + * if not set. * * Returns -1 for failure, 0 on success, 1 if the editor did not change * the file (e.g. the user closed the editor without saving).