Aliaksei Budavei | 6bff6a2 | 2024-08-19 21:33:26 +0200 | [diff] [blame^] | 1 | vim9script |
| 2 | |
| 3 | # (Script-local.) |
| 4 | # |
| 5 | # Render a loaded screendump file or the difference of a loaded screendump |
| 6 | # file and its namesake file from the "dumps" directory. |
| 7 | def Render() |
| 8 | const failed_fname: string = bufname() |
| 9 | try |
| 10 | setlocal suffixesadd=.dump |
| 11 | const dumps_fname: string = findfile( |
| 12 | fnamemodify(failed_fname, ':p:t'), |
| 13 | fnamemodify(failed_fname, ':p:h:h') .. '/dumps') |
| 14 | if filereadable(dumps_fname) |
| 15 | term_dumpdiff(failed_fname, dumps_fname) |
| 16 | else |
| 17 | term_dumpload(failed_fname) |
| 18 | endif |
| 19 | finally |
| 20 | exec 'bwipeout ' .. failed_fname |
| 21 | endtry |
| 22 | enddef |
| 23 | |
| 24 | # Search for the "failed" directory in the passed _subtreedirname_ directories |
| 25 | # (usually "\<src\>" or "\<syntax\>") and, if found, select its passed _count_ |
| 26 | # occurence, add all its "*.dump" files to the argument list and list them; |
| 27 | # also define a BufRead autocommand that would invoke "Render()" for every |
| 28 | # "*.dump" file. |
| 29 | def g:Init(subtreedirname: string, count: number) |
| 30 | # Support sourcing this script from any directory in the direct path that |
| 31 | # leads to the project's root directory. |
| 32 | const failed_path: string = finddir('failed', getcwd() .. '/**', -1) |
| 33 | ->filter(((cwdpath: string, parentdirname: string) => |
| 34 | (_: number, dirpath: string) => |
| 35 | cwdpath =~ parentdirname || dirpath =~ parentdirname)( |
| 36 | getcwd(), |
| 37 | subtreedirname)) |
| 38 | ->get(count, '') .. '/' |
| 39 | var error: string = null_string |
| 40 | |
| 41 | if failed_path == '/' |
| 42 | error = 'No such directory: "failed"' |
| 43 | else |
| 44 | const failed_fnames: string = failed_path .. readdir(failed_path, |
| 45 | (fname: string) => fname =~ '^.\+\.dump$') |
| 46 | ->join(' ' .. failed_path) |
| 47 | |
| 48 | if failed_fnames =~ 'failed/$' |
| 49 | error = 'No such file: "*.dump"' |
| 50 | else |
| 51 | exec ':0argedit ' .. failed_fnames |
| 52 | buffers |
| 53 | endif |
| 54 | endif |
| 55 | |
| 56 | autocmd_add([{ |
| 57 | replace: true, |
| 58 | group: 'viewdumps', |
| 59 | event: 'BufRead', |
| 60 | pattern: '*.dump', |
| 61 | cmd: 'Render()', |
| 62 | }]) |
| 63 | |
| 64 | # Unconditionally help, in case a list of filenames is passed to the |
| 65 | # command, the first terminal window with its BufRead event. |
| 66 | silent doautocmd viewdumps BufRead |
| 67 | |
| 68 | if error != null_string |
| 69 | # Instead of sleeping, fill half a window with blanks and prompt |
| 70 | # hit-enter. |
| 71 | echom error .. repeat("\x20", |
| 72 | (winwidth(0) * (winheight(0) / 2) - strlen(error))) |
| 73 | endif |
| 74 | enddef |
| 75 | |
| 76 | # vim:fdm=syntax:sw=2:ts=8:noet:nolist:nosta: |