+
+string const InsetCitation::generateLabel(Buffer const * buffer) const
+{
+ string const before = string();
+ string const after = getOptions();
+
+ string label;
+ if (buffer->params.use_natbib) {
+ string cmd = getCmdName();
+ if (cmd == "cite") {
+ // We may be "upgrading" from an older LyX version.
+ // If, however, we use "cite" because the necessary
+ // author/year info is not present in the biblio
+ // database, then getNatbibLabel will exit gracefully
+ // and we'll call getBasicLabel.
+ if (buffer->params.use_numerical_citations)
+ cmd = "citep";
+ else
+ cmd = "citet";
+ }
+ label = getNatbibLabel(buffer, cmd, getContents(),
+ before, after,
+ buffer->params.use_numerical_citations);
+ }
+
+ // Fallback to fail-safe
+ if (label.empty()) {
+ label = getBasicLabel(getContents(), after);
+ }
+
+ return label;
+}
+
+
+InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
+{
+ Cache::Style style = Cache::BASIC;
+
+ if (buffer->params.use_natbib) {
+ if (buffer->params.use_numerical_citations) {
+ style = Cache::NATBIB_NUM;
+ } else {
+ style = Cache::NATBIB_AY;
+ }
+ }
+
+ return style;
+}
+
+
+string const InsetCitation::getScreenLabel(Buffer const * buffer) const
+{
+ Cache::Style const style = getStyle(buffer);
+ if (cache.params == params() && cache.style == style)
+ return cache.screen_label;
+
+ // The label has changed, so we have to re-create it.
+ string const before = string();
+ string const after = getOptions();
+
+ string const glabel = generateLabel(buffer);
+
+ unsigned int const maxLabelChars = 45;
+
+ string label = glabel;
+ if (label.size() > maxLabelChars) {
+ label.erase(maxLabelChars-3);
+ label += "...";
+ }
+
+ cache.style = style;
+ cache.params = params();
+ cache.generated_label = glabel;
+ cache.screen_label = label;
+
+ return label;
+}
+
+
+void InsetCitation::setLoadingBuffer(Buffer const * buffer, bool state) const
+{
+ // Doesn't matter if there is no bv->buffer() entry in the map.
+ loading_buffer[buffer] = state;
+}
+
+
+void InsetCitation::edit(BufferView * bv, int, int, mouse_button::state)
+{
+ // A call to edit() indicates that we're no longer loading the
+ // buffer but doing some real work.
+ setLoadingBuffer(bv->buffer(), false);
+
+ InsetCommandMailer mailer("citation", *this);
+ mailer.showDialog(bv);
+}
+
+
+void InsetCitation::edit(BufferView * bv, bool)
+{
+ edit(bv, 0, 0, mouse_button::none);
+}
+
+
+int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
+{
+ string label;
+
+ if (cache.params == params() && cache.style == getStyle(buffer))
+ label = cache.generated_label;
+ else
+ label = generateLabel(buffer);
+
+ os << label;
+ return 0;
+}
+
+
+// Have to overwrite the default InsetCommand method in order to check that
+// the \cite command is valid. Eg, the user has natbib enabled, inputs some
+// citations and then changes his mind, turning natbib support off. The output
+// should revert to \cite[]{}
+int InsetCitation::latex(Buffer const * buffer, ostream & os,
+ bool /*fragile*/, bool/*fs*/) const
+{
+ os << "\\";
+ if (buffer->params.use_natbib)
+ os << getCmdName();
+ else
+ os << "cite";
+
+#warning What is this code supposed to do? (Lgb)
+// my guess is that this is just waiting for when we support before,
+// so it's a oneliner. But this is very silly ! - jbl
+
+#if 1
+ // The current strange code
+
+ string const before = string();
+ string const after = getOptions();
+ if (!before.empty() && buffer->params.use_natbib)
+ os << '[' << before << "][" << after << ']';
+ else if (!after.empty())
+ os << '[' << after << ']';
+#else
+ // and the cleaned up equvalent, should it just be changed? (Lgb)
+ string const after = getOptions();
+ if (!after.empty())
+ os << '[' << after << ']';
+#endif
+ string::const_iterator it = getContents().begin();
+ string::const_iterator end = getContents().end();
+ // Paranoia check: make sure that there is no whitespace in here
+ string content;
+ char last = ',';
+ for (; it != end; ++it) {
+ if (*it != ' ')
+ last = *it;
+ if (*it != ' ' || last != ',')
+ content += *it;
+ }
+
+ os << '{' << content << '}';
+
+ return 0;
+}
+
+
+void InsetCitation::validate(LaTeXFeatures & features) const
+{
+ if (features.bufferParams().use_natbib)
+ features.require("natbib");
+}