// http://akelpad.sourceforge.net/forum/viewtopic.php?p=5808#5808
// Version: 1.1
// Author: Shengalts Aleksander aka Instructor
//
//
// Description(1033): Select range of lines.
// Description(1049): Выделить диапазон строк.
//
// Arguments:
// -Range="15-20"  -Select lines from 15 to 20. If not specified dialog is appeared.
//
// Usage:
// Call("Scripts::Main", 1, "SelectRange.js", `-Range="3-11"`)

//Arguments
var pRange=AkelPad.GetArgValue("Range", "");

//Variables
var hMainWnd=AkelPad.GetMainWnd();
var hWndEdit=AkelPad.GetEditWnd();
var aRange;
var nMinLine;
var nMaxLine;
var nMinLineIndex;
var nMaxLineIndex;
var nSelEnd;

if (hWndEdit)
{
  if (!pRange)
  {
    nSelEnd=AkelPad.GetSelEnd();
    nMinLine=AkelPad.SendMessage(hWndEdit, 3129 /*AEM_GETLINENUMBER*/, 21 /*AEGI_UNWRAPLINEFROMRICHOFFSET*/, AkelPad.GetSelStart());
    nMaxLine=AkelPad.SendMessage(hWndEdit, 3129 /*AEM_GETLINENUMBER*/, 21 /*AEGI_UNWRAPLINEFROMRICHOFFSET*/, nSelEnd);
    if (AkelPad.SendMessage(hWndEdit, 3138 /*AEM_GETRICHOFFSET*/, 18 /*AEGI_WRAPLINEBEGIN*/, nSelEnd) == nSelEnd)
      --nMaxLine;
    pRange=AkelPad.InputBox(hMainWnd, WScript.ScriptName, "LineStart-LineEnd", (nMinLine + 1) + "-" + (nMaxLine + 1));
  }

  if (pRange)
  {
    if (aRange=pRange.split("-"))
    {
      nMinLineIndex=AkelPad.SendMessage(hWndEdit, 3138 /*AEM_GETRICHOFFSET*/, 41 /*AEGI_RICHOFFSETFROMUNWRAPLINE*/, parseInt(aRange[0]) - 1);
      nMaxLineIndex=AkelPad.SendMessage(hWndEdit, 3138 /*AEM_GETRICHOFFSET*/, 41 /*AEGI_RICHOFFSETFROMUNWRAPLINE*/, aRange.length > 1?parseInt(aRange[1]):parseInt(aRange[0]));
      AkelPad.SetSel(nMinLineIndex, nMaxLineIndex);
    }
  }
}
