[LyX/master] New helper lfun ifrelatives

Juergen Spitzmueller spitz at lyx.org
Thu Jan 2 12:54:16 UTC 2020


commit dc41c1f6dfe328932dfddaaa57ba4cd34b0d7380
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Thu Jan 2 14:09:24 2020 +0100

    New helper lfun ifrelatives
    
    This executes a command only if a buffer has either a master or children
    
    Helps to disable master-related items in the UI
---
 lib/RELEASE-NOTES                   |    2 ++
 src/FuncCode.h                      |    1 +
 src/LyXAction.cpp                   |   13 +++++++++++++
 src/frontends/qt/GuiApplication.cpp |   26 ++++++++++++++++++++++++++
 4 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/lib/RELEASE-NOTES b/lib/RELEASE-NOTES
index 9da7d8c..30e2948 100644
--- a/lib/RELEASE-NOTES
+++ b/lib/RELEASE-NOTES
@@ -69,6 +69,8 @@
 
 * export-cancel: Used to cancel background export processes.
 
+* ifrelatives is a helper function to check whether a buffer has a master or children.
+
 * master-buffer-forall executes an lfun in the master and all children of a document.
 
 * paragraph-select is a new convenience function to select the paragraph
diff --git a/src/FuncCode.h b/src/FuncCode.h
index 10d65b3..762bc03 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -489,6 +489,7 @@ enum FuncCode
 	// 380
 	LFUN_BUFFER_RESET_EXPORT,       // spitz 20191226
 	LFUN_MASTER_BUFFER_FORALL,      // spitz 20191231
+	LFUN_IF_RELATIVES,              // spitz 20200102
 	LFUN_LASTACTION                 // end of the table
 };
 
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index aa57112..a9789ad 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -1923,6 +1923,19 @@ void LyXAction::init()
  */
 		{ LFUN_ICON_SIZE, "icon-size", NoBuffer, Buffer },
 
+
+/*!
+ * \var lyx::FuncCode lyx::LFUN_IF_RELATIVES
+ * \li Action: Helper function for master-related actions
+ * \li Notion: In a buffer, the function will be disabled if the buffer
+ *             has no master or children. It thus allows to enable
+ *             actions only in documents with master/children
+ * \li Syntax: ifrelatives <ACTION>
+ * \li Origin: spitz, 2 January 2020
+ * \endvar
+ */
+		{ LFUN_IF_RELATIVES, "ifrelatives", Noop, Buffer },
+
 /*!
  * \var lyx::FuncCode lyx::LFUN_INDEX_INSERT
  * \li Action: Inserts Index entry.
diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index e67a476..9c15d7a 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -1310,6 +1310,20 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
 		break;
 	}
 
+	case LFUN_IF_RELATIVES: {
+		string const lfun = to_utf8(cmd.argument());
+		BufferView const * bv =
+			current_view_ ? current_view_->currentBufferView() : nullptr;
+		if (!bv || (bv->buffer().parent() == nullptr && !bv->buffer().hasChildren())) {
+			enable = false;
+			break;
+		}
+		FuncRequest func(lyxaction.lookupFunc(lfun));
+		func.setOrigin(cmd.origin());
+		flag = getStatus(func);
+		break;
+	}
+
 	case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
 	case LFUN_REPEAT:
 	case LFUN_PREFERENCES_SAVE:
@@ -2051,6 +2065,18 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 		break;
 	}
 
+	case LFUN_IF_RELATIVES: {
+		string const lfun = to_utf8(cmd.argument());
+		FuncRequest func(lyxaction.lookupFunc(lfun));
+		func.setOrigin(cmd.origin());
+		FuncStatus const stat = getStatus(func);
+		if (stat.enabled()) {
+			dispatch(func);
+			break;
+		}
+		break;
+	}
+
 	case LFUN_PREFERENCES_SAVE:
 		lyxrc.write(support::makeAbsPath("preferences",
 			package().user_support().absFileName()), false);


More information about the lyx-cvs mailing list