blob: 256c5387d12b4210311102e7367e69b376621e96 [file] [log] [blame]
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00001" Vim script for checking .po files.
2"
Bram Moolenaar595f51c2008-06-09 12:46:00 +00003" 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 Moolenaar1d94f9b2005-08-04 21:29:45 +00006
7if 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.
12func! 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 Moolenaara5792f52005-11-23 21:25:05 +000023 " remove '%', not used for formatting.
24 let idline = substitute(idline, "'%'", '', 'g')
25
Bram Moolenaar1c6136a2009-09-11 11:00:05 +000026 " remove '%' used for plural forms.
27 let idline = substitute(idline, '\\nPlural-Forms: .\+;\\n', '', '')
28
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +000029 " remove everything but % items.
30 return substitute(idline, '[^%]*\(%[-+ #''.0-9*]*l\=[dsuxXpoc%]\)\=', '\1', 'g')
31endfunc
32
Bram Moolenaarec420592016-08-26 15:51:53 +020033" This only works when 'wrapscan' is not set.
Bram Moolenaara411e5d2010-08-04 15:47:08 +020034let s:save_wrapscan = &wrapscan
Bram Moolenaarec420592016-08-26 15:51:53 +020035set nowrapscan
Bram Moolenaara411e5d2010-08-04 15:47:08 +020036
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +000037" Start at the first "msgid" line.
381
Bram Moolenaarec420592016-08-26 15:51:53 +020039/^msgid\>
40
41" When an error is detected this is set to the line number.
42" Note: this is used in the Makefile.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +000043let error = 0
44
45while 1
46 if getline(line('.') - 1) !~ "no-c-format"
Bram Moolenaarec420592016-08-26 15:51:53 +020047 " 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 Moolenaar1d94f9b2005-08-04 21:29:45 +000065 if getline('.') !~ '^msgstr'
Bram Moolenaarec420592016-08-26 15:51:53 +020066 echomsg 'Missing "msgstr" in line ' . line('.')
67 if error == 0
68 let error = line('.')
69 endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +000070 endif
Bram Moolenaarec420592016-08-26 15:51:53 +020071
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 Moolenaar1d94f9b2005-08-04 21:29:45 +000087 endif
88
Bram Moolenaarec420592016-08-26 15:51:53 +020089 " Find next msgid. Quit when there is no more.
90 let lnum = line('.')
91 silent! /^msgid\>
92 if line('.') == lnum
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +000093 break
94 endif
95endwhile
96
Bram Moolenaar595f51c2008-06-09 12:46:00 +000097" 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"
1051
106if search('msgid "\("\n"\)\?\([EW][0-9]\+:\).*\nmsgstr "\("\n"\)\?[^"]\@=\2\@!') > 0
Bram Moolenaarec420592016-08-26 15:51:53 +0200107 echomsg 'Mismatching error/warning code in line ' . line('.')
108 if error == 0
109 let error = line('.')
110 endif
Bram Moolenaar595f51c2008-06-09 12:46:00 +0000111endif
112
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +0000113if error == 0
Bram Moolenaarec420592016-08-26 15:51:53 +0200114 echomsg "OK"
115else
116 exe error
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +0000117endif
118
Bram Moolenaara411e5d2010-08-04 15:47:08 +0200119let &wrapscan = s:save_wrapscan
120unlet s:save_wrapscan
121
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +0000122endif