From d519c9814b5490dd9c6a80185c88f2737c38df58 Mon Sep 17 00:00:00 2001
From: Kevin Lyda <kevin@ie.suberic.net>
Date: Tue, 19 Sep 2017 22:50:31 +0100
Subject: [PATCH] Work in python2 environments.

---
 plugin/cobol.py  | 14 ++++++++++----
 plugin/cobol.vim | 15 ++++++++++-----
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/plugin/cobol.py b/plugin/cobol.py
index 838446a..d1b189e 100755
--- a/plugin/cobol.py
+++ b/plugin/cobol.py
@@ -5,6 +5,7 @@
 # Copyright © 2017 Kevin Lyda <kevin@phrye.com>
 #
 # Distributed under terms of the GPL license.
+from __future__ import print_function
 
 """
 COBOL utilities.
@@ -13,12 +14,17 @@ COBOL utilities.
 import vim
 import math
 
+try:
+  xrange
+except NameError:
+  xrange = range
+
 def cobol_Renumber():
   if vim.current.buffer.options['filetype'] == b'cobol':
     gap = {}
     last_line_no = 0
     lines = vim.current.buffer
-    for i in range(len(lines)):
+    for i in xrange(len(lines)):
       if len(lines[i]) == 0 or lines[i][:6] == '      ':
         if not gap:
           gap['i'] = i
@@ -34,14 +40,14 @@ def cobol_Renumber():
             (i - gap['i'] + 1))
           if delta > 1:
             new_line_no = gap['last_line_no'] + delta
-            for j in range(gap['i'], i):
+            for j in xrange(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)):
+      for j in xrange(gap['i'], len(lines)):
         lines[j] = ('%06d' % new_line_no) + lines[j][6:]
         new_line_no += delta
   else:
@@ -51,7 +57,7 @@ def cobol_Renumber():
 def cobol_Unnumber():
   if vim.current.buffer.options['filetype'] == b'cobol':
     lines = vim.current.buffer
-    for i in range(len(lines)):
+    for i in xrange(len(lines)):
       if lines[i][:6].isdigit():
         lines[i] = '      ' + lines[i][6:]
   else:
diff --git a/plugin/cobol.vim b/plugin/cobol.vim
index 82f1f37..8771c48 100644
--- a/plugin/cobol.vim
+++ b/plugin/cobol.vim
@@ -9,8 +9,13 @@ if exists('g:loaded_cobol') || &cp
 endif
 let g:loaded_cobol = 1
 
-if !has('python3')
-  echo "Missing python."
+if has('python3')
+  let s:pyfile_cmd = 'py3file'
+  let s:py_cmd = 'py3'
+elseif has('python')
+  let s:pyfile_cmd = 'pyfile'
+  let s:py_cmd = 'py'
+else
   finish
 endif
 
@@ -18,17 +23,17 @@ let s:path = fnamemodify(resolve(expand('<sfile>:p')), ':h')
 
 function! LoadCobolPython()
   if !exists('g:cobol_py_loaded')
-    exec 'py3file ' . s:path . '/cobol.py'
+    exec s:pyfile_cmd s:path . '/cobol.py'
     let g:cobol_py_loaded = 1
   endif
 endfunction
 
 function! Renumber()
-  py3 cobol_Renumber()
+  exec s:py_cmd 'cobol_Renumber()'
 endfunction
 
 function! Unnumber()
-  py3 cobol_Unnumber()
+  exec s:py_cmd 'cobol_Unnumber()'
 endfunction
 
 call LoadCobolPython()
-- 
GitLab