Notepad++ v6.6.7. I want to detect if a buffer is dirty.
NPPN_READONLYCHANGED isn't getting sent to my plugin.
SCI_GETMODIFY will only tell me if the buffer has changed since Notepad++ was last opened. If user changes a file, exits Npp, starts Npp that file will still be dirty but this method will say it's not.
Is there any method to be able to tell if a buffer is dirty?
Your best bet, if you don't get the notification, is to check whether the window title ends with '*'.
But the fact that you don't receive a notification is a little weird. What is the exact use case?
I have a plugin that allows Notepad++ to be used as an IDE by our developers. I added code to syntax check, compile, and copy to training.
Before any of those events fire I want to save the file first, throwing an error message if the save failed. If the file isn't dirty then NPPM_SAVECURRENTFILE returns false.
Looking at the window title is perfect! I never even realized it added the *. Thanks!
Actually, it looks like incorrect logic caused by the auto-backup feature. I mean, if a file was modified and Notepad++ restarted (silently saving the changes in a backup file), SCI_GETMODIFY should return TRUE anyway, considering the original file is not actually saved yet.
I see your logic, and I agree with you.
The message SCI_GETMODIFY return non-zero if the content is modified. This message is answered by Scintilla, so I can do nothing about it. And even the answer could be modified by Notepad++, there would be the side effect, since plugins expect the the content is "really" modified.
In order to solve this incoherent situation in plugin, I have added a new notification NPPN_SNAPSHOTDIRTYFILELOADED (in v6.6.8):
//scnNotification->nmhdr.code = NPPN_SNAPSHOTDIRTYFILELOADED;
//scnNotification->nmhdr.hwndFrom = NULL;
//scnNotification->nmhdr.idFrom = BufferID;
So a plugin can be notified when a dirty file in the session is loaded on Notepad++ startup.
What do you think?
Actually I'd prefer a more simple way. Don't get me wrong - the proposed solution will work, but it provides additional complexity.
I'd rather fix it by "playing" with the text in Scintilla's buffer. For example, when we want a Scintilla's buffer to become "dirty", it could be achieved by: 1) disabling the Undo buffer so that it ignores the next change; 2) modify or add one character; 3) reverse or delete the newly changed or added character; 4) enabling the Undo buffer again. Such or similar approach would give us "truly" "dirty" buffer. (Just to have a better understanding of the proposed approach, let's imagine you've added a space character at the end of the current buffer and then deleted it. The buffer became "dirty", but the test is the same after these operations. Also, as the Undo buffer was disabled during these operations, they will not be present in the Undo buffer. So, from the Scintilla's buffer perspective, it just became "dirty" without any side effect.)
Actually, we already have such a message: NPPM_MAKECURRENTBUFFERDIRTY. Looks like it can be used here.
Sign up for the SourceForge newsletter:
You seem to have CSS turned off.
Please don't fill out this field.