How to detect if buffer is dirty

  • briddums

    briddums - 2014-07-09

    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?

  • cchris

    cchris - 2014-07-10

    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?


  • briddums

    briddums - 2014-07-12

    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!

  • DV

    DV - 2014-07-14

    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.

    • Don HO

      Don HO - 2014-07-26

      Hi DV,

      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):

      define NPPN_SNAPSHOTDIRTYFILELOADED (NPPN_FIRST + 18) // To notify plugins that a snapshot dirty file is loaded on startup

      //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?


      Last edit: Don HO 2014-07-26
  • DV

    DV - 2014-07-28

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

    Last edit: DV 2014-07-28
  • DV

    DV - 2014-07-28

    Actually, we already have such a message: NPPM_MAKECURRENTBUFFERDIRTY. Looks like it can be used here.


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks