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