PythonScript icon indicating copy to clipboard operation
PythonScript copied to clipboard

Problem detecting saved/unsaved file state at startup

Open alankilborn opened this issue 2 years ago • 5 comments
trafficstars

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:

image

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.

alankilborn avatar Dec 29 '22 11:12 alankilborn

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.

Ekopalypse avatar Dec 30 '22 14:12 Ekopalypse

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...

alankilborn avatar Dec 30 '22 14:12 alankilborn

Bump; I just encountered this same issue in a different new script I am working on. :-(

alankilborn avatar May 05 '24 12:05 alankilborn

@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 avatar May 15 '24 04:05 rdipardo

@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.

Ekopalypse avatar May 15 '24 06:05 Ekopalypse