summaryrefslogtreecommitdiffstatshomepage
path: root/src/gen/gen_terminfo.lua
blob: 4431389a718a0ce334d445bbbec58bfdd40f6f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
-- USAGE:
--
--    # Optional: Delete cache to get latest terminfo from internet.
--    rm -rf /tmp/nvim_terminfo/
--
--    # Optional: Ensure the latest ncurses+tic is in your PATH.
--    export PATH="/opt/homebrew/Cellar/ncurses/6.5/bin/":"$PATH"
--
--    nvim -ll src/gen/gen_terminfo.lua
--
-- This script does:
--
--   1. Download Dickey's terminfo.src
--   2. Compile temporary terminfo database from terminfo.src
--   3. Use database to generate src/nvim/tui/terminfo_defs.h

local url = 'https://invisible-island.net/datafiles/current/terminfo.src.gz'
local target_gen = 'src/nvim/tui/terminfo_builtin.h'
local target_enum = 'src/nvim/tui/terminfo_enum_defs.h'

local entries = {
  { 'ansi', 'ansi_terminfo' },
  { 'ghostty', 'ghostty_terminfo' }, -- Note: ncurses defs do not exactly match what ghostty ships.
  { 'interix', 'interix_8colour_terminfo' },
  { 'iterm2', 'iterm_256colour_terminfo' },
  { 'linux', 'linux_16colour_terminfo' },
  { 'putty-256color', 'putty_256colour_terminfo' },
  { 'rxvt-256color', 'rxvt_256colour_terminfo' },
  { 'screen-256color', 'screen_256colour_terminfo' },
  { 'st-256color', 'st_256colour_terminfo' },
  { 'tmux-256color', 'tmux_256colour_terminfo' },
  { 'vte-256color', 'vte_256colour_terminfo' },
  { 'xterm-256color', 'xterm_256colour_terminfo' },
  { 'cygwin', 'cygwin_terminfo' },
  { 'win32con', 'win32con_terminfo' },
  { 'conemu', 'conemu_terminfo' },
  { 'vtpcon', 'vtpcon_terminfo' },
}

local wanted_numbers = { 'max_colors', 'lines', 'columns' }
local wanted_strings = {
  'carriage_return',
  'change_scroll_region',
  'clear_screen',
  'clr_eol',
  'clr_eos',
  'cursor_address',
  'cursor_down',
  'cursor_invisible',
  'cursor_left',
  'cursor_home',
  'cursor_normal',
  'cursor_up',
  'cursor_right',
  'delete_line',
  'enter_blink_mode',
  'enter_bold_mode',
  'enter_ca_mode',
  'enter_dim_mode',
  'enter_italics_mode',
  'enter_reverse_mode',
  'enter_secure_mode',
  'enter_standout_mode',
  'enter_underline_mode',
  'erase_chars',
  'exit_attribute_mode',
  'exit_ca_mode',
  'from_status_line',
  'insert_line',
  'keypad_local',
  'keypad_xmit',
  'parm_delete_line',
  'parm_down_cursor',
  'parm_insert_line',
  'parm_left_cursor',
  'parm_right_cursor',
  'parm_up_cursor',
  'set_a_background',
  'set_a_foreground',
  'set_attributes',
  'set_lr_margin',
  'to_status_line',
}

local wanted_strings_ext = {
  -- the following are our custom name for extensions, see "extmap"
  { 'reset_cursor_style', 'Se' },
  { 'set_cursor_style', 'Ss' },
  -- terminfo describes strikethrough modes as rmxx/smxx with respect
  -- to the ECMA-48 strikeout/crossed-out attributes.
  { 'enter_strikethrough_mode', 'smxx' },
  { 'set_rgb_foreground', 'setrgbf' },
  { 'set_rgb_background', 'setrgbb' },
  { 'set_cursor_color', 'Cs' },
  { 'reset_cursor_color', 'Cr' },
  { 'set_underline_style', 'Smulx' },
}

