aboutsummaryrefslogtreecommitdiff
path: root/.vim/autoload/neomake.vim
blob: 6322a05637c071f4a8d13bd4ec8237adcfaf8267 (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
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
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
" vim: ts=4 sw=4 et
scriptencoding utf-8

if !exists('s:make_id')
    let s:make_id = 0
endif
" A map of make_id to options, e.g. cwd when jobs where started.
if !exists('s:make_info')
    let s:make_info = {}
endif
if !exists('s:job_id')
    let s:job_id = 1
endif
if !exists('s:jobs')
    let s:jobs = {}
endif
if !exists('s:map_job_ids')
    let s:map_job_ids = {}
endif

" Errors by [maker_type][bufnr][lnum]
let s:current_errors = {'project': {}, 'file': {}}

if !has('nvim')
    let s:kill_vim_timers = {}
endif

" A list of references to keep when profiling.
" Workaround for https://github.com/vim/vim/issues/2350, where
" https://github.com/blueyed/vader.vim/commit/e66d91dea is not enough.
let s:hack_keep_refs_for_profiling = []

" Can Neovim buffer output?
let s:nvim_can_buffer_output = has('nvim-0.3.0') ? 1 : 0

" Private function to access script-local variables during tests.
function! neomake#_get_s() abort
    return s:
endfunction

" Sentinels.
let s:unset_list = []
let s:unset_dict = {}
let s:unset = {}

let s:can_use_env_in_job_opts = has('patch-8.0.0902') && has('patch-8.0.1832')

let s:is_testing = exists('g:neomake_test_messages')

let s:async = has('nvim')
            \ || has('channel') && has('job') && has('patch-8.0.0027')
function! neomake#has_async_support() abort
    return s:async
endfunction

if v:version >= 704 || (v:version == 703 && has('patch1058'))
    function! s:function(name) abort
        return function(a:name)
    endfunction
else
    " Older Vim does not handle s: function references across files.
    function! s:function(name) abort
        return function(substitute(a:name,'^s:',matchstr(expand('<sfile>'), '.*\zs<SNR>\d\+_'),''))
    endfunction
endif

function! s:sort_jobs(a, b) abort
    return a:a.id - a:b.id
endfunction

function! neomake#GetJobs(...) abort
    if empty(s:jobs)
        return []
    endif
    let jobs = copy(values(s:jobs))
    if a:0
        call filter(jobs, 'index(a:1, v:val.id) != -1')
    endif
    return sort(jobs, function('s:sort_jobs'))
endfunction

function! neomake#GetJob(job_id) abort
    return s:jobs[a:job_id]
endfunction

" Not documented, only used in tests for now.
function! neomake#GetStatus() abort
    return {
                \ 'last_make_id': s:make_id,
                \ 'make_info': s:make_info,
                \ 'action_queue': g:neomake#action_queue#_s.action_queue,
                \ }
endfunction

" neomake#GetMakeOptions: not documented, only used internally for now.
" More lax when not being used in tests to avoid errors, but fail during tests.
if s:is_testing
    function! neomake#GetMakeOptions(...) abort
        let make_id = a:0 ? a:1 : s:make_id
        try
            let r = s:make_info[make_id]
        catch
            let msg = printf('GetMakeOptions failed: %s (in %s)', v:exception, v:throwpoint)
            call vader#log(msg)
            let g:neomake_test_errors += [msg]
            return {'verbosity': 3}
        endtry
        return r
    endfunction
else
    function! neomake#GetMakeOptions(...) abort
        let make_id = a:0 ? a:1 : s:make_id
        if !has_key(s:make_info, make_id)
            call neomake#log#warning('warning: missing make_info key: '.make_id.'.')
            return {'verbosity': get(g:, 'neomake_verbose', 1)}
        endif
        return s:make_info[make_id]
    endfunction
endif

function! neomake#ListJobs() abort
    if !s:async
        echom 'This Vim version has no support for jobs.'
        return
    endif
    let jobs = neomake#GetJobs()
    if empty(jobs)
        return
    endif
    echom 'make_id | job_id | name/maker'
    for jobinfo in jobs
        let desc = !empty(jobinfo.maker.name) && jobinfo.name != jobinfo.maker.name
                    \ ? jobinfo.name. ' ('.jobinfo.maker.name.')'
                    \ : jobinfo.name
        echom printf('%7d | %6d | %s', jobinfo.make_id, jobinfo.id, desc)
    endfor
endfunction

function! neomake#CancelMake(...) abort
    let make_id = a:0 ? a:1 : s:make_id
    if !has_key(s:make_info, make_id)
        call neomake#log#error('CancelMake: make not found: '.make_id.'.')
        return 0
    endif
    let bang = a:0 > 1 ? a:1 : 0
    let make_info = s:make_info[make_id]
    call neomake#log#debug('Canceling make.', make_info)
    let make_info.canceled = 1
    let jobs = filter(copy(values(s:jobs)), 'v:val.make_id == make_id')
    call s:abort_next_makers(make_id)
    for job in jobs
        call neomake#CancelJob(job.id, bang)
    endfor
    call neomake#action_queue#clean(make_info)
    " Ensure that make info gets cleaned really, e.g. if there were no jobs yet.
    if has_key(s:make_info, make_id)
        call s:clean_make_info(make_info, bang)
    endif
    return 1
endfunction

function! neomake#CancelAllMakes(...) abort
    let bang = a:0 ? a:1 : 0
    for make_id in keys(s:make_info)
        call neomake#CancelMake(make_id, bang)
    endfor
endfunction

" Returns 1 if a job was canceled, 0 otherwise.
function! neomake#CancelJob(job_id, ...) abort
    let job_id = type(a:job_id) == type({}) ? a:job_id.id : +a:job_id
    let remove_always = a:0 ? a:1 : 0
    let jobinfo = get(s:jobs, job_id, {})
    call neomake#log#debug('Canceling job.', jobinfo)

    call neomake#action_queue#clean(empty(jobinfo) ? {'id': job_id} : jobinfo)

    if empty(jobinfo)
        call neomake#log#error('CancelJob: job not found: '.job_id.'.')
        return 0
    endif

    if get(jobinfo, 'canceled', 0)
        call neomake#log#info('Job was canceled already.', jobinfo)
        if remove_always
            call s:CleanJobinfo(jobinfo)
        endif
        return 0
    endif
    let jobinfo.canceled = 1

    let ret = 0
    if get(jobinfo, 'finished')
        call neomake#log#debug('Removing already finished job.', jobinfo)
    elseif has_key(jobinfo, 'exit_code')
        call neomake#log#debug('Job exited already.', jobinfo)
    elseif has_key(jobinfo.maker, 'get_list_entries')
        call neomake#log#debug('Removing job for get_list_entries.', jobinfo)
    elseif s:async
        if has('nvim')
            let job = jobinfo.nvim_job
            call neomake#log#debug(printf('Stopping Neovim job: %s.', job), jobinfo)
        else
            let job = jobinfo.vim_job
            call neomake#log#debug(printf('Stopping Vim job: %s.', job), jobinfo)
        endif
        if has('nvim')
            try
                call jobstop(job)
                let ret = 1
            catch /^Vim\%((\a\+)\)\=:\(E474\|E900\):/
                call neomake#log#info(printf(
                            \ 'jobstop failed: %s.', v:exception), jobinfo)
            endtry
        else
            " Use ch_status here, since job_status might be 'dead' already,
            " without the exit handler being called yet.
            if job_status(job) !=# 'run'
                call neomake#log#info(
                            \ 'job_stop: job was not running anymore.', jobinfo)
            else
                " NOTE: might be "dead" already, but that is fine.
                call job_stop(job)
                let ret = 1
                if job_status(job) ==# 'run'
                    let timer = timer_start(1000, function('s:kill_vimjob_cb'))
                    let s:kill_vim_timers[timer] = jobinfo
                endif
            endif
        endif
    endif

    if ret == 0 || remove_always
        call s:CleanJobinfo(jobinfo)
    endif
    return ret
endfunction

function! s:kill_vimjob_cb(timer) abort
    let jobinfo = s:kill_vim_timers[a:timer]
    let vim_job = jobinfo.vim_job
    if job_status(vim_job) ==# 'run'
        call neomake#log#debug('Forcefully killing still running Vim job.', jobinfo)
        call job_stop(vim_job, 'kill')
    endif
    unlet s:kill_vim_timers[a:timer]
endfunction

function! neomake#CancelJobs(bang) abort
    call neomake#log#debug(printf('Canceling %d jobs.', len(s:jobs)))
    for job in neomake#GetJobs()
        call neomake#CancelJob(job.id, a:bang)
    endfor
endfunction

function! s:handle_get_list_entries(jobinfo, ...) abort
    if !a:0
        return s:pcall('s:handle_get_list_entries', [a:jobinfo])
    endif
    let jobinfo = a:jobinfo
    let jobinfo.serialize = 0
    let maker = jobinfo.maker
    try
        let entries = maker.get_list_entries(jobinfo)
    catch /^\%(Vim\%((\a\+)\)\=:\%(E48\|E523\)\)\@!/  " everything, but E48/E523 (sandbox / not allowed here)
        if v:exception ==# 'NeomakeTestsException'
            throw v:exception
        endif
        call neomake#log#exception(printf(
                    \ 'Error during get_list_entries for %s: %s.',
                    \ jobinfo.maker.name, v:exception), jobinfo)
        call s:CleanJobinfo(jobinfo)
        return g:neomake#action_queue#processed
    endtry

    if type(entries) != type([])
        call neomake#log#error(printf('The get_list_entries method for maker %s did not return a list, but: %s.', jobinfo.maker.name, string(entries)[:100]), jobinfo)
    elseif !empty(entries) && type(entries[0]) != type({})
        call neomake#log#error(printf('The get_list_entries method for maker %s did not return a list of dicts, but: %s.', jobinfo.maker.name, string(entries)[:100]), jobinfo)
    else
        call s:ProcessEntries(jobinfo, entries)
    endif
    call s:CleanJobinfo(jobinfo)
    return g:neomake#action_queue#processed
endfunction

