Add a "noprefix" option to the "labelonly" version of the reference

inset.

If the reference is "sec:mysec", then this will output just "mysec".
This is what is needed, e.g., to use refstyle's range commands, e.g.,
    \secrangeref{mysec1}{mysec2}
You *cannot* use:
    \secrangeref{sec:mysec1}{sec:mysec2}
even if those are the actual labels.

Eventually, I'd like to add native support for this, but I'm a bit
frightened of trying to make the reference dialog support multiple
selections.
This commit is contained in:
Richard Heck 2017-01-07 14:39:03 -05:00
parent 6974e1ceb2
commit ff3690a5fd
6 changed files with 99 additions and 10 deletions

View File

@ -1082,7 +1082,7 @@ def revert_labelonly(document):
i = j + 1
continue
document.body[i:j+1] = put_cmd_in_ert([label])
i = j + 1
i += 1
def revert_plural_refs(document):
@ -1131,8 +1131,46 @@ def revert_plural_refs(document):
cmd += "[s]"
cmd += "{" + suffix + "}"
document.body[i:j+1] = put_cmd_in_ert([cmd])
i = j + 1
i += 1
def revert_noprefix(document):
" Revert labelonly tags with 'noprefix' set "
i = 0
while (True):
i = find_token(document.body, "\\begin_inset CommandInset ref", i)
if i == -1:
return
j = find_end_of_inset(document.body, i)
if j == -1:
document.warning("Can't find end of reference inset at line %d!!" %(i))
i += 1
continue
k = find_token(document.body, "LatexCommand labelonly", i, j)
if k == -1:
i = j
continue
noprefix = get_bool_value(document.body, "noprefix", i, j)
if not noprefix:
del_token(document.body, "noprefix", i, j)
i = j
continue
label = get_quoted_value(document.body, "reference", i, j)
if not label:
document.warning("Can't find label for reference at line %d!" %(i))
i = j + 1
continue
try:
(prefix, suffix) = label.split(":", 1)
except:
document.warning("No `:' separator in formatted reference at line %d!" % (i))
# we'll leave this as an ordinary labelonly reference
del_token(document.body, "noprefix", i, j)
i = j
continue
document.body[i:j+1] = put_cmd_in_ert([suffix])
i += 1
##
# Conversion hub
@ -1157,10 +1195,12 @@ convert = [
[523, []],
[524, []],
[525, []],
[526, []]
[526, []],
[527, []]
]
revert = [
[526, [revert_noprefix]],
[525, [revert_plural_refs]],
[524, [revert_labelonly]],
[523, [revert_crimson, revert_cochinealmath]],

View File

@ -110,6 +110,8 @@ GuiRef::GuiRef(GuiView & lv)
this, SLOT(changed_adaptor()));
connect(capsCB, SIGNAL(clicked()),
this, SLOT(changed_adaptor()));
connect(noprefixCB, SIGNAL(clicked()),
this, SLOT(changed_adaptor()));
enableBoxes();
@ -137,9 +139,12 @@ void GuiRef::enableBoxes()
{
bool const isFormatted =
(InsetRef::getName(typeCO->currentIndex()) == "formatted");
bool const isLabelOnly =
(InsetRef::getName(typeCO->currentIndex()) == "labelonly");
bool const usingRefStyle = buffer().params().use_refstyle;
pluralCB->setEnabled(isFormatted && usingRefStyle);
capsCB->setEnabled (isFormatted && usingRefStyle);
noprefixCB->setEnabled (isLabelOnly && usingRefStyle);
}
@ -301,6 +306,7 @@ void GuiRef::updateContents()
pluralCB->setChecked(params_["plural"] == "true");
capsCB->setChecked(params_["caps"] == "true");
noprefixCB->setChecked(params_["noprefix"] == "true");
// insert buffer list
bufferCO->clear();
@ -342,6 +348,8 @@ void GuiRef::applyView()
from_ascii("true") : from_ascii("false");
params_["caps"] = capsCB->isChecked() ?
from_ascii("true") : from_ascii("false");
params_["noprefix"] = noprefixCB->isChecked() ?
from_ascii("true") : from_ascii("false");
restored_buffer_ = bufferCO->currentIndex();
}

View File

@ -403,6 +403,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="noprefixCB">
<property name="toolTip">
<string>Do not output part of label before &quot;:&quot;</string>
</property>
<property name="text">
<string>No Prefix</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>

View File

@ -71,6 +71,7 @@ ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
ParamInfo::HANDLING_ESCAPE);
param_info_.add("plural", ParamInfo::LYX_INTERNAL);
param_info_.add("caps", ParamInfo::LYX_INTERNAL);
param_info_.add("noprefix", ParamInfo::LYX_INTERNAL);
}
return param_info_;
}
@ -110,7 +111,7 @@ docstring InsetRef::getFormattedCmd(docstring const & ref,
// we have to have xxx:xxxxx...
if (label.empty()) {
LYXERR0("Label `" << ref << "' contains no prefix.");
LYXERR0("Label `" << ref << "' contains no `:' separator.");
label = ref;
prefix = from_ascii("");
return defcmd;
@ -181,7 +182,19 @@ void InsetRef::latex(otexstream & os, OutputParams const & rp) const
os << '{' << label << '}';
}
else if (cmd == "labelonly") {
os << getParam("reference");
docstring const & ref = getParam("reference");
if (getParam("noprefix") != "true")
os << ref;
else {
docstring prefix;
docstring suffix = split(ref, prefix, ':');
if (suffix.empty()) {
LYXERR0("Label `" << ref << "' contains no `:' separator.");
os << ref;
} else {
os << suffix;
}
}
}
else {
// We don't want to output p_["name"], since that is only used
@ -303,13 +316,29 @@ void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
buffer().addReference(ref, this, it);
docstring label;
string const & cmd = getCmdName();
for (int i = 0; !types[i].latex_name.empty(); ++i) {
if (getCmdName() == types[i].latex_name) {
if (cmd == types[i].latex_name) {
label = _(types[i].short_gui_name);
break;
}
}
label += ref;
if (cmd != "labelonly")
label += ref;
else {
if (getParam("noprefix") != "true")
label += ref;
else {
docstring prefix;
docstring suffix = split(ref, prefix, ':');
if (suffix.empty()) {
label += ref;
} else {
label += suffix;
}
}
}
if (!buffer().params().isLatex() && !getParam("name").empty()) {
label += "||";

View File

@ -3498,6 +3498,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
<< "\"\n";
os << "plural \"false\"\n";
os << "caps \"false\"\n";
os << "noprefix \"false\"\n";
end_inset(os);
preamble.registerAutomaticallyLoadedPackage("refstyle");
}
@ -3516,6 +3517,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
<< "\"\n";
os << "plural \"false\"\n";
os << "caps \"false\"\n";
os << "noprefix \"false\"\n";
end_inset(os);
if (t.cs() == "vref" || t.cs() == "vpageref")
preamble.registerAutomaticallyLoadedPackage("varioref");

View File

@ -32,8 +32,8 @@ extern char const * const lyx_version_info;
// Do not remove the comment below, so we get merge conflict in
// independent branches. Instead add your own.
#define LYX_FORMAT_LYX 526 // rgh: labelonly for references
#define LYX_FORMAT_TEX2LYX 526
#define LYX_FORMAT_LYX 527 // rgh: labelonly for references
#define LYX_FORMAT_TEX2LYX 527
#if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
#ifndef _MSC_VER