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"
17 #include "dispatchresult.h"
19 #include "funcrequest.h"
21 #include "latexrunparams.h"
22 #include "metricsinfo.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/path.h"
31 using lyx::support::ascii_lowercase;
32 using lyx::support::ChangeExtension;
33 using lyx::support::contains;
34 using lyx::support::findtexfile;
35 using lyx::support::IsFileReadable;
36 using lyx::support::ltrim;
37 using lyx::support::MakeAbsPath;
38 using lyx::support::Path;
39 using lyx::support::prefixIs;
40 using lyx::support::rtrim;
41 using lyx::support::split;
42 using lyx::support::subst;
43 using lyx::support::tokenPos;
44 using lyx::support::trim;
46 namespace os = lyx::support::os;
57 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
62 InsetBibtex::~InsetBibtex()
64 InsetCommandMailer("bibtex", *this).hideDialog();
68 std::auto_ptr<InsetBase> InsetBibtex::clone() const
70 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
74 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
76 InsetCommand::metrics(mi, dim);
77 int center_indent = (mi.base.textwidth - dim.wid) / 2;
78 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
80 dim.wid = mi.base.textwidth;
85 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
87 InsetCommand::draw(pi, x + button().box().x1, y);
92 InsetBibtex::priv_dispatch(FuncRequest const & cmd,
93 idx_type & idx, pos_type & pos)
97 case LFUN_INSET_DIALOG_SHOW:
98 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
100 case LFUN_MOUSE_RELEASE:
101 if (button().box().contains(cmd.x, cmd.y))
102 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
105 case LFUN_INSET_MODIFY: {
106 InsetCommandParams p;
107 InsetCommandMailer::string2params(cmd.argument, p);
108 if (p.getCmdName().empty())
115 return InsetCommand::priv_dispatch(cmd, idx, pos);
120 string const InsetBibtex::getScreenLabel(Buffer const &) const
122 return _("BibTeX Generated References");
126 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
127 LatexRunParams const & runparams) const
129 // changing the sequence of the commands
130 // 1. \bibliographystyle{style}
131 // 2. \addcontentsline{...} - if option bibtotoc set
132 // 3. \bibliography{database}
134 string db_in = getContents();
135 db_in = split(db_in, adb, ',');
138 string style = getOptions(); // maybe empty! and with bibtotoc
140 if (prefixIs(style, "bibtotoc")) {
141 bibtotoc = "bibtotoc";
142 if (contains(style, ',')) {
143 style = split(style, bibtotoc, ',');
148 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
149 style = MakeAbsPath(style, buffer.filePath());
152 if (!style.empty()) { // we want no \biblio...{}
153 os << "\\bibliographystyle{" << style << "}\n";
157 if (!bibtotoc.empty()) {
158 // maybe a problem when a textclass has no "art" as
159 // part of its name, because it's than book.
160 // For the "official" lyx-layouts it's no problem to support
162 if (!contains(buffer.params().getLyXTextClass().name(),
164 if (buffer.params().sides == LyXTextClass::OneSide) {
169 os << "\\cleardoublepage";
173 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
177 os << "\\addcontentsline{toc}{section}{\\refname}";
182 // If we generate in a temp dir, we might need to give an
183 // absolute path there. This is a bit complicated since we can
184 // have a comma-separated list of bibliographies
186 while (!adb.empty()) {
187 if (!runparams.nice &&
188 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
189 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
192 db_in = split(db_in, adb,',');
194 db_out = rtrim(db_out, ",");
195 os << "\\bibliography{" << db_out << "}\n";
200 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
202 Path p(buffer.filePath());
207 string bibfiles = getContents();
208 bibfiles = split(bibfiles, tmp, ',');
209 while (!tmp.empty()) {
210 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
211 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
213 // If we didn't find a matching file name just fail silently
217 // Get next file name
218 bibfiles = split(bibfiles, tmp, ',');
225 // This method returns a comma separated list of Bibtex entries
226 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
227 std::vector<std::pair<string, string> > & keys) const
229 vector<string> const files = getFiles(buffer);
230 for (vector<string>::const_iterator it = files.begin();
231 it != files.end(); ++ it) {
232 // This is a _very_ simple parser for Bibtex database
233 // files. All it does is to look for lines starting
234 // in @ and not being @preamble and @string entries.
235 // It does NOT do any syntax checking!
236 ifstream ifs(it->c_str());
238 while (getline(ifs, linebuf0)) {
239 string linebuf = trim(linebuf0);
240 if (linebuf.empty()) continue;
241 if (prefixIs(linebuf, "@")) {
242 linebuf = subst(linebuf, '{', '(');
244 linebuf = split(linebuf, tmp, '(');
245 tmp = ascii_lowercase(tmp);
246 if (!prefixIs(tmp, "@string")
247 && !prefixIs(tmp, "@preamble")) {
248 linebuf = split(linebuf, tmp, ',');
249 tmp = ltrim(tmp, " \t");
251 keys.push_back(pair<string,string>(tmp,string()));
254 } else if (!keys.empty()) {
255 keys.back().second += linebuf + "\n";
262 bool InsetBibtex::addDatabase(string const & db)
264 string contents(getContents());
265 if (!contains(contents, db)) {
266 if (!contents.empty())
268 setContents(contents + db);
275 bool InsetBibtex::delDatabase(string const & db)
277 if (contains(getContents(), db)) {
279 int const n = tokenPos(getContents(), ',', bd);
281 // Weird code, would someone care to explain this?(Lgb)
284 setContents(subst(getContents(), tmp, ", "));
286 setContents(split(getContents(), bd, ','));