OK, one last try at a refstyle patch. It introduces a document option to use refstyle or prettyref, the former being the default for new documents, the latter staying in place for old ones, though of course users can change this.

This now works well enough for me, though there may be some other cases prettyref handles out of the box that refstyle does not. Theorem is one, and I've added a simple \newref definition for this to LaTeXFeatures, though perhaps it would be nice if we did some translation for it, as well. I'm not really sure how to do that, though, so if anyone wants to tell me....

Comments welcome.

Richard

Index: src/Buffer.cpp
===================================================================
--- src/Buffer.cpp	(revision 35615)
+++ src/Buffer.cpp	(working copy)
@@ -127,7 +127,7 @@
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-int const LYX_FORMAT = 403; // rgh: Dummy format for InsetFlex name conversion
+int const LYX_FORMAT = 404; // rgh: refstyle
 
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
Index: src/BufferParams.cpp
===================================================================
--- src/BufferParams.cpp	(revision 35615)
+++ src/BufferParams.cpp	(working copy)
@@ -415,6 +415,7 @@
 	html_math_img_scale = 1.0;
 
 	output_sync = false;
+	use_refstyle = true;
 }
 
 
@@ -853,6 +854,8 @@
 		lex >> output_sync;
 	} else if (token == "\\output_sync_macro") {
 		lex >> output_sync_macro;
+	} else if (token == "\\use_refstyle") {
+		lex >> use_refstyle;
 	} else {
 		lyxerr << "BufferParams::readToken(): Unknown token: " << 
 			token << endl;
@@ -980,6 +983,7 @@
 	   << "\n\\use_indices " << convert<string>(use_indices)
 	   << "\n\\paperorientation " << string_orientation[orientation]
 	   << "\n\\suppress_date " << convert<string>(suppress_date)
+		 << "\n\\use_refstyle " << use_refstyle
 	   << '\n';
 	if (isbackgroundcolor == true)
 		os << "\\backgroundcolor " << lyx::X11hexname(backgroundcolor) << '\n';
Index: src/BufferParams.h
===================================================================
--- src/BufferParams.h	(revision 35615)
+++ src/BufferParams.h	(working copy)
@@ -405,6 +405,8 @@
 	bool output_sync;
 	/// custom LaTeX macro from user instead our own
 	std::string output_sync_macro;
+	/// use refstyle? or prettyref?
+	bool use_refstyle;
 
 private:
 	///
Index: src/frontends/qt4/GuiDocument.cpp
===================================================================
--- src/frontends/qt4/GuiDocument.cpp	(revision 35615)
+++ src/frontends/qt4/GuiDocument.cpp	(working copy)
@@ -1118,6 +1118,8 @@
 		this, SLOT(browseMaster()));
 	connect(latexModule->suppressDateCB, SIGNAL(clicked()),
 		this, SLOT(change_adaptor()));