function! s:MakeJob(make_id, options) abort
    let job_id = s:job_id
    let s:job_id += 1

    " Optional:
    "  - serialize (default: 0 for async (and get_list_entries),
    "                        1 for non-async)
    "  - serialize_abort_on_error (default: 0)
    "  - exit_callback (string/function, default: 0)
    let jobinfo = extend(deepcopy(g:neomake#jobinfo#base), extend({
        \ 'id': job_id,
        \ 'make_id': a:make_id,
        \ 'name': empty(get(a:options.maker, 'name', '')) ? 'neomake_'.job_id : a:options.maker.name,
        \ 'maker': a:options.maker,
        \ 'bufnr': a:options.bufnr,
        \ 'file_mode': a:options.file_mode,
        \ 'ft': a:options.ft,
        \ 'cwd': s:make_info[a:make_id].cwd,
        \ }, a:options))

    let maker = jobinfo.maker

    if has_key(maker, 'get_list_entries')
        call neomake#log#info(printf(
                    \ '%s: getting entries via get_list_entries.',
                    \ maker.name), jobinfo)
        let s:jobs[jobinfo.id] = jobinfo
        let s:make_info[a:make_id].active_jobs += [jobinfo]
        call s:handle_get_list_entries(jobinfo)
        return jobinfo
    endif

    call extend(jobinfo, {
        \ 'output_stream': a:options.maker.output_stream,
        \ 'buffer_output': a:options.maker.buffer_output,
        \ }, 'keep')

    let error = ''
    try
        " Change to job's cwd (before args, for relative filename).
        let cd_error = jobinfo.cd()
        if !empty(cd_error)
            throw printf("Neomake: %s: could not change to maker's cwd (%s): %s.",
                        \ maker.name, jobinfo.cd_from_setting, cd_error)
        endif
        let jobinfo.argv = maker._get_argv(jobinfo)

        call neomake#utils#hook('NeomakeJobInit', {'jobinfo': jobinfo})

        let start_msg = s:async ? 'Starting async job' : 'Starting'
        if type(jobinfo.argv) == type('')
            let start_msg .= ' [string]: '.jobinfo.argv
        else
            let start_msg .= ': '.join(map(copy(jobinfo.argv), 'neomake#utils#shellescape(v:val)'))
        endif
        call neomake#log#info(start_msg.'.', jobinfo)

        let cwd = jobinfo.cwd
        let changed = !empty(jobinfo.cd_back_cmd)
        if changed
            call neomake#log#debug('cwd: '.cwd.' (changed).', jobinfo)
        else
            call neomake#log#debug('cwd: '.cwd.'.', jobinfo)
        endif

        let base_job_opts = {}
        if has_key(jobinfo, 'filename')
            if s:can_use_env_in_job_opts
                let base_job_opts = {
                            \ 'env': {
                            \   'NEOMAKE_FILE': jobinfo.filename
                            \ }}
            else
                let save_env_file = exists('$NEOMAKE_FILE') ? $NEOMAKE_FILE : s:unset
                let $NEOMAKE_FILE = jobinfo.filename
            endif
        endif

        " Lock maker to make sure it does not get changed accidentally, but
        " only with depth=1, so that a postprocess object can change itself.
        lockvar 1 maker
        if s:async
            if has('nvim')
                if jobinfo.buffer_output
                    let opts = extend(base_job_opts, {
                                \ 'stdout_buffered': 1,
                                \ 'stderr_buffered': 1,
                                \ })
                    if s:nvim_can_buffer_output == 1
                        let opts.on_exit = function('s:nvim_exit_handler_buffered')
                    else
                        call extend(opts, {
                                \ 'on_stdout': function('s:nvim_output_handler'),
                                \ 'on_stderr': function('s:nvim_output_handler'),
                                \ })
                        let opts.on_exit = function('s:nvim_exit_handler')
                    endif
                    let jobinfo.jobstart_opts = opts
                else
                    let opts = {
                                \ 'on_stdout': function('s:nvim_output_handler'),
                                \ 'on_stderr': function('s:nvim_output_handler'),
                                \ 'on_exit': function('s:nvim_exit_handler'),
                                \ }
                endif
                if has_key(maker, 'nvim_job_opts')
                    call extend(opts, maker.nvim_job_opts)
                endif
                if !has('nvim-0.3.0')
                            \ && !neomake#utils#IsRunningWindows()
                            \ && !has_key(opts, 'detach')
                            \ && !has_key(opts, 'pty')
                    " Always use detach to trigger setsid() with older Neovim.
                    let opts.detach = 1
                endif
                try
                    let job = jobstart(jobinfo.argv, opts)
                catch
                    let error = printf('Failed to start Neovim job: %s: %s.',
                                \ string(jobinfo.argv), v:exception)
                endtry
                if empty(error)
                    if job == 0
                        let error = printf('Failed to start Neovim job: %s: %s.',
                                    \ 'Job table is full or invalid arguments given', string(jobinfo.argv))
                    elseif job == -1
                        let error = printf('Failed to start Neovim job: %s: %s.',
                                    \ 'Executable not found', string(jobinfo.argv))
                    else
                        let s:map_job_ids[job] = jobinfo.id
                        let jobinfo.nvim_job = job
                        let s:jobs[jobinfo.id] = jobinfo

                        if get(jobinfo, 'uses_stdin', 0)
                            call jobsend(job, s:make_info[a:make_id].buffer_lines)
                            call jobclose(job, 'stdin')
                        endif
                    endif
                endif
            else
                " vim-async.
                let opts = extend(base_job_opts, {
                            \ 'out_cb': function('s:vim_output_handler_stdout'),
                            \ 'err_cb': function('s:vim_output_handler_stderr'),
                            \ 'close_cb': function('s:vim_exit_handler'),
                            \ 'mode': 'raw',
                            \ })
                if has_key(maker, 'vim_job_opts')
                    call extend(opts, maker.vim_job_opts)
                endif
                try
                    let job = job_start(jobinfo.argv, opts)
                    " Get this as early as possible!
                    let channel_id = ch_info(job)['id']
                catch
                    " NOTE: not covered in tests. Vim seems to always return
                    " a job. Might be able to trigger this using custom opts?!
                    let error = printf('Failed to start Vim job: %s: %s.',
                                \ jobinfo.argv, v:exception)
                endtry
                if empty(error)
                    let jobinfo.vim_job = job
                    let s:map_job_ids[channel_id] = jobinfo.id
                    let s:jobs[jobinfo.id] = jobinfo
                    call neomake#log#debug(printf('Vim job: %s.',
                                \ string(job_info(job))), jobinfo)
                    call neomake#log#debug(printf('Vim channel: %s.',
                                \ string(ch_info(job))), jobinfo)

                    if get(jobinfo, 'uses_stdin', 0)
                        call ch_sendraw(job, join(s:make_info[a:make_id].buffer_lines, "\n"))
                        call ch_close_in(job)
                    endif
                endif
            endif

            " Bail out on errors.
            if !empty(error)
                throw 'Neomake: '.error
            endif

            call neomake#utils#hook('NeomakeJobStarted', {'jobinfo': jobinfo})
        else
            " vim-sync.
            " Use a temporary file to capture stderr.
            let stderr_file = tempname()
            let argv = jobinfo.argv . ' 2>'.stderr_file

            try
                if get(jobinfo, 'uses_stdin', 0)
                    " Pass stdin to system(), but only if non-empty.
                    " Otherwise it might cause E677 (vim74-trusty at least).
                    let stdin = join(s:make_info[a:make_id].buffer_lines, "\n")
                    if !empty(stdin)
                        let output = system(argv, stdin)
                    else
                        let output = system(argv)
                    endif
                else
                    let output = system(argv)
                endif
            catch /^Vim(let):E484:/
                throw printf('Neomake: Could not run %s: %s.', argv, v:exception)
            endtry

            let jobinfo.id = job_id
            let s:jobs[job_id] = jobinfo
            let s:make_info[a:make_id].active_jobs += [jobinfo]

            call s:output_handler(jobinfo, split(output, '\r\?\n', 1), 'stdout', 0)
            let stderr_output = readfile(stderr_file)
            if !empty(stderr_output)
                call s:output_handler(jobinfo, stderr_output, 'stderr', 1)
            endif
            call delete(stderr_file)

            call s:exit_handler(jobinfo, v:shell_error)
            return jobinfo
        endif
    finally
        call jobinfo.cd_back()
        if exists('save_env_file')
            call s:restore_env('NEOMAKE_FILE', save_env_file)
        endif
    endtry
    let s:make_info[a:make_id].active_jobs += [jobinfo]
    return jobinfo
endfunction

if !s:can_use_env_in_job_opts
    function! s:restore_env(var, value) abort
        " Cannot unlet environment vars without patch 8.0.1832.
        exe printf('let $%s = %s', a:var, string(a:value is s:unset ? '' : a:value))
    endfunction
endif

let s:command_maker_base = copy(g:neomake#core#command_maker_base)
" Check if a temporary file is used, and set it in s:make_info in case it is.
function! s:command_maker_base._get_tempfilename(jobinfo) abort dict
    let l:Supports_stdin = neomake#utils#GetSetting('supports_stdin', self, s:unset_dict, a:jobinfo.ft, a:jobinfo.bufnr)
    if Supports_stdin isnot s:unset_dict
        if type(Supports_stdin) == type(function('tr'))
            let supports_stdin = call(Supports_stdin, [a:jobinfo], self)
        else
            let supports_stdin = Supports_stdin
        endif
        if supports_stdin
            let a:jobinfo.uses_stdin = 1
            return get(self, 'tempfile_name', '-')
        endif
    endif

    if has_key(self, 'tempfile_name')
        return self.tempfile_name
    endif

    let tempfile_enabled = neomake#utils#GetSetting('tempfile_enabled', self, 1, a:jobinfo.ft, a:jobinfo.bufnr)
    if !tempfile_enabled
        return ''
    endif

    let make_id = a:jobinfo.make_id
    if !has_key(s:make_info[make_id], 'tempfile_name')
        if !exists('s:pid')
            let s:pid = getpid()
        endif
        let slash = neomake#utils#Slash()

        let dir = neomake#utils#GetSetting('tempfile_dir', self, '', a:jobinfo.ft, a:jobinfo.bufnr)

        " Use absolute path internally, which is important for removal.
        let orig_fname = neomake#utils#fnamemodify(a:jobinfo.bufnr, ':p')
        if empty(dir)
            if empty(orig_fname)
                let dir = tempname()
            else
                let dir = fnamemodify(orig_fname, ':h')
                if filewritable(dir) != 2
                    let dir = tempname()
                    let s:make_info[make_id].tempfile_dir = dir
                    call neomake#log#debug('Using temporary directory for non-writable parent directory.')
                endif
            endif

            if empty(orig_fname)
                let filename = 'neomaketmp.'.a:jobinfo.ft
            else
                let filename = fnamemodify(orig_fname, ':t')
                            \ .'@neomake_'.s:pid.'_'.make_id
                let ext = fnamemodify(orig_fname, ':e')
                if !empty(ext)
                    let filename .= '.'.ext
                endif
                " Use hidden files to make e.g. pytest not trying to import it.
                if filename[0] !=# '.'
                    let filename = '.' . filename
                endif
            endif
        else
            let dir = neomake#utils#ExpandArgs([dir], a:jobinfo)[0]
            if empty(orig_fname)
                let filename = 'neomaketmp.'.a:jobinfo.ft
            else
                let filename = fnamemodify(orig_fname, ':t')
            endif
        endif

        let temp_file = dir . slash . filename
        let s:make_info[make_id].tempfile_name = temp_file
    endif
    return s:make_info[make_id].tempfile_name
endfunction

" Get the filename to use for a:jobinfo's make/buffer.
function! s:command_maker_base._get_fname_for_buffer(jobinfo) abort
    let bufnr = a:jobinfo.bufnr
    let bufname = bufname(bufnr)
    let temp_file = ''
    let _uses_stdin = neomake#utils#GetSetting('uses_stdin', a:jobinfo.maker, s:unset_dict, a:jobinfo.ft, bufnr)
    if _uses_stdin isnot s:unset_dict
        let a:jobinfo.uses_stdin = _uses_stdin
        let uses_stdin = _uses_stdin
        call neomake#log#debug(printf('Using uses_stdin (%s) from setting.',
                    \ a:jobinfo.uses_stdin), a:jobinfo)
        if a:jobinfo.uses_stdin
            let temp_file = neomake#utils#GetSetting('tempfile_name', a:jobinfo.maker, '-', a:jobinfo.ft, bufnr)
        endif
    else
        if empty(bufname)
            let temp_file = self._get_tempfilename(a:jobinfo)
            if !get(a:jobinfo, 'uses_stdin', 0) && empty(temp_file)
                throw 'Neomake: no file name.'
            endif
            let used_for = 'unnamed'
        elseif getbufvar(bufnr, '&modified')
            let temp_file = self._get_tempfilename(a:jobinfo)
            if !get(a:jobinfo, 'uses_stdin', 0) && empty(temp_file)
                throw 'Neomake: skip_job: buffer is modified, but temporary files are disabled.'
            endif
            let used_for = 'modified'
        elseif !filereadable(bufname)
            let temp_file = self._get_tempfilename(a:jobinfo)
            if !get(a:jobinfo, 'uses_stdin', 0) && empty(temp_file)
                " Using ':p' as modifier is unpredictable as per doc, but OK.
                throw printf('Neomake: file is not readable (%s)', fnamemodify(bufname, ':p'))
            endif
            let used_for = 'unreadable'
        else
            let bufname = fnamemodify(bufname, ':.')
            let used_for = ''
        endif

        let uses_stdin = get(a:jobinfo, 'uses_stdin', 0)

        if !empty(used_for)
            if uses_stdin
                call neomake#log#debug(printf(
                            \ 'Using stdin for %s buffer (%s).', used_for, temp_file),
                            \ a:jobinfo)
            elseif !empty(temp_file)
                call neomake#log#debug(printf(
                            \ 'Using tempfile for %s buffer: "%s".', used_for, temp_file),
                            \ a:jobinfo)
            endif
        endif
    endif

    let make_info = s:make_info[a:jobinfo.make_id]
    " Handle stdin when supports_stdin sets self.tempfile_name = ''.
    if uses_stdin
        if !has_key(make_info, 'buffer_lines')
            let make_info.buffer_lines = neomake#utils#get_buffer_lines(bufnr)
        endif
        let bufname = temp_file
    elseif !empty(temp_file)
        " Use relative path for args.
        let bufname = fnamemodify(temp_file, ':.')
        let temp_file = fnamemodify(temp_file, ':p')
        if !has_key(make_info, 'tempfiles')
            let make_info.tempfiles = [temp_file]
            let make_info.created_dirs = s:create_dirs_for_file(temp_file)
            call neomake#utils#write_tempfile(bufnr, temp_file)
        elseif temp_file !=# make_info.tempfiles[0]
            call extend(make_info.created_dirs, s:create_dirs_for_file(temp_file))
            call writefile(readfile(make_info.tempfiles[0], 'b'), temp_file, 'b')
            call add(make_info.tempfiles, temp_file)
        endif
        let a:jobinfo.tempfile = temp_file
    endif

    let a:jobinfo.filename = bufname
    return bufname
endfunction

function! s:create_dirs_for_file(fpath) abort
    let created_dirs = []
    let last_dir = a:fpath
    while 1
        let temp_dir = fnamemodify(last_dir, ':h')
        if isdirectory(temp_dir) || last_dir ==# temp_dir
            break
        endif
        call insert(created_dirs, temp_dir)
        let last_dir = temp_dir
    endwhile
    for dir in created_dirs
        call mkdir(dir, '', 0700)
    endfor
    return created_dirs
endfunction

function! s:command_maker_base._bind_args() abort dict
    " Resolve args, which might be a function or dictionary.
    if type(self.args) == type(function('tr'))
        " Deprecated: use InitForJob
        call neomake#log#warn_once(printf("Please use 'InitForJob' instead of 'args' for maker %s.", self.name),
                    \ printf('deprecated-args-%s', self.name))
        let args = call(self.args, [])
    elseif type(self.args) == type({})
        " Deprecated: use InitForJob
        call neomake#log#warn_once(printf("Please use 'InitForJob' instead of 'args.fn' for maker %s.", self.name),
                    \ printf('deprecated-args-fn-%s', self.name))
        let args = call(self.args.fn, [], self.args)
    else
        let args = copy(self.args)
    endif
    let self.args = args
    return self
endfunction

function! s:command_maker_base._get_argv(jobinfo) abort dict
    let filename = self._get_fname_for_args(a:jobinfo)
    let args_is_list = type(self.args) == type([])
    if args_is_list
        let args = neomake#utils#ExpandArgs(self.args, a:jobinfo)
        if !empty(filename)
            call add(args, filename)
        endif
    elseif !empty(filename)
        let args = copy(self.args)
        let args .= (empty(args) ? '' : ' ').neomake#utils#shellescape(filename)
    else
        let args = self.args
    endif
    return neomake#compat#get_argv(self.exe, args, args_is_list)
endfunction

function! s:GetMakerForFiletype(ft, maker_name) abort
    for config_ft in neomake#utils#get_config_fts(a:ft)
        call neomake#utils#load_ft_makers(config_ft)
        let f = 'neomake#makers#ft#'.config_ft.'#'.a:maker_name
        if exists('*'.f)
            let maker = call(f, [])
            return maker
        endif
    endfor
    return s:unset_dict
endfunction

function! neomake#get_maker_by_name(maker_name, ...) abort
    let for_ft = a:0 ? a:1 : 0
    let ft_config = for_ft is# 0 ? &filetype : for_ft
    let bufnr = bufnr('%')
    if a:maker_name !~# '\v^\w+$'
        throw printf('Neomake: Invalid maker name: "%s"', a:maker_name)
    endif

    let maker = neomake#utils#GetSetting('maker', {'name': a:maker_name}, s:unset_dict, ft_config, bufnr)
    if maker is# s:unset_dict
        if a:maker_name ==# 'makeprg'
            let maker = s:get_makeprg_maker()
        elseif for_ft isnot# 0
            let maker = s:GetMakerForFiletype(for_ft, a:maker_name)
        else
            call neomake#utils#load_global_makers()
            let f = 'neomake#makers#'.a:maker_name.'#'.a:maker_name
            if exists('*'.f)
                let maker = call(f, [])
            endif
        endif
    endif
    if type(maker) != type({})
        throw printf('Neomake: Got non-dict for maker %s: %s',
                    \ a:maker_name, maker)
    endif
    if maker isnot# s:unset_dict && !has_key(maker, 'name')
        let maker.name = a:maker_name
    endif
    return maker
endfunction

function! neomake#GetMaker(name_or_maker, ...) abort
    let for_ft = a:0 ? a:1 : 0
    if type(a:name_or_maker) == type({})
        let maker = a:name_or_maker
        if !has_key(maker, 'name')
            let maker.name = 'unnamed_maker'
        endif
    else
        let maker = neomake#get_maker_by_name(a:name_or_maker, for_ft)
        if maker is# s:unset_dict
            if !a:0
                " Check &filetype if no args where provided.
                let maker = neomake#get_maker_by_name(a:name_or_maker, &filetype)
            endif
        endif
        if maker is# s:unset_dict
            if for_ft isnot# 0
                throw printf('Neomake: Maker not found (for %s): %s',
                            \ !empty(for_ft) ? 'filetype '.for_ft : 'empty filetype',
                            \ a:name_or_maker)
            else
                throw printf('Neomake: Maker not found (without filetype): %s',
                            \ a:name_or_maker)
            endif
        endif
    endif
    return neomake#create_maker_object(maker, a:0 ? a:1 : &filetype)
endfunction

" NOTE: uses ft and bufnr for config only.
function! neomake#create_maker_object(maker, ft) abort
    let [maker, ft, bufnr] = [a:maker, a:ft, bufnr('%')]

    " Create the maker object.
    let l:GetEntries = neomake#utils#GetSetting('get_list_entries', maker, -1, ft, bufnr)
    if GetEntries isnot# -1
        let maker = copy(maker)
        let maker.get_list_entries = GetEntries
    else
        let maker = extend(copy(s:command_maker_base), copy(maker))
    endif
    if !has_key(maker, 'get_list_entries')
        " Set defaults for command/job based makers.
        let defaults = extend(
                    \ copy(g:neomake#config#_defaults['maker_defaults']),
                    \ neomake#config#get('maker_defaults'))
        call extend(defaults, {
            \ 'exe': maker.name,
            \ 'args': [],
            \ })
        if !has_key(maker, 'process_output') && !has_key(maker, 'process_json')
            call extend(defaults, {
                \ 'errorformat': &errorformat,
                \ })
        endif
        for [key, default] in items(defaults)
            let maker[key] = neomake#utils#GetSetting(key, {'name': maker.name}, get(maker, key, default), ft, bufnr, 1)
            unlet default  " for Vim without patch-7.4.1546
        endfor
    endif
    if v:profiling
        call add(s:hack_keep_refs_for_profiling, maker)
    endif
    return maker
endfunction

if exists('*getcompletion')
    function! s:get_makers_for_pattern(pattern) abort
        " Get function prefix based on pattern, until the first backslash.
        let prefix = substitute(a:pattern, '\v\\.*', '', '')

        " NOTE: the pattern uses &ignorecase.
        let funcs = getcompletion(prefix.'[a-z]', 'function')
        call filter(funcs, 'v:val =~# a:pattern')
        " Remove prefix.
        call map(funcs, 'v:val['.len(prefix).':]')
        " Only keep lowercase function names.
        call filter(funcs, "v:val =~# '\\m^[a-z].*('")
        " Remove parenthesis and #.* (for project makers).
        return sort(map(funcs, "substitute(v:val, '\\v[(#].*', '', '')"))
    endfunction
else
    function! s:get_makers_for_pattern(pattern) abort
        let funcs_output = neomake#utils#redir('fun /'.a:pattern)
        return sort(map(split(funcs_output, '\n'),
                    \ "substitute(v:val, '\\v^.*#(.*)\\(.*$', '\\1', '')"))
    endfunction
endif

function! neomake#GetMakers(ft) abort
    " Get all makers for a given filetype.  This is used from completion.
    " XXX: this should probably use a callback or some other more stable
    " approach to get the list of makers (than looking at the lowercase
    " functions)?!

    let makers = []
    " Do not use 'b:neomake_jsx_javascript_foo_maker' twice for
    " ft=jsx.javascript.
    let used_vars = []
    for ft in neomake#utils#get_config_fts(a:ft)
        call neomake#utils#load_ft_makers(ft)

        " Add sorted list per filetype.
        let add = []

        let maker_names = s:get_makers_for_pattern('neomake#makers#ft#'.ft.'#\l')
        for maker_name in maker_names
            if index(makers, maker_name) == -1 && index(add, maker_name) == -1
                let add += [maker_name]
            endif
        endfor

        " Get makers from g:/b: variables.
        for v in sort(extend(keys(g:), keys(b:)))
            if index(used_vars, v) != -1
                continue
            endif
            let maker_name = matchstr(v, '\v^neomake_'.ft.'_\zs[0-9a-z_]+\ze_maker$')
            if !empty(maker_name)
                        \ && index(makers, maker_name) == -1
                        \ && index(add, maker_name) == -1
                let used_vars += [v]
                let add += [maker_name]
            endif
        endfor

        " Get makers from new-style config.
        for [maker_name, val] in items(neomake#config#get('ft.'.ft))
            if has_key(val, 'maker')
                        \ && index(makers, maker_name) == -1
                        \ && index(add, maker_name) == -1
                let add += [maker_name]
            endif
        endfor

        call sort(add)
        call extend(makers, add)
    endfor
    return makers
endfunction

function! neomake#GetProjectMakers() abort
    call neomake#utils#load_global_makers()
    return s:get_makers_for_pattern('neomake#makers#\(ft#\)\@!\l')
endfunction

function! neomake#GetEnabledMakers(...) abort
    let file_mode = a:0
    if !file_mode
        " If we have no filetype, use the global default makers.
        " This variable is also used for project jobs, so it has no
        " buffer local ('b:') counterpart for now.
        let enabled_makers = copy(get(g:, 'neomake_enabled_makers', []))
        if empty(enabled_makers)
            let makeprg_maker = s:get_makeprg_maker()
            if !empty(makeprg_maker)
                let makeprg_maker = neomake#GetMaker(makeprg_maker)
                let makeprg_maker.auto_enabled = 1
                let enabled_makers = [makeprg_maker]
            endif
        else
            call map(enabled_makers, "extend(neomake#GetMaker(v:val),
                        \ {'auto_enabled': 0}, 'error')")
        endif
    else
        let enabled_makers = []
        let bufnr = bufnr('%')
        let makers = neomake#utils#GetSetting('enabled_makers', {}, s:unset_list, a:1, bufnr)
        if makers is# s:unset_list
            let auto_enabled = 1
            for config_ft in neomake#utils#get_config_fts(a:1)
                call neomake#utils#load_ft_makers(config_ft)
                let fnname = 'neomake#makers#ft#'.config_ft.'#EnabledMakers'
                if exists('*'.fnname)
                    try
                        let makers = call(fnname, [])
                    catch /^Vim(let):E119:/  " Not enough arguments for function
                        let makers = call(fnname, [{'file_mode': file_mode, 'bufnr': bufnr}])
                    endtry
                    break
                endif
            endfor
        else
            let auto_enabled = 0
        endif

        let makers = neomake#map_makers(makers, a:1, auto_enabled)
        for maker in makers
            let maker.auto_enabled = auto_enabled
            let enabled_makers += [maker]
        endfor
    endif
    return enabled_makers
endfunction

" a:1: override "open_list" setting.
function! s:HandleLoclistQflistDisplay(jobinfo, loc_or_qflist, ...) abort
    let open_list_default = a:0 ? a:1 : 0
    let open_val = neomake#utils#GetSetting('open_list', a:jobinfo.maker, open_list_default, a:jobinfo.ft, a:jobinfo.bufnr)
    if !open_val
        return
    endif
    let height = neomake#utils#GetSetting('list_height', a:jobinfo.maker, 10, a:jobinfo.ft, a:jobinfo.bufnr)
    if !height
        return
    endif
    let height = min([len(a:loc_or_qflist), height])
    if a:jobinfo.file_mode
        call neomake#log#debug('Handling location list: executing lwindow.', a:jobinfo)
        let cmd = 'lwindow'
    else
        call neomake#log#debug('Handling quickfix list: executing cwindow.', a:jobinfo)
        let cmd = 'botright cwindow'
    endif
    if open_val == 2
        let make_id = a:jobinfo.make_id
        let make_info = s:make_info[make_id]
        let g:neomake#core#_ignore_autocommands += 1
        try
            call neomake#compat#save_prev_windows()

            let win_count = winnr('$')
            exe cmd height
            let new_win_count = winnr('$')
            if win_count == new_win_count
                " No new window, adjust height eventually.
                let found = 0

                if get(make_info, '_did_lwindow', 0)
                    for w in range(1, winnr('$'))
                        if getwinvar(w, 'neomake_window_for_make_id') == make_id
                            let found = w
                            break
                        endif
                    endfor
                    if found
                        let cmd = printf('%dresize %d', found, height)
                        if winheight(found) != height
                            call neomake#log#debug(printf(
                                        \ 'Resizing existing quickfix window: %s.',
                                        \ cmd), a:jobinfo)
                            exe cmd
                        endif
                    else
                        call neomake#log#debug(
                                    \ 'Could not find corresponding quickfix window.',
                                    \ a:jobinfo)
                    endif
                endif
            elseif new_win_count > win_count
                if &filetype !=# 'qf'
                    call neomake#log#debug(printf(
                                \ 'WARN: unexpected filetype for new window: %s',
                                \ &filetype), a:jobinfo)
                else
                    call neomake#log#debug(printf(
                                \ 'list window has been opened (old count: %d, new count: %d, height: %d).',
                                \ win_count, new_win_count, winheight(0)), a:jobinfo)
                    let w:neomake_window_for_make_id = a:jobinfo.make_id
                endif
            else
                call neomake#log#debug(printf(
                            \ 'list window has been closed (old count: %d, new count: %d).',
                            \ win_count, new_win_count), a:jobinfo)
            endif
            call neomake#compat#restore_prev_windows()
            let make_info._did_lwindow = 1
        finally
            let g:neomake#core#_ignore_autocommands -= 1
        endtry
    else
        exe cmd height
    endif
