2003-02-21 09:20:18 +00:00
|
|
|
/**
|
2007-04-25 01:24:38 +00:00
|
|
|
* \file InsetBibitem.cpp
|
2003-02-21 09:20:18 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Alejandro Aguilar Sierra
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-02-21 09:20:18 +00:00
|
|
|
*/
|
2003-10-21 16:15:14 +00:00
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
#include <config.h>
|
2008-03-17 21:11:17 +00:00
|
|
|
#include <algorithm>
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "InsetBibitem.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2008-03-04 06:41:47 +00:00
|
|
|
#include "BiblioInfo.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
2008-03-04 06:41:47 +00:00
|
|
|
#include "buffer_funcs.h"
|
2007-08-16 11:12:56 +00:00
|
|
|
#include "BufferParams.h"
|
2006-10-09 14:21:11 +00:00
|
|
|
#include "BufferView.h"
|
2007-08-16 11:12:56 +00:00
|
|
|
#include "Counters.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "DispatchResult.h"
|
|
|
|
#include "FuncRequest.h"
|
2007-08-14 16:50:51 +00:00
|
|
|
#include "InsetIterator.h"
|
2007-10-24 15:32:43 +00:00
|
|
|
#include "InsetList.h"
|
2007-04-26 11:30:54 +00:00
|
|
|
#include "Lexer.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Paragraph.h"
|
2006-03-23 20:11:06 +00:00
|
|
|
#include "ParagraphList.h"
|
2007-11-07 23:25:08 +00:00
|
|
|
#include "TextClass.h"
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2008-03-04 06:41:47 +00:00
|
|
|
#include "frontends/alert.h"
|
|
|
|
|
2008-06-18 18:54:31 +00:00
|
|
|
#include "support/convert.h"
|
2007-11-01 22:17:22 +00:00
|
|
|
#include "support/docstream.h"
|
2008-03-04 06:41:47 +00:00
|
|
|
#include "support/gettext.h"
|
2008-06-18 18:54:31 +00:00
|
|
|
#include "support/lstrings.h"
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
using namespace std;
|
2007-12-12 19:57:42 +00:00
|
|
|
using namespace lyx::support;
|
2007-12-12 19:28:07 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
|
|
|
|
int InsetBibitem::key_counter = 0;
|
2006-10-21 00:16:43 +00:00
|
|
|
docstring const key_prefix = from_ascii("key-");
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
|
More cache fixing. Similar fix as for InsetBibtex, and I've renamed the key routine.
Unfortunately, we can't do a one-short fix for InsetCommand, requiring that it take
a Buffer & in the constructor, due to problems in mathed, specifically, here:
void InsetMathHull::label(row_type row, docstring const & label)
{
...
label_[row] = new InsetLabel(p);
if (buffer_)
label_[row]->setBuffer(buffer());
}
If InsetLabel has to have a Buffer, then buffer_ has to be set, which means InsetMathHull needs a Buffer. But then truckloads of these are created in Parser, where we don't really seem to have access to a Buffer.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25137 a592a061-630c-0410-9148-cb99ea01b6c8
2008-06-05 06:55:34 +00:00
|
|
|
InsetBibitem::InsetBibitem(Buffer const & buf, InsetCommandParams const & p)
|
2007-08-16 11:12:56 +00:00
|
|
|
: InsetCommand(p, "bibitem")
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
More cache fixing. Similar fix as for InsetBibtex, and I've renamed the key routine.
Unfortunately, we can't do a one-short fix for InsetCommand, requiring that it take
a Buffer & in the constructor, due to problems in mathed, specifically, here:
void InsetMathHull::label(row_type row, docstring const & label)
{
...
label_[row] = new InsetLabel(p);
if (buffer_)
label_[row]->setBuffer(buffer());
}
If InsetLabel has to have a Buffer, then buffer_ has to be set, which means InsetMathHull needs a Buffer. But then truckloads of these are created in Parser, where we don't really seem to have access to a Buffer.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25137 a592a061-630c-0410-9148-cb99ea01b6c8
2008-06-05 06:55:34 +00:00
|
|
|
Inset::setBuffer(const_cast<Buffer &>(buf));
|
|
|
|
buffer_->invalidateBibinfoCache();
|
2006-10-20 16:12:49 +00:00
|
|
|
if (getParam("key").empty())
|
|
|
|
setParam("key", key_prefix + convert<docstring>(++key_counter));
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
More cache fixing. Similar fix as for InsetBibtex, and I've renamed the key routine.
Unfortunately, we can't do a one-short fix for InsetCommand, requiring that it take
a Buffer & in the constructor, due to problems in mathed, specifically, here:
void InsetMathHull::label(row_type row, docstring const & label)
{
...
label_[row] = new InsetLabel(p);
if (buffer_)
label_[row]->setBuffer(buffer());
}
If InsetLabel has to have a Buffer, then buffer_ has to be set, which means InsetMathHull needs a Buffer. But then truckloads of these are created in Parser, where we don't really seem to have access to a Buffer.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25137 a592a061-630c-0410-9148-cb99ea01b6c8
2008-06-05 06:55:34 +00:00
|
|
|
InsetBibitem::~InsetBibitem()
|
|
|
|
{
|
2008-06-06 03:11:56 +00:00
|
|
|
if (isBufferValid())
|
More cache fixing. Similar fix as for InsetBibtex, and I've renamed the key routine.
Unfortunately, we can't do a one-short fix for InsetCommand, requiring that it take
a Buffer & in the constructor, due to problems in mathed, specifically, here:
void InsetMathHull::label(row_type row, docstring const & label)
{
...
label_[row] = new InsetLabel(p);
if (buffer_)
label_[row]->setBuffer(buffer());
}
If InsetLabel has to have a Buffer, then buffer_ has to be set, which means InsetMathHull needs a Buffer. But then truckloads of these are created in Parser, where we don't really seem to have access to a Buffer.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25137 a592a061-630c-0410-9148-cb99ea01b6c8
2008-06-05 06:55:34 +00:00
|
|
|
buffer_->invalidateBibinfoCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-04 06:41:47 +00:00
|
|
|
void InsetBibitem::initView()
|
|
|
|
{
|
|
|
|
updateCommand(getParam("key"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetBibitem::updateCommand(docstring const & new_key, bool)
|
|
|
|
{
|
|
|
|
docstring const old_key = getParam("key");
|
|
|
|
docstring key = new_key;
|
|
|
|
|
2008-04-25 20:03:03 +00:00
|
|
|
vector<docstring> bibkeys = buffer().masterBibInfo().getKeys();
|
2008-03-04 06:41:47 +00:00
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
if (find(bibkeys.begin(), bibkeys.end(), key) != bibkeys.end()) {
|
|
|
|
// generate unique label
|
|
|
|
key = new_key + '-' + convert<docstring>(i);
|
|
|
|
while (find(bibkeys.begin(), bibkeys.end(), key) != bibkeys.end()) {
|
|
|
|
++i;
|
|
|
|
key = new_key + '-' + convert<docstring>(i);
|
|
|
|
}
|
|
|
|
frontend::Alert::warning(_("Keys must be unique!"),
|
|
|
|
bformat(_("The key %1$s already exists,\n"
|
|
|
|
"it will be changed to %2$s."), new_key, key));
|
|
|
|
}
|
|
|
|
setParam("key", key);
|
|
|
|
|
|
|
|
lyx::updateLabels(buffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-23 22:01:02 +00:00
|
|
|
ParamInfo const & InsetBibitem::findInfo(string const & /* cmdName */)
|
2007-10-25 04:13:56 +00:00
|
|
|
{
|
2008-02-23 22:01:02 +00:00
|
|
|
static ParamInfo param_info_;
|
|
|
|
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("label", 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-04-26 14:56:30 +00:00
|
|
|
void InsetBibitem::doDispatch(Cursor & cur, FuncRequest & cmd)
|
2003-02-26 19:28:38 +00:00
|
|
|
{
|
2003-03-07 21:44:48 +00:00
|
|
|
switch (cmd.action) {
|
2003-05-16 07:44:00 +00:00
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
case LFUN_INSET_MODIFY: {
|
2007-10-19 17:22:55 +00:00
|
|
|
InsetCommandParams p(BIBITEM_CODE);
|
2008-03-27 22:26:24 +00:00
|
|
|
InsetCommand::string2params("bibitem", to_utf8(cmd.argument()), p);
|
2006-10-09 14:21:11 +00:00
|
|
|
if (p.getCmdName().empty()) {
|
2007-05-28 22:27:45 +00:00
|
|
|
cur.noUpdate();
|
2006-10-09 14:21:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-08-10 20:32:51 +00:00
|
|
|
docstring const & old_key = params()["key"];
|
2008-03-04 06:41:47 +00:00
|
|
|
setParam("label", p["label"]);
|
2008-08-10 20:32:51 +00:00
|
|
|
if (p["key"] != old_key) {
|
2008-07-14 12:49:39 +00:00
|
|
|
updateCommand(p["key"]);
|
2008-03-04 06:41:47 +00:00
|
|
|
cur.bv().buffer().changeRefsIfUnique(old_key,
|
|
|
|
params()["key"], CITE_CODE);
|
2008-07-14 12:49:39 +00:00
|
|
|
}
|
Forgot this bit.
The only remaining problem here is that, if you edit the TEXT that goes with
an InsetBibitem, then the information in the citation will not get updated
until something else invalidates the cache. The only way to fix this, given
the existing system, would seem to be to check, every time anything gets
edited, whether we're in a Bibliography environment or not, and if so to
invalidate the cache. I'm not sure that is worth it. Really, it points to a
more general issue with InsetBibitem, IMHO: It ought to be a kind of combined
inset, a hybrid of sorts between an InsetCollapsable and an InsetCommand. But
that would be for 1.7.x at least.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25138 a592a061-630c-0410-9148-cb99ea01b6c8
2008-06-05 06:59:35 +00:00
|
|
|
buffer_->invalidateBibinfoCache();
|
2008-03-04 07:33:43 +00:00
|
|
|
break;
|
2003-03-07 21:44:48 +00:00
|
|
|
}
|
2003-05-16 07:44:00 +00:00
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
default:
|
2004-11-24 21:58:42 +00:00
|
|
|
InsetCommand::doDispatch(cur, cmd);
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-02-26 19:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
void InsetBibitem::read(Lexer & lex)
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
2008-02-27 20:43:16 +00:00
|
|
|
InsetCommand::read(lex);
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2007-12-12 19:57:42 +00:00
|
|
|
if (prefixIs(getParam("key"), key_prefix)) {
|
2006-10-20 16:12:49 +00:00
|
|
|
int const key = convert<int>(getParam("key").substr(key_prefix.length()));
|
2007-12-12 19:28:07 +00:00
|
|
|
key_counter = max(key_counter, key);
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring InsetBibitem::bibLabel() const
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
2006-10-20 16:12:49 +00:00
|
|
|
docstring const & label = getParam("label");
|
2007-08-16 11:12:56 +00:00
|
|
|
return label.empty() ? autolabel_ : label;
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring InsetBibitem::screenLabel() const
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
2008-02-27 20:43:16 +00:00
|
|
|
return getParam("key") + " [" + bibLabel() + ']';
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
2006-10-11 19:40:50 +00:00
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
int InsetBibitem::plaintext(odocstream & os, OutputParams const &) const
|
2004-08-14 18:41:27 +00:00
|
|
|
{
|
2007-02-17 16:36:45 +00:00
|
|
|
odocstringstream oss;
|
2008-02-27 20:43:16 +00:00
|
|
|
oss << '[' << bibLabel() << "] ";
|
2007-02-17 16:36:45 +00:00
|
|
|
|
|
|
|
docstring const str = oss.str();
|
|
|
|
os << str;
|
|
|
|
|
|
|
|
return str.size();
|
2004-08-14 18:41:27 +00:00
|
|
|
}
|
2003-02-21 09:20:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ale070405
|
2008-03-27 22:26:24 +00:00
|
|
|
docstring bibitemWidest(Buffer const & buffer)
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
|
|
|
int w = 0;
|
|
|
|
|
2003-05-27 22:41:04 +00:00
|
|
|
InsetBibitem const * bitem = 0;
|
2006-10-14 15:31:50 +00:00
|
|
|
|
2006-10-14 15:22:08 +00:00
|
|
|
// FIXME: this font is used unitialized for now but should be set to
|
2006-10-14 15:31:50 +00:00
|
|
|
// a proportional font. Here is what Georg Baum has to say about it:
|
2006-10-14 15:22:08 +00:00
|
|
|
/*
|
2007-05-28 22:27:45 +00:00
|
|
|
bibitemWidest() is supposed to find the bibitem with the widest label in the
|
|
|
|
output, because that is needed as an argument of the bibliography
|
|
|
|
environment to dtermine the correct indentation. To be 100% correct we
|
|
|
|
would need the metrics of the font that is used in the output, but usually
|
2006-10-14 15:22:08 +00:00
|
|
|
we don't have access to these.
|
2007-05-28 22:27:45 +00:00
|
|
|
In practice, any proportional font is probably good enough, since we don't
|
|
|
|
need to know the final with, we only need to know the which label is the
|
2006-10-14 15:22:08 +00:00
|
|
|
widest.
|
2007-05-28 22:27:45 +00:00
|
|
|
Unless there is an easy way to get the metrics of the output font I suggest
|
2006-10-14 15:22:08 +00:00
|
|
|
to use a hardcoded font like "Times" or so.
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
It is very important that the result of this function is the same both with
|
|
|
|
and without GUI. After thinking about this it is clear that no Font
|
|
|
|
metrics should be used here, since these come from the gui. If we can't
|
2007-10-28 18:51:54 +00:00
|
|
|
easily get the LaTeX font metrics we should make our own poor mans font
|
2007-05-28 22:27:45 +00:00
|
|
|
metrics replacement, e.g. by hardcoding the metrics of the standard TeX
|
2006-10-14 15:22:08 +00:00
|
|
|
font.
|
|
|
|
*/
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2003-09-09 09:47:59 +00:00
|
|
|
ParagraphList::const_iterator it = buffer.paragraphs().begin();
|
|
|
|
ParagraphList::const_iterator end = buffer.paragraphs().end();
|
2003-05-28 06:47:15 +00:00
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
for (; it != end; ++it) {
|
2007-10-24 15:32:43 +00:00
|
|
|
if (it->insetList().empty())
|
|
|
|
continue;
|
|
|
|
Inset * inset = it->insetList().begin()->inset;
|
|
|
|
if (inset->lyxCode() != BIBITEM_CODE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bitem = static_cast<InsetBibitem const *>(inset);
|
2008-02-27 20:43:16 +00:00
|
|
|
docstring const label = bitem->bibLabel();
|
2007-10-24 15:32:43 +00:00
|
|
|
|
|
|
|
// FIXME: we can't be sure using the following that the GUI
|
|
|
|
// version and the command-line version will give the same
|
|
|
|
// result.
|
|
|
|
//
|
|
|
|
//int const wx = use_gui?
|
|
|
|
// theFontMetrics(font).width(label): label.size();
|
|
|
|
//
|
|
|
|
// So for now we just use the label size in order to be sure
|
|
|
|
// that GUI and no-GUI gives the same bibitem (even if that is
|
|
|
|
// potentially the wrong one.
|
|
|
|
int const wx = label.size();
|
|
|
|
|
|
|
|
if (wx > w)
|
|
|
|
w = wx;
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
if (bitem && !bitem->bibLabel().empty())
|
|
|
|
return bitem->bibLabel();
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
return from_ascii("99");
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2008-02-27 20:43:16 +00:00
|
|
|
void InsetBibitem::fillWithBibKeys(BiblioInfo & keys, InsetIterator const & it) const
|
2007-08-14 16:50:51 +00:00
|
|
|
{
|
2007-08-20 16:30:02 +00:00
|
|
|
docstring const key = getParam("key");
|
2008-02-14 05:00:54 +00:00
|
|
|
BibTeXInfo keyvalmap(false);
|
2007-08-16 01:59:20 +00:00
|
|
|
keyvalmap[from_ascii("label")] = getParam("label");
|
2007-08-14 16:50:51 +00:00
|
|
|
DocIterator doc_it(it);
|
|
|
|
doc_it.forwardPos();
|
2008-05-22 14:01:33 +00:00
|
|
|
keyvalmap[from_ascii("ref")] = doc_it.paragraph().asString();
|
2007-08-16 01:59:20 +00:00
|
|
|
keys[key] = keyvalmap;
|
2007-08-14 16:50:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 11:12:56 +00:00
|
|
|
|
|
|
|
/// Update the counters of this inset and of its contents
|
2008-02-27 20:43:16 +00:00
|
|
|
void InsetBibitem::updateLabels(ParIterator const &)
|
2007-08-16 11:12:56 +00:00
|
|
|
{
|
2008-11-15 19:30:58 +00:00
|
|
|
Counters & counters = buffer().masterBuffer()->params().documentClass().counters();
|
2007-08-16 11:12:56 +00:00
|
|
|
docstring const bibitem = from_ascii("bibitem");
|
|
|
|
if (counters.hasCounter(bibitem) && getParam("label").empty()) {
|
|
|
|
counters.step(bibitem);
|
|
|
|
autolabel_ = counters.theCounter(bibitem);
|
2008-02-27 20:43:16 +00:00
|
|
|
} else {
|
2007-08-16 11:12:56 +00:00
|
|
|
autolabel_ = from_ascii("??");
|
2008-02-27 20:43:16 +00:00
|
|
|
}
|
2007-08-16 11:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|