7 #include "run-command.h"
10 #ifndef DEFAULT_EDITOR
11 #define DEFAULT_EDITOR "vi"
14 int is_terminal_dumb(void)
16 const char *terminal
= getenv("TERM");
17 return !terminal
|| !strcmp(terminal
, "dumb");
20 const char *git_editor(void)
22 const char *editor
= getenv("GIT_EDITOR");
23 int terminal_is_dumb
= is_terminal_dumb();
25 if (!editor
&& editor_program
)
26 editor
= editor_program
;
27 if (!editor
&& !terminal_is_dumb
)
28 editor
= getenv("VISUAL");
30 editor
= getenv("EDITOR");
32 if (!editor
&& terminal_is_dumb
)
36 editor
= DEFAULT_EDITOR
;
41 const char *git_sequence_editor(void)
43 const char *editor
= getenv("GIT_SEQUENCE_EDITOR");
46 git_config_get_string_tmp("sequence.editor", &editor
);
48 editor
= git_editor();
53 static int launch_specified_editor(const char *editor
, const char *path
,
54 struct strbuf
*buffer
, const char *const *env
)
57 return error("Terminal is dumb, but EDITOR unset");
59 if (strcmp(editor
, ":")) {
60 struct strbuf realpath
= STRBUF_INIT
;
61 struct child_process p
= CHILD_PROCESS_INIT
;
63 int print_waiting_for_editor
= advice_enabled(ADVICE_WAITING_FOR_EDITOR
) && isatty(2);
65 if (print_waiting_for_editor
) {
67 * A dumb terminal cannot erase the line later on. Add a
68 * newline to separate the hint from subsequent output.
70 * Make sure that our message is separated with a whitespace
71 * from further cruft that may be written by the editor.
73 const char term
= is_terminal_dumb() ? '\n' : ' ';
76 _("hint: Waiting for your editor to close the file...%c"),
81 strbuf_realpath(&realpath
, path
, 1);
83 strvec_pushl(&p
.args
, editor
, realpath
.buf
, NULL
);
85 strvec_pushv(&p
.env
, (const char **)env
);
87 p
.trace2_child_class
= "editor";
88 if (start_command(&p
) < 0) {
89 strbuf_release(&realpath
);
90 return error("unable to start editor '%s'", editor
);
93 sigchain_push(SIGINT
, SIG_IGN
);
94 sigchain_push(SIGQUIT
, SIG_IGN
);
95 ret
= finish_command(&p
);
96 strbuf_release(&realpath
);
99 sigchain_pop(SIGQUIT
);
100 if (sig
== SIGINT
|| sig
== SIGQUIT
)
103 return error("There was a problem with the editor '%s'.",
106 if (print_waiting_for_editor
&& !is_terminal_dumb())
108 * Erase the entire line to avoid wasting the
116 if (strbuf_read_file(buffer
, path
, 0) < 0)
117 return error_errno("could not read file '%s'", path
);
121 int launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
123 return launch_specified_editor(git_editor(), path
, buffer
, env
);
126 int launch_sequence_editor(const char *path
, struct strbuf
*buffer
,
127 const char *const *env
)
129 return launch_specified_editor(git_sequence_editor(), path
, buffer
, env
);