cocci: remove 'unused.cocci'
[git.git] / editor.c
blobd632d7906608732a1059d197d7f06a62e1f55ed2
1 #include "cache.h"
2 #include "abspath.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "strbuf.h"
7 #include "strvec.h"
8 #include "run-command.h"
9 #include "sigchain.h"
11 #ifndef DEFAULT_EDITOR
12 #define DEFAULT_EDITOR "vi"
13 #endif
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");
30 if (!editor)
31 editor = getenv("EDITOR");
33 if (!editor && terminal_is_dumb)
34 return NULL;
36 if (!editor)
37 editor = DEFAULT_EDITOR;
39 return editor;
42 const char *git_sequence_editor(void)
44 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
46 if (!editor)
47 git_config_get_string_tmp("sequence.editor", &editor);
48 if (!editor)
49 editor = git_editor();
51 return editor;
54 static int launch_specified_editor(const char *editor, const char *path,
55 struct strbuf *buffer, const char *const *env)
57 if (!editor)
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;
63 int ret, sig;
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' : ' ';
76 fprintf(stderr,
77 _("hint: Waiting for your editor to close the file...%c"),
78 term);
79 fflush(stderr);
82 strbuf_realpath(&realpath, path, 1);
84 strvec_pushl(&p.args, editor, realpath.buf, NULL);
85 if (env)
86 strvec_pushv(&p.env, (const char **)env);
87 p.use_shell = 1;
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);
98 sig = ret - 128;
99 sigchain_pop(SIGINT);
100 sigchain_pop(SIGQUIT);
101 if (sig == SIGINT || sig == SIGQUIT)
102 raise(sig);
103 if (ret)
104 return error("There was a problem with the editor '%s'.",
105 editor);
107 if (print_waiting_for_editor && !is_terminal_dumb())
109 * Erase the entire line to avoid wasting the
110 * vertical space.
112 term_clear_line();
115 if (!buffer)
116 return 0;
117 if (strbuf_read_file(buffer, path, 0) < 0)
118 return error_errno("could not read file '%s'", path);
119 return 0;
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);