Vim Tips Wiki

Tlgrok/Automatic per-language settings based on first letter in document

< User:Tlgrok

Revision as of 15:13, February 8, 2009 by Tlgrok (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
1,616pages on
this wiki

This is a draft, quarantined as a subpage of my own userpage until (and if) it is ready to face the world. Feel free to edit it.

This tip assumes you're using UTF8. If you aren't, you should.

If you use vim to write documents in several languages, it is not unlikely that you'd like to do certain things whenever a foreign language document is loaded. Unfortunately, there is no one way to determine a document's "language". This tip suggests determening a document's language based on the very first letter in it.

First, you will need to add to your .vimrc the Unicode code-to-script-name function.

Second, add the following function to your .vimrc:

" Gets code of first multibyte letter
" Returns -1 if no letter found
function s:GetFirstMultibyteLetterCode()
    let l:save_cursor = getpos(".")
    let l:first_line = search("\\K", 'c') 
    if (l:first_line!=0)
        let l:temp_a = @a
        normal! "ayl
        let l:first_letter = char2nr(@a)
        let @a = l:temp_a
        let l:first_letter = -1
    call setpos('.', l:save_cursor)
    return l:first_letter

Third, add something like the following to your .vimrc (replace "Hebrew" with your script of choice):

" Automatically detect language
function s:DetectLanguage ()
    let l:first_letter_code = s:GetFirstMultibyteLetterCode()
    if (l:first_letter_code==-1) | return | endif
    if (UnicodeCodeToScriptName(l:first_letter_code)=="Hebrew")
        " do your Hebrew stuff
        " ...
autocmd BufRead *.txt call s:DetectLanguage()

And that's that.


What Hebrew stuff might you be inclined to do? Well, if you edit Hebrew or Arabic documents (or any other RTL language), you might like:

setlocal rightleft

Note that another way to achieve this, without bothering with any of the above, is using modelines. You can add the following line, for instance, to any given file:

vim: set rightleft:


There's got to be a better way to implement s:GetFirstMultibyteLetterCode(). I'll look into this. tlgrok 23:39, 26 January 2009 (UTC)

Around Wikia's network

Random Wiki