An even more updated patch, thanks to Pavel's comments.

To test it, put branchTest.lyx into /tmp and run:
/cvs/lyxsvn/src/lyx -userdir /tmp/test -x "branch-activate test" -e pdf2 /tmp/branchTest.lyx /cvs/lyxsvn/src/lyx -userdir /tmp/test -x "branch-deactivate tester" -e pdf2 /tmp/branchTest.lyx and look at the pdf files produced. You should see the branches turning on and off.

Here's why I'm unhappy with the patch. It's simple in a way: We have a vector of commands to execute rather than just one---that was the first problem. So now you could do something like: lyx -x "file-open /tmp/branchTest.lyx" -x "buffer-end" -x "break-paragraph" -x "self-insert This is so cool!"
with the expected results.

BUT this will not work when we are exporting. The reason is that when you use the export feature, LyX calls Bufer::dispatch() rather than (say) lyx::dispatch(), and the only thing that previously got handled in Buffer::dispatch() is LFUN_BUFFER_EXPORT. So to get branch activation and de-activation to work when we're exporting, I had to put those in Buffer::dispatch()---and then call them from Text::dispatch() so they'll work normally. This does not look pretty---though maybe there are so few things you'd actually want to do when exporting that it's OK. I don't know. But maybe you might want to alter some of the other buffer params (say, line spacing) before you export.

Still, I can imagine other uses for a gui-less LyX. E.g.:
  lyx --no-gui -x "buffer-import myfile.rtf" -x "buffer-write myfile.lyx"
Not that one could do that now. But a general solution would allow that sort of thing.

Comments welcome, of course.

rh

rh

Index: src/FuncCode.h
===================================================================
--- src/FuncCode.h	(revision 24968)
+++ src/FuncCode.h	(working copy)
@@ -410,6 +410,8 @@
 	// 315
 	LFUN_GRAPHICS_GROUPS_UNIFY,
 	LFUN_SET_GRAPHICS_GROUP,
+	LFUN_BRANCH_ACTIVATE,
+	LFUN_BRANCH_DEACTIVATE,
 	LFUN_LASTACTION                  // end of the table
 };
 
Index: src/LyXAction.cpp
===================================================================
--- src/LyXAction.cpp	(revision 24968)
+++ src/LyXAction.cpp	(working copy)
@@ -2168,6 +2168,24 @@
  * \endvar
  */
 		{ LFUN_COMPLETION_COMPLETE, "complete", SingleParUpdate, Edit },
+/*!
+ * \var lyx::FuncCode lyx::LFUN_BRANCH_ACTIVATE
+ * \li Action: Activate the branch
+ * \li Syntax: branch-activate <BRANCH>
+ * \li Params: <BRANCH>: The branch to activate
+ * \li Origin: rgh, 27 May 2008
+ * \endvar
+ */
+		{ LFUN_BRANCH_ACTIVATE, "branch-activate", Argument, Buffer },
+/*!
+ * \var lyx::FuncCode lyx::LFUN_BRANCH_ACTIVATE
+ * \li Action: De-activate the branch
+ * \li Syntax: branch-deactivate <BRANCH>
+ * \li Params: <BRANCH>: The branch to deactivate
+ * \li Origin: rgh, 27 May 2008
+ * \endvar
+ */
+		{ LFUN_BRANCH_DEACTIVATE, "branch-deactivate", Argument, Buffer },
 
 		{ LFUN_NOACTION, "", Noop, Hidden }
 #ifndef DOXYGEN_SHOULD_SKIP_THIS
Index: src/Buffer.cpp
===================================================================
--- src/Buffer.cpp	(revision 24968)
+++ src/Buffer.cpp	(working copy)
@@ -1428,6 +1428,19 @@
 			break;
 		}
 
+		case LFUN_BRANCH_ACTIVATE:
+		case LFUN_BRANCH_DEACTIVATE: {
+			BranchList & branchList = params().branchlist();
+			docstring const branchName = func.argument();
+			Branch * branch = branchList.find(branchName);
+			if (!branch)
+				LYXERR0("Branch " << branchName << " does not exist.");
+			else 
+				branch->setSelected(func.action == LFUN_BRANCH_ACTIVATE);
+			if (result)
+				*result = true;
+		}
+
 		default:
 			dispatched = false;
 	}
Index: src/LyXFunc.cpp
===================================================================
--- src/LyXFunc.cpp	(revision 24968)
+++ src/LyXFunc.cpp	(working copy)
@@ -547,6 +547,18 @@
 		break;
 	}
 
+	case LFUN_BRANCH_ACTIVATE: 
+	case LFUN_BRANCH_DEACTIVATE: {
+		docstring const branchName = cmd.argument();
+		if (branchName.empty())
+			enable = false;
+		else {
+			BranchList const & branchList = buf->params().branchlist();
+			enable = branchList.find(branchName);
+		}
+		break;
+	}
+
 	case LFUN_WORD_FIND_FORWARD:
 	case LFUN_WORD_FIND_BACKWARD:
 	case LFUN_COMMAND_PREFIX:
@@ -1588,6 +1600,17 @@
 			LyX::ref().session().bookmarks().clear();
 			break;
 
+		case LFUN_BRANCH_ACTIVATE:
+		case LFUN_BRANCH_DEACTIVATE: {
+			LASSERT(lyx_view_, /**/);
+			Buffer * buffer = lyx_view_->buffer();
+			if (buffer) {
+				buffer->dispatch(cmd);
+				updateFlags = Update::Force;
+			}
+			break;
+		}
+
 		default:
 			LASSERT(theApp(), /**/);
 			// Let the frontend dispatch its own actions.
Index: src/LyX.cpp
===================================================================
--- src/LyX.cpp	(revision 24968)
+++ src/LyX.cpp	(working copy)
@@ -163,7 +163,7 @@
 	/// has this user started lyx for the first time?
 	bool first_start;
 	/// the parsed command line batch command if any
-	string batch_command;
+	vector<string> batch_commands;
 };
 
 ///
@@ -382,7 +382,7 @@
 
 		loadFiles();
 
-		if (pimpl_->batch_command.empty() || pimpl_->buffer_list_.empty()) {
+		if (pimpl_->batch_commands.empty() || pimpl_->buffer_list_.empty()) {
 			prepareExit();
 			return EXIT_SUCCESS;
 		}
@@ -395,8 +395,12 @@
 			if (buf != buf->masterBuffer())
 				continue;
 			bool success = false;
-			buf->dispatch(pimpl_->batch_command, &success);
-			final_success |= success;
+			vector<string>::const_iterator bcit  = pimpl_->batch_commands.begin();
+			vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
+			for (; bcit != bcend; bcit++) {
+				buf->dispatch(*bcit, &success);
+				final_success |= success;
+			}
 		}
 		prepareExit();
 		return !final_success;
@@ -565,15 +569,15 @@
 
 	// if reconfiguration is needed.
 	while (LayoutFileList::get().empty()) {
-	    switch (Alert::prompt(
-		    _("No textclass is found"),
-		    _("LyX cannot continue because no textclass is found. "
-		      "You can either reconfigure normally, or reconfigure using "
-		      "default textclasses, or quit LyX."),
-		    0, 2,
-		    _("&Reconfigure"),
-		    _("&Use Default"),
-		    _("&Exit LyX")))
+		switch (Alert::prompt(
+			_("No textclass is found"),
+			_("LyX cannot continue because no textclass is found. "
+				"You can either reconfigure normally, or reconfigure using "
+				"default textclasses, or quit LyX."),
+			0, 2,
+			_("&Reconfigure"),
+			_("&Use Default"),
+			_("&Exit LyX")))
 		{
 		case 0:
 			// regular reconfigure
@@ -607,12 +611,15 @@
 		pimpl_->application_->restoreGuiSession();
 
 	// Execute batch commands if available
-	if (pimpl_->batch_command.empty())
+	if (pimpl_->batch_commands.empty())
 		return;
 
-	LYXERR(Debug::INIT, "About to handle -x '" << pimpl_->batch_command << '\'');
-
-	pimpl_->lyxfunc_.dispatch(lyxaction.lookupFunc(pimpl_->batch_command));
+	vector<string>::const_iterator bcit  = pimpl_->batch_commands.begin();
+	vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
+	for (; bcit != bcend; bcit++) {
+		LYXERR(Debug::INIT, "About to handle -x '" << *bcit << '\'');
+		pimpl_->lyxfunc_.dispatch(lyxaction.lookupFunc(*bcit));
+	}
 }
 
 
@@ -1006,12 +1013,10 @@
 
 namespace {
 
-string batch;
-
 /// return the the number of arguments consumed
-typedef boost::function<int(string const &, string const &)> cmd_helper;
+typedef boost::function<int(string const &, string const &, string &)> cmd_helper;
 
-int parse_dbg(string const & arg, string const &)
+int parse_dbg(string const & arg, string const &, string &)
 {
 	if (arg.empty()) {
 		lyxerr << to_utf8(_("List of supported debug flags:")) << endl;
@@ -1026,7 +1031,7 @@
 }
 
 
-int parse_help(string const &, string const &)
+int parse_help(string const &, string const &, string &)
 {
 	lyxerr <<
 		to_utf8(_("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
@@ -1054,7 +1059,7 @@
 }
 
 
-int parse_version(string const &, string const &)
+int parse_version(string const &, string const &, string &)
 {
 	lyxerr << "LyX " << lyx_version
 	       << " (" << lyx_release_date << ")" << endl;
@@ -1066,7 +1071,7 @@
 }
 
 
-int parse_sysdir(string const & arg, string const &)
+int parse_sysdir(string const & arg, string const &, string &)
 {
 	if (arg.empty()) {
 		Alert::error(_("No system directory"),
@@ -1078,7 +1083,7 @@
 }
 
 
-int parse_userdir(string const & arg, string const &)
+int parse_userdir(string const & arg, string const &, string &)
 {
 	if (arg.empty()) {
 		Alert::error(_("No user directory"),
@@ -1090,7 +1095,7 @@
 }
 
 
-int parse_execute(string const & arg, string const &)
+int parse_execute(string const & arg, string const &, string & batch)
 {
 	if (arg.empty()) {
 		Alert::error(_("Incomplete command"),
@@ -1102,7 +1107,7 @@
 }
 
 
-int parse_export(string const & type, string const &)
+int parse_export(string const & type, string const &, string & batch)
 {
 	if (type.empty()) {
 		lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
@@ -1115,7 +1120,7 @@
 }
 
 
-int parse_import(string const & type, string const & file)
+int parse_import(string const & type, string const & file, string & batch)
 {
 	if (type.empty()) {
 		lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
@@ -1132,7 +1137,7 @@
 }
 
 
-int parse_geometry(string const & arg1, string const &)
+int parse_geometry(string const & arg1, string const &, string &)
 {
 	geometryArg = arg1;
 #if defined(_WIN32) || (defined(__CYGWIN__) && defined(X_DISPLAY_MISSING))
@@ -1180,7 +1185,10 @@
 		string const arg2 =
 			(i + 2 < argc) ? to_utf8(from_local8bit(argv[i + 2])) : string();
 
-		int const remove = 1 + it->second(arg, arg2);
+		string batch;
+		int const remove = 1 + it->second(arg, arg2, batch);
+		if (!batch.empty())
+			pimpl_->batch_commands.push_back(batch);
 
 		// Now, remove used arguments by shifting
 		// the following ones remove places down.
@@ -1191,8 +1199,6 @@
 			--i;
 		}
 	}
-
-	pimpl_->batch_command = batch;
 }
 
 

Reply via email to