PythonScript
PythonScript copied to clipboard
Problem detecting saved/unsaved file state at startup
I’m having trouble detecting the saved/unsaved state of the filetabs at Notepad++ startup using a script. Specifically, I’m being told that I have no unsaved tabs, when I do have some:

For the filetabs in yellow, the print() output (also in yellow) should say "unsaved" not "saved".
Here’s the code as text, from startup.py:
if 1:
import os
for (pathname, buffer_id, index, view) in notepad.getFiles():
notepad.activateFile(pathname)
state = 'unsaved' if editor.getModify() else 'saved'
print('on startup, activating a {st} "{fn}"'.format(st=state, fn=pathname.rsplit(os.sep, 1)[-1]))
Using PS 2.0 or 3.10.4.
This was originally discussed on the Community HERE.
In my opinion it is the task of Notepad++ and Scintilla to ensure, that PS can determine this accordingly. However, here are some hurdles that are probably difficult to overcome by Npp, such as the fact that if Npp is restarted but configured with backup and session, does not have an undo/redo list, which Scintilla would need to reset a modified buffer to a savepoint or to change it again from there. This would only work if Npp would not only save the last state itself, but all changes as needed by Scintilla, would be saved.
In my opinion it is the task of Notepad++ and Scintilla to ensure
I think so, too, but I wanted to make sure PS isn't doing anything wrong first. Just like @chcg to confirm...
Bump; I just encountered this same issue in a different new script I am working on. :-(
@Ekopalypse
Scintilla would need to reset a modified buffer to a savepoint or to change it again from there. This would only work if Npp would not only save the last state itself, but all changes as needed by Scintilla, would be saved.
FYI, Scintilla now has APIs for persisting undo state:
An application may save both the document and its save stack between sessions to enable the user to return to the same state the next time they edit. https://www.scintilla.org/ScintillaDoc.html#UndoSaveRestore
Usage examples are on the mailing list.
@rdipardo - you are right, haven't had a chance to play with these new APIs yet but it's definitely interesting. Thx for pointing out the example too.