#include "FuncRequest.h"
#include "LaTeXFeatures.h"
-#include "frontends/controllers/frontend_helpers.h"
-
-#include "support/fs_extras.h"
#include "support/lstrings.h"
#include <algorithm>
namespace lyx {
-using support::ascii_lowercase;
-using support::contains;
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::replace;
using std::string;
-using std::ostream;
using std::vector;
-using std::map;
namespace fs = boost::filesystem;
namespace {
+vector<string> const init_possible_cite_commands()
+{
+ char const * const possible[] = {
+ "cite", "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<string>(possible, possible + size_possible);
+}
+
+
+vector<string> const & possible_cite_commands()
+{
+ static vector<string> const possible = init_possible_cite_commands();
+ return possible;
+}
+
+
+//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;
+}
+
+
+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 = default_str;
+ 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;
+ }
+ }
+
+ return output;
+}
+
+
docstring const getNatbibLabel(Buffer const & buffer,
- string const & citeType, string const & keyList,
+ 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<Buffer const *, biblio::InfoMap> CachedMap;
+ typedef std::map<Buffer const *, BiblioInfo> CachedMap;
static CachedMap cached_keys;
// and cache the timestamp of the bibliography files.
static std::map<FileName, time_t> bibfileStatus;
- biblio::InfoMap infomap;
+ BiblioInfo biblist;
vector<FileName> const & bibfilesCache = buffer.getBibfilesCache();
// compare the cached timestamps with the actual ones.
it != bibfilesCache.end(); ++ it) {
FileName const f = *it;
try {
- std::time_t lastw = fs::last_write_time(f.toFilesystemEncoding());
+ std::time_t lastw = f.lastModified();
if (lastw != bibfileStatus[f]) {
changed = true;
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<std::pair<string, docstring> > 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
// CITE: author/<before field>
// 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;
// puctuation mark separating citation entries.
char const * const 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<string> keys = getVectorFromString(keyList);
- vector<string>::const_iterator it = keys.begin();
- vector<string>::const_iterator end = keys.end();
+ vector<docstring> keys = getVectorFromString(keyList);
+ vector<docstring>::const_iterator it = keys.begin();
+ vector<docstring>::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())
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 +
} 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;
}
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 + ' ' +
docstring const getBasicLabel(docstring const & keyList, docstring const & after)
{
- docstring keys(keyList);
+ using support::contains;
+
+ docstring keys = keyList;
docstring label;
if (contains(keys, ',')) {
{}
+CommandInfo const * InsetCitation::findInfo(std::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(std::string const & cmd)
+{
+ vector<string> const & possibles = possible_cite_commands();
+ vector<string>::const_iterator const end = possibles.end();
+ return std::find(possibles.begin(), end, cmd) != end;
+}
+
+
docstring const InsetCitation::generateLabel(Buffer const & buffer) const
{
docstring const before = getParam("before");
docstring label;
biblio::CiteEngine const engine = buffer.params().getEngine();
if (engine != biblio::ENGINE_BASIC) {
- // FIXME UNICODE
- label = getNatbibLabel(buffer, getCmdName(), to_utf8(getParam("key")),
+ label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
before, after, engine);
}
}
-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();
return result;
}
-// end anon namyspace
-}
int InsetCitation::docbook(Buffer const &, odocstream & os,
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;
}
-void InsetCitation::replaceContents(string const & from, string const & to)
-{
- if (tokenPos(getContents(), ',', from) != -1) {
- vector<string> items = getVectorFromString(getContents());
- replace(items.begin(), items.end(), from, to);
- setContents(getStringFromVector(items));
- }
-}
-
-
} // namespace lyx