4c8d1f116328204cecf1ef3fd8ce7581c54bfb40
[visor] / libvisor / include / visor.h
1 /*
2 This file is part of the visor text editor and text editor framework
3 Copyright (C)  2019 John Tsiombikas <nuclear@member.fsf.org>
4
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.
9
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.
14
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/>.
17 */
18 #ifndef LIB_VISOR_TEXTED_CORE_H_
19 #define LIB_VISOR_TEXTED_CORE_H_
20
21 typedef long vi_addr;
22 typedef long vi_motion;
23
24 struct visor;
25 struct vi_buffer;
26
27 struct vi_span {
28         vi_addr beg, end;
29 };
30
31 enum vi_motdir {
32         VI_MOT_LEFT                     = 'h',
33         VI_MOT_DOWN                     = 'j',
34         VI_MOT_UP                       = 'k',
35         VI_MOT_RIGHT            = 'l',
36         VI_MOT_WORD_NEXT        = 'w',
37         VI_MOT_WORD_END         = 'e',
38         VI_MOT_WORD_BEG         = 'b',
39         VI_MOT_WORDP_NEXT       = 'W',
40         VI_MOT_WORDP_BEG        = 'B',
41         VI_MOT_LINE_BEG         = '^',
42         VI_MOT_LINE_END         = '$',
43         VI_MOT_SENT_NEXT        = ')',
44         VI_MOT_SENT_PREV        = '(',
45         VI_MOT_PAR_NEXT         = '}',
46         VI_MOT_PAR_PREV         = '{',
47         VI_MOT_SECT_NEXT        = ']',
48         VI_MOT_SECT_PREV        = '[',
49         VI_MOT_FIND_NEXT        = 'f',
50         VI_MOT_FIND_PREV        = 'F',
51         VI_MOT_FINDTO_NEXT      = 't',
52         VI_MOT_FINDTO_PREV      = 'T',
53         VI_MOT_GO                       = 'G',
54         VI_MOT_TOP                      = 'H',
55         VI_MOT_MID                      = 'M',
56         VI_MOT_BOT                      = 'B',
57         VI_MOT_INNER            = 'i',
58         VI_MOT_OUTER            = 'a'
59 };
60
61 #define VI_MOTION(d, n) (((long)(n) << 8) | ((long)(d)))
62
63 struct vi_alloc {
64         void *(*malloc)(unsigned long);
65         void (*free)(void*);
66         void *(*realloc)(void*, unsigned long); /* can be null, will use malloc/free */
67 };
68
69 struct vi_fileops {
70         void *(*open)(const char *path);
71         long (*size)(void *file);
72         void (*close)(void *file);
73         void *(*map)(void *file);
74         void (*unmap)(void *file);
75         long (*read)(void *file, void *buf, long count);
76         long (*write)(void *file, void *buf, long count);
77         long (*seek)(void *file, long offs, int whence);
78 };
79
80 struct vi_ttyops {
81         void (*clear)(void *cls);
82         void (*clear_line)(void *cls);
83         void (*clear_line_at)(int y, void *cls);
84         void (*setcursor)(int x, int y, void *cls);
85         void (*putchar)(char c, void *cls);
86         void (*putchar_at)(int x, int y, char c, void *cls);
87         void (*scroll)(int nlines, void *cls);
88         void (*del_back)(void *cls);
89         void (*del_fwd)(void *cls);
90         void (*status)(char *s, void *cls);
91 };
92
93 /* Create a new instance of the visor editor.
94  * The alloc argument can be used to provide custom memory allocation
95  * functions. It can be null in a hosted build, or if you set the HAVE_LIBC
96  * preprocessor macro, in which case the standard library allocator will be
97  * used.
98  */
99 struct visor *vi_create(struct vi_alloc *mm);
100 void vi_destroy(struct visor *vi);
101
102 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop);
103
104 /* vi_new_buf creates a new buffer and inserts it in the buffer list. If the
105  * path pointer is null, the new buffer will be empty, otherwise it's as if it
106  * was followed by a vi_buf_read call to read a file into the buffer.
107  */
108 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path);
109 int vi_delete_buf(struct visor *vi, struct vi_buffer *vb);
110 int vi_num_buf(struct visor *vi);
111
112 struct vi_buffer *vi_getcur_buf(struct visor *vi);
113 int vi_setcur_buf(struct visor *vi, struct vi_buffer *vb);
114
115 struct vi_buffer *vi_next_buf(struct visor *vi);
116 struct vi_buffer *vi_prev_buf(struct visor *vi);
117
118
119 /* Read a file into the buffer.
120  * Returns 0 on success, -1 on failure.
121  */
122 int vi_buf_read(struct vi_buffer *vb, const char *path);
123
124 /* Write the buffer out to a file. If the path is null, the buffer will be
125  * written out to the same file that was last read. If the path is null and
126  * no file was ever read in this buffer, the write fails.
127  * Returns 0 on success, -1 on failure.
128  */
129 int vi_buf_write(struct vi_buffer *vb, const char *path);
130 long vi_buf_size(struct vi_buffer *vb);
131
132 void vi_buf_ins_begin(struct vi_buffer *vb, vi_motion mot);
133 void vi_buf_insert(struct vi_buffer *vb, char *s);
134 void vi_buf_ins_end(struct vi_buffer *vb);
135
136 void vi_buf_del(struct vi_buffer *vb, vi_motion mot);
137 void vi_buf_yank(struct vi_buffer *vb, vi_motion mot);
138
139 #endif  /* LIB_VISOR_TEXTED_CORE_H_ */