4 #include "run-command.h"
6 #include "compat/terminal.h"
9 #define DEFAULT_EDITOR "vi"
12 int is_terminal_dumb(void)
14 const char *terminal
= getenv("TERM");
15 return !terminal
|| !strcmp(terminal
, "dumb");
18 const char *git_editor(void)
20 const char *editor
= getenv("GIT_EDITOR");
21 int terminal_is_dumb
= is_terminal_dumb();
23 if (!editor
&& editor_program
)
24 editor
= editor_program
;
25 if (!editor
&& !terminal_is_dumb
)
26 editor
= getenv("VISUAL");
28 editor
= getenv("EDITOR");
30 if (!editor
&& terminal_is_dumb
)
34 editor
= DEFAULT_EDITOR
;
39 const char *git_sequence_editor(void)
41 const char *editor
= getenv("GIT_SEQUENCE_EDITOR");
44 git_config_get_string_tmp("sequence.editor", &editor
);
46 editor
= git_editor();
51 static int launch_specified_editor(const char *editor
, const char *path
,
52 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 const char *args
[] = { editor
, NULL
, NULL
};
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);
83 args
[1] = realpath
.buf
;
88 p
.trace2_child_class
= "editor";
89 term_fail
= save_term(1);
90 if (start_command(&p
) < 0) {
93 strbuf_release(&realpath
);
94 return error("unable to start editor '%s'", editor
);
97 sigchain_push(SIGINT
, SIG_IGN
);
98 sigchain_push(SIGQUIT
, SIG_IGN
);
99 ret
= finish_command(&p
);
102 strbuf_release(&realpath
);
104 sigchain_pop(SIGINT
);
105 sigchain_pop(SIGQUIT
);
106 if (sig
== SIGINT
|| sig
== SIGQUIT
)
109 return error("There was a problem with the editor '%s'.",
112 if (print_waiting_for_editor
&& !is_terminal_dumb())
114 * Erase the entire line to avoid wasting the
122 if (strbuf_read_file(buffer
, path
, 0) < 0)
123 return error_errno("could not read file '%s'", path
);
127 int launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
129 return launch_specified_editor(git_editor(), path
, buffer
, env
);
132 int launch_sequence_editor(const char *path
, struct strbuf
*buffer
,
133 const char *const *env
)
135 return launch_specified_editor(git_sequence_editor(), path
, buffer
, env
);