Add the support for multiple formats.
Some people does not use the common YYYYMMDDXX format and/or does not want to use the "; serial" comment right after the serial number. This commit allow multiple formats to be defined, hense more users can use the plugin. A huge consequence of this is that user can also define additional custom formats depending on their needs.
This commit is contained in:
parent
acc6dd81e1
commit
317389ec31
3 changed files with 133 additions and 21 deletions
|
@ -12,28 +12,59 @@
|
|||
" See the License for the specific language governing permissions and
|
||||
" limitations under the License.
|
||||
|
||||
function! s:IncrementSerial(old_date, old_nb)
|
||||
let curr_date = strftime("%Y%m%d")
|
||||
if a:old_date ==? curr_date
|
||||
let curr_nb = a:old_nb + 1
|
||||
if curr_nb < 10
|
||||
let curr_nb = "0".curr_nb
|
||||
|
||||
function! s:HasDateChanged(pattern, old_matchlist)
|
||||
let i = 0
|
||||
for matching in a:pattern.matching
|
||||
if matching.type ==? 'date'
|
||||
let old_date = a:old_matchlist[i]
|
||||
let new_date = strftime(matching.fmt)
|
||||
if old_date !=? new_date
|
||||
return 1
|
||||
endif
|
||||
endif
|
||||
else
|
||||
let curr_nb = "01"
|
||||
let i += 1
|
||||
endfor
|
||||
return 0
|
||||
endfunction
|
||||
|
||||
function! s:GetNewValue(matching, old_value, date_changed)
|
||||
if a:matching.type ==? 'date'
|
||||
return strftime(a:matching.fmt)
|
||||
elseif a:matching.type ==? 'integer'
|
||||
let nb = a:old_value + get(a:matching, 'offset', 1)
|
||||
if a:date_changed && get(a:matching, 'date_reset', 0)
|
||||
let nb = 0
|
||||
endif
|
||||
return printf('%0' . get(a:matching, 'padding', 1) . 'd', nb)
|
||||
endif
|
||||
return curr_date.curr_nb
|
||||
return a:old_value
|
||||
endfunction
|
||||
|
||||
function! s:GetNewSerial(pattern, old_matchlist)
|
||||
let date_changed = s:HasDateChanged(a:pattern, a:old_matchlist)
|
||||
let new_serial = []
|
||||
let i = 0
|
||||
for matching in a:pattern.matching
|
||||
let new_value = s:GetNewValue(matching, a:old_matchlist[i], date_changed)
|
||||
call add(new_serial, new_value)
|
||||
let i += 1
|
||||
endfor
|
||||
return join(new_serial, '')
|
||||
endfunction
|
||||
|
||||
function! dnsserial#DNSSerialUpdate()
|
||||
let pattern = '\(\d\{8}\)\(\d\+\)\s*;\s*\cserial'
|
||||
if search(pattern) == 0
|
||||
echom "No serial found."
|
||||
return 0
|
||||
endif
|
||||
let old_pattern = matchlist(getline('.'), pattern)
|
||||
let old_serial = old_pattern[1].old_pattern[2]
|
||||
let new_serial = s:IncrementSerial(old_pattern[1], old_pattern[2])
|
||||
execute "s/".old_serial."/".new_serial."/"
|
||||
echom "Serial updated to ".new_serial."."
|
||||
let patterns = g:dnsserial_custom_patterns + g:dnsserial_patterns
|
||||
for pattern in patterns
|
||||
if search(pattern.regex) != 0
|
||||
let offset = len(pattern.matching)
|
||||
let old_matchlist = matchlist(getline('.'), pattern.regex)[1:offset]
|
||||
let old_serial = join(old_matchlist, '')
|
||||
let new_serial = s:GetNewSerial(pattern, old_matchlist)
|
||||
execute "s/" . old_serial . "/" . new_serial . "/"
|
||||
echom "Serial updated to " . new_serial . "."
|
||||
return 0
|
||||
endif
|
||||
endfor
|
||||
echom "No serial found."
|
||||
endfunction
|
||||
|
|
Reference in a new issue