4 #include "run-command.h"
8 #define DEFAULT_EDITOR "vi"
11 int is_terminal_dumb(void)
13 const char *terminal = getenv("TERM");
14 return !terminal || !strcmp(terminal, "dumb");
17 const char *git_editor(void)
19 const char *editor = getenv("GIT_EDITOR");
20 int terminal_is_dumb = is_terminal_dumb();
22 if (!editor && editor_program)
23 editor = editor_program;
24 if (!editor && !terminal_is_dumb)
25 editor = getenv("VISUAL");
27 editor = getenv("EDITOR");
29 if (!editor && terminal_is_dumb)
33 editor = DEFAULT_EDITOR;
38 const char *git_sequence_editor(void)
40 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
43 git_config_get_string_const("sequence.editor", &editor);
45 editor = git_editor();
50 static int launch_specified_editor(const char *editor, const char *path,
51 struct strbuf *buffer, const char *const *env)
54 return error("Terminal is dumb, but EDITOR unset");
56 if (strcmp(editor, ":")) {
57 const char *args[] = { editor, real_path(path), NULL };
58 struct child_process p = CHILD_PROCESS_INIT;
60 int print_waiting_for_editor = advice_waiting_for_editor && isatty(2);
62 if (print_waiting_for_editor) {
64 * A dumb terminal cannot erase the line later on. Add a
65 * newline to separate the hint from subsequent output.
67 * Make sure that our message is separated with a whitespace
68 * from further cruft that may be written by the editor.
70 const char term = is_terminal_dumb() ? '\n' : ' ';
73 _("hint: Waiting for your editor to close the file...%c"),
81 p.trace2_child_class = "editor";
82 if (start_command(&p) < 0)
83 return error("unable to start editor '%s'", editor);
85 sigchain_push(SIGINT, SIG_IGN);
86 sigchain_push(SIGQUIT, SIG_IGN);
87 ret = finish_command(&p);
90 sigchain_pop(SIGQUIT);
91 if (sig == SIGINT || sig == SIGQUIT)
94 return error("There was a problem with the editor '%s'.",
97 if (print_waiting_for_editor && !is_terminal_dumb())
99 * Go back to the beginning and erase the entire line to
100 * avoid wasting the vertical space.
102 fputs("\r\033[K", stderr);
107 if (strbuf_read_file(buffer, path, 0) < 0)
108 return error_errno("could not read file '%s'", path);
112 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
114 return launch_specified_editor(git_editor(), path, buffer, env);
117 int launch_sequence_editor(const char *path, struct strbuf *buffer,
118 const char *const *env)
120 return launch_specified_editor(git_sequence_editor(), path, buffer, env);