diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2014-10-17 15:43:48 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2014-10-17 15:43:48 +0200 |
| commit | e9180cf83ade29192d8407337f614e5a024017a9 (patch) | |
| tree | 0aa207c91d5bdb999a260ba54de015916dfbea5b /vis.c | |
| parent | 6be820495d70e6f10a92fbdaf6a1e4a278b02ab6 (diff) | |
| download | vis-e9180cf83ade29192d8407337f614e5a024017a9.tar.gz vis-e9180cf83ade29192d8407337f614e5a024017a9.tar.xz | |
Make editor usable as a filter: echo foo | vis - | cat
The terminal output is by default redirected to stderr,
making stdout available for communications purposes.
If a file is "opened" from stdin (i.e. vis is given '-' as
argument) and a subsequent write without a filename is performed
as in ":wq" the output is written to stdout.
Diffstat (limited to 'vis.c')
| -rw-r--r-- | vis.c | 26 |
1 files changed, 16 insertions, 10 deletions
@@ -1416,6 +1416,8 @@ static bool cmd_write(const char *argv[]) { if (!argv[1]) argv[1] = text_filename_get(text); if (!argv[1]) { + if (text_fd_get(text) == STDIN_FILENO) + return text_write(text, STDOUT_FILENO) >= 0; editor_info_show(vis, "Filename expected"); return false; } @@ -1490,6 +1492,15 @@ static struct Screen { bool need_resize; } screen = { .need_resize = true }; +static void die(const char *errstr, ...) { + va_list ap; + endwin(); + va_start(ap, errstr); + vfprintf(stderr, errstr, ap); + va_end(ap); + exit(EXIT_FAILURE); +} + static void sigwinch_handler(int sig) { screen.need_resize = true; } @@ -1513,7 +1524,11 @@ static void setup() { setlocale(LC_CTYPE, ""); if (!getenv("ESCDELAY")) set_escdelay(50); - initscr(); + char *term = getenv("TERM"); + if (!term) + term = DEFAULT_TERM; + if (!newterm(term, stderr, stdin) == ERR) + die("Can not initialize terminal\n"); start_color(); raw(); noecho(); @@ -1574,15 +1589,6 @@ static Key getkey(void) { return key; } -static void die(const char *errstr, ...) { - va_list ap; - endwin(); - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); - va_end(ap); - exit(EXIT_FAILURE); -} - static void mainloop() { struct timeval idle = { .tv_usec = 0 }, *timeout = NULL; Key key, key_prev, *key_mod = NULL; |
