git-prompt: use here-doc instead of here-string
[git/gitster.git] / editor.c
blobd1ba2d7c34fbf4bfa9c9b21c51ada7dd88fcf1e6
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "advice.h"
4 #include "config.h"
5 #include "editor.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "pager.h"
9 #include "path.h"
10 #include "strbuf.h"
11 #include "strvec.h"
12 #include "run-command.h"
13 #include "sigchain.h"
15 #ifndef DEFAULT_EDITOR
16 #define DEFAULT_EDITOR "vi"
17 #endif
19 int is_terminal_dumb(void)
21 const char *terminal = getenv("TERM");
22 return !terminal || !strcmp(terminal, "dumb");
25 const char *git_editor(void)
27 const char *editor = getenv("GIT_EDITOR");
28 int terminal_is_dumb = is_terminal_dumb();
30 if (!editor && editor_program)
31 editor = editor_program;
32 if (!editor && !terminal_is_dumb)
33 editor = getenv("VISUAL");
34 if (!editor)
35 editor = getenv("EDITOR");
37 if (!editor && terminal_is_dumb)
38 return NULL;
40 if (!editor)
41 editor = DEFAULT_EDITOR;
43 return editor;
46 const char *git_sequence_editor(void)
48 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
50 if (!editor)
51 git_config_get_string_tmp("sequence.editor", &editor);
52 if (!editor)
53 editor = git_editor();
55 return editor;
58 static int launch_specified_editor(const char *editor, const char *path,
59 struct strbuf *buffer, const char *const *env)
61 if (!editor)
62 return error("Terminal is dumb, but EDITOR unset");
64 if (strcmp(editor, ":")) {
65 struct strbuf realpath = STRBUF_INIT;
66 struct child_process p = CHILD_PROCESS_INIT;
67 int ret, sig;
68 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
70 if (print_waiting_for_editor) {
72 * A dumb terminal cannot erase the line later on. Add a
73 * newline to separate the hint from subsequent output.
75 * Make sure that our message is separated with a whitespace
76 * from further cruft that may be written by the editor.
78 const char term = is_terminal_dumb() ? '\n' : ' ';
80 fprintf(stderr,
81 _("hint: Waiting for your editor to close the file...%c"),
82 term);
83 fflush(stderr);
86 strbuf_realpath(&realpath, path, 1);
88 strvec_pushl(&p.args, editor, realpath.buf, NULL);
89 if (env)
90 strvec_pushv(&p.env, (const char **)env);
91 p.use_shell = 1;
92 p.trace2_child_class = "editor";
93 if (start_command(&p) < 0) {
94 strbuf_release(&realpath);
95 return error("unable to start editor '%s'", editor);
98 sigchain_push(SIGINT, SIG_IGN);
99 sigchain_push(SIGQUIT, SIG_IGN);
100 ret = finish_command(&p);
101 strbuf_release(&realpath);
102 sig = ret - 128;
103 sigchain_pop(SIGINT);
104 sigchain_pop(SIGQUIT);
105 if (sig == SIGINT || sig == SIGQUIT)
106 raise(sig);
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();
113 if (ret)
114 return error("there was a problem with the editor '%s'",
115 editor);
118 if (!buffer)
119 return 0;
120 if (strbuf_read_file(buffer, path, 0) < 0)
121 return error_errno("could not read file '%s'", path);
122 return 0;
125 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
127 return launch_specified_editor(git_editor(), path, buffer, env);
130 int launch_sequence_editor(const char *path, struct strbuf *buffer,
131 const char *const *env)
133 return launch_specified_editor(git_sequence_editor(), path, buffer, env);
136 int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
137 const char *const *env)
139 char *path2 = NULL;
140 int fd, res = 0;
142 if (!is_absolute_path(path))
143 path = path2 = xstrdup(git_path("%s", path));
145 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
146 if (fd < 0)
147 res = error_errno(_("could not open '%s' for writing"), path);
148 else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
149 res = error_errno(_("could not write to '%s'"), path);
150 close(fd);
151 } else if (close(fd) < 0)
152 res = error_errno(_("could not close '%s'"), path);
153 else {
154 strbuf_reset(buffer);
155 if (launch_editor(path, buffer, env) < 0)
156 res = error_errno(_("could not edit '%s'"), path);
157 unlink(path);
160 free(path2);
161 return res;