The attached patch update the "listing" counter if the InsetInclude
inset contains lstinputlisting and has a caption. If also set
InsetInclude label to "Program listing X" when needed.

OK to commit?

The only problem I can see is that the counters are not updated
immediately after, for example, add/remove caption of such an inset.

Bo
Index: src/insets/InsetInclude.cpp
===================================================================
--- src/insets/InsetInclude.cpp	(revision 18476)
+++ src/insets/InsetInclude.cpp	(working copy)
@@ -107,7 +107,7 @@
 InsetInclude::InsetInclude(InsetCommandParams const & p)
 	: params_(p), include_label(uniqueID()),
 	  preview_(new RenderMonitoredPreview(this)),
-	  set_label_(false)
+	  set_label_(false), counter_(0)
 {
 	preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
 }
@@ -118,7 +118,7 @@
 	  params_(other.params_),
 	  include_label(other.include_label),
 	  preview_(new RenderMonitoredPreview(this)),
-	  set_label_(false)
+	  set_label_(false), counter_(0)
 {
 	preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
 }
@@ -336,9 +336,13 @@
 		case INCLUDE:
 			temp += buf.B_("Include");
 			break;
-		case LISTINGS:
-			temp += buf.B_("Program Listing");
+		case LISTINGS: {
+			if (counter_ > 0)
+				temp += buf.B_("Program Listing ") + convert<docstring>(counter_);
+			else
+				temp += buf.B_("Program Listing");
 			break;
+		}
 	}
 
 	temp += ": ";
@@ -882,6 +886,20 @@
 }
 
 
+void InsetInclude::updateCounter(Counters & counters)
+{
+	if (!isListings(params_))
+		return;
+
+	bool hasCaption = !InsetListingsParams(params_.getOptions()).getParamValue("caption").empty();
+	if (hasCaption) {
+		counters.step(from_ascii("listing"));
+		counter_ = counters.value(from_ascii("listing"));
+	} else
+		counter_ = 0;
+}
+
+
 string const InsetIncludeMailer::name_("include");
 
 InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
Index: src/insets/InsetInclude.h
===================================================================
--- src/insets/InsetInclude.h	(revision 18476)
+++ src/insets/InsetInclude.h	(working copy)
@@ -16,6 +16,7 @@
 #include "InsetCommandParams.h"
 #include "RenderButton.h"
 #include "MailInset.h"
+#include "Counters.h"
 
 #include "support/FileName.h"
 
@@ -99,6 +100,9 @@
 	void updateLabels(Buffer const & buffer) const;
 	///
 	bool getStatus(Cursor &, FuncRequest const &, FuncStatus &) const;
+	/// if this inset contains lstinputlisting and has a caption, 
+	/// update internal counter and passed counter
+	void updateCounter(Counters & counters);
 protected:
 	InsetInclude(InsetInclude const &);
 	///
@@ -133,6 +137,7 @@
 	/// cache
 	mutable bool set_label_;
 	mutable RenderButton button_;
+	int counter_;
 };
 
 
Index: src/buffer_funcs.cpp
===================================================================
--- src/buffer_funcs.cpp	(revision 18476)
+++ src/buffer_funcs.cpp	(working copy)
@@ -419,6 +419,10 @@
 		}
 		else if (inset.lyxCode() == Inset::LISTINGS_CODE)
 			setCaptionLabels(inset, "listing", from_ascii("Listing"), counters);
+		else if (inset.lyxCode() == Inset::INCLUDE_CODE)
+			// if this include inset contains lstinputlisting, and has a caption
+			// it will increase the 'listing' counter by one
+			static_cast<InsetInclude &>(inset).updateCounter(counters);
 	}
 }
 

Reply via email to