Current line index

Ask questions about how to create a script or swap scripts with other users.
Post Reply
Aleks842
Posts: 14
Joined: 02 Apr 2014 16:37

Current line index

Post by Aleks842 »

Hello!

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

User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Current line index

Post by pjj »

Code: Select all

var
   y: Integer;
begin
y := Document.CursorY;
ShowMessage(y);
end.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

Aleks842
Posts: 14
Joined: 02 Apr 2014 16:37

Re: Current line index

Post by Aleks842 »

Thanks!

And how I can delete the current line in pascal?

User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Current line index

Post 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!
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

Aleks842
Posts: 14
Joined: 02 Apr 2014 16:37

Re: Current line index

Post 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;

User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Current line index

Post by pjj »

This is cool! Thanks for sharing :)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

User avatar
Rickard Johansson
Site Admin
Posts: 6001
Joined: 19 Jul 2006 14:29

Re: Current line index

Post by Rickard Johansson »

A simpler way to delete the current line would be add:

Code: Select all

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

User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Current line index

Post by pjj »

This doesn't delete line for me, just makes it empty -- but the newline is still there (at least in JS.)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

Aleks842
Posts: 14
Joined: 02 Apr 2014 16:37

Re: Current line index

Post 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. :)

User avatar
Rickard Johansson
Site Admin
Posts: 6001
Joined: 19 Jul 2006 14:29

Re: Current line index

Post 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 := '';
...

Post Reply