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);
90 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
94 case LFUN_INSET_DIALOG_SHOW:
95 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
97 case LFUN_MOUSE_RELEASE:
98 if (button().box().contains(cmd.x, cmd.y))
99 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
102 case LFUN_INSET_MODIFY: {
103 InsetCommandParams p;
104 InsetCommandMailer::string2params(cmd.argument, p);
105 if (p.getCmdName().empty())
112 return InsetCommand::localDispatch(cmd);
117 string const InsetBibtex::getScreenLabel(Buffer const &) const
119 return _("BibTeX Generated References");
123 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
124 LatexRunParams const & runparams) const
126 // changing the sequence of the commands
127 // 1. \bibliographystyle{style}
128 // 2. \addcontentsline{...} - if option bibtotoc set
129 // 3. \bibliography{database}
131 string db_in = getContents();
132 db_in = split(db_in, adb, ',');
135 string style = getOptions(); // maybe empty! and with bibtotoc
137 if (prefixIs(style, "bibtotoc")) {
138 bibtotoc = "bibtotoc";
139 if (contains(style, ',')) {
140 style = split(style, bibtotoc, ',');
145 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
146 style = MakeAbsPath(style, buffer.filePath());
149 if (!style.empty()) { // we want no \biblio...{}
150 os << "\\bibliographystyle{" << style << "}\n";
154 if (!bibtotoc.empty()) {
155 // maybe a problem when a textclass has no "art" as
156 // part of its name, because it's than book.
157 // For the "official" lyx-layouts it's no problem to support
159 if (!contains(buffer.params().getLyXTextClass().name(),
161 if (buffer.params().sides == LyXTextClass::OneSide) {
166 os << "\\cleardoublepage";
170 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
174 os << "\\addcontentsline{toc}{section}{\\refname}";
179 // If we generate in a temp dir, we might need to give an
180 // absolute path there. This is a bit complicated since we can
181 // have a comma-separated list of bibliographies
183 while (!adb.empty()) {
184 if (!runparams.nice &&
185 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
186 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
189 db_in = split(db_in, adb,',');
191 db_out = rtrim(db_out, ",");
192 os << "\\bibliography{" << db_out << "}\n";
197 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
199 Path p(buffer.filePath());
204 string bibfiles = getContents();
205 bibfiles = split(bibfiles, tmp, ',');
206 while (!tmp.empty()) {
207 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
208 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
210 // If we didn't find a matching file name just fail silently
214 // Get next file name
215 bibfiles = split(bibfiles, tmp, ',');
222 // This method returns a comma separated list of Bibtex entries
223 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
224 std::vector<std::pair<string, string> > & keys) const
226 vector<string> const files = getFiles(buffer);
227 for (vector<string>::const_iterator it = files.begin();
228 it != files.end(); ++ it) {
229 // This is a _very_ simple parser for Bibtex database
230 // files. All it does is to look for lines starting
231 // in @ and not being @preamble and @string entries.
232 // It does NOT do any syntax checking!
233 ifstream ifs(it->c_str());
235 while (getline(ifs, linebuf0)) {
236 string linebuf = trim(linebuf0);
237 if (linebuf.empty()) continue;
238 if (prefixIs(linebuf, "@")) {
239 linebuf = subst(linebuf, '{', '(');
241 linebuf = split(linebuf, tmp, '(');
242 tmp = ascii_lowercase(tmp);
243 if (!prefixIs(tmp, "@string")
244 && !prefixIs(tmp, "@preamble")) {
245 linebuf = split(linebuf, tmp, ',');
246 tmp = ltrim(tmp, " \t");
248 keys.push_back(pair<string,string>(tmp,string()));
251 } else if (!keys.empty()) {
252 keys.back().second += linebuf + "\n";
259 bool InsetBibtex::addDatabase(string const & db)
261 string contents(getContents());
262 if (!contains(contents, db)) {
263 if (!contents.empty())
265 setContents(contents + db);
272 bool InsetBibtex::delDatabase(string const & db)
274 if (contains(getContents(), db)) {
276 int const n = tokenPos(getContents(), ',', bd);
278 // Weird code, would someone care to explain this?(Lgb)
281 setContents(subst(getContents(), tmp, ", "));
283 setContents(split(getContents(), bd, ','));