-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.h
348 lines (324 loc) · 10.6 KB
/
command.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (C) 2014 Richard Burke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef WED_COMMAND_H
#define WED_COMMAND_H
#include <stdio.h>
#include "shared.h"
#include "status.h"
#include "value.h"
#include "radix_tree.h"
#include "help.h"
struct Session;
#define MAX_CMD_ARG_NUM 5
/* Commands are in effect just functions with the same signature
* that perform various actions in wed. This allows them to be mapped to
* key presses and called in a generic way. Commands interact with lower level
* entities like buffers and allow their functionality to be exposed.
* Below is a list of all the Commands that are currently available in wed */
typedef enum {
CMD_NOP,
CMD_BP_CHANGE_LINE,
CMD_BP_CHANGE_CHAR,
CMD_BP_TO_LINE_START,
CMD_BP_TO_HARD_LINE_START,
CMD_BP_TO_LINE_END,
CMD_BP_TO_HARD_LINE_END,
CMD_BP_TO_NEXT_WORD,
CMD_BP_TO_PREV_WORD,
CMD_BP_TO_NEXT_PARAGRAPH,
CMD_BP_TO_PREV_PARAGRAPH,
CMD_BP_CHANGE_PAGE,
CMD_BP_TO_BUFFER_START,
CMD_BP_TO_BUFFER_END,
CMD_BP_GOTO_MATCHING_BRACKET,
CMD_BUFFER_MOUSE_CLICK,
CMD_BUFFER_INSERT_CHAR,
CMD_BUFFER_INDENT,
CMD_BUFFER_DELETE_CHAR,
CMD_BUFFER_BACKSPACE,
CMD_BUFFER_DELETE_WORD,
CMD_BUFFER_DELETE_PREV_WORD,
CMD_BUFFER_INSERT_LINE,
CMD_BUFFER_SELECT_ALL_TEXT,
CMD_BUFFER_COPY_SELECTED_TEXT,
CMD_BUFFER_CUT_SELECTED_TEXT,
CMD_BUFFER_PASTE_TEXT,
CMD_BUFFER_UNDO,
CMD_BUFFER_REDO,
CMD_BUFFER_VERT_MOVE_LINES,
CMD_BUFFER_DUPLICATE_SELECTION,
CMD_BUFFER_JOIN_LINES,
CMD_BUFFER_SAVE_FILE,
CMD_BUFFER_SAVE_AS,
CMD_BUFFER_FIND,
CMD_BUFFER_TOGGLE_SEARCH_TYPE,
CMD_BUFFER_TOGGLE_SEARCH_CASE,
CMD_BUFFER_TOGGLE_SEARCH_DIRECTION,
CMD_BUFFER_REPLACE,
CMD_PREVIOUS_PROMPT_ENTRY,
CMD_NEXT_PROMPT_ENTRY,
CMD_PROMPT_INPUT_FINISHED,
CMD_CANCEL_PROMPT,
CMD_RUN_PROMPT_COMPLETION,
CMD_BUFFER_GOTO_LINE,
CMD_SESSION_OPEN_FILE,
CMD_SESSION_ADD_EMPTY_BUFFER,
CMD_SESSION_CHANGE_TAB,
CMD_SESSION_TAB_MOUSE_CLICK,
CMD_SESSION_SAVE_ALL,
CMD_SESSION_CLOSE_BUFFER,
CMD_SESSION_RUN_COMMAND,
CMD_SESSION_CHANGE_BUFFER,
CMD_SESSION_FILE_EXPLORER_TOGGLE_ACTIVE,
CMD_SESSION_FILE_EXPLORER_SELECT,
CMD_SESSION_FILE_EXPLORER_CLICK,
CMD_SUSPEND,
CMD_SESSION_END,
CMD_SESSION_ECHO,
CMD_SESSION_MAP,
CMD_SESSION_UNMAP,
CMD_SESSION_HELP,
CMD_BUFFER_FILTER,
CMD_BUFFER_READ,
CMD_BUFFER_WRITE,
CMD_SESSION_EXEC
} Command;
/* Operations are instances of commands i.e. they define a command with
* arguments */
typedef enum {
OP_NOP,
OP_MOVE_PREV_LINE,
OP_MOVE_NEXT_LINE,
OP_MOVE_NEXT_CHAR,
OP_MOVE_PREV_CHAR,
OP_MOVE_START_OF_SCREEN_LINE,
OP_MOVE_START_OF_LINE,
OP_MOVE_END_OF_SCREEN_LINE,
OP_MOVE_END_OF_LINE,
OP_MOVE_NEXT_WORD,
OP_MOVE_PREV_WORD,
OP_MOVE_NEXT_PARAGRAPH,
OP_MOVE_PREV_PARAGRAPH,
OP_MOVE_PREV_PAGE,
OP_MOVE_NEXT_PAGE,
OP_MOVE_BUFFER_START,
OP_MOVE_BUFFER_END,
OP_MOVE_SELECT_PREV_LINE,
OP_MOVE_SELECT_NEXT_LINE,
OP_MOVE_SELECT_NEXT_CHAR,
OP_MOVE_SELECT_PREV_CHAR,
OP_MOVE_SELECT_START_OF_SCREEN_LINE,
OP_MOVE_SELECT_START_OF_LINE,
OP_MOVE_SELECT_END_OF_SCREEN_LINE,
OP_MOVE_SELECT_END_OF_LINE,
OP_MOVE_SELECT_NEXT_WORD,
OP_MOVE_SELECT_PREV_WORD,
OP_MOVE_SELECT_NEXT_PARAGRAPH,
OP_MOVE_SELECT_PREV_PARAGRAPH,
OP_MOVE_SELECT_PREV_PAGE,
OP_MOVE_SELECT_NEXT_PAGE,
OP_MOVE_SELECT_BUFFER_START,
OP_MOVE_SELECT_BUFFER_END,
OP_MOVE_MATCHING_BRACKET,
OP_MOVE_TO_CLICK_POSITION,
OP_INDENT,
OP_UNINDENT,
OP_DELETE,
OP_BACKSPACE,
OP_DELETE_NEXT_WORD,
OP_DELETE_PREV_WORD,
OP_INSERT_NEWLINE,
OP_INSERT_SPACE,
OP_INSERT_KPDIV,
OP_INSERT_KPMULT,
OP_INSERT_KPMINUS,
OP_INSERT_KPPLUS,
OP_SELECT_ALL,
OP_COPY,
OP_CUT,
OP_PASTE,
OP_UNDO,
OP_REDO,
OP_MOVE_LINES_UP,
OP_MOVE_LINES_DOWN,
OP_DUPLICATE,
OP_JOIN_LINES,
OP_SAVE,
OP_SAVE_AS,
OP_FIND,
OP_FIND_REPLACE,
OP_GOTO_LINE,
OP_OPEN,
OP_NEW,
OP_NEXT_BUFFER,
OP_PREV_BUFFER,
OP_SET_CLICKED_TAB_ACTIVE,
OP_SAVE_ALL,
OP_CLOSE,
OP_CMD,
OP_CHANGE_BUFFER,
OP_SWITCH_TO_FILE_EXPLORER,
OP_SUSPEND,
OP_EXIT,
OP_TOGGLE_SEARCH_TYPE,
OP_TOGGLE_SEARCH_CASE_SENSITIVITY,
OP_TOGGLE_SEARCH_DIRECTION,
OP_PROMPT_PREV_ENTRY,
OP_PROMPT_NEXT_ENTRY,
OP_PROMPT_SUBMIT,
OP_PROMPT_CANCEL,
OP_PROMPT_COMPLETE,
OP_PROMPT_COMPLETE_PREV,
OP_FILE_EXPLORER_NEXT_ENTRY,
OP_FILE_EXPLORER_PREV_ENTRY,
OP_FILE_EXPLORER_NEXT_PAGE,
OP_FILE_EXPLORER_PREV_PAGE,
OP_FILE_EXPLORER_TOP,
OP_FILE_EXPLORER_BOTTOM,
OP_FILE_EXPLORER_FIND,
OP_FILE_EXPLORER_SELECT,
OP_FILE_EXPLORER_QUIT,
OP_FILE_EXPLORER_EXIT_WED,
OP_FILE_EXPLORER_CLICK_SELECT
} Operation;
/* Container structure for Command arguments */
typedef struct {
struct Session *sess; /* Commands can change global state */
Value args[MAX_CMD_ARG_NUM]; /* Array of int|string|bool values */
size_t arg_num; /* Number of values in args actually set */
const char *key; /* The key press that invoked this Command */
int *finished; /* Set equal to true will stop processing input */
} CommandArgs;
typedef Status (*CommandHandler)(const CommandArgs *);
/* Categorisation of commands */
typedef enum {
CMDT_NOP = 0,
CMDT_BUFFER_MOVE = 1 << 0,
CMDT_BUFFER_MOD = 1 << 1,
CMDT_CMD_INPUT = 1 << 2,
CMDT_EXIT = 1 << 3,
CMDT_SESS_MOD = 1 << 4,
CMDT_CMD_MOD = 1 << 5,
CMDT_SUSPEND = 1 << 6
} CommandType;
/* Describes the arguments a command expects */
typedef struct {
int is_var_args; /* True if a variable number of arguments are accepted.
When true other members of this struct aren't used */
size_t arg_num; /* The number of arguments this command expects */
ValueType arg_types[MAX_CMD_ARG_NUM]; /* Value types of the expected
arguments */
} CommandSignature;
#define CMDSIG_STRUCT(is_var_args,arg_num,...) { \
(is_var_args), \
(arg_num), \
{ __VA_ARGS__ } \
}
#define CMDSIG_VAR_ARGS CMDSIG_STRUCT(1,0,VAL_TYPE_INT)
#define CMDSIG_NO_ARGS CMDSIG_STRUCT(0,0,VAL_TYPE_INT)
#define CMDSIG(arg_num,...) CMDSIG_STRUCT(0,(arg_num),__VA_ARGS__)
/* General command properties */
typedef enum {
CP_NONE = 0,
CP_RUN_PRE_SESS_INIT = 1 << 0
} CommandProperties;
/* Command descriptor */
typedef struct {
const char *function_name; /* Config function name */
CommandHandler command_handler; /* function reference */
CommandSignature command_signature; /* Arg info */
CommandType command_type; /* High level categorisation of
what this command does */
CommandProperties command_properties; /* General properties */
const char *arg_description; /* Description of the arguments this command
expects */
const char *description; /* Description of the action this command
performs */
} CommandDefinition;
#define CMD_NO_ARGS { INT_VAL_STRUCT(0) }
/* Modes in which key bindings can be defined */
typedef enum {
OMM_SESSION = 1 << 0,
OMM_BUFFER = 1 << 1,
OMM_PROMPT = 1 << 2,
OMM_PROMPT_COMPLETER = 1 << 3,
OMM_USER = 1 << 4,
OMM_FILE_EXPLORER = 1 << 5
} OperationModeMap;
typedef enum {
OM_SESSION, /* Actions possible in any mode e.g. mouse click */
OM_BUFFER, /* Normal buffer is open */
OM_PROMPT, /* Prompt is open */
OM_PROMPT_COMPLETER, /* Prompt with auto complete functionality is open */
OM_USER, /* User defined mappings */
OM_FILE_EXPLORER, /* File Explorer is active */
OM_ENTRY_NUM
} OperationMode;
/* Stores all key bindings */
typedef struct {
OperationMode op_mode; /* The currently active operation mode */
OperationMode prev_op_mode; /* The previously active operation mode */
int active_op_modes[OM_ENTRY_NUM]; /* Track which modes are active */
RadixTree *maps[OM_ENTRY_NUM]; /* Key bindings for each mode */
} KeyMap;
/* A key mapping can either bind a key directly to an operation or to another
* keystr to be interpreted */
typedef enum {
KMT_OPERATION,
KMT_KEYSTR
} KeyMappingType;
/* Maps a key press to an operation. A collection of KeyMapping's defines
* a set of key bindings */
typedef struct {
KeyMappingType type;
char *key; /* Key string */
union {
Operation op; /* Operation */
char *keystr; /* Sequence of keys */
} value;
} KeyMapping;
/* An operation specifies a command, arguments to the command and an operation
* mode in which it is valid. They define actions which key's can be mapped
* to */
typedef struct {
const char *name; /* Operation name */
OperationMode op_mode; /* Operation mode in which this is valid */
Value args[MAX_CMD_ARG_NUM]; /* Arguments to Command */
size_t arg_num; /* Argument number */
Command command; /* The Command to be called */
const char *description; /* Description of what operation does */
} OperationDefinition;
/* Implements an OutputStream allowing shell command output to be written
* to the terminal */
typedef struct {
OutputStream os;
FILE *output_file;
} FileOutputStream;
int cm_init_key_map(KeyMap *);
void cm_free_key_map(KeyMap *);
void cm_set_operation_mode(KeyMap *, OperationMode);
Status cm_do_operation(struct Session *, const char *key, int *finished);
int cm_is_valid_operation(const struct Session *, const char *key,
size_t key_len, int *is_prefix);
Status cm_do_command(Command cmd, CommandArgs *cmd_args);
int cm_get_command(const char *function_name, Command *cmd);
Status cm_generate_keybinding_table(HelpTable *);
Status cm_generate_command_table(HelpTable *);
Status cm_generate_error_table(HelpTable *);
void cm_free_error_table(HelpTable *);
#endif