diff --git a/PlotDirector.NativeCompanion/Vsto/WordAdapter.cs b/PlotDirector.NativeCompanion/Vsto/WordAdapter.cs index 77e382f3..dc7debdb 100644 --- a/PlotDirector.NativeCompanion/Vsto/WordAdapter.cs +++ b/PlotDirector.NativeCompanion/Vsto/WordAdapter.cs @@ -303,18 +303,31 @@ namespace PlotDirector.WordAddIn localResult(summary); } } - // Word marks the document dirty when our own anchor mutation succeeds. - // Consume only a clean-to-dirty transition within this STA operation. - // A pre-existing dirty state, document length change or save still follows normal revision checks. + // Unwrapping a control can change Word's internal positions even with identical prose. + // Read the range and saved state on either side of our own STA mutation, then + // leave the usual revision check in place for edits between scheduler turns. void RemoveAnchor(Action remove) { - var wasClean=legacy&&doc.WasSaved&&Com(()=>doc.Document.Saved); + if(legacy&&(DocumentEnd(doc.Document)!=doc.End|| + (doc.WasSaved&&!Com(()=>doc.Document.Saved)))) + throw new InvalidOperationException("Document changed before the marker removal."); remove(); removed++; - if(legacy) + if(!legacy)return; + + expectedCount=count-1; + doc.End=DocumentEnd(doc.Document); + if(Com(()=>doc.Document.Saved))Com(()=>{doc.Document.Saved=false;return true;}); + doc.WasSaved=false; + if(removed==1) { - expectedCount=count-1; - if(wasClean&&!Com(()=>doc.Document.Saved))doc.WasSaved=false; + doc.Revision++; + command.Revision=doc.Revision; + doc.NeedsSnapshot=true; + doc.WordCount=null; + doc.ChapterTitle=""; + doc.SceneTitle=""; + RegisterState(doc); } }