10 #include "BufferView.h"
15 #include "frontends/LyXView.h"
16 #include "lyxtextclasslist.h"
18 #include "frontends/Dialogs.h"
20 #include "support/filetools.h"
21 #include "support/path.h"
22 #include "support/os.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
37 int InsetBibKey::key_counter = 0;
38 const string key_prefix = "key-";
40 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
41 : InsetCommand(p), counter(1)
43 if (getContents().empty())
44 setContents(key_prefix + tostr(++key_counter));
48 InsetBibKey::~InsetBibKey()
53 Inset * InsetBibKey::clone(Buffer const &, bool) const
55 InsetBibKey * b = new InsetBibKey(params());
56 b->setCounter(counter);
61 void InsetBibKey::setCounter(int c)
67 // I'm sorry but this is still necessary because \bibitem is used also
68 // as a LyX 2.x command, and lyxlex is not enough smart to understand
69 // real LaTeX commands. Yes, that could be fixed, but would be a waste
70 // of time cause LyX3 won't use lyxlex anyway. (ale)
71 void InsetBibKey::write(Buffer const *, ostream & os) const
74 if (! getOptions().empty()) {
76 << getOptions() << ']';
79 << getContents() << "}\n";
83 // This is necessary here because this is written without begin_inset
84 // This should be changed!!! (Jug)
85 void InsetBibKey::read(Buffer const *, LyXLex & lex)
88 string const token = lex.getString();
91 lex.printError("InsetCommand: Parse error: `$$Token'");
94 if (prefixIs(getContents(), key_prefix)) {
95 int key = strToInt(getContents().substr(key_prefix.length()));
96 key_counter = max(key_counter, key);
100 string const InsetBibKey::getBibLabel() const
102 if (! getOptions().empty())
104 return tostr(counter);
107 string const InsetBibKey::getScreenLabel(Buffer const *) const
109 return getContents() + " [" + getBibLabel() + "]";
113 void InsetBibKey::edit(BufferView * bv, int, int, unsigned int)
115 bv->owner()->getDialogs()->showBibitem(this);
119 void InsetBibKey::edit(BufferView * bv, bool)
125 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
130 InsetBibtex::~InsetBibtex()
135 string const InsetBibtex::getScreenLabel(Buffer const *) const
137 return _("BibTeX Generated References");
141 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
142 bool /*fragile*/, bool/*fs*/) const
144 // changing the sequence of the commands
145 // 1. \bibliographystyle{style}
146 // 2. \addcontentsline{...} - if option bibtotoc set
147 // 3. \bibliography{database}
149 string db_in = getContents();
150 db_in = split(db_in, adb, ',');
153 string style = getOptions(); // maybe empty! and with bibtotoc
155 if (prefixIs(style,"bibtotoc")) {
156 bibtotoc = "bibtotoc";
157 if (contains(style,',')) {
158 style = split(style,bibtotoc,',');
162 if (!buffer->niceFile
163 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
164 style = MakeAbsPath(style, buffer->filePath());
167 if (!style.empty()) { // we want no \biblio...{}
168 os << "\\bibliographystyle{" << style << "}\n";
172 if (!bibtotoc.empty()) {
173 // maybe a problem when a textclass has no "art" as
174 // part of its name, because it's than book.
175 // For the "official" lyx-layouts it's no problem to support
177 if (!contains(textclasslist[buffer->params.textclass].name(),
179 if (buffer->params.sides == LyXTextClass::OneSide) {
184 os << "\\cleardoublepage";
188 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
192 os << "\\addcontentsline{toc}{section}{\\refname}";
197 // If we generate in a temp dir, we might need to give an
198 // absolute path there. This is a bit complicated since we can
199 // have a comma-separated list of bibliographies
201 while (!adb.empty()) {
202 if (!buffer->niceFile &&
203 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
204 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
207 db_in= split(db_in, adb,',');
209 db_out = strip(db_out, ',');
210 os << "\\bibliography{" << db_out << "}\n";
215 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
217 // Doesn't appear to be used (Angus, 31 July 2001)
218 Path p(buffer.filePath());
223 string bibfiles = getContents();
224 bibfiles = split(bibfiles, tmp, ',');
225 while (!tmp.empty()) {
226 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
227 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
229 // If we didn't find a matching file name just fail silently
233 // Get next file name
234 bibfiles = split(bibfiles, tmp, ',');
240 // This method returns a comma separated list of Bibtex entries
241 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
243 vector<pair<string,string> > keys;
246 vector<string> const files = getFiles(*buffer);
247 for (vector<string>::const_iterator it = files.begin();
248 it != files.end(); ++ it) {
249 // This is a _very_ simple parser for Bibtex database
250 // files. All it does is to look for lines starting
251 // in @ and not being @preamble and @string entries.
252 // It does NOT do any syntax checking!
253 ifstream ifs(it->c_str());
255 while (getline(ifs, linebuf0)) {
256 string linebuf = frontStrip(strip(linebuf0));
257 if (linebuf.empty()) continue;
258 if (prefixIs(linebuf, "@")) {
259 linebuf = subst(linebuf, '{', '(');
261 linebuf = split(linebuf, tmp, '(');
262 tmp = lowercase(tmp);
263 if (!prefixIs(tmp, "@string")
264 && !prefixIs(tmp, "@preamble")) {
265 linebuf = split(linebuf, tmp, ',');
266 tmp = frontStrip(tmp);
267 tmp = frontStrip(tmp,'\t');
269 keys.push_back(pair<string,string>(tmp,string()));
272 } else if (!keys.empty()) {
273 keys.back().second += linebuf + "\n";
281 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
283 bv->owner()->getDialogs()->showBibtex(this);
287 void InsetBibtex::edit(BufferView * bv, bool)
293 bool InsetBibtex::addDatabase(string const & db)
295 string contents(getContents());
296 if (!contains(contents, db)) {
297 if (!contents.empty())
299 setContents(contents + db);
306 bool InsetBibtex::delDatabase(string const & db)
308 if (contains(getContents(), db)) {
310 int const n = tokenPos(getContents(), ',', bd);
312 // Weird code, would someone care to explain this?(Lgb)
315 setContents(subst(getContents(), tmp, ", "));
317 setContents(split(getContents(), bd, ','));
325 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
326 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
329 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
330 // Does look like a hack? It is! (but will change at 0.13)
331 Paragraph * par = bv->buffer()->paragraph;
335 int const wx = par->bibkey->width(bv, font);
345 string const bibitemWidest(Buffer const * buffer)
348 // Does look like a hack? It is! (but will change at 0.13)
349 Paragraph * par = buffer->paragraph;
350 InsetBibKey * bkey = 0;
356 lyxfont::width(par->bibkey->getBibLabel(),
366 if (bkey && !bkey->getBibLabel().empty())
367 return bkey->getBibLabel();