endfunction

" Experimental/private wrapper.
function! neomake#_handle_list_display(jobinfo, ...) abort
    if a:0
        let list = a:1
    else
        let list = a:jobinfo.file_mode ? getloclist(0) : getqflist()
    endif
    call s:HandleLoclistQflistDisplay(a:jobinfo, list, 2)
endfunction

" Get a maker for &makeprg.
" This could be cached, but needs to take into account / set &errorformat,
" and other settings that are handled by neomake#GetMaker.
function! s:get_makeprg_maker() abort
    if empty(&makeprg)
        return {}
    elseif &makeprg =~# '\s'
        let maker = neomake#utils#MakerFromCommand(&makeprg)
    else
        let maker = neomake#utils#MakerFromCommand([&makeprg])
    endif
    let maker.name = 'makeprg'
    " Do not append file.  &makeprg should contain %/# for this instead.
    let maker.append_file = 0
    return neomake#GetMaker(maker)
endfunction

function! s:Make(options) abort
    let is_automake = get(a:options, 'automake', !empty(expand('<abuf>')))
    if is_automake
        if g:neomake#core#_ignore_autocommands
            call neomake#log#debug(printf(
                        \ 'Ignoring Make through autocommand due to ignore_autocommands=%d.', g:neomake#core#_ignore_autocommands), {'winnr': winnr()})
            return []
        endif
        let disabled = neomake#config#get_with_source('disabled', 0)
        if disabled[0]
            call neomake#log#debug(printf(
                        \ 'Make through autocommand disabled via %s.', disabled[1]))
            return []
        endif
    endif

    let s:make_id += 1
    let make_id = s:make_id
    let options = extend(copy(a:options), {
                \ 'file_mode': 1,
                \ 'ft': &filetype,
                \ }, 'keep')
    let options.make_id = make_id  " Deprecated.
    let file_mode = options.file_mode

    " Require winid/winnr with non-current buffer in file_mode.
    if has_key(options, 'bufnr')
        if options.bufnr != bufnr('%')
            if !has_key(options, 'winid') && !has_key(options, 'winnr')
                throw 'Neomake: winid or winnr are required for non-current buffer.'
            endif
        endif
        if !bufexists(options.bufnr)
            throw printf('Neomake: buffer %d does not exist.', options.bufnr)
        endif
    else
        let options.bufnr = bufnr('%')
    endif

    " Validate winid/winnr (required for location list windows).
    let file_mode_win = 0
    if file_mode
        if has_key(options, 'winid')
            if win_id2tabwin(options.winid) == [0, 0]
                throw printf('Neomake: window id %d does not exist.', options.winid)
            endif
            let file_mode_win = options.winid
        elseif has_key(options, 'winnr')
            if winbufnr(options.winnr) == -1
                throw printf('Neomake: window %d does not exist.', options.winnr)
            endif
            let file_mode_win = options.winnr
        elseif exists('*win_getid')
            let options.winid = win_getid()
        endif
    elseif has_key(options, 'winid')
        throw 'Neomake: do not use winid with file_mode=0.'
    elseif has_key(options, 'winnr')
        throw 'Neomake: do not use winnr with file_mode=0.'
    endif

    lockvar 1 options
    let s:make_info[make_id] = {
                \ 'make_id': make_id,
                \ 'cwd': getcwd(),
                \ 'verbosity': get(g:, 'neomake_verbose', 1),
                \ 'active_jobs': [],
                \ 'finished_jobs': [],
                \ 'options': options,
                \ }
    let make_info = s:make_info[make_id]
    let bufnr = options.bufnr
    if &verbose
        let make_info.verbosity += &verbose
        call neomake#log#debug(printf(
                    \ 'Adding &verbose (%d) to verbosity level: %d.',
                    \ &verbose, make_info.verbosity), make_info)
    endif
    if make_info.verbosity >= 3
        call neomake#log#debug(printf(
                    \ 'Calling Make with options %s.',
                    \ string(filter(copy(options), "index(['bufnr', 'make_id'], v:key) == -1"))), {'make_id': make_id, 'bufnr': bufnr})
    endif

    " Use pre-compiled jobs (used with automake).
    if has_key(options, 'jobs')
        let jobs = map(copy(options.jobs), "extend(v:val, {'make_id': make_id})")
    else
        if has_key(options, 'enabled_makers')
            if file_mode
                let makers = neomake#map_makers(options.enabled_makers, options.ft, 0)
            else
                let makers = neomake#map_makers(options.enabled_makers, -1, 0)
            endif
        else
            let makers = call('neomake#GetEnabledMakers', file_mode ? [options.ft] : [])
            if empty(makers)
                if file_mode
                    let msg = printf('Nothing to make: no enabled file mode makers (filetype=%s).', options.ft)
                    if is_automake
                        call neomake#log#debug(msg, make_info)
                    else
                        call neomake#log#warning(msg, make_info)
                    endif
                    unlet s:make_info[make_id]
                    return []
                endif
            endif
        endif
        let job_options = copy(options)
        let job_options.make_id = make_id  " Used for logging.
        let jobs = neomake#core#create_jobs(job_options, makers)
    endif

    if empty(jobs)
        call neomake#log#debug('Nothing to make: no valid makers.', make_info)
        call s:clean_make_info(make_info)
        return []
    endif
    let make_info.jobs = copy(jobs)

    let maker_info = join(map(copy(jobs),
                \ "v:val.maker.name . (get(v:val.maker, 'auto_enabled', 0) ? ' (auto)' : '')"), ', ')
    call neomake#log#debug(printf('Running makers: %s.', maker_info), make_info)

    let make_info.jobs_queue = jobs

    if file_mode
        " XXX: this clears counts for job's buffer only, but we add counts for
        " the entry's buffers, which might be different!
        call neomake#statusline#ResetCountsForBuf(bufnr)
        if g:neomake_place_signs
            call neomake#signs#Reset(bufnr, 'file')
        endif
    else
        call neomake#statusline#ResetCountsForProject()
        if g:neomake_place_signs
            call neomake#signs#ResetProject()
        endif
    endif

    " Store make_id on window (used to find window for location lists (without
    " winid, but also used to check the current window via w: directly)).
    if file_mode
        call setwinvar(file_mode_win, 'neomake_make_ids',
                    \ neomake#compat#getwinvar(file_mode_win, 'neomake_make_ids', []) + [make_id])
    endif

    let use_list = get(options, 'use_list', 1)
    if use_list
        let any_job_uses_list = 0
        for job in jobs
            if get(job.maker, 'use_list', 1)
                let any_job_uses_list = 1
                break
            endif
        endfor
        if !any_job_uses_list
            let use_list = 0
        endif
    endif

    if use_list
        let make_info.entries_list = neomake#list#ListForMake(make_info)

        " Reuse existing location list window with automake.
        if is_automake && has('patch-7.4.2200')
            if file_mode
                let title = get(getloclist(0, {'title': 1}), 'title')
            else
                let title = get(getqflist({'title': 1}), 'title')
            endif
            if title =~# '\V\^Neomake[auto]'
                let make_info.entries_list.reset_existing_qflist = 1
            endif
        endif
    endif

    " Cancel any already running jobs for the makers from these jobs.
    if !empty(s:jobs)
        " @vimlint(EVL102, 1, l:job)
        for job in jobs
            let running_already = values(filter(copy(s:jobs),
                        \ 'v:val.maker == job.maker'
                        \ .' && v:val.bufnr == job.bufnr'
                        \ .' && v:val.file_mode == job.file_mode'
                        \ ." && !get(v:val, 'canceled')"))
            if !empty(running_already)
                let jobinfo = running_already[0]
                call neomake#log#info(printf(
                            \ 'Canceling already running job (%d.%d) for the same maker.',
                            \ jobinfo.make_id, jobinfo.id), {'make_id': make_id})
                call neomake#CancelJob(jobinfo.id, 1)
            endif
        endfor
    endif

    " Update automake tick (used to skip unchanged buffers).
    call neomake#configure#_update_automake_tick(bufnr, options.ft)

    " Start all jobs in the queue (until serialized).
    let jobinfos = []
    while 1
        if empty(make_info.jobs_queue)
            break
        endif
        let jobinfo = s:handle_next_job({})
        if empty(jobinfo)
            break
        endif
        call add(jobinfos, jobinfo)
        if jobinfo.serialize
            let make_info.serializing_for_job = jobinfo.id
            " Break and continue through exit handler.
            break
        endif
    endwhile
    return jobinfos
endfunction

function! s:AddExprCallback(jobinfo, lines) abort
    if s:need_to_postpone_loclist(a:jobinfo)
        return neomake#action_queue#add(['BufEnter', 'WinEnter'], [s:function('s:AddExprCallback'),
                    \ [a:jobinfo, a:lines] + a:000])
    endif

    " Create location/quickfix list and add lines to it.
    let cd_error = a:jobinfo.cd()
    if !empty(cd_error)
        call neomake#log#debug(printf(
                    \ "Could not change to job's cwd (%s): %s.",
                    \ a:jobinfo.cd_from_setting, cd_error), a:jobinfo)
    endif

    let make_list = s:make_info[a:jobinfo.make_id].entries_list
    let prev_list = copy(make_list.entries)

    let added_entries = make_list.add_lines_with_efm(a:lines, a:jobinfo)
    return s:ProcessEntries(a:jobinfo, added_entries, prev_list)
