From e43b81bd8ba3fa00561f5e97cbd47dcae8f2a701 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 3 Feb 2022 21:28:40 +0100
Subject: [PATCH] FIX: ScriptView: no more segv in vim edition mode

When the editor was in vim mode, a failure in a vim command (:w or :wq)
triggered std::terminate because a lambda that was declared as noexcept
invoked to save threw an std::runtime_error. Use the same try/catch
block from the MainWindow in the ScriptDocumentView when we use the fake
vim editor.
---
 src/UI/ScriptDocumentView.cc | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/UI/ScriptDocumentView.cc b/src/UI/ScriptDocumentView.cc
index 1f510c7c..a6d1732d 100644
--- a/src/UI/ScriptDocumentView.cc
+++ b/src/UI/ScriptDocumentView.cc
@@ -56,10 +56,20 @@ ScriptDocumentView::setUseFakeVimEditor(bool yes) noexcept
         connect(proxy, &EditorProxy::requestQuit, this, [this, mw]() noexcept -> void {
             mw->closeDocument(static_cast<AbstractDocumentView *>(this));
         });
-        connect(proxy, &EditorProxy::requestSave, this, [this]() { document->save(); });
+        connect(proxy, &EditorProxy::requestSave, this, [this]() noexcept -> void {
+            try {
+                document->save();
+            } catch (const std::runtime_error &e) {
+                logError() << "Failed to save current document: " << e.what();
+            }
+        });
         connect(proxy, &EditorProxy::requestSaveAndQuit, this, [this, mw]() noexcept -> void {
-            document->save();
-            mw->closeDocument(static_cast<AbstractDocumentView *>(this));
+            try {
+                document->save();
+                mw->closeDocument(static_cast<AbstractDocumentView *>(this));
+            } catch (const std::runtime_error &e) {
+                logError() << "Failed to save current document and close it: " << e.what();
+            }
         });
         initHandler(handler);
         clearUndoRedo(editor);
-- 
GitLab