vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 2a8532c222ba3264ca4db1a8f8e8a124c8b7a9a4
parent 1ba81bb08e343f744df0c8f85d9c8a005e094cc7
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Tue, 21 Oct 2014 16:50:53 +0200

Read text to edit from stdin if it isn't a terminal

This allows usage as a filter without specifying "-" as file name.

Diffstat:
Mvis.c | 25+++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/vis.c b/vis.c @@ -1685,14 +1685,8 @@ int main(int argc, char *argv[]) { for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { - case '\0': - if (!editor_window_new_fd(vis, STDIN_FILENO)) - die("Can not read from stdin\n"); - int fd = open("/dev/tty", O_RDONLY); - if (fd == -1) - die("Can not reopen stdin\n"); - dup2(fd, STDIN_FILENO); - close(fd); + /* handle command line arguments */ + default: break; } } else if (!editor_window_new(vis, argv[i])) { @@ -1700,8 +1694,19 @@ int main(int argc, char *argv[]) { } } - if (!vis->windows && !editor_window_new(vis, NULL)) - die("Can not create empty buffer\n"); + if (!vis->windows) { + if (!strcmp(argv[argc-1], "-") || !isatty(STDIN_FILENO)) { + if (!editor_window_new_fd(vis, STDIN_FILENO)) + die("Can not read from stdin\n"); + int fd = open("/dev/tty", O_RDONLY); + if (fd == -1) + die("Can not reopen stdin\n"); + dup2(fd, STDIN_FILENO); + close(fd); + } else if (!editor_window_new(vis, NULL)) { + die("Can not create empty buffer\n"); + } + } mainloop(); editor_free(vis);