calculate line and column of a text with JavaScript

Hello Gentlemen,

I have several programs written in cobol and I have developed a web
application called “Program Editor” to manipulate them.
This application , among several functions , calculates the row and column when positioning the cursor on the desired instruction using the mouse.
Program instructions are presented in a textarea field.
It is normal to write a cobol program, starting statement, in different columns depending on the statement level.
For example :
__________________________________________________________
FILE-CONTROL.
_________________________________________________________
COPY “C:\COBOL\WEB\ARQUIVO\CGIACRTO.SEL”.
SELECT PRINT-CIA ASSIGN TO
“C:\PAYBACK\CONTABIL\TABELAS\CONTRATO\X.JS”.
This program excerpt is being presented correctly in the textarea field, that is

a- The character * starts in column 7
b- The F character of FILE-CONTROL starts on line 8
c- The C character of COPY starts at column 8
d- Character S of SELECT starts at column 8
e- The " character of "c:\ starts on line 21

When clicking the mouse after the character " of ITEM E above, the application calculates column=8.
Note - Notepad also shows column 8 however it shows in column 21.

This is due to the coding of the text editor that generated this program ( PWB which is very old )
When pressing the key to move the cursor , it jumps from column 1 to column 9 and then to column 16.
It looks like the PWB EDITOR has a special encoding for consecutive spaces that is correctly interpreted by the field TEXTAREA and NOTEPAD however when counting the characters it is not added correctly.
Note - The line is being calculated correctly.

The Javascript that makes this count is:

function getPosCursor(form) {
var tan = doGetCaretPosition(document.getElementById(‘text’));
var row = 1;
var column = 1;
var element = document.editor.text.value;
if ( tan < 1 ) { return; }
for (var i = 0; i < tan; i++) {
if (element.charAt(i) == “\n” || element.charAt(i) == “\r” ) {
row = row + 1;
column = 1;
} else {
column = column + 1;
}
}
document.forms[0].txtLine.value = line;
document.forms[0].txtColumn.value = column;
}
function doGetCaretPosition(ctrl) {
var CaretPos = 0;
// IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange();
Sel.moveStart(‘character’, -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == ‘0’)
CaretPos = ctrl.selectionStart;
return (CaretPos);
}

Does anyone know how to solve this problem?
Thank you for your attention,
Kleber

Note - This text was translated by google.

After researching this issue on the internet, I managed to solve
this problem by changing the getPosCursor function to :

function getPosCursor(form) {
var tan = doGetCaretPosition(document.getElementById(‘texto’));
var linha = 1;
var coluna = 1;
var especial = ’ ';
/* special character ¬ used for space compression in an 8-byte word */
var resto = 0;
var element = document.editor.texto.value;
if ( tan < 0 ) { return; }
for (var i = 0; i < tan; i++) {
if (element.charAt(i) == “\n” || element.charAt(i) == “\r” ) {
linha = linha + 1;
coluna = 1;
} else {
if ( element.charAt(i) == especial ){
resto=coluna%8;
if ( resto == 0 ) { coluna += -8; }
coluna = coluna + 9 - resto;
} else {
coluna = coluna + 1;
}
}
}
document.forms[0].txtLine.value = linha;
document.forms[0].txtColumn.value = coluna;
};

1 Like

That sounds impressive and complicated! Do you have a screenshot of what the final app looks like? Seems like a great tool for you to make broadly available given how many Cobol-related posts cross my general news feed :grinning: