summaryrefslogtreecommitdiff
path: root/src/main.cc
blob: 60c0669ef8724a988f9e5dc0f9d18ba53072405e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
#include "assert.hh"
#include "backtrace.hh"
#include "buffer.hh"
#include "buffer_utils.hh"
#include "buffer_manager.hh"
#include "client_manager.hh"
#include "command_manager.hh"
#include "commands.hh"
#include "context.hh"
#include "debug.hh"
#include "event_manager.hh"
#include "face_registry.hh"
#include "file.hh"
#include "highlighters.hh"
#include "insert_completer.hh"
#include "json_ui.hh"
#include "terminal_ui.hh"
#include "option_types.hh"
#include "parameters_parser.hh"
#include "profile.hh"
#include "ranges.hh"
#include "regex.hh"
#include "register_manager.hh"
#include "remote.hh"
#include "scope.hh"
#include "shared_string.hh"
#include "shell_manager.hh"
#include "string.hh"
#include "unit_tests.hh"
#include "window.hh"

#include <fcntl.h>
#include <locale.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>

namespace Kakoune
{

extern const char* version;

struct {
    unsigned int version;
    StringView notes;
} constexpr version_notes[] = { {
        20250603,
        "» kak_* appearing in shell arguments will be added to the environment\n"
        "» {+U}double underline{} support\n"
        "» {+u}git apply{} can stage/revert selected changes to current buffer\n"
        "» {+u}exec/eval -client{} accepts '*' and comma separated list\n"
    }, {
        20240518,
        "» Fix tests failing on some platforms\n"
    }, {
        20240509,
        "» {+u}flag-lines -after{} highlighter\n"
        "» asynchronous {+u}shell-script-candidates{} completion\n"
        "» {+b}%val\\{window_range}{} is now emitted as separate strings\n"
        "» {+b}+{} only duplicates identical selections a single time\n"
        "» {+u}daemonize-session{} command\n"
        "» view mode and mouse scrolling no longer change selections\n"
        "» {+u}git apply/blame-jump/edit/grep{} commands\n"
        "» {+u}git blame{} works in {+u}git-diff{} and {+u}git-log{} buffers\n"
        "» custom completions are no longer sorted if the typed text is empty\n"
        "» {+u}terminal{} now selects implementation based on windowing options\n"
        "» {+u}local{} scopes\n"
    }
};

static_assert(sizeof(version_notes) / sizeof(version_notes[0]) <= 4 - (version_notes[0].version != 0 ? 1 : 0),
              "Maximum 3 versions should be displayed in version notes, not including the development version");

void show_startup_info(Client* local_client, int last_version)
{
    const Face version_face{.attributes=Attribute::Bold};
    DisplayLineList info;
    for (auto [version, notes] : version_notes)
    {
        if (version and version <= last_version)
            continue;

        if (not version)
            info.push_back({"• Development version", version_face});
        else
        {
            const auto year = version / 10000;
            const auto month = (version / 100) % 100;
            const auto day = version % 100;
            info.push_back({format("• Kakoune v{}.{:02}.{:02}", year, month, day), version_face});
        }

        for (auto&& line : notes | split<StringView>('\n'))
            info.push_back(parse_display_line(line, GlobalScope::instance().faces()));
    }
    if (not info.empty())
        local_client->info_show({{{format("Kakoune {}", version), version_face},
                                  {", more info at ", {}},
                                  {":doc changelog", {.attributes=Attribute::Underline}}}},
                                 std::move(info), {}, InfoStyle::Prompt);
}

inline void write_stdout(StringView str) { try { write(STDOUT_FILENO, str); } catch (runtime_error&) {} }
inline void write_stderr(StringView str) { try { write(STDERR_FILENO, str); } catch (runtime_error&) {} }

String runtime_directory()
{
    if (const char* runtime_directory = getenv("KAKOUNE_RUNTIME"))
        return runtime_directory;

    char relpath[PATH_MAX+1];
    format_to(relpath, "{}../share/kak", split_path(get_kak_binary_path()).first);
    struct stat st;
    if (stat(relpath, &st) == 0 and S_ISDIR(st.st_mode))
        return real_path(relpath);

    return "/usr/share/kak";
}

String config_directory()
{
    if (StringView kak_cfg_dir = getenv("KAKOUNE_CONFIG_DIR"); not kak_cfg_dir.empty())
        return kak_cfg_dir.str();
    if (StringView xdg_cfg_home = getenv("XDG_CONFIG_HOME"); not xdg_cfg_home.empty())
        return format("{}/kak", xdg_cfg_home);
    return format("{}/.config/kak", homedir());
}

static auto main_sel_first(const SelectionList& selections)
{
    auto beg = &*selections.begin(), end = &*selections.end();
    auto main = beg + selections.main_index();
    using View = ConstArrayView<Selection>;
    return concatenated(View{main, end}, View{beg, main});
}

static const EnvVarDesc builtin_env_vars[] = { {
        "bufname", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {context.buffer().display_name()}; }
    }, {
        "buffile", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {context.buffer().name()}; }
    }, {
        "buflist", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return BufferManager::instance() | transform(&Buffer::display_name) | gather<Vector<String>>(); }
    }, {
        "buf_line_count", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.buffer().line_count())}; }
    }, {
        "timestamp", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.buffer().timestamp())}; }
    }, {
        "history_id", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string((size_t)context.buffer().current_history_id())}; }
    }, {
        "selection", false,
        [](StringView name, const Context& context) -> Vector<String>
        { const Selection& sel = context.selections().main();
          return {content(context.buffer(), sel)}; }
    }, {
        "selections", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return context.selections_content(); }
    }, {
        "runtime", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {runtime_directory()}; }
    }, {
        "config", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {config_directory()}; }
    }, {
        "version", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {version}; }
    }, {
        "opt_", true,
        [](StringView name, const Context& context) -> Vector<String>
        { return context.options()[name.substr(4_byte)].get_as_strings(); }
    }, {
        "main_reg_", true,
        [](StringView name, const Context& context) -> Vector<String>
        { return {context.main_sel_register_value(name.substr(9_byte)).str()}; }
    }, {
        "reg_", true,
        [](StringView name, const Context& context)
        { return RegisterManager::instance()[name.substr(4_byte)].get(context) |
                     gather<Vector<String>>(); }
    }, {
        "client_env_", true,
        [](StringView name, const Context& context) -> Vector<String>
        { return {context.client().get_env_var(name.substr(11_byte)).str()}; }
    }, {
        "session", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {Server::instance().session()}; }
    }, {
        "client", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {context.name()}; }
    }, {
        "client_pid", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.client().pid())}; }
    }, {
        "client_list", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return ClientManager::instance() |
                      transform([](const std::unique_ptr<Client>& c) -> const String&
                                { return c->context().name(); }) | gather<Vector<String>>(); }
    }, {
        "modified", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {context.buffer().is_modified() ? "true" : "false"}; }
    }, {
        "cursor_line", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.selections().main().cursor().line + 1)}; }
    }, {
        "cursor_column", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.selections().main().cursor().column + 1)}; }
    }, {
        "cursor_char_value", false,
        [](StringView name, const Context& context) -> Vector<String>
        { auto coord = context.selections().main().cursor();
          auto& buffer = context.buffer();
          return {to_string((size_t)utf8::codepoint(buffer.iterator_at(coord), buffer.end()))}; }
    }, {
        "cursor_char_column", false,
        [](StringView name, const Context& context) -> Vector<String>
        { auto coord = context.selections().main().cursor();
          return {to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1)}; }
    }, {
        "cursor_display_column", false,
        [](StringView name, const Context& context) -> Vector<String>
        { auto coord = context.selections().main().cursor();
          return {to_string(get_column(context.buffer(),
                                       context.options()["tabstop"].get<int>(),
                                       coord) + 1)}; }
    }, {
        "cursor_byte_offset", false,
        [](StringView name, const Context& context) -> Vector<String>
        { auto cursor = context.selections().main().cursor();
          return {to_string(context.buffer().distance({0,0}, cursor))}; }
    }, {
        "selection_desc", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {selection_to_string(ColumnType::Byte, context.buffer(), context.selections().main())}; }
    }, {
        "selections_desc", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return main_sel_first(context.selections()) |
                     transform([&buffer=context.buffer()](const Selection& sel) {
                         return selection_to_string(ColumnType::Byte, buffer, sel);
                     }) | gather<Vector<String>>(); }
    }, {
        "selections_char_desc", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return main_sel_first(context.selections()) |
                     transform([&buffer=context.buffer()](const Selection& sel) {
                         return selection_to_string(ColumnType::Codepoint, buffer, sel);
                     }) | gather<Vector<String>>(); }
    }, {
        "selections_display_column_desc", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return main_sel_first(context.selections()) |
                     transform([&buffer=context.buffer(), tabstop=context.options()["tabstop"].get<int>()](const Selection& sel) {
                         return selection_to_string(ColumnType::DisplayColumn, buffer, sel, tabstop);
                     }) | gather<Vector<String>>(); }
    }, {
        "selection_length", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(char_length(context.buffer(), context.selections().main()))}; }
    }, {
        "selections_length", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return context.selections() |
                     transform([&](const Selection& s) -> String {
                         return to_string(char_length(context.buffer(), s));
                     }) | gather<Vector<String>>(); }
    }, {
        "selection_count", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.selections().size())}; }
    }, {
        "window_width", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.window().dimensions().column)}; }
    }, {
        "window_height", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return {to_string(context.window().dimensions().line)}; }
    }, {
        "user_modes", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return context.keymaps().user_modes(); }
    }, {
        "window_range", false,
        [](StringView name, const Context& context) -> Vector<String>
        {
            const auto& setup = context.window().last_display_setup();
            return {to_string(setup.first_line), to_string(setup.first_column),
                    to_string(setup.line_count), to_string(0)};
        }
    }, {
        "history", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return history_as_strings(context.buffer().history()); }
    }, {
        "history_since_", true,
        [](StringView name, const Context& context) -> Vector<String>
        { return history_as_strings(
            ArrayView(context.buffer().history())
                .subrange(str_to_int(name.substr(14_byte)) + 1)
        ); }
    }, {
        "uncommitted_modifications", false,
        [](StringView name, const Context& context) -> Vector<String>
        { return undo_group_as_strings(context.buffer().current_undo_group()); }
    }
};