endfunction

function! s:CleanJobinfo(jobinfo, ...) abort
    if get(a:jobinfo, '_in_exit_handler', 0)
        " Do not clean job yet.
        return
    endif
    if !empty(a:jobinfo.pending_output) && !get(a:jobinfo, 'canceled', 0)
        call neomake#log#debug(
                    \ 'Output left to be processed, not cleaning job yet.', a:jobinfo)
        return g:neomake#action_queue#not_processed
    endif

    let queued_actions = neomake#action_queue#get_queued_actions(a:jobinfo)
    if !empty(queued_actions)
        call neomake#log#debug(printf(
                    \ 'Skipping cleaning of job info because of queued actions: %s.',
                    \ join(queued_actions, ', ')), a:jobinfo)
        return neomake#action_queue#add(['WinEnter'], [s:function('s:CleanJobinfo'), [a:jobinfo]])
    endif

    call neomake#log#debug('Cleaning jobinfo.', a:jobinfo)
    let a:jobinfo.finished = 1

    if !has_key(s:make_info, a:jobinfo.make_id)
        return g:neomake#action_queue#processed
    endif
    let make_info = s:make_info[a:jobinfo.make_id]

    if has_key(s:jobs, get(a:jobinfo, 'id', -1))
        call remove(s:jobs, a:jobinfo.id)
        call filter(s:map_job_ids, 'v:val != a:jobinfo.id')
    endif

    if exists('s:kill_vim_timers')
        for [timer, job] in items(s:kill_vim_timers)
            if job == a:jobinfo
                call timer_stop(+timer)
                unlet s:kill_vim_timers[timer]
                break
            endif
        endfor
    endif

    if !get(a:jobinfo, 'canceled', 0)
                \ && !get(a:jobinfo, 'failed_to_start', 0)
        let make_info.finished_jobs += [a:jobinfo]
        call neomake#utils#hook('NeomakeJobFinished', {'jobinfo': a:jobinfo})
    endif

    call filter(make_info.active_jobs, 'v:val != a:jobinfo')

    " Trigger cleanup (and autocommands) if all jobs have finished.
    if empty(make_info.active_jobs) && empty(make_info.jobs_queue)
        call s:clean_make_info(make_info)
    endif
    return g:neomake#action_queue#processed
endfunction

function! s:clean_make_info(make_info, ...) abort
    let make_id = a:make_info.make_id
    let bang = a:0 ? a:1 : 0
    if !bang && !empty(a:make_info.active_jobs)
        call neomake#log#debug(printf(
                    \ 'Skipping cleaning of make info: %d active jobs: %s.',
                    \ len(a:make_info.active_jobs),
                    \ string(map(copy(a:make_info.active_jobs), 'v:val.as_string()'))),
                    \ a:make_info)
        return
    endif

    " Queue cleanup in case of queued actions, e.g. NeomakeJobFinished hook.
    let queued = []
    for [_, v] in g:neomake#action_queue#_s.action_queue
        if has_key(v[1][0], 'id')
            let jobinfo = v[1][0]
            if jobinfo.make_id == make_id
                let queued += ['job '.jobinfo.id]
            endif
        else
            if v[1][0] == a:make_info
                let queued += ['make '.make_id]
            endif
        endif
    endfor
    if !empty(queued)
        call neomake#log#debug(printf('Queuing clean_make_info for already queued actions: %s', string(queued)))
        return neomake#action_queue#add(
                    \ g:neomake#action_queue#any_event,
                    \ [s:function('s:clean_make_info'), [a:make_info]])
    endif

    if exists('*neomake#statusline#make_finished')
        call neomake#statusline#make_finished(a:make_info)
    endif

    if !empty(a:make_info.finished_jobs)
        " Clean old signs after all jobs have finished, so that they can be
        " reused, avoiding flicker and keeping them for longer in general.
        if g:neomake_place_signs
            if a:make_info.options.file_mode
                call neomake#signs#CleanOldSigns(a:make_info.options.bufnr, 'file')
            else
                call neomake#signs#CleanAllOldSigns('project')
            endif
        endif
        call s:clean_for_new_make(a:make_info)

        call neomake#EchoCurrentError(1)
        call neomake#virtualtext#handle_current_error()

        if get(a:make_info, 'canceled', 0)
            call neomake#log#debug('Skipping final processing for canceled make.', a:make_info)
            call s:do_clean_make_info(a:make_info)
        elseif has_key(a:make_info, 'entries_list')  " use_list option
            return s:handle_locqf_list_for_finished_jobs(a:make_info)
        else
            call s:handle_finished_make(a:make_info)
        endif
    else
        call s:do_clean_make_info(a:make_info)
    endif
    return g:neomake#action_queue#processed
endfunction

function! s:do_clean_make_info(make_info) abort
    call neomake#log#debug('Cleaning make info.', a:make_info)
    let make_id = a:make_info.make_id

    " Remove make_id from its window.
    let [t, w] = neomake#core#get_tabwin_for_makeid(make_id)
    if [t, w] != [-1, -1]
        let make_ids = neomake#compat#gettabwinvar(t, w, 'neomake_make_ids', [])
        let idx = index(make_ids, make_id)
        if idx != -1
            call remove(make_ids, idx)
            call settabwinvar(t, w, 'neomake_make_ids', make_ids)
        endif
    endif

    " Clean up temporary files and buffers.
    let wipe_unlisted_buffers = get(a:make_info, '_wipe_unlisted_buffers', [])
    let tempfiles = get(a:make_info, 'tempfiles')
    if !empty(tempfiles)
        for tempfile in tempfiles
            let delete_ret = delete(tempfile)
            if delete_ret == 0
                call neomake#log#debug(printf('Removing temporary file: "%s".',
                            \ tempfile))
            else
                call neomake#log#warning(printf('Failed to remove temporary file: "%s" (%d).',
                            \ tempfile, delete_ret))
            endif
            let bufnr_tempfile = bufnr(tempfile)
            if bufnr_tempfile != -1 && !buflisted(bufnr_tempfile)
                let wipe_unlisted_buffers += [bufnr_tempfile]
            endif
        endfor

        " Only delete the dir, if Vim supports it.
        if v:version >= 705 || (v:version == 704 && has('patch1107'))
            for dir in reverse(copy(get(a:make_info, 'created_dirs')))
                call delete(dir, 'd')
            endfor
        endif
    endif
    if !empty(wipe_unlisted_buffers)
        if !empty(wipe_unlisted_buffers)
            call neomake#compat#uniq(sort(wipe_unlisted_buffers))
        endif
        call neomake#log#debug(printf('Wiping out %d unlisted/remapped buffers: %s.',
                    \ len(wipe_unlisted_buffers),
                    \ string(wipe_unlisted_buffers)))
        " NOTE: needs to be silent with more than a single buffer.
        exe 'silent bwipeout '.join(wipe_unlisted_buffers)
    endif

    let buf_prev_makes = getbufvar(a:make_info.options.bufnr, '_neomake_automake_make_ids')
    if !empty(buf_prev_makes)
        call filter(buf_prev_makes, 'v:val != make_id')
        call setbufvar(a:make_info.options.bufnr, '_neomake_automake_make_ids', buf_prev_makes)
    endif

    unlet s:make_info[make_id]
endfunction

function! s:handle_locqf_list_for_finished_jobs(make_info) abort
    let file_mode = a:make_info.options.file_mode
    let create_list = a:make_info.entries_list.need_init

    let open_val = get(g:, 'neomake_open_list', 0)
    let height = open_val ? get(g:, 'neomake_list_height', 10) : 0
    if height
        let close_list = create_list || empty(file_mode ? getloclist(0) : getqflist())
    else
        let close_list = 0
    endif

    if file_mode
        if create_list && !bufexists(a:make_info.options.bufnr)
            call neomake#log#info('No buffer found for location list!', a:make_info)
            let create_list = 0
            let close_list = 0
        elseif (create_list || close_list)
            if index(get(w:, 'neomake_make_ids', []), a:make_info.make_id) == -1
                call neomake#log#debug(
                            \ 'Postponing final location list handling (in another window).',
                            \ a:make_info)
                return neomake#action_queue#add(['WinEnter'], [s:function('s:handle_locqf_list_for_finished_jobs'),
                            \ [a:make_info] + a:000])
            endif

            " TODO: merge with s:need_to_postpone_output_processing.
            if neomake#compat#in_completion()
                call neomake#log#debug(
                            \ 'Postponing final location list handling during completion.',
                            \ a:make_info)
                return neomake#action_queue#add(['CompleteDone'], [s:function('s:handle_locqf_list_for_finished_jobs'),
                            \ [a:make_info] + a:000])
            endif
            let mode = neomake#compat#get_mode()
            if index(['n', 'i'], mode) == -1
                call neomake#log#debug(printf(
                            \ 'Postponing final location list handling for mode "%s".', mode),
                            \ a:make_info)
                return neomake#action_queue#add(['CursorHold', 'WinEnter'], [s:function('s:handle_locqf_list_for_finished_jobs'),
                            \ [a:make_info] + a:000])
            endif
        endif
    endif

    " Update list title.
    " This has to be done currently by itself to reflect running/finished
    " state properly.
    if create_list || !a:make_info.entries_list.need_init
        if has_key(a:make_info, 'entries_list')
            call a:make_info.entries_list.finish_for_make()
        endif
    endif

    " Close empty list.
    if close_list
        if file_mode
            call neomake#log#debug('Handling location list: executing lclose.', {'winnr': winnr()})
            lclose
        else
            call neomake#log#debug('Handling quickfix list: executing cclose.')
            cclose
        endif
    endif

    call s:handle_finished_make(a:make_info)

    return g:neomake#action_queue#processed
endfunction

function! s:handle_finished_make(make_info) abort
    let hook_context = {
                \ 'make_info': a:make_info,
                \ 'make_id': a:make_info.make_id,
                \ 'options': a:make_info.options,
                \ 'finished_jobs': a:make_info.finished_jobs,
                \ }
    call neomake#utils#hook('NeomakeFinished', hook_context)

    call neomake#configure#_reset_automake_cancelations(a:make_info.options.bufnr)

    call s:do_clean_make_info(a:make_info)
endfunction

function! neomake#VimLeave() abort
    call neomake#log#debug('Calling VimLeave.')
    for make_id in keys(s:make_info)
        call neomake#CancelMake(make_id)
    endfor
endfunction

function! s:clean_for_new_make(make_info) abort
    if get(a:make_info, 'cleaned_for_make', 0)
        return
    endif
    " XXX: needs to handle buffers for list entries?!
    " See "get_list_entries: minimal example (from doc)" in
    " tests/makers.vader.
    call neomake#_clean_errors(extend(copy(a:make_info.options), {'make_id': a:make_info.make_id}))
    let a:make_info.cleaned_for_make = 1
endfunction

" a:context: dictionary with keys:
"  - file_mode
"  - bufnr (required for file_mode)
"  - make_id (used for logging)
function! neomake#_clean_errors(context) abort
    if a:context.file_mode
        let bufnr = a:context.bufnr
        if has_key(s:current_errors['file'], bufnr)
            unlet s:current_errors['file'][bufnr]
        endif
        call neomake#highlights#ResetFile(bufnr)
        call neomake#log#debug('File-level errors cleaned.', a:context)
    else
        let s:current_errors['project'] = {}
        call neomake#highlights#ResetProject()
        call neomake#log#debug('Project-level errors cleaned.', a:context)
    endif
endfunction

" Change to a job's cwd, if any.
" Returns: a list:
"  - error (empty for success)
"  - directory changed into (empty if skipped)
"  - command to change back to the current workding dir (might be empty)