-- Note: these are only consumed by driver-ti via it's table of "funcs" keys.
-- Second value is whether there is a "shift" variant in terminfo.
local wanted_termkeys = {
  { 'backspace', false },
  { 'beg', true }, -- sometimes known as: "begin"
  { 'btab', false },
  { 'clear', false },
  { 'dc', true },
  { 'end', true },
  { 'find', true },
  { 'home', true },
  { 'ic', true },
  { 'npage', false },
  { 'ppage', false },
  { 'select', false },
  { 'suspend', true },
  { 'undo', true },
  { 'left', true },
  { 'right', true },
}

local db = '/tmp/nvim_terminfo'
if vim.uv.fs_stat(db) == nil then
  local function sys(cmd)
    print(cmd)
    os.execute(cmd)
  end
  sys('curl -O ' .. url)
  sys('gunzip -f terminfo.src.gz')
  sys(('cat terminfo.src scripts/windows.ti | tic -x -o "%s" -'):format(db))
  sys('rm -f terminfo.src')
else
  print('using cached terminfo in ' .. db)
end

local function enumify(str)
  return 'kTerm_' .. str
end
local function quote(str)
  if str == nil then
    return 'NULL'
  end
  -- remungle the strings to look like C strings
  str = string.gsub(str, '\\E', '\\033')
  str = string.gsub(str, '%^G', '\\a')
  str = string.gsub(str, '%^H', '\\b')
  str = string.gsub(str, '%^O', '\\017') -- o dod
  -- str = string.gsub(str, "\\", "\\\\")
  str = string.gsub(str, '"', '\\"')
  return '"' .. str .. '"'
end

--- @type fun(...: any)
local dbg = function() end
-- dbg = print

local f_enum = assert(io.open(target_enum, 'wb'))
f_enum:write('// generated by src/gen/gen_terminfo.lua\n\n')
f_enum:write('#pragma once\n\n')
f_enum:write('typedef enum {\n')
for _, name in ipairs(wanted_strings) do
  f_enum:write('  ' .. enumify(name) .. ',\n')
end
f_enum:write('#define kTermExtOffset ' .. enumify(wanted_strings_ext[1][1]) .. '\n')
for _, item in ipairs(wanted_strings_ext) do
  f_enum:write('  ' .. enumify(item[1]) .. ',\n')
end
f_enum:write('  kTermCount,  // sentinel\n')
f_enum:write('} TerminfoDef;\n\n')

f_enum:write([[
// TODO(bfredl): physical F-keys beyond F12 are uncommon. But terminfo
// likes to represent chords with shift and/or ctrl and F keys as high
// F-key numbers. The same chords can also be recognized by driver-csi.c
// but will then be encoded as chords. We might actually prefer that but it is
// potentially breaking change.
]])
local func_key_max = 63
f_enum:write('#define kTerminfoFuncKeyMax ' .. func_key_max .. '\n')
f_enum:write('typedef enum {\n')
for _, item in ipairs(wanted_termkeys) do
  f_enum:write('  kTermKey_' .. item[1] .. ',\n')
end
f_enum:write('  kTermKeyCount,\n')
f_enum:write('} TerminfoKey;\n')
f_enum:close()

local f_defs = assert(io.open(target_gen, 'wb'))

f_defs:write('// uncrustify:off\n\n')

local version = io.popen('infocmp -V'):read '*a'
f_defs:write('// Generated by src/gen/gen_terminfo.lua and ' .. version .. '\n')

f_defs:write('#pragma once\n\n')
f_defs:write('#include "nvim/tui/terminfo_defs.h"\n')

