2 visor - lightweight system-independent embeddable text editor framework
3 Copyright (C) 2019 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #define vi_malloc vi->mm.malloc
24 #define vi_free vi->mm.free
25 #define vi_realloc vi->mm.realloc
27 #define vi_open vi->fop.open
28 #define vi_size vi->fop.size
29 #define vi_close vi->fop.close
30 #define vi_map vi->fop.map
31 #define vi_unmap vi->fop.unmap
32 #define vi_read vi->fop.read
33 #define vi_write vi->fop.write
34 #define vi_seek vi->fop.seek
36 #define vi_clear() vi->tty.clear(vi->tty_cls)
37 #define vi_clear_line() vi->tty.clear_line(vi->tty_cls)
38 #define vi_clear_line_at(y) vi->tty.clear_line_at(y, vi->tty_cls)
39 #define vi_setcursor(x, y) vi->tty.setcursor(x, y, vi->tty_cls)
40 #define vi_putchar(c) vi->tty.putchar(c, vi->tty_cls)
41 #define vi_putchar_at(x, y, c) v->tty.putchar_at(x, y, c, vi->tty_cls)
42 #define vi_scroll(n) vi->tty.scroll(n, vi->tty_cls)
43 #define vi_del_back() vi->tty.del_back(vi->tty_cls)
44 #define vi_del_fwd() vi->tty.del_fwd(vi->tty_cls)
45 #define vi_status(s) vi->tty.status(s, vi->tty_cls)
46 #define vi_flush() vi->tty.flush(vi->tty_cls)
48 static int remove_buf(struct visor *vi, struct vi_buffer *vb);
49 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size);
52 static const struct vi_alloc stdalloc = { malloc, free, realloc };
55 struct visor *vi_create(struct vi_alloc *mm)
60 if(!mm) mm = &stdalloc;
65 if(!(vi = mm->malloc(sizeof *vi))) {
68 memset(vi, 0, sizeof *vi);
77 void vi_destroy(struct visor *vi)
80 vi_delete_buf(vi, vi->buflist);
85 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop)
90 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty)
95 void vi_term_size(struct visor *vi, int xsz, int ysz)
98 vi->term_height = ysz;
101 void vi_redraw(struct visor *vi)
103 int i = 0, col, cur_x = 0, cur_y = 0;
105 struct vi_buffer *vb;
106 struct vi_span *sp, *spans_end;
107 const char *tptr, *tend;
108 vi_addr spoffs, addr;
110 if(!vi->buflist) goto end;
113 if(!(sp = vi_buf_find_span(vb, vb->view_start, &spoffs))) {
117 spans_end = vb->spans + vb->num_spans;
119 tptr = vi_buf_span_text(vb, sp);
120 tend = tptr + sp->size;
125 addr = vb->view_start;
126 for(i=0; i<vi->term_height; i++) {
128 col = -vb->view_xscroll - 1;
129 while(++col < vi->term_width && (c = (addr++, *tptr++)) != '\n') {
130 if(addr == vb->cursor) {
139 if(++sp >= spans_end) {
142 tptr = vi_buf_span_text(vb, sp);
143 tend = tptr + sp->size;
149 while(i < vi->term_height) {
150 vi_setcursor(0, i++);
154 vi_setcursor(cur_x, cur_y);
158 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
160 struct vi_buffer *nb;
162 if(!(nb = vi_malloc(sizeof *nb))) {
163 vi_error(vi, "failed to allocate new buffer\n");
166 memset(nb, 0, sizeof *nb);
170 if(vi_buf_read(nb, path) == -1) {
177 struct vi_buffer *last = vi->buflist->prev;
179 nb->next = vi->buflist;
181 vi->buflist->prev = nb;
183 nb->next = nb->prev = nb;
189 static int remove_buf(struct visor *vi, struct vi_buffer *vb)
192 vi_error(vi, "failed to remove a buffer which doesn't exist\n");
197 if(vi->buflist != vb) {
198 vi_error(vi, "failed to remove buffer, buffer list inconsistency\n");
205 if(vi->buflist == vb) {
206 vi->buflist = vb->next;
208 vb->prev->next = vb->next;
209 vb->next->prev = vb->prev;
210 vb->next = vb->prev = vb;
214 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb)
216 if(remove_buf(vi, vb) == -1) {
221 if(vb->file_mapped) {
235 int vi_num_buf(struct visor *vi)
238 struct vi_buffer *vb;
240 if(!vi->buflist) return 0;
243 vb = vi->buflist->next;
244 while(vb != vi->buflist) {
251 struct vi_buffer *vi_getcur_buf(struct visor *vi)
256 void vi_setcur_buf(struct visor *vi, struct vi_buffer *vb)
261 struct vi_buffer *vi_next_buf(struct visor *vi)
263 return vi->buflist ? vi->buflist->next : 0;
266 struct vi_buffer *vi_prev_buf(struct visor *vi)
268 return vi->buflist ? vi->buflist->prev : 0;
271 /* split_span splits the span sp. if size > 0 it moves the second part to sp+2,
272 * leaving an empty place at sp+1 for the new span. The start point of the
273 * second part is adjusted by size.
275 * It can't fail, because it's always called with the span array having at
276 * least two empty slots (see: add_span).
278 void split_span(struct vi_buffer *vb, struct vi_span *sp, vi_addr spoffs, unsigned long size)
280 int newseg = size > 0 ? 1 : 0;
281 struct vi_span *tail = sp + newseg + 1;
282 int num_move = vb->spans + vb->num_spans - sp - 1;
284 memmove(tail + 1, sp + 1, num_move * sizeof *sp);
285 vb->num_spans += tail - sp;
289 tail->start += spoffs;
290 tail->size -= spoffs;
294 if(size <= tail->size) {
304 /* we produced one or more zero-sized spans, drop them */
305 num_move = vb->num_spans - (tail - sp);
306 memmove(sp, tail, num_move * sizeof *sp);
307 vb->num_spans -= num_move;
311 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size)
313 struct visor *vi = vb->vi;
317 /* make sure we have space for at least two new spans (split + add) */
318 if(vb->num_spans + 1 >= vb->max_spans) {
319 int newmax = vb->max_spans > 0 ? (vb->max_spans << 1) : 16;
320 struct vi_span *tmp = vi_realloc(vb->spans, newmax * sizeof *tmp);
323 vb->max_spans = newmax;
326 if((sp = vi_buf_find_span(vb, at, &spoffs))) {
328 split_span(vb, sp++, spoffs, 1);
330 split_span(vb, sp++, 0, 0);
333 sp = vb->spans + vb->num_spans;
343 void vi_buf_reset(struct vi_buffer *vb)
345 struct visor *vi = vb->vi;
346 struct vi_buffer *prev, *next;
351 if(vb->file_mapped) vi_unmap(vb->fp);
360 memset(vb, 0, sizeof *vb);
366 int vi_buf_read(struct vi_buffer *vb, const char *path)
368 struct visor *vi = vb->vi;
375 if(!(fp = vi_open(path, VI_RDONLY | VI_CREAT))) {
379 if(!(vb->path = vi_malloc(plen + 1))) {
380 vi_error(vi, "failed to allocate path name buffer\n");
384 memcpy(vb->path, path, plen + 1);
388 if((fsz = vi_size(fp))) {
389 /* existing file, map it into memory, or failing that read it */
390 if(!vi->fop.map || !(vb->orig = vi_map(fp))) {
391 if(!(vb->orig = vi_malloc(fsz))) {
399 if(add_span(vb, 0, SPAN_ORIG, 0, fsz) == -1) {
400 vi_error(vi, "failed to allocate span\n");
409 int vi_buf_write(struct vi_buffer *vb, const char *path)
412 struct visor *vi = vb->vi;
414 static char wbuf[512];
416 if(!path) path = vb->path;
418 vi_error(vi, "failed to write buffer, unknown path\n");
422 if(!(fp = vi_open(path, VI_WRONLY | VI_CREAT))) {
423 vi_error(vi, "failed to open %s for writing\n", path);
428 for(i=0; i<vb->num_spans; i++) {
429 struct vi_span *sp = vb->spans + i;
430 const char *sptxt = vi_buf_span_text(vb, sp);
432 while(count < sp->size) {
433 n = sp->size - count;
434 if(n > sizeof wbuf - wbuf_count) {
435 n = sizeof wbuf - wbuf_count;
437 memcpy(wbuf + wbuf_count, sptxt + count, n);
442 if(wbuf_count >= sizeof wbuf) {
443 vi_write(fp, wbuf, wbuf_count);
448 vi_write(fp, wbuf, wbuf_count);
454 long vi_buf_size(struct vi_buffer *vb)
459 for(i=0; i<vb->num_spans; i++) {
460 sz += vb->spans[i].size;
465 struct vi_span *vi_buf_find_span(struct vi_buffer *vb, vi_addr at, vi_addr *soffs)
468 long sz = 0, prev_sz;
470 for(i=0; i<vb->num_spans; i++) {
472 sz += vb->spans[i].size;
474 if(soffs) *soffs = at - prev_sz;
475 return vb->spans + i;
481 const char *vi_buf_span_text(struct vi_buffer *vb, struct vi_span *sp)
483 const char *buf = sp->src == SPAN_ORIG ? vb->orig : vb->add;
484 return buf + sp->start;