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