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::latex_path;
39 using lyx::support::ltrim;
40 using lyx::support::MakeAbsPath;
41 using lyx::support::MakeRelPath;
42 using lyx::support::Path;
43 using lyx::support::prefixIs;
44 using lyx::support::rtrim;
45 using lyx::support::split;
46 using lyx::support::subst;
47 using lyx::support::tokenPos;
48 using lyx::support::trim;
50 namespace os = lyx::support::os;
61 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
62 : InsetCommand(p, "bibtex")
66 std::auto_ptr<InsetBase> InsetBibtex::doClone() const
68 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
72 void InsetBibtex::doDispatch(LCursor & cur, FuncRequest & cmd)
76 case LFUN_INSET_MODIFY: {
78 InsetCommandMailer::string2params("bibtex", cmd.argument, p);
79 if (!p.getCmdName().empty())
85 InsetCommand::doDispatch(cur, cmd);
91 string const InsetBibtex::getScreenLabel(Buffer const &) const
93 return _("BibTeX Generated References");
99 string normalize_name(Buffer const & buffer, OutputParams const & runparams,
100 string const & name, string const & ext)
102 string const fname = MakeAbsPath(name, buffer.filePath());
103 if (AbsolutePath(name) || !IsFileReadable(fname + ext))
105 else if (!runparams.nice)
108 return MakeRelPath(fname, buffer.getMasterBuffer()->filePath());
114 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
115 OutputParams const & runparams) const
117 // the sequence of the commands:
118 // 1. \bibliographystyle{style}
119 // 2. \addcontentsline{...} - if option bibtotoc set
120 // 3. \bibliography{database}
121 // and with bibtopic:
122 // 1. \bibliographystyle{style}
123 // 2. \begin{btSect}{database}
124 // 3. \btPrint{Cited|NotCited|All}
127 // the database string
129 string db_in = getContents();
130 db_in = split(db_in, adb, ',');
131 // If we generate in a temp dir, we might need to give an
132 // absolute path there. This is a bit complicated since we can
133 // have a comma-separated list of bibliographies
135 while (!adb.empty()) {
136 db_out += latex_path(normalize_name(buffer, runparams, adb, ".bib"));
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 << latex_path(normalize_name(buffer, runparams, style, ".bst"))
162 if (buffer.params().use_bibtopic){
163 os << "\\begin{btSect}{" << db_out << "}\n";
164 string btprint = getSecOptions();
167 btprint = "btPrintCited";
168 os << "\\" << btprint << "\n"
169 << "\\end{btSect}\n";
174 if (!bibtotoc.empty() && !buffer.params().use_bibtopic) {
175 // maybe a problem when a textclass has no "art" as
176 // part of its name, because it's than book.
177 // For the "official" lyx-layouts it's no problem to support
179 if (!contains(buffer.params().getLyXTextClass().name(),
181 if (buffer.params().sides == LyXTextClass::OneSide) {
186 os << "\\cleardoublepage";
190 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
194 os << "\\addcontentsline{toc}{section}{\\refname}";
198 if (!buffer.params().use_bibtopic){
199 os << "\\bibliography{" << db_out << "}\n";
207 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
209 Path p(buffer.filePath());
214 string bibfiles = getContents();
215 bibfiles = split(bibfiles, tmp, ',');
216 while (!tmp.empty()) {
217 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
218 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
220 // If we didn't find a matching file name just fail silently
224 // Get next file name
225 bibfiles = split(bibfiles, tmp, ',');
232 // This method returns a comma separated list of Bibtex entries
233 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
234 std::vector<std::pair<string, string> > & keys) const
236 vector<string> const files = getFiles(buffer);
237 for (vector<string>::const_iterator it = files.begin();
238 it != files.end(); ++ it) {
239 // This is a _very_ simple parser for Bibtex database
240 // files. All it does is to look for lines starting
241 // in @ and not being @preamble and @string entries.
242 // It does NOT do any syntax checking!
243 ifstream ifs(it->c_str());
245 while (getline(ifs, linebuf0)) {
246 string linebuf = trim(linebuf0);
247 if (linebuf.empty()) continue;
248 if (prefixIs(linebuf, "@")) {
249 linebuf = subst(linebuf, '{', '(');
251 linebuf = split(linebuf, tmp, '(');
252 tmp = ascii_lowercase(tmp);
253 if (!prefixIs(tmp, "@string")
254 && !prefixIs(tmp, "@preamble")) {
255 linebuf = split(linebuf, tmp, ',');
256 tmp = ltrim(tmp, " \t");
258 keys.push_back(pair<string,string>(tmp,string()));
261 } else if (!keys.empty()) {
262 keys.back().second += linebuf + "\n";
269 bool InsetBibtex::addDatabase(string const & db)
271 string contents(getContents());
272 if (!contains(contents, db)) {
273 if (!contents.empty())
275 setContents(contents + db);
282 bool InsetBibtex::delDatabase(string const & db)
284 if (contains(getContents(), db)) {
286 int const n = tokenPos(getContents(), ',', bd);
288 // Weird code, would someone care to explain this?(Lgb)
291 setContents(subst(getContents(), tmp, ", "));
293 setContents(split(getContents(), bd, ','));
301 void InsetBibtex::validate(LaTeXFeatures & features) const
303 if (features.bufferParams().use_bibtopic)
304 features.require("bibtopic");