3 #include "run-command.h"
7 #define DEFAULT_EDITOR "vi"
10 int is_terminal_dumb(void)
12 const char *terminal
= getenv("TERM");
13 return !terminal
|| !strcmp(terminal
, "dumb");
16 const char *git_editor(void)
18 const char *editor
= getenv("GIT_EDITOR");
19 int terminal_is_dumb
= is_terminal_dumb();
21 if (!editor
&& editor_program
)
22 editor
= editor_program
;
23 if (!editor
&& !terminal_is_dumb
)
24 editor
= getenv("VISUAL");
26 editor
= getenv("EDITOR");
28 if (!editor
&& terminal_is_dumb
)
32 editor
= DEFAULT_EDITOR
;
37 int launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
39 const char *editor
= git_editor();
42 return error("Terminal is dumb, but EDITOR unset");
44 if (strcmp(editor
, ":")) {
45 const char *args
[] = { editor
, real_path(path
), NULL
};
46 struct child_process p
= CHILD_PROCESS_INIT
;
48 int print_waiting_for_editor
= advice_waiting_for_editor
&& isatty(2);
50 if (print_waiting_for_editor
) {
52 * A dumb terminal cannot erase the line later on. Add a
53 * newline to separate the hint from subsequent output.
55 * Make sure that our message is separated with a whitespace
56 * from further cruft that may be written by the editor.
58 const char term
= is_terminal_dumb() ? '\n' : ' ';
61 _("hint: Waiting for your editor to close the file...%c"),
69 if (start_command(&p
) < 0)
70 return error("unable to start editor '%s'", editor
);
72 sigchain_push(SIGINT
, SIG_IGN
);
73 sigchain_push(SIGQUIT
, SIG_IGN
);
74 ret
= finish_command(&p
);
77 sigchain_pop(SIGQUIT
);
78 if (sig
== SIGINT
|| sig
== SIGQUIT
)
81 return error("There was a problem with the editor '%s'.",
84 if (print_waiting_for_editor
&& !is_terminal_dumb())
86 * Go back to the beginning and erase the entire line to
87 * avoid wasting the vertical space.
89 fputs("\r\033[K", stderr
);
94 if (strbuf_read_file(buffer
, path
, 0) < 0)
95 return error_errno("could not read file '%s'", path
);