Page 1 of 1

How to toggle lines first character?

Posted: Sun Jul 26, 2015 12:55 pm
by sinilill
Hi, I wan't to toggle the first character of the currently selected line.
If the first chracter of the line is # then it should remove the chracter and when it's not the first character, then it should write # as the first character.

Thanks

Posted: Sun Jul 26, 2015 3:52 pm
by KDJ
sinilill
Script :

Code: Select all

var hEditWnd = AkelPad.GetEditWnd();
var nOffset1;
var nOffset2;
var aLine;
var sChar;
var i;

if (hEditWnd)
{
  nOffset1 = AkelPad.SendMessage(hEditWnd, 3138 /*AEM_GETRICHOFFSET*/, 18 /*AEGI_WRAPLINEBEGIN*/, AkelPad.GetSelStart());
  nOffset2 = AkelPad.SendMessage(hEditWnd, 3138 /*AEM_GETRICHOFFSET*/, 19 /*AEGI_WRAPLINEEND*/, AkelPad.GetSelEnd());
  aLine    = AkelPad.GetTextRange(nOffset1, nOffset2).split("\r");
  sChar    = "#";

  for (i = 0; i < aLine.length; ++i)
  {
    if (aLine[i].charAt(0) == sChar)
      aLine[i] = aLine[i].substr(1);
    else
      aLine[i] = sChar + aLine[i];
  }

  AkelPad.SetSel(nOffset1, nOffset2);
  AkelPad.ReplaceSel(aLine.join("\r"), -1 /*RST_SELECT*/);
}