How to get text and/or scintilla handle for background file?

  • I fear I may have a pretty difficult problem.

    I’m writing a plugin and I need to get the text of all files when notepad++ open and track their changes. The problem is that when a file is opened it is not the “current Scintilla” when the NPPN_FILEOPENED notification is sent (unless it was the first file opened when you start NPP). Replace across documents also causes problems, background files are obviously not the current Scintilla (in fact, calling NPPM_GETCURRENTSCINTILLA during a multi-document replace seems to return -1 for all documents, even the one currently in view).

    I’ve done a lot of digging around the Scintilla documentation and the NPP source code. I see that Scintilla has a notion of a Document, if I had one of these I could create a new Scintilla (using NPPM_CREATESCINTILLAHANDLE) referencing the background document use that to get the text. NPP does this in some of it’s functions that manipulate files in the background, including the function to load a file. NPP itself has a notion of a Buffer, which contains a reference to the backing Document. During the NPPN_FILEOPENED I can get the Buffer ID for the background document (I also use this to get the filename), but I don’t see anyway to go from a Buffer ID to a Document from the context of a plugin.

    In short, what I need is a way to get the text, or preferably a Scintilla handle, for a background file. Does anyone know how to tackle this problem?

  • @Derek-Brown

    If I understand correctly then you are looking for something like this.

  • It looks like that only gets the filenames? Unless I am missing something. I want to get the actual data in the files, preferably in the form of a Scintilla handle.

  • @Derek-Brown

    It does a bit more, but yes, you do not get the contents of the file.
    But if you have the file name, you can read the content, and once the buffer is activated,
    which is indicated by the buffer ID, you can compare the current text with the text you read initially.
    Or am I missing something?

  • I’m not sure I follow you. Are you suggesting to use the filename to read the file directly (using Windows APIs or something)? That won’t always work: If session backups are enabled you can have an unsaved new file backed up. When you restart NPP it’s filename will be something like “new 1”, not an actual file on disk (I’m sure the backup file exists somewhere, but the filename does not point you to it). Likewise, if an existing file is unsaved then the contents that are opened when you restart NPP will be different from the contents on disk at the given filename.

    And sure you could compare what you loaded from disk with the contents of the buffer when it becomes active, but then I may as well just wait until the buffer is active to read anything at all. And I could write my plugin this way, but it would be much better and simpler if I could track the state of all buffers whether they were active or not.

  • @Derek-Brown

    Afaik, there is no direct way to read the loaded buffers.
    Sorry for the confusion, but I thought this was clear and you are looking for an alternative.

  • I’m hoping that someone might know a clever workaround or hack, or that I might have missed something.

Log in to reply