2002-04-24 10:00:39 +00:00
|
|
|
|
/**
|
2007-04-25 16:11:45 +00:00
|
|
|
|
* \file InsetCitation.cpp
|
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
|
2003-09-07 01:45:40 +00:00
|
|
|
|
* \author Herbert Vo<EFBFBD>
|
2002-09-25 14:26:13 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* 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
|
|
|
|
|
2007-04-25 16:11:45 +00:00
|
|
|
|
#include "InsetCitation.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Buffer.h"
|
|
|
|
|
#include "BufferParams.h"
|
|
|
|
|
#include "DispatchResult.h"
|
2007-12-20 15:46:14 +00:00
|
|
|
|
#include "support/gettext.h"
|
2008-01-08 18:55:34 +00:00
|
|
|
|
#include "EmbeddedFiles.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "FuncRequest.h"
|
2001-07-19 14:12:37 +00:00
|
|
|
|
#include "LaTeXFeatures.h"
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
|
#include "support/debug.h"
|
2007-11-28 22:12:03 +00:00
|
|
|
|
#include "support/docstream.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2000-07-15 23:51:46 +00:00
|
|
|
|
|
2006-10-09 14:21:11 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
namespace {
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
|
vector<string> const init_possible_cite_commands()
|
|
|
|
|
{
|
2007-09-11 22:31:24 +00:00
|
|
|
|
char const * const possible[] = {
|
2007-12-20 15:46:14 +00:00
|
|
|
|
"cite", "nocite", "citet", "citep", "citealt", "citealp",
|
2007-08-20 16:30:02 +00:00
|
|
|
|
"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*"
|
|
|
|
|
};
|
2007-09-11 22:31:24 +00:00
|
|
|
|
size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
|
2007-08-20 16:30:02 +00:00
|
|
|
|
|
2007-09-11 22:31:24 +00:00
|
|
|
|
return vector<string>(possible, possible + size_possible);
|
2007-08-20 16:30:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vector<string> const & possible_cite_commands()
|
|
|
|
|
{
|
2007-09-11 22:31:24 +00:00
|
|
|
|
static vector<string> const possible = init_possible_cite_commands();
|
|
|
|
|
return possible;
|
2007-08-20 16:30:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-10-25 04:13:56 +00:00
|
|
|
|
//FIXME See the header for the issue.
|
2007-08-20 16:30:02 +00:00
|
|
|
|
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);
|
2007-10-25 04:13:56 +00:00
|
|
|
|
if (!InsetCitation::isCompatibleCommand(input))
|
2007-08-20 16:30:02 +00:00
|
|
|
|
return default_str;
|
|
|
|
|
|
|
|
|
|
string output;
|
|
|
|
|
switch (engine) {
|
|
|
|
|
case biblio::ENGINE_BASIC:
|
2007-12-20 15:46:14 +00:00
|
|
|
|
output = input;
|
2007-08-20 16:30:02 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2007-09-20 06:22:07 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2007-08-20 16:30:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-20 15:46:14 +00:00
|
|
|
|
docstring const getComplexLabel(Buffer const & buffer,
|
2007-08-20 16:30:02 +00:00
|
|
|
|
string const & citeType, docstring const & keyList,
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring const & before, docstring const & after,
|
2004-05-13 20:44:35 +00:00
|
|
|
|
biblio::CiteEngine engine)
|
2000-06-07 08:53:40 +00:00
|
|
|
|
{
|
2003-10-22 13:15:18 +00:00
|
|
|
|
// Only start the process off after the buffer is loaded from file.
|
2007-10-20 10:03:45 +00:00
|
|
|
|
if (!buffer.isFullyLoaded())
|
2006-12-17 10:52:04 +00:00
|
|
|
|
return docstring();
|
2003-10-22 13:15:18 +00:00
|
|
|
|
|
2006-04-15 11:46:17 +00:00
|
|
|
|
// Cache the labels
|
2007-12-12 19:28:07 +00:00
|
|
|
|
typedef map<Buffer const *, BiblioInfo> CachedMap;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
static CachedMap cached_keys;
|
|
|
|
|
|
2006-04-15 11:46:17 +00:00
|
|
|
|
// and cache the timestamp of the bibliography files.
|
2007-12-12 19:28:07 +00:00
|
|
|
|
static map<FileName, time_t> bibfileStatus;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
|
BiblioInfo biblist;
|
2006-04-15 11:46:17 +00:00
|
|
|
|
|
2008-01-08 18:55:34 +00:00
|
|
|
|
EmbeddedFileList const & bibfilesCache = buffer.getBibfilesCache();
|
2006-04-15 11:46:17 +00:00
|
|
|
|
// compare the cached timestamps with the actual ones.
|
|
|
|
|
bool changed = false;
|
2008-01-08 18:55:34 +00:00
|
|
|
|
for (EmbeddedFileList::const_iterator it = bibfilesCache.begin();
|
2006-04-15 11:46:17 +00:00
|
|
|
|
it != bibfilesCache.end(); ++ it) {
|
2006-12-01 20:09:08 +00:00
|
|
|
|
FileName const f = *it;
|
2007-12-12 19:28:07 +00:00
|
|
|
|
time_t lastw = f.lastModified();
|
2007-11-08 00:09:58 +00:00
|
|
|
|
if (lastw != bibfileStatus[f]) {
|
2006-04-15 11:46:17 +00:00
|
|
|
|
changed = true;
|
2007-11-08 00:09:58 +00:00
|
|
|
|
bibfileStatus[f] = lastw;
|
2006-04-15 11:46:17 +00:00
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
}
|
2006-04-15 11:46:17 +00:00
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
|
// build the list only if the bibfiles have been changed
|
2007-01-08 17:38:39 +00:00
|
|
|
|
if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
|
2007-08-20 16:30:02 +00:00
|
|
|
|
biblist.fillWithBibKeys(&buffer);
|
|
|
|
|
cached_keys[&buffer] = biblist;
|
2007-10-20 10:03:45 +00:00
|
|
|
|
} else {
|
2006-04-15 11:46:17 +00:00
|
|
|
|
// use the cached keys
|
2007-08-20 16:30:02 +00:00
|
|
|
|
biblist = cached_keys[&buffer];
|
2007-10-20 10:03:45 +00:00
|
|
|
|
}
|
2006-04-15 11:46:17 +00:00
|
|
|
|
|
2007-08-20 16:30:02 +00:00
|
|
|
|
if (biblist.empty())
|
2006-12-17 10:52:04 +00:00
|
|
|
|
return docstring();
|
2002-07-28 18:13:51 +00:00
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
// the natbib citation-styles
|
|
|
|
|
// CITET: author (year)
|
|
|
|
|
// CITEP: (author,year)
|
|
|
|
|
// CITEALT: author year
|
|
|
|
|
// CITEALP: author, year
|
|
|
|
|
// CITEAUTHOR: author
|
|
|
|
|
// CITEYEAR: year
|
|
|
|
|
// CITEYEARPAR: (year)
|
2004-03-07 14:33:17 +00:00
|
|
|
|
// jurabib supports these plus
|
|
|
|
|
// CITE: author/<before field>
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
2002-05-14 09:47:12 +00:00
|
|
|
|
// We don't currently use the full or forceUCase fields.
|
2007-08-20 16:30:02 +00:00
|
|
|
|
string cite_type = asValidLatexCommand(citeType, engine);
|
2004-05-14 15:27:13 +00:00
|
|
|
|
if (cite_type[0] == 'C')
|
2007-08-20 16:30:02 +00:00
|
|
|
|
//If we were going to use them, this would mean ForceUCase
|
2004-05-14 15:27:13 +00:00
|
|
|
|
cite_type = string(1, 'c') + cite_type.substr(1);
|
|
|
|
|
if (cite_type[cite_type.size() - 1] == '*')
|
2007-08-20 16:30:02 +00:00
|
|
|
|
//and this would mean FULL
|
2004-05-14 15:27:13 +00:00
|
|
|
|
cite_type = cite_type.substr(0, cite_type.size() - 1);
|
2002-07-28 18:13:51 +00:00
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring before_str;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
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 + ' ';
|
2004-03-07 14:33:17 +00:00
|
|
|
|
// In CITE (jurabib), the "before" string is used to attach
|
2004-04-03 08:37:12 +00:00
|
|
|
|
// the annotator (of legal texts) to the author(s) of the
|
|
|
|
|
// first reference.
|
2004-03-07 14:33:17 +00:00
|
|
|
|
else if (cite_type == "cite")
|
|
|
|
|
before_str = '/' + before;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring after_str;
|
2007-12-20 15:46:14 +00:00
|
|
|
|
// The "after" key is appended only to the end of the whole.
|
|
|
|
|
if (cite_type == "nocite")
|
|
|
|
|
after_str = " (" + _("not cited") + ')';
|
|
|
|
|
else if (!after.empty()) {
|
2002-04-24 10:00:39 +00:00
|
|
|
|
after_str = ", " + after;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// One day, these might be tunable (as they are in BibTeX).
|
2007-12-20 15:46:14 +00:00
|
|
|
|
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 = ";";
|
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
2007-10-20 10:03:45 +00:00
|
|
|
|
docstring const op_str = ' ' + docstring(1, op);
|
|
|
|
|
docstring const cp_str = docstring(1, cp) + ' ';
|
|
|
|
|
docstring const sep_str = from_ascii(sep) + ' ';
|
2000-07-19 08:37:26 +00:00
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring label;
|
2007-08-20 16:30:02 +00:00
|
|
|
|
vector<docstring> keys = getVectorFromString(keyList);
|
|
|
|
|
vector<docstring>::const_iterator it = keys.begin();
|
|
|
|
|
vector<docstring>::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
|
2007-08-20 16:30:02 +00:00
|
|
|
|
docstring const author(biblist.getAbbreviatedAuthor(*it));
|
|
|
|
|
docstring const year(biblist.getYear(*it));
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
|
|
// Something isn't right. Fail safely.
|
|
|
|
|
if (author.empty() || year.empty())
|
2006-12-17 10:52:04 +00:00
|
|
|
|
return docstring();
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
2004-03-07 14:33:17 +00:00
|
|
|
|
// authors1/<before>; ... ;
|
|
|
|
|
// authors_last, <after>
|
2007-12-20 15:46:14 +00:00
|
|
|
|
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;
|
2004-04-03 08:37:12 +00:00
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
// (authors1 (<before> year); ... ;
|
|
|
|
|
// authors_last (<before> year, <after>)
|
2004-03-07 14:33:17 +00:00
|
|
|
|
} else if (cite_type == "citet") {
|
2004-05-13 20:44:35 +00:00
|
|
|
|
switch (engine) {
|
|
|
|
|
case biblio::ENGINE_NATBIB_AUTHORYEAR:
|
|
|
|
|
label += author + op_str + before_str +
|
|
|
|
|
year + cp + sep_str;
|
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_NATBIB_NUMERICAL:
|
2007-08-20 16:30:02 +00:00
|
|
|
|
label += author + op_str + before_str + '#' + *it + cp + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_JURABIB:
|
|
|
|
|
label += before_str + author + op_str +
|
|
|
|
|
year + cp + sep_str;
|
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_BASIC:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
|
|
// author, year; author, year; ...
|
2002-05-14 09:47:12 +00:00
|
|
|
|
} else if (cite_type == "citep" ||
|
|
|
|
|
cite_type == "citealp") {
|
2004-05-13 20:44:35 +00:00
|
|
|
|
if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
|
2007-08-20 16:30:02 +00:00
|
|
|
|
label += *it + sep_str;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
} 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") {
|
2004-05-13 20:44:35 +00:00
|
|
|
|
switch (engine) {
|
|
|
|
|
case biblio::ENGINE_NATBIB_AUTHORYEAR:
|
|
|
|
|
label += author + ' ' + before_str +
|
|
|
|
|
year + sep_str;
|
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_NATBIB_NUMERICAL:
|
2007-08-20 16:30:02 +00:00
|
|
|
|
label += author + ' ' + before_str + '#' + *it + sep_str;
|
2004-05-13 20:44:35 +00:00
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_JURABIB:
|
|
|
|
|
label += before_str + author + ' ' +
|
|
|
|
|
year + sep_str;
|
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_BASIC:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
|
|
// 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 {
|
2004-05-13 20:44:35 +00:00
|
|
|
|
bool const add =
|
|
|
|
|
!(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
|
|
|
|
|
(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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-20 15:46:14 +00:00
|
|
|
|
if (cite_type == "citep" || cite_type == "citeyearpar" ||
|
|
|
|
|
(cite_type == "cite" && engine == biblio::ENGINE_BASIC) )
|
2006-12-17 10:52:04 +00:00
|
|
|
|
label = op + label + cp;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
|
|
return label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring const getBasicLabel(docstring const & keyList, docstring const & after)
|
2002-04-24 10:00:39 +00:00
|
|
|
|
{
|
2007-10-20 10:03:45 +00:00
|
|
|
|
docstring keys = keyList;
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring label;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
2004-02-01 12:46:14 +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, ',')) + ',';
|
2004-02-01 12:46:14 +00:00
|
|
|
|
while (contains(keys, ',')) {
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring 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
|
|
|
|
|
|
|
|
|
|
|
2008-02-23 22:01:02 +00:00
|
|
|
|
ParamInfo InsetCitation::param_info_;
|
|
|
|
|
|
|
|
|
|
|
2003-05-26 09:13:55 +00:00
|
|
|
|
InsetCitation::InsetCitation(InsetCommandParams const & p)
|
2003-12-11 15:23:15 +00:00
|
|
|
|
: InsetCommand(p, "citation")
|
2002-04-24 10:00:39 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
2008-02-23 22:01:02 +00:00
|
|
|
|
ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
|
2007-10-25 04:13:56 +00:00
|
|
|
|
{
|
|
|
|
|
// 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.
|
2008-02-23 22:01:02 +00:00
|
|
|
|
if (param_info_.empty()) {
|
Per Abdel's suggestion that we focus on bug-fixing at this point, this will be the last patch in this series for a bit. But I wanted to get this done before I forget what it is I was doing, so here it is.
The idea behind this patch is to make real key-value support for InsetCommand parameters possible. This should be particularly useful for the listings version of InsetInclude, though we would need some kind of UI for it before it would really be helpful. (See below for some thoughts.) This doesn't substantially change anything else, though some things do get re-arranged a bit.
Basically, the idea is this. First, we introduce a whole range of parameter types: Normal LaTeX optional and required parameters; ones for LyX's internal use (like embed); and finally, in connection with keyval, ones that represent keys and ones that represent optional and required arguments where the keyval stuff will appear. (I'm assuming here that there will always be exactly one of those, and that it will accept only keyval-type material.) The parameters themselves are stored in a map, so it's really only the output routines that need to care about the different types of parameters.
Regarding the frontend, it seems to me that something like the following would work:
(i) scan the parameter list for LATEX_KEY type parameters
(ii) the dialog will have a series of lines, each of which has a combo box listing the acceptable keys and a QLineEdit for entering its value, as well as a "delete" button of some sort for removing this key and its value
(iii) there should be an "add line" button to add a new line, activated only when all other lines are filled with values
Probably not even too hard.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23235 a592a061-630c-0410-9148-cb99ea01b6c8
2008-02-25 22:13:45 +00:00
|
|
|
|
param_info_.add("after", ParamInfo::LATEX_OPTIONAL);
|
|
|
|
|
param_info_.add("before", ParamInfo::LATEX_OPTIONAL);
|
|
|
|
|
param_info_.add("key", ParamInfo::LATEX_REQUIRED);
|
2008-02-23 22:01:02 +00:00
|
|
|
|
}
|
|
|
|
|
return param_info_;
|
2007-10-25 04:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
|
bool InsetCitation::isCompatibleCommand(string const & cmd)
|
2007-10-25 04:13:56 +00:00
|
|
|
|
{
|
|
|
|
|
vector<string> const & possibles = possible_cite_commands();
|
|
|
|
|
vector<string>::const_iterator const end = possibles.end();
|
2007-12-12 19:28:07 +00:00
|
|
|
|
return find(possibles.begin(), end, cmd) != end;
|
2007-10-25 04:13:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
|
docstring const InsetCitation::generateLabel(Buffer const & buffer) const
|
2002-04-24 10:00:39 +00:00
|
|
|
|
{
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring const before = getParam("before");
|
|
|
|
|
docstring const after = getParam("after");
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring label;
|
2007-03-25 01:12:38 +00:00
|
|
|
|
biblio::CiteEngine const engine = buffer.params().getEngine();
|
2007-12-20 15:46:14 +00:00
|
|
|
|
label = getComplexLabel(buffer, getCmdName(), getParam("key"),
|
|
|
|
|
before, after, engine);
|
2000-06-07 08:53:40 +00:00
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
// Fallback to fail-safe
|
2007-11-08 00:09:58 +00:00
|
|
|
|
if (label.empty())
|
2006-12-17 10:52:04 +00:00
|
|
|
|
label = getBasicLabel(getParam("key"), after);
|
2000-07-19 08:37:26 +00:00
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
|
return label;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
|
docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const
|
2002-04-24 10:00:39 +00:00
|
|
|
|
{
|
2007-03-22 07:22:16 +00:00
|
|
|
|
biblio::CiteEngine const engine = buffer.params().getEngine();
|
2004-05-10 20:55:00 +00:00
|
|
|
|
if (cache.params == params() && cache.engine == engine)
|
2002-04-24 10:00:39 +00:00
|
|
|
|
return cache.screen_label;
|
|
|
|
|
|
|
|
|
|
// The label has changed, so we have to re-create it.
|
2006-10-11 19:40:50 +00:00
|
|
|
|
docstring const glabel = generateLabel(buffer);
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
|
|
|
|
unsigned int const maxLabelChars = 45;
|
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
|
docstring label = glabel;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
if (label.size() > maxLabelChars) {
|
|
|
|
|
label.erase(maxLabelChars-3);
|
|
|
|
|
label += "...";
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-10 20:55:00 +00:00
|
|
|
|
cache.engine = engine;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
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
|
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
|
int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
|
OutputParams const &) const
|
2001-05-27 17:51:16 +00:00
|
|
|
|
{
|
2007-02-17 16:36:45 +00:00
|
|
|
|
docstring str;
|
|
|
|
|
|
2004-05-10 20:55:00 +00:00
|
|
|
|
if (cache.params == params() &&
|
2007-03-22 07:22:16 +00:00
|
|
|
|
cache.engine == buffer.params().getEngine())
|
2007-02-17 16:36:45 +00:00
|
|
|
|
str = cache.generated_label;
|
2002-04-24 10:00:39 +00:00
|
|
|
|
else
|
2007-02-17 16:36:45 +00:00
|
|
|
|
str = generateLabel(buffer);
|
|
|
|
|
|
|
|
|
|
os << str;
|
|
|
|
|
return str.size();
|
2001-05-27 17:51:16 +00:00
|
|
|
|
}
|
2001-07-19 14:12:37 +00:00
|
|
|
|
|
2002-04-24 10:00:39 +00:00
|
|
|
|
|
2007-09-11 22:31:24 +00:00
|
|
|
|
static docstring const cleanupWhitespace(docstring const & citelist)
|
2004-10-21 22:39:20 +00:00
|
|
|
|
{
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring::const_iterator it = citelist.begin();
|
|
|
|
|
docstring::const_iterator end = citelist.end();
|
2004-10-21 22:39:20 +00:00
|
|
|
|
// Paranoia check: make sure that there is no whitespace in here
|
|
|
|
|
// -- at least not behind commas or at the beginning
|
2006-12-17 10:52:04 +00:00
|
|
|
|
docstring result;
|
|
|
|
|
char_type last = ',';
|
2004-10-21 22:39:20 +00:00
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
if (*it != ' ')
|
|
|
|
|
last = *it;
|
|
|
|
|
if (*it != ' ' || last != ',')
|
|
|
|
|
result += *it;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-02-17 16:36:45 +00:00
|
|
|
|
int InsetCitation::docbook(Buffer const &, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
|
OutputParams const &) const
|
2004-10-21 22:39:20 +00:00
|
|
|
|
{
|
2007-11-28 22:12:03 +00:00
|
|
|
|
os << from_ascii("<citation>")
|
2007-05-28 22:27:45 +00:00
|
|
|
|
<< cleanupWhitespace(getParam("key"))
|
2007-11-28 22:12:03 +00:00
|
|
|
|
<< from_ascii("</citation>");
|
2004-10-21 22:39:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-01-10 16:09:01 +00:00
|
|
|
|
void InsetCitation::textString(Buffer const & buf, odocstream & os) const
|
2005-11-25 14:40:34 +00:00
|
|
|
|
{
|
2008-01-10 16:09:01 +00:00
|
|
|
|
plaintext(buf, os, OutputParams(0));
|
2005-11-25 14:40:34 +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[]{}
|
2006-10-19 16:51:30 +00:00
|
|
|
|
int InsetCitation::latex(Buffer const & buffer, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
|
OutputParams const &) const
|
2001-07-19 14:12:37 +00:00
|
|
|
|
{
|
2007-03-22 07:22:16 +00:00
|
|
|
|
biblio::CiteEngine cite_engine = buffer.params().getEngine();
|
2006-10-19 16:51:30 +00:00
|
|
|
|
// FIXME UNICODE
|
2006-10-21 00:16:43 +00:00
|
|
|
|
docstring const cite_str = from_utf8(
|
2007-08-20 16:30:02 +00:00
|
|
|
|
asValidLatexCommand(getCmdName(), cite_engine));
|
2004-10-05 10:11:42 +00:00
|
|
|
|
|
2004-05-14 15:27:13 +00:00
|
|
|
|
os << "\\" << cite_str;
|
2004-10-05 10:11:42 +00:00
|
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
|
docstring const & before = getParam("before");
|
|
|
|
|
docstring const & after = getParam("after");
|
2004-05-13 20:44:35 +00:00
|
|
|
|
if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
|
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 << ']';
|
2004-03-05 14:49:10 +00:00
|
|
|
|
|
2006-12-17 10:52:04 +00:00
|
|
|
|
os << '{' << cleanupWhitespace(getParam("key")) << '}';
|
2001-07-19 14:12:37 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InsetCitation::validate(LaTeXFeatures & features) const
|
|
|
|
|
{
|
2007-03-25 01:12:38 +00:00
|
|
|
|
switch (features.bufferParams().getEngine()) {
|
2004-05-13 20:44:35 +00:00
|
|
|
|
case biblio::ENGINE_BASIC:
|
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_NATBIB_AUTHORYEAR:
|
|
|
|
|
case biblio::ENGINE_NATBIB_NUMERICAL:
|
2001-11-19 15:34:11 +00:00
|
|
|
|
features.require("natbib");
|
2004-05-13 20:44:35 +00:00
|
|
|
|
break;
|
|
|
|
|
case biblio::ENGINE_JURABIB:
|
2004-03-07 14:33:17 +00:00
|
|
|
|
features.require("jurabib");
|
2004-05-13 20:44:35 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2001-07-19 14:12:37 +00:00
|
|
|
|
}
|
2006-10-09 14:21:11 +00:00
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
} // namespace lyx
|