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"
16 #include "bufferparams.h"
18 #include "funcrequest.h"
20 #include "latexrunparams.h"
21 #include "metricsinfo.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26 #include "support/path.h"
30 using lyx::support::ascii_lowercase;
31 using lyx::support::ChangeExtension;
32 using lyx::support::contains;
33 using lyx::support::findtexfile;
34 using lyx::support::IsFileReadable;
35 using lyx::support::ltrim;
36 using lyx::support::MakeAbsPath;
37 using lyx::support::Path;
38 using lyx::support::prefixIs;
39 using lyx::support::rtrim;
40 using lyx::support::split;
41 using lyx::support::subst;
42 using lyx::support::tokenPos;
43 using lyx::support::trim;
45 namespace os = lyx::support::os;
56 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
61 InsetBibtex::~InsetBibtex()
63 InsetCommandMailer("bibtex", *this).hideDialog();
67 std::auto_ptr<InsetBase> InsetBibtex::clone() const
69 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
73 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
75 InsetCommand::metrics(mi, dim);
76 int center_indent = (mi.base.textwidth - dim.wid) / 2;
77 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
79 dim.wid = mi.base.textwidth;
84 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
86 InsetCommand::draw(pi, x + button().box().x1, y);
91 InsetBibtex::priv_dispatch(FuncRequest const & cmd,
92 idx_type & idx, pos_type & pos)
96 case LFUN_INSET_DIALOG_SHOW:
97 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
99 case LFUN_MOUSE_RELEASE:
100 if (button().box().contains(cmd.x, cmd.y))
101 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
104 case LFUN_INSET_MODIFY: {
105 InsetCommandParams p;
106 InsetCommandMailer::string2params(cmd.argument, p);
107 if (p.getCmdName().empty())
114 return InsetCommand::priv_dispatch(cmd, idx, pos);
119 string const InsetBibtex::getScreenLabel(Buffer const &) const
121 return _("BibTeX Generated References");
125 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
126 LatexRunParams const & runparams) const
128 // changing the sequence of the commands
129 // 1. \bibliographystyle{style}
130 // 2. \addcontentsline{...} - if option bibtotoc set
131 // 3. \bibliography{database}
133 string db_in = getContents();
134 db_in = split(db_in, adb, ',');
137 string style = getOptions(); // maybe empty! and with bibtotoc
139 if (prefixIs(style, "bibtotoc")) {
140 bibtotoc = "bibtotoc";
141 if (contains(style, ',')) {
142 style = split(style, bibtotoc, ',');
147 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
148 style = MakeAbsPath(style, buffer.filePath());
151 if (!style.empty()) { // we want no \biblio...{}
152 os << "\\bibliographystyle{" << style << "}\n";
156 if (!bibtotoc.empty()) {
157 // maybe a problem when a textclass has no "art" as
158 // part of its name, because it's than book.
159 // For the "official" lyx-layouts it's no problem to support
161 if (!contains(buffer.params().getLyXTextClass().name(),
163 if (buffer.params().sides == LyXTextClass::OneSide) {
168 os << "\\cleardoublepage";
172 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
176 os << "\\addcontentsline{toc}{section}{\\refname}";
181 // If we generate in a temp dir, we might need to give an
182 // absolute path there. This is a bit complicated since we can
183 // have a comma-separated list of bibliographies
185 while (!adb.empty()) {
186 if (!runparams.nice &&
187 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
188 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
191 db_in = split(db_in, adb,',');
193 db_out = rtrim(db_out, ",");
194 os << "\\bibliography{" << db_out << "}\n";
199 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
201 Path p(buffer.filePath());
206 string bibfiles = getContents();
207 bibfiles = split(bibfiles, tmp, ',');
208 while (!tmp.empty()) {
209 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
210 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
212 // If we didn't find a matching file name just fail silently
216 // Get next file name
217 bibfiles = split(bibfiles, tmp, ',');
224 // This method returns a comma separated list of Bibtex entries
225 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
226 std::vector<std::pair<string, string> > & keys) const
228 vector<string> const files = getFiles(buffer);
229 for (vector<string>::const_iterator it = files.begin();
230 it != files.end(); ++ it) {
231 // This is a _very_ simple parser for Bibtex database
232 // files. All it does is to look for lines starting
233 // in @ and not being @preamble and @string entries.
234 // It does NOT do any syntax checking!
235 ifstream ifs(it->c_str());
237 while (getline(ifs, linebuf0)) {
238 string linebuf = trim(linebuf0);
239 if (linebuf.empty()) continue;
240 if (prefixIs(linebuf, "@")) {
241 linebuf = subst(linebuf, '{', '(');
243 linebuf = split(linebuf, tmp, '(');
244 tmp = ascii_lowercase(tmp);
245 if (!prefixIs(tmp, "@string")
246 && !prefixIs(tmp, "@preamble")) {
247 linebuf = split(linebuf, tmp, ',');
248 tmp = ltrim(tmp, " \t");
250 keys.push_back(pair<string,string>(tmp,string()));
253 } else if (!keys.empty()) {
254 keys.back().second += linebuf + "\n";
261 bool InsetBibtex::addDatabase(string const & db)
263 string contents(getContents());
264 if (!contains(contents, db)) {
265 if (!contents.empty())
267 setContents(contents + db);
274 bool InsetBibtex::delDatabase(string const & db)
276 if (contains(getContents(), db)) {
278 int const n = tokenPos(getContents(), ',', bd);
280 // Weird code, would someone care to explain this?(Lgb)
283 setContents(subst(getContents(), tmp, ", "));
285 setContents(split(getContents(), bd, ','));