void vi_set_fileops(struct visor *vi, struct vi_fileops *fop);
void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty);
+void vi_window(struct visor *vi, int xsz, int ysz);
+void vi_redraw(struct visor *vi);
+
/* vi_new_buf creates a new buffer and inserts it in the buffer list. If the
* path pointer is null, the new buffer will be empty, otherwise it's as if it
* was followed by a vi_buf_read call to read a file into the buffer.
struct vi_alloc mm;
struct vi_ttyops tty;
void *tty_cls;
+
+ int term_width, term_height;
};
struct vi_buffer {
#define vi_write vi->fop.write
#define vi_seek vi->fop.seek
+#define vi_clear() vi->tty.clear(vi->tty_cls)
+#define vi_clear_line() vi->tty.clear_line(vi->tty_cls)
+#define vi_clear_line_at(y) vi->tty.clear_line_at(y, vi->tty_cls)
+#define vi_setcursor(x, y) vi->tty.setcursor(x, y, vi->tty_cls)
+#define vi_putchar(c) vi->tty.putchar(c, vi->tty_cls)
+#define vi_putchar_at(x, y, c) v->tty.putchar_at(x, y, c, vi->tty_cls)
+#define vi_scroll(n) vi->tty.scroll(n, vi->tty_cls)
+#define vi_del_back() vi->tty.del_back(vi->tty_cls)
+#define vi_del_fwd() vi->tty.del_fwd(vi->tty_cls)
+#define vi_status(s) vi->tty.status(s, vi->tty_cls)
+
+static int remove_buf(struct visor *vi, struct vi_buffer *vb);
+static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size);
+
#ifdef HAVE_LIBC
static const struct vi_alloc stdalloc = { malloc, free, realloc };
#endif
memset(vi, 0, sizeof *vi);
vi->mm = *mm;
+ vi->term_width = 80;
+ vi->term_height = 24;
+
return vi;
}
vi->tty = *tty;
}
+void vi_term_size(struct visor *vi, int xsz, int ysz)
+{
+ vi->term_width = xsz;
+ vi->term_height = ysz;
+}
+
+void vi_redraw(struct visor *vi)
+{
+ struct vi_buffer *vb;
+ struct vi_span *sp, *spend;
+ vi_addr spoffs;
+
+ vb = vi->buflist;
+ if(!(sp = vi_buf_find_span(vb, vi->view_start, &spoffs))) {
+ sp = vb->spans;
+ spoffs = 0;
+ }
+ spend = vb->spans + vb->num_spans;
+
+ vi_clear();
+
+ for(i=0; i<term_heig
+}
+
struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
{
struct vi_buffer *nb;
static int parse_args(int argc, char **argv);
static int init(void);
static void cleanup(void);
+static void resized(int x, int y);
/* file operations */
static vi_file *file_open(const char *path, unsigned int flags);
static void file_close(vi_file *file);
return 1;
}
+ vi_redraw(vi);
+
for(;;) {
int c = term_getchar();
if(!vi_new_buf(vi, fpaths[i])) {
return -1;
}
- printf("loaded file: %s\n", fpaths[i]);
}
return 0;
}
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <signal.h>
#include <errno.h>
+#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
static int term_width, term_height;
static int ttyfd = -1;
+static int selfpipe[2];
static struct termios saved_term;
+static void (*cb_resized)(int, int);
+
int term_init(const char *ttypath)
{
term_width = winsz.ws_col;
term_height = winsz.ws_row;
+ pipe(selfpipe);
+
signal(SIGWINCH, sighandler);
return 0;
}
ttyfd = -1;
}
+void term_getsize(int *width, int *height)
+{
+ *width = term_width;
+ *height = term_height;
+}
+
+void term_resize_func(void (*func)(int, int))
+{
+ cb_resized = func;
+}
+
+
+static char termbuf[1024];
+static int termbuf_len;
+
+void term_send(const char *s, int size)
+{
+ if(size >= sizeof termbuf) {
+ /* too large, just flush the buffer and write directly to the tty */
+ term_flush();
+ write(ttyfd, s, size);
+ } else {
+ if(size >= sizeof termbuf - termbuf_len) {
+ term_flush();
+ }
+ memcpy(termbuf + termbuf_len, s, size);
+ termbuf_len += size;
+ }
+}
+
+void term_puts(const char *s)
+{
+ term_send(s, strlen(s));
+}
+
+void term_printf(const char *fmt, ...)
+{
+ static char *buf;
+ static long bufsz;
+ va_list ap;
+ long len;
+
+ if(!buf) {
+ bufsz = 512;
+ if(!(buf = malloc(bufsz))) {
+ return;
+ }
+ }
+
+ for(;;) {
+ va_start(ap, fmt);
+ len = vsnprintf(buf, bufsz, fmt, ap);
+ va_end(ap);
+
+ if(len < bufsz) break;
+ if(len < 0) {
+ void *tmp;
+ long n = bufsz << 1;
+ if(!(tmp = realloc(buf, n))) {
+ break; /* if realloc fails, will result in truncated output */
+ }
+ }
+ }
+
+ term_send(buf, len);
+}
+
+void term_flush(void)
+{
+ if(termbuf_len > 0) {
+ write(ttyfd, termbuf, termbuf_len);
+ termbuf_len = 0;
+ }
+}
+
void term_clear(void)
{
- write(ttyfd, "\033[2J", 4);
+ term_puts("\033[2J");
}
int term_getchar(void)
#ifndef TERM_H_
#define TERM_H_
+enum term_event_type {
+ TERM_EV_KEY,
+ TERM_EV_RESIZE,
+ TERM_EV_MBUTTON,
+ TERM_EV_MMOTION
+};
+
+struct term_ev_any {
+ int type;
+};
+
+struct term_ev_resize {
+ int type;
+ int width, height;
+};
+
+struct term_ev_mbutton {
+ int type;
+ /* TODO */
+};
+
+struct term_ev_mmotion {
+ int type;
+ int x, y;
+};
+
+union term_event {
+ struct term_ev_any any;
+ struct term_ev_resize resize;
+ struct term_ev_mbutton mbutton;
+ struct term_ev_mmotion mmotion;
+};
+
int term_init(const char *ttypath);
void term_cleanup(void);
+void term_getsize(int *width, int *height);
+void term_resize_func(void (*func)(int, int));
+
+void term_send(const char *s, int size);
+void term_puts(const char *s);
+void term_printf(const char *fmt, ...);
+void term_flush(void);
+
void term_clear(void);
int term_getchar(void);