From 033ed53692bc26bc7080a18070f1705c79c4d76f Mon Sep 17 00:00:00 2001 From: Kevin Lyda <kevin@ie.suberic.net> Date: Tue, 19 Sep 2017 19:03:02 +0100 Subject: [PATCH] Clean up; add Unnumber. Have the python functions loaded in a better way. Make less clashy names. Make the : commands work. --- plugin/cobol.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++ plugin/cobol.vim | 11 ++++++--- plugin/renumber.py | 47 ------------------------------------ 3 files changed, 67 insertions(+), 50 deletions(-) create mode 100755 plugin/cobol.py delete mode 100755 plugin/renumber.py diff --git a/plugin/cobol.py b/plugin/cobol.py new file mode 100755 index 0000000..7b71d29 --- /dev/null +++ b/plugin/cobol.py @@ -0,0 +1,59 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 Kevin Lyda <kevin@phrye.com> +# +# Distributed under terms of the GPL license. + +""" +COBOL utilities. +""" + +import vim +import math + +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)): + 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: + print('Malformed line - has non-numbers in columns 1-6') + return + 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')) + +def cobol_Unnumber(): + if vim.current.buffer.options['filetype'] == b'cobol': + lines = vim.current.buffer + for i in range(len(lines)): + if lines[i][:6].isdigit(): + lines[i] = ' ' + lines[i][6:] + else: + print('Not a COBOL file. This is a %s file' % + vim.current.buffer.options['filetype'].decode('UTF-8')) diff --git a/plugin/cobol.vim b/plugin/cobol.vim index 3b172ef..82f1f37 100644 --- a/plugin/cobol.vim +++ b/plugin/cobol.vim @@ -17,15 +17,20 @@ endif let s:path = fnamemodify(resolve(expand('<sfile>:p')), ':h') function! LoadCobolPython() - !exists('g:cobol_py_loaded') + if !exists('g:cobol_py_loaded') exec 'py3file ' . s:path . '/cobol.py' let g:cobol_py_loaded = 1 endif endfunction function! Renumber() - exec 'py3file ' . s:path . '/renumber.py' + py3 cobol_Renumber() endfunction -" call LoadCobolPython() +function! Unnumber() + py3 cobol_Unnumber() +endfunction + +call LoadCobolPython() command! -nargs=0 Renumber call Renumber() +command! -nargs=0 Unnumber call Unnumber() diff --git a/plugin/renumber.py b/plugin/renumber.py deleted file mode 100755 index ac98e74..0000000 --- a/plugin/renumber.py +++ /dev/null @@ -1,47 +0,0 @@ -#! /usr/bin/env python3 -# -*- coding: utf-8 -*- -# vim:fenc=utf-8 -# -# Copyright © 2017 Kevin Lyda <kevin@phrye.com> -# -# Distributed under terms of the GPL license. - -""" -COBOL utilities. -""" - -import vim -import math - -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