+#include "support/filetools.h"
+#include "support/lstrings.h"
+#include "support/path.h"
+#include "support/os.h"
+#include "support/lstrings.h"
+#include "support/LAssert.h"
+
+#include <map>
+
+using std::ostream;
+using std::vector;
+using std::map;
+
+namespace {
+
+// An optimisation. We assume that until the first InsetCitation::edit is
+// called, we're loading the buffer and that, therefore, we don't need to
+// reload the bibkey list
+std::map<Buffer const *, bool> loading_buffer;
+
+string const getNatbibLabel(Buffer const * buffer,
+ string const & citeType, string const & keyList,
+ string const & before, string const & after,
+ bool numerical)
+{
+ // Only reload the bibkeys if we have to...
+ map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
+ if (lit != loading_buffer.end())
+ loading_buffer[buffer] = true;
+
+ typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
+ static CachedMap cached_keys;
+
+ CachedMap::iterator kit = cached_keys.find(buffer);
+
+ if (!loading_buffer[buffer] || kit == cached_keys.end()) {
+ // build the keylist
+ typedef vector<std::pair<string, string> > InfoType;
+ InfoType bibkeys = buffer->getBibkeyList();
+
+ InfoType::const_iterator bit = bibkeys.begin();
+ InfoType::const_iterator bend = bibkeys.end();
+
+ biblio::InfoMap infomap;
+ for (; bit != bend; ++bit) {
+ infomap[bit->first] = bit->second;
+ }
+ if (infomap.empty())
+ return string();
+
+ cached_keys[buffer] = infomap;
+ }
+
+ biblio::InfoMap infomap = cached_keys[buffer];
+
+ // the natbib citation-styles
+ // CITET: author (year)
+ // CITEP: (author,year)
+ // CITEALT: author year
+ // CITEALP: author, year
+ // CITEAUTHOR: author
+ // CITEYEAR: year
+ // CITEYEARPAR: (year)
+
+ // We don't currently use the full or forceUCase fields.
+ // bool const forceUCase = citeType[0] == 'C';
+ bool const full = citeType[citeType.size() - 1] == '*';
+
+ string const cite_type = full ?
+ ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
+ ascii_lowercase(citeType);
+
+ string before_str;
+ if (!before.empty()) {
+ // In CITET and CITEALT mode, the "before" string is
+ // attached to the label associated with each and every key.
+ // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
+ // to the front of the whole only.
+ // In other modes, it is not used at all.
+ if (cite_type == "citet" ||
+ cite_type == "citealt" ||
+ cite_type == "citep" ||
+ cite_type == "citealp" ||
+ cite_type == "citeyearpar")
+ before_str = before + ' ';
+ }
+
+ string after_str;
+ if (!after.empty()) {
+ // The "after" key is appended only to the end of the whole.
+ 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 = ";";
+
+ string const op_str(string(1, ' ') + string(1, op));
+ string const cp_str(string(1, cp) + string(1, ' '));
+ string const sep_str(string(sep) + " ");
+
+ string label;
+ vector<string> keys = getVectorFromString(keyList);
+ vector<string>::const_iterator it = keys.begin();
+ vector<string>::const_iterator end = keys.end();
+ for (; it != end; ++it) {
+ // get the bibdata corresponding to the key
+ string const author(biblio::getAbbreviatedAuthor(infomap, *it));
+ string const year(biblio::getYear(infomap, *it));
+
+ // Something isn't right. Fail safely.
+ if (author.empty() || year.empty())
+ return string();
+
+ // (authors1 (<before> year); ... ;
+ // authors_last (<before> year, <after>)
+ if (cite_type == "citet") {
+ string const tmp = numerical ? '#' + *it : year;
+ label += author + op_str + before_str + tmp +
+ cp + sep_str;
+
+ // author, year; author, year; ...
+ } else if (cite_type == "citep" ||
+ cite_type == "citealp") {
+ if (numerical) {
+ label += *it + sep_str;
+ } else {
+ label += author + ", " + year + sep_str;
+ }
+
+ // (authors1 <before> year;
+ // authors_last <before> year, <after>)
+ } else if (cite_type == "citealt") {
+ string const tmp = numerical ? '#' + *it : year;
+ label += author + ' ' + before_str + tmp + sep_str;
+
+ // author; author; ...
+ } else if (cite_type == "citeauthor") {
+ label += author + sep_str;
+
+ // year; year; ...
+ } else if (cite_type == "citeyear" ||
+ cite_type == "citeyearpar") {
+ label += year + sep_str;
+ }
+ }
+ label = rtrim(rtrim(label), sep);
+
+ if (!after_str.empty()) {
+ if (cite_type == "citet") {
+ // insert "after" before last ')'
+ label.insert(label.size() - 1, after_str);
+ } else {
+ bool const add = !(numerical &&
+ (cite_type == "citeauthor" ||
+ cite_type == "citeyear"));
+ if (add)
+ label += after_str;
+ }
+ }
+
+ if (!before_str.empty() && (cite_type == "citep" ||
+ cite_type == "citealp" ||
+ cite_type == "citeyearpar")) {
+ label = before_str + label;
+ }
+
+ if (cite_type == "citep" || cite_type == "citeyearpar")
+ label = string(1, op) + label + string(1, cp);
+
+ return label;
+}
+
+
+string const getBasicLabel(string const & keyList, string const & after)
+{
+ string keys(keyList);
+ string label;
+
+ if (contains(keys, ",")) {
+ // Final comma allows while loop to cover all keys
+ keys = ltrim(split(keys, label, ',')) + ",";
+ while (contains(keys, ",")) {
+ string key;
+ keys = ltrim(split(keys, key, ','));
+ label += ", " + key;
+ }
+ } else
+ label = keys;
+
+ if (!after.empty())
+ label += ", " + after;
+
+ return "[" + label + "]";
+}
+
+} // anon namespace
+
+
+InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
+ : InsetCommand(p)
+{}
+
+
+string const InsetCitation::generateLabel(Buffer const * buffer) const