10 #include "frontends/Dialogs.h"
14 #include "BufferView.h"
17 #include "support/filetools.h"
18 #include "support/path.h"
19 #include "support/os.h"
32 int InsetBibKey::key_counter = 0;
33 const string key_prefix = "key-";
35 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
36 : InsetCommand(p), counter(1)
38 if (getContents().empty())
39 setContents(key_prefix + tostr(++key_counter));
43 InsetBibKey::~InsetBibKey()
48 Inset * InsetBibKey::clone(Buffer const &, bool) const
50 InsetBibKey * b = new InsetBibKey(params());
51 b->setCounter(counter);
56 void InsetBibKey::setCounter(int c)
62 // I'm sorry but this is still necessary because \bibitem is used also
63 // as a LyX 2.x command, and lyxlex is not enough smart to understand
64 // real LaTeX commands. Yes, that could be fixed, but would be a waste
65 // of time cause LyX3 won't use lyxlex anyway. (ale)
66 void InsetBibKey::write(Buffer const *, ostream & os) const
69 if (! getOptions().empty()) {
71 << getOptions() << ']';
74 << getContents() << "}\n";
78 // This is necessary here because this is written without begin_inset
79 // This should be changed!!! (Jug)
80 void InsetBibKey::read(Buffer const *, LyXLex & lex)
85 token = lex.GetString();
88 lex.printError("InsetCommand: Parse error: `$$Token'");
90 if (prefixIs(getContents(), key_prefix)) {
91 int key = strToInt(getContents().substr(key_prefix.length()));
92 key_counter = max(key_counter, key);
96 string const InsetBibKey::getBibLabel() const
98 if (! getOptions().empty())
100 return tostr(counter);
103 string const InsetBibKey::getScreenLabel(Buffer const *) const
105 return getContents() + " [" + getBibLabel() + "]";
109 void InsetBibKey::edit(BufferView * bv, int, int, unsigned int)
111 bv->owner()->getDialogs()->showBibitem(this);
115 void InsetBibKey::edit(BufferView * bv, bool)
121 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
126 InsetBibtex::~InsetBibtex()
131 string const InsetBibtex::getScreenLabel(Buffer const *) const
133 return _("BibTeX Generated References");
137 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
138 bool /*fragile*/, bool/*fs*/) const
140 // If we generate in a temp dir, we might need to give an
141 // absolute path there. This is a bit complicated since we can
142 // have a comma-separated list of bibliographies
144 string db_in = getContents();
145 db_in = split(db_in, adb, ',');
146 while(!adb.empty()) {
147 if (!buffer->niceFile &&
148 IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib"))
149 adb = os::external_path(MakeAbsPath(adb, buffer->filepath));
153 db_in= split(db_in, adb,',');
155 db_out = strip(db_out, ',');
158 if (!buffer->niceFile
159 && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
161 style = MakeAbsPath(getOptions(), buffer->filepath);
163 style = getOptions();
165 os << "\\bibliographystyle{" << style << "}\n"
166 << "\\bibliography{" << db_out << "}\n";
171 // This method returns a comma separated list of Bibtex entries
172 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
174 Path p(buffer->filepath);
176 vector<pair<string,string> > keys;
178 string bibfiles = getContents();
179 bibfiles = split(bibfiles, tmp, ',');
180 while(!tmp.empty()) {
181 string fil = findtexfile(ChangeExtension(tmp, "bib"),
183 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
184 // If we didn't find a matching file name just fail silently
186 // This is a _very_ simple parser for Bibtex database
187 // files. All it does is to look for lines starting
188 // in @ and not being @preamble and @string entries.
189 // It does NOT do any syntax checking!
190 ifstream ifs(fil.c_str());
192 while (getline(ifs, linebuf0)) {
193 string linebuf = frontStrip(strip(linebuf0));
194 if (linebuf.empty() ) continue;
195 if (prefixIs(linebuf, "@")) {
196 linebuf = subst(linebuf, '{', '(');
197 linebuf = split(linebuf, tmp, '(');
198 tmp = lowercase(tmp);
199 if (!prefixIs(tmp, "@string")
200 && !prefixIs(tmp, "@preamble")) {
201 linebuf = split(linebuf, tmp, ',');
202 tmp = frontStrip(tmp);
204 keys.push_back(pair<string,string>(tmp,string()));
207 } else if (!keys.empty()) {
208 keys.back().second += linebuf + "\n";
212 // Get next file name
213 bibfiles = split(bibfiles, tmp, ',');
219 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
221 bv->owner()->getDialogs()->showBibtex(this);
225 void InsetBibtex::edit(BufferView * bv, bool)
231 bool InsetBibtex::addDatabase(string const & db)
233 string contents(getContents());
234 if (!contains(contents, db)) {
235 if (!contents.empty())
237 setContents(contents + db);
244 bool InsetBibtex::delDatabase(string const & db)
246 if (contains(getContents(), db)) {
248 int const n = tokenPos(getContents(), ',', bd);
250 // Weird code, would someone care to explain this?(Lgb)
253 setContents(subst(getContents(), tmp, ", "));
255 setContents(split(getContents(), bd, ','));
263 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
264 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
267 #warning Ha, now we are mainly at 1.2.0 and it is still here (Jug)
268 // Does look like a hack? It is! (but will change at 0.13)
269 Paragraph * par = bv->buffer()->paragraph;
273 int const wx = par->bibkey->width(bv, font);
283 string const bibitemWidest(Buffer const * buffer)
286 // Does look like a hack? It is! (but will change at 0.13)
287 Paragraph * par = buffer->paragraph;
288 InsetBibKey * bkey = 0;
294 lyxfont::width(par->bibkey->getBibLabel(),
304 if (bkey && !bkey->getBibLabel().empty())
305 return bkey->getBibLabel();