2

テキストを両端揃えにする新しい関数を作成しています。いくつかのプラグインがあることは知っていますが、それらは私がやりたいことをしないので、自分で関数を作成することにしました。

前:

text_text_text_text_____

後:

text__text___text___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

次に、スペースを見つけるには:

let spaces = textwidth_I_want - nonspaces

単語間のスペースを分割する必要があります。

let SpacesBetweenWords = spaces/(str2float(nrwords)-1)   

ただし、多くの場合、浮動小数点数です。
spaces = 34
nr.words-1 = 10
SpacesBetweenWords = 3.4

私がやりたいことは、次のように単語間のスペースを分割することです:
Spaces between words:
4 3 3 4 3 3 4 3 3 4
そしてそれらをリスト 'SpaceList' に入れます

そして、それらを単語の間に挿入します

 for m in range(1,len(SpaceList))
    exe i 's/^'.cols.'\(\S\+\zs\s\+\ze\S\+\)\{'.m.'}/'.repeat(' ', SpaceList[m-1]).'/'
 endfor

(列 = 私のブロック選択または行全体)

私の例
3 3 3 3 3 3 3 3 3 3(スペース = 30)ですべての整数 pe を含むリストを作成するのは簡単
ですが、単語間を区切るためにまだ 4 つのスペースがあります。
私の問題は、「これらのスペースを単語数でどのように分割できるか」です。

4

2 に答える 2

1

結果は3.4、つまり、単語間のすべての距離を同じ長さにすることはできません。textwidth_I_want結果が整数になるように値を調整するか、距離を として設定し、3必要なスペースの数を計算する必要があります ( 10*(3.4-3)=4)。これらの 4 つのスペースを 4 つの距離に追加できます。

于 2015-12-17T11:23:58.520 に答える
0

Pythonを使用して解決策を見つけました

let r = float2nr(SpacesBetweenWords)
let places = nrwords-1
"the extra spaces to put between words
let extraspaces2divide = float2nr(spaces-r*places)
"find randomly with the help of python a serie of (unique) numbers for every space to divide, numbers between 1 and the number of words
exe "py import vim,random;  Listvalue = random.sample(xrange(1,".(places+1).",1),".extraspaces2divide.");vim.command(\"let randomlist = '%s'\"% Listvalue)"

"the result of python is a string like this [1, 3, 6, 9]: remove the brackets and spaces and split the result to a list
let randomlist = substitute(randomlist, '[\[\] ]', '', 'g')
"this list is the serie of random numbers (= the place between words) where to add an extra space
let list = sort(split(randomlist, ','))

for s in range(1,nrwords-1)
  if (index(list, ''.s.'') != -1)
    let r2 = r+1
  else
    let r2 = r
  endif
  call add(SpaceList, r2)
endfor
于 2015-12-18T10:23:56.967 に答える