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"
20 #include "LaTeXFeatures.h"
22 #include "metricsinfo.h"
23 #include "outputparams.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/path.h"
32 using lyx::support::AbsolutePath;
33 using lyx::support::ascii_lowercase;
34 using lyx::support::ChangeExtension;
35 using lyx::support::contains;
36 using lyx::support::findtexfile;
37 using lyx::support::IsFileReadable;
38 using lyx::support::ltrim;
39 using lyx::support::MakeAbsPath;
40 using lyx::support::MakeRelPath;
41 using lyx::support::Path;
42 using lyx::support::prefixIs;
43 using lyx::support::rtrim;
44 using lyx::support::split;
45 using lyx::support::subst;
46 using lyx::support::tokenPos;
47 using lyx::support::trim;
49 namespace os = lyx::support::os;
60 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
61 : InsetCommand(p, "bibtex")
65 std::auto_ptr<InsetBase> InsetBibtex::doClone() const
67 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
71 void InsetBibtex::priv_dispatch(LCursor & cur, FuncRequest & cmd)
75 case LFUN_INSET_MODIFY: {
77 InsetCommandMailer::string2params("bibtex", cmd.argument, p);
78 if (!p.getCmdName().empty())
84 InsetCommand::priv_dispatch(cur, cmd);
90 string const InsetBibtex::getScreenLabel(Buffer const &) const
92 return _("BibTeX Generated References");
98 string normalize_name(Buffer const & buffer, OutputParams const & runparams,
99 string const & name, string const & ext)
101 string const fname = MakeAbsPath(name, buffer.filePath());
102 if (AbsolutePath(name) || !IsFileReadable(fname + ext))
104 else if (!runparams.nice)
107 return MakeRelPath(fname, buffer.getMasterBuffer()->filePath());
113 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
114 OutputParams const & runparams) const
116 // the sequence of the commands:
117 // 1. \bibliographystyle{style}
118 // 2. \addcontentsline{...} - if option bibtotoc set
119 // 3. \bibliography{database}
120 // and with bibtopic:
121 // 1. \bibliographystyle{style}
122 // 2. \begin{btSect}{database}
123 // 3. \btPrint{Cited|NotCited|All}
126 // the database string
128 string db_in = getContents();
129 db_in = split(db_in, adb, ',');
130 // If we generate in a temp dir, we might need to give an
131 // absolute path there. This is a bit complicated since we can
132 // have a comma-separated list of bibliographies
134 while (!adb.empty()) {
135 db_out += os::external_path(normalize_name(buffer, runparams,
138 db_in = split(db_in, adb,',');
140 db_out = rtrim(db_out, ",");
143 string style = getOptions(); // maybe empty! and with bibtotoc
145 if (prefixIs(style, "bibtotoc")) {
146 bibtotoc = "bibtotoc";
147 if (contains(style, ',')) {
148 style = split(style, bibtotoc, ',');
155 if (!style.empty()) {
156 os << "\\bibliographystyle{"
157 << os::external_path(normalize_name(buffer, runparams,
163 if (buffer.params().use_bibtopic){
164 os << "\\begin{btSect}{" << db_out << "}\n";
165 string btprint = getSecOptions();
168 btprint = "btPrintCited";
169 os << "\\" << btprint << "\n"
170 << "\\end{btSect}\n";
175 if (!bibtotoc.empty() && !buffer.params().use_bibtopic) {
176 // maybe a problem when a textclass has no "art" as
177 // part of its name, because it's than book.
178 // For the "official" lyx-layouts it's no problem to support
180 if (!contains(buffer.params().getLyXTextClass().name(),
182 if (buffer.params().sides == LyXTextClass::OneSide) {
187 os << "\\cleardoublepage";
191 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
195 os << "\\addcontentsline{toc}{section}{\\refname}";
199 if (!buffer.params().use_bibtopic){
200 os << "\\bibliography{" << db_out << "}\n";
208 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
210 Path p(buffer.filePath());
215 string bibfiles = getContents();
216 bibfiles = split(bibfiles, tmp, ',');
217 while (!tmp.empty()) {
218 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
219 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
221 // If we didn't find a matching file name just fail silently
225 // Get next file name
226 bibfiles = split(bibfiles, tmp, ',');
233 // This method returns a comma separated list of Bibtex entries
234 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
235 std::vector<std::pair<string, string> > & keys) const
237 vector<string> const files = getFiles(buffer);
238 for (vector<string>::const_iterator it = files.begin();
239 it != files.end(); ++ it) {
240 // This is a _very_ simple parser for Bibtex database
241 // files. All it does is to look for lines starting
242 // in @ and not being @preamble and @string entries.
243 // It does NOT do any syntax checking!
244 ifstream ifs(it->c_str());
246 while (getline(ifs, linebuf0)) {
247 string linebuf = trim(linebuf0);
248 if (linebuf.empty()) continue;
249 if (prefixIs(linebuf, "@")) {
250 linebuf = subst(linebuf, '{', '(');
252 linebuf = split(linebuf, tmp, '(');
253 tmp = ascii_lowercase(tmp);
254 if (!prefixIs(tmp, "@string")
255 && !prefixIs(tmp, "@preamble")) {
256 linebuf = split(linebuf, tmp, ',');
257 tmp = ltrim(tmp, " \t");
259 keys.push_back(pair<string,string>(tmp,string()));
262 } else if (!keys.empty()) {
263 keys.back().second += linebuf + "\n";
270 bool InsetBibtex::addDatabase(string const & db)
272 string contents(getContents());
273 if (!contains(contents, db)) {
274 if (!contents.empty())
276 setContents(contents + db);
283 bool InsetBibtex::delDatabase(string const & db)
285 if (contains(getContents(), db)) {
287 int const n = tokenPos(getContents(), ',', bd);
289 // Weird code, would someone care to explain this?(Lgb)
292 setContents(subst(getContents(), tmp, ", "));
294 setContents(split(getContents(), bd, ','));
302 void InsetBibtex::validate(LaTeXFeatures & features) const
304 if (features.bufferParams().use_bibtopic)
305 features.require("bibtopic");