|
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() |
| 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. |
| 11 | + |
| 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}}) |
| 19 | + |
| 20 | +" sort the table by the command character |
| 21 | +call sort(nv_cmdtbl, {a, b -> a.cmdchar - b.cmdchar}) |
| 22 | + |
| 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 |
| 32 | + |
| 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 | + */ |
| 41 | + |
| 42 | + // nv_cmd_idx[<normal mode command character>] => nv_cmds[] index |
| 43 | + static const unsigned short nv_cmd_idx[] = |
| 44 | + { |
| 45 | +END |
| 46 | + |
| 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)}) |
| 50 | + |
| 51 | +let output += ['};', '', |
| 52 | + \ '// The highest index for which', |
| 53 | + \ '// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]'] |
| 54 | + |
| 55 | +let output += ['static const int nv_max_linear = ' .. nv_max_linear .. ';'] |
| 56 | + |
| 57 | +call writefile(output, "nv_cmdidxs.h") |
70 | 58 | quit |
71 | 59 |
|
72 | | -# vim: shiftwidth=2 sts=2 expandtab |
| 60 | +" vim: shiftwidth=2 sts=2 expandtab |
0 commit comments