]>
Commit | Line | Data |
---|---|---|
77f091ed | 1 | #include "git-compat-util.h" |
0b027f6c | 2 | #include "abspath.h" |
6c6ddf92 | 3 | #include "advice.h" |
2aed0181 | 4 | #include "config.h" |
4e120823 | 5 | #include "editor.h" |
32a8f510 | 6 | #include "environment.h" |
f394e093 | 7 | #include "gettext.h" |
ca4eed70 | 8 | #include "pager.h" |
d1cbe1e6 | 9 | #include "path.h" |
d82f33e2 | 10 | #include "strbuf.h" |
c7c4bdec | 11 | #include "strvec.h" |
d82f33e2 | 12 | #include "run-command.h" |
913ef360 | 13 | #include "sigchain.h" |
d82f33e2 | 14 | |
8f4b576a JN |
15 | #ifndef DEFAULT_EDITOR |
16 | #define DEFAULT_EDITOR "vi" | |
17 | #endif | |
18 | ||
a64f213d LS |
19 | int is_terminal_dumb(void) |
20 | { | |
21 | const char *terminal = getenv("TERM"); | |
22 | return !terminal || !strcmp(terminal, "dumb"); | |
23 | } | |
24 | ||
44fcb497 | 25 | const char *git_editor(void) |
d82f33e2 | 26 | { |
d33738d7 | 27 | const char *editor = getenv("GIT_EDITOR"); |
a64f213d | 28 | int terminal_is_dumb = is_terminal_dumb(); |
d82f33e2 | 29 | |
d82f33e2 SB |
30 | if (!editor && editor_program) |
31 | editor = editor_program; | |
d33738d7 | 32 | if (!editor && !terminal_is_dumb) |
d82f33e2 SB |
33 | editor = getenv("VISUAL"); |
34 | if (!editor) | |
35 | editor = getenv("EDITOR"); | |
36 | ||
d33738d7 | 37 | if (!editor && terminal_is_dumb) |
44fcb497 | 38 | return NULL; |
d82f33e2 SB |
39 | |
40 | if (!editor) | |
8f4b576a | 41 | editor = DEFAULT_EDITOR; |
d82f33e2 | 42 | |
44fcb497 JN |
43 | return editor; |
44 | } | |
45 | ||
2aed0181 | 46 | const char *git_sequence_editor(void) |
44fcb497 | 47 | { |
2aed0181 AG |
48 | const char *editor = getenv("GIT_SEQUENCE_EDITOR"); |
49 | ||
50 | if (!editor) | |
f1de981e | 51 | git_config_get_string_tmp("sequence.editor", &editor); |
2aed0181 AG |
52 | if (!editor) |
53 | editor = git_editor(); | |
44fcb497 | 54 | |
2aed0181 AG |
55 | return editor; |
56 | } | |
57 | ||
58 | static int launch_specified_editor(const char *editor, const char *path, | |
59 | struct strbuf *buffer, const char *const *env) | |
60 | { | |
44fcb497 JN |
61 | if (!editor) |
62 | return error("Terminal is dumb, but EDITOR unset"); | |
63 | ||
d82f33e2 | 64 | if (strcmp(editor, ":")) { |
3d7747e3 | 65 | struct strbuf realpath = STRBUF_INIT; |
d3180279 | 66 | struct child_process p = CHILD_PROCESS_INIT; |
1250857c | 67 | int ret, sig; |
ed9bff08 | 68 | int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2); |
abfb04d0 LS |
69 | |
70 | if (print_waiting_for_editor) { | |
71 | /* | |
72 | * A dumb terminal cannot erase the line later on. Add a | |
73 | * newline to separate the hint from subsequent output. | |
74 | * | |
75 | * Make sure that our message is separated with a whitespace | |
76 | * from further cruft that may be written by the editor. | |
77 | */ | |
78 | const char term = is_terminal_dumb() ? '\n' : ' '; | |
79 | ||
80 | fprintf(stderr, | |
81 | _("hint: Waiting for your editor to close the file...%c"), | |
82 | term); | |
83 | fflush(stderr); | |
84 | } | |
d82f33e2 | 85 | |
3d7747e3 | 86 | strbuf_realpath(&realpath, path, 1); |
3d7747e3 | 87 | |
2b709893 | 88 | strvec_pushl(&p.args, editor, realpath.buf, NULL); |
c7c4bdec | 89 | if (env) |
29fda24d | 90 | strvec_pushv(&p.env, (const char **)env); |
f42ca31d | 91 | p.use_shell = 1; |
eee73d1d | 92 | p.trace2_child_class = "editor"; |
3d7747e3 AM |
93 | if (start_command(&p) < 0) { |
94 | strbuf_release(&realpath); | |
f42ca31d | 95 | return error("unable to start editor '%s'", editor); |
3d7747e3 | 96 | } |
f42ca31d | 97 | |
913ef360 PF |
98 | sigchain_push(SIGINT, SIG_IGN); |
99 | sigchain_push(SIGQUIT, SIG_IGN); | |
100 | ret = finish_command(&p); | |
3d7747e3 | 101 | strbuf_release(&realpath); |
709ca730 | 102 | sig = ret - 128; |
913ef360 PF |
103 | sigchain_pop(SIGINT); |
104 | sigchain_pop(SIGQUIT); | |
1250857c JK |
105 | if (sig == SIGINT || sig == SIGQUIT) |
106 | raise(sig); | |
913ef360 | 107 | if (ret) |
7198203a SB |
108 | return error("There was a problem with the editor '%s'.", |
109 | editor); | |
abfb04d0 LS |
110 | |
111 | if (print_waiting_for_editor && !is_terminal_dumb()) | |
112 | /* | |
cd1096b2 SG |
113 | * Erase the entire line to avoid wasting the |
114 | * vertical space. | |
abfb04d0 | 115 | */ |
cd1096b2 | 116 | term_clear_line(); |
d82f33e2 SB |
117 | } |
118 | ||
119 | if (!buffer) | |
7198203a | 120 | return 0; |
d82f33e2 | 121 | if (strbuf_read_file(buffer, path, 0) < 0) |
9f9a522c | 122 | return error_errno("could not read file '%s'", path); |
7198203a | 123 | return 0; |
d82f33e2 | 124 | } |
2aed0181 AG |
125 | |
126 | int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) | |
127 | { | |
128 | return launch_specified_editor(git_editor(), path, buffer, env); | |
129 | } | |
130 | ||
131 | int launch_sequence_editor(const char *path, struct strbuf *buffer, | |
132 | const char *const *env) | |
133 | { | |
134 | return launch_specified_editor(git_sequence_editor(), path, buffer, env); | |
135 | } | |
4e120823 EN |
136 | |
137 | int strbuf_edit_interactively(struct strbuf *buffer, const char *path, | |
138 | const char *const *env) | |
139 | { | |
140 | char *path2 = NULL; | |
141 | int fd, res = 0; | |
142 | ||
143 | if (!is_absolute_path(path)) | |
144 | path = path2 = xstrdup(git_path("%s", path)); | |
145 | ||
146 | fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
147 | if (fd < 0) | |
148 | res = error_errno(_("could not open '%s' for writing"), path); | |
149 | else if (write_in_full(fd, buffer->buf, buffer->len) < 0) { | |
150 | res = error_errno(_("could not write to '%s'"), path); | |
151 | close(fd); | |
152 | } else if (close(fd) < 0) | |
153 | res = error_errno(_("could not close '%s'"), path); | |
154 | else { | |
155 | strbuf_reset(buffer); | |
156 | if (launch_editor(path, buffer, env) < 0) | |
157 | res = error_errno(_("could not edit '%s'"), path); | |
158 | unlink(path); | |
159 | } | |
160 | ||
161 | free(path2); | |
162 | return res; | |
163 | } |