void register_registers()
{
    RegisterManager& register_manager = RegisterManager::instance();

    for (Codepoint c : StringView{"abcdefghijklmnopqrstuvwxyz\"^@"})
        register_manager.add_register(c, std::make_unique<StaticRegister>(String{c}));

    for (Codepoint c : StringView{"/|:\\"})
        register_manager.add_register(c, std::make_unique<HistoryRegister>(String{c}));

    using StringList = Vector<String, MemoryDomain::Registers>;

    register_manager.add_register('%', make_dyn_reg(
        "%",
        [](const Context& context)
        { return StringList{{context.buffer().display_name()}}; }));

    register_manager.add_register('.', make_dyn_reg(
        ".",
        [](const Context& context) {
            auto content = context.selections_content();
            return StringList{content.begin(), content.end()};
         }));

    register_manager.add_register('#', make_dyn_reg(
        "#",
        [](const Context& context) {
            const size_t count = context.selections().size();
            StringList res;
            res.reserve(count);
            for (size_t i = 1; i < count+1; ++i)
                res.push_back(to_string((int)i));
            return res;
        }));

    for (size_t i = 0; i < 10; ++i)
    {
        register_manager.add_register('0'+i, make_dyn_reg(
            String{Codepoint('0'+i)},
            [i](const Context& context) {
                StringList result;
                for (auto& sel : context.selections())
                    result.emplace_back(i < sel.captures().size() ? sel.captures()[i] : "");
                return result;
            },
            [i](Context& context, ConstArrayView<String> values) {
                if (values.empty())
                    return;

                auto& sels = context.selections();
                for (size_t sel_index = 0; sel_index < sels.size(); ++sel_index)
                {
                    auto& sel = sels[sel_index];
                    if (sel.captures().size() < i+1)
                        sel.captures().resize(i+1);
                    sel.captures()[i] = values[std::min(sel_index, values.size()-1)];
                }
            }));
    }

    register_manager.add_register('_', std::make_unique<NullRegister>());
}

