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
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
|
// sign.c: functions for managing with signs
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "klib/kvec.h"
#include "nvim/api/extmark.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/ascii_defs.h"
#include "nvim/buffer.h"
#include "nvim/buffer_defs.h"
#include "nvim/charset.h"
#include "nvim/cmdexpand_defs.h"
#include "nvim/cursor.h"
#include "nvim/decoration.h"
#include "nvim/decoration_defs.h"
#include "nvim/drawscreen.h"
#include "nvim/edit.h"
#include "nvim/eval/funcs.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/typval_defs.h"
#include "nvim/ex_cmds_defs.h"
#include "nvim/ex_docmd.h"
#include "nvim/extmark.h"
#include "nvim/fold.h"
#include "nvim/gettext_defs.h"
#include "nvim/globals.h"
#include "nvim/grid.h"
#include "nvim/grid_defs.h"
#include "nvim/highlight.h"
#include "nvim/highlight_defs.h"
#include "nvim/highlight_group.h"
#include "nvim/macros_defs.h"
#include "nvim/map_defs.h"
#include "nvim/marktree.h"
#include "nvim/marktree_defs.h"
#include "nvim/mbyte.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/move.h"
#include "nvim/pos_defs.h"
#include "nvim/sign.h"
#include "nvim/sign_defs.h"
#include "nvim/strings.h"
#include "nvim/types_defs.h"
#include "nvim/vim_defs.h"
#include "nvim/window.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "sign.c.generated.h"
#endif
static PMap(cstr_t) sign_map = MAP_INIT;
static kvec_t(Integer) sign_ns = KV_INITIAL_VALUE;
static char *cmds[] = {
"define",
#define SIGNCMD_DEFINE 0
"undefine",
#define SIGNCMD_UNDEFINE 1
"list",
#define SIGNCMD_LIST 2
"place",
#define SIGNCMD_PLACE 3
"unplace",
#define SIGNCMD_UNPLACE 4
"jump",
#define SIGNCMD_JUMP 5
NULL
#define SIGNCMD_LAST 6
};
// Convert the supplied "group" to a namespace filter
static int64_t group_get_ns(const char *group)
{
if (group == NULL) {
return 0; // Global namespace
} else if (strcmp(group, "*") == 0) {
return UINT32_MAX; // All namespaces
}
// Specific or non-existing namespace
int ns = map_get(String, int)(&namespace_ids, cstr_as_string(group));
return ns ? ns : -1;
}
static const char *sign_get_name(DecorSignHighlight *sh)
{
char *name = sh->sign_name;
return !name ? "" : map_has(cstr_t, &sign_map, name) ? name : "[Deleted]";
}
/// Create or update a sign extmark.
///
/// @param buf buffer to store sign in
/// @param id sign ID
/// @param group sign group
/// @param prio sign priority
/// @param lnum line number which gets the mark
/// @param sp sign properties
static void buf_set_sign(buf_T *buf, uint32_t *id, char *group, int prio, linenr_T lnum, sign_T *sp)
{
if (group && !map_get(String, int)(&namespace_ids, cstr_as_string(group))) {
kv_push(sign_ns, nvim_create_namespace(cstr_as_string(group)));
}
uint32_t ns = group ? (uint32_t)nvim_create_namespace(cstr_as_string(group)) : 0;
DecorSignHighlight sign = DECOR_SIGN_HIGHLIGHT_INIT;
sign.flags |= kSHIsSign;
memcpy(sign.text, sp->sn_text, SIGN_WIDTH * sizeof(schar_T));
sign.sign_name = xstrdup(sp->sn_name);
sign.hl_id = sp->sn_text_hl;
sign.line_hl_id = sp->sn_line_hl;
sign.number_hl_id = sp->sn_num_hl;
sign.cursorline_hl_id = sp->sn_cul_hl;
sign.priority = (DecorPriority)prio;
bool has_hl = (sp->sn_line_hl || sp->sn_num_hl || sp->sn_cul_hl);
uint16_t decor_flags = (sp->sn_text[0] ? MT_FLAG_DECOR_SIGNTEXT : 0)
| (has_hl ? MT_FLAG_DECOR_SIGNHL : 0);
DecorInline decor = { .ext = true, .data.ext = { .vt = NULL, .sh_idx = decor_put_sh(sign) } };
extmark_set(buf, ns, id, lnum - 1, 0, -1, -1, decor, decor_flags, true,
false, true, true, false, NULL);
}
/// For an existing, placed sign with "id", modify the sign, group or priority.
/// Returns the line number of the sign, or zero if the sign is not found.
///
/// @param buf buffer to store sign in
/// @param id sign ID
/// @param group sign group
/// @param prio sign priority
/// @param sp sign pointer
static linenr_T buf_mod_sign(buf_T *buf, uint32_t *id, char *group, int prio, sign_T *sp)
{
int64_t ns = group_get_ns(group);
if (ns < 0 || (group && ns == 0)) {
return 0;
}
MTKey mark = marktree_lookup_ns(buf->b_marktree, (uint32_t)ns, *id, false, NULL);
if (mark.pos.row >= 0) {
buf_set_sign(buf, id, group, prio, mark.pos.row + 1, sp);
}
return mark.pos.row + 1;
}
/// Find the line number of the sign with the requested id in group 'group'. If
/// the sign does not exist, return 0 as the line number. This will still let
/// the correct file get loaded.
///
/// @param buf buffer to store sign in
/// @param id sign ID
/// @param group sign group
static int buf_findsign(buf_T *buf, int id, char *group)
{
int64_t ns = group_get_ns(group);
if (ns < 0 || (group && ns == 0)) {
return 0;
}
return marktree_lookup_ns(buf->b_marktree, (uint32_t)ns, (uint32_t)id, false, NULL).pos.row + 1;
}
/// qsort() function to sort signs by line number, priority, id and recency.
static int sign_row_cmp(const void *p1, const void *p2)
{
const MTKey *s1 = (MTKey *)p1;
const MTKey *s2 = (MTKey *)p2;
if (s1->pos.row != s2->pos.row) {
return s1->pos.row > s2->pos.row ? 1 : -1;
}
DecorSignHighlight *sh1 = decor_find_sign(mt_decor(*s1));
DecorSignHighlight *sh2 = decor_find_sign(mt_decor(*s2));
assert(sh1 && sh2);
SignItem si1 = { sh1, s1->id };
SignItem si2 = { sh2, s2->id };
return sign_item_cmp(&si1, &si2);
}
/// Delete the specified sign(s)
///
/// @param buf buffer sign is stored in or NULL for all buffers
/// @param group sign group
/// @param id sign id
/// @param atlnum single sign at this line, specified signs at any line when -1
static int buf_delete_signs(buf_T *buf, char *group, int id, linenr_T atlnum)
{
int64_t ns = group_get_ns(group);
if (ns < 0) {
return FAIL;
}
MarkTreeIter itr[1];
int row = atlnum > 0 ? atlnum - 1 : 0;
kvec_t(MTKey) signs = KV_INITIAL_VALUE;
// Store signs at a specific line number to remove one later.
if (atlnum > 0) {
if (!marktree_itr_get_overlap(buf->b_marktree, row, 0, itr)) {
return FAIL;
}
MTPair pair;
while (marktree_itr_step_overlap(buf->b_marktree, itr, &pair)) {
if ((ns == UINT32_MAX || ns == pair.start.ns) && mt_decor_sign(pair.start)) {
kv_push(signs, pair.start);
}
}
} else {
marktree_itr_get(buf->b_marktree, 0, 0, itr);
}
while (itr->x) {
MTKey mark = marktree_itr_current(itr);
if (row && mark.pos.row > row) {
break;
}
if (!mt_end(mark) && mt_decor_sign(mark)
&& (id == 0 || (int)mark.id == id)
&& (ns == UINT32_MAX || ns == mark.ns)) {
if (atlnum > 0) {
kv_push(signs, mark);
marktree_itr_next(buf->b_marktree, itr);
} else {
extmark_del(buf, itr, mark, true);
}
} else {
marktree_itr_next(buf->b_marktree, itr);
}
}
// Sort to remove the highest priority sign at a specific line number.
if (kv_size(signs)) {
qsort((void *)&kv_A(signs, 0), kv_size(signs), sizeof(MTKey), sign_row_cmp);
extmark_del_id(buf, kv_A(signs, 0).ns, kv_A(signs, 0).id);
kv_destroy(signs);
} else if (atlnum > 0) {
return FAIL;
}
// When deleting the last sign need to redraw the windows to remove the
// sign column. Not when curwin is NULL (this means we're exiting).
if (!buf_meta_total(buf, kMTMetaSignText) && curwin != NULL) {
changed_line_abv_curs();
}
return OK;
}
bool buf_has_signs(const buf_T *buf)
{
return (buf_meta_total(buf, kMTMetaSignHL) + buf_meta_total(buf, kMTMetaSignText));
}
/// List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
static void sign_list_placed(buf_T *rbuf, char *group)
{
char lbuf[MSG_BUF_LEN];
char namebuf[MSG_BUF_LEN];
char groupbuf[MSG_BUF_LEN];
buf_T *buf = rbuf ? rbuf : firstbuf;
int64_t ns = group_get_ns(group);
msg_puts_title(_("\n--- Signs ---"));
msg_putchar('\n');
while (buf != NULL && !got_int) {
if (buf_has_signs(buf)) {
vim_snprintf(lbuf, MSG_BUF_LEN, _("Signs for %s:"), buf->b_fname);
msg_puts_attr(lbuf, HL_ATTR(HLF_D));
msg_putchar('\n');
}
if (ns >= 0) {
MarkTreeIter itr[1];
kvec_t(MTKey) signs = KV_INITIAL_VALUE;
marktree_itr_get(buf->b_marktree, 0, 0, itr);
while (itr->x) {
MTKey mark = marktree_itr_current(itr);
if (!mt_end(mark) && mt_decor_sign(mark)
&& (ns == UINT32_MAX || ns == mark.ns)) {
kv_push(signs, mark);
}
marktree_itr_next(buf->b_marktree, itr);
}
if (kv_size(signs)) {
qsort((void *)&kv_A(signs, 0), kv_size(signs), sizeof(MTKey), sign_row_cmp);
for (size_t i = 0; i < kv_size(signs); i++) {
namebuf[0] = '\0';
groupbuf[0] = '\0';
MTKey mark = kv_A(signs, i);
DecorSignHighlight *sh = decor_find_sign(mt_decor(mark));
if (sh->sign_name != NULL) {
vim_snprintf(namebuf, MSG_BUF_LEN, _(" name=%s"), sign_get_name(sh));
}
if (mark.ns != 0) {
vim_snprintf(groupbuf, MSG_BUF_LEN, _(" group=%s"), describe_ns((int)mark.ns, ""));
}
vim_snprintf(lbuf, MSG_BUF_LEN, _(" line=%" PRIdLINENR " id=%u%s%s priority=%d"),
mark.pos.row + 1, mark.id, groupbuf, namebuf, sh->priority);
msg_puts(lbuf);
msg_putchar('\n');
}
kv_destroy(signs);
}
}
if (rbuf != NULL) {
return;
}
buf = buf->b_next;
}
}
/// Find index of a ":sign" subcmd from its name.
/// "*end_cmd" must be writable.
///
/// @param begin_cmd begin of sign subcmd
/// @param end_cmd just after sign subcmd
static int sign_cmd_idx(char *begin_cmd, char *end_cmd)
{
int idx;
char save = *end_cmd;
*end_cmd = NUL;
for (idx = 0;; idx++) {
if (cmds[idx] == NULL || strcmp(begin_cmd, cmds[idx]) == 0) {
break;
}
}
*end_cmd = save;
return idx;
}
/// buf must be SIGN_WIDTH * MAX_SCHAR_SIZE (no extra +1 needed)
size_t describe_sign_text(char *buf, schar_T *sign_text)
{
size_t p = 0;
for (int i = 0; i < SIGN_WIDTH; i++) {
schar_get(buf + p, sign_text[i]);
size_t len = strlen(buf + p);
if (len == 0) {
break;
}
p += len;
}
return p;
}
/// Initialize the "text" for a new sign and store in "sign_text".
/// "sp" is NULL for signs added through nvim_buf_set_extmark().
int init_sign_text(sign_T *sp, schar_T *sign_text, char *text)
{
char *s;
char *endp = text + (int)strlen(text);
for (s = sp ? text : endp; s + 1 < endp; s++) {
if (*s == '\\') {
// Remove a backslash, so that it is possible to use a space.
STRMOVE(s, s + 1);
endp--;
}
}
// Count cells and check for non-printable chars
int cells = 0;
for (s = text; s < endp; s += utfc_ptr2len(s)) {
int c;
sign_text[cells] = utfc_ptr2schar(s, &c);
if (!vim_isprintc(c)) {
break;
}
int width = utf_char2cells(c);
if (width == 2) {
sign_text[cells + 1] = 0;
}
cells += width;
}
// Currently must be empty, one or two display cells
if (s != endp || cells > SIGN_WIDTH) {
if (sp != NULL) {
semsg(_("E239: Invalid sign text: %s"), text);
}
return FAIL;
}
if (cells < 1) {
sign_text[0] = 0;
} else if (cells == 1) {
sign_text[1] = schar_from_ascii(' ');
}
return OK;
}
/// Define a new sign or update an existing sign
static int sign_define_by_name(char *name, char *icon, char *text, char *linehl, char *texthl,
char *culhl, char *numhl)
{
cstr_t *key;
sign_T **sp = (sign_T **)pmap_put_ref(cstr_t)(&sign_map, name, &key, NULL);
if (*sp == NULL) {
*key = xstrdup(name);
*sp = xcalloc(1, sizeof(sign_T));
(*sp)->sn_name = (char *)(*key);
} else {
// Signs may already exist, a redraw is needed in windows with a non-empty sign list.
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (buf_has_signs(wp->w_buffer)) {
redraw_buf_later(wp->w_buffer, UPD_NOT_VALID);
}
}
}
// Set values for a defined sign.
if (icon != NULL) {
/// Initialize the icon information for a new sign
xfree((*sp)->sn_icon);
(*sp)->sn_icon = xstrdup(icon);
backslash_halve((*sp)->sn_icon);
}
if (text != NULL && (init_sign_text(*sp, (*sp)->sn_text, text) == FAIL)) {
return FAIL;
}
char *arg[] = { linehl, texthl, culhl, numhl };
int *hl[] = { &(*sp)->sn_line_hl, &(*sp)->sn_text_hl, &(*sp)->sn_cul_hl, &(*sp)->sn_num_hl };
for (int i = 0; i < 4; i++) {
if (arg[i] != NULL) {
*hl[i] = *arg[i] ? syn_check_group(arg[i], strlen(arg[i])) : 0;
}
}
return OK;
}
/// Free the sign specified by 'name'.
static int sign_undefine_by_name(const char *name)
{
sign_T *sp = pmap_del(cstr_t)(&sign_map, name, NULL);
if (sp == NULL) {
semsg(_("E155: Unknown sign: %s"), name);
return FAIL;
}
xfree(sp->sn_name);
xfree(sp->sn_icon);
xfree(sp);
return OK;
}
/// List one sign.
static void sign_list_defined(sign_T *sp)
{
smsg(0, "sign %s", sp->sn_name);
if (sp->sn_icon != NULL) {
msg_puts(" icon=");
msg_outtrans(sp->sn_icon, 0);
msg_puts(_(" (not supported)"));
}
if (sp->sn_text[0]) {
msg_puts(" text=");
char buf[SIGN_WIDTH * MAX_SCHAR_SIZE];
describe_sign_text(buf, sp->sn_text);
msg_outtrans(buf, 0);
}
static char *arg[] = { " linehl=", " texthl=", " culhl=", " numhl=" };
int hl[] = { sp->sn_line_hl, sp->sn_text_hl, sp->sn_cul_hl, sp->sn_num_hl };
for (int i = 0; i < 4; i++) {
if (hl[i] > 0) {
msg_puts(arg[i]);
const char *p = get_highlight_name_ext(NULL, hl[i] - 1, false);
msg_puts(p ? p : "NONE");
}
}
}
/// List the signs matching 'name'
static void sign_list_by_name(char *name)
{
sign_T *sp = pmap_get(cstr_t)(&sign_map, name);
if (sp != NULL) {
sign_list_defined(sp);
} else {
semsg(_("E155: Unknown sign: %s"), name);
}
}
static void may_force_numberwidth_recompute(buf_T *buf, int unplace)
{
FOR_ALL_TAB_WINDOWS(tp, wp)
if (wp->w_buffer == buf
&& (wp->w_p_nu || wp->w_p_rnu)
&& (unplace || wp->w_nrwidth_width < 2)
&& (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')) {
wp->w_nrwidth_line_count = 0;
}
}
/// Place a sign at the specified file location or update a sign.
static int sign_place(uint32_t *id, char *group, char *name, buf_T *buf, linenr_T lnum, int prio)
{
// Check for reserved character '*' in group name
if (group != NULL && (*group == '*' || *group == '\0')) {
return FAIL;
}
sign_T *sp = pmap_get(cstr_t)(&sign_map, name);
if (sp == NULL) {
semsg(_("E155: Unknown sign: %s"), name);
return FAIL;
}
if (lnum > 0) {
// ":sign place {id} line={lnum} name={name} file={fname}": place a sign
buf_set_sign(buf, id, group, prio, lnum, sp);
} else {
// ":sign place {id} file={fname}": change sign type and/or priority
lnum = buf_mod_sign(buf, id, group, prio, sp);
}
if (lnum > 0) {
// When displaying signs in the 'number' column, if the width of the
// number column is less than 2, then force recomputing the width.
may_force_numberwidth_recompute(buf, false);
} else {
semsg(_("E885: Not possible to change sign %s"), name);
return FAIL;
}
return OK;
}
static int sign_unplace_inner(buf_T *buf, int id, char *group, linenr_T atlnum)
{
if (!buf_has_signs(buf)) { // No signs in the buffer
return FAIL;
}
if (id == 0 || atlnum > 0 || (group != NULL && *group == '*')) {
// Delete multiple specified signs
if (!buf_delete_signs(buf, group, id, atlnum)) {
return FAIL;
}
} else {
// Delete only a single sign
int64_t ns = group_get_ns(group);
if (ns < 0 || !extmark_del_id(buf, (uint32_t)ns, (uint32_t)id)) {
return FAIL;
}
}
// When all the signs in a buffer are removed, force recomputing the
// number column width (if enabled) in all the windows displaying the
// buffer if 'signcolumn' is set to 'number' in that window.
if (!buf_meta_total(buf, kMTMetaSignText)) {
may_force_numberwidth_recompute(buf, true);
}
return OK;
}
/// Unplace the specified sign for a single or all buffers
static int sign_unplace(buf_T *buf, int id, char *group, linenr_T atlnum)
{
if (buf != NULL) {
return sign_unplace_inner(buf, id, group, atlnum);
} else {
int retval = OK;
FOR_ALL_BUFFERS(cbuf) {
if (!sign_unplace_inner(cbuf, id, group, atlnum)) {
retval = FAIL;
}
}
return retval;
}
}
/// Jump to a sign.
static linenr_T sign_jump(int id, char *group, buf_T *buf)
{
linenr_T lnum = buf_findsign(buf, id, group);
if (lnum <= 0) {
semsg(_("E157: Invalid sign ID: %" PRId32), id);
return -1;
}
// goto a sign ...
if (buf_jump_open_win(buf) != NULL) { // ... in a current window
curwin->w_cursor.lnum = lnum;
check_cursor_lnum(curwin);
beginline(BL_WHITE);
} else { // ... not currently in a window
if (buf->b_fname == NULL) {
emsg(_("E934: Cannot jump to a buffer that does not have a name"));
return -1;
}
size_t cmdlen = strlen(buf->b_fname) + 24;
char *cmd = xmallocz(cmdlen);
snprintf(cmd, cmdlen, "e +%" PRId64 " %s", (int64_t)lnum, buf->b_fname);
do_cmdline_cmd(cmd);
xfree(cmd);
}
foldOpenCursor();
return lnum;
}
/// ":sign define {name} ..." command
static void sign_define_cmd(char *name, char *cmdline)
{
char *icon = NULL;
char *text = NULL;
char *linehl = NULL;
char *texthl = NULL;
char *culhl = NULL;
char *numhl = NULL;
// set values for a defined sign.
while (true) {
char *arg = skipwhite(cmdline);
if (*arg == NUL) {
break;
}
cmdline = skiptowhite_esc(arg);
if (strncmp(arg, "icon=", 5) == 0) {
icon = arg + 5;
} else if (strncmp(arg, "text=", 5) == 0) {
text = arg + 5;
} else if (strncmp(arg, "linehl=", 7) == 0) {
linehl = arg + 7;
} else if (strncmp(arg, "texthl=", 7) == 0) {
texthl = arg + 7;
} else if (strncmp(arg, "culhl=", 6) == 0) {
culhl = arg + 6;
} else if (strncmp(arg, "numhl=", 6) == 0) {
numhl = arg + 6;
} else {
semsg(_(e_invarg2), arg);
return;
}
if (*cmdline == NUL) {
break;
}
*cmdline++ = NUL;
}
sign_define_by_name(name, icon, text, linehl, texthl, culhl, numhl);
}
/// ":sign place" command
static void sign_place_cmd(buf_T *buf, linenr_T lnum, char *name, int id, char *group, int prio)
{
if (id <= 0) {
// List signs placed in a file/buffer
// :sign place file={fname}
// :sign place group={group} file={fname}
// :sign place group=* file={fname}
// :sign place buffer={nr}
// :sign place group={group} buffer={nr}
// :sign place group=* buffer={nr}
// :sign place
// :sign place group={group}
// :sign place group=*
if (lnum >= 0 || name != NULL || (group != NULL && *group == '\0')) {
emsg(_(e_invarg));
} else {
sign_list_placed(buf, group);
}
} else {
// Place a new sign
if (name == NULL || buf == NULL || (group != NULL && *group == '\0')) {
emsg(_(e_invarg));
return;
}
uint32_t uid = (uint32_t)id;
sign_place(&uid, group, name, buf, lnum, prio);
}
}
/// ":sign unplace" command
static void sign_unplace_cmd(buf_T *buf, linenr_T lnum, const char *name, int id, char *group)
{
if (lnum >= 0 || name != NULL || (group != NULL && *group == '\0')) {
emsg(_(e_invarg));
return;
}
if (id == -1) {
lnum = curwin->w_cursor.lnum;
buf = curwin->w_buffer;
}
if (!sign_unplace(buf, MAX(0, id), group, lnum) && lnum > 0) {
emsg(_("E159: Missing sign number"));
}
}
/// Jump to a placed sign commands:
/// :sign jump {id} file={fname}
/// :sign jump {id} buffer={nr}
/// :sign jump {id} group={group} file={fname}
/// :sign jump {id} group={group} buffer={nr}
static void sign_jump_cmd(buf_T *buf, linenr_T lnum, const char *name, int id, char *group)
{
if (name == NULL && group == NULL && id == -1) {
emsg(_(e_argreq));
return;
}
if (buf == NULL || (group != NULL && *group == '\0') || lnum >= 0 || name != NULL) {
// File or buffer is not specified or an empty group is used
// or a line number or a sign name is specified.
emsg(_(e_invarg));
return;
}
sign_jump(id, group, buf);
}
/// Parse the command line arguments for the ":sign place", ":sign unplace" and
/// ":sign jump" commands.
/// The supported arguments are: line={lnum} name={name} group={group}
/// priority={prio} and file={fname} or buffer={nr}.
static int parse_sign_cmd_args(int cmd, char *arg, char **name, int *id, char **group, int *prio,
buf_T **buf, linenr_T *lnum)
{
char *arg1 = arg;
char *filename = NULL;
bool lnum_arg = false;
// first arg could be placed sign id
if (ascii_isdigit(*arg)) {
*id = getdigits_int(&arg, true, 0);
if (!ascii_iswhite(*arg) && *arg != NUL) {
*id = -1;
arg = arg1;
} else {
arg = skipwhite(arg);
}
}
while (*arg != NUL) {
if (strncmp(arg, "line=", 5) == 0) {
arg += 5;
*lnum = atoi(arg);
arg = skiptowhite(arg);
lnum_arg = true;
} else if (strncmp(arg, "*", 1) == 0 && cmd == SIGNCMD_UNPLACE) {
if (*id != -1) {
emsg(_(e_invarg));
return FAIL;
}
*id = -2;
arg = skiptowhite(arg + 1);
} else if (strncmp(arg, "name=", 5) == 0) {
arg += 5;
char *namep = arg;
arg = skiptowhite(arg);
if (*arg != NUL) {
*arg++ = NUL;
}
while (namep[0] == '0' && namep[1] != NUL) {
namep++;
}
*name = namep;
} else if (strncmp(arg, "group=", 6) == 0) {
arg += 6;
*group = arg;
arg = skiptowhite(arg);
if (*arg != NUL) {
*arg++ = NUL;
}
} else if (strncmp(arg, "priority=", 9) == 0) {
arg += 9;
*prio = atoi(arg);
arg = skiptowhite(arg);
} else if (strncmp(arg, "file=", 5) == 0) {
arg += 5;
filename = arg;
*buf = buflist_findname_exp(arg);
break;
} else if (strncmp(arg, "buffer=", 7) == 0) {
arg += 7;
filename = arg;
*buf = buflist_findnr(getdigits_int(&arg, true, 0));
if (*skipwhite(arg) != NUL) {
semsg(_(e_trailing_arg), arg);
}
break;
} else {
emsg(_(e_invarg));
return FAIL;
}
arg = skipwhite(arg);
}
if (filename != NULL && *buf == NULL) {
semsg(_(e_invalid_buffer_name_str), filename);
return FAIL;
}
// If the filename is not supplied for the sign place or the sign jump
// command, then use the current buffer.
if (filename == NULL && ((cmd == SIGNCMD_PLACE && lnum_arg) || cmd == SIGNCMD_JUMP)) {
*buf = curwin->w_buffer;
}
return OK;
}
/// ":sign" command
void ex_sign(exarg_T *eap)
{
char *arg = eap->arg;
// Parse the subcommand.
char *p = skiptowhite(arg);
int idx = sign_cmd_idx(arg, p);
if (idx == SIGNCMD_LAST) {
semsg(_("E160: Unknown sign command: %s"), arg);
return;
}
arg = skipwhite(p);
if (idx <= SIGNCMD_LIST) {
// Define, undefine or list signs.
if (idx == SIGNCMD_LIST && *arg == NUL) {
// ":sign list": list all defined signs
sign_T *sp;
map_foreach_value(&sign_map, sp, {
sign_list_defined(sp);
});
} else if (*arg == NUL) {
emsg(_("E156: Missing sign name"));
} else {
// Isolate the sign name. If it's a number skip leading zeroes,
// so that "099" and "99" are the same sign. But keep "0".
p = skiptowhite(arg);
if (*p != NUL) {
*p++ = NUL;
}
while (arg[0] == '0' && arg[1] != NUL) {
arg++;
}
if (idx == SIGNCMD_DEFINE) {
sign_define_cmd(arg, p);
} else if (idx == SIGNCMD_LIST) {
// ":sign list {name}"
sign_list_by_name(arg);
} else {
// ":sign undefine {name}"
sign_undefine_by_name(arg);
}
return;
}
} else {
int id = -1;
linenr_T lnum = -1;
char *name = NULL;
char *group = NULL;
int prio = SIGN_DEF_PRIO;
buf_T *buf = NULL;
// Parse command line arguments
if (parse_sign_cmd_args(idx, arg, &name, &id, &group, &prio, &buf, &lnum) == FAIL) {
return;
}
if (idx == SIGNCMD_PLACE) {
sign_place_cmd(buf, lnum, name, id, group, prio);
} else if (idx == SIGNCMD_UNPLACE) {
sign_unplace_cmd(buf, lnum, name, id, group);
} else if (idx == SIGNCMD_JUMP) {
sign_jump_cmd(buf, lnum, name, id, group);
}
}
}
/// Get dictionary of information for a defined sign "sp"
static dict_T *sign_get_info_dict(sign_T *sp)
{
dict_T *d = tv_dict_alloc();
tv_dict_add_str(d, S_LEN("name"), sp->sn_name);
if (sp->sn_icon != NULL) {
tv_dict_add_str(d, S_LEN("icon"), sp->sn_icon);
}
if (sp->sn_text[0]) {
char buf[SIGN_WIDTH * MAX_SCHAR_SIZE];
describe_sign_text(buf, sp->sn_text);
tv_dict_add_str(d, S_LEN("text"), buf);
}
static char *arg[] = { "linehl", "texthl", "culhl", "numhl" };
int hl[] = { sp->sn_line_hl, sp->sn_text_hl, sp->sn_cul_hl, sp->sn_num_hl };
for (int i = 0; i < 4; i++) {
if (hl[i] > 0) {
const char *p = get_highlight_name_ext(NULL, hl[i] - 1, false);
tv_dict_add_str(d, arg[i], strlen(arg[i]), p ? p : "NONE");
}
}
return d;
}
/// Get dictionary of information for placed sign "mark"
static dict_T *sign_get_placed_info_dict(MTKey mark)
{
dict_T *d = tv_dict_alloc();
DecorSignHighlight *sh = decor_find_sign(mt_decor(mark));
tv_dict_add_str(d, S_LEN("name"), sign_get_name(sh));
tv_dict_add_nr(d, S_LEN("id"), (int)mark.id);
tv_dict_add_str(d, S_LEN("group"), describe_ns((int)mark.ns, ""));
tv_dict_add_nr(d, S_LEN("lnum"), mark.pos.row + 1);
tv_dict_add_nr(d, S_LEN("priority"), sh->priority);
return d;
}
/// Returns information about signs placed in a buffer as list of dicts.
list_T *get_buffer_signs(buf_T *buf)
FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
list_T *const l = tv_list_alloc(kListLenMayKnow);
MarkTreeIter itr[1];
marktree_itr_get(buf->b_marktree, 0, 0, itr);
while (itr->x) {
MTKey mark = marktree_itr_current(itr);
if (!mt_end(mark) && mt_decor_sign(mark)) {
tv_list_append_dict(l, sign_get_placed_info_dict(mark));
}
marktree_itr_next(buf->b_marktree, itr);
}
return l;
}
/// @return information about all the signs placed in a buffer
static void sign_get_placed_in_buf(buf_T *buf, linenr_T lnum, int sign_id, const char *group,
list_T *retlist)
{
dict_T *d = tv_dict_alloc();
tv_list_append_dict(retlist, d);
tv_dict_add_nr(d, S_LEN("bufnr"), buf->b_fnum);
list_T *l = tv_list_alloc(kListLenMayKnow);
tv_dict_add_list(d, S_LEN("signs"), l);
int64_t ns = group_get_ns(group);
if (!buf_has_signs(buf) || ns < 0) {
return;
}
MarkTreeIter itr[1];
kvec_t(MTKey) signs = KV_INITIAL_VALUE;
marktree_itr_get(buf->b_marktree, lnum ? lnum - 1 : 0, 0, itr);
while (itr->x) {
MTKey mark = marktree_itr_current(itr);
if (lnum && mark.pos.row >= lnum) {
break;
}
if (!mt_end(mark)
&& (ns == UINT32_MAX || ns == mark.ns)
&& ((lnum == 0 && sign_id == 0)
|| (sign_id == 0 && lnum == mark.pos.row + 1)
|| (lnum == 0 && sign_id == (int)mark.id)
|| (lnum == mark.pos.row + 1 && sign_id == (int)mark.id))) {
if (mt_decor_sign(mark)) {
kv_push(signs, mark);
}
}
marktree_itr_next(buf->b_marktree, itr);
}
if (kv_size(signs)) {
qsort((void *)&kv_A(signs, 0), kv_size(signs), sizeof(MTKey), sign_row_cmp);
for (size_t i = 0; i < kv_size(signs); i++) {
tv_list_append_dict(l, sign_get_placed_info_dict(kv_A(signs, i)));
}
kv_destroy(signs);
}
}
/// Get a list of signs placed in buffer 'buf'. If 'num' is non-zero, return the
/// sign placed at the line number. If 'lnum' is zero, return all the signs
/// placed in 'buf'. If 'buf' is NULL, return signs placed in all the buffers.
static void sign_get_placed(buf_T *buf, linenr_T lnum, int id, const char *group, list_T *retlist)
{
if (buf != NULL) {
sign_get_placed_in_buf(buf, lnum, id, group, retlist);
} else {
FOR_ALL_BUFFERS(cbuf) {
if (buf_has_signs(cbuf)) {
sign_get_placed_in_buf(cbuf, 0, id, group, retlist);
}
}
}
}
void free_signs(void)
{
cstr_t name;
kvec_t(cstr_t) names = KV_INITIAL_VALUE;
map_foreach_key(&sign_map, name, {
kv_push(names, name);
});
for (size_t i = 0; i < kv_size(names); i++) {
sign_undefine_by_name(kv_A(names, i));
}
kv_destroy(names);
}
static enum {
EXP_SUBCMD, // expand :sign sub-commands
EXP_DEFINE, // expand :sign define {name} args
EXP_PLACE, // expand :sign place {id} args
EXP_LIST, // expand :sign place args
EXP_UNPLACE, // expand :sign unplace"
EXP_SIGN_NAMES, // expand with name of placed signs
EXP_SIGN_GROUPS, // expand with name of placed sign groups
} expand_what;
/// @return the n'th sign name (used for command line completion)
static char *get_nth_sign_name(int idx)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
{
// Complete with name of signs already defined
cstr_t name;
int current_idx = 0;
map_foreach_key(&sign_map, name, {
if (current_idx++ == idx) {
return (char *)name;
}
});
return NULL;
}
/// @return the n'th sign group name (used for command line completion)
static char *get_nth_sign_group_name(int idx)
{
// Complete with name of sign groups already defined
if (idx < (int)kv_size(sign_ns)) {
return (char *)describe_ns((NS)kv_A(sign_ns, idx), "");
}
return NULL;
}
/// Function given to ExpandGeneric() to obtain the sign command expansion.
char *get_sign_name(expand_T *xp, int idx)
{
switch (expand_what) {
case EXP_SUBCMD:
return cmds[idx];
case EXP_DEFINE: {
char *define_arg[] = { "culhl=", "icon=", "linehl=", "numhl=", "text=", "texthl=", NULL };
return define_arg[idx];
}
case EXP_PLACE: {
char *place_arg[] = { "line=", "name=", "group=", "priority=", "file=", "buffer=", NULL };
return place_arg[idx];
}
case EXP_LIST: {
char *list_arg[] = { "group=", "file=", "buffer=", NULL };
return list_arg[idx];
}
case EXP_UNPLACE: {
char *unplace_arg[] = { "group=", "file=", "buffer=", NULL };
return unplace_arg[idx];
}
case EXP_SIGN_NAMES:
return get_nth_sign_name(idx);
case EXP_SIGN_GROUPS:
return get_nth_sign_group_name(idx);
default:
return NULL;
}
}
/// Handle command line completion for :sign command.
void set_context_in_sign_cmd(expand_T *xp, char *arg)
{
// Default: expand subcommands.
xp->xp_context = EXPAND_SIGN;
expand_what = EXP_SUBCMD;
xp->xp_pattern = arg;
char *end_subcmd = skiptowhite(arg);
if (*end_subcmd == NUL) {
// expand subcmd name
// :sign {subcmd}<CTRL-D>
return;
}
int cmd_idx = sign_cmd_idx(arg, end_subcmd);
// :sign {subcmd} {subcmd_args}
// |
// begin_subcmd_args
char *begin_subcmd_args = skipwhite(end_subcmd);
// Expand last argument of subcmd.
//
// :sign define {name} {args}...
// |
// p
// Loop until reaching last argument.
char *last;
char *p = begin_subcmd_args;
do {
p = skipwhite(p);
last = p;
p = skiptowhite(p);
} while (*p != NUL);
p = vim_strchr(last, '=');
// :sign define {name} {args}... {last}=
// | |
// last p
if (p == NULL) {
// Expand last argument name (before equal sign).
xp->xp_pattern = last;
switch (cmd_idx) {
case SIGNCMD_DEFINE:
expand_what = EXP_DEFINE;
break;
case SIGNCMD_PLACE:
// List placed signs
if (ascii_isdigit(*begin_subcmd_args)) {
// :sign place {id} {args}...
expand_what = EXP_PLACE;
} else {
// :sign place {args}...
expand_what = EXP_LIST;
}
break;
case SIGNCMD_LIST:
case SIGNCMD_UNDEFINE:
// :sign list <CTRL-D>
// :sign undefine <CTRL-D>
expand_what = EXP_SIGN_NAMES;
break;
case SIGNCMD_JUMP:
case SIGNCMD_UNPLACE:
expand_what = EXP_UNPLACE;
break;
default:
xp->xp_context = EXPAND_NOTHING;
}
} else {
// Expand last argument value (after equal sign).
xp->xp_pattern = p + 1;
switch (cmd_idx) {
case SIGNCMD_DEFINE:
if (strncmp(last, "texthl", 6) == 0
|| strncmp(last, "linehl", 6) == 0
|| strncmp(last, "culhl", 5) == 0
|| strncmp(last, "numhl", 5) == 0) {
xp->xp_context = EXPAND_HIGHLIGHT;
} else if (strncmp(last, "icon", 4) == 0) {
xp->xp_context = EXPAND_FILES;
} else {
xp->xp_context = EXPAND_NOTHING;
}
break;
case SIGNCMD_PLACE:
if (strncmp(last, "name", 4) == 0) {
expand_what = EXP_SIGN_NAMES;
} else if (strncmp(last, "group", 5) == 0) {
expand_what = EXP_SIGN_GROUPS;
} else if (strncmp(last, "file", 4) == 0) {
xp->xp_context = EXPAND_BUFFERS;
} else {
xp->xp_context = EXPAND_NOTHING;
}
break;
case SIGNCMD_UNPLACE:
case SIGNCMD_JUMP:
if (strncmp(last, "group", 5) == 0) {
expand_what = EXP_SIGN_GROUPS;
} else if (strncmp(last, "file", 4) == 0) {
xp->xp_context = EXPAND_BUFFERS;
} else {
xp->xp_context = EXPAND_NOTHING;
}
break;
default:
xp->xp_context = EXPAND_NOTHING;
}
}
}
/// Define a sign using the attributes in 'dict'. Returns 0 on success and -1 on
/// failure.
static int sign_define_from_dict(char *name, dict_T *dict)
{
if (name == NULL) {
name = tv_dict_get_string(dict, "name", false);
if (name == NULL || name[0] == NUL) {
return -1;
}
}
char *icon = NULL;
char *linehl = NULL;
char *text = NULL;
char *texthl = NULL;
char *culhl = NULL;
char *numhl = NULL;
if (dict != NULL) {
icon = tv_dict_get_string(dict, "icon", false);
linehl = tv_dict_get_string(dict, "linehl", false);
text = tv_dict_get_string(dict, "text", false);
texthl = tv_dict_get_string(dict, "texthl", false);
culhl = tv_dict_get_string(dict, "culhl", false);
numhl = tv_dict_get_string(dict, "numhl", false);
}
return sign_define_by_name(name, icon, text, linehl, texthl, culhl, numhl) - 1;
}
/// Define multiple signs using attributes from list 'l' and store the return
/// values in 'retlist'.
static void sign_define_multiple(list_T *l, list_T *retlist)
{
TV_LIST_ITER_CONST(l, li, {
int retval = -1;
if (TV_LIST_ITEM_TV(li)->v_type == VAR_DICT) {
retval = sign_define_from_dict(NULL, TV_LIST_ITEM_TV(li)->vval.v_dict);
} else {
emsg(_(e_dictreq));
}
tv_list_append_number(retlist, retval);
});
}
/// "sign_define()" function
void f_sign_define(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_UNKNOWN) {
// Define multiple signs
tv_list_alloc_ret(rettv, kListLenMayKnow);
sign_define_multiple(argvars[0].vval.v_list, rettv->vval.v_list);
return;
}
// Define a single sign
rettv->vval.v_number = -1;
char *name = (char *)tv_get_string_chk(&argvars[0]);
if (name == NULL) {
return;
}
if (tv_check_for_opt_dict_arg(argvars, 1) == FAIL) {
return;
}
dict_T *d = argvars[1].v_type == VAR_DICT ? argvars[1].vval.v_dict : NULL;
rettv->vval.v_number = sign_define_from_dict(name, d);
}
/// "sign_getdefined()" function
void f_sign_getdefined(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
tv_list_alloc_ret(rettv, 0);
if (argvars[0].v_type == VAR_UNKNOWN) {
sign_T *sp;
map_foreach_value(&sign_map, sp, {
tv_list_append_dict(rettv->vval.v_list, sign_get_info_dict(sp));
});
} else {
sign_T *sp = pmap_get(cstr_t)(&sign_map, tv_get_string(&argvars[0]));
if (sp != NULL) {
tv_list_append_dict(rettv->vval.v_list, sign_get_info_dict(sp));
}
}
}
/// "sign_getplaced()" function
void f_sign_getplaced(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
buf_T *buf = NULL;
linenr_T lnum = 0;
int sign_id = 0;
const char *group = NULL;
bool notanum = false;
tv_list_alloc_ret(rettv, 0);
if (argvars[0].v_type != VAR_UNKNOWN) {
// get signs placed in the specified buffer
buf = get_buf_arg(&argvars[0]);
if (buf == NULL) {
return;
}
if (argvars[1].v_type != VAR_UNKNOWN) {
if (tv_check_for_nonnull_dict_arg(argvars, 1) == FAIL) {
return;
}
dictitem_T *di;
dict_T *dict = argvars[1].vval.v_dict;
if ((di = tv_dict_find(dict, "lnum", -1)) != NULL) {
// get signs placed at this line
lnum = tv_get_lnum(&di->di_tv);
if (lnum <= 0) {
return;
}
}
if ((di = tv_dict_find(dict, "id", -1)) != NULL) {
// get sign placed with this identifier
sign_id = (int)tv_get_number_chk(&di->di_tv, ¬anum);
if (notanum) {
return;
}
}
if ((di = tv_dict_find(dict, "group", -1)) != NULL) {
group = tv_get_string_chk(&di->di_tv);
if (group == NULL) {
return;
}
if (*group == '\0') { // empty string means global group
group = NULL;
}
}
}
}
sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
}
/// "sign_jump()" function
void f_sign_jump(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
rettv->vval.v_number = -1;
// Sign identifier
bool notanum = false;
int id = (int)tv_get_number_chk(&argvars[0], ¬anum);
if (notanum) {
return;
}
if (id <= 0) {
emsg(_(e_invarg));
return;
}
// Sign group
char *group = (char *)tv_get_string_chk(&argvars[1]);
if (group == NULL) {
return;
}
if (group[0] == NUL) {
group = NULL;
}
// Buffer to place the sign
buf_T *buf = get_buf_arg(&argvars[2]);
if (buf == NULL) {
return;
}
rettv->vval.v_number = sign_jump(id, group, buf);
}
/// Place a new sign using the values specified in dict 'dict'. Returns the sign
/// identifier if successfully placed, otherwise returns -1.
static int sign_place_from_dict(typval_T *id_tv, typval_T *group_tv, typval_T *name_tv,
typval_T *buf_tv, dict_T *dict)
{
dictitem_T *di;
int id = 0;
bool notanum = false;
if (id_tv == NULL) {
di = tv_dict_find(dict, "id", -1);
if (di != NULL) {
id_tv = &di->di_tv;
}
}
if (id_tv != NULL) {
id = (int)tv_get_number_chk(id_tv, ¬anum);
if (notanum) {
return -1;
}
if (id < 0) {
emsg(_(e_invarg));
return -1;
}
}
char *group = NULL;
if (group_tv == NULL) {
di = tv_dict_find(dict, "group", -1);
if (di != NULL) {
group_tv = &di->di_tv;
}
}
if (group_tv != NULL) {
group = (char *)tv_get_string_chk(group_tv);
if (group == NULL) {
return -1;
}
if (group[0] == NUL) {
group = NULL;
}
}
char *name = NULL;
if (name_tv == NULL) {
di = tv_dict_find(dict, "name", -1);
if (di != NULL) {
name_tv = &di->di_tv;
}
}
if (name_tv == NULL) {
return -1;
}
name = (char *)tv_get_string_chk(name_tv);
if (name == NULL) {
return -1;
}
if (buf_tv == NULL) {
di = tv_dict_find(dict, "buffer", -1);
if (di != NULL) {
buf_tv = &di->di_tv;
}
}
if (buf_tv == NULL) {
return -1;
}
buf_T *buf = get_buf_arg(buf_tv);
if (buf == NULL) {
return -1;
}
linenr_T lnum = 0;
di = tv_dict_find(dict, "lnum", -1);
if (di != NULL) {
lnum = tv_get_lnum(&di->di_tv);
if (lnum <= 0) {
emsg(_(e_invarg));
return -1;
}
}
int prio = SIGN_DEF_PRIO;
di = tv_dict_find(dict, "priority", -1);
if (di != NULL) {
prio = (int)tv_get_number_chk(&di->di_tv, ¬anum);
if (notanum) {
return -1;
}
}
uint32_t uid = (uint32_t)id;
if (sign_place(&uid, group, name, buf, lnum, prio) == OK) {
return (int)uid;
}
return -1;
}
/// "sign_place()" function
void f_sign_place(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
dict_T *dict = NULL;
rettv->vval.v_number = -1;
if (argvars[4].v_type != VAR_UNKNOWN) {
if (tv_check_for_nonnull_dict_arg(argvars, 4) == FAIL) {
return;
}
dict = argvars[4].vval.v_dict;
}
rettv->vval.v_number = sign_place_from_dict(&argvars[0], &argvars[1],
&argvars[2], &argvars[3], dict);
}
/// "sign_placelist()" function. Place multiple signs.
void f_sign_placelist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
tv_list_alloc_ret(rettv, kListLenMayKnow);
if (argvars[0].v_type != VAR_LIST) {
emsg(_(e_listreq));
return;
}
// Process the List of sign attributes
TV_LIST_ITER_CONST(argvars[0].vval.v_list, li, {
int sign_id = -1;
if (TV_LIST_ITEM_TV(li)->v_type == VAR_DICT) {
sign_id = sign_place_from_dict(NULL, NULL, NULL, NULL, TV_LIST_ITEM_TV(li)->vval.v_dict);
} else {
emsg(_(e_dictreq));
}
tv_list_append_number(rettv->vval.v_list, sign_id);
});
}
/// Undefine multiple signs
static void sign_undefine_multiple(list_T *l, list_T *retlist)
{
TV_LIST_ITER_CONST(l, li, {
int retval = -1;
char *name = (char *)tv_get_string_chk(TV_LIST_ITEM_TV(li));
if (name != NULL && (sign_undefine_by_name(name) == OK)) {
retval = 0;
}
tv_list_append_number(retlist, retval);
});
}
/// "sign_undefine()" function
void f_sign_undefine(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_UNKNOWN) {
// Undefine multiple signs
tv_list_alloc_ret(rettv, kListLenMayKnow);
sign_undefine_multiple(argvars[0].vval.v_list, rettv->vval.v_list);
return;
}
rettv->vval.v_number = -1;
if (argvars[0].v_type == VAR_UNKNOWN) {
// Free all the signs
free_signs();
rettv->vval.v_number = 0;
} else {
// Free only the specified sign
const char *name = tv_get_string_chk(&argvars[0]);
if (name == NULL) {
return;
}
if (sign_undefine_by_name(name) == OK) {
rettv->vval.v_number = 0;
}
}
}
/// Unplace the sign with attributes specified in 'dict'. Returns 0 on success
/// and -1 on failure.
static int sign_unplace_from_dict(typval_T *group_tv, dict_T *dict)
{
dictitem_T *di;
int id = 0;
buf_T *buf = NULL;
char *group = (group_tv != NULL) ? (char *)tv_get_string(group_tv)
: tv_dict_get_string(dict, "group", false);
if (group != NULL && group[0] == NUL) {
group = NULL;
}
if (dict != NULL) {
if ((di = tv_dict_find(dict, "buffer", -1)) != NULL) {
buf = get_buf_arg(&di->di_tv);
if (buf == NULL) {
return -1;
}
}
if (tv_dict_find(dict, "id", -1) != NULL) {
id = (int)tv_dict_get_number(dict, "id");
if (id <= 0) {
emsg(_(e_invarg));
return -1;
}
}
}
return sign_unplace(buf, id, group, 0) - 1;
}
/// "sign_unplace()" function
void f_sign_unplace(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
dict_T *dict = NULL;
rettv->vval.v_number = -1;
if (tv_check_for_string_arg(argvars, 0) == FAIL
|| tv_check_for_opt_dict_arg(argvars, 1) == FAIL) {
return;
}
if (argvars[1].v_type != VAR_UNKNOWN) {
dict = argvars[1].vval.v_dict;
}
rettv->vval.v_number = sign_unplace_from_dict(&argvars[0], dict);
}
/// "sign_unplacelist()" function
void f_sign_unplacelist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
tv_list_alloc_ret(rettv, kListLenMayKnow);
if (argvars[0].v_type != VAR_LIST) {
emsg(_(e_listreq));
return;
}
TV_LIST_ITER_CONST(argvars[0].vval.v_list, li, {
int retval = -1;
if (TV_LIST_ITEM_TV(li)->v_type == VAR_DICT) {
retval = sign_unplace_from_dict(NULL, TV_LIST_ITEM_TV(li)->vval.v_dict);
} else {
emsg(_(e_dictreq));
}
tv_list_append_number(rettv->vval.v_list, retval);
});
}
|