From e285d2a7d84cb372c6ec96a8dd422766ecadda7a Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Thu, 20 Dec 2007 15:46:14 +0000 Subject: [PATCH] Support for nocite, provided by Bernhard Reiter. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22217 a592a061-630c-0410-9148-cb99ea01b6c8 --- development/FORMAT | 3 ++ lib/lyx2lyx/LyX.py | 2 +- lib/lyx2lyx/lyx_1_6.py | 39 ++++++++++++++++- src/BiblioInfo.cpp | 23 +++++++---- src/BiblioInfo.h | 1 + src/Buffer.cpp | 2 +- src/frontends/qt4/GuiCitation.cpp | 69 ++++++++++++++++++------------- src/frontends/qt4/GuiCitation.h | 3 ++ src/insets/InsetCitation.cpp | 55 +++++++++++++++--------- src/tex2lyx/text.cpp | 2 +- 10 files changed, 139 insertions(+), 60 deletions(-) diff --git a/development/FORMAT b/development/FORMAT index 30a70641f8..46a6621e4c 100644 --- a/development/FORMAT +++ b/development/FORMAT @@ -1,6 +1,9 @@ LyX file-format changes ----------------------- +2007-12-11 Bernhard Reiter + * Format incremented to 309: support for \nocite + 2007-12-15 Uwe Stöhr * Format incremented to 308: support for Serbian (Latin) diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py index 01fc767102..d67c440e57 100644 --- a/lib/lyx2lyx/LyX.py +++ b/lib/lyx2lyx/LyX.py @@ -80,7 +80,7 @@ format_relation = [("0_06", [200], minor_versions("0.6" , 4)), ("1_3", [221], minor_versions("1.3" , 7)), ("1_4", range(222,246), minor_versions("1.4" , 5)), ("1_5", range(246,277), minor_versions("1.5" , 2)), - ("1_6", range(277,309), minor_versions("1.6" , 0))] # Uwe: Serbian-Latin + ("1_6", range(277,310), minor_versions("1.6" , 0))] # Bernhard Reiter: nocite def formats_list(): diff --git a/lib/lyx2lyx/lyx_1_6.py b/lib/lyx2lyx/lyx_1_6.py index 1817b0bb66..edae58d70f 100644 --- a/lib/lyx2lyx/lyx_1_6.py +++ b/lib/lyx2lyx/lyx_1_6.py @@ -945,6 +945,39 @@ def revert_nobreakdash(document): document.header[j] = "\\use_amsmath 2" +def revert_nocite_key(body, start, end): + 'key "..." -> \nocite{...}' + for i in range(start, end): + if (body[i][0:5] == 'key "'): + body[i] = body[i].replace('key "', "\\backslash\nnocite{") + body[i] = body[i].replace('"', "}") + else: + body[i] = "" + + +def revert_nocite(document): + "Revert LatexCommand nocite to ERT" + i = 0 + while 1: + i = find_token(document.body, "\\begin_inset CommandInset citation", i) + if i == -1: + return + i = i + 1 + if (document.body[i] == "LatexCommand nocite"): + j = find_end_of_inset(document.body, i + 1) + if j == -1: + #this should not happen + document.warning("End of CommandInset citation not found in revert_nocite!") + revert_nocite_key(document.body, i + 1, len(document.body)) + return + revert_nocite_key(document.body, i + 1, j) + document.body[i-1] = "\\begin_inset ERT" + document.body[i] = "status collapsed\n\n" \ + "\\begin_layout Standard" + document.body.insert(j, "\\end_layout\n"); + i = j + + def revert_bahasam(document): "Set language Bahasa Malaysia to Bahasa Indonesia" i = 0 @@ -1032,10 +1065,12 @@ convert = [[277, [fix_wrong_tables]], [305, []], [306, []], [307, []], - [308, []] + [308, []], + [309, []] ] -revert = [[307, [revert_serbianlatin]], +revert = [[308, [revert_nocite]], + [307, [revert_serbianlatin]], [306, [revert_slash, revert_nobreakdash]], [305, [revert_interlingua]], [304, [revert_bahasam]], diff --git a/src/BiblioInfo.cpp b/src/BiblioInfo.cpp index 8f9aab0905..9f00a3753d 100644 --- a/src/BiblioInfo.cpp +++ b/src/BiblioInfo.cpp @@ -282,7 +282,8 @@ vector const BiblioInfo::getCiteStrings( docstring const & key, Buffer const & buf) const { biblio::CiteEngine const engine = buf.params().getEngine(); - if (engine == biblio::ENGINE_NATBIB_NUMERICAL) + if (engine == biblio::ENGINE_BASIC || + engine == biblio::ENGINE_NATBIB_NUMERICAL) return getNumericalStrings(key, buf); else return getAuthorYearStrings(key, buf); @@ -304,7 +305,7 @@ vector const BiblioInfo::getNumericalStrings( biblio::getCiteStyles(buf.params().getEngine()); vector vec(styles.size()); - for (size_t i = 0; i != vec.size(); ++i) { + for (vector::size_type i = 0; i != vec.size(); ++i) { docstring str; switch (styles[i]) { @@ -313,6 +314,10 @@ vector const BiblioInfo::getNumericalStrings( str = from_ascii("[#ID]"); break; + case biblio::NOCITE: + str = _("Add to bibliography only."); + break; + case biblio::CITET: str = author + " [#ID]"; break; @@ -370,6 +375,10 @@ vector const BiblioInfo::getAuthorYearStrings( str = author + "/<" + _("before") + '>'; break; + case biblio::NOCITE: + str = _("Add to bibliography only."); + break; + case biblio::CITET: str = author + " (" + year + ')'; break; @@ -435,15 +444,15 @@ namespace { char const * const citeCommands[] = { - "cite", "citet", "citep", "citealt", "citealp", "citeauthor", - "citeyear", "citeyearpar" }; + "cite", "nocite", "citet", "citep", "citealt", "citealp", + "citeauthor", "citeyear", "citeyearpar" }; unsigned int const nCiteCommands = sizeof(citeCommands) / sizeof(char *); CiteStyle const citeStyles[] = { - CITE, CITET, CITEP, CITEALT, CITEALP, -CITEAUTHOR, CITEYEAR, CITEYEARPAR }; + CITE, NOCITE, CITET, CITEP, CITEALT, +CITEALP, CITEAUTHOR, CITEYEAR, CITEYEARPAR }; unsigned int const nCiteStyles = sizeof(citeStyles) / sizeof(CiteStyle); @@ -517,7 +526,7 @@ vector const getCiteStyles(CiteEngine const engine) switch (engine) { case ENGINE_BASIC: - nStyles = 1; + nStyles = 2; start = 0; break; case ENGINE_NATBIB_AUTHORYEAR: diff --git a/src/BiblioInfo.h b/src/BiblioInfo.h index b6973e1ead..7f57a31c6b 100644 --- a/src/BiblioInfo.h +++ b/src/BiblioInfo.h @@ -36,6 +36,7 @@ enum CiteEngine { enum CiteStyle { CITE, + NOCITE, CITET, CITEP, CITEALT, diff --git a/src/Buffer.cpp b/src/Buffer.cpp index b67c644bb9..9dc1b28000 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -118,7 +118,7 @@ namespace os = support::os; namespace { -int const LYX_FORMAT = 308; // Uwe: Serbian-Latin +int const LYX_FORMAT = 309; // Bernhard Reiter: support for \nocite } // namespace anon diff --git a/src/frontends/qt4/GuiCitation.cpp b/src/frontends/qt4/GuiCitation.cpp index fa5099d33f..10e6d9bdaa 100644 --- a/src/frontends/qt4/GuiCitation.cpp +++ b/src/frontends/qt4/GuiCitation.cpp @@ -82,7 +82,7 @@ GuiCitation::GuiCitation(GuiView & lv) setViewTitle(_("Citation")); connect(citationStyleCO, SIGNAL(activated(int)), - this, SLOT(changed())); + this, SLOT(on_citationStyleCO_currentIndexChanged(int))); connect(fulllistCB, SIGNAL(clicked()), this, SLOT(changed())); connect(forceuppercaseCB, SIGNAL(clicked()), @@ -188,7 +188,7 @@ void GuiCitation::updateView() // called in update() do not need to be called for INTERNAL updates, // such as when addPB is pressed, as the list of fields, entries, etc, // will not have changed. At the moment, however, the division between -// fillStyles() and updateStyles() doesn't lend itself to dividing the +// fillStyles() and updateStyle() doesn't lend itself to dividing the // two methods, though they should be divisible. void GuiCitation::updateDialog() { @@ -212,7 +212,7 @@ void GuiCitation::updateDialog() } -void GuiCitation::updateStyle() +void GuiCitation::updateFormatting(biblio::CiteStyle currentStyle) { biblio::CiteEngine const engine = getEngine(); bool const natbib_engine = @@ -222,15 +222,22 @@ void GuiCitation::updateStyle() bool const haveSelection = selectedLV->model()->rowCount() > 0; - fulllistCB->setEnabled(natbib_engine && haveSelection); - forceuppercaseCB->setEnabled(natbib_engine && haveSelection); - textBeforeED->setEnabled(!basic_engine && haveSelection); - textBeforeLA->setEnabled(!basic_engine && haveSelection); - textAfterED->setEnabled(haveSelection); - textAfterLA->setEnabled(haveSelection); - citationStyleCO->setEnabled(!basic_engine && haveSelection); - citationStyleLA->setEnabled(!basic_engine && haveSelection); + bool const isNocite = currentStyle == biblio::NOCITE; + + fulllistCB->setEnabled(natbib_engine && haveSelection && !isNocite); + forceuppercaseCB->setEnabled(natbib_engine && haveSelection && !isNocite); + textBeforeED->setEnabled(!basic_engine && haveSelection && !isNocite); + textBeforeLA->setEnabled(!basic_engine && haveSelection && !isNocite); + textAfterED->setEnabled(haveSelection && !isNocite); + textAfterLA->setEnabled(haveSelection && !isNocite); + citationStyleCO->setEnabled(haveSelection); + citationStyleLA->setEnabled(haveSelection); +} + + +void GuiCitation::updateStyle() +{ string const & command = params_.getCmdName(); // Find the style of the citekeys @@ -255,9 +262,9 @@ void GuiCitation::updateStyle() fulllistCB->setChecked(false); forceuppercaseCB->setChecked(false); } + updateFormatting(cs.style); } - //This one needs to be called whenever citationStyleCO needs //to be updated---and this would be on anything that changes the //selection in selectedLV, or on a general update. @@ -283,12 +290,10 @@ void GuiCitation::fillStyles() QStringList sty = citationStyles(curr); - bool const basic_engine = (getEngine() == biblio::ENGINE_BASIC); + citationStyleCO->setEnabled(!sty.isEmpty()); + citationStyleLA->setEnabled(!sty.isEmpty()); - citationStyleCO->setEnabled(!sty.isEmpty() && !basic_engine); - citationStyleLA->setEnabled(!sty.isEmpty() && !basic_engine); - - if (sty.isEmpty() || basic_engine) + if (sty.isEmpty()) return; citationStyleCO->insertItems(0, sty); @@ -403,6 +408,15 @@ void GuiCitation::on_entriesCO_currentIndexChanged(int /*index*/) } +void GuiCitation::on_citationStyleCO_currentIndexChanged(int index) +{ + if (index >= 0 && index < citationStyleCO->count()) { + vector const & styles = citeStyles_; + updateFormatting(styles[index]); + } +} + + void GuiCitation::on_findLE_textChanged(const QString & text) { clearPB->setDisabled(text.isEmpty()); @@ -426,19 +440,24 @@ void GuiCitation::on_regexCB_stateChanged(int) void GuiCitation::changed() { - fillStyles(); setButtons(); } -void GuiCitation::apply(int const choice, bool const full, bool const force, +void GuiCitation::apply(int const choice, bool full, bool force, QString before, QString after) { if (cited_keys_.isEmpty()) return; vector const & styles = citeStyles_; - + if (styles[choice] == biblio::NOCITE) { + full = false; + force = false; + before.clear(); + after.clear(); + } + string const command = biblio::CitationStyle(styles[choice], full, force) .asLatexStr(); @@ -575,17 +594,9 @@ bool GuiCitation::initialiseParams(string const & data) biblio::CiteEngine const engine = buffer().params().getEngine(); - bool use_styles = engine != biblio::ENGINE_BASIC; - bibkeysInfo_.fillWithBibKeys(&buffer()); - if (citeStyles_.empty()) - citeStyles_ = biblio::getCiteStyles(engine); - else { - if ((use_styles && citeStyles_.size() == 1) || - (!use_styles && citeStyles_.size() != 1)) - citeStyles_ = biblio::getCiteStyles(engine); - } + citeStyles_ = biblio::getCiteStyles(engine); return true; } diff --git a/src/frontends/qt4/GuiCitation.h b/src/frontends/qt4/GuiCitation.h index 9b9a867fc7..f8029ece78 100644 --- a/src/frontends/qt4/GuiCitation.h +++ b/src/frontends/qt4/GuiCitation.h @@ -65,6 +65,7 @@ private Q_SLOTS: void on_findLE_textChanged(const QString & text); void on_fieldsCO_currentIndexChanged(int index); void on_entriesCO_currentIndexChanged(int index); + void on_citationStyleCO_currentIndexChanged(int index); void on_caseCB_stateChanged(int); void on_regexCB_stateChanged(int); void changed(); @@ -84,6 +85,8 @@ private: void fillEntries(); /// set the styles combo void updateStyle(); + /// set the formatting widgets + void updateFormatting(biblio::CiteStyle currentStyle); /// last used citation style int style_; diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index b7faae15e2..dcc4fad43a 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -17,6 +17,7 @@ #include "BufferParams.h" #include "support/debug.h" #include "DispatchResult.h" +#include "support/gettext.h" #include "FuncRequest.h" #include "LaTeXFeatures.h" @@ -36,7 +37,7 @@ namespace { vector const init_possible_cite_commands() { char const * const possible[] = { - "cite", "citet", "citep", "citealt", "citealp", + "cite", "nocite", "citet", "citep", "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*", "citealt*", "citealp*", "citeauthor*", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", @@ -91,7 +92,7 @@ string const string output; switch (engine) { case biblio::ENGINE_BASIC: - output = default_str; + output = input; break; case biblio::ENGINE_NATBIB_AUTHORYEAR: @@ -125,7 +126,7 @@ string const } -docstring const getNatbibLabel(Buffer const & buffer, +docstring const getComplexLabel(Buffer const & buffer, string const & citeType, docstring const & keyList, docstring const & before, docstring const & after, biblio::CiteEngine engine) @@ -209,16 +210,25 @@ docstring const getNatbibLabel(Buffer const & buffer, } docstring after_str; - if (!after.empty()) { - // The "after" key is appended only to the end of the whole. + // The "after" key is appended only to the end of the whole. + if (cite_type == "nocite") + after_str = " (" + _("not cited") + ')'; + else if (!after.empty()) { after_str = ", " + after; } // One day, these might be tunable (as they are in BibTeX). - char const op = '('; // opening parenthesis. - char const cp = ')'; // closing parenthesis. - // puctuation mark separating citation entries. - char const * const sep = ";"; + char op, cp; // opening and closing parenthesis. + char * sep; // punctuation mark separating citation entries. + if (engine == biblio::ENGINE_BASIC) { + op = '['; + cp = ']'; + sep = ","; + } else { + op = '('; + cp = ')'; + sep = ";"; + } docstring const op_str = ' ' + docstring(1, op); docstring const cp_str = docstring(1, cp) + ' '; @@ -239,11 +249,19 @@ docstring const getNatbibLabel(Buffer const & buffer, // authors1/; ... ; // authors_last, - if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) { - if (it == keys.begin()) - label += author + before_str + sep_str; - else - label += author + sep_str; + if (cite_type == "cite") { + if (engine == biblio::ENGINE_BASIC) { + label += *it + sep_str; + } else if (engine == biblio::ENGINE_JURABIB) { + if (it == keys.begin()) + label += author + before_str + sep_str; + else + label += author + sep_str; + } + + // nocite + } else if (cite_type == "nocite") { + label += *it + sep_str; // (authors1 ( year); ... ; // authors_last ( year, ) @@ -324,7 +342,8 @@ docstring const getNatbibLabel(Buffer const & buffer, label = before_str + label; } - if (cite_type == "citep" || cite_type == "citeyearpar") + if (cite_type == "citep" || cite_type == "citeyearpar" || + (cite_type == "cite" && engine == biblio::ENGINE_BASIC) ) label = op + label + cp; return label; @@ -390,10 +409,8 @@ docstring const InsetCitation::generateLabel(Buffer const & buffer) const docstring label; biblio::CiteEngine const engine = buffer.params().getEngine(); - if (engine != biblio::ENGINE_BASIC) { - label = getNatbibLabel(buffer, getCmdName(), getParam("key"), - before, after, engine); - } + label = getComplexLabel(buffer, getCmdName(), getParam("key"), + before, after, engine); // Fallback to fail-safe if (label.empty()) diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index 211fbb24e7..c73c051cd3 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -87,7 +87,7 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer, } -char const * const known_latex_commands[] = { "ref", "cite", "label", +char const * const known_latex_commands[] = { "ref", "cite", "nocite", "label", "index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 }; -- 2.39.5