void register_keymaps()
{
    auto& keymaps = GlobalScope::instance().keymaps();
    keymaps.map_key(Key::Left, KeymapMode::Normal, {'h'}, "");
    keymaps.map_key(Key::Right, KeymapMode::Normal, {'l'}, "");
    keymaps.map_key(Key::Down, KeymapMode::Normal, {'j'}, "");
    keymaps.map_key(Key::Up, KeymapMode::Normal, {'k'}, "");

    keymaps.map_key(shift(Key::Left), KeymapMode::Normal, {'H'}, "");
    keymaps.map_key(shift(Key::Right), KeymapMode::Normal, {'L'}, "");
    keymaps.map_key(shift(Key::Down), KeymapMode::Normal, {'J'}, "");
    keymaps.map_key(shift(Key::Up), KeymapMode::Normal, {'K'}, "");

    keymaps.map_key(Key::End, KeymapMode::Normal, {alt('l')}, "");
    keymaps.map_key(Key::Home, KeymapMode::Normal, {alt('h')}, "");
    keymaps.map_key(shift(Key::End), KeymapMode::Normal, {alt('L')}, "");
    keymaps.map_key(shift(Key::Home), KeymapMode::Normal, {alt('H')}, "");
}

static void check_tabstop(const int& val)
{
    if (val < 1) throw runtime_error{"tabstop should be strictly positive"};
}

static void check_indentwidth(const int& val)
{
    if (val < 0) throw runtime_error{"indentwidth should be positive or zero"};
}

static void check_scrolloff(const DisplayCoord& so)
{
    if (so.line < 0 or so.column < 0)
        throw runtime_error{"scroll offset must be positive or zero"};
}

static void check_timeout(const int& timeout)
{
    if (timeout < 50)
        throw runtime_error{"the minimum acceptable timeout is 50 milliseconds"};
}

static void check_extra_word_chars(const Vector<Codepoint, MemoryDomain::Options>& extra_chars)
{
    if (any_of(extra_chars, is_blank))
        throw runtime_error{"blanks are not accepted for extra completion characters"};
}

static void check_matching_pairs(const Vector<Codepoint, MemoryDomain::Options>& pairs)
{
    if ((pairs.size() % 2) != 0)
        throw runtime_error{"matching pairs should have a pair number of element"};
    if (not all_of(pairs, [](Codepoint cp) { return is_punctuation(cp); }))
        throw runtime_error{"matching pairs can only be punctuation"};
}

