-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathedit.vim
More file actions
247 lines (212 loc) · 6.53 KB
/
edit.vim
File metadata and controls
247 lines (212 loc) · 6.53 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
" ============================================================================
" Description: An ack/ag/pt/rg powered code search and view tool.
" Author: Ye Ding <dygvirus@gmail.com>
" Licence: Vim licence
" Version: 2.6.0
" ============================================================================
" s:DiffFile()
"
func! s:DiffFile(orig, modi) abort
if a:orig.filename !=# a:modi.filename
\ || len(a:orig.paragraphs) != len(a:modi.paragraphs)
throw 'InconsistentException'
endif
let i = 0
while i < len(a:orig.paragraphs)
let opar = a:orig.paragraphs[i]
let mpar = a:modi.paragraphs[i]
let i += 1
if opar.range() != mpar.range()
return 1
endif
let j = 0
while j < opar.range()
if opar.lines[j].content !=# mpar.lines[j].content
return 1
endif
let j += 1
endwh
endwh
return 0
endf
" s:Diff()
"
" Return list of changed files.
"
func! s:Diff(orig, modi) abort
if len(a:orig) != len(a:modi)
throw 'InconsistentException'
endif
let changed_files = []
let i = 0
while i < len(a:orig)
let [file_orig, file_modi] = [a:orig[i], a:modi[i]]
let i += 1
if s:DiffFile(file_orig, file_modi)
call add(changed_files, {
\ "orig": file_orig,
\ "modi": file_modi
\ })
call ctrlsf#log#Debug("ChangedFile: %s", file_orig.filename)
endif
endwh
return changed_files
endf
" s:TrimPhantomLine()
"
" workaround an issue of ag:
"
" https://github.com/ggreer/the_silver_searcher/issues/685
"
func! s:TrimPhantomLine(buffer, orig) abort
let tail_par = a:orig.paragraphs[-1]
let tail_lnum = tail_par.lnum() + tail_par.range() - 1
if len(a:buffer) <= tail_lnum - 1
call ctrlsf#log#Debug("Remove phatom line in file: %s", a:orig.filename)
call tail_par.trim_tail()
endif
endf
" s:VerifyConsistent()
"
" Check if a file in resultset is different from its disk counterpart.
"
func! s:VerifyConsistent(on_disk, on_mem) abort
for par in a:on_mem.paragraphs
for i in range(par.range())
let ln = par.lnum() + i
let line = par.lines[i]
let content = line.content
if ctrlsf#opt#GetOpt('encoding') != ''
let line = iconv(content, 'utf-8', ctrlsf#opt#GetOpt('encoding'))
endif
if content !=# a:on_disk[ln-1]
call ctrlsf#log#Debug("InconsistentContent: [Lnum]: %d,
\ [FileInMem]: %s, [FileOnDisk]: %s",
\ ln, line.content, a:on_disk[ln-1])
return 0
endif
endfo
endfo
return 1
endf
" s:WriteParagraph()
"
" Write contents in paragraph to buffer.
"
func! s:WriteParagraph(buffer, orig, modi) abort
let orig_count = a:orig.range()
let modi_count = a:modi.range()
let start_lnum = a:modi.lnum()
for i in range(modi_count)
let mline = a:modi.lines[i]
let ln = start_lnum + i
let line = mline.content
if ctrlsf#opt#GetOpt('encoding') != ''
let line = iconv(line, 'utf-8', ctrlsf#opt#GetOpt('encoding'))
endif
if i < orig_count
let a:buffer[ln-1] = line
else
call insert(a:buffer, line, ln-1)
endif
endfo
" remove deleted lines from paragraph
if orig_count > modi_count
for i in range(orig_count-1, modi_count, -1)
let ln = start_lnum + i
call remove(a:buffer, ln-1)
endfo
endif
endf
" s:SaveFile()
"
func! s:SaveFile(orig, modi) abort
let file = ''
if isabsolutepath(a:orig.filename)
let file = a:orig.filename
else
let file = g:ctrlsf_pwd . '/' . a:orig.filename
endif
try
let buffer = readfile(file)
catch
call ctrlsf#log#Error("Failed to open file %s", file)
return -1
endtry
" workaround an issue of ag
call s:TrimPhantomLine(buffer, a:orig)
" if file is changed after searching thus shows differences against
" resultset, skip writing and warn user.
if !s:VerifyConsistent(buffer, a:orig)
call ctrlsf#log#Error("File %s has been changed since last search. Skip
\ this file. Please run :CtrlsfUpdate to update your search result."
\ , file)
return -1
endif
let i = 0
while i < len(a:orig.paragraphs)
let opar = a:orig.paragraphs[i]
let mpar = a:modi.paragraphs[i]
let i += 1
call s:WriteParagraph(buffer, opar, mpar)
endwh
" append <CR> to each line when file's format is 'dos'
if ctrlsf#fs#DetectFileFormat(file) == 'dos'
for i in range(len(buffer))
let buffer[i] .= "\r"
endfo
endif
if writefile(buffer, file) == -1
call ctrlsf#log#Error("Failed to write file %s", file)
else
call ctrlsf#log#Debug("WritingFile: %s succeed.", file)
endif
endf
" Save()
"
func! ctrlsf#edit#Save() abort
let orig = ctrlsf#db#FileResultSet()
let rs = ctrlsf#view#Unrender(getline(0, '$'))
let modi = ctrlsf#db#FileResultSetBy(rs)
call ctrlsf#log#Debug("UnrenderedResultSet: %s", rs)
" check difference and validity
try
let changed = s:Diff(orig, modi)
catch /InconsistentException/
call ctrlsf#log#Error("CtrlSF's write buffer is corrupted. Note that
\ you can't insert line/delete block/delete entire
\ file in edit mode.")
return -1
endtry
" prompt to confirm save
if g:ctrlsf_confirm_save
call ctrlsf#log#Info(printf("%s files will be saved. Confirm? (Y/n)",
\ len(changed)))
let confirm = nr2char(getchar()) | redraw!
if !(confirm ==? "y" || confirm ==? "\r")
call ctrlsf#log#Info("Cancelled.")
return -1
endif
endif
if len(changed) == 0
call ctrlsf#log#Warn("No file has been changed.")
return -1
endif
" start saving...
let [saved, skipped] = [0, 0]
for file in changed
if s:SaveFile(file.orig, file.modi) > -1
let saved += 1
else
let skipped += 1
endif
endfo
" update resultset after saving
call ctrlsf#db#SetResultSet(rs)
if skipped == 0
call ctrlsf#log#Info("%s files are saved.", saved)
else
call ctrlsf#log#Info("%s files are saved (%s skipped).", saved, skipped)
endif
return len(changed)
endf