2002-04-24 10:00:39 +00:00
|
|
|
/**
|
|
|
|
* \file insetcite.C
|
2002-09-25 14:26:13 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2000-07-07 07:46:37 +00:00
|
|
|
*
|
2002-09-25 14:26:13 +00:00
|
|
|
* \author Angus Leeming
|
|
|
|
* \author Herbert Voss
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2002-04-24 10:00:39 +00:00
|
|
|
*/
|
2000-06-07 08:53:40 +00:00
|
|
|
|
2000-07-07 07:46:37 +00:00
|
|
|
#include <config.h>
|
2000-06-07 08:53:40 +00:00
|
|
|
|
|
|
|
#include "insetcite.h"
|
2003-05-16 07:44:00 +00:00
|
|
|
#include "funcrequest.h"
|
2001-07-19 14:12:37 +00:00
|
|
|
#include "buffer.h"
|
2000-07-14 07:52:03 +00:00
|
|
|
#include "BufferView.h"
|
2001-07-19 14:12:37 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
#include "frontends/controllers/biblio.h"
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2000-07-19 08:37:26 +00:00
|
|
|
#include "support/lstrings.h"
|
2000-07-15 23:51:46 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
#include <map>
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
using std::ostream;
|
2002-04-24 10:00:39 +00:00
|
|
|
using std::vector;
|
|
|
|
using std::map;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
namespace {
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
// An optimisation. We assume that until the first InsetCitation::edit is
|
2002-08-02 16:30:58 +00:00
|
|
|
// called, we're loading the buffer and that, therefore, we don't need to
|
2002-04-24 10:00:39 +00:00
|
|
|
// reload the bibkey list
|
|
|
|
std::map<Buffer const *, bool> loading_buffer;
|
2000-07-15 23:51:46 +00:00
|
|
|
|
2002-07-28 18:13:51 +00:00
|
|
|
string const getNatbibLabel(Buffer const * buffer,
|
2002-04-24 10:00:39 +00:00
|
|
|
string const & citeType, string const & keyList,
|
|
|
|
string const & before, string const & after,
|
|
|
|
bool numerical)
|
2000-06-07 08:53:40 +00:00
|
|
|
{
|
2002-04-24 10:00:39 +00:00
|
|
|
typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
|
|
|
|
static CachedMap cached_keys;
|
|
|
|
|
2002-10-11 12:18:55 +00:00
|
|
|
// Only load the bibkeys once if we're loading up the buffer,
|
|
|
|
// else load them afresh each time.
|
|
|
|
map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
|
|
|
|
if (lit == loading_buffer.end())
|
|
|
|
loading_buffer[buffer] = true;
|
|
|
|
|
|
|
|
bool loadkeys = !loading_buffer[buffer];
|
|
|
|
if (!loadkeys) {
|
|
|
|
CachedMap::iterator kit = cached_keys.find(buffer);
|
|
|
|
loadkeys = kit == cached_keys.end();
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2002-10-11 12:18:55 +00:00
|
|
|
if (loadkeys) {
|
2002-04-24 10:00:39 +00:00
|
|
|
// build the keylist
|
|
|
|
typedef vector<std::pair<string, string> > InfoType;
|
2003-02-18 13:25:18 +00:00
|
|
|
InfoType bibkeys;
|
|
|
|
buffer->fillWithBibKeys(bibkeys);
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
InfoType::const_iterator bit = bibkeys.begin();
|
|
|
|
InfoType::const_iterator bend = bibkeys.end();
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
biblio::InfoMap infomap;
|
|
|
|
for (; bit != bend; ++bit) {
|
|
|
|
infomap[bit->first] = bit->second;
|
|
|
|
}
|
|
|
|
if (infomap.empty())
|
2002-10-11 12:18:55 +00:00
|
|
|
return string();
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
cached_keys[buffer] = infomap;
|
|
|
|
}
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
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)
|
|
|
|
|
2002-05-14 09:47:12 +00:00
|
|
|
// We don't currently use the full or forceUCase fields.
|
|
|
|
// bool const forceUCase = citeType[0] == 'C';
|
2002-07-28 18:13:51 +00:00
|
|
|
bool const full = citeType[citeType.size() - 1] == '*';
|
2002-05-14 09:47:12 +00:00
|
|
|
|
|
|
|
string const cite_type = full ?
|
2002-07-28 18:13:51 +00:00
|
|
|
ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
|
2002-07-16 21:17:10 +00:00
|
|
|
ascii_lowercase(citeType);
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
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.
|
2002-05-14 09:47:12 +00:00
|
|
|
if (cite_type == "citet" ||
|
|
|
|
cite_type == "citealt" ||
|
|
|
|
cite_type == "citep" ||
|
2002-07-28 18:13:51 +00:00
|
|
|
cite_type == "citealp" ||
|
2002-05-14 09:47:12 +00:00
|
|
|
cite_type == "citeyearpar")
|
2002-04-24 10:00:39 +00:00
|
|
|
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.
|
2002-07-28 18:13:51 +00:00
|
|
|
// puctuation mark separating citation entries.
|
|
|
|
char const * const sep = ";";
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
string const op_str(' ' + string(1, op));
|
|
|
|
string const cp_str(string(1, cp) + ' ');
|
|
|
|
string const sep_str(string(sep) + ' ');
|
2000-07-19 08:37:26 +00:00
|
|
|
|
|
|
|
string label;
|
2002-04-24 10:00:39 +00:00
|
|
|
vector<string> keys = getVectorFromString(keyList);
|
|
|
|
vector<string>::const_iterator it = keys.begin();
|
|
|
|
vector<string>::const_iterator end = keys.end();
|
2002-07-28 18:13:51 +00:00
|
|
|
for (; it != end; ++it) {
|
2002-04-24 10:00:39 +00:00
|
|
|
// 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>)
|
2002-05-14 09:47:12 +00:00
|
|
|
if (cite_type == "citet") {
|
2002-04-24 10:00:39 +00:00
|
|
|
string const tmp = numerical ? '#' + *it : year;
|
|
|
|
label += author + op_str + before_str + tmp +
|
|
|
|
cp + sep_str;
|
|
|
|
|
|
|
|
// author, year; author, year; ...
|
2002-05-14 09:47:12 +00:00
|
|
|
} else if (cite_type == "citep" ||
|
|
|
|
cite_type == "citealp") {
|
2002-04-24 10:00:39 +00:00
|
|
|
if (numerical) {
|
|
|
|
label += *it + sep_str;
|
|
|
|
} else {
|
|
|
|
label += author + ", " + year + sep_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (authors1 <before> year;
|
|
|
|
// authors_last <before> year, <after>)
|
2002-05-14 09:47:12 +00:00
|
|
|
} else if (cite_type == "citealt") {
|
2002-04-24 10:00:39 +00:00
|
|
|
string const tmp = numerical ? '#' + *it : year;
|
|
|
|
label += author + ' ' + before_str + tmp + sep_str;
|
|
|
|
|
|
|
|
// author; author; ...
|
2002-05-14 09:47:12 +00:00
|
|
|
} else if (cite_type == "citeauthor") {
|
2002-04-24 10:00:39 +00:00
|
|
|
label += author + sep_str;
|
|
|
|
|
|
|
|
// year; year; ...
|
2002-05-14 09:47:12 +00:00
|
|
|
} else if (cite_type == "citeyear" ||
|
|
|
|
cite_type == "citeyearpar") {
|
2002-04-24 10:00:39 +00:00
|
|
|
label += year + sep_str;
|
|
|
|
}
|
|
|
|
}
|
2002-07-28 22:50:13 +00:00
|
|
|
label = rtrim(rtrim(label), sep);
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
if (!after_str.empty()) {
|
2002-05-14 09:47:12 +00:00
|
|
|
if (cite_type == "citet") {
|
2002-04-24 10:00:39 +00:00
|
|
|
// insert "after" before last ')'
|
2002-07-28 18:13:51 +00:00
|
|
|
label.insert(label.size() - 1, after_str);
|
2002-04-24 10:00:39 +00:00
|
|
|
} else {
|
|
|
|
bool const add = !(numerical &&
|
2002-05-14 09:47:12 +00:00
|
|
|
(cite_type == "citeauthor" ||
|
|
|
|
cite_type == "citeyear"));
|
2002-04-24 10:00:39 +00:00
|
|
|
if (add)
|
|
|
|
label += after_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-14 09:47:12 +00:00
|
|
|
if (!before_str.empty() && (cite_type == "citep" ||
|
2002-07-28 18:13:51 +00:00
|
|
|
cite_type == "citealp" ||
|
2002-05-14 09:47:12 +00:00
|
|
|
cite_type == "citeyearpar")) {
|
2002-04-24 10:00:39 +00:00
|
|
|
label = before_str + label;
|
|
|
|
}
|
|
|
|
|
2002-05-14 09:47:12 +00:00
|
|
|
if (cite_type == "citep" || cite_type == "citeyearpar")
|
2002-04-24 10:00:39 +00:00
|
|
|
label = string(1, op) + label + string(1, cp);
|
|
|
|
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const getBasicLabel(string const & keyList, string const & after)
|
|
|
|
{
|
|
|
|
string keys(keyList);
|
|
|
|
string label;
|
|
|
|
|
2000-09-27 18:13:30 +00:00
|
|
|
if (contains(keys, ",")) {
|
2000-07-19 08:37:26 +00:00
|
|
|
// Final comma allows while loop to cover all keys
|
2002-11-27 10:30:28 +00:00
|
|
|
keys = ltrim(split(keys, label, ',')) + ',';
|
2002-02-16 15:59:55 +00:00
|
|
|
while (contains(keys, ",")) {
|
2000-07-19 08:37:26 +00:00
|
|
|
string key;
|
2002-07-28 22:50:13 +00:00
|
|
|
keys = ltrim(split(keys, key, ','));
|
2000-07-19 08:37:26 +00:00
|
|
|
label += ", " + key;
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
} else
|
2000-07-19 08:37:26 +00:00
|
|
|
label = keys;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
if (!after.empty())
|
|
|
|
label += ", " + after;
|
2002-07-28 18:13:51 +00:00
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
return '[' + label + ']';
|
2002-04-24 10:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
|
2003-05-26 09:13:55 +00:00
|
|
|
InsetCitation::InsetCitation(InsetCommandParams const & p)
|
2002-04-24 10:00:39 +00:00
|
|
|
: InsetCommand(p)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-05-26 09:13:55 +00:00
|
|
|
// InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
|
|
|
|
// : InsetCommand(p, false)
|
|
|
|
// {}
|
|
|
|
|
|
|
|
|
2003-02-25 14:51:38 +00:00
|
|
|
InsetCitation::~InsetCitation()
|
|
|
|
{
|
|
|
|
InsetCommandMailer mailer("citation", *this);
|
|
|
|
mailer.hideDialog();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
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";
|
|
|
|
}
|
2002-07-28 18:13:51 +00:00
|
|
|
label = getNatbibLabel(buffer, cmd, getContents(),
|
2002-04-24 10:00:39 +00:00
|
|
|
before, after,
|
|
|
|
buffer->params.use_numerical_citations);
|
2000-06-07 08:53:40 +00:00
|
|
|
}
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
// Fallback to fail-safe
|
|
|
|
if (label.empty()) {
|
|
|
|
label = getBasicLabel(getContents(), after);
|
|
|
|
}
|
2000-07-19 08:37:26 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
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;
|
2000-06-07 08:53:40 +00:00
|
|
|
}
|
2000-07-14 07:52:03 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2002-10-11 12:18:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-16 07:44:00 +00:00
|
|
|
dispatch_result InsetCitation::localDispatch(FuncRequest const & cmd)
|
2000-07-14 07:52:03 +00:00
|
|
|
{
|
2003-05-16 07:44:00 +00:00
|
|
|
switch (cmd.action) {
|
2003-06-11 22:02:03 +00:00
|
|
|
case LFUN_INSET_EDIT:
|
|
|
|
// A call to edit indicates that we're no longer loading the
|
|
|
|
// buffer but doing some real work.
|
|
|
|
setLoadingBuffer(cmd.view()->buffer(), false);
|
|
|
|
InsetCommandMailer("citation", *this).showDialog(cmd.view());
|
2003-06-30 09:10:49 +00:00
|
|
|
return DISPATCHED;
|
2003-06-11 22:02:03 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return InsetCommand::localDispatch(cmd);
|
2003-05-16 07:44:00 +00:00
|
|
|
}
|
2001-07-20 14:18:48 +00:00
|
|
|
}
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
|
2001-05-27 17:51:16 +00:00
|
|
|
{
|
2002-04-24 10:00:39 +00:00
|
|
|
if (cache.params == params() && cache.style == getStyle(buffer))
|
2003-06-30 09:10:49 +00:00
|
|
|
os << cache.generated_label;
|
2002-04-24 10:00:39 +00:00
|
|
|
else
|
2003-06-30 09:10:49 +00:00
|
|
|
os << generateLabel(buffer);
|
2002-03-21 17:09:55 +00:00
|
|
|
return 0;
|
2001-05-27 17:51:16 +00:00
|
|
|
}
|
2001-07-19 14:12:37 +00:00
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
2001-07-19 14:12:37 +00:00
|
|
|
// 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[]{}
|
2003-05-22 22:44:30 +00:00
|
|
|
int InsetCitation::latex(Buffer const * buffer, ostream & os,
|
2003-05-23 08:59:47 +00:00
|
|
|
LatexRunParams const &) const
|
2001-07-19 14:12:37 +00:00
|
|
|
{
|
|
|
|
os << "\\";
|
|
|
|
if (buffer->params.use_natbib)
|
|
|
|
os << getCmdName();
|
|
|
|
else
|
|
|
|
os << "cite";
|
2002-07-28 18:13:51 +00:00
|
|
|
|
|
|
|
#warning What is this code supposed to do? (Lgb)
|
2002-08-27 20:30:20 +00:00
|
|
|
// my guess is that this is just waiting for when we support before,
|
|
|
|
// so it's a oneliner. But this is very silly ! - jbl
|
2002-07-28 18:13:51 +00:00
|
|
|
|
|
|
|
#if 1
|
|
|
|
// The current strange code
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
string const before = string();
|
|
|
|
string const after = getOptions();
|
|
|
|
if (!before.empty() && buffer->params.use_natbib)
|
2002-11-27 10:30:28 +00:00
|
|
|
os << '[' << before << "][" << after << ']';
|
2002-04-24 10:00:39 +00:00
|
|
|
else if (!after.empty())
|
2002-11-27 10:30:28 +00:00
|
|
|
os << '[' << after << ']';
|
2002-07-28 18:13:51 +00:00
|
|
|
#else
|
|
|
|
// and the cleaned up equvalent, should it just be changed? (Lgb)
|
|
|
|
string const after = getOptions();
|
|
|
|
if (!after.empty())
|
2002-11-27 10:30:28 +00:00
|
|
|
os << '[' << after << ']';
|
2002-07-28 18:13:51 +00:00
|
|
|
#endif
|
2002-04-24 10:00:39 +00:00
|
|
|
string::const_iterator it = getContents().begin();
|
|
|
|
string::const_iterator end = getContents().end();
|
2001-08-01 15:26:04 +00:00
|
|
|
// Paranoia check: make sure that there is no whitespace in here
|
|
|
|
string content;
|
2002-08-21 09:14:16 +00:00
|
|
|
char last = ',';
|
2002-04-24 10:00:39 +00:00
|
|
|
for (; it != end; ++it) {
|
2002-08-21 09:14:16 +00:00
|
|
|
if (*it != ' ')
|
|
|
|
last = *it;
|
|
|
|
if (*it != ' ' || last != ',')
|
|
|
|
content += *it;
|
2001-08-01 15:26:04 +00:00
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
os << '{' << content << '}';
|
2001-07-19 14:12:37 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetCitation::validate(LaTeXFeatures & features) const
|
|
|
|
{
|
2001-07-28 12:24:16 +00:00
|
|
|
if (features.bufferParams().use_natbib)
|
2001-11-19 15:34:11 +00:00
|
|
|
features.require("natbib");
|
2001-07-19 14:12:37 +00:00
|
|
|
}
|