aboutsummaryrefslogtreecommitdiffhomepage
path: root/highlight.cpp
blob: fd5ffb7906eb6343fb86779e63a0fc14c95915f8 (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
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
/** \file highlight.c
  Functions for syntax highlighting
*/
#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <wchar.h>
#include <wctype.h>
#include <termios.h>
#include <signal.h>

#include "fallback.h"
#include "util.h"

#include "wutil.h"
#include "highlight.h"
#include "tokenizer.h"
#include "proc.h"
#include "parser.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "builtin.h"
#include "function.h"
#include "env.h"
#include "expand.h"
#include "sanity.h"
#include "common.h"
#include "complete.h"
#include "output.h"
#include "wildcard.h"
#include "path.h"
#include "history.h"

/**
   Number of elements in the highlight_var array
*/
#define VAR_COUNT ( sizeof(highlight_var)/sizeof(wchar_t *) )

static void highlight_universal_internal(const wcstring &buff, std::vector<int> &color, size_t pos);

/**
   The environment variables used to specify the color of different tokens.
*/
static const wchar_t * const highlight_var[] =
{
    L"fish_color_normal",
    L"fish_color_error",
    L"fish_color_command",
    L"fish_color_end",
    L"fish_color_param",
    L"fish_color_comment",
    L"fish_color_match",
    L"fish_color_search_match",
    L"fish_color_operator",
    L"fish_color_escape",
    L"fish_color_quote",
    L"fish_color_redirection",
    L"fish_color_valid_path",
    L"fish_color_autosuggestion"
};

/* If the given path looks like it's relative to the working directory, then prepend that working directory. */
static wcstring apply_working_directory(const wcstring &path, const wcstring &working_directory)
{
    if (path.empty() || working_directory.empty())
        return path;

    /* We're going to make sure that if we want to prepend the wd, that the string has no leading / */
    bool prepend_wd;
    switch (path.at(0))
    {
        case L'/':
        case L'~':
            prepend_wd = false;
            break;
        default:
            prepend_wd = true;
            break;
    }

    if (! prepend_wd)
    {
        /* No need to prepend the wd, so just return the path we were given */
        return path;
    }
    else
    {
        /* Remove up to one ./ */
        wcstring path_component = path;
        if (string_prefixes_string(L"./", path_component))
        {
            path_component.erase(0, 2);
        }

        /* Removing leading /s */
        while (string_prefixes_string(L"/", path_component))
        {
            path_component.erase(0, 1);
        }

        /* Construct and return a new path */
        wcstring new_path = working_directory;
        append_path_component(new_path, path_component);
        return new_path;
    }
}

/* Determine if the filesystem containing the given fd is case insensitive. */
typedef std::map<wcstring, bool> case_sensitivity_cache_t;
bool fs_is_case_insensitive(const wcstring &path, int fd, case_sensitivity_cache_t &case_sensitivity_cache)
{
    /* If _PC_CASE_SENSITIVE is not defined, assume case sensitive */
    bool result = false;
#ifdef _PC_CASE_SENSITIVE
    /* Try the cache first */
    case_sensitivity_cache_t::iterator cache = case_sensitivity_cache.find(path);
    if (cache != case_sensitivity_cache.end())
    {
        /* Use the cached value */
        result = cache->second;
    }
    else
    {
        /* Ask the system. A -1 value means error (so assume case sensitive), a 1 value means case sensitive, and a 0 value means case insensitive */
        long ret = fpathconf(fd, _PC_CASE_SENSITIVE);
        result = (ret == 0);
        case_sensitivity_cache[path] = result;
    }
#endif
    return result;
}

/* Tests whether the specified string cpath is the prefix of anything we could cd to. directories is a list of possible parent directories (typically either the working directory, or the cdpath). This does I/O!

   We expect the path to already be unescaped.
*/
bool is_potential_path(const wcstring &const_path, const wcstring_list_t &directories, path_flags_t flags, wcstring *out_path)
{
    ASSERT_IS_BACKGROUND_THREAD();

    const bool require_dir = !!(flags & PATH_REQUIRE_DIR);
    wcstring clean_path;
    int has_magic = 0;
    bool result = false;

    wcstring path(const_path);
    if (flags & PATH_EXPAND_TILDE)
        expand_tilde(path);

    //  debug( 1, L"%ls -> %ls ->%ls", path, tilde, unescaped );

    for (size_t i=0; i < path.size(); i++)
    {
        wchar_t c = path.at(i);
        switch (c)
        {
            case PROCESS_EXPAND:
            case VARIABLE_EXPAND:
            case VARIABLE_EXPAND_SINGLE:
            case BRACKET_BEGIN:
            case BRACKET_END:
            case BRACKET_SEP:
            case ANY_CHAR:
            case ANY_STRING:
            case ANY_STRING_RECURSIVE:
            {
                has_magic = 1;
                break;
            }

            case INTERNAL_SEPARATOR:
            {
                break;
            }

            default:
            {
                clean_path.push_back(c);
                break;
            }

        }

    }

    if (! has_magic && ! clean_path.empty())
    {
        /* Don't test the same path multiple times, which can happen if the path is absolute and the CDPATH contains multiple entries */
        std::set<wcstring> checked_paths;

        /* Keep a cache of which paths / filesystems are case sensitive */
        case_sensitivity_cache_t case_sensitivity_cache;

        for (size_t wd_idx = 0; wd_idx < directories.size() && ! result; wd_idx++)
        {
            const wcstring &wd = directories.at(wd_idx);

            const wcstring abs_path = apply_working_directory(clean_path, wd);

            /* Skip this if it's empty or we've already checked it */
            if (abs_path.empty() || checked_paths.count(abs_path))
                continue;
            checked_paths.insert(abs_path);

            /* If we end with a slash, then it must be a directory */
            bool must_be_full_dir = abs_path.at(abs_path.size()-1) == L'/';
            if (must_be_full_dir)
            {
                struct stat buf;
                if (0 == wstat(abs_path, &buf) && S_ISDIR(buf.st_mode))
                {
                    result = true;
                    /* Return the path suffix, not the whole absolute path */
                    if (out_path)
                        *out_path = clean_path;
                }
            }
            else
            {
                DIR *dir = NULL;

                /* We do not end with a slash; it does not have to be a directory */
                const wcstring dir_name = wdirname(abs_path);
                const wcstring base_name = wbasename(abs_path);
                if (dir_name == L"/" && base_name == L"/")
                {
                    result = true;
                    if (out_path)
                        *out_path = clean_path;
                }
                else if ((dir = wopendir(dir_name)))
                {
                    // We opened the dir_name; look for a string where the base name prefixes it
                    wcstring ent;

                    // Check if we're case insensitive
                    bool case_insensitive = fs_is_case_insensitive(dir_name, dirfd(dir), case_sensitivity_cache);

                    // Don't ask for the is_dir value unless we care, because it can cause extra filesystem acces */
                    bool is_dir = false;
                    while (wreaddir_resolving(dir, dir_name, ent, require_dir ? &is_dir : NULL))
                    {

                        /* Determine which function to call to check for prefixes */
                        bool (*prefix_func)(const wcstring &, const wcstring &);
                        if (case_insensitive)
                        {
                            prefix_func = string_prefixes_string_case_insensitive;
                        }
                        else
                        {
                            prefix_func = string_prefixes_string;
                        }

                        if (prefix_func(base_name, ent) && (! require_dir || is_dir))
                        {
                            result = true;
                            if (out_path)
                            {
                                /* We want to return the path in the same "form" as it was given. Take the given path, get its basename. Append that to the output if the basename actually prefixes the path (which it won't if the given path contains no slashes), and isn't a slash (so we don't duplicate slashes). Then append the directory entry. */

                                out_path->clear();
                                const wcstring path_base = wdirname(const_path);


                                if (prefix_func(path_base, const_path))
                                {
                                    out_path->append(path_base);
                                    if (! string_suffixes_string(L"/", *out_path))
                                        out_path->push_back(L'/');
                                }
                                out_path->append(ent);
                                /* We actually do want a trailing / for directories, since it makes autosuggestion a bit nicer */
                                if (is_dir)
                                    out_path->push_back(L'/');
                            }
                            break;
                        }
                    }
                    closedir(dir);
                }
            }
        }
    }
    return result;
}


/* Given a string, return whether it prefixes a path that we could cd into. Return that path in out_path. Expects path to be unescaped. */
static bool is_potential_cd_path(const wcstring &path, const wcstring &working_directory, path_flags_t flags, wcstring *out_path)
{
    wcstring_list_t directories;

    if (string_prefixes_string(L"./", path))
    {
        /* Ignore the CDPATH in this case; just use the working directory */
        directories.push_back(working_directory);
    }
    else
    {
        /* Get the CDPATH */
        env_var_t cdpath = env_get_string(L"CDPATH");
        if (cdpath.missing_or_empty())
            cdpath = L".";

        /* Tokenize it into directories */
        wcstokenizer tokenizer(cdpath, ARRAY_SEP_STR);
        wcstring next_path;
        while (tokenizer.next(next_path))
        {
            /* Ensure that we use the working directory for relative cdpaths like "." */
            directories.push_back(apply_working_directory(next_path, working_directory));
        }
    }

    /* Call is_potential_path with all of these directories */
    bool result = is_potential_path(path, directories, flags | PATH_REQUIRE_DIR, out_path);
#if 0
    if (out_path)
    {
        printf("%ls -> %ls\n", path.c_str(), out_path->c_str());
    }
#endif
    return result;
}

rgb_color_t highlight_get_color(int highlight, bool is_background)
{
    size_t idx=0;
    rgb_color_t result;

    if (highlight < 0)
        return rgb_color_t::normal();
    if (highlight > (1<<VAR_COUNT))
        return rgb_color_t::normal();
    for (size_t i=0; i<VAR_COUNT; i++)
    {
        if (highlight & (1<<i))
        {
            idx = i;
            break;
        }
    }

    env_var_t val_wstr = env_get_string(highlight_var[idx]);

//  debug( 1, L"%d -> %d -> %ls", highlight, idx, val );

    if (val_wstr.missing())
        val_wstr = env_get_string(highlight_var[0]);

    if (! val_wstr.missing())
        result = parse_color(val_wstr, is_background);

    if (highlight & HIGHLIGHT_VALID_PATH)
    {
        env_var_t val2_wstr =  env_get_string(L"fish_color_valid_path");
        const wcstring val2 = val2_wstr.missing() ? L"" : val2_wstr.c_str();

        rgb_color_t result2 = parse_color(val2, is_background);
        if (result.is_normal())
            result = result2;
        else
        {
            if (result2.is_bold())
                result.set_bold(true);
            if (result2.is_underline())
                result.set_underline(true);
        }
    }
    return result;
}


/**
   Highlight operators (such as $, ~, %, as well as escaped characters.
*/
static void highlight_param(const wcstring &buffstr, std::vector<int> &colors, wcstring_list_t *error)
{
    const wchar_t * const buff = buffstr.c_str();
    enum {e_unquoted, e_single_quoted, e_double_quoted} mode = e_unquoted;
    size_t in_pos, len = buffstr.size();
    int bracket_count=0;
    int normal_status = colors.at(0);

    for (in_pos=0; in_pos<len; in_pos++)
    {
        wchar_t c = buffstr.at(in_pos);
        switch (mode)
        {
                /*
                 Mode 0 means unquoted string
                 */
            case e_unquoted:
            {
                if (c == L'\\')
                {
                    size_t start_pos = in_pos;
                    in_pos++;

                    if (wcschr(L"~%", buff[in_pos]))
                    {
                        if (in_pos == 1)
                        {
                            colors.at(start_pos) = HIGHLIGHT_ESCAPE;
                            colors.at(in_pos+1) = normal_status;
                        }
                    }
                    else if (buff[in_pos]==L',')
                    {
                        if (bracket_count)
                        {
                            colors.at(start_pos) = HIGHLIGHT_ESCAPE;
                            colors.at(in_pos+1) = normal_status;
                        }
                    }
                    else if (wcschr(L"abefnrtv*?$(){}[]'\"<>^ \\#;|&", buff[in_pos]))
                    {
                        colors.at(start_pos)=HIGHLIGHT_ESCAPE;
                        colors.at(in_pos+1)=normal_status;
                    }
                    else if (wcschr(L"c", buff[in_pos]))
                    {
                        colors.at(start_pos)=HIGHLIGHT_ESCAPE;
                        if (in_pos+2 < colors.size())
                            colors.at(in_pos+2)=normal_status;
                    }
                    else if (wcschr(L"uUxX01234567", buff[in_pos]))
                    {
                        int i;
                        long long res=0;
                        int chars=2;
                        int base=16;

                        wchar_t max_val = ASCII_MAX;

                        switch (buff[in_pos])
                        {
                            case L'u':
                            {
                                chars=4;
                                max_val = UCS2_MAX;
                                break;
                            }

                            case L'U':
                            {
                                chars=8;
                                max_val = WCHAR_MAX;
                                break;
                            }

                            case L'x':
                            {
                                break;
                            }

                            case L'X':
                            {
                                max_val = BYTE_MAX;
                                break;
                            }

                            default:
                            {
                                base=8;
                                chars=3;
                                in_pos--;
                                break;
                            }
                        }

                        for (i=0; i<chars; i++)
                        {
                            long d = convert_digit(buff[++in_pos],base);

                            if (d < 0)
                            {
                                in_pos--;
                                break;
                            }

                            res=(res*base)|d;
                        }

                        if ((res <= max_val))
                        {
                            colors.at(start_pos) = HIGHLIGHT_ESCAPE;
                            colors.at(in_pos+1) = normal_status;
                        }
                        else
                        {
                            colors.at(start_pos) = HIGHLIGHT_ERROR;
                            colors.at(in_pos+1) = normal_status;
                        }
                    }

                }
                else
                {
                    switch (buff[in_pos])
                    {
                        case L'~':
                        case L'%':
                        {
                            if (in_pos == 0)
                            {
                                colors.at(in_pos) = HIGHLIGHT_OPERATOR;
                                colors.at(in_pos+1) = normal_status;
                            }
                            break;
                        }

                        case L'$':
                        {
                            wchar_t n = buff[in_pos+1];
                            colors.at(in_pos) = (n==L'$'||wcsvarchr(n))? HIGHLIGHT_OPERATOR:HIGHLIGHT_ERROR;
                            colors.at(in_pos+1) = normal_status;
                            break;
                        }


                        case L'*':
                        case L'?':
                        case L'(':
                        case L')':
                        {
                            colors.at(in_pos) = HIGHLIGHT_OPERATOR;
                            colors.at(in_pos+1) = normal_status;
                            break;
                        }

                        case L'{':
                        {
                            colors.at(in_pos) = HIGHLIGHT_OPERATOR;
                            colors.at(in_pos+1) = normal_status;
                            bracket_count++;
                            break;
                        }

                        case L'}':
                        {
                            colors.at(in_pos) = HIGHLIGHT_OPERATOR;
                            colors.at(in_pos+1) = normal_status;
                            bracket_count--;
                            break;
                        }

                        case L',':
                        {
                            if (bracket_count)
                            {
                                colors.at(in_pos) = HIGHLIGHT_OPERATOR;
                                colors.at(in_pos+1) = normal_status;
                            }

                            break;
                        }

                        case L'\'':
                        {
                            colors.at(in_pos) = HIGHLIGHT_QUOTE;
                            mode = e_single_quoted;
                            break;
                        }

                        case L'\"':
                        {
                            colors.at(in_pos) = HIGHLIGHT_QUOTE;
                            mode = e_double_quoted;
                            break;
                        }

                    }
                }
                break;
            }

            /*
             Mode 1 means single quoted string, i.e 'foo'
             */
            case e_single_quoted:
            {
                if (c == L'\\')
                {
                    size_t start_pos = in_pos;
                    switch (buff[++in_pos])
                    {
                        case '\\':
                        case L'\'':
                        {
                            colors.at(start_pos) = HIGHLIGHT_ESCAPE;
                            colors.at(in_pos+1) = HIGHLIGHT_QUOTE;
                            break;
                        }

                        case 0:
                        {
                            return;
                        }

                    }

                }
                if (c == L'\'')
                {
                    mode = e_unquoted;
                    colors.at(in_pos+1) = normal_status;
                }

                break;
            }

            /*
             Mode 2 means double quoted string, i.e. "foo"
             */
            case e_double_quoted:
            {
                switch (c)
                {
                    case '"':
                    {
                        mode = e_unquoted;
                        colors.at(in_pos+1) = normal_status;
                        break;
                    }

                    case '\\':
                    {
                        size_t start_pos = in_pos;
                        switch (buff[++in_pos])
                        {
                            case L'\0':
                            {
                                return;
                            }

                            case '\\':
                            case L'$':
                            case '"':
                            {
                                colors.at(start_pos) = HIGHLIGHT_ESCAPE;
                                colors.at(in_pos+1) = HIGHLIGHT_QUOTE;
                                break;
                            }
                        }
                        break;
                    }

                    case '$':
                    {
                        wchar_t n = buff[in_pos+1];
                        colors.at(in_pos) = (n==L'$'||wcsvarchr(n))? HIGHLIGHT_OPERATOR:HIGHLIGHT_ERROR;
                        colors.at(in_pos+1) = HIGHLIGHT_QUOTE;
                        break;
                    }

                }
                break;
            }
        }
    }
}

static int has_expand_reserved(const wchar_t *str)
{
    while (*str)
    {
        if (*str >= EXPAND_RESERVED &&
                *str <= EXPAND_RESERVED_END)
        {
            return 1;
        }
        str++;
    }
    return 0;
}

/* Parse a command line. Return by reference the last command, its arguments, and the offset in the string of the beginning of the last argument. This is used by autosuggestions */
static bool autosuggest_parse_command(const wcstring &str, wcstring *out_command, wcstring_list_t *out_arguments, int *out_last_arg_pos)
{
    if (str.empty())
        return false;

    wcstring cmd;
    wcstring_list_t args;
    int arg_pos = -1;

    bool had_cmd = false;
    tokenizer_t tok(str.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS);
    for (; tok_has_next(&tok); tok_next(&tok))
    {
        int last_type = tok_last_type(&tok);

        switch (last_type)
        {
            case TOK_STRING:
            {
                if (had_cmd)
                {
                    /* Parameter to the command. We store these escaped. */
                    args.push_back(tok_last(&tok));
                    arg_pos = tok_get_pos(&tok);
                }
                else
                {
                    /* Command. First check that the command actually exists. */
                    wcstring local_cmd = tok_last(&tok);
                    bool expanded = expand_one(cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES);
                    if (! expanded || has_expand_reserved(cmd.c_str()))
                    {
                        /* We can't expand this cmd, ignore it */
                    }
                    else
                    {
                        bool is_subcommand = false;
                        int mark = tok_get_pos(&tok);

                        if (parser_keywords_is_subcommand(cmd))
                        {
                            int sw;
                            tok_next(&tok);

                            sw = parser_keywords_is_switch(tok_last(&tok));
                            if (!parser_keywords_is_block(cmd) &&
                                    sw == ARG_SWITCH)
                            {
                                /* It's an argument to the subcommand itself */
                            }
                            else
                            {
                                if (sw == ARG_SKIP)
                                    mark = tok_get_pos(&tok);
                                is_subcommand = true;
                            }
                            tok_set_pos(&tok, mark);
                        }

                        if (!is_subcommand)
                        {
                            /* It's really a command */
                            had_cmd = true;
                            cmd = local_cmd;
                        }
                    }

                }
                break;
            }

            case TOK_REDIRECT_NOCLOB:
            case TOK_REDIRECT_OUT:
            case TOK_REDIRECT_IN:
            case TOK_REDIRECT_APPEND:
            case TOK_REDIRECT_FD:
            {
                if (!had_cmd)
                {
                    break;
                }
                tok_next(&tok);
                break;
            }

            case TOK_PIPE:
            case TOK_BACKGROUND:
            case TOK_END:
            {
                had_cmd = false;
                cmd.clear();
                args.clear();
                arg_pos = -1;
                break;
            }

            case TOK_COMMENT:
            case TOK_ERROR:
            default:
            {
                break;
            }
        }
    }

    /* Remember our command if we have one */
    if (had_cmd)
    {
        if (out_command) out_command->swap(cmd);
        if (out_arguments) out_arguments->swap(args);
        if (out_last_arg_pos) *out_last_arg_pos = arg_pos;
    }
    return had_cmd;
}


/* We have to return an escaped string here */
bool autosuggest_suggest_special(const wcstring &str, const wcstring &working_directory, wcstring &outSuggestion)
{
    if (str.empty())
        return false;

    ASSERT_IS_BACKGROUND_THREAD();

    /* Parse the string */
    wcstring parsed_command;
    wcstring_list_t parsed_arguments;
    int parsed_last_arg_pos = -1;
    if (! autosuggest_parse_command(str, &parsed_command, &parsed_arguments, &parsed_last_arg_pos))
    {
        return false;
    }

    bool result = false;
    if (parsed_command == L"cd" && ! parsed_arguments.empty())
    {
        /* We can possibly handle this specially */
        const wcstring escaped_dir = parsed_arguments.back();
        wcstring suggested_path;

        /* We always return true because we recognized the command. This prevents us from falling back to dumber algorithms; for example we won't suggest a non-directory for the cd command. */
        result = true;
        outSuggestion.clear();

        /* Unescape the parameter */
        wcstring unescaped_dir = escaped_dir;
        bool unescaped = unescape_string(unescaped_dir, UNESCAPE_INCOMPLETE);

        /* Determine the quote type we got from the input directory. */
        wchar_t quote = L'\0';
        parse_util_get_parameter_info(escaped_dir, 0, &quote, NULL, NULL);

        /* Big hack to avoid expanding a tilde inside quotes */
        path_flags_t path_flags = (quote == L'\0') ? PATH_EXPAND_TILDE : 0;
        if (unescaped && is_potential_cd_path(unescaped_dir, working_directory, path_flags, &suggested_path))
        {

            /* Note: this looks really wrong for strings that have an "unescapable" character in them, e.g. a \t, because parse_util_escape_string_with_quote will insert that character */
            wcstring escaped_suggested_path = parse_util_escape_string_with_quote(suggested_path, quote);

            /* Return it */
            outSuggestion = str;
            outSuggestion.erase(parsed_last_arg_pos);
            if (quote != L'\0') outSuggestion.push_back(quote);
            outSuggestion.append(escaped_suggested_path);
            if (quote != L'\0') outSuggestion.push_back(quote);
        }
    }
    else
    {
        /* Either an error or some other command, so we don't handle it specially */
    }
    return result;
}

bool autosuggest_validate_from_history(const history_item_t &item, file_detection_context_t &detector, const wcstring &working_directory, const env_vars_snapshot_t &vars)
{
    ASSERT_IS_BACKGROUND_THREAD();

    bool handled = false, suggestionOK = false;

    /* Parse the string */
    wcstring parsed_command;
    wcstring_list_t parsed_arguments;
    int parsed_last_arg_pos = -1;
    if (! autosuggest_parse_command(item.str(), &parsed_command, &parsed_arguments, &parsed_last_arg_pos))
        return false;

    if (parsed_command == L"cd" && ! parsed_arguments.empty())
    {
        /* We can possibly handle this specially */
        wcstring dir = parsed_arguments.back();
        if (expand_one(dir, EXPAND_SKIP_CMDSUBST))
        {
            handled = true;
            bool is_help = string_prefixes_string(dir, L"--help") || string_prefixes_string(dir, L"-h");
            if (is_help)
            {
                suggestionOK = false;
            }
            else
            {
                wcstring path;
                bool can_cd = path_get_cdpath(dir, &path, working_directory.c_str(), vars);
                if (! can_cd)
                {
                    suggestionOK = false;
                }
                else if (paths_are_same_file(working_directory, path))
                {
                    /* Don't suggest the working directory as the path! */
                    suggestionOK = false;
                }
                else
                {
                    suggestionOK = true;
                }
            }
        }
    }

    /* If not handled specially, handle it here */
    if (! handled)
    {
        bool cmd_ok = false;

        if (path_get_path(parsed_command, NULL))
        {
            cmd_ok = true;
        }
        else if (builtin_exists(parsed_command) || function_exists_no_autoload(parsed_command, vars))
        {
            cmd_ok = true;
        }

        if (cmd_ok)
        {
            const path_list_t &paths = item.get_required_paths();
            if (paths.empty())
            {
                suggestionOK= true;
            }
            else
            {
                detector.potential_paths = paths;
                suggestionOK = detector.paths_are_valid(paths);
            }
        }
    }

    return suggestionOK;
}

// This function does I/O
static void tokenize(const wchar_t * const buff, std::vector<int> &color, const size_t pos, wcstring_list_t *error, const wcstring &working_directory, const env_vars_snapshot_t &vars)
{
    ASSERT_IS_BACKGROUND_THREAD();

    wcstring cmd;
    int had_cmd=0;
    wcstring last_cmd;

    int accept_switches = 1;

    int use_function = 1;
    int use_command = 1;
    int use_builtin = 1;

    CHECK(buff,);

    if (buff[0] == L'\0')
        return;

    std::fill(color.begin(), color.end(), -1);

    tokenizer_t tok(buff, TOK_SHOW_COMMENTS | TOK_SQUASH_ERRORS);
    for (; tok_has_next(&tok); tok_next(&tok))
    {
        int last_type = tok_last_type(&tok);

        switch (last_type)
        {
            case TOK_STRING:
            {
                if (had_cmd)
                {

                    /*Parameter */
                    const wchar_t *param = tok_last(&tok);
                    if (param[0] == L'-')
                    {
                        if (wcscmp(param, L"--") == 0)
                        {
                            accept_switches = 0;
                            color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM;
                        }
                        else if (accept_switches)
                        {
                            if (complete_is_valid_option(last_cmd, param, error, false /* no autoload */))
                                color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM;
                            else
                                color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                        }
                        else
                        {
                            color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM;
                        }
                    }
                    else
                    {
                        color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM;
                    }

                    if (cmd == L"cd")
                    {
                        wcstring dir = tok_last(&tok);
                        if (expand_one(dir, EXPAND_SKIP_CMDSUBST))
                        {
                            int is_help = string_prefixes_string(dir, L"--help") || string_prefixes_string(dir, L"-h");
                            if (!is_help && ! is_potential_cd_path(dir, working_directory, PATH_EXPAND_TILDE, NULL))
                            {
                                color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                            }
                        }
                    }

                    /* Highlight the parameter. highlight_param wants to write one more color than we have characters (hysterical raisins) so allocate one more in the vector. But don't copy it back. */
                    const wcstring param_str = param;
                    size_t tok_pos = tok_get_pos(&tok);

                    std::vector<int>::const_iterator where = color.begin() + tok_pos;
                    std::vector<int> subcolors(where, where + param_str.size());
                    subcolors.push_back(-1);
                    highlight_param(param_str, subcolors, error);

                    /* Copy the subcolors back into our colors array */
                    std::copy(subcolors.begin(), subcolors.begin() + param_str.size(), color.begin() + tok_pos);
                }
                else
                {
                    /*
                     Command. First check that the command actually exists.
                     */
                    cmd = tok_last(&tok);
                    bool expanded = expand_one(cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES | EXPAND_SKIP_JOBS);
                    if (! expanded || has_expand_reserved(cmd.c_str()))
                    {
                        color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                    }
                    else
                    {
                        bool is_cmd = false;
                        int is_subcommand = 0;
                        int mark = tok_get_pos(&tok);
                        color.at(tok_get_pos(&tok)) = use_builtin ? HIGHLIGHT_COMMAND : HIGHLIGHT_ERROR;

                        if (parser_keywords_is_subcommand(cmd))
                        {

                            int sw;

                            if (cmd == L"builtin")
                            {
                                use_function = 0;
                                use_command  = 0;
                                use_builtin  = 1;
                            }
                            else if (cmd == L"command" || cmd == L"exec")
                            {
                                use_command  = 1;
                                use_function = 0;
                                use_builtin  = 0;
                            }

                            tok_next(&tok);

                            sw = parser_keywords_is_switch(tok_last(&tok));

                            if (!parser_keywords_is_block(cmd) &&
                                    sw == ARG_SWITCH)
                            {
                                /*
                                 The 'builtin' and 'command' builtins
                                 are normally followed by another
                                 command, but if they are invoked
                                 with a switch, they aren't.

                                 */
                                use_command  = 1;
                                use_function = 1;
                                use_builtin  = 2;
                            }
                            else
                            {
                                if (sw == ARG_SKIP)
                                {
                                    color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM;
                                    mark = tok_get_pos(&tok);
                                }

                                is_subcommand = 1;
                            }
                            tok_set_pos(&tok, mark);
                        }

                        if (!is_subcommand)
                        {
                            /*
                             OK, this is a command, it has been
                             successfully expanded and everything
                             looks ok. Lets check if the command
                             exists.
                             */

                            /*
                             First check if it is a builtin or
                             function, since we don't have to stat
                             any files for that
                             */
                            if (! is_cmd && use_builtin)
                                is_cmd = builtin_exists(cmd);

                            if (! is_cmd && use_function)
                                is_cmd = function_exists_no_autoload(cmd, vars);

                            if (! is_cmd)
                                is_cmd = expand_abbreviation(cmd, NULL);

                            /*
                             Moving on to expensive tests
                             */

                            /*
                             Check if this is a regular command
                             */
                            if (! is_cmd && use_command)
                            {
                                is_cmd = path_get_path(cmd, NULL, vars);
                            }

                            /* Maybe it is a path for a implicit cd command. */
                            if (! is_cmd)
                            {
                                if (use_builtin || (use_function && function_exists_no_autoload(L"cd", vars)))
                                    is_cmd = path_can_be_implicit_cd(cmd, NULL, working_directory.c_str(), vars);
                            }

                            if (is_cmd)
                            {
                                color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMAND;
                            }
                            else
                            {
                                if (error)
                                {
                                    error->push_back(format_string(L"Unknown command \'%ls\'", cmd.c_str()));
                                }
                                color.at(tok_get_pos(&tok)) = (HIGHLIGHT_ERROR);
                            }
                            had_cmd = 1;
                        }

                        if (had_cmd)
                        {
                            last_cmd = tok_last(&tok);
                        }
                    }

                }
                break;
            }

            case TOK_REDIRECT_NOCLOB:
            case TOK_REDIRECT_OUT:
            case TOK_REDIRECT_IN:
            case TOK_REDIRECT_APPEND:
            case TOK_REDIRECT_FD:
            {
                if (!had_cmd)
                {
                    color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                    if (error)
                        error->push_back(L"Redirection without a command");
                    break;
                }

                wcstring target_str;
                const wchar_t *target=NULL;

                color.at(tok_get_pos(&tok)) = HIGHLIGHT_REDIRECTION;
                tok_next(&tok);

                /*
                 Check that we are redirecting into a file
                 */

                switch (tok_last_type(&tok))
                {
                    case TOK_STRING:
                    {
                        target_str = tok_last(&tok);
                        if (expand_one(target_str, EXPAND_SKIP_CMDSUBST))
                        {
                            target = target_str.c_str();
                        }
                        /*
                         Redirect filename may contain a cmdsubst.
                         If so, it will be ignored/not flagged.
                         */
                    }
                    break;
                    default:
                    {
                        size_t pos = tok_get_pos(&tok);
                        if (pos < color.size())
                        {
                            color.at(pos) = HIGHLIGHT_ERROR;
                        }
                        if (error)
                            error->push_back(L"Invalid redirection");
                    }

                }

                if (target != 0)
                {
                    wcstring dir = target;
                    size_t slash_idx = dir.find_last_of(L'/');
                    struct stat buff;
                    /*
                     If file is in directory other than '.', check
                     that the directory exists.
                     */
                    if (slash_idx != wcstring::npos)
                    {
                        dir.resize(slash_idx);
                        if (wstat(dir, &buff) == -1)
                        {
                            color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                            if (error)
                                error->push_back(format_string(L"Directory \'%ls\' does not exist", dir.c_str()));

                        }
                    }

                    /*
                     If the file is read from or appended to, check
                     if it exists.
                     */
                    if (last_type == TOK_REDIRECT_IN ||
                            last_type == TOK_REDIRECT_APPEND)
                    {
                        if (wstat(target, &buff) == -1)
                        {
                            color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                            if (error)
                                error->push_back(format_string(L"File \'%ls\' does not exist", target));
                        }
                    }
                    if (last_type == TOK_REDIRECT_NOCLOB)
                    {
                        if (wstat(target, &buff) != -1)
                        {
                            color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                            if (error)
                                error->push_back(format_string(L"File \'%ls\' exists", target));
                        }
                    }
                }
                break;
            }

            case TOK_PIPE:
            case TOK_BACKGROUND:
            {
                if (had_cmd)
                {
                    color.at(tok_get_pos(&tok)) = HIGHLIGHT_END;
                    had_cmd = 0;
                    use_command  = 1;
                    use_function = 1;
                    use_builtin  = 1;
                    accept_switches = 1;
                }
                else
                {
                    color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                    if (error)
                        error->push_back(L"No job to put in background");
                }

                break;
            }

            case TOK_END:
            {
                color.at(tok_get_pos(&tok)) = HIGHLIGHT_END;
                had_cmd = 0;
                use_command  = 1;
                use_function = 1;
                use_builtin  = 1;
                accept_switches = 1;
                break;
            }

            case TOK_COMMENT:
            {
                color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMENT;
                break;
            }

            case TOK_ERROR:
            default:
            {
                /*
                 If the tokenizer reports an error, highlight it as such.
                 */
                if (error)
                    error->push_back(tok_last(&tok));
                color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR;
                break;
            }
        }
    }
}


// PCA This function does I/O, (calls is_potential_path, path_get_path, maybe others) and so ought to only run on a background thread
void highlight_shell(const wcstring &buff, std::vector<int> &color, size_t pos, wcstring_list_t *error, const env_vars_snapshot_t &vars)
{
    ASSERT_IS_BACKGROUND_THREAD();

    const size_t length = buff.size();
    assert(buff.size() == color.size());


    if (length == 0)
        return;

    std::fill(color.begin(), color.end(), -1);

    /* Do something sucky and get the current working directory on this background thread. This should really be passed in. */
    const wcstring working_directory = env_get_pwd_slash();

    /* Tokenize the string */
    tokenize(buff.c_str(), color, pos, error, working_directory, vars);

    /* Locate and syntax highlight cmdsubsts recursively */

    wchar_t * const subbuff = wcsdup(buff.c_str());
    wchar_t * subpos = subbuff;
    bool done = false;

    while (1)
    {
        wchar_t *begin, *end;

        if (parse_util_locate_cmdsubst(subpos, &begin, &end, true) <= 0)
        {
            break;
        }

        /* Note: This *end = 0 writes into subbuff! */
        if (!*end)
            done = true;
        else
            *end = 0;

        //our subcolors start at color + (begin-subbuff)+1
        size_t start = begin - subbuff + 1, len = wcslen(begin + 1);
        std::vector<int> subcolors(len, -1);

        highlight_shell(begin+1, subcolors, -1, error, vars);

        // insert subcolors
        std::copy(subcolors.begin(), subcolors.end(), color.begin() + start);

        // highlight the end of the subcommand
        assert(end >= subbuff);
        if ((size_t)(end - subbuff) < length)
        {
            color.at(end-subbuff)=HIGHLIGHT_OPERATOR;
        }

        if (done)
            break;

        subpos = end+1;
    }
    free(subbuff);

    /*
      The highlighting code only changes the first element when the
      color changes. This fills in the rest.
    */
    int last_val=0;
    for (size_t i=0; i < buff.size(); i++)
    {
        int &current_val = color.at(i);
        if (current_val >= 0)
        {
            last_val = current_val;
        }
        else
        {
            current_val = last_val; //note - this writes into the vector
        }
    }

    /*
      Color potentially valid paths in a special path color if they
      are the current token.
        For reasons that I don't yet understand, it's required that pos be allowed to be length (e.g. when backspacing).
    */
    if (pos <= length)
    {

        const wchar_t *cbuff = buff.c_str();
        const wchar_t *tok_begin, *tok_end;
        parse_util_token_extent(cbuff, pos, &tok_begin, &tok_end, 0, 0);
        if (tok_begin && tok_end)
        {
            wcstring token(tok_begin, tok_end-tok_begin);
            const wcstring_list_t working_directory_list(1, working_directory);
            if (unescape_string(token, 1))
            {
                /* Big hack: is_potential_path expects a tilde, but unescape_string gives us HOME_DIRECTORY. Put it back. */
                if (! token.empty() && token.at(0) == HOME_DIRECTORY)
                    token.at(0) = L'~';
                if (is_potential_path(token, working_directory_list, PATH_EXPAND_TILDE))
                {
                    for (ptrdiff_t i=tok_begin-cbuff; i < (tok_end-cbuff); i++)
                    {
                        // Don't color HIGHLIGHT_ERROR because it looks dorky. For example, trying to cd into a non-directory would show an underline and also red.
                        if (!(color.at(i) & HIGHLIGHT_ERROR))
                        {
                            color.at(i) |= HIGHLIGHT_VALID_PATH;
                        }
                    }
                }
            }
        }
    }


    highlight_universal_internal(buff, color, pos);

    /*
      Spaces should not be highlighted at all, since it makes cursor look funky in some terminals
    */
    for (size_t i=0; i < buff.size(); i++)
    {
        if (iswspace(buff.at(i)))
        {
            color.at(i)=0;
        }
    }
}



/**
   Perform quote and parenthesis highlighting on the specified string.
*/
static void highlight_universal_internal(const wcstring &buffstr, std::vector<int> &color, size_t pos)
{
    assert(buffstr.size() == color.size());
    if (pos < buffstr.size())
    {

        /*
          Highlight matching quotes
        */
        if ((buffstr.at(pos) == L'\'') || (buffstr.at(pos) == L'\"'))
        {
            std::vector<size_t> lst;

            int level=0;
            wchar_t prev_q=0;

            const wchar_t * const buff = buffstr.c_str();
            const wchar_t *str = buff;

            int match_found=0;

            while (*str)
            {
                switch (*str)
                {
                    case L'\\':
                        str++;
                        break;
                    case L'\"':
                    case L'\'':
                        if (level == 0)
                        {
                            level++;
                            lst.push_back(str-buff);
                            prev_q = *str;
                        }
                        else
                        {
                            if (prev_q == *str)
                            {
                                size_t pos1, pos2;

                                level--;
                                pos1 = lst.back();
                                pos2 = str-buff;
                                if (pos1==pos || pos2==pos)
                                {
                                    color.at(pos1)|=HIGHLIGHT_MATCH<<16;
                                    color.at(pos2)|=HIGHLIGHT_MATCH<<16;
                                    match_found = 1;

                                }
                                prev_q = *str==L'\"'?L'\'':L'\"';
                            }
                            else
                            {
                                level++;
                                lst.push_back(str-buff);
                                prev_q = *str;
                            }
                        }

                        break;
                }
                if ((*str == L'\0'))
                    break;

                str++;
            }

            if (!match_found)
                color.at(pos) = HIGHLIGHT_ERROR<<16;
        }

        /*
          Highlight matching parenthesis
        */
        const wchar_t c = buffstr.at(pos);
        if (wcschr(L"()[]{}", c))
        {
            int step = wcschr(L"({[", c)?1:-1;
            wchar_t dec_char = *(wcschr(L"()[]{}", c) + step);
            wchar_t inc_char = c;
            int level = 0;
            int match_found=0;
            for (long i=pos; i >= 0 && (size_t)i < buffstr.size(); i+=step)
            {
                const wchar_t test_char = buffstr.at(i);
                if (test_char == inc_char)
                    level++;
                if (test_char == dec_char)
                    level--;
                if (level == 0)
                {
                    long pos2 = i;
                    color.at(pos)|=HIGHLIGHT_MATCH<<16;
                    color.at(pos2)|=HIGHLIGHT_MATCH<<16;
                    match_found=1;
                    break;
                }
            }

            if (!match_found)
                color[pos] = HIGHLIGHT_ERROR<<16;
        }
    }
}

void highlight_universal(const wcstring &buff, std::vector<int> &color, size_t pos, wcstring_list_t *error, const env_vars_snapshot_t &vars)
{
    assert(buff.size() == color.size());
    std::fill(color.begin(), color.end(), 0);
    highlight_universal_internal(buff, color, pos);
}