for _, entry in ipairs(entries) do
  local term, target = unpack(entry)
  local fil = io.popen('infocmp -L -x -1 -A ' .. db .. ' ' .. term):read '*a'
  local lines = vim.split(fil, '\n')
  local prepat = '^%s*([%w_]+)'
  local boolpat = prepat .. ','
  local numpat = prepat .. '#([^,]+),'
  local strpat = prepat .. '=([^,]+),'
  local bools = {} --- @type table<string, true>
  local nums = {} --- @type table<string, string>
  local strs = {} --- @type table<string, string>
  for i, line in ipairs(lines) do
    local boolmatch = string.match(line, boolpat)
    local nummatch, numval = string.match(line, numpat)
    local strmatch, strval = string.match(line, strpat)
    if boolmatch then
      dbg('boolean: ' .. boolmatch)
      bools[boolmatch] = true
    elseif nummatch then
      dbg('number: ' .. nummatch .. ' is ' .. numval)
      nums[nummatch] = numval
    elseif strmatch then
      dbg('string: ' .. strmatch .. ' is ' .. strval)
      strs[strmatch] = strval
    else
      dbg('UNKNOWN:', i, line)
    end
  end

  f_defs:write('\nstatic const TerminfoEntry ' .. target .. ' = {\n')
  f_defs:write('  .bce = ' .. tostring(bools.back_color_erase or false) .. ',\n')
  local has_Tc_or_RGB = (bools.Tc or bools.RGB) or false
  f_defs:write('  .has_Tc_or_RGB = ' .. tostring(has_Tc_or_RGB or false) .. ',\n')
  f_defs:write('  .Su = ' .. tostring(bools.Su or false) .. ',\n')

  for _, name in ipairs(wanted_numbers) do
    f_defs:write('  .' .. name .. ' = ' .. (nums[name] or '-1') .. ',\n')
  end
  f_defs:write('  .defs = {\n')
  for _, name in ipairs(wanted_strings) do
    f_defs:write('    [' .. enumify(name) .. '] = ' .. quote(strs[name]) .. ',\n')
  end
  for _, item in ipairs(wanted_strings_ext) do
    f_defs:write('    [' .. enumify(item[1]) .. '] = ' .. quote(strs[item[2]]) .. ',\n')
  end
  f_defs:write('  },\n')
  f_defs:write('  .keys = {\n')
  for _, item in ipairs(wanted_termkeys) do
    local name = item[1]
    f_defs:write(
      '    [kTermKey_'
        .. name
        .. '] = {'
        .. quote(strs['key_' .. name])
        .. ', '
        .. quote(strs['key_s' .. name])
        .. '},\n'
    )
  end
  f_defs:write('  },\n')
  f_defs:write('  .f_keys = {\n')
  if strs['key_f1'] == nil then
    f_defs:write('    NULL,\n') -- compiler get sad if list is empty
  else
    f_defs:write('    // note: offset by one, f_keys[0] is F1 and so on\n')
  end
  for i = 1, func_key_max do
    if strs['key_f' .. i] ~= nil then
      f_defs:write('    [' .. i - 1 .. '] = ' .. quote(strs['key_f' .. i]) .. ',\n')
    end
  end
  f_defs:write('  },\n')

  f_defs:write('};\n')
end

f_defs:write('\n#define XLIST_TERMINFO_BUILTIN \\\n')
for _, name in ipairs(wanted_strings) do
  f_defs:write('  X(' .. name .. ') \\\n')
end
f_defs:write('// end of list\n\n')
f_defs:write('#define XLIST_TERMINFO_EXT \\\n')
for _, item in ipairs(wanted_strings_ext) do
  f_defs:write('  X(' .. item[1] .. ', ' .. item[2] .. ') \\\n')
end
f_defs:write('// end of list\n\n')
f_defs:write('#define XYLIST_TERMINFO_KEYS \\\n')
for _, item in ipairs(wanted_termkeys) do
  f_defs:write('  ' .. (item[2] and 'Y' or 'X') .. '(' .. item[1] .. ') \\\n')
end
f_defs:write('// end of list\n\n')
f_defs:write('#define XLIST_TERMINFO_FKEYS \\\n')
for i = 1, func_key_max do
  f_defs:write('  X(f' .. i .. ') \\\n')
end
f_defs:write('// end of list\n')
f_defs:close()