" Vim ftplugin for PO file (GNU gettext) editing. " Maintainer: Aleksandar Jelenak " Last Change: Tue, 12 Apr 2005 13:49:55 -0400 " " *** Latest version: http://www.vim.org/scripts/script.php?script_id=695 *** " " DESCRIPTION " This file is a Vim ftplugin for editing PO files (GNU gettext -- the GNU " i18n and l10n system). It automates over a dozen frequent tasks that " occur while editing files of this type. " " Key mappings " Action (Insert mode) GUI Vim Vim " =============================================================== " Move to an untransl. string forward \m " Move to an untransl. string backward \p " Copy the msgid string to msgstr \c " Delete the msgstr string \d " Move to the next fuzzy translation \f " Move to the previous fuzzy translation \b " Label the translation fuzzy \z " Remove the fuzzy label \r " Show msgfmt statistics for the file(*) \s " Browse through msgfmt errors for the file(*) \e " Put the translator info in the header \t \t " Put the lang. team info in the header \l \l " --------------------------------------------------------------- " (*) Only available on UNIX computers. " " " Key mappings " Action (Normal mode) GUI Vim Vim " =============================================================== " Move to an untransl. string forward \m " Move to an untransl. string backward \p " Move to the next fuzzy translation \f " Move to the previous fuzzy translation \b " Label the translation fuzzy \z " Remove the fuzzy label \r " Split-open the file under cursor gf gf " Show msgfmt statistics for the file(*) \s " Browse through msgfmt errors for the file(*) \e " Put the translator info in the header \t \t " Put the lang. team info in the header \l \l " --------------------------------------------------------------- " (*) Only available on UNIX computers. " " Remarks: " - "S" in the above key mappings stands for the key and "\" in " fact means "" (:help ), which is "\" by " Vim's default. " - Information about the translator and language team is supplied by two " global variables: 'g:po_translator' and 'g:po_lang_team'. They should " be defined in the ".vimrc" (UNIX) or "_vimrc" (Windows) file. If they " are not defined, the default values (descriptive strings) are put " instead. " - Vim's "gf" Normal mode command is remapped (local to the PO buffer, of " course). It will only function on lines starting with "#: ". Search " for the file is performed in the directories specified by the 'path' " option. The user can supply its own addition to this option via the " 'g:po_path' global variable. Its default value for PO files can be " found by typing ":set path?" from within a PO buffer. For the correct " format please see ":help 'path'". Warning messages are printed if no " or more than one file is found. " - Vim's Quickfix mode (see ":help quickfix") is used for browsing " through msgfmt-reported errors for the file. No MO file is created " when running the msgfmt program since its output is directed to " "/dev/null". The user can supply command-line arguments to the msgfmt " program via the global variable 'g:po_msgfmt_args'. All arguments are " allowed except the "-o" for output file. The default value is " "-vv -c". " " But there's even more! " " Every time the PO file is saved, a PO-formatted time stamp is " automatically added to the file header. " " INSTALLATION " Put this file in a Vim ftplugin directory. On UNIX computers it is " usually either "~/.vim/ftplugin" or "~/.vim/after/ftplugin". On Windows " computers, the defaults are "$VIM\vimfiles\ftplugin" or " "$VIM\vimfiles\after\ftplugin". For more information consult the Vim " help, ":help 'ftplugin'" and ":help 'runtimepath'". " " REMOVAL " Just delete the bloody file! " Only do this when not done yet for this buffer. if exists("b:did_po_mode_ftplugin") finish endif let b:did_po_mode_ftplugin = 1 setlocal comments= setlocal errorformat=%f:%l:\ %m setlocal makeprg=msgfmt let b:po_path = '.,..,../src,../src/*' if exists("g:po_path") let b:po_path = b:po_path . ',' . g:po_path endif exe "setlocal path=" . b:po_path unlet b:po_path " Check if GUI Vim is running. if has("gui_running") let gui = 1 else let gui = 0 endif " Move to the first untranslated msgstr string forward. if !hasmapto('NextTransFwd') if gui imap NextTransFwd nmap NextTransFwd else imap m NextTransFwd nmap m NextTransFwd endif endif inoremap NextTransFwd /^msgstr\s*""\(\n\n\\|\%$\):let @/="":call histdel("/", -1)z.f"a nnoremap NextTransFwd /^msgstr\s*""\(\n\n\\|\%$\):let @/="":call histdel("/", -1)z. " Move to the first untranslated msgstr string backward. if !hasmapto('NextTransBwd') if gui imap NextTransBwd nmap NextTransBwd else imap p NextTransBwd nmap p NextTransBwd endif endif inoremap NextTransBwd {?^msgstr\s*""\(\n\n\\|\%$\):let @/="":call histdel("/", -1)z.f"a nnoremap NextTransBwd {?^msgstr\s*""\(\n\n\\|\%$\):let @/="":call histdel("/", -1)z. " Copy original msgid string into msgstr string. if !hasmapto('CopyMsgid') if gui imap CopyMsgid else imap c CopyMsgid endif endif inoremap CopyMsgid }?^msgid:let @/="":call histdel("/", -1)f"y/^msgstr/^msgstr:let @/="":call histdel("/", -1)f""_d$pa " Erase the translation string. if !hasmapto('DeleteTrans') if gui imap DeleteTrans else imap d DeleteTrans endif endif inoremap DeleteTrans }?^msgstr:let @/="":call histdel("/", -1)f"lc}"i " Move to the first fuzzy translation forward. if !hasmapto('NextFuzzy') if gui imap NextFuzzy nmap NextFuzzy else imap f NextFuzzy nmap f NextFuzzy endif endif inoremap NextFuzzy /^#,\(.*,\)\=\s*fuzzy:let @/="":call histdel("/", -1)/^msgstr:let @/="":call histdel("/", -1)z.$i nnoremap NextFuzzy /^#,\(.*,\)\=\s*fuzzy:let @/="":call histdel("/", -1)/^msgstr:let @/="":call histdel("/", -1)z.$ " Move to the first fuzzy descriptor backward. if !hasmapto('PreviousFuzzy') if gui imap PreviousFuzzy nmap PreviousFuzzy else imap b PreviousFuzzy nmap b PreviousFuzzy endif endif inoremap PreviousFuzzy {?^#,\(.*,\)\=\s*fuzzy:let @/="":call histdel("/", -1)/^msgstr:let @/="":call histdel("/", -1)z.$i nnoremap PreviousFuzzy {?^#,\(.*,\)\=\s*fuzzy:let @/="":call histdel("/", -1)/^msgstr:let @/="":call histdel("/", -1)z.$ " Insert fuzzy description for the translation. if !hasmapto('InsertFuzzy') if gui imap InsertFuzzy nmap InsertFuzzy else imap z InsertFuzzy nmap z InsertFuzzy endif endif inoremap InsertFuzzy {vap:call InsertFuzzy()gv}i nnoremap InsertFuzzy {vap:call InsertFuzzy()gv} fu! InsertFuzzy() range let n = a:firstline while n <= a:lastline let line = getline(n) if line =~ '^#,.*fuzzy' return elseif line =~ '^#,' call setline(n, substitute(line, '#,','#, fuzzy,', "")) return elseif line =~ '^msgid' call append(n-1, '#, fuzzy') return endif let n = n + 1 endwhile endf " Remove fuzzy description from the translation. if !hasmapto('RemoveFuzzy') if gui imap RemoveFuzzy nmap RemoveFuzzy else imap r RemoveFuzzy nmap r RemoveFuzzy endif endif inoremap RemoveFuzzy {vap:call RemoveFuzzy()i nnoremap RemoveFuzzy {vap:call RemoveFuzzy() fu! RemoveFuzzy() let line = getline(".") if line =~ '^#,\s*fuzzy$' exe "normal! dd" elseif line =~ '^#,\(.*,\)\=\s*fuzzy' exe 's/,\s*fuzzy//' endif endf " Show PO translation statistics. (Only available on UNIX computers for now.) if has("unix") if !hasmapto('MsgfmtStats') if gui imap MsgfmtStats nmap MsgfmtStats else imap s MsgfmtStats nmap s MsgfmtStats endif endif inoremap MsgfmtStats :call Msgfmt('stats') nnoremap MsgfmtStats :call Msgfmt('stats') if !hasmapto('MsgfmtTest') if gui imap MsgfmtTest nmap MsgfmtTest else imap e MsgfmtTest nmap e MsgfmtTest endif endif inoremap MsgfmtTest :call Msgfmt('test') nnoremap MsgfmtTest :call Msgfmt('test') fu! Msgfmt(action) " Check if the file needs to be saved first. exe "if &modified | w | endif" if a:action == 'stats' exe "!msgfmt --statistics -o /dev/null %" elseif a:action == 'test' if exists("g:po_msgfmt_args") let args = g:po_msgfmt_args else let args = '-vv -c' endif exe "make! " . args . " -o /dev/null %" copen endif endf endif " Add translator info in the file header. if !hasmapto('TranslatorInfo') if gui imap t TranslatorInfo nmap t TranslatorInfo else imap t TranslatorInfo nmap t TranslatorInfo endif endif inoremap TranslatorInfo :call AddHeaderInfo('person')i nnoremap TranslatorInfo :call AddHeaderInfo('person') " Add language team info in the file header. if !hasmapto('LangTeamInfo') if gui imap l LangTeamInfo nmap l LangTeamInfo else imap l LangTeamInfo nmap l LangTeamInfo endif endif inoremap LangTeamInfo :call AddHeaderInfo('team')i nnoremap LangTeamInfo :call AddHeaderInfo('team') fu! AddHeaderInfo(action) if a:action == 'person' let search_for = 'Last-Translator' if exists("g:po_translator") let add = g:po_translator else let add = 'YOUR NAME ' endif elseif a:action == 'team' let search_for = 'Language-Team' if exists("g:po_lang_team") let add = g:po_lang_team else let add = 'LANGUAGE TEAM ' endif else " Undefined action -- just do nothing. return endif let search_for = '"' . search_for . ':' let add = add . '\\n"' normal! 1G if search('^' . search_for) silent! exe 's/^\(' . search_for . '\).*$/\1 ' . add endif call histdel("/", -1) endf " Write automagically PO-formatted time stamp every time the file is saved. augroup PoFileTimestamp au! au BufWrite *.po,*.po.gz call PoFileTimestamp() augroup END fu! PoFileTimestamp() " Prepare for cleanup at the end of this function. let hist_search = histnr("/") let old_report = 'set report='.&report let &report = 100 let cursor_pos_cmd = line(".").'normal! '.virtcol(".").'|' normal! H let scrn_pos = line(".").'normal! zt' " Put in time stamp. normal! 1G if search('^"PO-Revision-Date:') silent! exe 's/^\("PO-Revision-Date:\).*$/\1 ' . strftime("%Y-%m-%d %H:%M%z") . '\\n"' endif " Cleanup and restore old cursor position. while histnr("/") > hist_search && histnr("/") > 0 call histdel("/", -1) endwhile exe scrn_pos exe cursor_pos_cmd exe old_report endf " On "gf" Normal mode command, split window and open the file under the " cursor. if !hasmapto('OpenSourceFile') map gf OpenSourceFile endif noremap OpenSourceFile :call OpenSourceFile() " This opens the file under the cursor in a split-window. fu! OpenSourceFile() " Check if we're at the right line. Return if not. if getline(".") !~ '^#:\s\+' | return | endif " Get the reference, check it, and return if it doesn't have the assumed " format. let ref = expand("") if ref !~ ':\d\+$' | return | endif " Split the reference into the file name and the line number parts. let d = match(ref, ':') let flnm = strpart(ref, 0, d) let lnr = strpart(ref, d+1, 100) " Start searching for the file in the directories specified with the 'path' " option. let ff = globpath(&path, flnm) " Check what's been found. Report if no or more than one file found and " return. if ff == '' echohl WarningMsg | echo "No file found in the path." echohl None exe "normal \" elseif match(ff, "\n") > 0 echohl WarningMsg | echo "More than one file found: " . ff . "\nAborting." echohl None exe "normal \" else " Split the window and open the file at the correct line. execute "silent sp +" . lnr . " " . ff endif endf unlet gui