2

I'm using the "Finding the next sectioning command" TeXstudio script from the TeXstudio wiki.

The script moves the editor's cursor to the next sectioning command, i.e. the next \section{}, \subsection{}, etc.

The script works flawflessly, in that the cursor is moved to the correct places when running the script. However, the editor's viewport, i.e. the part of the document being viewed, does not update. This means the updated cursor location at the next section can be outside of the part of the document being viewed. The view is updated if the cursor is moved via the arrow keys.

Is there a way to update the editor's viewport in a script to move the view to the new cursor position? I was unable to find anything obvious in the script section of the TeXstudio user manual.

I am running TeXStudio 2.11.2 (hg 6191:c01fb289a5fe). The script is:

%SCRIPT
commands = ["\\part", 
            "\\chapter",
            "\\section",
            "\\subsection",
            "\\subsubsection",
            "\\paragraph"]
while (!cursor.atEnd()) {
    cursor.movePosition(1, cursorEnums.NextWord)
    if (cursor.nextChar() != '\\'.charCodeAt(0))
        continue;
    cursor.movePosition(1, cursorEnums.NextCharacter, cursorEnums.KeepAnchor);
    cursor.movePosition(1, cursorEnums.EndOfWord, cursorEnums.KeepAnchor);
    if (commands.indexOf(cursor.selectedText()) >= 0) {
        cursor.setColumnNumber(cursor.anchorColumnNumber())
        break;
    }
}
3

Option 1:

You need editor.scrollToFirstLine(cursor.lineNumber()). This sets the first line of your editor window to be the line with the \section etc. command. If you rather the line be in the center of your window, you can try playing around with the values, like: editor.scrollToFirstLine(cursor.lineNumber()-15), for example.

gif jump section

%SCRIPT
commands = ["\\part", 
            "\\chapter",
            "\\section",
            "\\subsection",
            "\\subsubsection",
            "\\paragraph"]
while (!cursor.atEnd()) {
    cursor.movePosition(1, cursorEnums.NextWord)
    if (cursor.nextChar() != '\\'.charCodeAt(0))
        continue;
    cursor.movePosition(1, cursorEnums.NextCharacter, cursorEnums.KeepAnchor);
    cursor.movePosition(1, cursorEnums.EndOfWord, cursorEnums.KeepAnchor);
    if (commands.indexOf(cursor.selectedText()) >= 0) {
        cursor.setColumnNumber(cursor.anchorColumnNumber())
        editor.scrollToFirstLine(cursor.lineNumber())
        break;
    }
}

Option 2

Alternatively, there is the editor.ensureCursorVisible() command, but it usually sets the cursor (and editor view) near the bottom of the screen. So slightly less helpful than Option 1 above, in my opinion.

gif jump section again

%SCRIPT
commands = ["\\part", 
"\\chapter",
"\\section",
"\\subsection",
"\\subsubsection",
"\\paragraph"]
while (!cursor.atEnd()) {
    cursor.movePosition(1, cursorEnums.NextWord)
    if (cursor.nextChar() != '\\'.charCodeAt(0))
    continue;
    cursor.movePosition(1, cursorEnums.NextCharacter, cursorEnums.KeepAnchor);
    cursor.movePosition(1, cursorEnums.EndOfWord, cursorEnums.KeepAnchor);
    editor.ensureCursorVisible()
    if (commands.indexOf(cursor.selectedText()) >= 0) {
        cursor.setColumnNumber(cursor.anchorColumnNumber())
        break;
    }
}
  • Thanks! That works nicely. Do you have a pointer for me, where I can find more documentation on this functionality and TeXstudio scripting? Both the GitHub project as well as the TeXstudio user manual don't seem to have any documentation scrollToFirstLine or ensureCursorVisible. – chrisma Aug 4 '18 at 13:51
  • @chrisma They're not documented, unfortunately. Since scripting is an advanced functionality, I guess the devs don't have the time to write up docs for these commands. – Troy Aug 4 '18 at 14:03
  • That's a shame, scripting in TeXstudio seems like a really powerful feature. I've added your answer to the command in the GitHub wiki. I hope that's ok with you. – chrisma Aug 4 '18 at 15:12
  • @chrisma I agree. It's alright with me, thanks. – Troy Aug 4 '18 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.