aboutsummaryrefslogtreecommitdiff
path: root/text.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 /text.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 'text.c')
-rw-r--r--text.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/text.c b/text.c
index 7757e70..235c98c 100644
--- a/text.c
+++ b/text.c
@@ -645,12 +645,36 @@ err:
return -1;
}
+ssize_t text_write(Text *txt, int fd) {
+ ssize_t len = 0;
+ text_iterate(txt, it, 0) {
+ size_t plen = it.end - it.start, poff = 0;
+ while (plen > 0) {
+ ssize_t res = write(fd, it.start + poff, plen);
+ if (res < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+ return -1;
+ }
+ if (res == 0)
+ break;
+ poff += res;
+ plen -= res;
+ }
+ len += plen;
+ }
+ txt->saved_action = txt->undo;
+ text_snapshot(txt);
+ return len;
+}
+
/* load the given file as starting point for further editing operations.
* to start with an empty document, pass NULL as filename. */
Text *text_load(const char *filename) {
Text *txt = calloc(1, sizeof(Text));
if (!txt)
return NULL;
+ txt->fd = -1;
txt->begin.index = 1;
txt->end.index = 2;
txt->piece_count = 2;
@@ -686,8 +710,10 @@ Text *text_load(const char *filename) {
}
return txt;
out:
- if (txt->fd > 2)
+ if (txt->fd > 2) {
close(txt->fd);
+ txt->fd = -1;
+ }
text_free(txt);
return NULL;
}
@@ -1124,6 +1150,10 @@ void text_mark_clear_all(Text *txt) {
text_mark_clear(txt, mark);
}
+int text_fd_get(Text *txt) {
+ return txt->fd;
+}
+
const char *text_filename_get(Text *txt) {
return txt->filename;
}