1declare-option -docstring "shell command run to build the project" \
2    str makecmd make
3declare-option -docstring "pattern that describes lines containing information about errors in the output of the `makecmd` command" \
4    str make_error_pattern " (?:fatal )?error:"
5
6declare-option -docstring "name of the client in which utilities display information" \
7    str toolsclient
8declare-option -hidden int make_current_error_line
9
10define-command -params .. \
11    -docstring %{
12        make [<arguments>]: make utility wrapper
13        All the optional arguments are forwarded to the make utility
14     } make %{ evaluate-commands %sh{
15     output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-make.XXXXXXXX)/fifo
16     mkfifo ${output}
17     ( eval "${kak_opt_makecmd}" "$@" > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null
18
19     printf %s\\n "evaluate-commands -try-client '$kak_opt_toolsclient' %{
20               edit! -fifo ${output} -scroll *make*
21               set-option buffer filetype make
22               set-option buffer make_current_error_line 0
23               hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } }
24           }"
25}}
26
27add-highlighter shared/make group
28add-highlighter shared/make/ regex "^((?:\w:)?[^:\n]+):(\d+):(?:(\d+):)?\h+(?:((?:fatal )?error)|(warning)|(note)|(required from(?: here)?))?.*?$" 1:cyan 2:green 3:green 4:red 5:yellow 6:blue 7:yellow
29add-highlighter shared/make/ regex "^\h*(~*(?:(\^)~*)?)$" 1:green 2:cyan+b
30add-highlighter shared/make/ line '%opt{make_current_error_line}' default+b
31
32hook -group make-highlight global WinSetOption filetype=make %{
33    add-highlighter window/make ref make
34    hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/make }
35}
36
37hook global WinSetOption filetype=make %{
38    hook buffer -group make-hooks NormalKey <ret> make-jump
39    hook -once -always window WinSetOption filetype=.* %{ remove-hooks buffer make-hooks }
40}
41
42declare-option -docstring "name of the client in which all source code jumps will be executed" \
43    str jumpclient
44
45define-command -hidden make-open-error -params 4 %{
46    evaluate-commands -try-client %opt{jumpclient} %{
47        edit -existing "%arg{1}" %arg{2} %arg{3}
48        echo -markup "{Information}{\}%arg{4}"
49        try %{ focus }
50    }
51}
52
53define-command -hidden make-jump %{
54    evaluate-commands %{
55        try %{
56            execute-keys gl<a-?> "Entering directory" <ret><a-:>
57            # Try to parse the error into capture groups, failing on absolute paths
58            execute-keys s "Entering directory [`']([^']+)'.*\n([^:/][^:]*):(\d+):(?:(\d+):)?([^\n]+)\z" <ret>l
59            set-option buffer make_current_error_line %val{cursor_line}
60            make-open-error "%reg{1}/%reg{2}" "%reg{3}" "%reg{4}" "%reg{5}"
61        } catch %{
62            execute-keys <a-h><a-l> s "((?:\w:)?[^:]+):(\d+):(?:(\d+):)?([^\n]+)\z" <ret>l
63            set-option buffer make_current_error_line %val{cursor_line}
64            make-open-error "%reg{1}" "%reg{2}" "%reg{3}" "%reg{4}"
65        }
66    }
67}
68
69define-command make-next-error -docstring 'Jump to the next make error' %{
70    evaluate-commands -try-client %opt{jumpclient} %{
71        buffer '*make*'
72        execute-keys "%opt{make_current_error_line}ggl" "/^(?:\w:)?[^:\n]+:\d+:(?:\d+:)?%opt{make_error_pattern}<ret>"
73        make-jump
74    }
75    try %{ evaluate-commands -client %opt{toolsclient} %{ execute-keys %opt{make_current_error_line}g } }
76}
77
78define-command make-previous-error -docstring 'Jump to the previous make error' %{
79    evaluate-commands -try-client %opt{jumpclient} %{
80        buffer '*make*'
81        execute-keys "%opt{make_current_error_line}g" "<a-/>^(?:\w:)?[^:\n]+:\d+:(?:\d+:)?%opt{make_error_pattern}<ret>"
82        make-jump
83    }
84    try %{ evaluate-commands -client %opt{toolsclient} %{ execute-keys %opt{make_current_error_line}g } }
85}
86