Community
    • Login

    Python script cursor position

    Scheduled Pinned Locked Moved General Discussion
    6 Posts 4 Posters 1.5k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ollixx77O
      ollixx77
      last edited by

      How can I affect the cursor position after I started a Python script in NPP?
      In my current setup the cursor stays before the Python output of the script.

      Michael VincentM 1 Reply Last reply Reply Quote 0
      • Michael VincentM
        Michael Vincent @ollixx77
        last edited by

        @ollixx77 said in Python script cursor position:

        How can I affect the cursor position after I started a Python script in NPP?

        SCI_GOTOPOS.

        editor.gotoPos(POSITION)
        

        where POSITION is a number 0 <= POSITION <= size of document.

        Cheers.

        1 Reply Last reply Reply Quote 6
        • ollixx77O
          ollixx77
          last edited by

          Made it. Script runs and inserts a text in the document and sets the cursor after the inserted text. Had to be very cautious, because the commands are case sensitive.

          Got some info from here:
          http://npppythonscript.sourceforge.net/docs/latest/scintilla.html
          But had to change Editor.something() to editor.something()

          Here my example code:

          import datetime
          text = datetime.datetime.today().strftime("%d.%m.%Y - %H:%M:%S")
          editor.insertText(editor.getCurrentPos(), text)
          editor.gotoPos(editor.getCurrentPos()+len(text))
          
          Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @ollixx77
            last edited by Alan Kilborn

            @ollixx77 said in Python script cursor position:

            Had to be very cautious, because the commands are case sensitive.

            Yes, aren’t most programming languages this way?

            @ollixx77 said in Python script cursor position:

            But had to change Editor.something() to editor.something()

            Yes, Editor is the class, editor is the instantiation of a specific instance of the class. PythonScript creates two of these objects, editor1 (primary N++ editing view) and editor2 (secondary N++ editing view).

            editor is a convenience name for whichever view is currently active (PythonScript changes the value of editor behind the scenes as you move back and forth between the views). Of course, if you never work with more than the primary view, this can be ignored.

            1 Reply Last reply Reply Quote 3
            • PeterJonesP
              PeterJones @ollixx77
              last edited by PeterJones

              @ollixx77 said in Python script cursor position:

              Got some info from here:
              http://npppythonscript.sourceforge.net/docs/latest/scintilla.html

              Just so you know, that information on that site is regarding PythonScript 1.0.8. Python Script is now on version 1.5.4 (for Notepad++ v8.2.1 and earlier), 2.0.0 (for Notepad++ v8.3 and later), and 3.0.13-alpha (if you want to use Python 3 syntax and libraries instead of Python 2.7 syntax and libraries). The PythonScript plugin distributes up-to-date documentation which can be access from the Notepad++'s Plugins > Python Script > Context-Help menu entry, which will open up a local, up-to-date copy of the documentation in your default web browser.

              However, having you switch from looking at the old documentation to the bundled up-to-date docs won’t change the misunderstanding of the documentation that Alan has already addressed: the uppercase Class.methodName(args) (like the Editor.gotoPos(pos) entry in that documentation) prototype shown in the docs and the lowercase instance.methodName(args) (like the editor.gotoPos(editor.getCurrentPos()+len(text)) that you have in your code). The author of the plugin documentation assumed that you knew enough about Python language module documentation to know that it’s a common idiom to have the class name uppercase, and have the documentation describe methods with an uppercase classname, but have a generic instance that is lowercase, just like other variables usually are.

              edit: actually, it’s not even an assumption:

              The Editor class, with the instance name of Npp.editor (or normally simply editor) represents the Scintilla component of Notepad++. Scintilla is the “edit” component, the place where text is entered and altered. Scintilla is well documented, and Python Script allows you to call any of the Scintilla functions in a Pythonised way.
              Npp.editor (or editor) always refers to the active document.

              1 Reply Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn
                last edited by Alan Kilborn

                An additional tip – don’t know how many scripters know this – is to put your caret on an Editor or Notepad function and invoke the context-sensitive help that Peter mentioned.

                So, for example, if you have editor.gotoPos( in your code, and you put your caret somewhere in that text, and run the help, the documentation for that function will open in your default browser:

                d86e2026-3753-49cd-a643-003371c51cc9-image.png

                Invoking the context help can be done on the PythonScript plugin menu (see near the bottom):

                4b3859de-2bc8-499a-b7cd-f4f74d015cd5-image.png

                As shown, I like to assign the Ctrl+F1 shortcut to it; by design it doesn’t have a shortcut key.

                1 Reply Last reply Reply Quote 3
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors