You are here: PSPad forum > English discussion forum > Go To Line

Go To Line

#1 Go To Line

Posted by: pythonui | Date: 2015-09-26 06:24 | IP: IP Logged

Hello,

I need to run 'Go To Line' functionality from scripting. Is there any function or action for same ?

Thanks

Options: Reply | Quote | Up ^


#2 Re: Go To Line

Posted by: pspad | Date: 2015-09-26 07:32 | IP: IP Logged

In the script you can use cursor position directly. Look at setCaretPos(x, y) or caretX(), caretY() method

Options: Reply | Quote | Up ^


#3 Re: Go To Line

Posted by: pythonui | Date: 2015-09-26 08:50 | IP: IP Logged

Yes !

setCaretPos(x, y)working great but its not set the center of editor ..

Any workaround ?

Options: Reply | Quote | Up ^


#4 Re: Go To Line

Posted by: pspad | Date: 2015-09-26 09:56 | IP: IP Logged

topLineIndex(), linesInWindow()
I hope you understand hwo to use it.
For more information see Scripting.rtf in PSPad folder or PSPad help

Options: Reply | Quote | Up ^


#5 Re: Go To Line

Posted by: vbr | Date: 2015-09-26 10:19 | IP: IP Logged

pythonui:
Yes !

setCaretPos(x, y)working great but its not set the center of editor ..

Any workaround ?

Hi,
you could probably use something like the following hack

Quote:
function setCaretAndScrollToView(){
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
actEd.setCaretPos(1, 1);
actEd.setCaretPos(1, 94 + Math.floor(actEd.linesInWindow()/2));
actEd.setCaretPos(7, 94);
}

i.e. set the caret to the top to ensure the direction of the scrolling (top-bottom, aligning to the bottom margin)
- set the caret about half the visible line count further
- set the caret to the needed coordinates

hth,
vbr

Edited 1 time(s). Last edit at 2015-09-26 10:29 by vbr.

Options: Reply | Quote | Up ^


#6 Re: Go To Line

Posted by: pspad | Date: 2015-09-26 11:22 | IP: IP Logged

VBR, better is use topLineIndex() funstion.
You know number of lines in the window - linesInWindow()
Half of it is center line - caretY position. So you need to set top line to CaretY position - half of the lines in window

Options: Reply | Quote | Up ^


#7 Re: Go To Line

Posted by: vbr | Date: 2015-09-26 11:56 | IP: IP Logged

pspad:
VBR, better is use topLineIndex() funstion.
You know number of lines in the window - linesInWindow()
Half of it is center line - caretY position. So you need to set top line to CaretY position - half of the lines in window

Thanks for your suggestion; however, can you elaborate on the usage in the script? - I can't find a way with less code than above and possibly without repeated caret settings.
One could possibly check whether the current view (via topLineIndex) is below or above the needed caret position and adjust the half-page offset to the needed direction, but it seems a bit more complicated than the above.

vbr

Options: Reply | Quote | Up ^


#8 Re: Go To Line

Posted by: pspad | Date: 2015-09-26 12:31 | IP: IP Logged

One line less winking smiley

Quote:
function setCaretAndScrollToView(){
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
actEd.setCaretPos(1, 45);
actEd.topLineIndex(45 - Math.floor(actEd.linesInWindow()/2));
}

Options: Reply | Quote | Up ^


#9 Re: Go To Line

Posted by: vbr | Date: 2015-09-26 13:58 | IP: IP Logged

Ok, thanks,
I forgot, we can even set topLine this way (not only determine the value).
vbr

Options: Reply | Quote | Up ^


#10 Re: Go To Line

Posted by: pythonui | Date: 2015-09-28 03:48 | IP: IP Logged

Quote:
One line less

Quote:
function setCaretAndScrollToView(){
if (editorsCount()<1){return;}
var actEd = newEditor();
actEd.assignActiveEditor();
actEd.setCaretPos(1, 45);
actEd.topLineIndex(45 - Math.floor(actEd.linesInWindow()/2));
}

That's perfect smiling smiley

Edited 1 time(s). Last edit at 2015-09-28 03:48 by pythonui.

Options: Reply | Quote | Up ^






Editor PSPad - freeware editor, © 2001 - 2024 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR