3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * Full author contact details are available in file CREDITS
13 #pragma implementation
19 #include "BufferView.h"
24 #include "frontends/font_metrics.h"
25 #include "frontends/LyXView.h"
27 #include "frontends/Dialogs.h"
29 #include "support/filetools.h"
30 #include "support/path.h"
31 #include "support/os.h"
32 #include "support/lstrings.h"
33 #include "support/LAssert.h"
46 int InsetBibKey::key_counter = 0;
47 const string key_prefix = "key-";
49 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
50 : InsetCommand(p), counter(1)
52 if (getContents().empty())
53 setContents(key_prefix + tostr(++key_counter));
57 InsetBibKey::~InsetBibKey()
62 Inset * InsetBibKey::clone(Buffer const &, bool) const
64 InsetBibKey * b = new InsetBibKey(params());
65 b->setCounter(counter);
70 void InsetBibKey::setCounter(int c)
76 // I'm sorry but this is still necessary because \bibitem is used also
77 // as a LyX 2.x command, and lyxlex is not enough smart to understand
78 // real LaTeX commands. Yes, that could be fixed, but would be a waste
79 // of time cause LyX3 won't use lyxlex anyway. (ale)
80 void InsetBibKey::write(Buffer const *, ostream & os) const
83 if (! getOptions().empty()) {
85 << getOptions() << ']';
88 << getContents() << "}\n";
92 // This is necessary here because this is written without begin_inset
93 // This should be changed!!! (Jug)
94 void InsetBibKey::read(Buffer const *, LyXLex & lex)
97 string const token = lex.getString();
100 lex.printError("InsetCommand: Parse error: `$$Token'");
103 if (prefixIs(getContents(), key_prefix)) {
104 int key = strToInt(getContents().substr(key_prefix.length()));
105 key_counter = max(key_counter, key);
109 string const InsetBibKey::getBibLabel() const
111 if (! getOptions().empty())
113 return tostr(counter);
116 string const InsetBibKey::getScreenLabel(Buffer const *) const
118 return getContents() + " [" + getBibLabel() + ']';
122 void InsetBibKey::edit(BufferView * bv, int, int, mouse_button::state)
124 bv->owner()->getDialogs().showBibitem(this);
128 void InsetBibKey::edit(BufferView * bv, bool)
130 edit(bv, 0, 0, mouse_button::none);
134 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
139 InsetBibtex::~InsetBibtex()
144 string const InsetBibtex::getScreenLabel(Buffer const *) const
146 return _("BibTeX Generated References");
150 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
151 bool /*fragile*/, bool/*fs*/) const
153 // changing the sequence of the commands
154 // 1. \bibliographystyle{style}
155 // 2. \addcontentsline{...} - if option bibtotoc set
156 // 3. \bibliography{database}
158 string db_in = getContents();
159 db_in = split(db_in, adb, ',');
162 string style = getOptions(); // maybe empty! and with bibtotoc
164 if (prefixIs(style, "bibtotoc")) {
165 bibtotoc = "bibtotoc";
166 if (contains(style, ',')) {
167 style = split(style, bibtotoc, ',');
171 if (!buffer->niceFile
172 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
173 style = MakeAbsPath(style, buffer->filePath());
176 if (!style.empty()) { // we want no \biblio...{}
177 os << "\\bibliographystyle{" << style << "}\n";
181 if (!bibtotoc.empty()) {
182 // maybe a problem when a textclass has no "art" as
183 // part of its name, because it's than book.
184 // For the "official" lyx-layouts it's no problem to support
186 if (!contains(buffer->params.getLyXTextClass().name(),
188 if (buffer->params.sides == LyXTextClass::OneSide) {
193 os << "\\cleardoublepage";
197 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
201 os << "\\addcontentsline{toc}{section}{\\refname}";
206 // If we generate in a temp dir, we might need to give an
207 // absolute path there. This is a bit complicated since we can
208 // have a comma-separated list of bibliographies
210 while (!adb.empty()) {
211 if (!buffer->niceFile &&
212 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
213 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
216 db_in= split(db_in, adb,',');
218 db_out = rtrim(db_out, ",");
219 os << "\\bibliography{" << db_out << "}\n";
224 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
226 // Doesn't appear to be used (Angus, 31 July 2001)
227 Path p(buffer.filePath());
232 string bibfiles = getContents();
233 bibfiles = split(bibfiles, tmp, ',');
234 while (!tmp.empty()) {
235 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
236 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
238 // If we didn't find a matching file name just fail silently
242 // Get next file name
243 bibfiles = split(bibfiles, tmp, ',');
249 // This method returns a comma separated list of Bibtex entries
250 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
252 vector<pair<string,string> > keys;
255 vector<string> const files = getFiles(*buffer);
256 for (vector<string>::const_iterator it = files.begin();
257 it != files.end(); ++ it) {
258 // This is a _very_ simple parser for Bibtex database
259 // files. All it does is to look for lines starting
260 // in @ and not being @preamble and @string entries.
261 // It does NOT do any syntax checking!
262 ifstream ifs(it->c_str());
264 while (getline(ifs, linebuf0)) {
265 string linebuf = trim(linebuf0);
266 if (linebuf.empty()) continue;
267 if (prefixIs(linebuf, "@")) {
268 linebuf = subst(linebuf, '{', '(');
270 linebuf = split(linebuf, tmp, '(');
271 tmp = ascii_lowercase(tmp);
272 if (!prefixIs(tmp, "@string")
273 && !prefixIs(tmp, "@preamble")) {
274 linebuf = split(linebuf, tmp, ',');
275 tmp = ltrim(tmp, " \t");
277 keys.push_back(pair<string,string>(tmp,string()));
280 } else if (!keys.empty()) {
281 keys.back().second += linebuf + "\n";
289 void InsetBibtex::edit(BufferView * bv, int, int, mouse_button::state)
291 bv->owner()->getDialogs().showBibtex(this);
295 void InsetBibtex::edit(BufferView * bv, bool)
297 edit(bv, 0, 0, mouse_button::none);
301 bool InsetBibtex::addDatabase(string const & db)
303 string contents(getContents());
304 if (!contains(contents, db)) {
305 if (!contents.empty())
307 setContents(contents + db);
314 bool InsetBibtex::delDatabase(string const & db)
316 if (contains(getContents(), db)) {
318 int const n = tokenPos(getContents(), ',', bd);
320 // Weird code, would someone care to explain this?(Lgb)
323 setContents(subst(getContents(), tmp, ", "));
325 setContents(split(getContents(), bd, ','));
333 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
334 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
337 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
338 // Does look like a hack? It is! (but will change at 0.13)
339 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
340 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
341 for (; it != end; ++it) {
343 int const wx = it->bibkey->width(bv, font);
353 string const bibitemWidest(Buffer const * buffer)
356 // Does look like a hack? It is! (but will change at 0.13)
358 InsetBibKey * bkey = 0;
361 ParagraphList::iterator it = buffer->paragraphs.begin();
362 ParagraphList::iterator end = buffer->paragraphs.end();
363 for (; it != end; ++it) {
366 font_metrics::width(it->bibkey->getBibLabel(),
375 if (bkey && !bkey->getBibLabel().empty())
376 return bkey->getBibLabel();