4 #include "environment.h"
8 #include "run-command.h"
11 #ifndef DEFAULT_EDITOR
12 #define DEFAULT_EDITOR "vi"
15 int is_terminal_dumb(void)
17 const char *terminal
= getenv("TERM");
18 return !terminal
|| !strcmp(terminal
, "dumb");
21 const char *git_editor(void)
23 const char *editor
= getenv("GIT_EDITOR");
24 int terminal_is_dumb
= is_terminal_dumb();
26 if (!editor
&& editor_program
)
27 editor
= editor_program
;
28 if (!editor
&& !terminal_is_dumb
)
29 editor
= getenv("VISUAL");
31 editor
= getenv("EDITOR");
33 if (!editor
&& terminal_is_dumb
)
37 editor
= DEFAULT_EDITOR
;
42 const char *git_sequence_editor(void)
44 const char *editor
= getenv("GIT_SEQUENCE_EDITOR");
47 git_config_get_string_tmp("sequence.editor", &editor
);
49 editor
= git_editor();
54 static int launch_specified_editor(const char *editor
, const char *path
,
55 struct strbuf
*buffer
, const char *const *env
)
58 return error("Terminal is dumb, but EDITOR unset");
60 if (strcmp(editor
, ":")) {
61 struct strbuf realpath
= STRBUF_INIT
;
62 struct child_process p
= CHILD_PROCESS_INIT
;
64 int print_waiting_for_editor
= advice_enabled(ADVICE_WAITING_FOR_EDITOR
) && isatty(2);
66 if (print_waiting_for_editor
) {
68 * A dumb terminal cannot erase the line later on. Add a
69 * newline to separate the hint from subsequent output.
71 * Make sure that our message is separated with a whitespace
72 * from further cruft that may be written by the editor.
74 const char term
= is_terminal_dumb() ? '\n' : ' ';
77 _("hint: Waiting for your editor to close the file...%c"),
82 strbuf_realpath(&realpath
, path
, 1);
84 strvec_pushl(&p
.args
, editor
, realpath
.buf
, NULL
);
86 strvec_pushv(&p
.env
, (const char **)env
);
88 p
.trace2_child_class
= "editor";
89 if (start_command(&p
) < 0) {
90 strbuf_release(&realpath
);
91 return error("unable to start editor '%s'", editor
);
94 sigchain_push(SIGINT
, SIG_IGN
);
95 sigchain_push(SIGQUIT
, SIG_IGN
);
96 ret
= finish_command(&p
);
97 strbuf_release(&realpath
);
100 sigchain_pop(SIGQUIT
);
101 if (sig
== SIGINT
|| sig
== SIGQUIT
)
104 return error("There was a problem with the editor '%s'.",
107 if (print_waiting_for_editor
&& !is_terminal_dumb())
109 * Erase the entire line to avoid wasting the
117 if (strbuf_read_file(buffer
, path
, 0) < 0)
118 return error_errno("could not read file '%s'", path
);
122 int launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
124 return launch_specified_editor(git_editor(), path
, buffer
, env
);
127 int launch_sequence_editor(const char *path
, struct strbuf
*buffer
,
128 const char *const *env
)
130 return launch_specified_editor(git_sequence_editor(), path
, buffer
, env
);