+	connect(latexModule->refstyleCB, SIGNAL(clicked()),
+		this, SLOT(change_adaptor()));
 
 	// postscript drivers
 	for (int n = 0; tex_graphics[n][0]; ++n) {
@@ -2106,6 +2108,7 @@
 
 	// date
 	bp_.suppress_date = latexModule->suppressDateCB->isChecked();
+	bp_.use_refstyle  = latexModule->refstyleCB->isChecked();
 
 	// biblio
 	bp_.setCiteEngine(ENGINE_BASIC);
@@ -2531,6 +2534,7 @@
 
 	// date
 	latexModule->suppressDateCB->setChecked(bp_.suppress_date);
+	latexModule->refstyleCB->setChecked(bp_.use_refstyle);
 
 	// biblio
 	biblioModule->citeDefaultRB->setChecked(
Index: src/insets/InsetRef.cpp
===================================================================
--- src/insets/InsetRef.cpp	(revision 35615)
+++ src/insets/InsetRef.cpp	(working copy)
@@ -12,6 +12,7 @@
 #include "InsetRef.h"
 
 #include "Buffer.h"
+#include "BufferParams.h"
 #include "Cursor.h"
 #include "DispatchResult.h"
 #include "InsetLabel.h"
@@ -50,7 +51,7 @@
 		|| s == "pageref"
 		|| s == "vref" 
 		|| s == "vpageref"
-		|| s == "prettyref"
+		|| s == "formatted"
 		|| s == "eqref"
 		|| s == "nameref";
 }
@@ -70,11 +71,24 @@
 
 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
 {
-	// We don't want to output p_["name"], since that is only used 
-	// in docbook. So we construct new params, without it, and use that.
-	InsetCommandParams p(REF_CODE, getCmdName());
-	p["reference"] = getParam("reference");
-	os << p.getCommand(runparams);
+	string const cmd = getCmdName();
+	docstring const ref = getParam("reference");
+	if (cmd != "formatted") {
+		// We don't want to output p_["name"], since that is only used 
+		// in docbook. So we construct new params, without it, and use that.
+		InsetCommandParams p(REF_CODE, cmd);
+		p["reference"] = ref;
+		os << p.getCommand(runparams);
+		return 0;
+	} 
+	
+	// so we're doing a formatted reference.
+	if  (!buffer().params().use_refstyle) {
+		os << "\\prettyref{" << ref << '}';
+		return 0;
+	}
+	
+	os << "\\lyxref{" << ref << '}';
 	return 0;
 }
 
@@ -211,9 +225,14 @@
 	string const cmd = getCmdName();
 	if (cmd == "vref" || cmd == "vpageref")
 		features.require("varioref");
-	else if (cmd == "prettyref")
-		features.require("prettyref");
-	else if (cmd == "eqref")
+	else if (getCmdName() == "formatted") {
+		if (buffer().params().use_refstyle) {
+			features.require("refstyle");
+			//features.addPreambleSnippet("");
+		} else
+			features.require("prettyref");
+	} else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
+		// refstyle defines its own version
 		features.require("amsmath");
 	else if (cmd == "nameref")
 		features.require("nameref");
@@ -226,7 +245,7 @@
 	{ "pageref",   N_("Page Number"),           N_("Page: ")},
 	{ "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
 	{ "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
-	{ "prettyref", N_("PrettyRef"),             N_("FrmtRef: ")},
+	{ "formatted", N_("Formatted"),             N_("Format: ")},
 	{ "nameref",   N_("Reference to Name"),     N_("NameRef:")},
 	{ "", "", "" }
 };
Index: src/LaTeXFeatures.cpp
===================================================================
--- src/LaTeXFeatures.cpp	(revision 35615)
+++ src/LaTeXFeatures.cpp	(working copy)
@@ -256,6 +256,25 @@
 	"  \\mathchar\"0\\hexnum...@\\symtipasymb0c}{#2}}\n"
 	"\\newcommand{\\ogonek}[1]{\\mathpalette\\doogonek{#1}}\n");
 
+static docstring const lyxref_def = from_ascii(
+	"\\makeatletter\n"
+	"\\def\\lyxref#1...@lyxref#1:@@@@@:}\n"
+	"\\de...@lyxref#1:#2:{%\n"
+	"\\ifthenelse{\\equal{#2}{@@@@@}}%\n"
+	"	{\\ref{#1}}%\n"
+	"	{\\@@lyxref#1:#2:}%\n"
+	"}\n"
+	"\\def\\@@lyxref#1:#2:@@@@@:{%\n"
+	"	\...@ifundefined{#1ref}%\n"
+	"	{\\ref{#1:#2}}%\n"
+	"	{\...@nameuse{#1ref}{#2}}%\n"
+	"}\n"
+	"\...@ifundefined{\thmref}\n"
+	"  {\\def\\RSthmtxt{theorem~}\\newref{thm}{name = \\RSthmtxt}}\n"
+	"  {}\n"
+	"\\makeatother\n"
+	);
+
 /////////////////////////////////////////////////////////////////////
 //
 // LaTeXFeatures
@@ -507,6 +526,7 @@
 	// subfig is handled in BufferParams.cpp
 	"varioref",
 	"prettyref",
+	"refstyle",
 	/*For a successful cooperation of the `wrapfig' package with the
 	  `float' package you should load the `wrapfig' package *after*
 	  the `float' package. See the caption package documentation
@@ -900,6 +920,9 @@
 
 	// floats
 	getFloatDefinitions(macros);
+	
+	if (mustProvide("refstyle"))
+		macros << lyxref_def << '\n';
 
 	// change tracking
 	if (mustProvide("ct-dvipost"))
Index: src/LyXAction.cpp
===================================================================
--- src/LyXAction.cpp	(revision 35615)
+++ src/LyXAction.cpp	(working copy)
@@ -2311,7 +2311,7 @@
                pageref -- <page> \n
                vpageref -- on <page> \n
                vref -- <reference> on <page> \n
-               prettyref -- Formatted reference
+               formatted -- Formatted reference
  * \endvar
  */
 		{ LFUN_INSET_INSERT, "inset-insert", Noop, Edit },
Index: lib/chkconfig.ltx
===================================================================
--- lib/chkconfig.ltx	(revision 35615)
+++ lib/chkconfig.ltx	(working copy)
@@ -287,6 +287,7 @@
 \TestPackage{pdfpages}
 \TestPackage{prettyref}
 \TestPackage{preview}
+\TestPackage{refstyle}
 \TestPackage{rotating}
 \TestPackage{rotfloat}
 \TestPackage{setspace}
Index: lib/lyx2lyx/lyx_2_0.py
===================================================================
--- lib/lyx2lyx/lyx_2_0.py	(revision 35615)
+++ lib/lyx2lyx/lyx_2_0.py	(working copy)
@@ -1960,6 +1960,52 @@
           del document.body[i:j + 1]
 
 
+def convert_prettyref(document):
+	" Converts prettyref references to neutral formatted refs "
+	re_ref = re.compile("^\s*reference\s+\"(\w+):(\S+)\"")
+	nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
+
+	i = 0
+	while True:
+		i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+		if i == -1:
+			break
+		j = find_end_of_inset(document.body, i)
+		if j == -1:
+			document.warning("Malformed LyX document: No end of InsetRef!")
+			i += 1
+			continue
+		k = find_token(document.body, "LatexCommand prettyref", i)
+		if k != -1 and k < j:
+			document.body[k] = "LatexCommand formatted"
+		i = j + 1
+	document.header.insert(-1, "\\use_refstyle 0")
+		
+ 
+def revert_refstyle(document):
+	" Reverts neutral formatted refs to prettyref "
+	re_ref = re.compile("^reference\s+\"(\w+):(\S+)\"")
+	nm_ref = re.compile("^\s*name\s+\"(\w+):(\S+)\"")
+
+	i = 0
+	while True:
+		i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+		if i == -1:
+			break
+		j = find_end_of_inset(document.body, i)
+		if j == -1:
+			document.warning("Malformed LyX document: No end of InsetRef")
+			i += 1
+			continue
+		k = find_token(document.body, "LatexCommand formatted", i)
+		if k != -1 and k < j:
+			document.body[k] = "LatexCommand prettyref"
+		i = j + 1
+	i = find_token(document.header, "\\use_refstyle", 0)
+	if i != -1:
+		document.header.pop(i)
+ 
+
 def revert_nameref(document):
   " Convert namerefs to regular references "
   cmds = ["Nameref", "nameref"]
@@ -2398,10 +2444,12 @@
            [400, [convert_rule]],
            [401, []],
            [402, [convert_bibtexClearpage]],
-           [403, [convert_flexnames]]
+           [403, [convert_flexnames]],
+           [404, [convert_prettyref]]
 ]
 