void register_options()
{
    OptionsRegistry& reg = GlobalScope::instance().option_registry();

    reg.declare_option<int, check_tabstop>("tabstop", "size of a tab character", 8);
    reg.declare_option<int, check_indentwidth>("indentwidth", "indentation width", 4);
    reg.declare_option<DisplayCoord, check_scrolloff>(
        "scrolloff", "number of lines and columns to keep visible main cursor when scrolling",
        {0,0});
    reg.declare_option("eolformat", "end of line format", EolFormat::Lf);
    reg.declare_option("BOM", "byte order mark to use when writing buffer",
                       ByteOrderMark::None);
    reg.declare_option("incsearch",
                       "incrementally apply search/select/split regex",
                       true);
    reg.declare_option("autoinfo",
                       "automatically display contextual help",
                       AutoInfo::Command | AutoInfo::OnKey);
    reg.declare_option("autocomplete",
                       "automatically display possible completions",
                       AutoComplete::Insert | AutoComplete::Prompt);
    reg.declare_option("aligntab",
                       "use tab characters when possible for alignment",
                       false);
    reg.declare_option("ignored_files",
                       "patterns to ignore when completing filenames",
                       Regex{R"(^(\..*|.*\.(o|so|a))$)"});
    reg.declare_option("disabled_hooks",
                      "patterns to disable hooks whose group is matched",
                      Regex{});
    reg.declare_option("filetype", "buffer filetype", ""_str);
    reg.declare_option("path", "path to consider when trying to find a file",
                   Vector<String, MemoryDomain::Options>({ "./", "%/", "/usr/include" }));
    reg.declare_option("completers", "insert mode completers to execute.",
                       InsertCompleterDescList({
                           InsertCompleterDesc{ InsertCompleterDesc::Filename, {} },
                           InsertCompleterDesc{ InsertCompleterDesc::Word, "all"_str }
                       }), OptionFlags::None);
    reg.declare_option("static_words", "list of words to always consider for insert word completion",
                   Vector<String, MemoryDomain::Options>{});
    reg.declare_option("autoreload",
                       "autoreload buffer when a filesystem modification is detected",
                       Autoreload::Ask);
    reg.declare_option("writemethod",
                       "how to write buffer to files",
                       WriteMethod::Overwrite);
    reg.declare_option<int, check_timeout>(
        "idle_timeout", "timeout, in milliseconds, before idle hooks are triggered", 50);
    reg.declare_option<int, check_timeout>(
        "fs_check_timeout", "timeout, in milliseconds, between file system buffer modification checks",
        500);
    reg.declare_option("ui_options",
                       "space separated list of <key>=<value> options that are "
                       "passed to and interpreted by the user interface\n"
                       "\n"
                       "The terminal ui supports the following options:\n"
                       "    <key>:                        <value>:\n"
                       "    terminal_assistant             clippy|cat|dilbert|none|off\n"
                       "    terminal_status_on_top         bool\n"
                       "    terminal_set_title             bool\n"
                       "    terminal_title                 str\n"
                       "    terminal_enable_mouse          bool\n"
                       "    terminal_synchronized          bool\n"
                       "    terminal_wheel_scroll_amount   int\n"
                       "    terminal_shift_function_key    int\n"
                       "    terminal_padding_char          codepoint\n"
                       "    terminal_padding_fill          bool\n"
                       "    terminal_cursor_native         bool\n"
                       "    terminal_info_max_width        int\n",
                       UserInterface::Options{});
    reg.declare_option("modelinefmt", "format string used to generate the modeline",
                       "%val{bufname} %val{cursor_line}:%val{cursor_char_column} {{context_info}} {{mode_info}} - %val{client}@[%val{session}]"_str);

    reg.declare_option("debug", "various debug flags", DebugFlags::None);
    reg.declare_option("readonly", "prevent buffers from being modified", false);
    reg.declare_option<Vector<Codepoint, MemoryDomain::Options>, check_extra_word_chars>(
        "extra_word_chars",
        "Additional characters to be considered as words for insert completion",
        { '_' });
    reg.declare_option<Vector<Codepoint, MemoryDomain::Options>, check_matching_pairs>(
        "matching_pairs",
        "set of pair of characters to be considered as matching pairs",
        { '(', ')', '{', '}', '[', ']', '<', '>' });
    reg.declare_option<int>("startup_info_version", "version up to which startup info changes should be hidden", 0);
}

static Client* local_client = nullptr;
static bool convert_to_client_pending = false;

enum class UIType
{
    Terminal,
    Json,
    Dummy,
};

UIType parse_ui_type(StringView ui_name)
{
    if (ui_name == "terminal") return UIType::Terminal;
    if (ui_name == "json") return UIType::Json;
    if (ui_name == "dummy") return UIType::Dummy;

    throw parameter_error(format("error: unknown ui type: '{}'", ui_name));
}

std::unique_ptr<UserInterface> make_ui(UIType ui_type)
{
    struct DummyUI : UserInterface
    {
        DummyUI() { set_signal_handler(SIGINT, SIG_DFL); }
        bool is_ok() const override { return true; }
        void menu_show(ConstArrayView<DisplayLine>, DisplayCoord,
                       Face, Face, MenuStyle) override {}
        void menu_select(int) override {}
        void menu_hide() override {}

        void info_show(const DisplayLine&, const DisplayLineList&, DisplayCoord, Face, InfoStyle) override {}
        void info_hide() override {}

        void draw(const DisplayBuffer&, const Face&, const Face&) override {}
        void draw_status(const DisplayLine&, const DisplayLine&, const Face&) override {}
        DisplayCoord dimensions() override { return {24,80}; }
        void set_cursor(CursorMode, DisplayCoord) override {}
        void refresh(bool) override {}
        void set_on_key(OnKeyCallback) override {}
        void set_on_paste(OnPasteCallback) override {}
        void set_ui_options(const Options&) override {}
    };

    switch (ui_type)
    {
        case UIType::Terminal: return std::make_unique<TerminalUI>();
        case UIType::Json: return std::make_unique<JsonUI>();
        case UIType::Dummy: return std::make_unique<DummyUI>();
    }
    throw logic_error{};
}

