/* MocaScript for Peggy Series * $Id: cobolautonum.msl,v 1.5 2009/08/22 16:59:27 nzawa Exp nzawa $ * -------------------------------------------------------------------------- * * cobolautonum.msl * * 【概要】 * 言語モードがCOBOLの場合、改行時に一連番号を補完します。 * * 【使用方法】 * Startup.ms から読み込んで、 * cobolAutoNumbering を onEnter のハンドラ関数として登録します。 * * require("cobolautonum"); * Event.onEnter = cobolAutoNumbering; * * _/_/_/_/_/_/_/_/_/_/_/_/_/ COPYRIGHT NOTICE _/_/_/_/_/_/_/_/_/_/_/_/_/ * * Copyright (c) 2009 Nzawa * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ */ global.cobolAutoNumbering = cobolAutoNumbering; function cobolAutoNumbering(vw) { if (vw.getLanguageID() != "COBOL") return false; var CP_LINE = vw.CP.line; var CP_INDEX = vw.CP.index; var EOF_LINE = vw.EOF.line; var NEWLINE = vw.getNewline(); var COB_SEQ_LEN = 6; var COB_SEQ_INTERVAL = 10; var COB_SEQ_SUBINTERVAL = 1; // 前行の一連番号を取得 var seqPrev; if ( CP_LINE > 1 ) { seqPrev = vw.getLine(CP_LINE - 1).match(/^\d{6}/f); seqPrev = seqPrev ? parseInt(seqPrev,10) : COB_SEQ_INTERVAL; } else { // 一行目で未入力の場合 if ( vw.getLine().length == 0 ) { vw.insertText(sprintf("%06d", COB_SEQ_INTERVAL)); if ( CP_LINE == EOF_LINE ) return true; } seqPrev = 0; } // 現在行の一連番号を取得 var seqCurrent = vw.getLine().match(/^\d{6}/f); seqCurrent = seqCurrent ? parseInt(seqCurrent,10) : seqPrev; // 次行の一連番号を取得 var seqNext = null; if ( CP_LINE < EOF_LINE ) seqNext = vw.getLine(CP_LINE + 1).match(/^\d{6}/f); seqNext = seqNext ? parseInt(seqNext,10) : 0; // 新規行の一連番号を決定 var seqNew; if ( CP_LINE == EOF_LINE || seqNext - seqCurrent > COB_SEQ_INTERVAL ) { // 最終行にいる場合、または次行と現在行の差に余裕がある場合は通常の増分とする // 現在行の一連番号が中途半端な可能性があるので、端数を切り捨てて計算する seqNew = (seqCurrent - (seqCurrent % COB_SEQ_INTERVAL)) + COB_SEQ_INTERVAL; } else if ( seqNext - seqCurrent > COB_SEQ_SUBINTERVAL ) { // 余裕が無い時はサブインターバルを使う seqNew = seqCurrent + COB_SEQ_SUBINTERVAL; } else { // それ以外の時は現在行と同じにする seqNew = seqCurrent; } // カーソル位置に応じてインデント用スペースを準備 var indent; if ( CP_INDEX < COB_SEQ_LEN ) { // 一連番号領域にある場合 indent = ""; } else { // 一連番号領域以降の場合 var firstNonSpaceCharIndex = vw.getLine().slice(COB_SEQ_LEN, CP_INDEX).search(/[^ \t]|$/); if ( firstNonSpaceIndex == -1 ) firstNonSpaceCharIndex = COB_SEQ_LEN; indent = vw.getLine().slice(COB_SEQ_LEN, COB_SEQ_LEN + firstNonSpaceCharIndex); } // カーソルが一連番号領域内だったら位置を移動 if ( CP_INDEX < COB_SEQ_LEN ) vw.gotoPoint(Point(CP_LINE, COB_SEQ_LEN)); // 改行処理 vw.insertText(NEWLINE, sprintf("%06d", seqNew), indent); return true; } /* END OF SCRIPT */