Yegappan Lakshmanan | 4dc0dd8 | 2022-01-29 13:06:40 +0000 | [diff] [blame] | 1 | vim9script |
| 2 | |
| 3 | # This script generates the table nv_cmd_idx[] which contains the index in |
| 4 | # nv_cmds[] table (normal.c) for each of the command character supported in |
| 5 | # normal/visual mode. |
| 6 | # This is used to speed up the command lookup in nv_cmds[]. |
| 7 | # |
| 8 | # Script should be run using "make nvcmdidxs", every time the nv_cmds[] table |
| 9 | # in src/normal.c changes. |
| 10 | |
| 11 | def Create_nvcmdidxs_table() |
| 12 | var nv_cmdtbl: list<dict<number>> = [] |
| 13 | |
| 14 | # Generate the table of normal/visual mode command characters and their |
| 15 | # corresponding index. |
| 16 | var idx: number = 0 |
| 17 | var ch: number |
| 18 | while true |
| 19 | ch = internal_get_nv_cmdchar(idx) |
| 20 | if ch == -1 |
| 21 | break |
| 22 | endif |
| 23 | add(nv_cmdtbl, {idx: idx, cmdchar: ch}) |
| 24 | idx += 1 |
| 25 | endwhile |
| 26 | |
| 27 | # sort the table by the command character |
| 28 | sort(nv_cmdtbl, (a, b) => a.cmdchar - b.cmdchar) |
| 29 | |
| 30 | # Compute the highest index upto which the command character can be directly |
| 31 | # used as an index. |
| 32 | var nv_max_linear: number = 0 |
| 33 | for i in range(nv_cmdtbl->len()) |
| 34 | if i != nv_cmdtbl[i].cmdchar |
| 35 | nv_max_linear = i - 1 |
| 36 | break |
| 37 | endif |
| 38 | endfor |
| 39 | |
| 40 | # Generate a header file with the table |
| 41 | var output: list<string> =<< trim END |
| 42 | /* |
| 43 | * Automatically generated code by the create_nvcmdidxs.vim script. |
| 44 | * |
| 45 | * Table giving the index in nv_cmds[] to lookup based on |
| 46 | * the command character. |
| 47 | */ |
| 48 | |
| 49 | // nv_cmd_idx[<normal mode command character>] => nv_cmds[] index |
| 50 | static const unsigned short nv_cmd_idx[] = |
| 51 | { |
| 52 | END |
| 53 | |
| 54 | # Add each command character in comment and the corresponding index |
| 55 | var tbl: list<string> = mapnew(nv_cmdtbl, (k, v) => |
| 56 | ' /* ' .. printf('%5d', v.cmdchar) .. ' */ ' .. |
| 57 | printf('%3d', v.idx) .. ',' |
| 58 | ) |
| 59 | output += tbl |
| 60 | |
| 61 | output += [ '};', '', |
| 62 | '// The highest index for which', |
| 63 | '// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]'] |
| 64 | output += ['static const int nv_max_linear = ' .. nv_max_linear .. ';'] |
| 65 | |
| 66 | writefile(output, "nv_cmdidxs.h") |
| 67 | enddef |
| 68 | |
| 69 | Create_nvcmdidxs_table() |
| 70 | quit |
| 71 | |
| 72 | # vim: shiftwidth=2 sts=2 expandtab |