" Call a:fn with a:args and queue it, in case if fails with E48/E523.
function! s:pcall(fn, args) abort
    let jobinfo = a:args[0]
    try
        return call(a:fn, a:args + [1])
    catch /^\%(Vim\%((\a\+)\)\=:\%(E48\|E523\)\)/  " only E48/E523 (sandbox / not allowed here)
        call neomake#log#debug('Error during pcall: '.v:exception.'.', jobinfo)
        call neomake#log#debug(printf('(in %s)', v:throwpoint), jobinfo)
        " Might throw in case of X failed attempts.
        call neomake#action_queue#add(['Timer', 'WinEnter'], [s:function(a:fn), a:args])
    endtry
    return g:neomake#action_queue#not_processed
endfunction

function! s:ProcessEntries(jobinfo, entries, ...) abort
    if empty(a:entries)
        return
    endif
    if get(a:jobinfo, 'canceled')
        return
    endif
    if s:need_to_postpone_loclist(a:jobinfo)
        return neomake#action_queue#add(['BufEnter', 'WinEnter'], [s:function('s:ProcessEntries'),
                    \ [a:jobinfo, a:entries] + a:000])
    endif
    if !a:0 || type(a:[len(a:000)]) != 0
        return s:pcall('s:ProcessEntries', [a:jobinfo, a:entries] + a:000)
    endif
    let file_mode = a:jobinfo.file_mode

    call neomake#log#debug(printf(
                \ 'Processing %d entries.', len(a:entries)), a:jobinfo)

    let make_info = s:make_info[a:jobinfo.make_id]
    let make_list = make_info.entries_list
    let maker_name = a:jobinfo.maker.name
    if a:0 > 1
        " Via errorformat processing, where the list has been set already.
        let prev_list = a:1
        let parsed_entries = a:entries
    else
        " Fix entries with get_list_entries/process_output/process_json.
        " @vimlint(EVL102, 1, l:default_type)
        let default_type = neomake#utils#GetSetting('default_entry_type', a:jobinfo.maker, 'W', a:jobinfo.ft, a:jobinfo.bufnr)
        call map(a:entries, 'extend(v:val, {'
                    \ . "'bufnr': str2nr(get(v:val, 'bufnr', 0)),"
                    \ . "'lnum': str2nr(get(v:val, 'lnum', 0)),"
                    \ . "'col': str2nr(get(v:val, 'col', 0)),"
                    \ . "'vcol': str2nr(get(v:val, 'vcol', 0)),"
                    \ . "'type': get(v:val, 'type', default_type),"
                    \ . "'nr': get(v:val, 'nr', has_key(v:val, 'text') ? -1 : 0),"
                    \ . "'text': get(v:val, 'text', ''),"
                    \ . '})')

        let cd_error = a:jobinfo.cd()
        if !empty(cd_error)
            call neomake#log#debug(printf(
                        \ "Could not change to job's cwd (%s): %s.",
                        \ a:jobinfo.cd_from_setting, cd_error), a:jobinfo)
        endif

        let prev_list = file_mode ? getloclist(0) : getqflist()
        try
            let parsed_entries = make_list.add_entries_for_job(a:entries, a:jobinfo)
            if exists(':Assert') && !empty(a:entries)
                Assert get(a:entries[0], 'text', '') !~# 'nmcfg:'
            endif
        finally
            call a:jobinfo.cd_back()
        endtry
    endif
    call s:clean_for_new_make(make_info)

    let counts_changed = 0
    let maker_type = file_mode ? 'file' : 'project'
    let do_highlight = get(g:, 'neomake_highlight_columns', 1)
                \ || get(g:, 'neomake_highlight_lines', 0)
    let signs_by_bufnr = {}
    let debug = neomake#utils#get_verbosity(a:jobinfo) >= 3 || !empty(get(g:, 'neomake_logfile')) || s:is_testing
    let entries_with_lnum_by_bufnr = {}
    let skipped_without_bufnr = []
    let skipped_without_lnum = []

    let idx = -1
    for entry in parsed_entries
        let idx += 1
        if !file_mode
            if neomake#statusline#AddQflistCount(entry)
                let counts_changed = 1
            endif
        endif

        if !entry.bufnr
            if debug
                let skipped_without_bufnr += [idx]
            endif
            continue
        endif

        if file_mode
            if neomake#statusline#AddLoclistCount(entry.bufnr, entry)
                let counts_changed = 1
            endif
        endif

        if !entry.lnum
            if debug
                let skipped_without_lnum += [idx]
            endif
            continue
        endif

        if !has_key(entries_with_lnum_by_bufnr, entry.bufnr)
            let entries_with_lnum_by_bufnr[entry.bufnr] = []
            let signs_by_bufnr[entry.bufnr] = []
        endif

        if do_highlight || g:neomake_place_signs
            " NOTE: only lnum/type required for signs.  Similar for do_highlight?!
            call add(entries_with_lnum_by_bufnr[entry.bufnr], entry)
        endif

        " Track all errors by buffer and line
        let entry.maker_name = maker_name
        call neomake#_add_error(maker_type, entry)
    endfor

    " Handle placing signs and highlights.
    for [b, entries] in items(entries_with_lnum_by_bufnr)
        if g:neomake_place_signs
            call neomake#signs#PlaceSigns(b, entries, maker_type)
        endif
        if do_highlight
            for entry in entries
                call neomake#highlights#AddHighlight(entry, maker_type)
            endfor
        endif
    endfor

    if !empty(skipped_without_bufnr)
        call neomake#log#debug(printf('Skipped %d entries without bufnr: %s.',
                    \ len(skipped_without_bufnr),
                    \ string(map(skipped_without_bufnr, 'a:entries[v:val]'))), a:jobinfo)
    endif

    if !empty(skipped_without_lnum)
        call neomake#log#debug(printf(
                    \ 'Could not place signs for %d entries without line number: %s.',
                    \ len(skipped_without_lnum),
                    \ string(map(skipped_without_lnum, 'a:entries[v:val]'))), a:jobinfo)
    endif

    let new_list = make_list.entries
    if !counts_changed
        let counts_changed = new_list != prev_list
    endif
    if counts_changed
        call neomake#utils#hook('NeomakeCountsChanged', {'reset': 0, 'jobinfo': a:jobinfo})
    endif

    if has_key(a:jobinfo, '_delayed_qf_autocmd') && exists('#QuickfixCmdPost')
        " NOTE: need to use :silent, since we can only check the event, but
        " not the pattern - `exists()` for 'laddexpr' will not match '*'.
        silent call neomake#compat#doautocmd(a:jobinfo._delayed_qf_autocmd)
        unlet a:jobinfo._delayed_qf_autocmd
    endif

    if !empty(new_list)
        call s:HandleLoclistQflistDisplay(a:jobinfo, new_list)
    endif
    call neomake#highlights#ShowHighlights()
    return g:neomake#action_queue#processed
endfunction

function! s:ProcessJobOutput(jobinfo, lines, source, ...) abort
    if s:need_to_postpone_loclist(a:jobinfo)
        return neomake#action_queue#add(['BufEnter', 'WinEnter'], [s:function('s:ProcessJobOutput'),
                    \ [a:jobinfo, a:lines, a:source]])
    endif
    if !a:0
        return s:pcall('s:ProcessJobOutput', [a:jobinfo, a:lines, a:source])
    endif

    let maker = a:jobinfo.maker
    call neomake#log#debug(printf('Processing %d lines of output.',
                \ len(a:lines)), a:jobinfo)
    let cd_error = a:jobinfo.cd()
    if !empty(cd_error)
        call neomake#log#debug(printf(
                    \ "Could not change to job's cwd (%s): %s.",
                    \ a:jobinfo.cd_from_setting, cd_error), a:jobinfo)
    endif
    try
        if has_key(maker, 'process_json') || has_key(maker, 'process_output')
            if has_key(maker, 'process_json')
                let method = 'process_json'
                let output = join(a:lines, "\n")
                try
                    let json = neomake#compat#json_decode(output)
                catch
                    let error = printf(
                                \ 'Failed to decode JSON: %s (output: %s).',
                                \ substitute(v:exception, '^Neomake: ', '', ''), string(output))
                    call neomake#log#exception(error, a:jobinfo)
                    return g:neomake#action_queue#not_processed
                endtry
                call neomake#log#debug(printf(
                            \ "Calling maker's process_json method with %d JSON entries.",
                            \ len(json)), a:jobinfo)
                let entries = call(maker.process_json, [{
                            \ 'json': json,
                            \ 'source': a:source,
                            \ 'jobinfo': a:jobinfo}], maker)
            else
                call neomake#log#debug(printf(
                            \ "Calling maker's process_output method with %d lines of output on %s.",
                            \ len(a:lines), a:source), a:jobinfo)
                let method = 'process_output'
                let entries = call(maker.process_output, [{
                            \ 'output': a:lines,
                            \ 'source': a:source,
                            \ 'jobinfo': a:jobinfo}], maker)
            endif
            if type(entries) != type([])
                call neomake#log#error(printf('The %s method for maker %s did not return a list, but: %s.',
                            \ method, maker.name, string(entries)[:100]), a:jobinfo)
                return g:neomake#action_queue#not_processed
            elseif !empty(entries) && type(entries[0]) != type({})
                call neomake#log#error(printf('The %s method for maker %s did not return a list of dicts, but: %s.',
                            \ method, maker.name, string(entries)[:100]), a:jobinfo)
                return g:neomake#action_queue#not_processed
            endif
            return s:ProcessEntries(a:jobinfo, entries)
        endif

        " Old-school handling through errorformat.
        if has_key(maker, 'mapexpr')
            let neomake_bufname = fnamemodify(bufname(a:jobinfo.bufnr), ':p')
            " @vimlint(EVL102, 1, l:neomake_bufdir)
            let neomake_bufdir = fnamemodify(neomake_bufname, ':h')
            " @vimlint(EVL102, 1, l:neomake_output_source)
            let neomake_output_source = a:source
            call map(a:lines, maker.mapexpr)
        endif

        if !empty(a:lines)
            call s:AddExprCallback(a:jobinfo, a:lines)
        endif
    catch /^\%(Vim\%((\a\+)\)\=:\%(E48\|E523\)\)\@!/  " everything, but E48/E523 (sandbox / not allowed here)
        if v:exception ==# 'NeomakeTestsException'
            throw v:exception
        endif
        call neomake#log#exception(printf(
                    \ 'Error during output processing for %s: %s.',
                    \ a:jobinfo.maker.name, v:exception), a:jobinfo)
        return
    finally
        call a:jobinfo.cd_back()
    endtry
    return g:neomake#action_queue#processed
endfunction

function! s:process_pending_output(jobinfo, lines, source, ...) abort
    let retry_events = s:need_to_postpone_output_processing(a:jobinfo)
    if empty(retry_events)
        let retry_events = s:ProcessPendingOutput(a:jobinfo, a:lines, a:source)
        if empty(retry_events)
            return g:neomake#action_queue#processed
        endif
    endif
    call add(a:jobinfo.pending_output, [a:lines, a:source])
    if index(neomake#action_queue#get_queued_actions(a:jobinfo),
                \ ['process_pending_output', retry_events]) == -1
        return neomake#action_queue#add(retry_events, [s:function('s:process_pending_output'), [a:jobinfo, [], a:source, retry_events]])
    endif
    return g:neomake#action_queue#not_processed
endfunction

