forked from dilawar/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.vim
203 lines (174 loc) · 4.52 KB
/
format.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
if exists('g:format_vim_done')
finish
endif
let g:format_vim_done=1
function s:Goback()
exec "normal ".s:cursorline."G".s:cursorcol."|"
endfunction
" really go back (including the screen position)
function s:ReallyGoback()
exec "normal ".s:topline."Gzt"
exec "normal ".s:cursorline."G".s:cursorcol."|"
endfunction
" Check whether this line can join the previous line
function s:CanJoin(str)
if &filetype=='tex'
let checks=['\section', '\subsection', '\subsubsection', '\chapter', '\paragraph', '\clearpage', '\include', '\biblio', '\usepack', '\def', '\table', '\psfrag', '\thanks', '\label', '\caption', '\document', '\listoffigures', '\DeclareMathOperator', '\begin', '\end', '\bi', '\ei']
let regex=join(checks, '\|')
let regex=substitute(regex, '\zs\\\ze[^|]', '^\\\\', 'g')
let regex.='\|^%\|^\s'
else
let regex='^\s'
endif
return (match(a:str, regex)==-1)
endfunction
" check if it is the start of a verbatim block (eg. equation)
function s:BlockStart(str)
if &filetype=='tex'
let checks=['\[', '\begin{equation', '\begin{align', ]
let regex=join(checks, '\|')
let regex=substitute(regex, '\zs\\\ze[^|]', '^\\\\', 'g')
let regex=substitute(regex, '\(\[\|\]\)', '\\\1', 'g')
return match(a:str, regex)==0
else
return 0
endif
endfunction
" check if it is the end of a verbatim block (eg. equation)
function s:BlockEnd(str)
if &filetype=='tex'
let checks=['\]', '\end{equation', '\end{align']
let regex=join(checks, '\|')
let regex=substitute(regex, '\zs\\\ze[^|]', '^\\\\', 'g')
let regex=substitute(regex, '\(\[\|\]\)', '\\\1', 'g')
return match(a:str, regex)==0
else
return 0
endif
endfunction
" Output str verbatimly, after finishing half-line business
function s:Verbatim(str)
if s:halfline!=""
let s:newlines+=[s:halfline]
endif
let s:newlines+=[a:str]
let s:halfline=""
endfunction
" Format a single line
function s:FormatLine(line)
if &textwidth>0
let s:textwidth=&textwidth
else
let s:textwidth=80
endif
let str=getline(a:line)
if s:BlockStart(str)
let s:inblock=1
call s:Verbatim(str)
return
endif
if s:BlockEnd(str)
let s:inblock=0
call s:Verbatim(str)
return
endif
" empty line
if match(str, '^\s*$')>=0
if s:prevempty==1
return
else
call s:Verbatim("")
let s:prevempty=1
return
endif
else
let s:prevempty=0
endif
" in a block or cannot join previously
if s:inblock==1 || !s:CanJoin(str)
call s:Verbatim(str)
return
endif
" join with the previous line
let words = split(str, '\s\+')
for word in words
if s:halfline==""
let s:halfline=word
else
if (strlen(word)+strlen(s:halfline)+1)>s:textwidth
let s:newlines+=[s:halfline]
let s:halfline=word
else
let s:halfline.= ' '.word
endif
endif
endfor
endfunction
" format a range of lines
function s:FormatLines(startl, endl)
let s:newlines=[]
let s:halfline=""
let s:inblock=0
let s:prevempty=0
let index=a:startl
while index<=a:endl
call s:FormatLine(index)
let index+=1
endwhile
"flush the last half line if necessary
if s:halfline!=""
let s:newlines+=[s:halfline]
endif
"deal with the stupid/elusive vim undo cursor placement
" Do some change so that VIM will undo to this point
call s:Goback()
normal rx
" delete the old ones
exec ':'.a:startl.','.a:endl.'d'
" insert the new lines
call append(a:startl-1, s:newlines)
endfunction
" format a paragraph
function s:FormatParagraph()
let s:cursorline=line(".")
let s:cursorcol=col(".")
let cursorchar=strpart(getline("."), col(".") - 1, 1)
normal! H
let s:topline = line(".")
call s:Goback()
normal }{
let startl=nextnonblank('.')
if startl>s:cursorline "we are on a blank line
call s:ReallyGoback()
return
endif
normal }
let endl=prevnonblank('.')
call s:Goback()
" compute logical position (num chars) relative to start of paragraph
let index=startl
let old_pos=0
while index < s:cursorline
let old_pos+=strlen(substitute(getline(index), '\s', '', 'g'))
let index+=1
endwhile
let old_pos+=strlen(substitute(strpart(getline(index),0,col('.')), '\s', '', 'g'))
" format
call s:FormatLines(startl, endl)
" restore cursor position
exec ':'.startl
normal 0
if old_pos>1
call search('^\%(\_s*\S\)\{'.(old_pos).'}', 'e', endl+len(s:newlines))
if match(cursorchar, '\s')==0
call search('\_s', '', line('$'))
endif
endif
" restore screen position
let tmpline=line('.')
let tmpcol=col('.')
execute "normal! ".s:topline."Gzt"
exec "normal ".tmpline."G".tmpcol."|"
endfunction
silent! unmap K
nnoremap <silent><buffer> K :silent call <SID>FormatParagraph()<CR>