X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2FInsetCitation.cpp;h=a9597ca872b8d8939d52a2e2355aaa257d9730be;hb=c3a8b3a566e9e90f9ade72acbc723232d721d0b1;hp=e1a727c4c4c2aa0835bd24e1a4c56f91f0082ef8;hpb=00e305c9d9bd8b0ff54b6c58adb6192d5934ed60;p=lyx.git diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index e1a727c4c4..a9597ca872 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -15,104 +15,158 @@ #include "Buffer.h" #include "BufferParams.h" -#include "debug.h" +#include "support/debug.h" #include "DispatchResult.h" +#include "support/gettext.h" +#include "EmbeddedFiles.h" #include "FuncRequest.h" #include "LaTeXFeatures.h" -#include "frontends/controllers/frontend_helpers.h" - -#include "support/fs_extras.h" #include "support/lstrings.h" +#include "support/docstream.h" #include -#include -#include - +using namespace std; +using namespace lyx::support; namespace lyx { -using support::ascii_lowercase; -using support::contains; -using support::FileName; -using support::getStringFromVector; -using support::getVectorFromString; -using support::ltrim; -using support::rtrim; -using support::split; -using support::tokenPos; +namespace { + +vector const init_possible_cite_commands() +{ + char const * const possible[] = { + "cite", "nocite", "citet", "citep", "citealt", "citealp", + "citeauthor", "citeyear", "citeyearpar", + "citet*", "citep*", "citealt*", "citealp*", "citeauthor*", + "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", + "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*", + "fullcite", + "footcite", "footcitet", "footcitep", "footcitealt", + "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar", + "citefield", "citetitle", "cite*" + }; + size_t const size_possible = sizeof(possible) / sizeof(possible[0]); + + return vector(possible, possible + size_possible); +} + -using std::endl; -using std::replace; -using std::string; -using std::ostream; -using std::vector; -using std::map; +vector const & possible_cite_commands() +{ + static vector const possible = init_possible_cite_commands(); + return possible; +} -namespace fs = boost::filesystem; +//FIXME See the header for the issue. +string const default_cite_command(biblio::CiteEngine engine) +{ + string str; + switch (engine) { + case biblio::ENGINE_BASIC: + str = "cite"; + break; + case biblio::ENGINE_NATBIB_AUTHORYEAR: + str = "citet"; + break; + case biblio::ENGINE_NATBIB_NUMERICAL: + str = "citep"; + break; + case biblio::ENGINE_JURABIB: + str = "cite"; + break; + } + return str; +} -namespace { + +string const + asValidLatexCommand(string const & input, biblio::CiteEngine const engine) +{ + string const default_str = default_cite_command(engine); + if (!InsetCitation::isCompatibleCommand(input)) + return default_str; + + string output; + switch (engine) { + case biblio::ENGINE_BASIC: + output = input; + break; + + case biblio::ENGINE_NATBIB_AUTHORYEAR: + case biblio::ENGINE_NATBIB_NUMERICAL: + if (input == "cite" || input == "citefield" || + input == "citetitle" || input == "cite*") + output = default_str; + else if (prefixIs(input, "foot")) + output = input.substr(4); + else + output = input; + break; + + case biblio::ENGINE_JURABIB: { + // Jurabib does not support the 'uppercase' natbib style. + if (input[0] == 'C') + output = string(1, 'c') + input.substr(1); + else + output = input; + + // Jurabib does not support the 'full' natbib style. + string::size_type const n = output.size() - 1; + if (output != "cite*" && output[n] == '*') + output = output.substr(0, n); + + break; + } + } -docstring const getNatbibLabel(Buffer const & buffer, - string const & citeType, string const & keyList, + return output; +} + + +docstring const getComplexLabel(Buffer const & buffer, + string const & citeType, docstring const & keyList, docstring const & before, docstring const & after, biblio::CiteEngine engine) { // Only start the process off after the buffer is loaded from file. - if (!buffer.fully_loaded()) + if (!buffer.isFullyLoaded()) return docstring(); // Cache the labels - typedef std::map CachedMap; + typedef map CachedMap; static CachedMap cached_keys; // and cache the timestamp of the bibliography files. - static std::map bibfileStatus; + static map bibfileStatus; - biblio::InfoMap infomap; + BiblioInfo biblist; - vector const & bibfilesCache = buffer.getBibfilesCache(); + EmbeddedFileList const & bibfilesCache = buffer.getBibfilesCache(); // compare the cached timestamps with the actual ones. bool changed = false; - for (vector::const_iterator it = bibfilesCache.begin(); + for (EmbeddedFileList::const_iterator it = bibfilesCache.begin(); it != bibfilesCache.end(); ++ it) { FileName const f = *it; - try { - std::time_t lastw = fs::last_write_time(f.toFilesystemEncoding()); - if (lastw != bibfileStatus[f]) { - changed = true; - bibfileStatus[f] = lastw; - } - } - catch (fs::filesystem_error & fserr) { + time_t lastw = f.lastModified(); + if (lastw != bibfileStatus[f]) { changed = true; - lyxerr << "Couldn't find or read bibtex file " - << f << endl; - LYXERR(Debug::DEBUG) << "Fs error: " - << fserr.what() << endl; + bibfileStatus[f] = lastw; } } - // build the keylist only if the bibfiles have been changed + // build the list only if the bibfiles have been changed if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) { - typedef vector > InfoType; - InfoType bibkeys; - buffer.fillWithBibKeys(bibkeys); - - InfoType::const_iterator bit = bibkeys.begin(); - InfoType::const_iterator bend = bibkeys.end(); - - for (; bit != bend; ++bit) - infomap[bit->first] = bit->second; - - cached_keys[&buffer] = infomap; - } else + biblist.fillWithBibKeys(&buffer); + cached_keys[&buffer] = biblist; + } else { // use the cached keys - infomap = cached_keys[&buffer]; + biblist = cached_keys[&buffer]; + } - if (infomap.empty()) + if (biblist.empty()) return docstring(); // the natbib citation-styles @@ -127,10 +181,12 @@ docstring const getNatbibLabel(Buffer const & buffer, // CITE: author/ // We don't currently use the full or forceUCase fields. - string cite_type = biblio::asValidLatexCommand(citeType, engine); + string cite_type = asValidLatexCommand(citeType, engine); if (cite_type[0] == 'C') + //If we were going to use them, this would mean ForceUCase cite_type = string(1, 'c') + cite_type.substr(1); if (cite_type[cite_type.size() - 1] == '*') + //and this would mean FULL cite_type = cite_type.substr(0, cite_type.size() - 1); docstring before_str; @@ -154,29 +210,38 @@ 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) + ' '); - docstring const sep_str(from_ascii(sep) + ' '); + docstring const op_str = ' ' + docstring(1, op); + docstring const cp_str = docstring(1, cp) + ' '; + docstring const sep_str = from_ascii(sep) + ' '; docstring label; - vector keys = getVectorFromString(keyList); - vector::const_iterator it = keys.begin(); - vector::const_iterator end = keys.end(); + vector keys = getVectorFromString(keyList); + vector::const_iterator it = keys.begin(); + vector::const_iterator end = keys.end(); for (; it != end; ++it) { // get the bibdata corresponding to the key - docstring const author(biblio::getAbbreviatedAuthor(infomap, *it)); - docstring const year(biblio::getYear(infomap, *it)); + docstring const author(biblist.getAbbreviatedAuthor(*it)); + docstring const year(biblist.getYear(*it)); // Something isn't right. Fail safely. if (author.empty() || year.empty()) @@ -184,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, ) @@ -199,9 +272,7 @@ docstring const getNatbibLabel(Buffer const & buffer, year + cp + sep_str; break; case biblio::ENGINE_NATBIB_NUMERICAL: - // FIXME UNICODE - label += author + op_str + before_str + - '#' + from_utf8(*it) + cp + sep_str; + label += author + op_str + before_str + '#' + *it + cp + sep_str; break; case biblio::ENGINE_JURABIB: label += before_str + author + op_str + @@ -215,8 +286,7 @@ docstring const getNatbibLabel(Buffer const & buffer, } else if (cite_type == "citep" || cite_type == "citealp") { if (engine == biblio::ENGINE_NATBIB_NUMERICAL) { - // FIXME UNICODE - label += from_utf8(*it) + sep_str; + label += *it + sep_str; } else { label += author + ", " + year + sep_str; } @@ -230,9 +300,7 @@ docstring const getNatbibLabel(Buffer const & buffer, year + sep_str; break; case biblio::ENGINE_NATBIB_NUMERICAL: - // FIXME UNICODE - label += author + ' ' + before_str + - '#' + from_utf8(*it) + sep_str; + label += author + ' ' + before_str + '#' + *it + sep_str; break; case biblio::ENGINE_JURABIB: label += before_str + author + ' ' + @@ -274,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; @@ -283,7 +352,7 @@ docstring const getNatbibLabel(Buffer const & buffer, docstring const getBasicLabel(docstring const & keyList, docstring const & after) { - docstring keys(keyList); + docstring keys = keyList; docstring label; if (contains(keys, ',')) { @@ -311,6 +380,28 @@ InsetCitation::InsetCitation(InsetCommandParams const & p) {} +CommandInfo const * InsetCitation::findInfo(string const & /* cmdName */) +{ + // standard cite does only take one argument if jurabib is + // not used, but jurabib extends this to two arguments, so + // we have to allow both here. InsetCitation takes care that + // LaTeX output is nevertheless correct. + static const char * const paramnames[] = + {"after", "before", "key", ""}; + static const bool isoptional[] = {true, true, false}; + static const CommandInfo info = {3, paramnames, isoptional}; + return &info; +} + + +bool InsetCitation::isCompatibleCommand(string const & cmd) +{ + vector const & possibles = possible_cite_commands(); + vector::const_iterator const end = possibles.end(); + return find(possibles.begin(), end, cmd) != end; +} + + docstring const InsetCitation::generateLabel(Buffer const & buffer) const { docstring const before = getParam("before"); @@ -318,16 +409,12 @@ docstring const InsetCitation::generateLabel(Buffer const & buffer) const docstring label; biblio::CiteEngine const engine = buffer.params().getEngine(); - if (engine != biblio::ENGINE_BASIC) { - // FIXME UNICODE - label = getNatbibLabel(buffer, getCmdName(), to_utf8(getParam("key")), - before, after, engine); - } + label = getComplexLabel(buffer, getCmdName(), getParam("key"), + before, after, engine); // Fallback to fail-safe - if (label.empty()) { + if (label.empty()) label = getBasicLabel(getParam("key"), after); - } return label; } @@ -360,7 +447,7 @@ docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const int InsetCitation::plaintext(Buffer const & buffer, odocstream & os, - OutputParams const &) const + OutputParams const &) const { docstring str; @@ -375,9 +462,7 @@ int InsetCitation::plaintext(Buffer const & buffer, odocstream & os, } -namespace { - -docstring const cleanupWhitespace(docstring const & citelist) +static docstring const cleanupWhitespace(docstring const & citelist) { docstring::const_iterator it = citelist.begin(); docstring::const_iterator end = citelist.end(); @@ -394,23 +479,20 @@ docstring const cleanupWhitespace(docstring const & citelist) return result; } -// end anon namyspace -} int InsetCitation::docbook(Buffer const &, odocstream & os, - OutputParams const &) const + OutputParams const &) const { - os << "" - << cleanupWhitespace(getParam("key")) - << ""; + os << from_ascii("") + << cleanupWhitespace(getParam("key")) + << from_ascii(""); return 0; } -int InsetCitation::textString(Buffer const & buf, odocstream & os, - OutputParams const & op) const +void InsetCitation::textString(Buffer const & buf, odocstream & os) const { - return plaintext(buf, os, op); + plaintext(buf, os, OutputParams(0)); } @@ -419,12 +501,12 @@ int InsetCitation::textString(Buffer const & buf, odocstream & os, // citations and then changes his mind, turning natbib support off. The output // should revert to \cite[]{} int InsetCitation::latex(Buffer const & buffer, odocstream & os, - OutputParams const &) const + OutputParams const &) const { biblio::CiteEngine cite_engine = buffer.params().getEngine(); // FIXME UNICODE docstring const cite_str = from_utf8( - biblio::asValidLatexCommand(getCmdName(), cite_engine)); + asValidLatexCommand(getCmdName(), cite_engine)); os << "\\" << cite_str; @@ -457,14 +539,4 @@ void InsetCitation::validate(LaTeXFeatures & features) const } -void InsetCitation::replaceContents(string const & from, string const & to) -{ - if (tokenPos(getContents(), ',', from) != -1) { - vector items = getVectorFromString(getContents()); - replace(items.begin(), items.end(), from, to); - setContents(getStringFromVector(items)); - } -} - - } // namespace lyx