pid_t fork_server_to_background()
{
    if (pid_t pid = fork())
        return pid;

    setsid();
    if (fork()) // double fork to orphan the server
        exit(0);

    write_stderr(format("Kakoune forked server to background ({}), for session '{}'\n",
                        getpid(), Server::instance().session()));
    return 0;
}

std::unique_ptr<UserInterface> create_local_ui(UIType ui_type)
{
    auto ui = make_ui(ui_type);

    static SignalHandler old_handler = set_signal_handler(SIGTSTP, [](int sig) {
        if (ClientManager::instance().count() == 1 and
            *ClientManager::instance().begin() == local_client and
            not Server::instance().is_daemon())
            old_handler(sig);
        else
        {
            convert_to_client_pending = true;
            set_signal_handler(SIGTSTP, old_handler);
        }
    });
    return ui;
}

int run_client(StringView session, StringView name, StringView client_init,
               Optional<BufferCoord> init_coord, UIType ui_type,
               bool suspend)
{
    try
    {
        Optional<int> stdin_fd;
        // json-ui (or dummy) is not intended to be user interactive.
        // So only worry about making the tty your stdin if:
        // (a) ui_type is Terminal, *and*
        // (b) fd 0 is not interactive.
        if (ui_type == UIType::Terminal && not isatty(0))
        {
            // move stdin to another fd, and restore tty as stdin
            stdin_fd = dup(0);
            int tty = open("/dev/tty", O_RDONLY);
            dup2(tty, 0);
            close(tty);
        }

        EventManager event_manager;
        RemoteClient client{session, name, make_ui(ui_type), getpid(), get_env_vars(),
                            client_init, std::move(init_coord), stdin_fd};
        stdin_fd.map(close);

        if (suspend)
            raise(SIGTSTP);
        while (not client.exit_status() and client.is_ui_ok())
            event_manager.handle_next_events(EventMode::Normal);
        return client.exit_status().value_or(-1);
    }
    catch (disconnected& e)
    {
        write_stderr(format("{}\ndisconnecting\n", e.what()));
        return -1;
    }
}

struct convert_to_client_mode
{
    String session;
    String client_name;
    String buffer_name;
    String selections;
};

enum class ServerFlags
{
    None        = 0,
    IgnoreKakrc = 1 << 0,
    Daemon      = 1 << 1,
    ReadOnly    = 1 << 2,
    StartupInfo = 1 << 3,
};
constexpr bool with_bit_ops(Meta::Type<ServerFlags>) { return true; }

