diff --git a/plugin/cobol.vim b/plugin/cobol.vim index 72893d28bfcda6a09d72e8f919ed7abc38fc4336..3b172ef92fe77e908dbce138b90af72323f7de3d 100644 --- a/plugin/cobol.vim +++ b/plugin/cobol.vim @@ -28,3 +28,4 @@ function! Renumber() endfunction " call LoadCobolPython() +command! -nargs=0 Renumber call Renumber() diff --git a/plugin/renumber.py b/plugin/renumber.py index f757c9d3ad8f2a471805bc8dd5133aa4bb107ac7..ac98e74b52515764f883075b3349288d436bfdb9 100755 --- a/plugin/renumber.py +++ b/plugin/renumber.py @@ -11,8 +11,37 @@ COBOL utilities. """ import vim +import math -print(vim.current.buffer.name) -b = vim.current.buffer -for i in range(len(b)): - b[i] = 'moo ' + b[i] +if vim.current.buffer.options['filetype'] == b'cobol': + gap = {} + last_line_no = 0 + lines = vim.current.buffer + for i in range(len(lines)): + if len(lines[i]) == 0 or lines[i][:6] == ' ': + if not gap: + gap['i'] = i + gap['last_line_no'] = last_line_no + else: + try: + last_line_no = int(lines[i][:6]) + except ValueError: + raise ValueError('Malformed line - has non-numbers in columns 1-6') + if gap: + delta = math.floor((last_line_no - gap['last_line_no']) / + (i - gap['i'] + 1)) + if delta > 1: + new_line_no = gap['last_line_no'] + delta + for j in range(gap['i'], i): + lines[j] = ('%06d' % new_line_no) + lines[j][6:] + new_line_no += delta + gap = {} + if gap: + delta = 100 + new_line_no = gap['last_line_no'] + delta + for j in range(gap['i'], len(lines)): + lines[j] = ('%06d' % new_line_no) + lines[j][6:] + new_line_no += delta +else: + print('Not a COBOL file. This is a %s file' % + vim.current.buffer.options['filetype'].decode('UTF-8'))