How to toggle lines first character?

English main discussion
Post Reply
  • Author
  • Message
Offline
Posts: 1
Joined: Sun Jul 26, 2015 12:08 pm

How to toggle lines first character?

Post 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

KDJ
Offline
Posts: 1949
Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland

Post 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*/);
}

Post Reply