treewide: be explicit about dependence on oid-array.h
[alt-git.git] / editor.c
blob3bea3ef72f7eb0f867eac6a8ecd7467d1e878b82
1 #include "cache.h"
2 #include "abspath.h"
3 #include "advice.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "strbuf.h"
8 #include "strvec.h"
9 #include "run-command.h"
10 #include "sigchain.h"
12 #ifndef DEFAULT_EDITOR
13 #define DEFAULT_EDITOR "vi"
14 #endif
16 int is_terminal_dumb(void)
18 const char *terminal = getenv("TERM");
19 return !terminal || !strcmp(terminal, "dumb");
22 const char *git_editor(void)
24 const char *editor = getenv("GIT_EDITOR");
25 int terminal_is_dumb = is_terminal_dumb();
27 if (!editor && editor_program)
28 editor = editor_program;
29 if (!editor && !terminal_is_dumb)
30 editor = getenv("VISUAL");
31 if (!editor)
32 editor = getenv("EDITOR");
34 if (!editor && terminal_is_dumb)
35 return NULL;
37 if (!editor)
38 editor = DEFAULT_EDITOR;
40 return editor;
43 const char *git_sequence_editor(void)
45 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
47 if (!editor)
48 git_config_get_string_tmp("sequence.editor", &editor);
49 if (!editor)
50 editor = git_editor();
52 return editor;
55 static int launch_specified_editor(const char *editor, const char *path,
56 struct strbuf *buffer, const char *const *env)
58 if (!editor)
59 return error("Terminal is dumb, but EDITOR unset");
61 if (strcmp(editor, ":")) {
62 struct strbuf realpath = STRBUF_INIT;
63 struct child_process p = CHILD_PROCESS_INIT;
64 int ret, sig;
65 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
67 if (print_waiting_for_editor) {
69 * A dumb terminal cannot erase the line later on. Add a
70 * newline to separate the hint from subsequent output.
72 * Make sure that our message is separated with a whitespace
73 * from further cruft that may be written by the editor.
75 const char term = is_terminal_dumb() ? '\n' : ' ';
77 fprintf(stderr,
78 _("hint: Waiting for your editor to close the file...%c"),
79 term);
80 fflush(stderr);
83 strbuf_realpath(&realpath, path, 1);
85 strvec_pushl(&p.args, editor, realpath.buf, NULL);
86 if (env)
87 strvec_pushv(&p.env, (const char **)env);
88 p.use_shell = 1;
89 p.trace2_child_class = "editor";
90 if (start_command(&p) < 0) {
91 strbuf_release(&realpath);
92 return error("unable to start editor '%s'", editor);
95 sigchain_push(SIGINT, SIG_IGN);
96 sigchain_push(SIGQUIT, SIG_IGN);
97 ret = finish_command(&p);
98 strbuf_release(&realpath);
99 sig = ret - 128;
100 sigchain_pop(SIGINT);
101 sigchain_pop(SIGQUIT);
102 if (sig == SIGINT || sig == SIGQUIT)
103 raise(sig);
104 if (ret)
105 return error("There was a problem with the editor '%s'.",
106 editor);
108 if (print_waiting_for_editor && !is_terminal_dumb())
110 * Erase the entire line to avoid wasting the
111 * vertical space.
113 term_clear_line();
116 if (!buffer)
117 return 0;
118 if (strbuf_read_file(buffer, path, 0) < 0)
119 return error_errno("could not read file '%s'", path);
120 return 0;
123 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
125 return launch_specified_editor(git_editor(), path, buffer, env);
128 int launch_sequence_editor(const char *path, struct strbuf *buffer,
129 const char *const *env)
131 return launch_specified_editor(git_sequence_editor(), path, buffer, env);