Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 2 | #include "config.h" |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 3 | #include "strbuf.h" |
| 4 | #include "run-command.h" |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 5 | #include "sigchain.h" |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 6 | |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 7 | #ifndef DEFAULT_EDITOR |
| 8 | #define DEFAULT_EDITOR "vi" |
| 9 | #endif |
| 10 | |
Lars Schneider | a64f213 | 2017-11-29 15:37:51 +0100 | [diff] [blame] | 11 | int is_terminal_dumb(void) |
| 12 | { |
| 13 | const char *terminal = getenv("TERM"); |
| 14 | return !terminal || !strcmp(terminal, "dumb"); |
| 15 | } |
| 16 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 17 | const char *git_editor(void) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 18 | { |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 19 | const char *editor = getenv("GIT_EDITOR"); |
Lars Schneider | a64f213 | 2017-11-29 15:37:51 +0100 | [diff] [blame] | 20 | int terminal_is_dumb = is_terminal_dumb(); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 21 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 22 | if (!editor && editor_program) |
| 23 | editor = editor_program; |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 24 | if (!editor && !terminal_is_dumb) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 25 | editor = getenv("VISUAL"); |
| 26 | if (!editor) |
| 27 | editor = getenv("EDITOR"); |
| 28 | |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 29 | if (!editor && terminal_is_dumb) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 30 | return NULL; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 31 | |
| 32 | if (!editor) |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 33 | editor = DEFAULT_EDITOR; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 34 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 35 | return editor; |
| 36 | } |
| 37 | |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 38 | const char *git_sequence_editor(void) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 39 | { |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 40 | const char *editor = getenv("GIT_SEQUENCE_EDITOR"); |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 41 | |
| 42 | if (!editor) |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 43 | git_config_get_string_const("sequence.editor", &editor); |
| 44 | if (!editor) |
| 45 | editor = git_editor(); |
| 46 | |
| 47 | return editor; |
| 48 | } |
| 49 | |
| 50 | static int launch_specified_editor(const char *editor, const char *path, |
| 51 | struct strbuf *buffer, const char *const *env) |
| 52 | { |
| 53 | if (!editor) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 54 | return error("Terminal is dumb, but EDITOR unset"); |
| 55 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 56 | if (strcmp(editor, ":")) { |
Ramkumar Ramachandra | 09c5ae5 | 2013-07-28 22:29:42 +0530 | [diff] [blame] | 57 | const char *args[] = { editor, real_path(path), NULL }; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 58 | struct child_process p = CHILD_PROCESS_INIT; |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 59 | int ret, sig; |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 60 | 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 Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 77 | |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 78 | p.argv = args; |
| 79 | p.env = env; |
| 80 | p.use_shell = 1; |
Jeff Hostetler | eee73d1 | 2019-02-22 14:25:04 -0800 | [diff] [blame] | 81 | p.trace2_child_class = "editor"; |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 82 | if (start_command(&p) < 0) |
| 83 | return error("unable to start editor '%s'", editor); |
| 84 | |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 85 | sigchain_push(SIGINT, SIG_IGN); |
| 86 | sigchain_push(SIGQUIT, SIG_IGN); |
| 87 | ret = finish_command(&p); |
Jeff King | 709ca73 | 2013-01-05 09:49:49 -0500 | [diff] [blame] | 88 | sig = ret - 128; |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 89 | sigchain_pop(SIGINT); |
| 90 | sigchain_pop(SIGQUIT); |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 91 | if (sig == SIGINT || sig == SIGQUIT) |
| 92 | raise(sig); |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 93 | if (ret) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 94 | return error("There was a problem with the editor '%s'.", |
| 95 | editor); |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 96 | |
| 97 | if (print_waiting_for_editor && !is_terminal_dumb()) |
| 98 | /* |
SZEDER Gábor | cd1096b | 2019-06-24 20:13:16 +0200 | [diff] [blame] | 99 | * Erase the entire line to avoid wasting the |
| 100 | * vertical space. |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 101 | */ |
SZEDER Gábor | cd1096b | 2019-06-24 20:13:16 +0200 | [diff] [blame] | 102 | term_clear_line(); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (!buffer) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 106 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 107 | if (strbuf_read_file(buffer, path, 0) < 0) |
Nguyễn Thái Ngọc Duy | 9f9a522 | 2016-05-08 16:47:43 +0700 | [diff] [blame] | 108 | return error_errno("could not read file '%s'", path); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 109 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 110 | } |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 111 | |
| 112 | int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) |
| 113 | { |
| 114 | return launch_specified_editor(git_editor(), path, buffer, env); |
| 115 | } |
| 116 | |
| 117 | int launch_sequence_editor(const char *path, struct strbuf *buffer, |
| 118 | const char *const *env) |
| 119 | { |
| 120 | return launch_specified_editor(git_sequence_editor(), path, buffer, env); |
| 121 | } |