add -i, send-email, svn, p4, etc: use "git var GIT_EDITOR"
[git/dscho.git] / editor.c
blob70618f106699ececf18357b63f1cf394d1f2f3bb
1 #include "cache.h"
2 #include "strbuf.h"
3 #include "run-command.h"
5 const char *git_editor(void)
7 const char *editor = getenv("GIT_EDITOR");
8 const char *terminal = getenv("TERM");
9 int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
11 if (!editor && editor_program)
12 editor = editor_program;
13 if (!editor && !terminal_is_dumb)
14 editor = getenv("VISUAL");
15 if (!editor)
16 editor = getenv("EDITOR");
18 if (!editor && terminal_is_dumb)
19 return NULL;
21 if (!editor)
22 editor = "vi";
24 return editor;
27 int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
29 const char *editor = git_editor();
31 if (!editor)
32 return error("Terminal is dumb, but EDITOR unset");
34 if (strcmp(editor, ":")) {
35 size_t len = strlen(editor);
36 int i = 0;
37 int failed;
38 const char *args[6];
39 struct strbuf arg0 = STRBUF_INIT;
41 if (strcspn(editor, "|&;<>()$`\\\"' \t\n*?[#~=%") != len) {
42 /* there are specials */
43 strbuf_addf(&arg0, "%s \"$@\"", editor);
44 args[i++] = "sh";
45 args[i++] = "-c";
46 args[i++] = arg0.buf;
48 args[i++] = editor;
49 args[i++] = path;
50 args[i] = NULL;
52 failed = run_command_v_opt_cd_env(args, 0, NULL, env);
53 strbuf_release(&arg0);
54 if (failed)
55 return error("There was a problem with the editor '%s'.",
56 editor);
59 if (!buffer)
60 return 0;
61 if (strbuf_read_file(buffer, path, 0) < 0)
62 return error("could not read file '%s': %s",
63 path, strerror(errno));
64 return 0;