markdown - vimscript templating with global-counter -


i trying create generic markdown template, using vimscript. each time template called, increment counter.

function 1

#cntr= read/parsed markdown file, incremented

function! getcntr()     let yogaf = readfile("yoga.js") 

i read last-line - 1. last (the line number) prints. however, item (the value @ list index) not:

    let last=len(yogaf)-1     let item= yogaf[last]         echo item         let indx = 0 

i'm little confused vimscript pattern looping through list. tried several things, including #cntr=*... no luck finding match!

    in yogaf         if =~ "#cntr=\&"             let indx =             break         else             let indx = len(yogaf)-1         endif     endfor 

when function called, don't see indx, cnt, or cntr printed.

    echo indx     let cnt = yogaf[indx]     echo string(cnt)     let [g:cntr] = cnt[6:]     echo g:cntr     return g:cntr endfunction 

function2

calls function1, gets g:cntr, writes template markdown file

function3

increments g:cntr , writes update file

i'm not sure trying of things in script don't make sense (mainly loop).

i'm going try , explain think doing , should doing

let yogaf = readfile("yoga.js") 

this reads in file list of strings yagaf. (this fine)

let last=len(yogaf)-1 let item= yogaf[last] 

this gets last line. can simplified using negative index index end of list. whole line useless , nothing cause print item

let item = yogaf[-1] 

next have loop iterate on lines in , try , find line matches pattern #cntr=\& if find store in index. if don't find store last line number in index.

for in yogaf     if =~ '#cntr=\&'         let indx =         break     else         let indx = len(yogaf)-1     endif endfor 

at end of loop indx either string containing line matches or number of lines in file. doubt meant do.

i think meant find line matched #cntr= thats i'm going change do.

you can match line i =~# '#cntr=' match line contains #cntr= in (and has same case). other regex #cntr=* means match line #cntr=* 0 or more = equal signs. might match wanted after equal sign have been #cntr=.* (. matches * 0 or more times)

after loop had

echo indx let cnt = yogaf[indx] echo string(cnt) let [g:cntr] = cnt[6:] echo g:cntr return g:cntr 

cnt (almost) never want lucky if string matched (which did) indexing list string , vim seems return first line isn't want.

for line cnt[6:] think trying strip #cntr front of string matched. cnt doesn't contain want.

so lets modify loop set g:cntr correct value , return if matching line found.

for in g:yogaf     if =~ '#cntr='         let g:cntr = i[6:]         return g:cntr     endif endfor 

at end of loop g:cntr either set or didn't find anything. if didn't find set g:cntr equal -1 , return that.

let g:cntr = -1 return g:cntr 

so completed function should (with extraneous stuff removed)

function! getcntr()     let l:yogaf = readfile("yoga.js")     in l:yogaf         if =~ '^#cntr='             let g:cntr = i[6:]             return g:cntr         endif     endfor     let g:cntr = -1     return g:cntr endfunction 

although i'm not sure want because didn't ask question.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -