Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 1 | " Vim script for checking .po files. |
| 2 | " |
Bram Moolenaar | 595f51c | 2008-06-09 12:46:00 +0000 | [diff] [blame] | 3 | " Go through the file and verify that: |
| 4 | " - All %...s items in "msgid" are identical to the ones in "msgstr". |
| 5 | " - An error or warning code in "msgid" matches the one in "msgstr". |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6 | |
| 7 | if 1 " Only execute this if the eval feature is available. |
| 8 | |
| 9 | " Function to get a split line at the cursor. |
| 10 | " Used for both msgid and msgstr lines. |
| 11 | " Removes all text except % items and returns the result. |
| 12 | func! GetMline() |
| 13 | let idline = substitute(getline('.'), '"\(.*\)"$', '\1', '') |
| 14 | while line('.') < line('$') |
| 15 | + |
| 16 | let line = getline('.') |
| 17 | if line[0] != '"' |
| 18 | break |
| 19 | endif |
| 20 | let idline .= substitute(line, '"\(.*\)"$', '\1', '') |
| 21 | endwhile |
| 22 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 23 | " remove '%', not used for formatting. |
| 24 | let idline = substitute(idline, "'%'", '', 'g') |
| 25 | |
Bram Moolenaar | 1c6136a | 2009-09-11 11:00:05 +0000 | [diff] [blame] | 26 | " remove '%' used for plural forms. |
| 27 | let idline = substitute(idline, '\\nPlural-Forms: .\+;\\n', '', '') |
| 28 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 29 | " remove everything but % items. |
| 30 | return substitute(idline, '[^%]*\(%[-+ #''.0-9*]*l\=[dsuxXpoc%]\)\=', '\1', 'g') |
| 31 | endfunc |
| 32 | |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 33 | " This only works when 'wrapscan' is not set. |
Bram Moolenaar | a411e5d | 2010-08-04 15:47:08 +0200 | [diff] [blame] | 34 | let s:save_wrapscan = &wrapscan |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 35 | set nowrapscan |
Bram Moolenaar | a411e5d | 2010-08-04 15:47:08 +0200 | [diff] [blame] | 36 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 37 | " Start at the first "msgid" line. |
| 38 | 1 |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 39 | /^msgid\> |
| 40 | |
| 41 | " When an error is detected this is set to the line number. |
| 42 | " Note: this is used in the Makefile. |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 43 | let error = 0 |
| 44 | |
| 45 | while 1 |
| 46 | if getline(line('.') - 1) !~ "no-c-format" |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 47 | " go over the "msgid" and "msgid_plural" lines |
| 48 | let prevfromline = 'foobar' |
| 49 | while 1 |
| 50 | let fromline = GetMline() |
| 51 | if prevfromline != 'foobar' && prevfromline != fromline |
| 52 | echomsg 'Mismatching % in line ' . (line('.') - 1) |
| 53 | echomsg 'msgid: ' . prevfromline |
| 54 | echomsg 'msgid ' . fromline |
| 55 | if error == 0 |
| 56 | let error = line('.') |
| 57 | endif |
| 58 | endif |
| 59 | if getline('.') !~ 'msgid_plural' |
| 60 | break |
| 61 | endif |
| 62 | let prevfromline = fromline |
| 63 | endwhile |
| 64 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 65 | if getline('.') !~ '^msgstr' |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 66 | echomsg 'Missing "msgstr" in line ' . line('.') |
| 67 | if error == 0 |
| 68 | let error = line('.') |
| 69 | endif |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 70 | endif |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 71 | |
| 72 | " check all the 'msgstr' lines |
| 73 | while getline('.') =~ '^msgstr' |
| 74 | let toline = GetMline() |
| 75 | if fromline != toline |
| 76 | echomsg 'Mismatching % in line ' . (line('.') - 1) |
| 77 | echomsg 'msgid: ' . fromline |
| 78 | echomsg 'msgstr: ' . toline |
| 79 | if error == 0 |
| 80 | let error = line('.') |
| 81 | endif |
| 82 | endif |
| 83 | if line('.') == line('$') |
| 84 | break |
| 85 | endif |
| 86 | endwhile |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 87 | endif |
| 88 | |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 89 | " Find next msgid. Quit when there is no more. |
| 90 | let lnum = line('.') |
| 91 | silent! /^msgid\> |
| 92 | if line('.') == lnum |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 93 | break |
| 94 | endif |
| 95 | endwhile |
| 96 | |
Bram Moolenaar | 595f51c | 2008-06-09 12:46:00 +0000 | [diff] [blame] | 97 | " Check that error code in msgid matches the one in msgstr. |
| 98 | " |
| 99 | " Examples of mismatches found with msgid "E123: ..." |
| 100 | " - msgstr "E321: ..." error code mismatch |
| 101 | " - msgstr "W123: ..." warning instead of error |
| 102 | " - msgstr "E123 ..." missing colon |
| 103 | " - msgstr "..." missing error code |
| 104 | " |
| 105 | 1 |
| 106 | if search('msgid "\("\n"\)\?\([EW][0-9]\+:\).*\nmsgstr "\("\n"\)\?[^"]\@=\2\@!') > 0 |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 107 | echomsg 'Mismatching error/warning code in line ' . line('.') |
| 108 | if error == 0 |
| 109 | let error = line('.') |
| 110 | endif |
Bram Moolenaar | 595f51c | 2008-06-09 12:46:00 +0000 | [diff] [blame] | 111 | endif |
| 112 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 113 | if error == 0 |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 114 | echomsg "OK" |
| 115 | else |
| 116 | exe error |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 117 | endif |
| 118 | |
Bram Moolenaar | a411e5d | 2010-08-04 15:47:08 +0200 | [diff] [blame] | 119 | let &wrapscan = s:save_wrapscan |
| 120 | unlet s:save_wrapscan |
| 121 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 122 | endif |