Page 1 of 1

Current line index

Posted: 23 Apr 2015 22:09
by Aleks842
Hello!

How can I get the current line index in pascal script?

Re: Current line index

Posted: 24 Apr 2015 07:29
by pjj

Code: Select all

var
   y: Integer;
begin
y := Document.CursorY;
ShowMessage(y);
end.

Re: Current line index

Posted: 24 Apr 2015 08:47
by Aleks842
Thanks!

And how I can delete the current line in pascal?

Re: Current line index

Posted: 24 Apr 2015 09:33
by pjj
First off, let me show you the answer in JavaScript, it's easier for me and shouldn't be hard for you to translate it to Pascal; just remember, JS surrounds strings with double quotes, while Pascal with single ones.

Here's a quick solution:

Code: Select all

var y = Document.CursorY;
Document.CursorDocEnd(false);
var maxY = Document.CursorY;
for (var i = y; i < maxY; i++) {
    Document.Lines[i] = Document.Lines[i + 1];
}
Document.Lines[maxY] = "";
Variable y holds the index of line with cursor, maxY is the number of all lines in your document minus 1 (index of the last line, that is.) The code loops through all remaining lines (after y), copying them up, then deletes the doubled last line. You end up with blank line at the end of your document, which can be then easily removed during save.

If you don't have blank lines in your document, you can use

Code: Select all

var y = Document.CursorY;
Document.Lines[y] = "";
Document.DeleteBlankLines();
You could also use

Code: Select all

var y = Document.CursorY;
Document.Lines[y] = "";
Document.JoinLines();
but JoinLines doesn't work with blank lines, which imho is a bug and I'm going to report it.

Hope that helps!

Re: Current line index

Posted: 24 Apr 2015 11:36
by Aleks842
Thanks!
But I found another solution. :)

Code: Select all

//Pascal
var
   y: integer;
   list: TStringList;

begin
   Document.BeginUpdate();
   Document.Wordwrap:= False;
   y:= Document.CursorY; //Current line
   list:= TStringList.Create; 
   list.Text:= Document.Text;
   list.Delete(y);
   Document.Text:= list.Text; //after paste cursor is in the end of the document
   Document.CursorY:=y; //set cursor in start position
   list.free;
   Document.Wordwrap:= True;
   Document.EndUpdate();
end;

Re: Current line index

Posted: 24 Apr 2015 11:48
by pjj
This is cool! Thanks for sharing :)

Re: Current line index

Posted: 24 Apr 2015 13:32
by Rickard Johansson
A simpler way to delete the current line would be add:

Code: Select all

...
  Document.SelectCurrentLine;
  Document.SelText := '';
...
Just saying :wink:

Re: Current line index

Posted: 24 Apr 2015 13:42
by pjj
This doesn't delete line for me, just makes it empty -- but the newline is still there (at least in JS.)

Re: Current line index

Posted: 24 Apr 2015 13:44
by Aleks842
Rickard Johansson wrote:A simpler way to delete the current line would be add:

Code: Select all

...
  Document.SelectCurrentLine;
  Document.SelText := '';
...
Just saying :wink:
But in this case we delete only content of the line. And I need to delete full line with move up. In other words I need to delete the char #13 in the end of line. :)

Re: Current line index

Posted: 24 Apr 2015 14:28
by Rickard Johansson
Ah, well maybe this will work then :lol:

Code: Select all

...
  y := Document.CursorY;
  if (y < Document.LineCount - 1) then
    Document.SetSelection(0, y, 0 , y+1)
  else
    Document.SetSelection(Length(Document.Lines[y-1]), y-1, Length(Document.Lines[y]), y);
  Document.SelText := '';
...