diff options
| -rw-r--r-- | editor.c | 11 | ||||
| -rw-r--r-- | editor.h | 1 | ||||
| -rw-r--r-- | text.c | 12 | ||||
| -rw-r--r-- | text.h | 1 | ||||
| -rw-r--r-- | vis.c | 23 |
5 files changed, 44 insertions, 4 deletions
@@ -365,6 +365,17 @@ bool editor_window_new(Editor *ed, const char *filename) { return true; } +bool editor_window_new_fd(Editor *ed, int fd) { + Text *txt = text_load_fd(fd); + if (!txt) + return false; + EditorWin *win = editor_window_new_text(ed, txt); + if (!win) + return false; + editor_draw(ed); + return true; +} + static void editor_window_detach(Editor *ed, EditorWin *win) { if (win->prev) win->prev->next = win->next; @@ -137,6 +137,7 @@ void editor_syntax_unload(Editor*); * in another window, share the underlying text that is changes will be * visible in both windows */ bool editor_window_new(Editor*, const char *filename); +bool editor_window_new_fd(Editor*, int fd); /* reload the file currently displayed in the window from disk */ bool editor_window_reload(EditorWin*); void editor_window_close(EditorWin*); @@ -692,6 +692,18 @@ out: return NULL; } +Text *text_load_fd(int fd) { + Text *txt = text_load(NULL); + if (!txt) + return NULL; + char buf[1024]; + for (ssize_t len = 0; (len = read(fd, buf, sizeof buf)) > 0;) + text_insert(txt, text_size(txt), buf, len); + text_snapshot(txt); + txt->fd = fd; + return txt; +} + static void print_piece(Piece *p) { fprintf(stderr, "index: %d\tnext: %d\tprev: %d\t len: %d\t data: %p\n", p->index, p->next ? p->next->index : -1, @@ -33,6 +33,7 @@ typedef struct { text_iterator_next(&it)) Text *text_load(const char *file); +Text *text_load_fd(int fd); /* the filename from which this text was loaded or first saved to */ const char *text_filename_get(Text*); /* associate a filename with the yet unnamed buffer */ @@ -1671,12 +1671,27 @@ int main(int argc, char *argv[]) { die("Could not load syntax highlighting definitions\n"); editor_statusbar_set(vis, config->statusbar); - for (int i = 1; i < MAX(argc, 2); i++) { - const char *file = i < argc ? argv[i] : NULL; - if (!editor_window_new(vis, file)) - die("Could not load `%s': %s\n", file, strerror(errno)); + 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); + break; + } + } else if (!editor_window_new(vis, argv[i])) { + die("Can not load `%s': %s\n", argv[i], strerror(errno)); + } } + if (!vis->windows && !editor_window_new(vis, NULL)) + die("Can not create empty buffer\n"); + mainloop(); editor_free(vis); endwin(); |
