-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode-emacs.c
3111 lines (2757 loc) · 75.6 KB
/
mode-emacs.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 Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Define some keystrokes to create an editor with an
* "emacs" feel.
*
*/
#define _GNU_SOURCE /* for asprintf */
#include <unistd.h>
#include <stdlib.h>
#include <wchar.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <wctype.h>
#include <stdio.h>
#define PANE_DATA_PTR_TYPE char *
#include "core.h"
#include "core-pane.h"
static const char * safe file_normalize(struct pane *p safe, const char *path,
const char *initial_path);
/* num2 is used to track if successive commands are related.
* Only low 16 bits identify command, other bits are free.
*/
enum {
N2_zero = 0,
N2_undo_insert, /* adjacent commands form a single undo set */
N2_undo_delete, /* adjacent commands form a single undo set */
N2_undo_change, /* adjacent commands form a single undo set */
N2_recentre, /* repeated recentre goes to different places */
N2_yank, /* repeated yank-pop takes different result */
N2_match, /* repeated ` after Cx` repeats the search */
N2_undo, /* Last command was 'undo' too */
N2_close_others,/* Last command was close-other, just a 1 can repeat */
N2_runmacro, /* Last command was CX-e, just an 'e' can repeat */
N2_shift, /* Last command was CX-< or CX-> */
N2_growx, /* Last command was CX-{ or CX-} */
N2_uniquote, /* Last command was :C-q inserting a unicode from name */
};
static inline int N2(const struct cmd_info *ci safe)
{
return ci->num2 & 0xffff;
}
static inline int N2a(const struct cmd_info *ci safe)
{
return ci->num2 >> 16;
}
REDEF_CMD(emacs_move);
REDEF_CMD(emacs_delete);
REDEF_CMD(emacs_kill);
REDEF_CMD(emacs_case);
REDEF_CMD(emacs_swap);
static struct move_command {
struct command cmd;
char *type safe;
int direction, extra;
char *k1 safe, *k2, *k3;
} move_commands[] = {
{CMD(emacs_move), "Move-Char", 1, 0,
"K:C-F", "K:Right", NULL},
{CMD(emacs_move), "Move-Char", -1, 0,
"K:C-B", "K:Left", NULL},
{CMD(emacs_move), "doc:word", 1, 0,
"K:A-f", "K:A:Right", NULL},
{CMD(emacs_move), "doc:word", -1, 0,
"K:A-b", "K:A:Left", NULL},
{CMD(emacs_move), "doc:expr", 1, 0,
"K:A:C-F", NULL, NULL},
{CMD(emacs_move), "doc:expr", -1, 0,
"K:A:C-B", NULL, NULL},
{CMD(emacs_move), "doc:expr", -1, 1,
"K:A:C-U", NULL, NULL},
{CMD(emacs_move), "doc:expr", 1, 1,
"K:A:C-D", NULL, NULL},
{CMD(emacs_move), "doc:WORD", 1, 0,
"K:A-F", NULL, NULL},
{CMD(emacs_move), "doc:WORD", -1, 0,
"K:A-B", NULL, NULL},
{CMD(emacs_move), "doc:EOL", 1, 0,
"K:C-E", "K:End", NULL},
{CMD(emacs_move), "doc:EOL", -1, 0,
"K:C-A", "K:Home", NULL},
{CMD(emacs_move), "Move-Line", -1, 0,
"K:C-P", "K:Up", NULL},
{CMD(emacs_move), "Move-Line", 1, 0,
"K:C-N", "K:Down", NULL},
{CMD(emacs_move), "doc:file", 1, 0,
"K:A->", "K:S:End", NULL},
{CMD(emacs_move), "doc:file", -1, 0,
"K:A-<", "K:S:Home", NULL},
{CMD(emacs_move), "Move-View", 900, 0,
"K:Next", "K:C-V", "emacs-move-large-other"},
{CMD(emacs_move), "Move-View", -900, 0,
"K:Prior", "K:A-v", NULL},
{CMD(emacs_move), "doc:paragraph", -1, 0,
"K:A:C-A", NULL, NULL},
{CMD(emacs_move), "doc:paragraph", 1, 0,
"K:A:C-E", NULL, NULL},
{CMD(emacs_delete), "Move-Char", 1, 0,
"K:C-D", "K:Del", "del"},
{CMD(emacs_delete), "Move-Char", -1, 0,
"K:C-H", "K:Backspace", "K:Delete"},
{CMD(emacs_delete), "doc:word", 1, 0,
"K:A-d", NULL, NULL},
{CMD(emacs_delete), "doc:word", -1, 0,
"K:A:C-H", "K:A:Backspace", NULL},
{CMD(emacs_kill), "doc:EOL", 1, 0,
"K:C-K", NULL, NULL},
{CMD(emacs_kill), "doc:expr", 1, 0,
"K:A:C-K", NULL, NULL},
{CMD(emacs_case), "Ldoc:word", 1, 0,
"K:A-l", "K:A-L", NULL},
{CMD(emacs_case), "Udoc:word", 1, 0,
"K:A-u", "K:A-U", NULL},
{CMD(emacs_case), "Cdoc:word", 1, 0,
"K:A-c", "K:A-C", NULL},
{CMD(emacs_case), "TMove-Char", 1, 0,
"K:A-`", NULL, NULL},
{CMD(emacs_swap), "Move-Char", 1, 0,
"K:C-T", NULL, NULL},
{CMD(emacs_swap), "doc:word", 1, 0,
"K:A-t", NULL, NULL},
{CMD(emacs_swap), "doc:WORD", 1, 0,
"K:A-T", NULL, NULL},
};
REDEF_CMD(emacs_move)
{
struct move_command *mv = container_of(ci->comm, struct move_command, cmd);
struct pane *cursor_pane = ci->focus;
int ret = 0;
struct mark *mk;
if (!ci->mark)
return Enoarg;
/* if doc:file, leave inactive mark behind */
if (strcmp(mv->type, "doc:file") == 0) {
mk = call_ret(mark2, "doc:point", ci->focus);
if (mk)
/* Don't change selection:active */
mark_to_mark(mk, ci->mark);
else {
call("Move-to", ci->focus, 1, ci->mark);
mk = call_ret(mark2, "doc:point", ci->focus);
}
}
ret = call(mv->type, ci->focus, mv->direction * RPT_NUM(ci), ci->mark,
NULL, mv->extra);
if (ret < 0)
return ret;
if (strcmp(mv->type, "Move-View") == 0)
attr_set_int(&cursor_pane->attrs, "emacs-repoint",
mv->direction*2);
mk = call_ret(mark2, "doc:point", ci->focus);
/* Discard a transient selection */
call("selection:clear", ci->focus, 2, mk);
return 1;
}
REDEF_CMD(emacs_delete)
{
struct move_command *mv = container_of(ci->comm, struct move_command, cmd);
int ret;
struct mark *m, *mk;
if (!ci->mark)
return Enoarg;
mk = call_ret(mark2, "doc:point", ci->focus);
/* If selection is replacable, clear it and use mk */
if (call("selection:clear", ci->focus, 3, mk) == Efalse) {
/* else clear any transient selection */
call("selection:clear", ci->focus, 2, mk);
mk = NULL;
}
m = mark_dup(ci->mark);
ret = call(mv->type, ci->focus, mv->direction * RPT_NUM(ci), m);
if (ret < 0) {
mark_free(m);
return ret;
}
if (mk)
call("Replace", ci->focus, 1, mk, NULL, 0, mk);
ret = call("Replace", ci->focus, 1, m, NULL, N2(ci) == N2_undo_delete);
mark_free(m);
call("Mode:set-num2", ci->focus, N2_undo_delete);
return 1;
}
REDEF_CMD(emacs_kill)
{
/* Like delete, but copy to copy-buffer */
struct move_command *mv = container_of(ci->comm, struct move_command, cmd);
int ret;
struct mark *m;
char *str;
if (!ci->mark)
return Enoarg;
m = mark_dup(ci->mark);
if (strcmp(mv->type, "doc:EOL") == 0 &&
mv->direction == 1 && RPT_NUM(ci) == 1 &&
is_eol(doc_following(ci->focus, m)))
ret = call("Move-Char", ci->focus, mv->direction * RPT_NUM(ci), m);
else
ret = call(mv->type, ci->focus, mv->direction * RPT_NUM(ci), m);
if (ret < 0) {
mark_free(m);
return ret;
}
/* Remove any selection so it cannot be claimed and so replace this copy */
call("selection:claim", ci->focus);
call("selection:discard", ci->focus);
str = call_ret(strsave, "doc:get-str", ci->focus, 0, NULL, NULL, 0, m);
if (str && *str)
call("copy:save", ci->focus, N2(ci) == N2_undo_delete, NULL, str);
ret = call("Replace", ci->focus, 1, m, NULL, N2(ci) == N2_undo_delete);
mark_free(m);
call("Mode:set-num2", ci->focus, N2_undo_delete);
return 1;
}
REDEF_CMD(emacs_case)
{
struct move_command *mv = container_of(ci->comm, struct move_command, cmd);
int ret;
struct mark *start = NULL;
int cnt = mv->direction * RPT_NUM(ci);
int dir;
if (!ci->mark)
return Enoarg;
if (cnt == 0)
return 1;
if (cnt > 0) {
dir = 1;
} else {
dir = -1;
cnt = -cnt;
start = mark_dup(ci->mark);
}
while (cnt > 0) {
struct mark *m = mark_dup(ci->mark);
ret = call(mv->type+1, ci->focus, dir, ci->mark);
if (ret <= 0 || mark_same(ci->mark, m))
/* Hit end of file */
cnt = 1;
else {
char *str = call_ret(str, "doc:get-str", ci->focus,
0, ci->mark, NULL,
0, m);
char *c;
int changed = 0;
int found = 0;
for (c = str; c && *c; c += 1) {
char type = mv->type[0];
if (type == 'C') {
type = found ? 'L' : 'U';
if (isalpha(*c))
found = 1;
}
switch(type) {
default: /* silence compiler */
case 'U': /* Uppercase */
if (islower(*c)) {
changed = 1;
*c = toupper(*c);
}
break;
case 'L': /* Lowercase */
if (isupper(*c)) {
changed = 1;
*c = tolower(*c);
}
break;
case 'T': /* Toggle */
if (isupper(*c)) {
changed = 1;
*c = tolower(*c);
} else if (islower(*c)) {
changed = 1;
*c = toupper(*c);
}
break;
}
}
if (changed) {
ret = call("Replace", ci->focus, 1, m, str,
N2(ci) == N2_undo_change);
if (dir < 0)
call(mv->type+1, ci->focus, dir, ci->mark);
}
free(str);
if (changed || N2(ci) == N2_undo_change)
call("Mode:set-num2", ci->focus, N2_undo_change);
}
mark_free(m);
cnt -= 1;
}
/* When moving forward, move point. When backward, leave point alone */
if (start) {
mark_to_mark(ci->mark, start);
mark_free(start);
}
return 1;
}
REDEF_CMD(emacs_swap)
{
/* collect the object behind point and insert it after the object
* after point. With a +ve repeat count, insert after n objects.
* With -ve repeast, collect object after and insert behind n
* previous objects. Object is determined by mv->type.
*/
struct move_command *mv = container_of(ci->comm, struct move_command, cmd);
struct mark *start = NULL;
int cnt = mv->direction * RPT_NUM(ci);
int dir;
if (!ci->mark)
return Enoarg;
if (cnt == 0)
return 1;
if (cnt > 0) {
dir = 1;
} else {
dir = -1;
cnt = -cnt;
start = mark_dup(ci->mark);
}
while (cnt > 0) {
struct mark *as, *ae, *bs, *be;
char *astr, *bstr;
call(mv->type, ci->focus, -dir, ci->mark);
as = mark_dup(ci->mark);
call(mv->type, ci->focus, dir, ci->mark);
ae = mark_dup(ci->mark);
/* as to ae is the object to be moved. */
call(mv->type, ci->focus, dir, ci->mark);
be = mark_dup(ci->mark);
call(mv->type, ci->focus, -dir, ci->mark);
bs = mark_dup(ci->mark);
/* bs to be is the object to be swapped with.
* bs must be after ae in the direction
*/
if (mark_same(as, ae) ||
mark_same(bs, be) ||
bs->seq - ae->seq * dir < 0) {
mark_to_mark(ci->mark, ae);
mark_free(as);
mark_free(ae);
mark_free(bs);
mark_free(be);
break;
}
astr = call_ret(str, "doc:get-str", ci->focus,
0, as, NULL,
0, ae);
bstr = call_ret(str, "doc:get-str", ci->focus,
0, bs, NULL,
0, be);
mark_to_mark(ci->mark, ae);
call("Replace", ci->focus, 1, as, bstr);
mark_to_mark(ci->mark, be);
call("Replace", ci->focus, 1, bs, astr, 1);
if (dir < 0)
call(mv->type, ci->focus, dir, ci->mark);
free(astr);
free(bstr);
mark_free(as);
mark_free(ae);
mark_free(bs);
mark_free(be);
cnt -= 1;
}
/* When moving forward, move point. When backward, leave point alone */
if (start) {
mark_to_mark(ci->mark, start);
mark_free(start);
}
return 1;
}
DEF_CMD(emacs_move_view_other)
{
/* If there is an 'other' pane', Send "K:Next" there */
struct pane *p;
/* '512' means 'fail if no other pane' */
p = call_ret(pane, "OtherPane", ci->focus, 512);
if (!p)
return 1;
call("Mode:set-num", p, ci->num);
call("Keystroke", p, 0, NULL, "emacs-move-large-other");
return 1;
}
DEF_CMD(emacs_recenter)
{
int step = 0;
if (ci->num == NO_NUMERIC && N2(ci) == N2_recentre) {
/* Repeated command - go to top, or bottom, or middle in order */
switch (N2a(ci)) {
default:
case 0: /* was center, go to top */
call("Move-View-Line", ci->focus, 1, ci->mark);
step = 1;
break;
case 1: /* was top, go to bottom */
call("Move-View-Line", ci->focus, -1, ci->mark);
step = 2;
break;
case 2: /* was bottom, go to middle */
call("Move-View-Line", ci->focus, 0, ci->mark);
step = 0;
break;
}
} else if (ci->num == -NO_NUMERIC) {
/* Move point to bottom */
call("Move-View-Line", ci->focus, -1, ci->mark);
step = 2;
} else if (ci->num != NO_NUMERIC) {
/* Move point to display line N */
call("Move-View-Line", ci->focus, ci->num, ci->mark);
} else {
/* Move point to middle and refresh */
call("Move-View-Line", ci->focus, 0, ci->mark);
call("Window:refresh", ci->focus);
}
call("Mode:set-num2", ci->focus, N2_recentre | (step << 16));
return 1;
}
REDEF_CMD(emacs_simple);
REDEF_CMD(emacs_simple_num);
REDEF_CMD(emacs_simple_str);
static struct simple_command {
struct command cmd;
char *type safe;
char *k safe;
} simple_commands[] = {
{CMD(emacs_simple), "Tile:next", "K:CX-o"},
{CMD(emacs_simple), "Tile:prev", "K:CX-O"},
{CMD(emacs_simple), "Tile:y+", "K:CX-^"},
{CMD(emacs_simple), "Tile:split-y", "K:CX-2"},
{CMD(emacs_simple), "Tile:split-x", "K:CX-3"},
{CMD(emacs_simple), "Tile:close", "K:CX-0"},
{CMD(emacs_simple), "Tile:bury", "K:A-B"},
{CMD(emacs_simple), "Window:new", "K:CX5-2"},
{CMD(emacs_simple), "Window:close", "K:CX5-0"},
{CMD(emacs_simple), "lib-server:done", "K:CX-#"},
{CMD(emacs_simple), "mode-swap-mark", "K:CX:C-X"},
{CMD(emacs_simple), "Abort", "K:C-G"},
{CMD(emacs_simple), "Cancel", "K:ESC"},
{CMD(emacs_simple), "NOP", "K:A-G"},
{CMD(emacs_simple), "NOP", "K:CX:C-G"},
{CMD(emacs_simple), "NOP", "K:CX4:C-G"},
{CMD(emacs_simple), "doc:save-file", "K:CX:C-S"},
{CMD(emacs_simple), "Commit", "K:CC:C-C"},
/* one day, this will be "find definition", now it is same as "find any" */
{CMD(emacs_simple_num), "interactive-cmd-git-grep", "K:CX:A-."},
{CMD(emacs_simple_str), "interactive-cmd-git-grep", "K:A-."},
{CMD(emacs_simple), "interactive-cmd-merge-mode", "K:A-m"},
{CMD(emacs_simple_str), "interactive-cmd-calc-replace", "K:A-#"},
};
REDEF_CMD(emacs_simple)
{
struct simple_command *sc = container_of(ci->comm, struct simple_command, cmd);
if (!ci->mark)
return Enoarg;
return call(sc->type, ci->focus, ci->num, ci->mark);
}
REDEF_CMD(emacs_simple_num)
{
struct simple_command *sc = container_of(ci->comm, struct simple_command, cmd);
if (!ci->mark)
return Enoarg;
return call(sc->type, ci->focus, RPT_NUM(ci), ci->mark);
}
REDEF_CMD(emacs_simple_str)
{
struct simple_command *sc = container_of(ci->comm, struct simple_command, cmd);
struct mark *mk = call_ret(mark2, "doc:point", ci->focus);
char *str = NULL;
if (!ci->mark)
return Enoarg;
if (call("selection:clear", ci->focus, 0, mk) >= 1)
str = call_ret(strsave, "doc:get-str", ci->focus, 0, NULL, NULL, 0, mk);
return call(sc->type, ci->focus, ci->num, ci->mark, str, 0, mk);
}
DEF_CMD(emacs_close_others)
{
if (strcmp(ci->key, "K-1") == 0 && N2(ci) != N2_close_others)
return Efallthrough;
if (call("Tile:close-others", ci->focus) <= 0)
return Efalse;
call("Mode:set-num2", ci->focus, N2_close_others);
call("Message:modal", ci->focus, 0, NULL, "Type 1 to close more");
return 1;
}
DEF_CB(cnt_disp)
{
struct call_return *cr = container_of(ci->comm, struct call_return, c);
cr->i += 1;
return 1;
}
DEF_CMD(emacs_deactivate)
{
/* close-all popup has closed, see if it aborted */
if (ci->str)
call("event:deactivate", ci->focus);
return 1;
}
DEF_CMD(emacs_exit)
{
struct call_return cr;
cr.c = cnt_disp;
cr.i = 0;
if (ci->num == NO_NUMERIC) {
struct pane *p;
/* If this is not only display, then refuse to exit */
call_comm("editor:notify:all-displays", ci->focus, &cr.c);
if (cr.i > 1) {
call("Message", ci->focus, 0, NULL,
"Cannot exit when there are multiple windows.");
return 1;
}
p = call_ret(pane, "PopupTile", ci->focus, 0, NULL, "DM");
if (!p) {
/* Probably called from a pop-up. Abort the popup
* and let the user try again.
*/
call("Abort", ci->focus);
return Efail;
}
attr_set_str(&p->attrs, "done-key", "emacs:deactivate");
call("docs:show-modified", p);
return 1;
} else
call("event:deactivate", ci->focus);
return 1;
}
DEF_CMD(emacs_quote_insert)
{
int ret;
char buf[2] = ".";
const char *str;
bool first = N2(ci) != N2_undo_insert;
if (!ci->mark)
return Enoarg;
if (call("selection:clear", ci->focus, 3, ci->mark) >= 1) {
call("Replace", ci->focus, 1, ci->mark, NULL, !first);
first = False;
} else
call("selection:clear", ci->focus, 2, ci->mark);
str = ksuffix(ci, "K:CQ-");
if (!str[0]) {
str = ksuffix(ci, "K:CQ:C-");
if (str[0]) {
buf[0] = str[0] & 0x1f;
str = buf;
} else
str = "??";
}
ret = call("Replace", ci->focus, 1, ci->mark, str, !first);
call("Mode:set-num2", ci->focus, N2_undo_insert);
return ret < 0 ? ret : 1;
}
DEF_CMD(emacs_open_line)
{
if (call("Interactive:insert", ci->focus, 0, ci->mark, "\n") > 0)
call("Move-Char", ci->focus, -1, ci->mark);
return 1;
}
DEF_CMD(emacs_undo)
{
int ret;
ret = call("doc:reundo", ci->focus, N2(ci) == N2_undo);
call("Mode:set-num2", ci->focus, N2_undo);
if (ret == Efalse)
call("Message", ci->focus, 0, NULL,
"No further undo information");
return 1;
}
REDEF_CMD(emacs_file_complete);
REDEF_CMD(emacs_doc_complete);
REDEF_CMD(emacs_cmd_complete);
DEF_CMD(find_complete)
{
char *type = ci->home->data;
if (strstarts(type, "file"))
return emacs_file_complete_func(ci);
if (strcmp(type, "shellcmd") == 0)
return emacs_file_complete_func(ci);
if (strcmp(type, "doc") == 0)
return emacs_doc_complete_func(ci);
if (strcmp(type, "cmd") == 0)
return emacs_cmd_complete_func(ci);
return Efallthrough;
}
DEF_CMD(find_done)
{
int ret;
char *type = ci->home->data;
char *str = call_ret(strsave, "doc:get-str", ci->focus);
const char *norm = NULL;
struct stat stb;
if (!str || !str[0])
/* close with no string */
ret = call("popup:close", ci->focus);
if (strcmp(type, "doc") == 0 &&
call_ret(pane, "docs:byname", ci->focus, 0, NULL, str) == NULL) {
call("Message:modal", ci->focus, 0, NULL, "Document not found");
return 1;
}
if (strstarts(type, "file")) {
char *sl;
bool can_create = True;
bool can_create_dir = True;
norm = file_normalize(ci->focus, str,
attr_find(ci->home->attrs,
"initial_path"));
sl = strrchr(norm, '/');
while (sl && sl > norm) {
/* Need to check directories exist. */
*sl = 0;
stb.st_mode = 0;
stat(norm, &stb);
if ((stb.st_mode & S_IFMT) == S_IFDIR)
break;
if (stb.st_mode)
can_create_dir = False;
can_create = False;
sl = strrchr(norm, '/');
}
if (sl)
*sl = '/';
if (!can_create_dir) {
call("Message:modal", ci->focus, 0, NULL,
strconcat(ci->focus, norm, " is not a directory!!"));
return 1;
}
if (!can_create) {
/* Need to create directory first */
if (strcmp(ci->key, "K:A:Enter") == 0) {
char *m = "Cannot create directory: ";
if (mkdir(norm, 0777) == 0) {
m = "Created directory: ";
attr_set_str(&ci->home->attrs,
"prev_dir", NULL);
/* Trigger recalc on replace */
call("doc:replace", ci->focus, 0, NULL, "");
}
call("Message:modal", ci->focus, 0, NULL,
strconcat(ci->focus, m, norm));
return 1;
}
call("Message:modal", ci->focus, 0, NULL,
strconcat(ci->focus,
"Use Alt-Enter to create directory ", norm));
return 1;
}
}
if (strcmp(type, "file") == 0 && norm &&
strcmp(ci->key, "K:Enter") == 0 &&
stat(norm, &stb) != 0) {
call("Message:modal", ci->focus, 0, NULL,
"File not found - use Alt-Enter to create");
return 1;
}
if (strcmp(type, "file write") == 0 && norm &&
strcmp(ci->key, "K:Enter") == 0 &&
stat(norm, &stb) == 0) {
call("Message:modal", ci->focus, 0, NULL,
"File exists - use Alt-Enter to overwrite");
return 1;
}
ret = call("popup:close", ci->focus, 0, NULL, norm ?: str);
return ret < 0 ? ret : 1;
}
struct find_helper {
char *name;
int want_prev;
struct pane *ret;
struct command c;
};
DEF_CB(find_helper)
{
struct find_helper *h = container_of(ci->comm, struct find_helper, c);
struct pane *p = ci->focus;
char *name;
if (!p)
return 1;
if (!h->name) {
if (h->want_prev) {
/* want the pane before nothing, so the last.
* So return this one and keep looking.
*/
h->ret = p;
return 1;
} else {
/* Want the pane that is after nothing, so
* the first. This one. All done.
*/
h->ret = p;
return Efalse;
}
}
name = pane_attr_get(ci->focus, "doc-name");
if (!name)
return 1;
if (strcmp(name, h->name) == 0) {
if (h->want_prev) {
/* Want the previous one, which is
* already in ->ret
*/
return Efalse;
} else {
/* Want the next one, so clear name
* and keep going.
*/
h->name = NULL;
return 1;
}
} else {
if (h->want_prev) {
/* This might be what I want - keep it in case */
h->ret = p;
return 1;
} else {
/* Don't want this - just keep going */
return 1;
}
}
}
DEF_CMD(find_prevnext)
{
/* Find the previous document lru or, which is "next" as we
* walk the list in mru order.
* When we find it, insert the name into ci->focus document
*/
char *type = ci->home->data;
struct find_helper h;
if (strcmp(type, "doc") != 0)
return Efallthrough;
h.name = attr_find(ci->home->attrs, "find-doc");
h.ret = NULL;
h.c = find_helper;
h.want_prev = strcmp(ci->key, "K:A-n") == 0;
call_comm("docs:byeach", ci->focus, &h.c);
if (h.ret) {
char *name = pane_attr_get(h.ret, "doc-name");
struct mark *m, *m2;
attr_set_str(&ci->home->attrs, "find-doc", name);
m = mark_new(ci->focus);
m2 = m ? mark_dup(m) : NULL;
call("doc:file", ci->focus, -1, m);
call("doc:file", ci->focus, 1, m2);
call("Replace", ci->focus, 1, m, name, 0, m2);
mark_free(m);
mark_free(m2);
}
return 1;
}
DEF_CMD(find_attr)
{
char *type = ci->home->data;
if (!ci->str)
return Enoarg;
if (strcmp(type, "file") != 0)
return Efallthrough;
if (strcmp(ci->str, "start-of-line") == 0) {
char *lens = attr_find(ci->home->attrs, "path_lengths");
int dir_start = 0, dir_end = 0, nondir_end = 0,
basename_start = 0;
if (lens)
sscanf(lens, "%d %d %d %d", &dir_start, &dir_end,
&nondir_end, &basename_start);
if (dir_start > 0)
comm_call(ci->comm2, "cb", ci->focus, dir_start,
ci->mark, "fg:grey+20,nobold,noinverse", 115);
if (dir_end > dir_start)
comm_call(ci->comm2, "cb", ci->focus, dir_end,
ci->mark, "fg:black,nobold,noinverse", 114);
if (nondir_end > dir_end)
comm_call(ci->comm2, "cb", ci->focus, nondir_end,
ci->mark, "fg:red-80,bold,inverse", 113);
if (basename_start > nondir_end)
comm_call(ci->comm2, "cb", ci->focus, basename_start,
ci->mark, "fg:magenta", 112);
comm_call(ci->comm2, "cb", ci->focus, 10000, ci->mark,
"fg:black", 111);
}
return 1;
}
DEF_CMD(find_check_replace)
{
char *str, *cp, *sl;
char *type = ci->home->data;
char *initial_path;
char *prev_dir;
struct stat stb;
int ipl; // Initial Path Len
int dir_start, dir_end, nondir_end, basename_start;
char nbuf[4 * (10+1) + 1], *lens;
if (strcmp(type, "file") != 0)
return Efallthrough;
home_call(ci->home->parent, ci->key, ci->focus,
ci->num, ci->mark, ci->str,
ci->num2, ci->mark2, ci->str2);
/* The doc content can have 5 different sections that might
* be different colours.
* - ignored prefix: grey - This inital_path followed by something
* that looks like another path. "/" or "~/"
* - True directories: black - directory part of the path that
* exists and is a directory
* - non-directory-in-path: red - directory part that exists but
* is not a directory. At most one component.
* - non-existant name: magenta - directory path that doesn't exist.
* - basename: black, whether it exists or not.
* These are found as:
* - dir_start
* - dir_end
* - nondir_end
* - basename_start
* These are all lengths from start of path. They are all stored
* in a single attribute: "path_lengths".
*/
str = call_ret(str, "doc:get-str", ci->focus);
if (!str)
return 1;
sl = strrchr(str, '/');
if (!sl)
sl = str;
prev_dir = attr_find(ci->home->attrs, "prev_dir");
if (prev_dir && strlen(prev_dir) == (size_t)(sl - str + 1) &&
strncmp(prev_dir, str, sl - str + 1) == 0)
/* No change before last '/' */
return 1;
initial_path = attr_find(ci->home->attrs, "initial_path");
if (!initial_path)
return 1;
ipl = strlen(initial_path);
cp = str;
if (strncmp(str, initial_path, ipl) == 0 &&
(str[ipl] == '/' || (str[ipl] == '~' &&
(str[ipl+1] == '/' ||
str[ipl+1] == 0))))
cp = str + ipl;
dir_start = utf8_strnlen(str, cp - str);
basename_start = utf8_strnlen(str, sl - str + 1);
stb.st_mode = 0;
if (sl < cp)
sl = cp;
while (sl > cp) {
const char *path;
*sl = 0;
path = file_normalize(ci->focus, str, initial_path);
stat(path, &stb);
*sl = '/';
if (stb.st_mode)
break;
sl -= 1;
while (sl > cp && *sl != '/')
sl -= 1;
}
nondir_end = utf8_strnlen(str, sl - str + 1);
dir_end = nondir_end;
if (stb.st_mode != 0 &&
(stb.st_mode & S_IFMT) != S_IFDIR) {
/* There is a non-dir on the path */
while (sl > cp && sl[-1] != '/')
sl -= 1;
/* This must actually be a dir */
dir_end = utf8_strnlen(str, sl - str);
}
snprintf(nbuf, sizeof(nbuf), "%d %d %d %d",
dir_start, dir_end, nondir_end, basename_start);
lens = attr_find(ci->home->attrs, "path_lengths");
if (!lens || strcmp(lens, nbuf) != 0)
attr_set_str(&ci->home->attrs, "path_lengths", nbuf);
sl = strrchr(str, '/');
if (sl) {
sl[1] = 0;
attr_set_str(&ci->home->attrs, "prev_dir", str);
}
return 1;
}
static struct map *fh_map;
DEF_LOOKUP_CMD(find_handle, fh_map);
static void findmap_init(void)
{
if (fh_map)
return;
fh_map = key_alloc();
key_add(fh_map, "K:Tab", &find_complete);
key_add(fh_map, "K:Enter", &find_done);
key_add(fh_map, "K:A:Enter", &find_done);
key_add(fh_map, "K:A-p", &find_prevnext);
key_add(fh_map, "K:A-n", &find_prevnext);
key_add(fh_map, "map-attr", &find_attr);
key_add(fh_map, "doc:replace", &find_check_replace);
}
static const char * safe file_normalize(struct pane *p safe,
const char *path,
const char *initial_path)
{