简体   繁体   中英

python code in vim script

I have a file, say:

Program  foo
Implicit None
integer::ab
End Program bar

Now, I want the "bar" in last line to be "foo" (ie Program and End program name should be same). I wrote a python script for this, which works very well:

#!/usr/bin/python
import fileinput
with open("i.f90") as inp:
    for line in inp:
        if line.startswith("Program"):
            var = line.rsplit(' ', 1)[-1].strip()
        if line.startswith("End Program"):
            v2 = line.strip()
            print v2
inp.close()
STR="End Program "+var
print STR
for line in fileinput.input("i.f90", inplace=True):
    print line.replace(v2, STR).strip()

But, as I want to call it from vim , as a ftplugin , I put it in vim's ftplugin as:

function! Edname() 
python<<EOF
import vim
import fileinput
with open("i.f90") as inp:
    for line in inp:
        if line.startswith("Program"):
            var = line.rsplit(' ', 1)[-1].strip()
        if line.startswith("End Program"):
            v2 = line.strip()
            print v2
inp.close()
STR="End Program "+var
print STR
for line in fileinput.input("i.f90", inplace=True):
    print line.replace(v2, STR).strip()
EOF
endfunction

As you can see the only changes I have made is to put it in vim. No real change in the actual python code. But in this case, this is not working. It is printing the v2 and STR properly, but the line in the file (or the buffer) is not getting updated.

Any clue?

This is largely to do with my earlier post . But now, I have find a partial solution with python , but that is not working when called from vim.

If that's what you mean by automation , you don't need Python. Just put something like this in a ftplugin for your filetype:

function! s:FixName()
    let [buf, l, c, off] = getpos('.')
    call cursor([1, 1, 0])

    let lnum = search('\v\c^Program\s+', 'cnW')
    if !lnum
        call cursor(l, c, off)
        return
    endif

    let parts = matchlist(getline(lnum), '\v\c^Program\s+(\S*)\s*$')
    if len(parts) < 2
        call cursor(l, c, off)
        return
    endif

    let lnum = search('\v\c^End\s+Program\s+', 'cnW')
    call cursor(l, c, off)
    if !lnum
        return
    endif

    call setline(lnum, substitute(getline(lnum), '\v\c^End\s+Program\s+\zs.*', parts[1], ''))
endfunction

call s:FixName()

You can also do it with a macro, but that doesn't look as clever as the function above. ;) Something like this:

nnoremap <buffer> <silent> <C-p> /\v\c^Program\s+\zs<CR>"zy$/\v\c^End\s+Program\s+\zs<CR>D"zP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM