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));
57 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
59 InsetCommand::metrics(mi, dim);
60 center_indent_ = (mi.base.textwidth - dim.wid) / 2;
61 dim.wid = mi.base.textwidth;
66 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
68 InsetCommand::draw(pi, x + center_indent_, y);
72 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
77 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
80 case LFUN_INSET_MODIFY: {
82 InsetCommandMailer::string2params(cmd.argument, p);
83 if (p.getCmdName().empty())
90 return InsetCommand::localDispatch(cmd);
95 string const InsetBibtex::getScreenLabel(Buffer const *) const
97 return _("BibTeX Generated References");
101 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
102 LatexRunParams const & runparams) const
104 // changing the sequence of the commands
105 // 1. \bibliographystyle{style}
106 // 2. \addcontentsline{...} - if option bibtotoc set
107 // 3. \bibliography{database}
109 string db_in = getContents();
110 db_in = split(db_in, adb, ',');
113 string style = getOptions(); // maybe empty! and with bibtotoc
115 if (prefixIs(style, "bibtotoc")) {
116 bibtotoc = "bibtotoc";
117 if (contains(style, ',')) {
118 style = split(style, bibtotoc, ',');
123 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
124 style = MakeAbsPath(style, buffer->filePath());
127 if (!style.empty()) { // we want no \biblio...{}
128 os << "\\bibliographystyle{" << style << "}\n";
132 if (!bibtotoc.empty()) {
133 // maybe a problem when a textclass has no "art" as
134 // part of its name, because it's than book.
135 // For the "official" lyx-layouts it's no problem to support
137 if (!contains(buffer->params.getLyXTextClass().name(),
139 if (buffer->params.sides == LyXTextClass::OneSide) {
144 os << "\\cleardoublepage";
148 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
152 os << "\\addcontentsline{toc}{section}{\\refname}";
157 // If we generate in a temp dir, we might need to give an
158 // absolute path there. This is a bit complicated since we can
159 // have a comma-separated list of bibliographies
161 while (!adb.empty()) {
162 if (!runparams.nice &&
163 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
164 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
167 db_in = split(db_in, adb,',');
169 db_out = rtrim(db_out, ",");
170 os << "\\bibliography{" << db_out << "}\n";
175 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
177 Path p(buffer.filePath());
182 string bibfiles = getContents();
183 bibfiles = split(bibfiles, tmp, ',');
184 while (!tmp.empty()) {
185 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
186 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
188 // If we didn't find a matching file name just fail silently
192 // Get next file name
193 bibfiles = split(bibfiles, tmp, ',');
200 // This method returns a comma separated list of Bibtex entries
201 void InsetBibtex::fillWithBibKeys(Buffer const * buffer,
202 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, ','));