function! s:ProcessPendingOutput(jobinfo, lines, source) abort
    if a:jobinfo.file_mode
        let window_make_ids = get(w:, 'neomake_make_ids', [])
        if index(window_make_ids, a:jobinfo.make_id) == -1
            if !bufexists(a:jobinfo.bufnr)
                call neomake#log#info('No buffer found for output!', a:jobinfo)
                return []
            endif

            if a:jobinfo.bufnr != bufnr('%')
                call neomake#log#debug(printf('Skipped pending job output for another buffer (current=%d).', bufnr('%')), a:jobinfo)
                return ['BufEnter', 'WinEnter']
            elseif neomake#core#get_tabwin_for_makeid(a:jobinfo.make_id) != [-1, -1]
                call neomake#log#debug('Skipped pending job output (not in origin window).', a:jobinfo)
                return ['WinEnter']
            else
                call neomake#log#debug("Processing pending output for job's buffer in new window.", a:jobinfo)
                let w:neomake_make_ids = add(get(w:, 'neomake_make_ids', []), a:jobinfo.make_id)
            endif
        endif
    endif

    " Process any pending output first.
    if !empty(a:jobinfo.pending_output)
        let outputs = {'stdout': [], 'stderr': []}
        for [lines, source] in a:jobinfo.pending_output
            call extend(outputs[source], lines)
        endfor
        for [source, lines] in items(outputs)
            if !empty(lines)
                call s:ProcessJobOutput(a:jobinfo, lines, source)
            endif
        endfor
        call neomake#log#debug(printf(
                    \ 'Processed %d pending outputs.', len(a:jobinfo.pending_output)),
                    \ a:jobinfo)
        call neomake#action_queue#remove(a:jobinfo, s:function('s:process_pending_output'))
    endif

    if !empty(a:lines)
        call s:ProcessJobOutput(a:jobinfo, a:lines, a:source)
    endif

    " Clean job if it had exited already.
    if !empty(a:jobinfo.pending_output)
        let a:jobinfo.pending_output = []
        if has_key(a:jobinfo, 'exit_code')
            " XXX: add test (tested manually)
            call s:CleanJobinfo(a:jobinfo)
        endif
    endif
    return []
endfunction

" Do we need to postpone location list processing (creation and :laddexpr)?
function! s:need_to_postpone_loclist(jobinfo) abort
    if !a:jobinfo.file_mode
        return 0
    endif
    if index(get(w:, 'neomake_make_ids', []), a:jobinfo.make_id) != -1
        return 0
    endif
    call neomake#log#debug('Postponing location list processing.', a:jobinfo)
    return 1
endfunction

" TODO: merge with s:handle_locqf_list_for_finished_jobs.
let s:has_getcmdwintype = exists('*getcmdwintype')
function! s:need_to_postpone_output_processing(jobinfo) abort
    " We can only process output (change the location/quickfix list) in
    " certain modes, otherwise e.g. the visual selection gets lost.
    if neomake#compat#in_completion()
        call neomake#log#debug('Not processing output during completion.', a:jobinfo)
        return ['CompleteDone']
    endif
    let mode = neomake#compat#get_mode()
    if index(['n', 'i'], mode) == -1
        call neomake#log#debug('Not processing output for mode "'.mode.'".', a:jobinfo)
        return ['BufEnter', 'WinEnter', 'InsertLeave', 'CursorHold', 'CursorHoldI']
    endif
    if s:has_getcmdwintype && !empty(getcmdwintype())
        call neomake#log#debug('Not processing output from command-line window "'.getcmdwintype().'".', a:jobinfo)
        return ['InsertLeave', 'CursorHold', 'CursorHoldI']
    endif
    return []
endfunction

function! s:RegisterJobOutput(jobinfo, lines, source) abort
    " Allow to filter output (storing the setting on the jobinfo lazily).
    if !has_key(a:jobinfo, 'filter_output')
        let a:jobinfo.filter_output = neomake#utils#GetSetting('filter_output', a:jobinfo.maker, '', a:jobinfo.ft, a:jobinfo.bufnr)
    endif
    if !empty(a:jobinfo.filter_output)
        call call(a:jobinfo.filter_output, [
                    \ a:lines, {'source': a:source, 'jobinfo': a:jobinfo}],
                    \ a:jobinfo.maker)
    endif

    if empty(a:lines)
        return
    endif

    " Register unexpected output.
    if a:jobinfo.output_stream !=# 'both' && a:jobinfo.output_stream !=# a:source
        if !has_key(a:jobinfo, 'unexpected_output')
            let a:jobinfo.unexpected_output = {}
        endif
        if !has_key(a:jobinfo.unexpected_output, a:source)
            let a:jobinfo.unexpected_output[a:source] = []
        endif
        let a:jobinfo.unexpected_output[a:source] += a:lines
        return
    endif

    let make_info = s:make_info[a:jobinfo.make_id]
    if has_key(make_info, 'entries_list')  " use_list option
        " Process output for list processing.
        call s:process_pending_output(a:jobinfo, a:lines, a:source)
    endif
endfunction

function! s:vim_output_handler(channel, output, event_type) abort
    let channel_id = ch_info(a:channel)['id']
    let jobinfo = get(s:jobs, get(s:map_job_ids, channel_id, -1), {})
    if empty(jobinfo)
        call neomake#log#debug(printf("warn: job '%s' not found for output on %s.",
                    \ a:channel, a:event_type))
        return
    endif
    let data = split(a:output, '\r\?\n', 1)
    call s:output_handler_queued(jobinfo, data, a:event_type, 0)
endfunction

function! s:vim_output_handler_stdout(channel, output) abort
    call s:vim_output_handler(a:channel, a:output, 'stdout')
endfunction

function! s:vim_output_handler_stderr(channel, output) abort
    call s:vim_output_handler(a:channel, a:output, 'stderr')
endfunction

function! s:vim_exit_handler(channel) abort
    let channel_id = ch_info(a:channel)['id']
    let jobinfo = get(s:jobs, get(s:map_job_ids, channel_id, -1), {})
    if empty(jobinfo)
        try
            let job_info = job_info(ch_getjob(a:channel))
        catch /^Vim(let):E916:/
            " Might happen with older Vim (8.0.69, but not 8.0.586).
            call neomake#log#debug(printf('exit: job not found: %s.', a:channel))
            return
        endtry
        call neomake#log#debug(printf('exit: job not found: %s (%s).', a:channel, job_info))
        return
    endif
    let job_info = job_info(ch_getjob(a:channel))

    " Handle failing starts from Vim here.
    let status = job_info['exitval']
    if status == 122  " Vim uses EXEC_FAILED, but only on Unix?!
        let jobinfo.failed_to_start = 1
        " The error is on stderr.
        let error = 'Vim job failed to run: '.substitute(join(jobinfo.stderr), '\v\s+$', '', '').'.'
        let jobinfo.stderr = []
        call neomake#log#error(error)
        call s:CleanJobinfo(jobinfo)
    else
        call s:exit_handler(jobinfo, status)
    endif
endfunction

" @vimlint(EVL108, 1)
if has('nvim-0.2.0')
" @vimlint(EVL108, 0)
    function! s:nvim_output_handler(job_id, data, event_type) abort
        let jobinfo = get(s:jobs, get(s:map_job_ids, a:job_id, -1), {})
        if empty(jobinfo)
            call neomake#log#debug(printf('output [%s]: job %d not found.', a:event_type, a:job_id))
            return
        endif
        if a:data == [''] && !exists('jobinfo[a:event_type]')
            " EOF in Neovim (see :h on_data).
            return
        endif
        call s:output_handler_queued(jobinfo, copy(a:data), a:event_type, 1)
    endfunction
else
    " Neovim: register output from jobs as quick as possible, and trigger
    " processing through a timer.
    " This works around https://github.com/neovim/neovim/issues/5889).
    " NOTE: a:data is never [''] here (like with other/newer Neovim
    " handlers)
    let s:nvim_output_handler_queue = []
    function! s:nvim_output_handler(job_id, data, event_type) abort
        let jobinfo = get(s:jobs, get(s:map_job_ids, a:job_id, -1), {})
        if empty(jobinfo)
            call neomake#log#debug(printf('output [%s]: job %d not found.', a:event_type, a:job_id))
            return
        endif
        let args = [jobinfo, copy(a:data), a:event_type, 1]
        call add(s:nvim_output_handler_queue, args)
        if !exists('jobinfo._nvim_in_handler')
            let jobinfo._nvim_in_handler = 1
        else
            let jobinfo._nvim_in_handler += 1
        endif
        if !exists('s:nvim_output_handler_timer')
            let s:nvim_output_handler_timer = timer_start(0, function('s:nvim_output_handler_cb'))
        endif
    endfunction

    function! s:nvim_output_handler_cb(_timer) abort
        while !empty(s:nvim_output_handler_queue)
            let args = remove(s:nvim_output_handler_queue, 0)
            let jobinfo = args[0]
            call call('s:output_handler', args)
            let jobinfo._nvim_in_handler -= 1

            if !jobinfo._nvim_in_handler
                " Trigger previously delayed exit handler.
                unlet jobinfo._nvim_in_handler
                if exists('jobinfo._exited_while_in_handler')
                    call neomake#log#debug('Trigger delayed exit.', jobinfo)
                    call s:exit_handler(jobinfo, jobinfo._exited_while_in_handler)
                endif
            endif
        endwhile
        unlet! s:nvim_output_handler_timer
    endfunction
endif

" Exit handler for buffered output with Neovim.
" In this case the output gets stored on the jobstart-options dict.
function! s:nvim_exit_handler_buffered(job_id, data, _event_type) abort
    let jobinfo = get(s:jobs, get(s:map_job_ids, a:job_id, -1), {})
    if empty(jobinfo)
        call neomake#log#debug(printf('exit: job not found: %d.', a:job_id))
        return
    endif

    for stream in ['stdout', 'stderr']
        if has_key(jobinfo.jobstart_opts, stream)
            let data = copy(jobinfo.jobstart_opts[stream])
            if data == ['']
                " EOF in Neovim (see :h on_data).
                continue
            endif
            call s:output_handler(jobinfo, data, stream, 1)
        endif
    endfor

    call s:exit_handler(jobinfo, a:data)
endfunction

function! s:nvim_exit_handler(job_id, data, _event_type) abort
    let jobinfo = get(s:jobs, get(s:map_job_ids, a:job_id, -1), {})
    if empty(jobinfo)
        call neomake#log#debug(printf('exit: job not found: %d.', a:job_id))
        return
    endif
    call s:exit_handler(jobinfo, a:data)
endfunction

function! s:exit_handler(jobinfo, data) abort
    let jobinfo = a:jobinfo
    let jobinfo.exit_code = a:data
    if get(jobinfo, 'canceled')
        call neomake#log#debug('exit: job was canceled.', jobinfo)
        call s:CleanJobinfo(jobinfo)
        return
    endif
    let maker = jobinfo.maker

    if exists('jobinfo._output_while_in_handler') || exists('jobinfo._nvim_in_handler')
        let jobinfo._exited_while_in_handler = a:data
        call neomake#log#debug(printf('exit (delayed): %s: %s.',
                    \ maker.name, string(a:data)), jobinfo)
        return
    endif
    call neomake#log#debug(printf('exit: %s: %s.',
                \ maker.name, string(a:data)), jobinfo)

    let jobinfo._in_exit_handler = 1
    try
        " Handle any unfinished lines from stdout/stderr callbacks.
        for event_type in ['stdout', 'stderr']
            if has_key(jobinfo, event_type)
                let lines = jobinfo[event_type]
                if !empty(lines)
                    if lines[-1] ==# ''
                        call remove(lines, -1)
                    endif
                    if !empty(lines)
                        call s:RegisterJobOutput(jobinfo, lines, event_type)
                    endif
                    unlet jobinfo[event_type]
                endif
            endif
        endfor

        if !get(jobinfo, 'failed_to_start')
            let l:ExitCallback = neomake#utils#GetSetting('exit_callback',
                        \ extend(copy(jobinfo), maker), 0, jobinfo.ft, jobinfo.bufnr)
            if ExitCallback isnot# 0
                let callback_dict = { 'status': jobinfo.exit_code,
                                    \ 'name': maker.name,
                                    \ 'has_next': !empty(s:make_info[jobinfo.make_id].jobs_queue) }
                try
                    if type(ExitCallback) == type('')
                        let l:ExitCallback = function(ExitCallback)
                    endif
                    call call(ExitCallback, [callback_dict], jobinfo)
                catch
                    call neomake#log#error(printf(
                                \ 'Error during exit_callback: %s.', v:exception),
                                \ jobinfo)
                endtry
            endif
        endif

        if s:async
            if has('nvim') || jobinfo.exit_code != 122
                call neomake#log#debug(printf(
                            \ '%s: completed with exit code %d.',
                            \ maker.name, jobinfo.exit_code), jobinfo)
            endif
            let jobinfo.finished = 1
        endif

        if has_key(jobinfo, 'unexpected_output')
            redraw
            for [source, output] in items(jobinfo.unexpected_output)
                let msg = printf('%s: unexpected output on %s: ', maker.name, source)
                call neomake#log#debug(msg . join(output, '\n') . '.', jobinfo)

                echohl WarningMsg
                echom printf('Neomake: %s%s', msg, output[0])
                for line in output[1:-1]
                    echom line
                endfor
                echohl None
            endfor
            call neomake#log#error(printf(
                        \ '%s: unexpected output. See :messages for more information.', maker.name), jobinfo)
        endif
    finally
        unlet jobinfo._in_exit_handler
    endtry
    call s:handle_next_job(jobinfo)
