Python script cursor position
-
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. -
@ollixx77 said in Python script cursor position:
How can I affect the cursor position after I started a Python script in NPP?
editor.gotoPos(POSITION)
where POSITION is a number 0 <= POSITION <= size of document.
Cheers.
-
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))
-
@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) andeditor2
(secondary N++ editing view).editor
is a convenience name for whichever view is currently active (PythonScript changes the value ofeditor
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. -
@ollixx77 said in Python script cursor position:
Got some info from here:
http://npppythonscript.sourceforge.net/docs/latest/scintilla.htmlJust 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 theEditor.gotoPos(pos)
entry in that documentation) prototype shown in the docs and the lowercaseinstance.methodName(args)
(like theeditor.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 simplyeditor
) 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
(oreditor
) always refers to the active document. -
An additional tip – don’t know how many scripters know this – is to put your caret on an
Editor
orNotepad
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:Invoking the context help can be done on the PythonScript plugin menu (see near the bottom):
As shown, I like to assign the Ctrl+F1 shortcut to it; by design it doesn’t have a shortcut key.