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" (i.e. 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.