[PATCH] Adding LFUN for removing LyX modules

Yuriy Skalko yuriy.skalko at gmail.com
Fri Oct 22 21:27:29 UTC 2021


> Note that it is best to move the test for removing the module in getStatus, so that the function is disabled. With that, it would become possible in the frontend to use that code. I do not like to have duplicated (and possibly different) logic in those places. The solution might be to have a temporary module list in GuiDocument and call the methods of this this instance to handle the dialogs (the code uses the widgets as data structure now).
> 
> 
> JMarc

Thanks for the suggestions, Jean-Marc. Moved the check there. Probably 
it is worth to do the same for layoutModuleCanBeAdded, right? As I see 
now, the check for removing modules in frontend (updateDelPB) is more 
complex. Is that additional check really necessary? With current LyX 
built-in modules situation handled there is impossible.


> Cosmetics, but please add * \li Origin here... Pavel

Ok, added.


Yuriy

-------------- next part --------------
From 8cc029d13a0a6f44e87ccc71f7a3bc33f7d438ff Mon Sep 17 00:00:00 2001
From: Yuriy Skalko <yuriy.skalko at gmail.com>
Date: Fri, 22 Oct 2021 23:59:58 +0300
Subject: [PATCH] Add LFUN for removing LyX modules

---
 src/BufferParams.cpp   |  6 ++++++
 src/BufferParams.h     |  2 ++
 src/BufferView.cpp     | 28 +++++++++++++++++++++++++++-
 src/FuncCode.h         |  1 +
 src/LayoutModuleList.h |  2 ++
 src/LyXAction.cpp      | 11 +++++++++++
 6 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index bbbce16bef..a14e3211f9 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -2608,6 +2608,12 @@ bool BufferParams::addLayoutModule(string const & modName)
 }
 
 
+void BufferParams::removeLayoutModule(string const & modName)
+{
+	layout_modules_.remove(modName);
+}
+
+
 string BufferParams::bufferFormat() const
 {
 	return documentClass().outputFormat();
diff --git a/src/BufferParams.h b/src/BufferParams.h
index 52d8219cb4..3a40be60a0 100644
--- a/src/BufferParams.h
+++ b/src/BufferParams.h
@@ -175,6 +175,8 @@ public:
 	/// if you want to check for compatibility.
 	/// \return true if module was successfully added.
 	bool addLayoutModule(std::string const & modName);
+	///
+	void removeLayoutModule(std::string const & modName);
 	/// checks to make sure module's requriements are satisfied, that it does
 	/// not conflict with already-present modules, isn't already loaded, etc.
 	bool layoutModuleCanBeAdded(std::string const & modName) const;
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index ac9a8235db..d9337eff52 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -36,6 +36,7 @@
 #include "lyxfind.h"
 #include "LyXRC.h"
 #include "MetricsInfo.h"
+#include "ModuleList.h"
 #include "Paragraph.h"
 #include "Session.h"
 #include "Text.h"
@@ -1161,7 +1162,20 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
 	case LFUN_TEXTCLASS_LOAD:
 		flag.setEnabled(!buffer_.isReadonly());
 		break;
-
+	case LFUN_LAYOUT_MODULE_REMOVE: {
+		string const modName = to_ascii(cmd.argument());
+		bool hasDependentModules = false;
+		LayoutModuleList const mods = buffer().params().getModules();
+		for (string const & mod : mods) {
+			vector<string> const reqs = theModuleList[mod]->getRequiredModules();
+			if (find(reqs.begin(), reqs.end(), modName) != reqs.end()) {
+				hasDependentModules = true;
+				break;
+			}
+		}
+		flag.setEnabled(!buffer_.isReadonly() && !hasDependentModules);
+		break;
+	}
 	case LFUN_UNDO:
 		// We do not use the LyXAction flag for readonly because Undo sets the
 		// buffer clean/dirty status by itself.
@@ -1400,6 +1414,18 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 		break;
 	}
 
+	case LFUN_LAYOUT_MODULE_REMOVE: {
+		// FIXME: this modifies the document in cap::switchBetweenClasses
+		//  without calling recordUndo. Fix this before using
+		//  recordUndoBufferParams().
+		cur.recordUndoFullBuffer();
+		buffer_.params().removeLayoutModule(argument);
+		makeDocumentClass();
+		dr.screenUpdate(Update::Force);
+		dr.forceBufferUpdate();
+		break;
+	}
+
 	case LFUN_TEXTCLASS_APPLY: {
 		// since this shortcircuits, the second call is made only if
 		// the first fails
diff --git a/src/FuncCode.h b/src/FuncCode.h
index 20231eb164..2a3221641d 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -502,6 +502,7 @@ enum FuncCode
 	LFUN_SPELLING_REMOVE_LOCAL,     // jspitzm 20210307
 	LFUN_FINISHED_DOWN,             // lasgouttes 20210629
 	LFUN_FINISHED_UP,               // lasgouttes 20210629
+	LFUN_LAYOUT_MODULE_REMOVE,      // yuriy 20211021
 	LFUN_LASTACTION                 // end of the table
 };
 
diff --git a/src/LayoutModuleList.h b/src/LayoutModuleList.h
index 868c1cf945..1c65ae8564 100644
--- a/src/LayoutModuleList.h
+++ b/src/LayoutModuleList.h
@@ -51,6 +51,8 @@ public:
 	///
 	void push_back(std::string const & str) { lml_.push_back(str); }
 	///
+	void remove(std::string const & str) { lml_.remove(str); }
+	///
 	size_t size() const { return lml_.size(); }
 	/// This is needed in GuiDocument. It seems better than an
 	/// implicit conversion.
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index 254060e789..17c4aa2441 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -2477,6 +2477,17 @@ void LyXAction::init()
  */
 		{ LFUN_LAYOUT_MODULE_ADD, "layout-module-add", NoInternal, Layout },
 
+/*!
+ * \var lyx::FuncCode lyx::LFUN_LAYOUT_MODULE_REMOVE
+ * \li Action: Removes a module.
+ * \li Notion: Removes a module from the list of included modules for the current buffer.
+ * \li Syntax: layout-module-remove <MODULE>
+ * \li Params: <MODULE>: the module to be removed
+ * \li Origin: yuriy, 21 October 2021
+ * \endvar
+ */
+		{ LFUN_LAYOUT_MODULE_REMOVE, "layout-module-remove", NoInternal, Layout },
+
 /*!
  * \var lyx::FuncCode lyx::LFUN_LAYOUT_PARAGRAPH
  * \li Action: Launches the paragraph settings dialog.
-- 
2.28.0.windows.1



More information about the lyx-devel mailing list