10 #include FORMS_H_LOCATION
11 #include "frontends/Dialogs.h"
15 #include "BufferView.h"
18 #include "support/filetools.h"
19 #include "support/path.h"
32 int InsetBibKey::key_counter = 0;
33 const string key_prefix = "key-";
35 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
39 if (getContents().empty())
40 setContents(key_prefix + tostr(++key_counter));
44 InsetBibKey::~InsetBibKey()
49 Inset * InsetBibKey::Clone(Buffer const &) const
51 InsetBibKey * b = new InsetBibKey(params());
52 b->setCounter(counter);
57 void InsetBibKey::setCounter(int c)
63 // I'm sorry but this is still necessary because \bibitem is used also
64 // as a LyX 2.x command, and lyxlex is not enough smart to understand
65 // real LaTeX commands. Yes, that could be fixed, but would be a waste
66 // of time cause LyX3 won't use lyxlex anyway. (ale)
67 void InsetBibKey::Write(Buffer const *, ostream & os) const
70 if (! getOptions().empty()) {
72 << getOptions() << ']';
75 << getContents() << "}\n";
79 // This is necessary here because this is written without begin_inset
80 // This should be changed!!! (Jug)
81 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
86 token = lex.GetString();
89 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() const
106 return getContents() + " [" + getBibLabel() + "]";
110 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
112 bv->owner()->getDialogs()->showBibitem(this);
116 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
121 InsetBibtex::~InsetBibtex()
126 string const InsetBibtex::getScreenLabel() const
128 return _("BibTeX Generated References");
132 int InsetBibtex::Latex(Buffer const * buffer, ostream & os,
133 bool /*fragile*/, bool/*fs*/) const
135 // If we generate in a temp dir, we might need to give an
136 // absolute path there. This is a bit complicated since we can
137 // have a comma-separated list of bibliographies
139 string db_in = getContents();
140 db_in = split(db_in, adb, ',');
141 while(!adb.empty()) {
142 if (!buffer->niceFile &&
143 IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib"))
144 adb = MakeAbsPath(adb, buffer->filepath);
148 db_in= split(db_in, adb,',');
150 db_out = strip(db_out, ',');
153 if (!buffer->niceFile
154 && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
156 style = MakeAbsPath(getOptions(), buffer->filepath);
158 style = getOptions();
160 os << "\\bibliographystyle{" << style << "}\n"
161 << "\\bibliography{" << db_out << "}\n";
166 // This method returns a comma separated list of Bibtex entries
167 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
169 Path p(buffer->filepath);
171 vector<pair<string,string> > keys;
173 string bibfiles = getContents();
174 bibfiles = split(bibfiles, tmp, ',');
175 while(!tmp.empty()) {
176 string fil = findtexfile(ChangeExtension(tmp, "bib"),
178 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
179 // If we didn't find a matching file name just fail silently
181 // This is a _very_ simple parser for Bibtex database
182 // files. All it does is to look for lines starting
183 // in @ and not being @preamble and @string entries.
184 // It does NOT do any syntax checking!
185 ifstream ifs(fil.c_str());
187 while (getline(ifs, linebuf0)) {
188 string linebuf = frontStrip(strip(linebuf0));
189 if (linebuf.empty() ) continue;
190 if (prefixIs(linebuf, "@")) {
191 linebuf = subst(linebuf, '{', '(');
192 linebuf = split(linebuf, tmp, '(');
193 tmp = lowercase(tmp);
194 if (!prefixIs(tmp, "@string")
195 && !prefixIs(tmp, "@preamble")) {
196 linebuf = split(linebuf, tmp, ',');
197 tmp = frontStrip(tmp);
199 keys.push_back(pair<string,string>(tmp,string()));
202 } else if (!keys.empty()) {
203 keys.back().second += linebuf + "\n";
207 // Get next file name
208 bibfiles = split(bibfiles, tmp, ',');
214 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
216 bv->owner()->getDialogs()->showBibtex(this);
220 bool InsetBibtex::addDatabase(string const & db)
222 string contents(getContents());
223 if (!contains(contents, db)) {
224 if (!contents.empty())
226 setContents(contents + db);
233 bool InsetBibtex::delDatabase(string const & db)
235 if (contains(getContents(), db)) {
237 int const n = tokenPos(getContents(), ',', bd);
239 // Weird code, would someone care to explain this?(Lgb)
242 setContents(subst(getContents(), tmp, ", "));
244 setContents(split(getContents(), bd, ','));
252 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
253 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
256 // Does look like a hack? It is! (but will change at 0.13)
257 LyXParagraph * par = bv->buffer()->paragraph;
261 int const wx = par->bibkey->width(bv, font);
275 string const bibitemWidest(Buffer const * buffer)
278 // Does look like a hack? It is! (but will change at 0.13)
279 LyXParagraph * par = buffer->paragraph;
280 InsetBibKey * bkey = 0;
286 lyxfont::width(par->bibkey->getBibLabel(),
300 if (bkey && !bkey->getBibLabel().empty())
301 return bkey->getBibLabel();