X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=visor;a=blobdiff_plain;f=libvisor%2Finclude%2Fvisor.h;h=a9b3ec78f9e3035249d0f892534a3848a3bd057d;hp=165e2b27ef0c8f024d575474811aaa65302e29c7;hb=a4536cf08230dba14f221b661abd7804fcc1ebbc;hpb=dc83bdff56d4dd7454559f5e918652f32f7e9d6d diff --git a/libvisor/include/visor.h b/libvisor/include/visor.h index 165e2b2..a9b3ec7 100644 --- a/libvisor/include/visor.h +++ b/libvisor/include/visor.h @@ -1,5 +1,5 @@ /* -This file is part of the visor text editor and text editor framework +visor - lightweight system-independent text editor and framework Copyright (C) 2019 John Tsiombikas This program is free software: you can redistribute it and/or modify @@ -20,12 +20,15 @@ along with this program. If not, see . typedef long vi_addr; typedef long vi_motion; +typedef void vi_file; struct visor; struct vi_buffer; struct vi_span { - vi_addr beg, end; + vi_addr start; + unsigned long size; + int src; }; enum vi_motdir { @@ -60,15 +63,27 @@ enum vi_motdir { #define VI_MOTION(d, n) (((long)(n) << 8) | ((long)(d))) +struct vi_alloc { + void *(*malloc)(unsigned long); + void (*free)(void*); + void *(*realloc)(void*, unsigned long); /* can be null, will use malloc/free */ +}; + +/* open flags (same as POSIX O_*) */ +enum { VI_RDONLY, VI_WRONLY, VI_RDWR, VI_CREAT = 0x100 }; +/* seek origin (same as C SEEK_*) */ +enum { VI_SEEK_SET, VI_SEEK_CUR, VI_SEEK_END }; + struct vi_fileops { - void *(*open)(const char *path); - long (*size)(void *file); - void (*close)(void *file); - void *(*map)(void *file); - void (*unmap)(void *file); - long (*read)(void *file, void *buf, long count); - long (*write)(void *file, void *buf, long count); + vi_file *(*open)(const char *path, unsigned int flags); + long (*size)(vi_file *file); + void (*close)(vi_file *file); + void *(*map)(vi_file *file); + void (*unmap)(vi_file *file); + long (*read)(vi_file *file, void *buf, long count); + long (*write)(vi_file *file, void *buf, long count); + long (*seek)(vi_file *file, long offs, int whence); }; struct vi_ttyops { @@ -84,11 +99,17 @@ struct vi_ttyops { void (*status)(char *s, void *cls); }; - -struct visor *vi_init(void); -void vi_cleanup(struct visor *vi); +/* Create a new instance of the visor editor. + * The alloc argument can be used to provide custom memory allocation + * functions. It can be null in a hosted build, or if you set the HAVE_LIBC + * preprocessor macro, in which case the standard library allocator will be + * used. + */ +struct visor *vi_create(struct vi_alloc *mm); +void vi_destroy(struct visor *vi); void vi_set_fileops(struct visor *vi, struct vi_fileops *fop); +void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty); /* 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 @@ -99,12 +120,15 @@ int vi_delete_buf(struct visor *vi, struct vi_buffer *vb); int vi_num_buf(struct visor *vi); struct vi_buffer *vi_getcur_buf(struct visor *vi); -int vi_setcur_buf(struct visor *vi, struct vi_buffer *vb); +void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb); struct vi_buffer *vi_next_buf(struct visor *vi); struct vi_buffer *vi_prev_buf(struct visor *vi); +/* reset a buffer to the newly created state, freeing all resources */ +void vi_buf_reset(struct vi_buffer *vb); + /* Read a file into the buffer. * Returns 0 on success, -1 on failure. */ @@ -118,6 +142,10 @@ int vi_buf_read(struct vi_buffer *vb, const char *path); int vi_buf_write(struct vi_buffer *vb, const char *path); long vi_buf_size(struct vi_buffer *vb); +/* find the span which corresponds to the specified text position */ +struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at); +const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *span); + void vi_buf_ins_begin(struct vi_buffer *vb, vi_motion mot); void vi_buf_insert(struct vi_buffer *vb, char *s); void vi_buf_ins_end(struct vi_buffer *vb);