🌐 AI搜索 & 代理 主页
blob: c985eee1f9d1c7307b7f533cb2d38de5a42ccb23 [file] [log] [blame]
Stephan Beyerd82f33e2008-07-25 16:28:411#include "cache.h"
Alban Gruin2aed0182018-08-10 16:51:302#include "config.h"
Stephan Beyerd82f33e2008-07-25 16:28:413#include "strbuf.h"
4#include "run-command.h"
Paul Fox913ef362012-11-30 22:41:265#include "sigchain.h"
Stephan Beyerd82f33e2008-07-25 16:28:416
Jonathan Nieder8f4b5762009-10-31 01:44:417#ifndef DEFAULT_EDITOR
8#define DEFAULT_EDITOR "vi"
9#endif
10
Lars Schneidera64f2132017-11-29 14:37:5111int is_terminal_dumb(void)
12{
13 const char *terminal = getenv("TERM");
14 return !terminal || !strcmp(terminal, "dumb");
15}
16
Jonathan Nieder44fcb492009-11-12 00:01:2717const char *git_editor(void)
Stephan Beyerd82f33e2008-07-25 16:28:4118{
Jonathan Niederd33738d2009-11-11 23:56:0719 const char *editor = getenv("GIT_EDITOR");
Lars Schneidera64f2132017-11-29 14:37:5120 int terminal_is_dumb = is_terminal_dumb();
Stephan Beyerd82f33e2008-07-25 16:28:4121
Stephan Beyerd82f33e2008-07-25 16:28:4122 if (!editor && editor_program)
23 editor = editor_program;
Jonathan Niederd33738d2009-11-11 23:56:0724 if (!editor && !terminal_is_dumb)
Stephan Beyerd82f33e2008-07-25 16:28:4125 editor = getenv("VISUAL");
26 if (!editor)
27 editor = getenv("EDITOR");
28
Jonathan Niederd33738d2009-11-11 23:56:0729 if (!editor && terminal_is_dumb)
Jonathan Nieder44fcb492009-11-12 00:01:2730 return NULL;
Stephan Beyerd82f33e2008-07-25 16:28:4131
32 if (!editor)
Jonathan Nieder8f4b5762009-10-31 01:44:4133 editor = DEFAULT_EDITOR;
Stephan Beyerd82f33e2008-07-25 16:28:4134
Jonathan Nieder44fcb492009-11-12 00:01:2735 return editor;
36}
37
Alban Gruin2aed0182018-08-10 16:51:3038const char *git_sequence_editor(void)
Jonathan Nieder44fcb492009-11-12 00:01:2739{
Alban Gruin2aed0182018-08-10 16:51:3040 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
Jonathan Nieder44fcb492009-11-12 00:01:2741
42 if (!editor)
Alban Gruin2aed0182018-08-10 16:51:3043 git_config_get_string_const("sequence.editor", &editor);
44 if (!editor)
45 editor = git_editor();
46
47 return editor;
48}
49
50static int launch_specified_editor(const char *editor, const char *path,
51 struct strbuf *buffer, const char *const *env)
52{
53 if (!editor)
Jonathan Nieder44fcb492009-11-12 00:01:2754 return error("Terminal is dumb, but EDITOR unset");
55
Stephan Beyerd82f33e2008-07-25 16:28:4156 if (strcmp(editor, ":")) {
Ramkumar Ramachandra09c5ae52013-07-28 16:59:4257 const char *args[] = { editor, real_path(path), NULL };
René Scharfed3180272014-08-19 19:09:3558 struct child_process p = CHILD_PROCESS_INIT;
Jeff King12508572012-11-30 22:41:5059 int ret, sig;
Lars Schneiderabfb04d2017-12-07 15:16:4160 int print_waiting_for_editor = advice_waiting_for_editor && isatty(2);
61
62 if (print_waiting_for_editor) {
63 /*
64 * A dumb terminal cannot erase the line later on. Add a
65 * newline to separate the hint from subsequent output.
66 *
67 * Make sure that our message is separated with a whitespace
68 * from further cruft that may be written by the editor.
69 */
70 const char term = is_terminal_dumb() ? '\n' : ' ';
71
72 fprintf(stderr,
73 _("hint: Waiting for your editor to close the file...%c"),
74 term);
75 fflush(stderr);
76 }
Stephan Beyerd82f33e2008-07-25 16:28:4177
Jeff Kingf42ca312012-11-30 22:41:0478 p.argv = args;
79 p.env = env;
80 p.use_shell = 1;
81 if (start_command(&p) < 0)
82 return error("unable to start editor '%s'", editor);
83
Paul Fox913ef362012-11-30 22:41:2684 sigchain_push(SIGINT, SIG_IGN);
85 sigchain_push(SIGQUIT, SIG_IGN);
86 ret = finish_command(&p);
Jeff King709ca732013-01-05 14:49:4987 sig = ret - 128;
Paul Fox913ef362012-11-30 22:41:2688 sigchain_pop(SIGINT);
89 sigchain_pop(SIGQUIT);
Jeff King12508572012-11-30 22:41:5090 if (sig == SIGINT || sig == SIGQUIT)
91 raise(sig);
Paul Fox913ef362012-11-30 22:41:2692 if (ret)
Stephan Beyer71982032008-07-25 16:28:4293 return error("There was a problem with the editor '%s'.",
94 editor);
Lars Schneiderabfb04d2017-12-07 15:16:4195
96 if (print_waiting_for_editor && !is_terminal_dumb())
97 /*
98 * Go back to the beginning and erase the entire line to
99 * avoid wasting the vertical space.
100 */
101 fputs("\r\033[K", stderr);
Stephan Beyerd82f33e2008-07-25 16:28:41102 }
103
104 if (!buffer)
Stephan Beyer71982032008-07-25 16:28:42105 return 0;
Stephan Beyerd82f33e2008-07-25 16:28:41106 if (strbuf_read_file(buffer, path, 0) < 0)
Nguyễn Thái Ngọc Duy9f9a5222016-05-08 09:47:43107 return error_errno("could not read file '%s'", path);
Stephan Beyer71982032008-07-25 16:28:42108 return 0;
Stephan Beyerd82f33e2008-07-25 16:28:41109}
Alban Gruin2aed0182018-08-10 16:51:30110
111int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
112{
113 return launch_specified_editor(git_editor(), path, buffer, env);
114}
115
116int launch_sequence_editor(const char *path, struct strbuf *buffer,
117 const char *const *env)
118{
119 return launch_specified_editor(git_sequence_editor(), path, buffer, env);
120}