<p>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:</p>
<pre><code>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);
</code></pre>
<p>If you add in these lines:</p>
<pre><code>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);
}
</code></pre>
<p>before the 'g_signal_emit_by_name' function call, you can print out the state of<br>
the document before plugin hooks are called to see what they see.  The final<br>
function will now look like this:</p>
<pre><code>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);
</code></pre>
<p>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:</p>
<pre><code>Document /path/to/changed_file.c has unsaved changes
</code></pre>
<p>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:</p>
<pre><code>Document /path/to/changed_file.c is up-to-date
</code></pre>
<p>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.</p>
<p><a target="_blank" href="https://user-images.githubusercontent.com/39241996/40156796-7fe932a8-5950-11e8-9a53-427112f06dd8.png"><img src="https://user-images.githubusercontent.com/39241996/40156796-7fe932a8-5950-11e8-9a53-427112f06dd8.png" alt="document-close" style="max-width:100%;"></a></p>
<p>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?</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/geany/geany/pull/1857#issuecomment-389742523">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABDrJ2Tg2mRbz5VnvI5nb7gv32CRCpvyks5tzP9tgaJpZM4T82nj">mute the thread</a>.<img src="https://github.com/notifications/beacon/ABDrJ3znl1z4RvJ8A35-PuBO-KyyPpGZks5tzP9tgaJpZM4T82nj.gif" height="1" width="1" alt="" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/geany/geany/pull/1857#issuecomment-389742523"></link>
  <meta itemprop="name" content="View Pull Request"></meta>
</div>
<meta itemprop="description" content="View this Pull Request on GitHub"></meta>
</div>

<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/geany/geany","title":"geany/geany","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/geany/geany"}},"updates":{"snippets":[{"icon":"PERSON","message":"@chrontec in #1857: 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:\r\n\r\n```\r\nstatic gboolean remove_page(guint page_num)\r\n{\r\n   GeanyDocument *doc = document_get_from_page(page_num);\r\n\r\n   g_return_val_if_fail(doc != NULL, FALSE);\r\n\r\n   if (doc-\u003echanged \u0026\u0026 ! dialogs_show_unsaved_file(doc))\r\n      return FALSE;\r\n\r\n   /* tell any plugins that the document is about to be closed */\r\n   g_signal_emit_by_name(geany_object, \"document-close\", doc);\r\n````\r\n\r\nIf you add in these lines:\r\n\r\n```\r\nif (doc-\u003echanged)\r\n{\r\n  printf(\"Document %s has unsaved changes\\n\", doc-\u003efile_name);\r\n}\r\nelse\r\n{\r\n  printf(\"Document %s is up-to-date\\n\", doc-\u003efile_name);\r\n}\r\n```\r\n\r\nbefore the 'g_signal_emit_by_name' function call, you can print out the state of\r\nthe document before plugin hooks are called to see what they see.  The final\r\nfunction will now look like this:\r\n\r\n```\r\nstatic gboolean remove_page(guint page_num)\r\n{\r\n   GeanyDocument *doc = document_get_from_page(page_num);\r\n\r\n   g_return_val_if_fail(doc != NULL, FALSE);\r\n\r\n   if (doc-\u003echanged \u0026\u0026 ! dialogs_show_unsaved_file(doc))\r\n      return FALSE;\r\n\r\n   if (doc-\u003echanged)\r\n   {\r\n      printf(\"Document %s has unsaved changes\\n\", doc-\u003efile_name);\r\n   }\r\n   else\r\n   {\r\n      printf(\"Document %s is up-to-date\\n\", doc-\u003efile_name);\r\n   }\r\n\r\n   /* tell any plugins that the document is about to be closed */\r\n   g_signal_emit_by_name(geany_object, \"document-close\", doc);\r\n```\r\n\r\nNow 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:\r\n\r\n```\r\nDocument /path/to/changed_file.c has unsaved changes\r\n```\r\n\r\nNow 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:\r\n\r\n```\r\nDocument /path/to/changed_file.c is up-to-date\r\n```\r\n\r\nTo 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.\r\n\r\n![document-close](https://user-images.githubusercontent.com/39241996/40156796-7fe932a8-5950-11e8-9a53-427112f06dd8.png)\r\n\r\nIf 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?"}],"action":{"name":"View Pull Request","url":"https://github.com/geany/geany/pull/1857#issuecomment-389742523"}}}</script>
<script type="application/ld+json">{"@type":"MessageCard","@context":"http://schema.org/extensions","hideOriginalBody":"false","originator":"37567f93-e2a7-4e2a-ad37-a9160fc62647","title":"Re: [geany/geany] Fix 'changed' flag being altered when all documents are closing. (#1857)","sections":[{"text":"","activityTitle":"**chrontec**","activityImage":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","activitySubtitle":"@chrontec","facts":[]}],"potentialAction":[{"name":"Add a comment","@type":"ActionCard","inputs":[{"isMultiLine":true,"@type":"TextInput","id":"IssueComment","isRequired":false}],"actions":[{"name":"Comment","@type":"HttpPOST","target":"https://api.github.com","body":"{\"commandName\":\"IssueComment\",\"repositoryFullName\":\"geany/geany\",\"issueId\":1857,\"IssueComment\":\"{{IssueComment.value}}\"}"}]},{"name":"Close pull request","@type":"HttpPOST","target":"https://api.github.com","body":"{\"commandName\":\"PullRequestClose\",\"repositoryFullName\":\"geany/geany\",\"pullRequestId\":1857}"},{"targets":[{"os":"default","uri":"https://github.com/geany/geany/pull/1857#issuecomment-389742523"}],"@type":"OpenUri","name":"View on GitHub"},{"name":"Unsubscribe","@type":"HttpPOST","target":"https://api.github.com","body":"{\"commandName\":\"MuteNotification\",\"threadId\":334719459}"}],"themeColor":"26292E"}</script>