X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2FInsetCitation.cpp;h=d899d929e0222d8f1ecb3d0885ce4e0ebbf179d8;hb=b5153b3d2689abffebb2f4c97b3fb781dadf0c3c;hp=020e2b36891ebd2c7f38606d321a3eaa9bd02d0b;hpb=887bd847c6bc413531c388dbc2fcb26411d2266c;p=lyx.git diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index 020e2b3689..d899d929e0 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -15,44 +15,29 @@ #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 "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::FileName; -using support::getStringFromVector; -using support::getVectorFromString; -using support::ltrim; -using support::prefixIs; -using support::rtrim; -using support::split; -using support::tokenPos; - -using std::endl; -using std::string; -using std::vector; - -namespace fs = boost::filesystem; - - 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", @@ -75,14 +60,7 @@ vector const & possible_cite_commands() } -bool is_possible_cite_command(string const & input) -{ - vector const & possibles = possible_cite_commands(); - vector::const_iterator const end = possibles.end(); - return std::find(possibles.begin(), end, input) != end; -} - - +//FIXME See the header for the issue. string const default_cite_command(biblio::CiteEngine engine) { string str; @@ -108,13 +86,13 @@ string const asValidLatexCommand(string const & input, biblio::CiteEngine const engine) { string const default_str = default_cite_command(engine); - if (!is_possible_cite_command(input)) + if (!InsetCitation::isCompatibleCommand(input)) return default_str; string output; switch (engine) { case biblio::ENGINE_BASIC: - output = default_str; + output = input; break; case biblio::ENGINE_NATBIB_AUTHORYEAR: @@ -128,63 +106,54 @@ string const 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; + 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); + // 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; - } + break; + } } return output; } -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) { // 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; 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; } } @@ -192,9 +161,10 @@ docstring const getNatbibLabel(Buffer const & buffer, if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) { biblist.fillWithBibKeys(&buffer); cached_keys[&buffer] = biblist; - } else + } else { // use the cached keys biblist = cached_keys[&buffer]; + } if (biblist.empty()) return docstring(); @@ -240,20 +210,29 @@ 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); @@ -270,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, ) @@ -355,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; @@ -364,9 +352,7 @@ docstring const getNatbibLabel(Buffer const & buffer, docstring const getBasicLabel(docstring const & keyList, docstring const & after) { - using support::contains; - - docstring keys(keyList); + docstring keys = keyList; docstring label; if (contains(keys, ',')) { @@ -394,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"); @@ -401,15 +409,12 @@ 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()) { + if (label.empty()) label = getBasicLabel(getParam("key"), after); - } return label; } @@ -478,9 +483,9 @@ static docstring const cleanupWhitespace(docstring const & citelist) int InsetCitation::docbook(Buffer const &, odocstream & os, OutputParams const &) const { - os << "" + os << from_ascii("") << cleanupWhitespace(getParam("key")) - << ""; + << from_ascii(""); return 0; } @@ -535,14 +540,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()); - std::replace(items.begin(), items.end(), from, to); - setContents(getStringFromVector(items)); - } -} - - } // namespace lyx