-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.c
2848 lines (2294 loc) · 108 KB
/
command.c
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*/
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <errno.h>
#include <assert.h>
#include "shared.h"
#include "status.h"
#include "command.h"
#include "session.h"
#include "buffer.h"
#include "value.h"
#include "util.h"
#include "input.h"
#include "config.h"
#include "config_parse_util.h"
#include "search.h"
#include "replace.h"
#include "prompt_completer.h"
/* Used for Yes/No type prompt questions
* e.g. Do you want to save file? */
typedef enum {
QR_NONE = 0,
QR_YES = 1,
QR_NO = 1 << 1,
QR_CANCEL = 1 << 2,
QR_ERROR = 1 << 3,
QR_ALL = 1 << 4
} QuestionRespose;
static KeyMapping *cm_new_op_key_mapping(const char *key, Operation);
static KeyMapping *cm_new_keystr_key_mapping(const char *key,
const char *keystr);
static KeyMapping *cm_new_key_mapping(KeyMappingType, const char *key,
Operation, const char *keystr);
static void cm_free_key_mapping(KeyMapping *);
static Status cm_run_command(const CommandDefinition *, CommandArgs *);
static const char *cm_get_op_mode_str(OperationMode);
static Status cm_file_output_stream_write(OutputStream *, const char buf[],
size_t buf_len,
size_t *bytes_written);
static Status cm_file_output_stream_close(OutputStream *);
static Status cm_nop(const CommandArgs *);
static Status cm_bp_change_line(const CommandArgs *);
static Status cm_bp_change_char(const CommandArgs *);
static Status cm_bp_to_line_start(const CommandArgs *);
static Status cm_bp_to_hard_line_start(const CommandArgs *);
static Status cm_bp_to_line_end(const CommandArgs *);
static Status cm_bp_to_hard_line_end(const CommandArgs *);
static Status cm_bp_to_next_word(const CommandArgs *);
static Status cm_bp_to_prev_word(const CommandArgs *);
static Status cm_bp_to_buffer_start(const CommandArgs *);
static Status cm_bp_to_buffer_end(const CommandArgs *);
static Status cm_bp_change_page(const CommandArgs *);
static Status cm_bp_to_next_paragraph(const CommandArgs *);
static Status cm_bp_to_prev_paragraph(const CommandArgs *);
static Status cm_bp_goto_matching_bracket(const CommandArgs *);
static Status cm_buffer_insert_char(const CommandArgs *);
static Status cm_buffer_delete_char(const CommandArgs *);
static Status cm_buffer_backspace(const CommandArgs *);
static Status cm_buffer_delete_word(const CommandArgs *);
static Status cm_buffer_delete_prev_word(const CommandArgs *);
static Status cm_buffer_insert_line(const CommandArgs *);
static Status cm_buffer_select_all_text(const CommandArgs *);
static Status cm_buffer_copy_selected_text(const CommandArgs *);
static Status cm_buffer_cut_selected_text(const CommandArgs *);
static Status cm_buffer_paste_text(const CommandArgs *);
static Status cm_buffer_undo(const CommandArgs *);
static Status cm_buffer_redo(const CommandArgs *);
static Status cm_buffer_vert_move_lines(const CommandArgs *);
static Status cm_buffer_duplicate_selection(const CommandArgs *);
static Status cm_buffer_join_lines(const CommandArgs *);
static Status cm_buffer_indent(const CommandArgs *);
static Status cm_buffer_mouse_click(const CommandArgs *);
static Status cm_buffer_save_file(const CommandArgs *);
static Status cm_buffer_save_as(const CommandArgs *);
static Status cm_save_file_prompt(Session *, char **file_path_ptr);
static void cm_generate_find_prompt(const BufferSearch *,
char prompt_text[MAX_CMD_PROMPT_LENGTH]);
static Status cm_prepare_search(Session *, const BufferPos *start_pos,
int allow_find_all, int select_last_entry);
static Status cm_buffer_find(const CommandArgs *);
static Status cm_buffer_find_next(const CommandArgs *);
static Status cm_buffer_toggle_search_direction(const CommandArgs *);
static Status cm_buffer_toggle_search_type(const CommandArgs *);
static Status cm_buffer_toggle_search_case(const CommandArgs *);
static Status cm_buffer_replace(const CommandArgs *);
static Status cm_buffer_goto_line(const CommandArgs *);
static Status cm_prepare_replace(Session *, char **rep_text_ptr,
size_t *rep_length);
static Status cm_session_open_file_prompt(const CommandArgs *);
static Status cm_session_open_file(Session *, const char *);
static Status cm_session_add_empty_buffer(const CommandArgs *);
static Status cm_session_change_tab(const CommandArgs *);
static Status cm_session_tab_mouse_click(const CommandArgs *);
static Status cm_session_save_all(const CommandArgs *);
static Status cm_session_close_buffer(const CommandArgs *);
static Status cm_session_run_command(const CommandArgs *);
static Status cm_previous_prompt_entry(const CommandArgs *);
static Status cm_next_prompt_entry(const CommandArgs *);
static Status cm_prompt_input_finished(const CommandArgs *);
static Status cm_session_change_buffer(const CommandArgs *);
static Status cm_session_file_explorer_toggle_active(const CommandArgs *);
static Status cm_session_file_explorer_select(const CommandArgs *);
static Status cm_session_file_explorer_click(const CommandArgs *);
static Status cm_determine_buffer(Session *, const char *input,
const Buffer **buffer_ptr);
static Status cm_suspend(const CommandArgs *);
static Status cm_session_end(const CommandArgs *);
static Status cm_cmd_input_prompt(Session *, const PromptOpt *);
static QuestionRespose cm_question_prompt(Session *, PromptType,
const char *question,
QuestionRespose allowed_answers,
QuestionRespose default_answer);
static Status cm_cancel_prompt(const CommandArgs *);
static Status cm_run_prompt_completion(const CommandArgs *);
static Status cm_session_echo(const CommandArgs *);
static Status cm_session_map(const CommandArgs *);
static Status cm_session_unmap(const CommandArgs *);
static Status cm_session_help(const CommandArgs *);
static Status cm_buffer_filter(const CommandArgs *);
static Status cm_buffer_read(const CommandArgs *);
static Status cm_session_write(const CommandArgs *);
static Status cm_session_exec(const CommandArgs *);
/* Allow the following to exceed 80 columns.
* This format is easier to read and maipulate in visual block mode in vim */
static const CommandDefinition cm_commands[] = {
[CMD_NOP] = { NULL , cm_nop , CMDSIG_NO_ARGS , CMDT_NOP, CP_NONE, NULL, NULL },
[CMD_BP_CHANGE_LINE] = { NULL , cm_bp_change_line , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_CHANGE_CHAR] = { NULL , cm_bp_change_char , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_LINE_START] = { NULL , cm_bp_to_line_start , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_HARD_LINE_START] = { NULL , cm_bp_to_hard_line_start , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_LINE_END] = { NULL , cm_bp_to_line_end , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_HARD_LINE_END] = { NULL , cm_bp_to_hard_line_end , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_NEXT_WORD] = { NULL , cm_bp_to_next_word , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_PREV_WORD] = { NULL , cm_bp_to_prev_word , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_NEXT_PARAGRAPH] = { NULL , cm_bp_to_next_paragraph , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_PREV_PARAGRAPH] = { NULL , cm_bp_to_prev_paragraph , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_CHANGE_PAGE] = { NULL , cm_bp_change_page , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_BUFFER_START] = { NULL , cm_bp_to_buffer_start , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_TO_BUFFER_END] = { NULL , cm_bp_to_buffer_end , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BP_GOTO_MATCHING_BRACKET] = { NULL , cm_bp_goto_matching_bracket , CMDSIG_NO_ARGS , CMDT_BUFFER_MOVE, CP_NONE, NULL, NULL },
[CMD_BUFFER_MOUSE_CLICK] = { NULL , cm_buffer_mouse_click , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_INSERT_CHAR] = { NULL , cm_buffer_insert_char , CMDSIG(1, VAL_TYPE_STR) , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_INDENT] = { NULL , cm_buffer_indent , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_DELETE_CHAR] = { NULL , cm_buffer_delete_char , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_BACKSPACE] = { NULL , cm_buffer_backspace , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_DELETE_WORD] = { NULL , cm_buffer_delete_word , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_DELETE_PREV_WORD] = { NULL , cm_buffer_delete_prev_word , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_INSERT_LINE] = { NULL , cm_buffer_insert_line , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_SELECT_ALL_TEXT] = { NULL , cm_buffer_select_all_text , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_COPY_SELECTED_TEXT] = { NULL , cm_buffer_copy_selected_text , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_CUT_SELECTED_TEXT] = { NULL , cm_buffer_cut_selected_text , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_PASTE_TEXT] = { NULL , cm_buffer_paste_text , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_UNDO] = { NULL , cm_buffer_undo , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_REDO] = { NULL , cm_buffer_redo , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_VERT_MOVE_LINES] = { NULL , cm_buffer_vert_move_lines , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_DUPLICATE_SELECTION] = { NULL , cm_buffer_duplicate_selection , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_JOIN_LINES] = { NULL , cm_buffer_join_lines , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_SAVE_FILE] = { NULL , cm_buffer_save_file , CMDSIG_NO_ARGS , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_BUFFER_SAVE_AS] = { NULL , cm_buffer_save_as , CMDSIG_NO_ARGS , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_BUFFER_FIND] = { NULL , cm_buffer_find , CMDSIG(1, VAL_TYPE_INT) , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_BUFFER_TOGGLE_SEARCH_TYPE] = { NULL , cm_buffer_toggle_search_type , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_TOGGLE_SEARCH_CASE] = { NULL , cm_buffer_toggle_search_case , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_TOGGLE_SEARCH_DIRECTION] = { NULL , cm_buffer_toggle_search_direction , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_REPLACE] = { NULL , cm_buffer_replace , CMDSIG_NO_ARGS , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_PREVIOUS_PROMPT_ENTRY] = { NULL , cm_previous_prompt_entry , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_NEXT_PROMPT_ENTRY] = { NULL , cm_next_prompt_entry , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_PROMPT_INPUT_FINISHED] = { NULL , cm_prompt_input_finished , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_CANCEL_PROMPT] = { NULL , cm_cancel_prompt , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_RUN_PROMPT_COMPLETION] = { NULL , cm_run_prompt_completion , CMDSIG_NO_ARGS , CMDT_CMD_MOD, CP_NONE, NULL, NULL },
[CMD_BUFFER_GOTO_LINE] = { NULL , cm_buffer_goto_line , CMDSIG_NO_ARGS , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_SESSION_OPEN_FILE] = { NULL , cm_session_open_file_prompt , CMDSIG_NO_ARGS , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_SESSION_ADD_EMPTY_BUFFER] = { NULL , cm_session_add_empty_buffer , CMDSIG_NO_ARGS , CMDT_SESS_MOD, CP_NONE, NULL, NULL },
[CMD_SESSION_CHANGE_TAB] = { NULL , cm_session_change_tab , CMDSIG(1, VAL_TYPE_INT) , CMDT_SESS_MOD, CP_NONE, NULL, NULL },
[CMD_SESSION_TAB_MOUSE_CLICK] = { NULL , cm_session_tab_mouse_click , CMDSIG_NO_ARGS , CMDT_SESS_MOD, CP_NONE, NULL, NULL },
[CMD_SESSION_SAVE_ALL] = { NULL , cm_session_save_all , CMDSIG_NO_ARGS , CMDT_SESS_MOD, CP_NONE, NULL, NULL },
[CMD_SESSION_CLOSE_BUFFER] = { NULL , cm_session_close_buffer , CMDSIG(1, VAL_TYPE_INT) , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_SESSION_RUN_COMMAND] = { NULL , cm_session_run_command , CMDSIG_NO_ARGS , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_SESSION_CHANGE_BUFFER] = { NULL , cm_session_change_buffer , CMDSIG_NO_ARGS , CMDT_CMD_INPUT, CP_NONE, NULL, NULL },
[CMD_SESSION_FILE_EXPLORER_TOGGLE_ACTIVE] = { NULL , cm_session_file_explorer_toggle_active, CMDSIG_NO_ARGS , CMDT_SESS_MOD, CP_NONE, NULL, NULL },
[CMD_SESSION_FILE_EXPLORER_SELECT] = { NULL , cm_session_file_explorer_select , CMDSIG_NO_ARGS , CMDT_SESS_MOD, CP_NONE, NULL, NULL },
[CMD_SESSION_FILE_EXPLORER_CLICK] = { NULL , cm_session_file_explorer_click , CMDSIG_NO_ARGS , CMDT_SESS_MOD, CP_NONE, NULL, NULL },
[CMD_SUSPEND] = { NULL , cm_suspend , CMDSIG_NO_ARGS , CMDT_SUSPEND, CP_NONE, NULL, NULL },
[CMD_SESSION_END] = { NULL , cm_session_end , CMDSIG_NO_ARGS , CMDT_EXIT, CP_NONE, NULL, NULL },
/* Commands that by default are not mapped to key bindings and are instead exposed as functions */
[CMD_SESSION_ECHO] = { "echo" , cm_session_echo , CMDSIG_VAR_ARGS , CMDT_SESS_MOD, CP_NONE, "variable", "Displays arguments in the status bar" },
[CMD_SESSION_MAP] = { "map" , cm_session_map , CMDSIG(2, VAL_TYPE_STR, VAL_TYPE_STR), CMDT_SESS_MOD, CP_RUN_PRE_SESS_INIT, "string KEYS, string KEYS", "Maps a sequence of keys to another sequence of keys" },
[CMD_SESSION_UNMAP] = { "unmap" , cm_session_unmap , CMDSIG(1, VAL_TYPE_STR) , CMDT_SESS_MOD, CP_RUN_PRE_SESS_INIT, "string KEYS", "Unmaps a previously created key mapping" },
[CMD_SESSION_HELP] = { "help" , cm_session_help , CMDSIG_NO_ARGS , CMDT_SESS_MOD, CP_RUN_PRE_SESS_INIT, "none", "Display basic help information" },
[CMD_BUFFER_FILTER] = { "filter", cm_buffer_filter , CMDSIG(1, VAL_TYPE_SHELL_COMMAND) , CMDT_BUFFER_MOD, CP_NONE, "shell command CMD", "Filter buffer through shell command" },
[CMD_BUFFER_READ] = { "read" , cm_buffer_read , CMDSIG(1, VAL_TYPE_STR | VAL_TYPE_SHELL_COMMAND), CMDT_BUFFER_MOD, CP_NONE, "shell command CMD or string FILE", "Read command output or file content into buffer" },
[CMD_BUFFER_WRITE] = { "write" , cm_session_write , CMDSIG(1, VAL_TYPE_STR | VAL_TYPE_SHELL_COMMAND), CMDT_SESS_MOD, CP_NONE, "shell command CMD or string FILE", "Write buffer content to command or file" },
[CMD_SESSION_EXEC] = { "exec" , cm_session_exec , CMDSIG(1, VAL_TYPE_SHELL_COMMAND), CMDT_SESS_MOD, CP_NONE, "shell command CMD", "Run shell command" }
};
static const OperationDefinition cm_operations[] = {
[OP_NOP] = { "<wed-nop>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_NOP, "No operation" },
[OP_MOVE_PREV_LINE] = { "<wed-move-prev-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_UP) }, 1, CMD_BP_CHANGE_LINE, "Move up a (screen) line" },
[OP_MOVE_NEXT_LINE] = { "<wed-move-next-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_DOWN) }, 1, CMD_BP_CHANGE_LINE, "Move down a (screen) line" },
[OP_MOVE_NEXT_CHAR] = { "<wed-move-next-char>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_RIGHT) }, 1, CMD_BP_CHANGE_CHAR, "Move right one character" },
[OP_MOVE_PREV_CHAR] = { "<wed-move-prev-char>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_LEFT) }, 1, CMD_BP_CHANGE_CHAR, "Move left one character" },
[OP_MOVE_START_OF_SCREEN_LINE] = { "<wed-move-start-of-screen-line>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_LINE_START, "Move to start of (screen) line" },
[OP_MOVE_START_OF_LINE] = { "<wed-move-start-of-line>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_HARD_LINE_START, "Move to start of line" },
[OP_MOVE_END_OF_SCREEN_LINE] = { "<wed-move-end-of-screen-line>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_LINE_END, "Move to end of (screen) line" },
[OP_MOVE_END_OF_LINE] = { "<wed-move-end-of-line>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_HARD_LINE_END, "Move to end of line" },
[OP_MOVE_NEXT_WORD] = { "<wed-move-next-word>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_NEXT_WORD, "Move to next word" },
[OP_MOVE_PREV_WORD] = { "<wed-move-prev-word>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_PREV_WORD, "Move to previous word" },
[OP_MOVE_PREV_PARAGRAPH] = { "<wed-move-prev-paragraph>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_PREV_PARAGRAPH, "Move to previous paragraph" },
[OP_MOVE_NEXT_PARAGRAPH] = { "<wed-move-next-paragraph>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_NEXT_PARAGRAPH, "Move to next paragraph" },
[OP_MOVE_PREV_PAGE] = { "<wed-move-prev-page>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_UP) }, 1, CMD_BP_CHANGE_PAGE, "Move up a page" },
[OP_MOVE_NEXT_PAGE] = { "<wed-move-next-page>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_DOWN) }, 1, CMD_BP_CHANGE_PAGE, "Move down a page" },
[OP_MOVE_BUFFER_START] = { "<wed-move-buffer-start>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_BUFFER_START, "Move to start of file" },
[OP_MOVE_BUFFER_END] = { "<wed-move-buffer-end>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_BUFFER_END, "Move to end of file" },
[OP_MOVE_SELECT_PREV_LINE] = { "<wed-move-select-prev-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_UP | DIRECTION_WITH_SELECT) }, 1, CMD_BP_CHANGE_LINE, "Select up a (screen) line" },
[OP_MOVE_SELECT_NEXT_LINE] = { "<wed-move-select-next-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_DOWN | DIRECTION_WITH_SELECT) }, 1, CMD_BP_CHANGE_LINE, "Select down a (screen) line" },
[OP_MOVE_SELECT_NEXT_CHAR] = { "<wed-move-select-next-char>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_RIGHT | DIRECTION_WITH_SELECT) }, 1, CMD_BP_CHANGE_CHAR, "Select right one character" },
[OP_MOVE_SELECT_PREV_CHAR] = { "<wed-move-select-prev-char>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_LEFT | DIRECTION_WITH_SELECT) }, 1, CMD_BP_CHANGE_CHAR, "Select left one character" },
[OP_MOVE_SELECT_START_OF_SCREEN_LINE] = { "<wed-move-select-start-of-screen-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_LINE_START, "Select to start of (screen) line" },
[OP_MOVE_SELECT_START_OF_LINE] = { "<wed-move-select-start-of-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_HARD_LINE_START, "Select to start of line" },
[OP_MOVE_SELECT_END_OF_SCREEN_LINE] = { "<wed-move-select-end-of-screen-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_LINE_END, "Select to end of (screen) line" },
[OP_MOVE_SELECT_END_OF_LINE] = { "<wed-move-select-end-of-line>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_HARD_LINE_END, "Select to end of line" },
[OP_MOVE_SELECT_NEXT_WORD] = { "<wed-move-select-next-word>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_NEXT_WORD, "Select to next word" },
[OP_MOVE_SELECT_PREV_WORD] = { "<wed-move-select-prev-word>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_PREV_WORD, "Select to previous word" },
[OP_MOVE_SELECT_PREV_PARAGRAPH] = { "<wed-move-select-prev-paragraph>", OM_BUFFER, { INT_VAL_STRUCT(1) }, 1, CMD_BP_TO_PREV_PARAGRAPH, "Select to previous paragraph" },
[OP_MOVE_SELECT_NEXT_PARAGRAPH] = { "<wed-move-select-next-paragraph>", OM_BUFFER, { INT_VAL_STRUCT(1) }, 1, CMD_BP_TO_NEXT_PARAGRAPH, "Select to next paragraph" },
[OP_MOVE_SELECT_PREV_PAGE] = { "<wed-move-select-prev-page>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_UP | DIRECTION_WITH_SELECT) }, 1, CMD_BP_CHANGE_PAGE, "Select up a page" },
[OP_MOVE_SELECT_NEXT_PAGE] = { "<wed-move-select-next-page>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_DOWN | DIRECTION_WITH_SELECT) }, 1, CMD_BP_CHANGE_PAGE, "Select down a page" },
[OP_MOVE_SELECT_BUFFER_START] = { "<wed-move-select-buffer-start>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_BUFFER_START, "Select to start of file" },
[OP_MOVE_SELECT_BUFFER_END] = { "<wed-move-select-buffer-end>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_BUFFER_END, "Select to end of file" },
[OP_MOVE_MATCHING_BRACKET] = { "<wed-move-matching-bracket>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BP_GOTO_MATCHING_BRACKET, "Move to matching bracket" },
[OP_MOVE_TO_CLICK_POSITION] = { "<wed-buffer-mouse-click>", OM_SESSION, CMD_NO_ARGS, 0, CMD_BUFFER_MOUSE_CLICK, "Move to mouse click position in buffer" },
[OP_INDENT] = { "<wed-indent>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_RIGHT) }, 1, CMD_BUFFER_INDENT, "Indent selected text" },
[OP_UNINDENT] = { "<wed-unindent>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_LEFT) }, 1, CMD_BUFFER_INDENT, "Unindent selected text" },
[OP_DELETE] = { "<wed-delete>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_DELETE_CHAR, "Delete next character" },
[OP_BACKSPACE] = { "<wed-backspace>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_BACKSPACE, "Delete previous character" },
[OP_DELETE_NEXT_WORD] = { "<wed-delete-next-word>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_DELETE_WORD, "Delete next word" },
[OP_DELETE_PREV_WORD] = { "<wed-delete-prev-word>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_DELETE_PREV_WORD, "Delete previous word" },
[OP_INSERT_NEWLINE] = { "<wed-insert-newline>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_INSERT_LINE, "Insert new line" },
[OP_INSERT_SPACE] = { "<wed-insert-space>", OM_BUFFER, { STR_VAL_STRUCT(" ") }, 1, CMD_BUFFER_INSERT_CHAR, "Insert space" },
[OP_INSERT_KPDIV] = { "<wed-insert-kpdiv>", OM_BUFFER, { STR_VAL_STRUCT("/") }, 1, CMD_BUFFER_INSERT_CHAR, "Insert forward slash" },
[OP_INSERT_KPMULT] = { "<wed-insert-kpmult>", OM_BUFFER, { STR_VAL_STRUCT("*") }, 1, CMD_BUFFER_INSERT_CHAR, "Insert star" },
[OP_INSERT_KPMINUS] = { "<wed-insert-kpminus>", OM_BUFFER, { STR_VAL_STRUCT("-") }, 1, CMD_BUFFER_INSERT_CHAR, "Insert minus" },
[OP_INSERT_KPPLUS] = { "<wed-insert-kpplus>", OM_BUFFER, { STR_VAL_STRUCT("+") }, 1, CMD_BUFFER_INSERT_CHAR, "Insert plus" },
[OP_SELECT_ALL] = { "<wed-select-all>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_SELECT_ALL_TEXT, "Select all text" },
[OP_COPY] = { "<wed-copy>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_COPY_SELECTED_TEXT, "Copy selected text" },
[OP_CUT] = { "<wed-cut>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_CUT_SELECTED_TEXT, "Cut selected text" },
[OP_PASTE] = { "<wed-paste>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_PASTE_TEXT, "Paste text" },
[OP_UNDO] = { "<wed-undo>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_UNDO, "Undo" },
[OP_REDO] = { "<wed-redo>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_REDO, "Redo" },
[OP_MOVE_LINES_UP] = { "<wed-move-lines-up>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_UP) }, 1, CMD_BUFFER_VERT_MOVE_LINES, "Move current line (or selected lines) up" },
[OP_MOVE_LINES_DOWN] = { "<wed-move-lines-down>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_DOWN) }, 1, CMD_BUFFER_VERT_MOVE_LINES, "Move current line (or selected lines) down" },
[OP_DUPLICATE] = { "<wed-duplicate>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_DUPLICATE_SELECTION, "Duplicate current line (or selected lines)" },
[OP_JOIN_LINES] = { "<wed-join-lines>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_JOIN_LINES, "Join (selected) lines" },
[OP_SAVE] = { "<wed-save>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_SAVE_FILE, "Save" },
[OP_SAVE_AS] = { "<wed-save-as>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_SAVE_AS, "Save as" },
[OP_FIND] = { "<wed-find>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_BUFFER_FIND, "Find" },
[OP_FIND_REPLACE] = { "<wed-find-replace>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_REPLACE, "Replace" },
[OP_GOTO_LINE] = { "<wed-goto-line>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_BUFFER_GOTO_LINE, "Goto line" },
[OP_OPEN] = { "<wed-open>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SESSION_OPEN_FILE, "Open file" },
[OP_NEW] = { "<wed-new>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SESSION_ADD_EMPTY_BUFFER, "New file" },
[OP_NEXT_BUFFER] = { "<wed-next-buffer>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_RIGHT) }, 1, CMD_SESSION_CHANGE_TAB, "Next tab" },
[OP_PREV_BUFFER] = { "<wed-prev-buffer>", OM_BUFFER, { INT_VAL_STRUCT(DIRECTION_LEFT) }, 1, CMD_SESSION_CHANGE_TAB, "Previous tab" },
[OP_SET_CLICKED_TAB_ACTIVE] = { "<wed-tab-mouse-click>", OM_SESSION, CMD_NO_ARGS, 0, CMD_SESSION_TAB_MOUSE_CLICK, "Make clicked tab active" },
[OP_SAVE_ALL] = { "<wed-save-all>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SESSION_SAVE_ALL, "Save all" },
[OP_CLOSE] = { "<wed-close>", OM_BUFFER, { INT_VAL_STRUCT(0) }, 1, CMD_SESSION_CLOSE_BUFFER, "Close file" },
[OP_CMD] = { "<wed-cmd>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SESSION_RUN_COMMAND, "Open wed command prompt" },
[OP_CHANGE_BUFFER] = { "<wed-change-buffer>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SESSION_CHANGE_BUFFER, "Change file" },
[OP_SWITCH_TO_FILE_EXPLORER] = { "<wed-toggle-file-explorer-active>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SESSION_FILE_EXPLORER_TOGGLE_ACTIVE, "Switch to the file explorer" },
[OP_SUSPEND] = { "<wed-suspend>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SUSPEND, "Suspend" },
[OP_EXIT] = { "<wed-exit>", OM_BUFFER, CMD_NO_ARGS, 0, CMD_SESSION_END, "Exit" },
[OP_TOGGLE_SEARCH_TYPE] = { "<wed-toggle-search-type>", OM_PROMPT, CMD_NO_ARGS, 0, CMD_BUFFER_TOGGLE_SEARCH_TYPE, "Toggle search type" },
[OP_TOGGLE_SEARCH_CASE_SENSITIVITY] = { "<wed-toggle-search-case-sensitivity>", OM_PROMPT, CMD_NO_ARGS, 0, CMD_BUFFER_TOGGLE_SEARCH_CASE, "Toggle search case sensitivity" },
[OP_TOGGLE_SEARCH_DIRECTION] = { "<wed-toggle-search-direction>", OM_PROMPT, CMD_NO_ARGS, 0, CMD_BUFFER_TOGGLE_SEARCH_DIRECTION, "Toggle search direction" },
[OP_PROMPT_PREV_ENTRY] = { "<wed-prompt-prev-entry>", OM_PROMPT, CMD_NO_ARGS, 0, CMD_PREVIOUS_PROMPT_ENTRY, "Previous prompt entry" },
[OP_PROMPT_NEXT_ENTRY] = { "<wed-prompt-next-entry>", OM_PROMPT, CMD_NO_ARGS, 0, CMD_NEXT_PROMPT_ENTRY, "Subsequent prompt entry" },
[OP_PROMPT_SUBMIT] = { "<wed-prompt-submit>", OM_PROMPT, CMD_NO_ARGS, 0, CMD_PROMPT_INPUT_FINISHED, "Submit" },
[OP_PROMPT_CANCEL] = { "<wed-prompt-cancel>", OM_PROMPT, CMD_NO_ARGS, 0, CMD_CANCEL_PROMPT, "Quit prompt" },
[OP_PROMPT_COMPLETE] = { "<wed-prompt-complete>", OM_PROMPT_COMPLETER, CMD_NO_ARGS, 0, CMD_RUN_PROMPT_COMPLETION, "Complete entered text, then cycle through suggestions on subsequent presses" },
[OP_PROMPT_COMPLETE_PREV] = { "<wed-prompt-complete-prev>", OM_PROMPT_COMPLETER, CMD_NO_ARGS, 0, CMD_RUN_PROMPT_COMPLETION, "Complete entered text, then cycle through suggestions in reverse on subsequent presses" },
[OP_FILE_EXPLORER_NEXT_ENTRY] = { "<wed-file-explorer-next-entry>", OM_FILE_EXPLORER, { INT_VAL_STRUCT(DIRECTION_DOWN) }, 1, CMD_BP_CHANGE_LINE, "Move down to the next entry" },
[OP_FILE_EXPLORER_PREV_ENTRY] = { "<wed-file-explorer-prev-entry>", OM_FILE_EXPLORER, { INT_VAL_STRUCT(DIRECTION_UP) }, 1, CMD_BP_CHANGE_LINE, "Move up to the previous entry" },
[OP_FILE_EXPLORER_NEXT_PAGE] = { "<wed-file-explorer-next-page>", OM_FILE_EXPLORER, { INT_VAL_STRUCT(DIRECTION_DOWN) }, 1, CMD_BP_CHANGE_PAGE, "Move down a page" },
[OP_FILE_EXPLORER_PREV_PAGE] = { "<wed-file-explorer-prev-page>", OM_FILE_EXPLORER, { INT_VAL_STRUCT(DIRECTION_UP) }, 1, CMD_BP_CHANGE_PAGE, "Move up a page" },
[OP_FILE_EXPLORER_TOP] = { "<wed-file-explorer-top>", OM_FILE_EXPLORER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_BUFFER_START, "Go to the first entry" },
[OP_FILE_EXPLORER_BOTTOM] = { "<wed-file-explorer-bottom>", OM_FILE_EXPLORER, { INT_VAL_STRUCT(0) }, 1, CMD_BP_TO_BUFFER_END, "Go to the last entry" },
[OP_FILE_EXPLORER_FIND] = { "<wed-file-explorer-find>", OM_FILE_EXPLORER, { INT_VAL_STRUCT(0) }, 1, CMD_BUFFER_FIND, "Search the file explorer entries" },
[OP_FILE_EXPLORER_SELECT] = { "<wed-file-explorer-select>", OM_FILE_EXPLORER, CMD_NO_ARGS, 0, CMD_SESSION_FILE_EXPLORER_SELECT, "Open the selected file or navigate into the selected directory" },
[OP_FILE_EXPLORER_QUIT] = { "<wed-file-explorer-quit>", OM_FILE_EXPLORER, CMD_NO_ARGS, 0, CMD_SESSION_FILE_EXPLORER_TOGGLE_ACTIVE, "Return to the last active buffer" },
[OP_FILE_EXPLORER_EXIT_WED] = { "<wed-file-explorer-exit-wed>", OM_FILE_EXPLORER, CMD_NO_ARGS, 0, CMD_SESSION_END, "Exit" },
[OP_FILE_EXPLORER_CLICK_SELECT] = { "<wed-file-explorer-mouse-click>", OM_SESSION, CMD_NO_ARGS, 0, CMD_SESSION_FILE_EXPLORER_CLICK, "Selected a file or directory" }
};
/* Default wed keybindings */
static const KeyMapping cm_key_mappings[] = {
{ KMT_OPERATION, "<Up>", { OP_MOVE_PREV_LINE } },
{ KMT_OPERATION, "<Down>", { OP_MOVE_NEXT_LINE } },
{ KMT_OPERATION, "<Right>", { OP_MOVE_NEXT_CHAR } },
{ KMT_OPERATION, "<Left>", { OP_MOVE_PREV_CHAR } },
{ KMT_OPERATION, "<Home>", { OP_MOVE_START_OF_SCREEN_LINE } },
{ KMT_OPERATION, "<M-Home>", { OP_MOVE_START_OF_LINE } },
{ KMT_OPERATION, "<End>", { OP_MOVE_END_OF_SCREEN_LINE } },
{ KMT_OPERATION, "<M-End>", { OP_MOVE_END_OF_LINE } },
{ KMT_OPERATION, "<C-Right>", { OP_MOVE_NEXT_WORD } },
{ KMT_OPERATION, "<C-Left>", { OP_MOVE_PREV_WORD } },
{ KMT_OPERATION, "<C-Up>", { OP_MOVE_PREV_PARAGRAPH } },
{ KMT_OPERATION, "<C-Down>", { OP_MOVE_NEXT_PARAGRAPH } },
{ KMT_OPERATION, "<PageUp>", { OP_MOVE_PREV_PAGE } },
{ KMT_OPERATION, "<PageDown>", { OP_MOVE_NEXT_PAGE } },
{ KMT_OPERATION, "<C-Home>", { OP_MOVE_BUFFER_START } },
{ KMT_OPERATION, "<C-End>", { OP_MOVE_BUFFER_END } },
{ KMT_OPERATION, "<S-Up>", { OP_MOVE_SELECT_PREV_LINE } },
{ KMT_OPERATION, "<S-Down>", { OP_MOVE_SELECT_NEXT_LINE } },
{ KMT_OPERATION, "<S-Right>", { OP_MOVE_SELECT_NEXT_CHAR } },
{ KMT_OPERATION, "<S-Left>", { OP_MOVE_SELECT_PREV_CHAR } },
{ KMT_OPERATION, "<S-Home>", { OP_MOVE_SELECT_START_OF_SCREEN_LINE } },
{ KMT_OPERATION, "<M-S-Home>", { OP_MOVE_SELECT_START_OF_LINE } },
{ KMT_OPERATION, "<S-End>", { OP_MOVE_SELECT_END_OF_SCREEN_LINE } },
{ KMT_OPERATION, "<M-S-End>", { OP_MOVE_SELECT_END_OF_LINE } },
{ KMT_OPERATION, "<C-S-Right>", { OP_MOVE_SELECT_NEXT_WORD } },
{ KMT_OPERATION, "<C-S-Left>", { OP_MOVE_SELECT_PREV_WORD } },
{ KMT_OPERATION, "<C-S-Up>", { OP_MOVE_SELECT_PREV_PARAGRAPH } },
{ KMT_OPERATION, "<C-S-Down>", { OP_MOVE_SELECT_NEXT_PARAGRAPH } },
{ KMT_OPERATION, "<S-PageUp>", { OP_MOVE_SELECT_PREV_PAGE } },
{ KMT_OPERATION, "<S-PageDown>", { OP_MOVE_SELECT_NEXT_PAGE } },
{ KMT_OPERATION, "<C-S-Home>", { OP_MOVE_SELECT_BUFFER_START } },
{ KMT_OPERATION, "<C-S-End>", { OP_MOVE_SELECT_BUFFER_END } },
{ KMT_OPERATION, "<C-b>", { OP_MOVE_MATCHING_BRACKET } },
{ KMT_OPERATION, "<Tab>", { OP_INDENT } },
{ KMT_OPERATION, "<S-Tab>", { OP_UNINDENT } },
{ KMT_OPERATION, "<Delete>", { OP_DELETE } },
{ KMT_OPERATION, "<Backspace>", { OP_BACKSPACE } },
{ KMT_OPERATION, "<C-Delete>", { OP_DELETE_NEXT_WORD } },
{ KMT_OPERATION, "<M-Backspace>", { OP_DELETE_PREV_WORD } },
{ KMT_OPERATION, "<Enter>", { OP_INSERT_NEWLINE } },
{ KMT_OPERATION, "<Space>", { OP_INSERT_SPACE } },
{ KMT_OPERATION, "<KPDiv>", { OP_INSERT_KPDIV } },
{ KMT_OPERATION, "<KPMult>", { OP_INSERT_KPMULT } },
{ KMT_OPERATION, "<KPMinus>", { OP_INSERT_KPMINUS } },
{ KMT_OPERATION, "<KPPlus>", { OP_INSERT_KPPLUS } },
{ KMT_OPERATION, "<C-a>", { OP_SELECT_ALL } },
{ KMT_OPERATION, "<C-c>", { OP_COPY } },
{ KMT_OPERATION, "<C-x>", { OP_CUT } },
{ KMT_OPERATION, "<C-v>", { OP_PASTE } },
{ KMT_OPERATION, "<C-z>", { OP_UNDO } },
{ KMT_OPERATION, "<C-y>", { OP_REDO } },
{ KMT_OPERATION, "<M-C-Up>", { OP_MOVE_LINES_UP } },
{ KMT_OPERATION, "<M-C-Down>", { OP_MOVE_LINES_DOWN } },
{ KMT_OPERATION, "<C-d>", { OP_DUPLICATE } },
{ KMT_OPERATION, "<C-j>", { OP_JOIN_LINES } },
{ KMT_OPERATION, "<C-s>", { OP_SAVE } },
{ KMT_OPERATION, "<M-C-s>", { OP_SAVE_AS } },
{ KMT_OPERATION, "<C-f>", { OP_FIND } },
{ KMT_OPERATION, "<C-h>", { OP_FIND_REPLACE } },
{ KMT_OPERATION, "<C-r>", { OP_FIND_REPLACE } },
{ KMT_OPERATION, "<C-g>", { OP_GOTO_LINE } },
{ KMT_OPERATION, "<C-o>", { OP_OPEN } },
{ KMT_OPERATION, "<C-n>", { OP_NEW } },
{ KMT_OPERATION, "<M-C-Right>", { OP_NEXT_BUFFER } },
{ KMT_OPERATION, "<M-Right>", { OP_NEXT_BUFFER } },
{ KMT_OPERATION, "<M-C-Left>", { OP_PREV_BUFFER } },
{ KMT_OPERATION, "<M-Left>", { OP_PREV_BUFFER } },
{ KMT_OPERATION, "<C-^>", { OP_SAVE_ALL } },
{ KMT_OPERATION, "<C-w>", { OP_CLOSE } },
{ KMT_OPERATION, "<C-\\>", { OP_CMD } },
{ KMT_OPERATION, "<C-e>", { OP_CMD } },
{ KMT_OPERATION, "<C-_>", { OP_CHANGE_BUFFER } },
{ KMT_OPERATION, "<C-t>", { OP_SWITCH_TO_FILE_EXPLORER } },
{ KMT_OPERATION, "<M-z>", { OP_SUSPEND } },
{ KMT_OPERATION, "<Escape>", { OP_EXIT } },
{ KMT_OPERATION, "<C-t>", { OP_TOGGLE_SEARCH_TYPE } },
{ KMT_OPERATION, "<C-s>", { OP_TOGGLE_SEARCH_CASE_SENSITIVITY } },
{ KMT_OPERATION, "<C-d>", { OP_TOGGLE_SEARCH_DIRECTION } },
{ KMT_OPERATION, "<Up>", { OP_PROMPT_PREV_ENTRY } },
{ KMT_OPERATION, "<Down>", { OP_PROMPT_NEXT_ENTRY } },
{ KMT_OPERATION, "<Enter>", { OP_PROMPT_SUBMIT } },
{ KMT_OPERATION, "<Escape>", { OP_PROMPT_CANCEL } },
{ KMT_OPERATION, "<Tab>", { OP_PROMPT_COMPLETE } },
{ KMT_OPERATION, "<S-Tab>", { OP_PROMPT_COMPLETE_PREV } },
{ KMT_OPERATION, "<Down>", { OP_FILE_EXPLORER_NEXT_ENTRY } },
{ KMT_OPERATION, "<Up>", { OP_FILE_EXPLORER_PREV_ENTRY } },
{ KMT_OPERATION, "<PageDown>", { OP_FILE_EXPLORER_NEXT_PAGE } },
{ KMT_OPERATION, "<PageUp>", { OP_FILE_EXPLORER_PREV_PAGE } },
{ KMT_OPERATION, "<C-Home>", { OP_FILE_EXPLORER_TOP } },
{ KMT_OPERATION, "<C-End>", { OP_FILE_EXPLORER_BOTTOM } },
{ KMT_OPERATION, "<C-f>", { OP_FILE_EXPLORER_FIND } },
{ KMT_OPERATION, "<Enter>", { OP_FILE_EXPLORER_SELECT } },
{ KMT_OPERATION, "<C-t>", { OP_FILE_EXPLORER_QUIT } },
{ KMT_OPERATION, "<Escape>", { OP_FILE_EXPLORER_EXIT_WED } }
};
/* Map key presses to operations. User input can be used to look
* up an operation in a key_map so that it can be invoked */
int cm_init_key_map(KeyMap *key_map)
{
memset(key_map, 0, sizeof(KeyMap));
const size_t key_mapping_num = ARRAY_SIZE(cm_key_mappings, KeyMapping);
for (size_t k = 0; k < OM_ENTRY_NUM; k++) {
key_map->maps[k] = rt_new();
if (key_map->maps[k] == NULL) {
return 0;
}
}
KeyMapping *key_mapping;
const OperationDefinition *operation;
RadixTree *map;
for (size_t k = 0; k < key_mapping_num; k++) {
key_mapping = (KeyMapping *)&cm_key_mappings[k];
key_mapping = cm_new_op_key_mapping(key_mapping->key,
key_mapping->value.op);
if (key_mapping == NULL) {
return 0;
}
operation = &cm_operations[key_mapping->value.op];
map = key_map->maps[operation->op_mode];
if (!rt_insert(map, key_mapping->key, strlen(key_mapping->key),
key_mapping)) {
return 0;
}
}
const size_t operation_num = ARRAY_SIZE(cm_operations, OperationDefinition);
for (size_t k = 0; k < operation_num; k++) {
operation = &cm_operations[k];
key_mapping = cm_new_op_key_mapping(operation->name, k);
if (key_mapping == NULL) {
return 0;
}
map = key_map->maps[operation->op_mode];
if (!rt_insert(map, key_mapping->key, strlen(key_mapping->key),
key_mapping)) {
return 0;
}
}
cm_set_operation_mode(key_map, OM_BUFFER);
return 1;
}
void cm_free_key_map(KeyMap *key_map)
{
if (key_map == NULL) {
return;
}
for (size_t k = 0; k < OM_ENTRY_NUM; k++) {
rt_free_including_entries(key_map->maps[k],
(FreeFunction)cm_free_key_mapping);
}
}
void cm_set_operation_mode(KeyMap *key_map, OperationMode op_mode)
{
static const OperationModeMap operation_mode_active_maps[OM_ENTRY_NUM] = {
OMM_SESSION,
OMM_SESSION | OMM_BUFFER | OMM_USER,
OMM_SESSION | OMM_BUFFER | OMM_PROMPT | OMM_USER,
OMM_SESSION | OMM_BUFFER | OMM_PROMPT | OMM_PROMPT_COMPLETER | OMM_USER,
OMM_USER,
OMM_SESSION | OMM_FILE_EXPLORER | OMM_USER
};
key_map->prev_op_mode = key_map->op_mode;
key_map->op_mode = op_mode;
OperationModeMap active_maps = operation_mode_active_maps[op_mode];
for (size_t k = 0; k < OM_ENTRY_NUM; k++) {
key_map->active_op_modes[k] = (active_maps >> k) & 1;
}
}
static KeyMapping *cm_new_op_key_mapping(const char *key, Operation operation)
{
return cm_new_key_mapping(KMT_OPERATION, key, operation, NULL);
}
static KeyMapping *cm_new_keystr_key_mapping(const char *key,
const char *keystr)
{
return cm_new_key_mapping(KMT_KEYSTR, key, 0, keystr);
}
static KeyMapping *cm_new_key_mapping(KeyMappingType type, const char *key,
Operation operation, const char *keystr)
{
KeyMapping *key_mapping = malloc(sizeof(KeyMapping));
if (key_mapping == NULL) {
return NULL;
}
memset(key_mapping, 0, sizeof(KeyMapping));
key_mapping->key = strdup(key);
if (key_mapping->key == NULL) {
cm_free_key_mapping(key_mapping);
return NULL;
}
if (type == KMT_OPERATION) {
key_mapping->value.op = operation;
} else if (type == KMT_KEYSTR) {
key_mapping->value.keystr = strdup(keystr);
if (key_mapping->value.keystr == NULL) {
cm_free_key_mapping(key_mapping);
return NULL;
}
} else {
assert(!"Invalid KeyMappingType");
}
key_mapping->type = type;
return key_mapping;
}
static void cm_free_key_mapping(KeyMapping *key_mapping)
{
if (key_mapping == NULL) {
return;
}
if (key_mapping->type == KMT_KEYSTR) {
free(key_mapping->value.keystr);
}
free(key_mapping->key);
free(key_mapping);
}
Status cm_do_operation(Session *sess, const char *key, int *finished)
{
assert(!is_null_or_empty(key));
assert(finished != NULL);
const KeyMapping *key_mapping = NULL;
const KeyMap *key_map = &sess->key_map;
const RadixTree *map;
for (int k = OM_ENTRY_NUM - 1; k > -1; k--) {
if (key_map->active_op_modes[k]) {
map = key_map->maps[k];
if (rt_find(map, key, strlen(key), (void **)&key_mapping, NULL)) {
break;
}
}
}
if (key_mapping != NULL) {
if (key_mapping->type == KMT_OPERATION) {
const OperationDefinition *operation =
&cm_operations[key_mapping->value.op];
const CommandDefinition *command = &cm_commands[operation->command];
CommandArgs cmd_args;
cmd_args.sess = sess;
cmd_args.arg_num = operation->arg_num;
cmd_args.key = key;
cmd_args.finished = finished;
memcpy(cmd_args.args, operation->args, sizeof(operation->args));
return cm_run_command(command, &cmd_args);
} else if (key_mapping->type == KMT_KEYSTR) {
size_t keystr_len = strlen(key_mapping->value.keystr);
return ip_add_keystr_input_to_start(&sess->input_buffer,
key_mapping->value.keystr,
keystr_len);
}
}
if (!(key[0] == '<' && key[1] != '\0') &&
!se_command_type_excluded(sess, CMDT_BUFFER_MOD)) {
/* Just a normal letter character so insert it into buffer */
return bf_insert_character(sess->active_buffer, key, 1);
} else if (strncmp(key, "<wed-", 5) == 0 && key_mapping == NULL) {
/* An invalid operation was specified */
for (int k = OM_ENTRY_NUM - 1; k > -1; k--) {
map = key_map->maps[k];
if (rt_find(map, key, strlen(key), NULL, NULL)) {
return st_get_error(ERR_INVALID_OPERATION_KEY_STRING,
"Operation \"%s\" cannot be "
"used in this context", key);
}
}
return st_get_error(ERR_INVALID_OPERATION_KEY_STRING,
"Invalid operation \"%s\"", key);
}
return STATUS_SUCCESS;
}
int cm_is_valid_operation(const Session *sess, const char *key,
size_t key_len, int *is_prefix)
{
assert(!is_null_or_empty(key));
assert(is_prefix != NULL);
const KeyMapping *key_mapping = NULL;
const KeyMap *key_map = &sess->key_map;
const RadixTree *map;
*is_prefix = 0;
for (int k = OM_ENTRY_NUM - 1; k > -1; k--) {
if (key_map->active_op_modes[k]) {
map = key_map->maps[k];
int mode_is_prefix;
if (rt_find(map, key, key_len, (void **)&key_mapping,
&mode_is_prefix)) {
break;
}
*is_prefix |= mode_is_prefix;
}
}
if (key_mapping != NULL ||
!(key[0] == '<' && key[1] != '\0')) {
*is_prefix = 0;
return 1;
}
return 0;
}
Status cm_do_command(Command cmd, CommandArgs *cmd_args)
{
const CommandDefinition *cmd_def = &cm_commands[cmd];
return cm_run_command(cmd_def, cmd_args);
}
static Status cm_run_command(const CommandDefinition *cmd,
CommandArgs *cmd_args)
{
Session *sess = cmd_args->sess;
if (!(se_initialised(sess) ||
cmd->command_properties & CP_RUN_PRE_SESS_INIT) ||
se_command_type_excluded(sess, cmd->command_type)) {
return STATUS_SUCCESS;
}
const CommandSignature *cmd_sig = &cmd->command_signature;
if (!cmd_sig->is_var_args) {
if (cmd_args->arg_num < cmd_sig->arg_num) {
return st_get_error(ERR_INVALID_COMMAND_ARG_NUM,
"Command expects %zu argument(s) "
"but was invoked with %zu",
cmd_sig->arg_num, cmd_args->arg_num);
}
for (size_t k = 0; k < cmd_sig->arg_num; k++) {
if (!(cmd_args->args[k].type & cmd_sig->arg_types[k])) {
return st_get_error(ERR_INVALID_COMMAND_ARG_TYPE,
"Command expects argument number %zu to "
"have type %s but provided argument has "
"type %s", k + 1,
va_multi_value_type_string(
cmd_sig->arg_types[k]),
va_value_type_string(
cmd_args->args[k].type));
}
}
}
return cmd->command_handler(cmd_args);
}
int cm_get_command(const char *function_name, Command *cmd)
{
if (is_null_or_empty(function_name) || cmd == NULL) {
return 0;
}
static const size_t cmd_num = ARRAY_SIZE(cm_commands, CommandDefinition);
/* TODO Load function names into a hash map for efficient lookup */
for (size_t k = 0; k < cmd_num; k++) {
if (cm_commands[k].function_name != NULL &&
strcmp(function_name, cm_commands[k].function_name) == 0) {
*cmd = k;
return 1;
}
}
return 0;
}
Status cm_generate_keybinding_table(HelpTable *help_table)
{
const size_t key_mapping_num = ARRAY_SIZE(cm_key_mappings, KeyMapping);
if (!hp_init_help_table(help_table, key_mapping_num + 1, 4)) {
return OUT_OF_MEMORY("Unable to create key table");
}
const char ***key_table = help_table->table;
key_table[0][0] = "Key";
key_table[0][1] = "Operation";
key_table[0][2] = "Mode";
key_table[0][3] = "Description";
const KeyMapping *key_mapping;
const OperationDefinition *op_def;
for (size_t k = 0; k < key_mapping_num; k++) {
key_mapping = &cm_key_mappings[k];
op_def = &cm_operations[key_mapping->value.op];
key_table[k + 1][0] = key_mapping->key;
key_table[k + 1][1] = op_def->name;
key_table[k + 1][2] = cm_get_op_mode_str(op_def->op_mode);
key_table[k + 1][3] = op_def->description;
}
return STATUS_SUCCESS;
}
static const char *cm_get_op_mode_str(OperationMode op_mode)
{
switch (op_mode) {
case OM_BUFFER:
return "Normal";
case OM_PROMPT:
return "Prompt";
case OM_PROMPT_COMPLETER:
return "Prompt";
case OM_USER:
return "User";
case OM_FILE_EXPLORER:
return "File Explorer";
default:
break;
}
assert(!"Invalid OperationMode value");
return "";
}
Status cm_generate_command_table(HelpTable *help_table)
{
const size_t command_num = ARRAY_SIZE(cm_commands, CommandDefinition);
size_t function_num = 0;
for (size_t k = 0; k < command_num; k++) {
if (cm_commands[k].function_name != NULL) {
function_num++;
}
}
if (!hp_init_help_table(help_table, function_num + 1, 3)) {
return OUT_OF_MEMORY("Unable to create command table");
}
const char ***cmd_table = help_table->table;
cmd_table[0][0] = "Command";
cmd_table[0][1] = "Arguments";
cmd_table[0][2] = "Description";
const CommandDefinition *cmd_def;
size_t function_index = 1;
for (size_t k = 0; k < command_num; k++) {
if (cm_commands[k].function_name != NULL) {
cmd_def = &cm_commands[k];
cmd_table[function_index][0] = cmd_def->function_name;
cmd_table[function_index][1] = cmd_def->arg_description;
cmd_table[function_index][2] = cmd_def->description;
function_index++;
}
}
return STATUS_SUCCESS;
}
Status cm_generate_error_table(HelpTable *help_table)
{
if (!hp_init_help_table(help_table, ERR_ENTRY_NUM, 2)) {
return OUT_OF_MEMORY("Unable to create error table");
}
const char ***error_table = help_table->table;
error_table[0][0] = "Error Code";
error_table[0][1] = "Description";
char error_code[8];
char *error_code_str;
for (size_t k = ERR_NONE + 1; k < ERR_ENTRY_NUM; k++) {
snprintf(error_code, sizeof(error_code), "%zu", k);
if ((error_code_str = strdup(error_code)) == NULL) {
return OUT_OF_MEMORY("Unable to allocate error code string");
}
error_table[k][0] = error_code_str;
error_table[k][1] = st_get_default_error_message(k);
}
return STATUS_SUCCESS;
}
void cm_free_error_table(HelpTable *help_table)
{
if (help_table == NULL || help_table->table == NULL) {
return;
}
const char ***error_table = help_table->table;
for (size_t k = ERR_NONE + 1; k < ERR_ENTRY_NUM; k++) {
free((char *)error_table[k][0]);
}
}
Status cm_get_file_output_stream(FileOutputStream *fos, const char *file_path)
{
assert(!is_null_or_empty(file_path));
FILE *output_file = fopen(file_path, "wb");
if (output_file == NULL) {
return st_get_error(ERR_UNABLE_TO_OPEN_FILE,
"Unable to open file %s for writing: %s",
file_path, strerror(errno));
}
*fos = (FileOutputStream) {
.os = {
.write = cm_file_output_stream_write,
.close = cm_file_output_stream_close
},
.output_file = output_file
};
return STATUS_SUCCESS;
}
static Status cm_file_output_stream_write(OutputStream *os, const char buf[],
size_t buf_len,
size_t *bytes_written)
{
FileOutputStream *fos = (FileOutputStream *)os;
*bytes_written = fwrite(buf, sizeof(char), buf_len, fos->output_file);
if (*bytes_written != buf_len) {
return st_get_error(ERR_UNABLE_TO_WRITE_TO_FILE,
"Unable to write to file: %s",
strerror(errno));
}
return STATUS_SUCCESS;
}
static Status cm_file_output_stream_close(OutputStream *os)
{
FileOutputStream *fos = (FileOutputStream *)os;
fclose(fos->output_file);
return STATUS_SUCCESS;
}
static Status cm_nop(const CommandArgs *cmd_args)
{
(void)cmd_args;
return STATUS_SUCCESS;
}
static Status cm_bp_change_line(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
return bf_change_line(sess->active_buffer, &sess->active_buffer->pos,
IVAL(param), 1);
}
static Status cm_bp_change_char(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
return bf_change_char(sess->active_buffer, &sess->active_buffer->pos,
IVAL(param), 1);
}
static Status cm_bp_to_line_start(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
return bf_to_line_start(sess->active_buffer, &sess->active_buffer->pos,
IVAL(param) & DIRECTION_WITH_SELECT, 1);
}
static Status cm_bp_to_hard_line_start(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
Buffer *buffer = sess->active_buffer;
BufferPos *pos = &buffer->pos;
int is_select = IVAL(param) & DIRECTION_WITH_SELECT;
return bf_bp_to_line_start(buffer, pos, is_select, 1);
}
static Status cm_bp_to_line_end(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
return bf_to_line_end(sess->active_buffer,
IVAL(param) & DIRECTION_WITH_SELECT);
}
static Status cm_bp_to_hard_line_end(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
Buffer *buffer = sess->active_buffer;
BufferPos *pos = &buffer->pos;
int is_select = IVAL(param) & DIRECTION_WITH_SELECT;
return bf_bp_to_line_end(buffer, pos, is_select, 1);
}
static Status cm_bp_to_next_word(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
return bf_to_next_word(sess->active_buffer,
IVAL(param) & DIRECTION_WITH_SELECT);
}
static Status cm_bp_to_prev_word(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;
Value param = cmd_args->args[0];
return bf_to_prev_word(sess->active_buffer,
IVAL(param) & DIRECTION_WITH_SELECT);
}
static Status cm_bp_to_buffer_start(const CommandArgs *cmd_args)
{
assert(cmd_args->arg_num == 1);
Session *sess = cmd_args->sess;