// http://akelpad.sourceforge.net/forum/viewtopic.php?p=6504#6504
// Version: 1.2
// Author: Shengalts Aleksander aka Instructor
//
//
// Description(1033): Duplicate selected lines.
// Description(1049): Продублировать выделенные строки.

var hWndEdit=AkelPad.GetEditWnd();
var pSelText;
var aLines;
var aResultLines=[];
var nIndex;
var nResultIndex=0;

if (hWndEdit)
{
  if (AkelPad.GetSelStart() == AkelPad.GetSelEnd())
    SelectCaretLine(hWndEdit);

  if (pSelText=AkelPad.GetSelText())
  {
    if (aLines=pSelText.split("\r"))
    {
      for (nIndex=0; nIndex < aLines.length; ++nIndex)
      {
        aResultLines[nResultIndex++]=aLines[nIndex];
        aResultLines[nResultIndex++]=aLines[nIndex];
      }
      pSelText=aResultLines.join("\r");
    }
    AkelPad.ReplaceSel(pSelText, -2);
  }
}

function SelectCaretLine(hWnd)
{
  var nCaretOffset=AkelPad.SendMessage(hWndEdit, 3138 /*AEM_GETRICHOFFSET*/, 5 /*AEGI_CARETCHAR*/, 0);
  var nLineStart=AkelPad.SendMessage(hWndEdit, 3138 /*AEM_GETRICHOFFSET*/, 18 /*AEGI_WRAPLINEBEGIN*/, nCaretOffset);
  var nLineEnd=AkelPad.SendMessage(hWndEdit, 3138 /*AEM_GETRICHOFFSET*/, 19 /*AEGI_WRAPLINEEND*/, nCaretOffset);

  AkelPad.SetSel(nLineStart, nLineEnd);
}
