I'm creating a new function to justify text. I know there are a few plugins but they don't do what I want to do so I decided to create a function myself.
before:
text_text_text_text_____
after:
text__text___text___text
What I did first is to find the number of words and the number of non space characters (for every line in my text):
let @e = '' | redir @e | silent exe i.'s/'.cols.'\(\S\+\)/&/gne' | redir END
if matchstr(@e, 'match') != '' | let nrwords = matchstr(@e, '\d\+\ze') | else | continue | endif
let @e = '' | redir @e | silent exe i.'s/'.cols.'\S/&/gne' | redir END
if matchstr(@e, 'match') != '' | let nonspaces = matchstr(@e, '\d\+\ze') | else | let nonspaces = 0 | endif
Then to find the spaces:
let spaces = textwidth_I_want - nonspaces
I have to divide the spaces between the words:
let SpacesBetweenWords = spaces/(str2float(nrwords)-1)
However often it is a float number.
p.e. spaces = 34
nr.words-1 = 10
SpacesBetweenWords = 3.4
What I want to do is to divide the spaces between the word, like this:
Spaces between words:
4 3 3 4 3 3 4 3 3 4
and put them in a list 'SpaceList'
and then insert them between the words
for m in range(1,len(SpaceList))
exe i 's/^'.cols.'\(\S\+\zs\s\+\ze\S\+\)\{'.m.'}/'.repeat(' ', SpaceList[m-1]).'/'
endfor
(cols = my block selection OR entire line)
It is easy to create a list with all integers p.e. in my example
3 3 3 3 3 3 3 3 3 3
(Spaces = 30)
but there are still 4 spaces to divide between the words.
My problem is "how can I divide these spaces between the number of words"?