-revert =  [[402, [revert_flexnames]],
+revert =  [[403, [revert_refstyle]],
+           [402, [revert_flexnames]],
            [401, []],
            [400, [revert_diagram]],
            [399, [revert_rule]],
Index: lib/symbols
===================================================================
--- lib/symbols	(revision 35615)
+++ lib/symbols	(working copy)
@@ -136,6 +136,7 @@
 # references
 pageref           ref         none
 prettyref         ref         none
+refstyle          ref         none
 ref               ref         none
 vpageref          ref         none
 vref              ref         none
Index: lib/ui/stdcontext.inc
===================================================================
--- lib/ui/stdcontext.inc	(revision 35615)
+++ lib/ui/stdcontext.inc	(working copy)
@@ -90,7 +90,7 @@
 		Item "<Page>|P" "inset-modify changetype pageref"
 		Item "On Page <Page>|O" "inset-modify changetype vpageref"
 		Item "<Reference> on Page <Page>|f" "inset-modify changetype vref"
-		Item "Formatted Reference|t" "inset-modify changetype prettyref"
+		Item "Formatted Reference|t" "inset-modify changetype formatted"
 		Item "Textual Reference|x" "inset-modify changetype nameref"
 		Separator
 		Item "Settings...|S" "inset-settings"
Index: src/frontends/qt4/ui/LaTeXUi.ui
===================================================================
--- src/frontends/qt4/ui/LaTeXUi.ui	(revision 35615)
+++ src/frontends/qt4/ui/LaTeXUi.ui	(working copy)
@@ -6,7 +6,7 @@
     <x>0</x>
     <y>0</y>
     <width>341</width>
-    <height>319</height>
+    <height>442</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -187,7 +187,7 @@
      </property>
     </widget>
    </item>
-   <item row="5" column="1">
+   <item row="6" column="1">
     <spacer name="verticalSpacer">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
@@ -200,6 +200,13 @@
      </property>
     </spacer>
    </item>
+   <item row="5" column="0" colspan="4">
+    <widget class="QCheckBox" name="refstyleCB">
+     <property name="text">
+      <string>Use refstyle for cross-references</string>
+     </property>
+    </widget>
+   </item>
   </layout>
  </widget>
  <tabstops>

Reply via email to