run-command API users: use strvec_push(), not argv construction
[git/debian.git] / editor.c
blobd92a8d9ab5bb4f48d2eff713464f1da744ce50b1
1 #include "cache.h"
2 #include "config.h"
3 #include "strbuf.h"
4 #include "run-command.h"
5 #include "sigchain.h"
7 #ifndef DEFAULT_EDITOR
8 #define DEFAULT_EDITOR "vi"
9 #endif
11 int is_terminal_dumb(void)
13 const char *terminal = getenv("TERM");
14 return !terminal || !strcmp(terminal, "dumb");
17 const char *git_editor(void)
19 const char *editor = getenv("GIT_EDITOR");
20 int terminal_is_dumb = is_terminal_dumb();
22 if (!editor && editor_program)
23 editor = editor_program;
24 if (!editor && !terminal_is_dumb)
25 editor = getenv("VISUAL");
26 if (!editor)
27 editor = getenv("EDITOR");
29 if (!editor && terminal_is_dumb)
30 return NULL;
32 if (!editor)
33 editor = DEFAULT_EDITOR;
35 return editor;
38 const char *git_sequence_editor(void)
40 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
42 if (!editor)
43 git_config_get_string_tmp("sequence.editor", &editor);
44 if (!editor)
45 editor = git_editor();
47 return editor;
50 static int launch_specified_editor(const char *editor, const char *path,
51 struct strbuf *buffer, const char *const *env)
53 if (!editor)
54 return error("Terminal is dumb, but EDITOR unset");
56 if (strcmp(editor, ":")) {
57 struct strbuf realpath = STRBUF_INIT;
58 struct child_process p = CHILD_PROCESS_INIT;
59 int ret, sig;
60 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
62 if (print_waiting_for_editor) {
64 * A dumb terminal cannot erase the line later on. Add a
65 * newline to separate the hint from subsequent output.
67 * Make sure that our message is separated with a whitespace
68 * from further cruft that may be written by the editor.
70 const char term = is_terminal_dumb() ? '\n' : ' ';
72 fprintf(stderr,
73 _("hint: Waiting for your editor to close the file...%c"),
74 term);
75 fflush(stderr);
78 strbuf_realpath(&realpath, path, 1);
80 strvec_pushl(&p.args, editor, realpath.buf, NULL);
81 p.env = env;
82 p.use_shell = 1;
83 p.trace2_child_class = "editor";
84 if (start_command(&p) < 0) {
85 strbuf_release(&realpath);
86 return error("unable to start editor '%s'", editor);
89 sigchain_push(SIGINT, SIG_IGN);
90 sigchain_push(SIGQUIT, SIG_IGN);
91 ret = finish_command(&p);
92 strbuf_release(&realpath);
93 sig = ret - 128;
94 sigchain_pop(SIGINT);
95 sigchain_pop(SIGQUIT);
96 if (sig == SIGINT || sig == SIGQUIT)
97 raise(sig);
98 if (ret)
99 return error("There was a problem with the editor '%s'.",
100 editor);
102 if (print_waiting_for_editor && !is_terminal_dumb())
104 * Erase the entire line to avoid wasting the
105 * vertical space.
107 term_clear_line();
110 if (!buffer)
111 return 0;
112 if (strbuf_read_file(buffer, path, 0) < 0)
113 return error_errno("could not read file '%s'", path);
114 return 0;
117 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
119 return launch_specified_editor(git_editor(), path, buffer, env);
122 int launch_sequence_editor(const char *path, struct strbuf *buffer,
123 const char *const *env)
125 return launch_specified_editor(git_sequence_editor(), path, buffer, env);