diff --git a/aegisub/src/dialog_attachments.cpp b/aegisub/src/dialog_attachments.cpp index 2300f30ed..13bc4ff6f 100644 --- a/aegisub/src/dialog_attachments.cpp +++ b/aegisub/src/dialog_attachments.cpp @@ -129,6 +129,50 @@ void DialogAttachments::UpdateList() { /// @brief Destructor /// DialogAttachments::~DialogAttachments() { + + // Remove empty attachments sections from the file + + std::list::iterator cur = AssFile::top->Line.end(); + --cur; + + bool found_attachments = false; + bool removed_any = false; + wxString last_section_name; + + while (cur != AssFile::top->Line.begin()) { + if (!((*cur)->group == L"[Fonts]" || (*cur)->group == L"[Graphics]")) + break; + + if ((*cur)->GetEntryData() == L"[Fonts]" || (*cur)->GetEntryData() == L"[Graphics]") { + if (found_attachments) continue; + // found section heading with no attachments in, remove it + wxString delgroup = (*cur)->group; + std::list::iterator di = cur; + while (++di != AssFile::top->Line.end() && (*di)->group == delgroup) { + delete *di; + AssFile::top->Line.erase(di); + di = cur; + } + di = cur; + --cur; + delete *di; + AssFile::top->Line.erase(di); + removed_any = true; + continue; + } + + if (last_section_name != (*cur)->group) + found_attachments = false; + if (dynamic_cast(*cur) != 0) + found_attachments = true; + last_section_name = (*cur)->group; + + --cur; + } + + if (removed_any) { + AssFile::top->FlagAsModified(_("remove empty attachments sections")); + } }