blob: 13192761b367a4c1de64ef767a94c459411b8c42 [file] [log] [blame]
ichizok672776d2022-01-31 12:27:18 +00001" This script generates the table nv_cmd_idx[] which contains the index in
2" nv_cmds[] table (normal.c) for each of the command character supported in
3" normal/visual mode.
4" This is used to speed up the command lookup in nv_cmds[].
5"
6" Script should be run using "make nvcmdidxs", every time the nv_cmds[] table
7" in src/nv_cmds.h changes.
8"
9" This is written in legacy Vim script so that it can be run by a slightly
10" older Vim version.
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000011
ichizok672776d2022-01-31 12:27:18 +000012" Generate the table of normal/visual mode command characters and their
13" corresponding index.
14let cmd = 'create_nvcmdidxs'
15if has('unix')
16 let cmd = './' .. cmd
17endif
18let nv_cmdtbl = systemlist(cmd)->map({i, ch -> {'idx': i, 'cmdchar': ch}})
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000019
ichizok672776d2022-01-31 12:27:18 +000020" sort the table by the command character
21call sort(nv_cmdtbl, {a, b -> a.cmdchar - b.cmdchar})
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000022
ichizok672776d2022-01-31 12:27:18 +000023" Compute the highest index upto which the command character can be directly
24" used as an index.
25let nv_max_linear = 0
26for i in range(nv_cmdtbl->len())
27 if i != nv_cmdtbl[i].cmdchar
28 let nv_max_linear = i - 1
29 break
30 endif
31endfor
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000032
ichizok672776d2022-01-31 12:27:18 +000033" Generate a header file with the table
34let output =<< trim END
35 /*
36 * Automatically generated code by the create_nvcmdidxs.vim script.
37 *
38 * Table giving the index in nv_cmds[] to lookup based on
39 * the command character.
40 */
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000041
ichizok672776d2022-01-31 12:27:18 +000042 // nv_cmd_idx[<normal mode command character>] => nv_cmds[] index
43 static const unsigned short nv_cmd_idx[] =
44 {
45END
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000046
ichizok672776d2022-01-31 12:27:18 +000047" Add each command character in comment and the corresponding index
48let output += nv_cmdtbl->map({_, v ->
49 \ printf(' /* %5d */ %3d,', v.cmdchar, v.idx)})
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000050
ichizok672776d2022-01-31 12:27:18 +000051let output += ['};', '',
52 \ '// The highest index for which',
53 \ '// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]']
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000054
ichizok672776d2022-01-31 12:27:18 +000055let output += ['static const int nv_max_linear = ' .. nv_max_linear .. ';']
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000056
ichizok672776d2022-01-31 12:27:18 +000057call writefile(output, "nv_cmdidxs.h")
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +000058quit
59
ichizok672776d2022-01-31 12:27:18 +000060" vim: shiftwidth=2 sts=2 expandtab