remove unused function cmd()
[vis.git] / vis-core.h
blob15b4167f4243e04f2b2d740291cc1682dc733eaa
1 #ifndef VIS_CORE_H
2 #define VIS_CORE_H
4 #include <setjmp.h>
5 #include "vis.h"
6 #include "register.h"
7 #include "text.h"
8 #include "text-regex.h"
9 #include "map.h"
10 #include "ring-buffer.h"
11 #include "array.h"
13 /* a mode contains a set of key bindings which are currently valid.
15 * each mode can specify one parent mode which is consultated if a given key
16 * is not found in the current mode. hence the modes form a tree which is
17 * searched from the current mode up towards the root mode until a valid binding
18 * is found.
20 * if no binding is found, mode->input(...) is called and the user entered
21 * keys are passed as argument. this is used to change the document content.
23 typedef struct Mode Mode;
24 struct Mode {
25 enum VisMode id;
26 Mode *parent; /* if no match is found in this mode, search will continue there */
27 Map *bindings;
28 const char *name; /* descriptive, user facing name of the mode */
29 const char *status; /* name displayed in the window status bar */
30 const char *help; /* short description used by :help */
31 void (*enter)(Vis*, Mode *old); /* called right before the mode becomes active */
32 void (*leave)(Vis*, Mode *new); /* called right before the mode becomes inactive */
33 void (*input)(Vis*, const char*, size_t); /* called whenever a key is not found in this mode and all its parent modes */
34 void (*idle)(Vis*); /* called whenever a certain idle time i.e. without any user input elapsed */
35 time_t idle_timeout; /* idle time in seconds after which the registered function will be called */
36 bool visual; /* whether text selection is possible in this mode */
39 typedef struct {
40 int count; /* how many times should the command be executed? */
41 Register *reg; /* always non-NULL, set to a default register */
42 Filerange range; /* which part of the file should be affected by the operator */
43 size_t pos; /* at which byte from the start of the file should the operation start? */
44 size_t newpos; /* new position after motion or EPOS if none given */
45 bool linewise; /* should the changes always affect whole lines? */
46 const Arg *arg; /* arbitrary arguments */
47 } OperatorContext;
49 typedef struct {
50 /* operator logic, returns new cursor position, if EPOS is
51 * the cursor is disposed (except if it is the primary one) */
52 size_t (*func)(Vis*, Text*, OperatorContext*);
53 } Operator;
55 typedef struct { /* Motion implementation, takes a cursor postion and returns a new one */
56 /* TODO: merge types / use union to save space */
57 size_t (*cur)(Cursor*);
58 size_t (*txt)(Text*, size_t pos);
59 size_t (*file)(Vis*, File*, size_t pos);
60 size_t (*vis)(Vis*, Text*, size_t pos);
61 size_t (*view)(Vis*, View*);
62 size_t (*win)(Vis*, Win*, size_t pos);
63 size_t (*user)(Vis*, Win*, void*, size_t pos);
64 enum {
65 LINEWISE = VIS_MOTIONTYPE_LINEWISE, /* should the covered range be extended to whole lines? */
66 CHARWISE = VIS_MOTIONTYPE_CHARWISE, /* scrolls window content until position is visible */
67 INCLUSIVE = 1 << 2, /* should new position be included in operator range? */
68 IDEMPOTENT = 1 << 3, /* does the returned postion remain the same if called multiple times? */
69 JUMP = 1 << 4,
70 } type;
71 void *data;
72 } Movement;
74 typedef struct {
75 /* gets a cursor position and returns a file range (or text_range_empty())
76 * representing the text object containing the position. */
77 Filerange (*txt)(Text*, size_t pos);
78 Filerange (*vis)(Vis*, Text*, size_t pos);
79 Filerange (*user)(Vis*, Win*, void *data, size_t pos);
80 enum {
81 INNER = 1 << 0, /* whether the object should include */
82 OUTER = 1 << 1, /* the delimiting symbols or not */
83 SPLIT = 1 << 2, /* whether multiple applications will yield a split range */
84 } type;
85 void *data;
86 } TextObject;
88 /* a macro is just a sequence of symbolic keys as received from ui->getkey */
89 typedef Buffer Macro;
90 #define macro_release buffer_release
91 #define macro_reset buffer_truncate
92 #define macro_append buffer_append0
94 typedef struct { /** collects all information until an operator is executed */
95 int count;
96 enum VisMotionType type;
97 const Operator *op;
98 const Movement *movement;
99 const TextObject *textobj;
100 const Macro *macro;
101 Register *reg;
102 enum VisMark mark;
103 Arg arg;
104 } Action;
106 struct File { /* shared state among windows displaying the same file */
107 Text *text; /* data structure holding the file content */
108 const char *name; /* file name used when loading/saving */
109 volatile sig_atomic_t truncated; /* whether the underlying memory mapped region became invalid (SIGBUS) */
110 bool is_stdin; /* whether file content was read from stdin */
111 bool internal; /* whether it is an internal file (e.g. used for the prompt) */
112 struct stat stat; /* filesystem information when loaded/saved, used to detect changes outside the editor */
113 int refcount; /* how many windows are displaying this file? (always >= 1) */
114 Mark marks[VIS_MARK_INVALID]; /* marks which are shared across windows */
115 File *next, *prev;
118 typedef struct {
119 time_t state; /* state of the text, used to invalidate change list */
120 size_t index; /* #number of changes */
121 size_t pos; /* where the current change occured */
122 } ChangeList;
124 struct Win {
125 Vis *vis; /* editor instance to which this window belongs to */
126 UiWin *ui; /* ui object handling visual appearance of this window */
127 File *file; /* file being displayed in this window */
128 View *view; /* currently displayed part of underlying text */
129 RingBuffer *jumplist; /* LRU jump management */
130 ChangeList changelist; /* state for iterating through least recently changes */
131 Mode modes[VIS_MODE_INVALID]; /* overlay mods used for per window key bindings */
132 Win *parent; /* window which was active when showing the command prompt */
133 Mode *parent_mode; /* mode which was active when showing the command prompt */
134 Win *prev, *next; /* neighbouring windows */
137 struct Vis {
138 Ui *ui; /* user interface repsonsible for visual appearance */
139 File *files; /* all files currently managed by this editor instance */
140 File *command_file; /* special internal file used to store :-command prompt */
141 File *search_file; /* special internal file used to store /,? search prompt */
142 Win *windows; /* all windows currently managed by this editor instance */
143 Win *win; /* currently active/focused window */
144 Win *message_window; /* special window to display multi line messages */
145 Register registers[VIS_REG_INVALID]; /* registers used for yank and put */
146 Macro macros[VIS_MACRO_INVALID]; /* recorded macros */
147 Macro *recording, *last_recording; /* currently (if non NULL) and least recently recorded macro */
148 Macro *macro_operator; /* special macro used to repeat certain operators */
149 Mode *mode_before_prompt; /* user mode which was active before entering prompt */
150 Regex *search_pattern; /* last used search pattern */
151 char search_char[8]; /* last used character to search for via 'f', 'F', 't', 'T' */
152 int last_totill; /* last to/till movement used for ';' and ',' */
153 int tabwidth; /* how many spaces should be used to display a tab */
154 bool expandtab; /* whether typed tabs should be converted to spaces */
155 bool autoindent; /* whether indentation should be copied from previous line on newline */
156 Map *cmds; /* ":"-commands, used for unique prefix queries */
157 Map *options; /* ":set"-options */
158 Map *keymap; /* key translation before any bindings are matched */
159 Buffer input_queue; /* holds pending input keys */
160 Buffer *keys; /* currently active keys buffer (either the input_queue or a macro) */
161 Action action; /* current action which is in progress */
162 Action action_prev; /* last operator action used by the repeat (dot) command */
163 Mode *mode; /* currently active mode, used to search for keybindings */
164 Mode *mode_prev; /* previsouly active user mode */
165 volatile bool running; /* exit main loop once this becomes false */
166 int exit_status; /* exit status when terminating main loop */
167 volatile sig_atomic_t cancel_filter; /* abort external command/filter (SIGINT occured) */
168 volatile sig_atomic_t sigbus; /* one of the memory mapped region became unavailable (SIGBUS) */
169 sigjmp_buf sigbus_jmpbuf; /* used to jump back to a known good state in the mainloop after (SIGBUS) */
170 Map *actions; /* registered editor actions / special keys commands */
171 lua_State *lua; /* lua context used for syntax highligthing */
172 VisEvent *event;
173 Array motions;
174 Array textobjects;
177 /** stuff used by multiple of the vis-* files */
179 extern Mode vis_modes[VIS_MODE_INVALID];
180 extern const Movement vis_motions[VIS_MOVE_INVALID];
181 extern const Operator vis_operators[VIS_OP_INVALID];
182 extern const TextObject vis_textobjects[VIS_TEXTOBJECT_INVALID];
184 void macro_operator_stop(Vis *vis);
185 void macro_operator_record(Vis *vis);
187 void action_do(Vis *vis, Action *a);
188 void action_reset(Action*);
190 void mode_set(Vis *vis, Mode *new_mode);
192 void window_selection_save(Win *win);
193 Win *window_new_file(Vis *vis, File *file);
195 #endif