int run_server(StringView session, StringView server_init,
               StringView client_init, StringView init_buffer, Optional<BufferCoord> init_coord,
               ServerFlags flags, UIType ui_type, DebugFlags debug_flags,
               ConstArrayView<StringView> files)
{
    static bool terminate = false;
    set_signal_handler(SIGTERM, [](int) { terminate = true; });
    set_signal_handler(SIGINT, [](int) { terminate = true; });
    if ((flags & ServerFlags::Daemon) and session.empty())
    {
        write_stderr("-d needs a session name to be specified with -s\n");
        return -1;
    }

    EventManager        event_manager;
    Server              server{session.empty() ? to_string(getpid()) : session.str(),
                               (bool)(flags & ServerFlags::Daemon)};

    StringRegistry      string_registry;
    GlobalScope         global_scope;
    ShellManager        shell_manager{builtin_env_vars};
    CommandManager      command_manager;
    RegisterManager     register_manager;
    HighlighterRegistry highlighter_registry;
    SharedHighlighters  defined_highlighters;
    ClientManager       client_manager;
    BufferManager       buffer_manager;

    register_options();
    register_registers();
    register_keymaps();
    register_commands();
    register_highlighters();

    global_scope.options()["debug"].set(debug_flags);

    write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***");

#ifdef KAK_DEBUG
    {
        ProfileScope profile{debug_flags, [&](std::chrono::microseconds duration) {
            write_to_debug_buffer(format("running the unit tests took {} ms", duration.count()));
        }};
        UnitTest::run_all_tests();
    }
#endif

    bool startup_error = false;
    if (not (flags & ServerFlags::IgnoreKakrc)) try
    {
        Context init_context{Context::EmptyContextFlag{}};
        command_manager.execute(format("source {}/kakrc", runtime_directory()),
                                init_context);
    }
    catch (runtime_error& error)
    {
        startup_error = true;
        write_to_debug_buffer(format("error while parsing kakrc:\n"
                                     "    {}", error.what()));
    }

    {
        Context empty_context{Context::EmptyContextFlag{}};
        global_scope.hooks().run_hook(Hook::EnterDirectory, real_path("."), empty_context);
        global_scope.hooks().run_hook(Hook::KakBegin, session, empty_context);
    }

    if (not server_init.empty()) try
    {
        Context init_context{Context::EmptyContextFlag{}};
        command_manager.execute(server_init, init_context);
    }
    catch (const kill_session& kill)
    {
        Context empty_context{Context::EmptyContextFlag{}};
        global_scope.hooks().run_hook(Hook::KakEnd, "", empty_context);
        return kill.exit_status;
    }
    catch (runtime_error& error)
    {
        startup_error = true;
        write_to_debug_buffer(format("error while running server init commands:\n"
                                     "    {}", error.what()));
    }

    if (not files.empty()) try
    {
        for (auto& file : files)
        {
            try
            {
                Buffer *buffer = open_or_create_file_buffer(file);
                if (flags & ServerFlags::ReadOnly)
                {
                    buffer->flags() |= Buffer::Flags::ReadOnly;
                    buffer->options().get_local_option("readonly").set(true);
                }
            }
            catch (runtime_error& error)
            {
                startup_error = true;
                write_to_debug_buffer(format("error while opening file '{}':\n"
                                             "    {}", file, error.what()));
            }
        }
    }
    catch (runtime_error& error)
    {
         write_to_debug_buffer(format("error while opening command line files: {}", error.what()));
    }

    int exit_status = 0;
    try
    {
        if (ui_type == UIType::Terminal and not isatty(0))
        {
            // move stdin to another fd, and restore tty as stdin
            int fd = dup(0);
            int tty = open("/dev/tty", O_RDONLY);
            dup2(tty, 0);
            close(tty);
            create_fifo_buffer("*stdin*", fd, Buffer::Flags::None, AutoScroll::NotInitially);
        }

        if (not server.is_daemon())
        {
            local_client = client_manager.create_client(
                 create_local_ui(ui_type), getpid(), {}, get_env_vars(), client_init, init_buffer, std::move(init_coord),
                 [&](int status) { exit_status = status; });

            if (startup_error and local_client)
                local_client->print_status({
                    "error during startup, see `:buffer *debug*` for details",
                    local_client->context().faces()["Error"]
                });

            if (flags & ServerFlags::StartupInfo and local_client)
                show_startup_info(local_client, global_scope.options()["startup_info_version"].get<int>());
        }

        while (not terminate and
               (not client_manager.empty() or server.negotiating() or server.is_daemon()))
        {
            client_manager.redraw_clients();

            // Loop so that eventual inputs happening during the processing are handled as
            // well, avoiding unneeded redraws.
            Optional<std::chrono::nanoseconds> timeout;
            if (client_manager.has_pending_inputs())
                timeout = std::chrono::nanoseconds{};
            try
            {
                while (event_manager.handle_next_events(EventMode::Normal, nullptr, timeout))
                {
                    if (client_manager.process_pending_inputs())
                        break;
                    timeout = std::chrono::nanoseconds{};
                }
            }
            catch (const cancel&) {}

            client_manager.process_pending_inputs();

            client_manager.clear_client_trash();
            client_manager.clear_window_trash();
            buffer_manager.clear_buffer_trash();
            global_scope.option_registry().clear_option_trash();

            if (local_client and not contains(client_manager, local_client))
            {
                local_client = nullptr;
                if ((not client_manager.empty() or server.is_daemon()) and fork_server_to_background())
                    exit(exit_status); // We do not want to run destructors and hooks here
            }
            else if (convert_to_client_pending)
            {
                kak_assert(local_client);
                auto& local_context = local_client->context();
                String client_name = local_context.name();
                String buffer_name = local_context.buffer().name();
                String selections = selection_list_to_string(ColumnType::Byte, local_context.selections());

                ClientManager::instance().remove_client(*local_client, true, 0);
                client_manager.clear_client_trash();
                local_client = nullptr;
                convert_to_client_pending = false;

                if (fork_server_to_background())
                {
                    ClientManager::instance().clear(false);
                    String session = server.session();
                    server.close_session(false);
                    throw convert_to_client_mode{ std::move(session), std::move(client_name), std::move(buffer_name), std::move(selections) };
                }
            }
        }
    }
    catch (const kill_session& kill)
    {
        exit_status = kill.exit_status;
    }

    {
        Context empty_context{Context::EmptyContextFlag{}};
        global_scope.hooks().run_hook(Hook::KakEnd, "", empty_context);
    }

    return exit_status;
}

int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet, StringView suffix_backup)
{
    StringRegistry  string_registry;
    GlobalScope     global_scope;
    EventManager    event_manager;
    ShellManager    shell_manager{builtin_env_vars};
    RegisterManager register_manager;
    BufferManager   buffer_manager;

    register_options();
    register_registers();

    try
    {
        auto keys = parse_keys(keystr);

        auto apply_to_buffer = [&](Buffer& buffer)
        {
            try
            {
                InputHandler input_handler{
                    { buffer, Selection{{0,0}, buffer.back_coord()} },
                    Context::Flags::Draft
                };

                for (auto& key : keys)
                    input_handler.handle_key(key);
            }
            catch (runtime_error& err)
            {
                if (not quiet)
                    write_stderr(format("error while applying keys to buffer '{}': {}\n",
                                        buffer.display_name(), err.what()));
            }
        };

        for (auto& file : files)
        {
            Buffer* buffer = open_file_buffer(file, Buffer::Flags::NoHooks);
            if (not suffix_backup.empty())
                write_buffer_to_file(*buffer, buffer->name() + suffix_backup,
                                     WriteMethod::Overwrite, WriteFlags::None);
            apply_to_buffer(*buffer);
            write_buffer_to_file(*buffer, buffer->name(),
                                 WriteMethod::Overwrite, WriteFlags::None);
            buffer_manager.delete_buffer(*buffer);
        }
        if (not isatty(0))
        {
            Buffer& buffer = *create_buffer_from_string(
                "*stdin*", Buffer::Flags::NoHooks, read_fd(0));
            apply_to_buffer(buffer);
            write_buffer_to_fd(buffer, 1);
            buffer_manager.delete_buffer(buffer);
        }
    }
    catch (runtime_error& err)
    {
        write_stderr(format("error: {}\n", err.what()));
    }

    buffer_manager.clear_buffer_trash();
    return 0;
}

