Run command on file open
-
I tried searching for this but my search-foo is weak. Every time I open an XML file with NPP, I hit ctrl+alt+shift+b to ‘pretty print’ reformat it via the XML tools plugin. Is there any way to designate internal/plugin commands to be run automatically when a file is opened?
Perhaps this specific instance could also be achieved via a user style extension.
Thank you!
-
You can make this work if you install the python script plugin.
Once installed, goto Plugins->Python Script->New Script
and name the new script startup.py
It is important to name it like that because the python script plugin
checks if this file exists and in case it does, it executes it.
The content of the script should be like thisfrom Npp import notepad, NOTIFICATION, LANGTYPE def BufferActivated(args): if notepad.getLangType() == LANGTYPE.XML: notepad.runPluginCommand('XML Tools','Pretty print (XML only - with line breaks)') notepad.callback(BufferActivated, [NOTIFICATION.BUFFERACTIVATED])
This means, whenever a document gets activated, the BufferActivated function gets called
and checks if the current document has the language type xml set and if so, tries to run
the XML plugin function pretty print …One last thing, goto Plugins->Python Script->Configuration… and change the
Initialisation from LAZY to ATSTARTUPRestart npp - done
Eko
-
I’m far from expert on this, but isn’t your solution going to run every time a tab is changed by the user instead of just on starting up? Could this run into a lot of time for a big XML file? A big time wouldn’t be really noticable on npp startup, but everytime switching a tab it might be.
-
You are correct this runs when the buffer has been activated, always.
Means it might run even it isn’t necessary to run as it has been formatted already.There is one issue here, as far as I see/understood from pythonscript examples/documents/forum
and this is that you cannot modify a buffer as long
as it isn’t part of one of the two existing editor objects and the XML plugin can
modify the current buffer only anyways.There are workarounds here but without knowing what @quzar exactly wants to do
I can’t say for sure which one would fit best(?). For example we could add an property
to each document or using an internal list containing the bufferids of the
documents which have been already formatted or …Eko
-
Thank you, I had seen some other forum responses saying to use a python script for on-open execution of instructions, but they did not provide instructions.
The re-applying at every modification/tab switch might be a bit onerous. Perhaps a temporary variable to have this only happen once per buffer? After the initial pretty-fication, it shouldn’t have to be done again.
-
so something like this would do it once as long as the already_formatted property
does not get changed to empty string.from Npp import notepad, editor, NOTIFICATION, LANGTYPE def BufferActivated(args): if notepad.getLangType() == LANGTYPE.XML and editor.getProperty('already_formatted') == '': notepad.runPluginCommand('XML Tools','Pretty print (XML only - with line breaks)') editor.setProperty('already_formatted', '1') notepad.callback(BufferActivated, [NOTIFICATION.BUFFERACTIVATED])
Eko