Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "strbuf.h" |
| 3 | #include "run-command.h" |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 4 | #include "sigchain.h" |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 5 | |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 6 | #ifndef DEFAULT_EDITOR |
| 7 | #define DEFAULT_EDITOR "vi" |
| 8 | #endif |
| 9 | |
Lars Schneider | a64f213 | 2017-11-29 15:37:51 +0100 | [diff] [blame] | 10 | int is_terminal_dumb(void) |
| 11 | { |
| 12 | const char *terminal = getenv("TERM"); |
| 13 | return !terminal || !strcmp(terminal, "dumb"); |
| 14 | } |
| 15 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 16 | const char *git_editor(void) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 17 | { |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 18 | const char *editor = getenv("GIT_EDITOR"); |
Lars Schneider | a64f213 | 2017-11-29 15:37:51 +0100 | [diff] [blame] | 19 | int terminal_is_dumb = is_terminal_dumb(); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 20 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 21 | if (!editor && editor_program) |
| 22 | editor = editor_program; |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 23 | if (!editor && !terminal_is_dumb) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 24 | editor = getenv("VISUAL"); |
| 25 | if (!editor) |
| 26 | editor = getenv("EDITOR"); |
| 27 | |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 28 | if (!editor && terminal_is_dumb) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 29 | return NULL; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 30 | |
| 31 | if (!editor) |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 32 | editor = DEFAULT_EDITOR; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 33 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 34 | return editor; |
| 35 | } |
| 36 | |
| 37 | int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) |
| 38 | { |
| 39 | const char *editor = git_editor(); |
| 40 | |
| 41 | if (!editor) |
| 42 | return error("Terminal is dumb, but EDITOR unset"); |
| 43 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 44 | if (strcmp(editor, ":")) { |
Ramkumar Ramachandra | 09c5ae5 | 2013-07-28 22:29:42 +0530 | [diff] [blame] | 45 | const char *args[] = { editor, real_path(path), NULL }; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 46 | struct child_process p = CHILD_PROCESS_INIT; |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 47 | int ret, sig; |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 48 | int print_waiting_for_editor = advice_waiting_for_editor && isatty(2); |
| 49 | |
| 50 | if (print_waiting_for_editor) { |
| 51 | /* |
| 52 | * A dumb terminal cannot erase the line later on. Add a |
| 53 | * newline to separate the hint from subsequent output. |
| 54 | * |
| 55 | * Make sure that our message is separated with a whitespace |
| 56 | * from further cruft that may be written by the editor. |
| 57 | */ |
| 58 | const char term = is_terminal_dumb() ? '\n' : ' '; |
| 59 | |
| 60 | fprintf(stderr, |
| 61 | _("hint: Waiting for your editor to close the file...%c"), |
| 62 | term); |
| 63 | fflush(stderr); |
| 64 | } |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 65 | |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 66 | p.argv = args; |
| 67 | p.env = env; |
| 68 | p.use_shell = 1; |
| 69 | if (start_command(&p) < 0) |
| 70 | return error("unable to start editor '%s'", editor); |
| 71 | |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 72 | sigchain_push(SIGINT, SIG_IGN); |
| 73 | sigchain_push(SIGQUIT, SIG_IGN); |
| 74 | ret = finish_command(&p); |
Jeff King | 709ca73 | 2013-01-05 09:49:49 -0500 | [diff] [blame] | 75 | sig = ret - 128; |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 76 | sigchain_pop(SIGINT); |
| 77 | sigchain_pop(SIGQUIT); |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 78 | if (sig == SIGINT || sig == SIGQUIT) |
| 79 | raise(sig); |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 80 | if (ret) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 81 | return error("There was a problem with the editor '%s'.", |
| 82 | editor); |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 83 | |
| 84 | if (print_waiting_for_editor && !is_terminal_dumb()) |
| 85 | /* |
| 86 | * Go back to the beginning and erase the entire line to |
| 87 | * avoid wasting the vertical space. |
| 88 | */ |
| 89 | fputs("\r\033[K", stderr); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | if (!buffer) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 93 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 94 | 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] | 95 | return error_errno("could not read file '%s'", path); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 96 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 97 | } |