From 4a991ec738a3f34f774c560afb798cc42e258457 Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@ie.suberic.net>
Date: Tue, 19 Sep 2017 18:26:31 +0100
Subject: [PATCH] Renumbering works.

---
 plugin/cobol.vim   |  1 +
 plugin/renumber.py | 37 +++++++++++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/plugin/cobol.vim b/plugin/cobol.vim
index 72893d2..3b172ef 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 f757c9d..ac98e74 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'))
-- 
GitLab