vis: reject invalid register name when recording a macro
[vis.git] / vis-core.h
blobfc9853c8e9d164efaa29b3bfce13620f8aa599ee
1 #ifndef VIS_CORE_H
2 #define VIS_CORE_H
4 #include <setjmp.h>
5 #include "vis.h"
6 #include "sam.h"
7 #include "vis-lua.h"
8 #include "register.h"
9 #include "text.h"
10 #include "map.h"
11 #include "ring-buffer.h"
12 #include "array.h"
14 /* a mode contains a set of key bindings which are currently valid.
16 * each mode can specify one parent mode which is consultated if a given key
17 * is not found in the current mode. hence the modes form a tree which is
18 * searched from the current mode up towards the root mode until a valid binding
19 * is found.
21 * if no binding is found, mode->input(...) is called and the user entered
22 * keys are passed as argument. this is used to change the document content.
24 typedef struct Mode Mode;
25 struct Mode {
26 enum VisMode id;
27 Mode *parent; /* if no match is found in this mode, search will continue there */
28 Map *bindings;
29 const char *name; /* descriptive, user facing name of the mode */
30 const char *status; /* name displayed in the window status bar */
31 const char *help; /* short description used by :help */
32 void (*enter)(Vis*, Mode *old); /* called right before the mode becomes active */
33 void (*leave)(Vis*, Mode *new); /* called right before the mode becomes inactive */
34 void (*input)(Vis*, const char*, size_t); /* called whenever a key is not found in this mode and all its parent modes */
35 void (*idle)(Vis*); /* called whenever a certain idle time i.e. without any user input elapsed */
36 time_t idle_timeout; /* idle time in seconds after which the registered function will be called */
37 bool visual; /* whether text selection is possible in this mode */
40 struct OperatorContext {
41 int count; /* how many times should the command be executed? */
42 Register *reg; /* always non-NULL, set to a default register */
43 Filerange range; /* which part of the file should be affected by the operator */
44 size_t pos; /* at which byte from the start of the file should the operation start? */
45 size_t newpos; /* new position after motion or EPOS if none given */
46 bool linewise; /* should the changes always affect whole lines? */
47 const Arg *arg; /* arbitrary arguments */
48 void *context; /* used by user-registered operators */
51 typedef struct {
52 /* operator logic, returns new cursor position, if EPOS is
53 * the cursor is disposed (except if it is the primary one) */
54 VisOperatorFunction *func;
55 void *context;
56 } Operator;
58 typedef struct { /* Motion implementation, takes a cursor postion and returns a new one */
59 /* TODO: merge types / use union to save space */
60 size_t (*cur)(Cursor*);
61 size_t (*txt)(Text*, size_t pos);
62 size_t (*file)(Vis*, File*, size_t pos);
63 size_t (*vis)(Vis*, Text*, size_t pos);
64 size_t (*view)(Vis*, View*);
65 size_t (*win)(Vis*, Win*, size_t pos);
66 size_t (*user)(Vis*, Win*, void*, size_t pos);
67 enum {
68 LINEWISE = VIS_MOTIONTYPE_LINEWISE, /* should the covered range be extended to whole lines? */
69 CHARWISE = VIS_MOTIONTYPE_CHARWISE, /* scrolls window content until position is visible */
70 INCLUSIVE = 1 << 2, /* should new position be included in operator range? */
71 LINEWISE_INCLUSIVE = 1 << 3, /* inclusive, but only if motion is linewise? */
72 IDEMPOTENT = 1 << 4, /* does the returned postion remain the same if called multiple times? */
73 JUMP = 1 << 5, /* should the resulting position of the motion be recorded in the jump list? */
74 COUNT_EXACT = 1 << 6, /* fail (keep initial position) if count can not be satisfied exactly */
75 } type;
76 void *data;
77 } Movement;
79 typedef struct {
80 /* gets a cursor position and returns a file range (or text_range_empty())
81 * representing the text object containing the position. */
82 Filerange (*txt)(Text*, size_t pos);
83 Filerange (*vis)(Vis*, Text*, size_t pos);
84 Filerange (*user)(Vis*, Win*, void *data, size_t pos);
85 enum {
86 TEXTOBJECT_DELIMITED_INNER = 1 << 0, /* single byte delimited, inner variant */
87 TEXTOBJECT_DELIMITED_OUTER = 1 << 1, /* single byte delimited, outer variant */
88 TEXTOBJECT_NON_CONTIGUOUS = 1 << 2, /* multiple applications yield a split range */
89 TEXTOBJECT_EXTEND_FORWARD = 1 << 3, /* multiple applications extend towards the end of file (default) */
90 TEXTOBJECT_EXTEND_BACKWARD = 1 << 4, /* multiple applications extend towards the begin of file */
91 } type;
92 void *data;
93 } TextObject;
95 /* a macro is just a sequence of symbolic keys as received from ui->getkey */
96 typedef Buffer Macro;
97 #define macro_init buffer_init
98 #define macro_release buffer_release
99 #define macro_reset buffer_clear
100 #define macro_append buffer_append0
102 typedef struct { /** collects all information until an operator is executed */
103 int count;
104 enum VisMode mode;
105 enum VisMotionType type;
106 const Operator *op;
107 const Movement *movement;
108 const TextObject *textobj;
109 const Macro *macro;
110 Register *reg;
111 enum VisMark mark;
112 Arg arg;
113 } Action;
115 typedef struct Change Change;
116 typedef struct {
117 Change *changes; /* all changes in monotonically increasing file position */
118 Change *latest; /* most recent change */
119 enum SamError error; /* non-zero in case something went wrong */
120 } Transcript;
122 struct File { /* shared state among windows displaying the same file */
123 Text *text; /* data structure holding the file content */
124 const char *name; /* file name used when loading/saving */
125 volatile sig_atomic_t truncated; /* whether the underlying memory mapped region became invalid (SIGBUS) */
126 int fd; /* output file descriptor associated with this file or -1 if loaded by file name */
127 bool internal; /* whether it is an internal file (e.g. used for the prompt) */
128 struct stat stat; /* filesystem information when loaded/saved, used to detect changes outside the editor */
129 int refcount; /* how many windows are displaying this file? (always >= 1) */
130 Mark marks[VIS_MARK_INVALID]; /* marks which are shared across windows */
131 enum TextSaveMethod save_method; /* whether the file is saved using rename(2) or overwritten */
132 Transcript transcript; /* keeps track of changes performed by sam commands */
133 File *next, *prev;
136 typedef struct {
137 time_t state; /* state of the text, used to invalidate change list */
138 size_t index; /* #number of changes */
139 size_t pos; /* where the current change occured */
140 } ChangeList;
142 struct Win {
143 Vis *vis; /* editor instance to which this window belongs to */
144 UiWin *ui; /* ui object handling visual appearance of this window */
145 File *file; /* file being displayed in this window */
146 View *view; /* currently displayed part of underlying text */
147 RingBuffer *jumplist; /* LRU jump management */
148 ChangeList changelist; /* state for iterating through least recently changes */
149 Mode modes[VIS_MODE_INVALID]; /* overlay mods used for per window key bindings */
150 Win *parent; /* window which was active when showing the command prompt */
151 Mode *parent_mode; /* mode which was active when showing the command prompt */
152 Win *prev, *next; /* neighbouring windows */
155 struct Vis {
156 Ui *ui; /* user interface repsonsible for visual appearance */
157 File *files; /* all files currently managed by this editor instance */
158 File *command_file; /* special internal file used to store :-command prompt */
159 File *search_file; /* special internal file used to store /,? search prompt */
160 File *error_file; /* special internal file used to store lua error messages */
161 Win *windows; /* all windows currently managed by this editor instance */
162 Win *win; /* currently active/focused window */
163 Win *message_window; /* special window to display multi line messages */
164 Register registers[VIS_REG_INVALID]; /* registers used for text manipulations yank/put etc. and macros */
165 Macro *recording, *last_recording; /* currently (if non NULL) and least recently recorded macro */
166 const Macro *replaying; /* macro currently being replayed */
167 Macro *macro_operator; /* special macro used to repeat certain operators */
168 Mode *mode_before_prompt; /* user mode which was active before entering prompt */
169 char search_char[8]; /* last used character to search for via 'f', 'F', 't', 'T' */
170 int last_totill; /* last to/till movement used for ';' and ',' */
171 int search_direction; /* used for `n` and `N` */
172 int tabwidth; /* how many spaces should be used to display a tab */
173 bool expandtab; /* whether typed tabs should be converted to spaces */
174 bool autoindent; /* whether indentation should be copied from previous line on newline */
175 bool change_colors; /* whether to adjust 256 color palette for true colors */
176 char *shell; /* shell used to launch external commands */
177 Map *cmds; /* ":"-commands, used for unique prefix queries */
178 Map *usercmds; /* user registered ":"-commands */
179 Map *options; /* ":set"-options */
180 Map *keymap; /* key translation before any bindings are matched */
181 bool keymap_disabled; /* ignore key map for next key press, gets automatically re-enabled */
182 char key[VIS_KEY_LENGTH_MAX]; /* last pressed key as reported from the UI */
183 char key_current[VIS_KEY_LENGTH_MAX];/* current key being processed by the input queue */
184 char key_prev[VIS_KEY_LENGTH_MAX]; /* previous key which was processed by the input queue */
185 Buffer input_queue; /* holds pending input keys */
186 bool errorhandler; /* whether we are currently in an error handler, used to avoid recursion */
187 Action action; /* current action which is in progress */
188 Action action_prev; /* last operator action used by the repeat (dot) command */
189 Mode *mode; /* currently active mode, used to search for keybindings */
190 Mode *mode_prev; /* previsouly active user mode */
191 bool initialized; /* whether UI and Lua integration has been initialized */
192 int nesting_level; /* parsing state to hold keep track of { } nesting level */
193 volatile bool running; /* exit main loop once this becomes false */
194 int exit_status; /* exit status when terminating main loop */
195 volatile sig_atomic_t cancel_filter; /* abort external command/filter (SIGINT occured) */
196 volatile sig_atomic_t sigbus; /* one of the memory mapped region became unavailable (SIGBUS) */
197 volatile sig_atomic_t need_resize; /* need to resize UI (SIGWINCH occured) */
198 volatile sig_atomic_t resume; /* need to resume UI (SIGCONT occured) */
199 volatile sig_atomic_t terminate; /* need to terminate we were being killed by SIGTERM */
200 sigjmp_buf sigbus_jmpbuf; /* used to jump back to a known good state in the mainloop after (SIGBUS) */
201 Map *actions; /* registered editor actions / special keys commands */
202 Array actions_user; /* dynamically allocated editor actions */
203 lua_State *lua; /* lua context used for syntax highligthing */
204 VisEvent *event;
205 Array operators;
206 Array motions;
207 Array textobjects;
208 Array bindings;
211 enum VisEvents {
212 VIS_EVENT_INIT,
213 VIS_EVENT_START,
214 VIS_EVENT_QUIT,
215 VIS_EVENT_FILE_OPEN,
216 VIS_EVENT_FILE_SAVE_PRE,
217 VIS_EVENT_FILE_SAVE_POST,
218 VIS_EVENT_FILE_CLOSE,
219 VIS_EVENT_WIN_OPEN,
220 VIS_EVENT_WIN_CLOSE,
221 VIS_EVENT_WIN_HIGHLIGHT,
222 VIS_EVENT_WIN_STATUS,
225 bool vis_event_emit(Vis*, enum VisEvents, ...);
227 typedef struct {
228 char name;
229 VIS_HELP_DECL(const char *help;)
230 } MarkDef;
232 typedef MarkDef RegisterDef;
234 /** stuff used by multiple of the vis-* files */
236 extern Mode vis_modes[VIS_MODE_INVALID];
237 extern const Movement vis_motions[VIS_MOVE_INVALID];
238 extern const Operator vis_operators[VIS_OP_INVALID];
239 extern const TextObject vis_textobjects[VIS_TEXTOBJECT_INVALID];
240 extern const MarkDef vis_marks[VIS_MARK_a];
241 extern const RegisterDef vis_registers[VIS_REG_a];
243 void macro_operator_stop(Vis *vis);
244 void macro_operator_record(Vis *vis);
246 void vis_do(Vis *vis);
247 void action_reset(Action*);
248 size_t vis_text_insert_nl(Vis*, Text*, size_t pos);
250 void mode_set(Vis *vis, Mode *new_mode);
252 void window_selection_save(Win *win);
253 Win *window_new_file(Vis*, File*, enum UiOption);
255 const char *file_name_get(File*);
256 void file_name_set(File*, const char *name);
258 #endif