ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 1 | " 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 Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 11 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 12 | " Generate the table of normal/visual mode command characters and their |
| 13 | " corresponding index. |
| 14 | let cmd = 'create_nvcmdidxs' |
| 15 | if has('unix') |
| 16 | let cmd = './' .. cmd |
| 17 | endif |
| 18 | let nv_cmdtbl = systemlist(cmd)->map({i, ch -> {'idx': i, 'cmdchar': ch}}) |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 19 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 20 | " sort the table by the command character |
| 21 | call sort(nv_cmdtbl, {a, b -> a.cmdchar - b.cmdchar}) |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 22 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 23 | " Compute the highest index upto which the command character can be directly |
| 24 | " used as an index. |
| 25 | let nv_max_linear = 0 |
| 26 | for i in range(nv_cmdtbl->len()) |
| 27 | if i != nv_cmdtbl[i].cmdchar |
| 28 | let nv_max_linear = i - 1 |
| 29 | break |
| 30 | endif |
| 31 | endfor |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 32 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 33 | " Generate a header file with the table |
| 34 | let 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 Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 41 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 42 | // nv_cmd_idx[<normal mode command character>] => nv_cmds[] index |
| 43 | static const unsigned short nv_cmd_idx[] = |
| 44 | { |
| 45 | END |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 46 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 47 | " Add each command character in comment and the corresponding index |
| 48 | let output += nv_cmdtbl->map({_, v -> |
| 49 | \ printf(' /* %5d */ %3d,', v.cmdchar, v.idx)}) |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 50 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 51 | let output += ['};', '', |
| 52 | \ '// The highest index for which', |
| 53 | \ '// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]'] |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 54 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 55 | let output += ['static const int nv_max_linear = ' .. nv_max_linear .. ';'] |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 56 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 57 | call writefile(output, "nv_cmdidxs.h") |
Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 58 | quit |
| 59 | |
ichizok | 672776d | 2022-01-31 12:27:18 +0000 | [diff] [blame] | 60 | " vim: shiftwidth=2 sts=2 expandtab |