FORTRANcase.vim

From NA-Wiki

Jump to: navigation, search
" $Id: FORTRANcase.vim,v 5f7ae0a76eef 2008/10/16 12:29:11 holst $
"
" FORTRANcase.vim by Henrik Holst <holst@nada.kth.se>
" Vim script to make all your code in FORTRAN UPPER CASE the way the Gods intended FORTRAN CODE to be written.
" 
" Version 4 - fixed a bug with nested strings
" Version 3 - utilize "ft" variable to determine if case should be altered
"             instead of hardcoded against file extensions
" Version 2 - changed numeric codes into symbol names to make it portable.
" Version 1 - initial version.

function FortranPutChar(chr)
    if &ft != 'fortran' 
        return a:chr
    endif
    let l:toupper = 1
    if (synIDattr(synID(line("."),col(".")-1,1),'name') == 'fortranComment') && (col(".") > 1)
        let l:toupper = 0
    elseif (synIDattr(synID(line("."),col(".")-1,1),'name') == 'fortranStringEx') && 
                \ (getline(".")[col(".")-2] != "\'") || 
                \ (synIDattr(synID(line("."),col(".")-1,1),'name') == 'fortranStringEx') &&
                \ (synIDattr(synID(line("."),col(".")-3,1),'name') != 'fortranStringEx')
        let l:toupper = 0
    elseif (synIDattr(synID(line("."),col(".")-1,1),'name') == 'fortranString') &&
                \ (getline(".")[col(".")-2] != "\"")
                \ || (synIDattr(synID(line("."),col(".")-1,1),'name') == 'fortranString')
                \ && (synIDattr(synID(line("."),col(".")-3,1),'name') != 'fortranString')
        let l:toupper = 0
    elseif (synIDattr(synID(line("."),col(".")-1,1),'name') == 'cIncluded')
                \ && (getline(".")[col(".")-2] != "\"")
                \ || (synIDattr(synID(line("."),col(".")-1,1),'name') == 'cIncluded')
                \ && (synIDattr(synID(line("."),col(".")-3,1),'name') != 'cIncluded')
        let l:toupper = 0
    endif
    if l:toupper
        return toupper(a:chr)
    else
        return a:chr
    endif
endfunction

" Maybe we should enable the script for certain file extensions only?

inoremap <expr> a FortranPutChar("a")
inoremap <expr> b FortranPutChar("b")
inoremap <expr> c FortranPutChar("c")
inoremap <expr> d FortranPutChar("d")
inoremap <expr> e FortranPutChar("e")
inoremap <expr> f FortranPutChar("f")
inoremap <expr> g FortranPutChar("g")
inoremap <expr> h FortranPutChar("h")
inoremap <expr> i FortranPutChar("i")
inoremap <expr> j FortranPutChar("j")
inoremap <expr> k FortranPutChar("k")
inoremap <expr> l FortranPutChar("l")
inoremap <expr> m FortranPutChar("m")
inoremap <expr> n FortranPutChar("n")
inoremap <expr> o FortranPutChar("o")
inoremap <expr> p FortranPutChar("p")
inoremap <expr> q FortranPutChar("q")
inoremap <expr> r FortranPutChar("r")
inoremap <expr> s FortranPutChar("s")
inoremap <expr> t FortranPutChar("t")
inoremap <expr> u FortranPutChar("u")
inoremap <expr> v FortranPutChar("v")
inoremap <expr> w FortranPutChar("w")
inoremap <expr> x FortranPutChar("x")
inoremap <expr> y FortranPutChar("y")
inoremap <expr> z FortranPutChar("z")
Personal tools