10 #include FORMS_H_LOCATION
11 #include "frontends/Dialogs.h"
15 #include "BufferView.h"
18 #include "support/filetools.h"
19 #include "support/path.h"
20 #include "support/os.h"
33 int InsetBibKey::key_counter = 0;
34 const string key_prefix = "key-";
36 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
40 if (getContents().empty())
41 setContents(key_prefix + tostr(++key_counter));
45 InsetBibKey::~InsetBibKey()
50 Inset * InsetBibKey::Clone(Buffer const &) 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)
87 token = lex.GetString();
90 lex.printError("InsetCommand: Parse error: `$$Token'");
92 if (prefixIs(getContents(), key_prefix)) {
93 int key = strToInt(getContents().substr(key_prefix.length()));
94 key_counter = max(key_counter, key);
98 string const InsetBibKey::getBibLabel() const
100 if (! getOptions().empty())
102 return tostr(counter);
105 string const InsetBibKey::getScreenLabel() const
107 return getContents() + " [" + getBibLabel() + "]";
111 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
113 bv->owner()->getDialogs()->showBibitem(this);
117 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
122 InsetBibtex::~InsetBibtex()
127 string const InsetBibtex::getScreenLabel() const
129 return _("BibTeX Generated References");
133 int InsetBibtex::Latex(Buffer const * buffer, ostream & os,
134 bool /*fragile*/, bool/*fs*/) const
136 // If we generate in a temp dir, we might need to give an
137 // absolute path there. This is a bit complicated since we can
138 // have a comma-separated list of bibliographies
140 string db_in = getContents();
141 db_in = split(db_in, adb, ',');
142 while(!adb.empty()) {
143 if (!buffer->niceFile &&
144 IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib"))
145 adb = os::external_path(MakeAbsPath(adb, buffer->filepath));
149 db_in= split(db_in, adb,',');
151 db_out = strip(db_out, ',');
154 if (!buffer->niceFile
155 && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
157 style = MakeAbsPath(getOptions(), buffer->filepath);
159 style = getOptions();
161 os << "\\bibliographystyle{" << style << "}\n"
162 << "\\bibliography{" << db_out << "}\n";
167 // This method returns a comma separated list of Bibtex entries
168 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
170 Path p(buffer->filepath);
172 vector<pair<string,string> > keys;
174 string bibfiles = getContents();
175 bibfiles = split(bibfiles, tmp, ',');
176 while(!tmp.empty()) {
177 string fil = findtexfile(ChangeExtension(tmp, "bib"),
179 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
180 // If we didn't find a matching file name just fail silently
182 // This is a _very_ simple parser for Bibtex database
183 // files. All it does is to look for lines starting
184 // in @ and not being @preamble and @string entries.
185 // It does NOT do any syntax checking!
186 ifstream ifs(fil.c_str());
188 while (getline(ifs, linebuf0)) {
189 string linebuf = frontStrip(strip(linebuf0));
190 if (linebuf.empty() ) continue;
191 if (prefixIs(linebuf, "@")) {
192 linebuf = subst(linebuf, '{', '(');
193 linebuf = split(linebuf, tmp, '(');
194 tmp = lowercase(tmp);
195 if (!prefixIs(tmp, "@string")
196 && !prefixIs(tmp, "@preamble")) {
197 linebuf = split(linebuf, tmp, ',');
198 tmp = frontStrip(tmp);
200 keys.push_back(pair<string,string>(tmp,string()));
203 } else if (!keys.empty()) {
204 keys.back().second += linebuf + "\n";
208 // Get next file name
209 bibfiles = split(bibfiles, tmp, ',');
215 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
217 bv->owner()->getDialogs()->showBibtex(this);
221 bool InsetBibtex::addDatabase(string const & db)
223 string contents(getContents());
224 if (!contains(contents, db)) {
225 if (!contents.empty())
227 setContents(contents + db);
234 bool InsetBibtex::delDatabase(string const & db)
236 if (contains(getContents(), db)) {
238 int const n = tokenPos(getContents(), ',', bd);
240 // Weird code, would someone care to explain this?(Lgb)
243 setContents(subst(getContents(), tmp, ", "));
245 setContents(split(getContents(), bd, ','));
253 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
254 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
257 // Does look like a hack? It is! (but will change at 0.13)
258 LyXParagraph * par = bv->buffer()->paragraph;
262 int const wx = par->bibkey->width(bv, font);
272 string const bibitemWidest(Buffer const * buffer)
275 // Does look like a hack? It is! (but will change at 0.13)
276 LyXParagraph * par = buffer->paragraph;
277 InsetBibKey * bkey = 0;
283 lyxfont::width(par->bibkey->getBibLabel(),
293 if (bkey && !bkey->getBibLabel().empty())
294 return bkey->getBibLabel();