endfunction

function! s:output_handler_queued(jobinfo, data, event_type, trim_CR) abort
    let jobinfo = a:jobinfo
    if exists('jobinfo._output_while_in_handler')
        call neomake#log#debug(printf('Queuing: %s: %s: %s.',
                    \ a:event_type, jobinfo.maker.name, string(a:data)), jobinfo)
        let jobinfo._output_while_in_handler += [[jobinfo, a:data, a:event_type, a:trim_CR]]
        return
    else
        let jobinfo._output_while_in_handler = []
    endif

    call s:output_handler(jobinfo, a:data, a:event_type, a:trim_CR)

    " Process queued events that might have arrived by now.
    " The attribute might be unset here, since output_handler might have
    " been interrupted.
    if exists('jobinfo._output_while_in_handler')
        while has_key(jobinfo, '_output_while_in_handler') && !empty(jobinfo._output_while_in_handler)
            let args = remove(jobinfo._output_while_in_handler, 0)
            call call('s:output_handler', args)
        endwhile
        unlet! jobinfo._output_while_in_handler
    endif
    " Trigger previously delayed exit handler.
    if exists('jobinfo._exited_while_in_handler')
        call neomake#log#debug('Trigger delayed exit.', jobinfo)
        call s:exit_handler(jobinfo, jobinfo._exited_while_in_handler)
    endif
endfunction

function! s:output_handler(jobinfo, data, event_type, trim_CR) abort
    let jobinfo = a:jobinfo
    call neomake#log#debug(printf('output on %s: %s.',
                \ a:event_type, string(a:data)), jobinfo)
    if get(jobinfo, 'canceled')
        call neomake#log#debug('Ignoring output (job was canceled).', jobinfo)
        return
    endif
    let data = copy(a:data)
    if a:trim_CR && !empty(a:data)
        call map(data, "substitute(v:val, '\\r$', '', '')")
    endif
    let last_event_type = get(jobinfo, 'event_type', a:event_type)

    " data is a list of 'lines' read. Each element *after* the first
    " element represents a newline.
    if has_key(jobinfo, a:event_type)
        let jobinfo[a:event_type][-1] .= data[0]
        call extend(jobinfo[a:event_type], data[1:])
    else
        let jobinfo[a:event_type] = data
    endif

    if !jobinfo.buffer_output || last_event_type !=# a:event_type
        let lines = jobinfo[a:event_type][:-2]
        let jobinfo[a:event_type] = jobinfo[a:event_type][-1:]

        if !empty(lines)
            call s:RegisterJobOutput(jobinfo, lines, a:event_type)
        endif
    endif
endfunction

function! s:abort_next_makers(make_id) abort
    let jobs_queue = s:make_info[a:make_id].jobs_queue
    if !empty(jobs_queue)
        let next_makers = join(map(copy(jobs_queue), 'v:val.maker.name'), ', ')
        call neomake#log#info('Aborting next makers: '.next_makers.'.', {'make_id': a:make_id})
        let s:make_info[a:make_id].aborted_jobs = copy(s:make_info[a:make_id].jobs_queue)
        let s:make_info[a:make_id].jobs_queue = []
    endif
endfunction

function! s:handle_next_job(prev_jobinfo) abort
    let make_id = get(a:prev_jobinfo, 'make_id', s:make_id)
    if !has_key(s:make_info, make_id)
        return {}
    endif
    let make_info = s:make_info[make_id]

    if !empty(a:prev_jobinfo)
        let status = get(a:prev_jobinfo, 'exit_code', 0)
        if status != 0 && index([122, 127], status) == -1
            " TODO: mark maker.exe as non-executable with status 127, and
            "       maybe re-introduce a wrapper for `executable()` to handle it.
            "       Ref: https://github.com/neomake/neomake/issues/1699
            if neomake#utils#GetSetting('serialize_abort_on_error', a:prev_jobinfo.maker, 0, a:prev_jobinfo.ft, a:prev_jobinfo.bufnr)
                let a:prev_jobinfo.aborted = 1
                call s:abort_next_makers(make_id)
                call s:CleanJobinfo(a:prev_jobinfo)
                return {}
            endif
        endif
        call s:CleanJobinfo(a:prev_jobinfo)
        if !has_key(s:make_info, make_id)
            " Last job was cleaned.
            return {}
        endif

        let serializing_for_job = get(make_info, 'serializing_for_job')
        if serializing_for_job
            if serializing_for_job != a:prev_jobinfo.id
                call neomake#log#debug(printf('waiting for job %d to finish.', serializing_for_job))
                return {}
            endif
            unlet make_info.serializing_for_job
        endif
    endif

    " Create job from the start of the queue, returning it.
    while !empty(make_info.jobs_queue)
        let options = remove(make_info.jobs_queue, 0)
        let maker = options.maker
        if empty(maker)
            continue
        endif

        " Serialization of jobs, always for non-async Vim.
        if !has_key(options, 'serialize')
            if !s:async || neomake#utils#GetSetting('serialize', maker, 0, options.ft, options.bufnr)
                let options.serialize = 1
            else
                let options.serialize = 0
            endif
        endif
        try
            let jobinfo = s:MakeJob(make_id, options)
        catch /^Neomake: /
            let log_context = extend(options, {'make_id': make_id})
            if v:exception =~# '\v^Neomake: skip_job: '
                let msg = substitute(v:exception, '^Neomake: skip_job: ', '', '')
                call neomake#log#debug(printf('Skipping job: %s', msg), log_context)
            else
                let error = substitute(v:exception, '^Neomake: ', '', '')
                call neomake#log#exception(error, log_context)

                if options.serialize
                    if neomake#utils#GetSetting('serialize_abort_on_error', maker, 0, options.ft, options.bufnr)
                        call s:abort_next_makers(make_id)
                        break
                    endif
                endif
            endif
            continue
        endtry
        if !empty(jobinfo)
            return jobinfo
        endif
    endwhile

    " Cleanup make info, but only if there are no queued actions.
    for [_, v] in g:neomake#action_queue#_s.action_queue
        if v[1][0] == make_info
            call neomake#log#debug('Skipping cleaning of make info for queued actions.', make_info)
            return {}
        endif
    endfor
    call s:clean_make_info(make_info)
    return {}
endfunction

function! neomake#_add_error(maker_type, entry) abort
    if !has_key(s:current_errors[a:maker_type], a:entry.bufnr)
        let s:current_errors[a:maker_type][a:entry.bufnr] = {}
    endif
    if !has_key(s:current_errors[a:maker_type][a:entry.bufnr], a:entry.lnum)
        let s:current_errors[a:maker_type][a:entry.bufnr][a:entry.lnum] = [a:entry]
    else
        call add(s:current_errors[a:maker_type][a:entry.bufnr][a:entry.lnum], a:entry)
    endif
endfunction

function! neomake#get_nearest_error() abort
    let buf = bufnr('%')
    let ln = line('.')
    let ln_errors = []

    for maker_type in ['file', 'project']
        let buf_errors = get(s:current_errors[maker_type], buf, {})
        let ln_errors += get(buf_errors, ln, [])
    endfor

    if empty(ln_errors)
        return {}
    endif

    if len(ln_errors) > 1
        call sort(ln_errors, function('neomake#utils#sort_by_col'))
    endif
    return ln_errors[0]
endfunction

function! neomake#GetCurrentErrorMsg() abort
    let entry = neomake#get_nearest_error()
    if empty(entry)
        return ''
    endif
    let r = entry.maker_name . ': ' . entry.text
    let suffix = entry.type . (entry.nr != -1 ? entry.nr : '')
    if !empty(suffix)
        let r .= ' ('.suffix.')'
    endif
    return r
endfunction

function! neomake#EchoCurrentError(...) abort
    if !get(g:, 'neomake_echo_current_error', 1)
        return
    endif
    " a:1 might be a timer from the VimResized event.
    let force = a:0 ? a:1 : 0

    let message = neomake#GetCurrentErrorMsg()
    if empty(message)
        if exists('s:neomake_last_echoed_error')
            echon ''
            unlet s:neomake_last_echoed_error
        endif
        return
    endif
    if !force && exists('s:neomake_last_echoed_error')
                \ && s:neomake_last_echoed_error == message
        return
    endif
    let s:neomake_last_echoed_error = message
    call neomake#utils#WideMessage(message)
endfunction

function! neomake#CursorMoved() abort
    call neomake#EchoCurrentError()
    call neomake#virtualtext#handle_current_error()
endfunction

function! s:cursormoved_delayed_cb(...) abort
    if getpos('.') == s:cursormoved_last_pos
        call neomake#CursorMoved()
    endif
endfunction
function! neomake#CursorMovedDelayed() abort
    if exists('s:cursormoved_timer')
        call timer_stop(s:cursormoved_timer)
    endif
    let delay = get(g:, 'neomake_cursormoved_delay', 100)
    let s:cursormoved_timer = timer_start(delay, function('s:cursormoved_delayed_cb'))
    let s:cursormoved_last_pos = getpos('.')
endfunction

function! neomake#Make(file_mode_or_options, ...) abort
    if type(a:file_mode_or_options) == type({})
        return s:Make(a:file_mode_or_options)
    endif

    let file_mode = a:file_mode_or_options
    let options = {'file_mode': file_mode}
    if file_mode
        let options.ft = &filetype
    endif
    if a:0
        if !empty(a:1)
            let maker_names = a:1
            " Split names on non-breaking space (annotation from completion).
            call map(maker_names, "type(v:val) == 1 ? split(v:val, ' (')[0] : v:val")
            let options.enabled_makers = a:1
        endif
        if a:0 > 1
            let options.exit_callback = a:2
        endif
    endif
    return map(copy(s:Make(options)), 'v:val.id')
endfunction

function! neomake#ShCommand(bang, sh_command, ...) abort
    let maker = neomake#utils#MakerFromCommand(a:sh_command)
    let maker.name = 'sh: '.a:sh_command
    let maker.errorformat = '%m'
    let maker.default_entry_type = ''
    let options = {
                \ 'enabled_makers': [maker],
                \ 'file_mode': 0,
                \ 'output_stream': 'both',
                \ 'buffer_output': !a:bang,
                \ }
    if a:0
        call extend(options, a:1)
    endif
    let jobinfos = s:Make(options)
    return empty(jobinfos) ? -1 : jobinfos[0].id
endfunction

function! neomake#Sh(sh_command, ...) abort
    " Deprecated, but documented.
    let options = a:0 ? { 'exit_callback': a:1 } : {}
    return neomake#ShCommand(0, a:sh_command, options)
endfunction

function! neomake#map_makers(makers, ft, auto_enabled) abort
    let makers = []
    let errors = []
    let get_args = a:ft is# -1 ? [] : [a:ft]
    for maker in a:makers
        try
            let m = call('neomake#GetMaker', [maker] + get_args)
        catch /^Neomake: /
            call add(errors, substitute(v:exception, '^Neomake: ', '', '').'.')
            unlet maker  " vim73/vim-trusty
            continue
        endtry
        call add(makers, m)
        unlet maker  " vim73/vim-trusty
    endfor
    if !empty(errors)
        let log_context = get(s:make_info, s:make_id, {})
        for error in errors
            if a:auto_enabled
                call neomake#log#debug(error, log_context)
            else
                call neomake#log#error(error, log_context)
            endif
        endfor
    endif
    return makers
endfunction