aboutsummaryrefslogtreecommitdiff
path: root/vis.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-10-17 15:43:48 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-10-17 15:43:48 +0200
commite9180cf83ade29192d8407337f614e5a024017a9 (patch)
tree0aa207c91d5bdb999a260ba54de015916dfbea5b /vis.c
parent6be820495d70e6f10a92fbdaf6a1e4a278b02ab6 (diff)
downloadvis-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.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/vis.c b/vis.c
index 9054ccc..554b4f7 100644
--- a/vis.c
+++ b/vis.c
@@ -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;