int run_pipe(StringView session)
{
    try
    {
        send_command(session, read_fd(0));
    }
    catch (disconnected& e)
    {
        write_stderr(format("{}\ndisconnecting\n", e.what()));
        return -1;
    }
    return 0;
}

void signal_handler(int signal)
{
    TerminalUI::restore_terminal();
    const char* text = nullptr;
    switch (signal)
    {
        case SIGSEGV: text = "SIGSEGV"; break;
        case SIGFPE:  text = "SIGFPE";  break;
        case SIGQUIT: text = "SIGQUIT"; break;
        case SIGPIPE: text = "SIGPIPE"; break;
    }
    auto msg = format("Received {}, exiting.\nPid: {}\nCallstack:\n{}",
                      text, getpid(), Backtrace{}.desc());
    write_stderr(msg);
    notify_fatal_error(msg);

    if (Server::has_instance())
        Server::instance().close_session();
    if (BufferManager::has_instance())
        BufferManager::instance().backup_modified_buffers();

    if (signal == SIGSEGV)
    {
        // generate core dump
        ::signal(SIGSEGV, SIG_DFL);
        ::kill(getpid(), SIGSEGV);
    }
    else
        abort();
}

}

int main(int argc, char* argv[])
{
    using namespace Kakoune;

    setlocale(LC_ALL, "");

    set_signal_handler(SIGSEGV, signal_handler);
    set_signal_handler(SIGFPE,  signal_handler);
    set_signal_handler(SIGQUIT, signal_handler);
    set_signal_handler(SIGTERM, signal_handler);
    set_signal_handler(SIGPIPE, [](int){});
    set_signal_handler(SIGINT, [](int){});
    set_signal_handler(SIGCHLD, [](int){});
    set_signal_handler(SIGTTOU, SIG_IGN);

    const ParameterDesc param_desc{
        SwitchMap{ { "c", { ArgCompleter{},  "connect to given session" } },
                   { "e", { ArgCompleter{},  "execute argument on client initialisation" } },
                   { "E", { ArgCompleter{},  "execute argument on server initialisation" } },
                   { "n", { {}, "do not source kakrc files on startup" } },
                   { "s", { ArgCompleter{},  "set session name" } },
                   { "d", { {}, "run as a headless session (requires -s)" } },
                   { "p", { ArgCompleter{},  "just send stdin as commands to the given session" } },
                   { "f", { ArgCompleter{},  "filter: for each file, select the entire buffer and execute the given keys" } },
                   { "i", { ArgCompleter{}, "backup the files on which a filter is applied using the given suffix" } },
                   { "q", { {}, "in filter mode, be quiet about errors applying keys" } },
                   { "ui", { ArgCompleter{}, "set the type of user interface to use (terminal, dummy, or json)" } },
                   { "l", { {}, "list existing sessions" } },
                   { "clear", { {}, "clear dead sessions" } },
                   { "debug", { ArgCompleter{}, "initial debug option value" } },
                   { "version", { {}, "display kakoune version and exit" } },
                   { "ro", { {}, "readonly mode" } },
                   { "help", { {}, "display a help message and quit" } } }
    };

    try
    {
        auto show_usage = [&]() {
            write_stdout(format("Usage: {} [options] [file]... [+<line>[:<col>]|+:]\n\n"
                    "Options:\n"
                    "{}\n"
                    "Prefixing a positional argument with a plus (`+`) sign will place the\n"
                    "cursor at a given set of coordinates, or the end of the buffer if the plus\n"
                    "sign is followed only by a colon (`:`)\n",
                    argv[0], generate_switches_doc(param_desc.switches)));
            return 0;
        };

        const auto params = ArrayView<char*>{argv+1, argv + argc}
                          | transform([](auto* s) { return String{s}; })
                          | gather<Vector<String>>();

        if (contains(params, "--help"_sv))
            return show_usage();

        ParametersParser parser{params, param_desc};

        const bool show_help_message = (bool)parser.get_switch("help");
        if (show_help_message)
            return show_usage();

        if (parser.get_switch("version"))
        {
            write_stdout(format("Kakoune {}\n", Kakoune::version));
            return 0;
        }

        const bool list_sessions = (bool)parser.get_switch("l");
        const bool clear_sessions = (bool)parser.get_switch("clear");
        if (list_sessions or clear_sessions)
        {
            list_files(session_directory(), [&](StringView session, auto&) {
                if (session.substr(0_byte, 1_byte) == ".")
                    return;

                const bool valid = check_session(session);
                if (list_sessions)
                    write_stdout(format("{}{}\n", session, valid ? "" : " (dead)"));
                if (not valid and clear_sessions)
                    unlink(session_path(session).c_str());
            });
            return 0;
        }

        if (auto session = parser.get_switch("p"))
        {
            for (auto opt : { "c", "n", "s", "d", "e", "E", "ro" })
            {
                if (parser.get_switch(opt))
                {
                    write_stderr(format("error: -{} is incompatible with -p\n", opt));
                    return -1;
                }
            }
            return run_pipe(*session);
        }

        auto client_init = parser.get_switch("e").value_or(StringView{});
        auto server_init = parser.get_switch("E").value_or(StringView{});
        const UIType ui_type = parse_ui_type(parser.get_switch("ui").value_or("terminal"));

        if (auto keys = parser.get_switch("f"))
        {
            if (parser.get_switch("ro"))
            {
                write_stderr("error: -ro is incompatible with -f\n");
                return -1;
            }

            Vector<StringView> files;
            for (size_t i = 0; i < parser.positional_count(); ++i)
                files.emplace_back(parser[i]);

            return run_filter(*keys, files, (bool)parser.get_switch("q"),
                              parser.get_switch("i").value_or(StringView{}));
        }

        Vector<StringView> files;
        Optional<BufferCoord> init_coord;
        for (auto& name : parser)
        {
            if (not name.empty() and name[0_byte] == '+')
            {
                if (name == "+" or name  == "+:")
                {
                    client_init = client_init + "; exec gj";
                    continue;
                }
                auto colon = find(name, ':');
                if (auto line = str_to_int_ifp({name.begin()+1, colon}))
                {
                    init_coord = std::max<BufferCoord>({0,0}, {
                        *line - 1,
                        colon != name.end() ?
                            str_to_int_ifp({colon+1, name.end()}).value_or(1) - 1
                          : 0
                    });
                    continue;
                }
            }

            files.emplace_back(name);
        }

        if (auto server_session = parser.get_switch("c"))
        {
            for (auto opt : { "n", "s", "d", "E", "ro" })
            {
                if (parser.get_switch(opt))
                {
                    write_stderr(format("error: -{} is incompatible with -c\n", opt));
                    return -1;
                }
            }
            String new_files;
            for (auto name : files) {
                new_files += format("edit '{}'", escape(real_path(name), "'", '\''));
                if (init_coord) {
                    new_files += format(" {} {}", init_coord->line + 1, init_coord->column + 1);
                    init_coord.reset();
                }
                new_files += ";";
            }

            return run_client(*server_session, {}, new_files + client_init, init_coord, ui_type, false);
        }
        else
        {
            StringView session = parser.get_switch("s").value_or(StringView{});
            try
            {
                auto ignore_kakrc = (bool)parser.get_switch("n");
                auto flags = (ignore_kakrc                                ? ServerFlags::IgnoreKakrc : ServerFlags::None) |
                             (parser.get_switch("d")                      ? ServerFlags::Daemon      : ServerFlags::None) |
                             (parser.get_switch("ro")                     ? ServerFlags::ReadOnly    : ServerFlags::None) |
                             ((argc == 1 or (ignore_kakrc and argc == 2))
                              and isatty(0)                               ? ServerFlags::StartupInfo : ServerFlags::None);
                auto debug_flags = option_from_string(Meta::Type<DebugFlags>{}, parser.get_switch("debug").value_or(""));
                return run_server(session, server_init, client_init, files.empty() ? StringView{} : files[0], init_coord, flags, ui_type, debug_flags, files);
            }
            catch (convert_to_client_mode& convert)
            {
                return run_client(convert.session, convert.client_name,
                                  format("try %^buffer '{}'; select '{}'^; echo converted to client only mode",
                                         escape(convert.buffer_name, "'^", '\\'), convert.selections), {}, ui_type, true);
            }
        }
    }
    catch (parameter_error& error)
    {
        write_stderr(format("Error while parsing parameters: {}\n"
                            "Valid switches:\n"
                            "{}", error.what(),
                            generate_switches_doc(param_desc.switches)));
       return -1;
    }
    catch (Kakoune::exception& error)
    {
        write_stderr(format("Fatal error: {}\n", error.what()));
        return -1;
    }
    catch (std::exception& error)
    {
        write_stderr(format("uncaught exception ({}):\n{}\n", typeid(error).name(), error.what()));
        return -1;
    }
    catch (...)
    {
        write_stderr("uncaught exception");
        return -1;
    }
    return 0;
}

#if defined(__ELF__)
#ifdef __arm__
# define PROGBITS "%progbits"
#else
# define PROGBITS "@progbits"
#endif
asm(".pushsection \".debug_gdb_scripts\", \"MS\"," PROGBITS ",1" R"(
.byte 4
.ascii "kakoune-inline-gdb.py\n"
.ascii "import os.path\n"
.ascii "sys.path.insert(0, os.path.dirname(gdb.current_objfile().filename) + '/../share/kak/gdb/')\n"
.ascii "import gdb.printing\n"
.ascii "import kakoune\n"
.ascii "gdb.printing.register_pretty_printer(gdb.current_objfile(), kakoune.build_pretty_printer())\n\0"
.popsection
)");
#endif