3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * Full author contact details are available in file CREDITS.
13 #include "insetbibtex.h"
14 #include "metricsinfo.h"
16 #include "BufferView.h"
18 #include "funcrequest.h"
20 #include "latexrunparams.h"
22 #include "support/filetools.h"
23 #include "support/path.h"
24 #include "support/os.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
31 using namespace lyx::support;
41 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
46 InsetBibtex::~InsetBibtex()
48 InsetCommandMailer("bibtex", *this).hideDialog();
52 std::auto_ptr<InsetBase> InsetBibtex::clone() const
54 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
58 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
60 InsetCommand::metrics(mi, dim);
61 center_indent_ = (mi.base.textwidth - dim.wid) / 2;
62 dim.wid = mi.base.textwidth;
67 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
69 InsetCommand::draw(pi, x + center_indent_, y);
73 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
78 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
81 case LFUN_INSET_MODIFY: {
83 InsetCommandMailer::string2params(cmd.argument, p);
84 if (p.getCmdName().empty())
91 return InsetCommand::localDispatch(cmd);
96 string const InsetBibtex::getScreenLabel(Buffer const &) const
98 return _("BibTeX Generated References");
102 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
103 LatexRunParams const & runparams) const
105 // changing the sequence of the commands
106 // 1. \bibliographystyle{style}
107 // 2. \addcontentsline{...} - if option bibtotoc set
108 // 3. \bibliography{database}
110 string db_in = getContents();
111 db_in = split(db_in, adb, ',');
114 string style = getOptions(); // maybe empty! and with bibtotoc
116 if (prefixIs(style, "bibtotoc")) {
117 bibtotoc = "bibtotoc";
118 if (contains(style, ',')) {
119 style = split(style, bibtotoc, ',');
124 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
125 style = MakeAbsPath(style, buffer.filePath());
128 if (!style.empty()) { // we want no \biblio...{}
129 os << "\\bibliographystyle{" << style << "}\n";
133 if (!bibtotoc.empty()) {
134 // maybe a problem when a textclass has no "art" as
135 // part of its name, because it's than book.
136 // For the "official" lyx-layouts it's no problem to support
138 if (!contains(buffer.params.getLyXTextClass().name(),
140 if (buffer.params.sides == LyXTextClass::OneSide) {
145 os << "\\cleardoublepage";
149 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
153 os << "\\addcontentsline{toc}{section}{\\refname}";
158 // If we generate in a temp dir, we might need to give an
159 // absolute path there. This is a bit complicated since we can
160 // have a comma-separated list of bibliographies
162 while (!adb.empty()) {
163 if (!runparams.nice &&
164 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
165 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
168 db_in = split(db_in, adb,',');
170 db_out = rtrim(db_out, ",");
171 os << "\\bibliography{" << db_out << "}\n";
176 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
178 Path p(buffer.filePath());
183 string bibfiles = getContents();
184 bibfiles = split(bibfiles, tmp, ',');
185 while (!tmp.empty()) {
186 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
187 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
189 // If we didn't find a matching file name just fail silently
193 // Get next file name
194 bibfiles = split(bibfiles, tmp, ',');
201 // This method returns a comma separated list of Bibtex entries
202 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
203 std::vector<std::pair<string, string> > & keys) const
205 vector<string> const files = getFiles(buffer);
206 for (vector<string>::const_iterator it = files.begin();
207 it != files.end(); ++ it) {
208 // This is a _very_ simple parser for Bibtex database
209 // files. All it does is to look for lines starting
210 // in @ and not being @preamble and @string entries.
211 // It does NOT do any syntax checking!
212 ifstream ifs(it->c_str());
214 while (getline(ifs, linebuf0)) {
215 string linebuf = trim(linebuf0);
216 if (linebuf.empty()) continue;
217 if (prefixIs(linebuf, "@")) {
218 linebuf = subst(linebuf, '{', '(');
220 linebuf = split(linebuf, tmp, '(');
221 tmp = ascii_lowercase(tmp);
222 if (!prefixIs(tmp, "@string")
223 && !prefixIs(tmp, "@preamble")) {
224 linebuf = split(linebuf, tmp, ',');
225 tmp = ltrim(tmp, " \t");
227 keys.push_back(pair<string,string>(tmp,string()));
230 } else if (!keys.empty()) {
231 keys.back().second += linebuf + "\n";
238 bool InsetBibtex::addDatabase(string const & db)
240 string contents(getContents());
241 if (!contains(contents, db)) {
242 if (!contents.empty())
244 setContents(contents + db);
251 bool InsetBibtex::delDatabase(string const & db)
253 if (contains(getContents(), db)) {
255 int const n = tokenPos(getContents(), ',', bd);
257 // Weird code, would someone care to explain this?(Lgb)
260 setContents(subst(getContents(), tmp, ", "));
262 setContents(split(getContents(), bd, ','));