1 " SPDX-License-Identifier: NONE
2 " settings & syntax hilighting for FRR codebase
3 " 2019 by David Lamparter, placed in public domain
9 syn match cFormat display "%\(\d\+\$\)\=[-+' #0*]*\(\d*\|\*\|\*\d\+\$\)\(\.\(\d*\|\*\|\*\d\+\$\)\)\=\([hlLjzt]\|ll\|hh\)\=\([aAbiuoxXDOUfFeEgGcCsSn]\|[pd]\([A-Z][A-Z0-9]*[a-z]*\|\)\|\[\^\=.[^]]*\]\)" contained
10 syn match cFormat display "%%" contained
12 syn keyword cIterator frr_each frr_each_safe frr_each_from
13 syn keyword cMacroOp offsetof container_of container_of_null array_size
15 syn keyword cStorageClass atomic
16 syn keyword cFormatConst PRId64 PRIu64 PRIx64
17 syn keyword cFormatConst PRId32 PRIu32 PRIx32
18 syn keyword cFormatConst PRId16 PRIu16 PRIx16
19 syn keyword cFormatConst PRId8 PRIu8 PRIx8
21 " you can unlink these by just giving them their own hilighting / color
22 hi link cFormatConst cFormat
23 hi link cIterator cRepeat
24 hi link cMacroOp cOperator
28 setlocal cinoptions=:0,(0,u4,w1,W8
30 setlocal softtabstop=0
36 " auto-apply the above based on path rules
37 "autocmd BufRead,BufNewFile /home/.../frr/*.[ch] call CStyleFRR()
39 " only load xref file once, remember on script-scope
43 " call directly to force reload with :call FRRLoadXrefJson()
44 function! FRRLoadXrefJson() abort
45 let s:xrefpath = findfile("frr.xref", ".;")
47 throw "frr.xref JSON file not found in current or parent directories"
49 let xreflines = readfile(s:xrefpath)
50 let s:xrefjson = json_decode(join(xreflines, "\n"))
53 function! FRRXrefJson() abort
55 call FRRLoadXrefJson()
60 function! FRRGotoXref(ident) abort
61 let refs = FRRXrefJson()["refs"]
62 if has_key(refs, a:ident)
63 " TODO: in rare cases, one ID may occur in multiple places.
64 " Add some UI for that. (This happens if the exact same
65 " format string is logged in multiple places in the same
67 let loc = refs[a:ident][0]
68 let basepath = fnamemodify(s:xrefpath, ":p:h")
69 let path = fnamemodify(basepath . "/" . loc["file"], ":.")
70 execute "e ".fnameescape(path)
71 execute ":".loc["line"]
73 echoerr printf("cannot find xref with ID %s", a:ident)
77 " invoke as :GotoXref 23456-ABCDE
78 command! -bang -nargs=1 GotoXref :call FRRGotoXref(<q-args>)