@@ -134,7 +134,7 @@ def ActionList()
134134 endif
135135 sort (terms)
136136
137- var items = [' protocol' , ' version' , ' status' ]
137+ var items = [' protocol' , ' version' , ' status' , ' resource ' ]
138138 + key_entries- >copy ()- >map ((_, v ) = > v [1 ])
139139
140140 # For each terminal compute the needed width, add two.
@@ -198,8 +198,9 @@ def DoTerm(name: string)
198198 if proto == 1
199199 &t_TI = " "
200200 elseif proto == 2
201- # Enable modifyOtherKeys level 2 - no status is reported
202- &t_TI = " \<Esc> [>4;2m"
201+ # Enable modifyOtherKeys level 2
202+ # Request the resource value: DCS + Q modifyOtherKeys ST
203+ &t_TI = " \<Esc> [>4;2m" .. " \<Esc> P+Q6d6f646966794f746865724b657973\<Esc> \\ "
203204 proto_name = ' mok2'
204205 elseif proto == 3
205206 # Enable Kitty keyboard protocol and request the status
@@ -217,9 +218,14 @@ def DoTerm(name: string)
217218 # Pattern that matches the line with the version response.
218219 const version_pattern = " \<Esc> \\ [>\\ d\\ +;\\ d\\ +;\\ d*c"
219220
221+ # Pattern that matches the resource value response:
222+ # DCS 1 + R Pt ST valid
223+ # DCS 0 + R Pt ST invalid
224+ const resource_pattern = " \<Esc> P[01]+R.*\<Esc> \\\\ "
225+
220226 # Pattern that matches the line with the status. Currently what terminals
221227 # return for the Kitty keyboard protocol.
222- const status_pattern = " \<Esc> \\ [?\\ d\\ +u"
228+ const kitty_status_pattern = " \<Esc> \\ [?\\ d\\ +u"
223229
224230 ch_logfile (' keylog' , ' w' )
225231
@@ -244,6 +250,7 @@ def DoTerm(name: string)
244250 endfor
245251 endif
246252 if reltime (startTime)- >reltimefloat () > 3
253+ # break out after three seconds
247254 break
248255 endif
249256 endwhile
@@ -257,18 +264,39 @@ def DoTerm(name: string)
257264 keycodes[name][' protocol' ] = proto_name
258265 keycodes[name][' version' ] = ' '
259266 keycodes[name][' status' ] = ' '
267+ keycodes[name][' resource' ] = ' '
260268
261269 # Check the log file for a status and the version response
262270 ch_logfile (' ' , ' ' )
263271 var log = readfile (' keylog' )
264272 delete (' keylog' )
273+
265274 for line in log
266275 if line = ~ ' raw key input'
267276 var code = substitute (line , ' .*raw key input: "\([^"]*\).*' , ' \1' , ' ' )
277+
278+ # Check for resource value response
279+ if code = ~ resource_pattern
280+ var resource = substitute (code, ' .*\(' .. resource_pattern .. ' \).*' , ' \1' , ' ' )
281+ # use the value as the resource, " =30" means zero
282+ resource = substitute (resource, ' .*\(=\p\+\).*' , ' \1' , ' ' )
283+
284+ if keycodes[name][' resource' ] != ' '
285+ echomsg ' Another resource found after ' .. keycodes[name][' resource' ]
286+ endif
287+ keycodes[name][' resource' ] = resource
288+ endif
289+
268290 # Check for kitty keyboard protocol status
269- if code = ~ status_pattern
270- var status = substitute (code, ' .*\(' .. status_pattern .. ' \).*' , ' \1' , ' ' )
271- keycodes[name][' status' ] = Literal2hex (status)
291+ if code = ~ kitty_status_pattern
292+ var status = substitute (code, ' .*\(' .. kitty_status_pattern .. ' \).*' , ' \1' , ' ' )
293+ # use the response itself as the status
294+ status = Literal2hex (status)
295+
296+ if keycodes[name][' status' ] != ' '
297+ echomsg ' Another status found after ' .. keycodes[name][' status' ]
298+ endif
299+ keycodes[name][' status' ] = status
272300 endif
273301
274302 if code = ~ version_pattern
@@ -282,13 +310,23 @@ def DoTerm(name: string)
282310 echo " For Alt to work you may need to press the Windows/Super key as well"
283311 echo " When a key press doesn't get to Vim (e.g. when using Alt) press x"
284312
313+ # The log of ignored typeahead is left around for debugging, start with an
314+ # empty file here.
315+ delete (' keylog-ignore' )
316+
285317 for entry in key_entries
286318 # Consume any typeahead. Wait a bit for any responses to arrive.
287- sleep 100 m
288- while getchar (1 )
289- getchar ()
319+ ch_logfile (' keylog-ignore' , ' a' )
320+ while 1
290321 sleep 100 m
322+ if ! getchar (1 )
323+ break
324+ endif
325+ while getchar (1 )
326+ getchar ()
327+ endwhile
291328 endwhile
329+ ch_logfile (' ' , ' ' )
292330
293331 ch_logfile (' keylog' , ' w' )
294332 echo $ ' Press the {entry[0]} key (q to quit):'
@@ -297,13 +335,9 @@ def DoTerm(name: string)
297335 if r == ' q'
298336 break
299337 endif
338+
300339 log = readfile (' keylog' )
301- if entry[1 ] == ' Tab'
302- # keep a copy
303- rename (' keylog' , ' keylog-tab' )
304- else
305- delete (' keylog' )
306- endif
340+ delete (' keylog' )
307341 if len (log ) < 2
308342 echoerr ' failed to read result'
309343 return
@@ -321,7 +355,7 @@ rename('keylog', 'keylog-tab')
321355 code = substitute (code, cappat, ' ' , ' g' )
322356
323357 # Remove any kitty status reply
324- code = substitute (code, status_pattern , ' ' , ' g' )
358+ code = substitute (code, kitty_status_pattern , ' ' , ' g' )
325359 if code == ' '
326360 continue
327361 endif
0 commit comments