10 #include "frontends/Dialogs.h"
14 #include "BufferView.h"
17 #include "support/filetools.h"
18 #include "support/path.h"
19 #include "support/os.h"
20 #include "support/lstrings.h"
21 #include "support/LAssert.h"
34 int InsetBibKey::key_counter = 0;
35 const string key_prefix = "key-";
37 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
38 : InsetCommand(p), counter(1)
40 if (getContents().empty())
41 setContents(key_prefix + tostr(++key_counter));
45 InsetBibKey::~InsetBibKey()
50 Inset * InsetBibKey::clone(Buffer const &, bool) const
52 InsetBibKey * b = new InsetBibKey(params());
53 b->setCounter(counter);
58 void InsetBibKey::setCounter(int c)
64 // I'm sorry but this is still necessary because \bibitem is used also
65 // as a LyX 2.x command, and lyxlex is not enough smart to understand
66 // real LaTeX commands. Yes, that could be fixed, but would be a waste
67 // of time cause LyX3 won't use lyxlex anyway. (ale)
68 void InsetBibKey::write(Buffer const *, ostream & os) const
71 if (! getOptions().empty()) {
73 << getOptions() << ']';
76 << getContents() << "}\n";
80 // This is necessary here because this is written without begin_inset
81 // This should be changed!!! (Jug)
82 void InsetBibKey::read(Buffer const *, LyXLex & lex)
85 string const token = lex.getString();
88 lex.printError("InsetCommand: Parse error: `$$Token'");
91 if (prefixIs(getContents(), key_prefix)) {
92 int key = strToInt(getContents().substr(key_prefix.length()));
93 key_counter = max(key_counter, key);
97 string const InsetBibKey::getBibLabel() const
99 if (! getOptions().empty())
101 return tostr(counter);
104 string const InsetBibKey::getScreenLabel(Buffer const *) const
106 return getContents() + " [" + getBibLabel() + "]";
110 void InsetBibKey::edit(BufferView * bv, int, int, unsigned int)
112 bv->owner()->getDialogs()->showBibitem(this);
116 void InsetBibKey::edit(BufferView * bv, bool)
122 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
127 InsetBibtex::~InsetBibtex()
132 string const InsetBibtex::getScreenLabel(Buffer const *) const
134 return _("BibTeX Generated References");
138 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
139 bool /*fragile*/, bool/*fs*/) const
141 // changing the sequence of the commands
142 // 1. \bibliographystyle{style}
143 // 2. \addcontentsline{...} - if option bibtotoc set
144 // 3. \bibliography{database}
146 string db_in = getContents();
147 db_in = split(db_in, adb, ',');
150 string style = getOptions(); // maybe empty! and with bibtotoc
152 if (prefixIs(style,"bibtotoc")) {
153 bibtotoc = "bibtotoc";
154 if (contains(style,',')) {
155 style = split(style,bibtotoc,',');
159 if (!buffer->niceFile
160 && IsFileReadable(MakeAbsPath(style, buffer->filepath) + ".bst")) {
161 style = MakeAbsPath(style, buffer->filepath);
164 if (!style.empty()) { // we want no \biblio...{}
165 os << "\\bibliographystyle{" << style << "}\n";
169 if (!bibtotoc.empty()) {
170 // maybe a problem when a textclass has no "art" as
171 // part of its name, because it's than book.
172 // For the "official" lyx-layouts it's no problem to support
174 if (!contains(textclasslist.NameOfClass(buffer->params.textclass),"art")) {
175 if (buffer->params.sides == LyXTextClass::OneSide) {
180 os << "\\cleardoublepage";
184 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
188 os << "\\addcontentsline{toc}{section}{\\refname}";
193 // If we generate in a temp dir, we might need to give an
194 // absolute path there. This is a bit complicated since we can
195 // have a comma-separated list of bibliographies
196 while(!adb.empty()) {
197 if (!buffer->niceFile &&
198 IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib"))
199 adb = os::external_path(MakeAbsPath(adb, buffer->filepath));
202 db_in= split(db_in, adb,',');
204 db_out = strip(db_out, ',');
205 os << "\\bibliography{" << db_out << "}\n";
210 vector<string> const InsetBibtex::getFiles(Buffer const &) const
212 // Doesn't appear to be used (Angus, 31 July 2001)
213 // Path p(buffer->filepath);
218 string bibfiles = getContents();
219 bibfiles = split(bibfiles, tmp, ',');
220 while(!tmp.empty()) {
221 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
222 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
224 // If we didn't find a matching file name just fail silently
228 // Get next file name
229 bibfiles = split(bibfiles, tmp, ',');
235 // This method returns a comma separated list of Bibtex entries
236 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
238 vector<pair<string,string> > keys;
241 vector<string> const files = getFiles(*buffer);
242 for (vector<string>::const_iterator it = files.begin();
243 it != files.end(); ++ it) {
244 // This is a _very_ simple parser for Bibtex database
245 // files. All it does is to look for lines starting
246 // in @ and not being @preamble and @string entries.
247 // It does NOT do any syntax checking!
248 ifstream ifs(it->c_str());
250 while (getline(ifs, linebuf0)) {
251 string linebuf = frontStrip(strip(linebuf0));
252 if (linebuf.empty() ) continue;
253 if (prefixIs(linebuf, "@")) {
254 linebuf = subst(linebuf, '{', '(');
256 linebuf = split(linebuf, tmp, '(');
257 tmp = lowercase(tmp);
258 if (!prefixIs(tmp, "@string")
259 && !prefixIs(tmp, "@preamble")) {
260 linebuf = split(linebuf, tmp, ',');
261 tmp = frontStrip(tmp);
263 keys.push_back(pair<string,string>(tmp,string()));
266 } else if (!keys.empty()) {
267 keys.back().second += linebuf + "\n";
275 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
277 bv->owner()->getDialogs()->showBibtex(this);
281 void InsetBibtex::edit(BufferView * bv, bool)
287 bool InsetBibtex::addDatabase(string const & db)
289 string contents(getContents());
290 if (!contains(contents, db)) {
291 if (!contents.empty())
293 setContents(contents + db);
300 bool InsetBibtex::delDatabase(string const & db)
302 if (contains(getContents(), db)) {
304 int const n = tokenPos(getContents(), ',', bd);
306 // Weird code, would someone care to explain this?(Lgb)
309 setContents(subst(getContents(), tmp, ", "));
311 setContents(split(getContents(), bd, ','));
319 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
320 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
323 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
324 // Does look like a hack? It is! (but will change at 0.13)
325 Paragraph * par = bv->buffer()->paragraph;
329 int const wx = par->bibkey->width(bv, font);
339 string const bibitemWidest(Buffer const * buffer)
342 // Does look like a hack? It is! (but will change at 0.13)
343 Paragraph * par = buffer->paragraph;
344 InsetBibKey * bkey = 0;
350 lyxfont::width(par->bibkey->getBibLabel(),
360 if (bkey && !bkey->getBibLabel().empty())
361 return bkey->getBibLabel();