robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e2bc84e315
master
1" Vim script for checking .po files. 2" 3" Goes through the xx.po file (more than once) 4" and verify various congruences 5" See the comments in the code 6 7" Last Update: 2025 Aug 06 8 9if 1 " Only execute this if the eval feature is available. 10 11" Using line continuation (set cpo to vim default value) 12let s: save_cpo = & cpo 13set cpo & vim 14 15" This only works when 'wrapscan' is not set. 16let s: save_wrapscan = & wrapscan 17set nowrapscan 18 19let filename = "check-" . expand ( "%:t:r" ) . ".log" 20exe 'redir! > ' . filename 21 22" Function to get a split line at the cursor. 23" Used for both msgid and msgstr lines. 24" Removes all text except % items and returns the result. 25func ! GetMline () 26let idline = substitute ( getline ( '.' ), '"\(.*\)"$' , '\1' , '' ) 27while line ( '.' ) < line ( '$' ) 28silent + 29let line = getline ( '.' ) 30if line [ 0 ] != '"' 31break 32endif 33let idline .= substitute ( line , '"\(.*\)"$' , '\1' , '' ) 34endwhile 35 36" remove '%', not used for formatting. 37let idline = substitute ( idline , "'%'" , '' , 'g' ) 38let idline = substitute ( idline , "%%" , '' , 'g' ) 39 40" remove '%' used for plural forms. 41let idline = substitute ( idline , '\\nPlural-Forms: .\+;\\n' , '' , '' ) 42 43" remove duplicate positional format arguments 44let idline2 = "" 45while idline2 != idline 46let idline2 = idline 47let idline = substitute ( idline , '%\([1-9][0-9]*\)\$\([-+ #''.*]*[0-9]*l\=[dsuxXpoc%]\)\(.*\)%\1$\([-+ #''.*]*\)\(l\=[dsuxXpoc%]\)' , '%\1$\2\3\4' , 'g' ) 48endwhile 49 50" remove everything but % items. 51return substitute ( idline , '[^%]*\(%([1-9][0-9]*\$)\=[-+ #''.0-9*]*l\=[dsuxXpoc%]\)\=' , '\1' , 'g' ) 52endfunc 53 54func ! CountNl ( first , last ) 55let nl = 0 56for lnum in range ( a: first , a: last ) 57let nl += count ( getline ( lnum ), "\n" ) 58endfor 59return nl 60endfunc 61 62" main 63 64" Start at the first "msgid" line. 65let wsv = winsaveview () 66silent 1 67silent keeppatterns /^msgid\> 68 69" When an error is detected this is set to the line number. 70" Note: this is used in the Makefile. 71let error = 0 72 73while 1 74" for each "msgid" 75 76" check msgid "Text;editor;" 77" translation must have two or more ";" (in case of more categories) 78let lnum = line ( '.' ) 79if getline ( lnum ) =~ 'msgid "Text;.*;"' 80if getline ( lnum + 1 ) !~ '^msgstr "\([^;]\+;\)\+"$' 81echomsg 'Mismatching ; in line ' . ( lnum + 1 ) 82echomsg 'Wrong semicolon count' 83if error == 0 84let error = lnum + 1 85endif 86endif 87endif 88 89" check for equal number of % in msgid and msgstr 90" it is skipping the no-c-format strings 91if getline ( line ( '.' ) - 1 ) !~ "no-c-format" 92" skip the "msgid_plural" lines 93let prevfromline = 'foobar' 94let plural = 0 95while 1 96if getline ( '.' ) =~ 'msgid_plural' 97let plural += 1 98endif 99let fromline = GetMline () 100if prevfromline != 'foobar' && prevfromline != fromline 101 \&& ( plural != 1 102 \|| count ( prevfromline , '%' ) + 1 != count ( fromline , '%' )) 103echomsg 'possibly mismatching % in line ' . ( line ( '.' ) - 1 ) 104echomsg 'msgid: ' . prevfromline 105echomsg 'msgid: ' . fromline 106if error == 0 107let error = line ( '.' ) 108endif 109endif 110if getline ( '.' ) !~ 'msgid_plural' 111break 112endif 113let prevfromline = fromline 114endwhile 115 116" checks that for each 'msgid' there is a 'msgstr' 117if getline ( '.' ) !~ '^msgstr' 118echomsg 'Missing "msgstr" in line ' . line ( '.' ) 119if error == 0 120let error = line ( '.' ) 121endif 122endif 123 124" check all the 'msgstr' lines have the same number of '%' 125" only the number of '%' is checked, 126" %d vs. %s or %d vs. %ld go undetected 127while getline ( '.' ) =~ '^msgstr' 128let toline = GetMline () 129if fromline != toline 130 \&& ( plural == 0 || count ( fromline , '%' ) != count ( toline , '%' ) + 1 ) 131echomsg 'possibly mismatching % in line ' . ( line ( '.' ) - 1 ) 132echomsg 'msgid: ' . fromline 133echomsg 'msgstr: ' . toline 134if error == 0 135let error = line ( '.' ) 136endif 137endif 138if line ( '.' ) == line ( '$' ) 139break 140endif 141endwhile 142endif 143 144" Find next msgid. Quit when there is no more. 145let lnum = line ( '.' ) 146silent ! keeppatterns /^msgid\> 147if line ( '.' ) == lnum 148break 149endif 150endwhile 151 152" Check that error code in msgid matches the one in msgstr. 153" 154" Examples of mismatches found with msgid "E123: ..." 155" - msgstr "E321: ..." error code mismatch 156" - msgstr "W123: ..." warning instead of error 157" - msgstr "E123 ..." missing colon 158" - msgstr "..." missing error code 159" 160silent 1 161if search ( 'msgid "\("\n"\)\?\([EW][0-9]\+:\).*\nmsgstr "\("\n"\)\?[^"]\@=\2\@!' ) > 0 162echomsg 'Mismatching error/warning code in line ' . line ( '.' ) 163if error == 0 164let error = line ( '.' ) 165endif 166endif 167 168" Check that the \n at the end of the msgid line is also present in the msgstr 169" line. Skip over the header. 170silent 1 171silent keeppatterns /^"MIME-Version: 172while 1 173let lnum = search ( '^msgid\>' ) 174if lnum <= 0 175break 176endif 177let strlnum = search ( '^msgstr\>' ) 178let end = search ( '^$' ) 179if end <= 0 180let end = line ( '$' ) + 1 181endif 182let origcount = CountNl ( lnum , strlnum - 1 ) 183let transcount = CountNl ( strlnum , end - 1 ) 184" Allow for a few more or less line breaks when there are 2 or more 185if origcount != transcount && ( origcount <= 2 || transcount <= 2 ) 186echomsg 'Mismatching "\n" in line ' . line ( '.' ) 187if error == 0 188let error = lnum 189endif 190endif 191endwhile 192 193" Check that the eventual continuation of 'msgstr' is well formed 194" final '""', '\n"', ' "' '/"' '."' '-"' are OK 195" Beware, it can give false positives if the message is split 196" in the middle of a word 197silent 1 198silent keeppatterns /^"MIME-Version: 199while 1 200let lnum = search ( '^msgid\>' ) 201if lnum <= 0 202break 203endif 204" "msgstr" goes from strlnum to end-1 205let strlnum = search ( '^msgstr\>' ) 206let end = search ( '^$' ) 207if end <= 0 208let end = line ( '$' ) + 1 209endif 210" only if there is a continuation line... 211if end > strlnum + 1 212let ilnum = strlnum 213while ilnum < end - 1 214let iltype = 0 215if getline ( ilnum ) =~ "^msgid_plural" 216let iltype = 2 217endif 218if getline ( ilnum ) =~ "^msgstr[" 219let iltype = 2 220endif 221if getline ( ilnum ) =~ "\"\"" 222let iltype = 1 223endif 224if getline ( ilnum ) =~ " \"$" 225let iltype = 1 226endif 227if getline ( ilnum ) =~ "-\"$" 228let iltype = 1 229endif 230if getline ( ilnum ) =~ "/\"$" 231let iltype = 1 232endif 233if getline ( ilnum ) =~ "\\.\"$" 234let iltype = 1 235endif 236if getline ( ilnum ) =~ "\\\\n\"$" 237let iltype = 1 238endif 239if iltype == 0 240echomsg 'Possibly incorrect final at line: ' . ilnum 241" TODO: make this an error 242" if error == 0 243" let error = ilnum 244" endif 245endif 246let ilnum += 1 247endwhile 248endif 249endwhile 250 251" Check that the file is well formed according to msgfmts understanding 252if executable ( "msgfmt" ) 253let filename = expand ( "%" ) 254" Newer msgfmt does not take OLD_PO_FILE_INPUT argument, must be in 255" environment. 256let $ OLD_PO_FILE_INPUT = 'yes' 257let a = system ( "msgfmt --statistics " . filename ) 258if v: shell_error != 0 259let error = matchstr ( a , filename . ':\zs\d\+\ze:' ) + 0 260for line in split ( a , '\n' ) |echomsg line |endfor 261endif 262endif 263 264" Check that the plural form is properly initialized 265silent 1 266let plural = search ( '^msgid_plural ' , 'n' ) 267if ( plural && search ( '^"Plural-Forms: ' , 'n' ) == 0 ) || ( plural && search ( '^msgstr\[0\] ".\+"' , 'n' ) != plural + 1 ) 268if search ( '^"Plural-Forms: ' , 'n' ) == 0 269echomsg "Missing Plural header" 270if error == 0 271let error = search ( '\(^"[A-Za-z-_]\+: .*\\n"\n\)\+\zs' , 'n' ) - 1 272endif 273elseif error == 0 274let error = plural 275endif 276elseif ! plural && search ( '^"Plural-Forms: ' , 'n' ) 277" We allow for a stray plural header, msginit adds one. 278endif 279 280" Check that 8bit encoding is used instead of 8-bit 281let cte = search ( '^"Content-Transfer-Encoding:\s\+8-bit' , 'n' ) 282let ctc = search ( '^"Content-Type:.*;\s\+\<charset=[iI][sS][oO]_' , 'n' ) 283let ctu = search ( '^"Content-Type:.*;\s\+\<charset=utf-8' , 'n' ) 284if cte 285echomsg "Warn: Content-Transfer-Encoding should be 8bit instead of 8-bit" 286" TODO: make this an error 287" if error == 0 288" let error = cte 289" endif 290elseif ctc 291echomsg "Warn: Content-Type charset should be 'ISO-...' instead of 'ISO_...'" 292" TODO: make this an error 293" if error == 0 294" let error = ct 295" endif 296elseif ctu 297echomsg "Warn: Content-Type charset should be 'UTF-8' instead of 'utf-8'" 298" TODO: make this an error 299" if error == 0 300" let error = ct 301" endif 302endif 303 304" Check that no lines are longer than 80 chars (except comments) 305let overlong = search ( '^[^#]\%>80v' , 'n' ) 306if overlong > 0 307echomsg "Warn: Lines should be wrapped at 80 columns" 308" TODO: make this an error 309" if error == 0 310" let error = overlong 311" endif 312endif 313 314" Check that there is no trailing whitespace 315let overlong = search ( '\s\+$' , 'n' ) 316if overlong > 0 317echomsg $ "Warn: Trailing whitespace at line: {overlong}" 318" TODO: make this an error 319" if error == 0 320" let error = overlong 321" endif 322endif 323 324if error == 0 325" If all was OK restore the view. 326call winrestview ( wsv ) 327echomsg "OK" 328else 329" Put the cursor on the line with the error. 330exe error 331endif 332 333redir END 334 335" restore original wrapscan 336let & wrapscan = s: save_wrapscan 337unlet s: save_wrapscan 338 339" restore original cpo 340let & cpo = s: save_cpo 341unlet s: save_cpo 342 343endif 344 345" vim:sts=2:sw=2:et