The behavior I'm seeing is fairly straight forward to observe with the current 
state of the repository. In the 'document.c' file there is a function 
'remove_page' which is the last function called to finish closing a document 
and remove references to it from Geany.  This function is also where the 
'document-close' signal is emitted by Geany, and the signal is sent to plugins 
really early on in the function.  The code for this function up to the 
'document-close' signal being sent looks like this:

```
static gboolean remove_page(guint page_num)
{
   GeanyDocument *doc = document_get_from_page(page_num);

   g_return_val_if_fail(doc != NULL, FALSE);

   if (doc->changed && ! dialogs_show_unsaved_file(doc))
      return FALSE;

   /* tell any plugins that the document is about to be closed */
   g_signal_emit_by_name(geany_object, "document-close", doc);
````

If you add in these lines:

```
if (doc->changed)
{
  printf("Document %s has unsaved changes\n", doc->file_name);
}
else
{
  printf("Document %s is up-to-date\n", doc->file_name);
}
```

before the 'g_signal_emit_by_name' function call, you can print out the state of
the document before plugin hooks are called to see what they see.  The final
function will now look like this:

```
static gboolean remove_page(guint page_num)
{
   GeanyDocument *doc = document_get_from_page(page_num);

   g_return_val_if_fail(doc != NULL, FALSE);

   if (doc->changed && ! dialogs_show_unsaved_file(doc))
      return FALSE;

   if (doc->changed)
   {
      printf("Document %s has unsaved changes\n", doc->file_name);
   }
   else
   {
      printf("Document %s is up-to-date\n", doc->file_name);
   }

   /* tell any plugins that the document is about to be closed */
   g_signal_emit_by_name(geany_object, "document-close", doc);
```

Now just recompile Geany. Open Geany from the command line, then open a 
document. Make some changes to the document, close only that document using the 
UI tab and click "Don't Save" when prompted. The message you should see printed 
in the terminal is this:

```
Document /path/to/changed_file.c has unsaved changes
```

Now open that same document, make the same changes, and this time close Geany 
instead. Click "Don't Save" when prompted. The message you should see printed 
in the terminal is this:

```
Document /path/to/changed_file.c is up-to-date
```

To me this behavior looks like a bug.  The documentation for the GeanyDocument 
struct states that the 'changed' flag shows if the document has been changed 
since it was last saved, but in this case that is not what plugins will see. 
Here is a screen shot of the behavior.

![document-close](https://user-images.githubusercontent.com/39241996/40156796-7fe932a8-5950-11e8-9a53-427112f06dd8.png)

If this behavior isn't considered a bug, maybe the API documentation for 
document close can be updated to warn plugin developers of this behavior so 
they aren't tripped up by this when coding?  If we can't use the 'changed' flag 
to check if there are unsaved changes, is there something else that I'm missing 
that can be checked, short of extracting the document contents and comparing 
them to the file on disk, to figure this out?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/pull/1857#issuecomment-389742523

Reply via email to