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())
87 InsetCommand::doDispatch(cur, cmd);
93 string const InsetBibtex::getScreenLabel(Buffer const &) const
95 return _("BibTeX Generated Bibliography");
101 string normalize_name(Buffer const & buffer, OutputParams const & runparams,
102 string const & name, string const & ext)
104 string const fname = MakeAbsPath(name, buffer.filePath());
105 if (AbsolutePath(name) || !IsFileReadable(fname + ext))
107 else if (!runparams.nice)
110 return MakeRelPath(fname, buffer.getMasterBuffer()->filePath());
116 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
117 OutputParams const & runparams) const
119 // the sequence of the commands:
120 // 1. \bibliographystyle{style}
121 // 2. \addcontentsline{...} - if option bibtotoc set
122 // 3. \bibliography{database}
123 // and with bibtopic:
124 // 1. \bibliographystyle{style}
125 // 2. \begin{btSect}{database}
126 // 3. \btPrint{Cited|NotCited|All}
129 // the database string
131 string db_in = getContents();
132 db_in = split(db_in, adb, ',');
133 // If we generate in a temp dir, we might need to give an
134 // absolute path there. This is a bit complicated since we can
135 // have a comma-separated list of bibliographies
137 while (!adb.empty()) {
138 db_out += latex_path(normalize_name(buffer, runparams, adb, ".bib"));
140 db_in = split(db_in, adb,',');
142 db_out = rtrim(db_out, ",");
145 string style = getOptions(); // maybe empty! and with bibtotoc
147 if (prefixIs(style, "bibtotoc")) {
148 bibtotoc = "bibtotoc";
149 if (contains(style, ',')) {
150 style = split(style, bibtotoc, ',');
157 if (!style.empty()) {
158 os << "\\bibliographystyle{"
159 << latex_path(normalize_name(buffer, runparams, style, ".bst"))
164 if (buffer.params().use_bibtopic){
165 os << "\\begin{btSect}{" << db_out << "}\n";
166 string btprint = getSecOptions();
169 btprint = "btPrintCited";
170 os << "\\" << btprint << "\n"
171 << "\\end{btSect}\n";
176 if (!bibtotoc.empty() && !buffer.params().use_bibtopic) {
177 // maybe a problem when a textclass has no "art" as
178 // part of its name, because it's than book.
179 // For the "official" lyx-layouts it's no problem to support
181 if (!contains(buffer.params().getLyXTextClass().name(),
183 if (buffer.params().sides == LyXTextClass::OneSide) {
188 os << "\\cleardoublepage";
192 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
196 os << "\\addcontentsline{toc}{section}{\\refname}";
200 if (!buffer.params().use_bibtopic){
201 os << "\\bibliography{" << db_out << "}\n";
209 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
211 Path p(buffer.filePath());
216 string bibfiles = getContents();
217 bibfiles = split(bibfiles, tmp, ',');
218 while (!tmp.empty()) {
219 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
220 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
222 // If we didn't find a matching file name just fail silently
226 // Get next file name
227 bibfiles = split(bibfiles, tmp, ',');
234 // This method returns a comma separated list of Bibtex entries
235 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
236 std::vector<std::pair<string, string> > & keys) const
238 vector<string> const files = getFiles(buffer);
239 for (vector<string>::const_iterator it = files.begin();
240 it != files.end(); ++ it) {
241 // This is a _very_ simple parser for Bibtex database
242 // files. All it does is to look for lines starting
243 // in @ and not being @preamble and @string entries.
244 // It does NOT do any syntax checking!
245 ifstream ifs(it->c_str());
247 while (getline(ifs, linebuf0)) {
248 string linebuf = trim(linebuf0);
249 if (linebuf.empty()) continue;
250 if (prefixIs(linebuf, "@")) {
251 linebuf = subst(linebuf, '{', '(');
253 linebuf = split(linebuf, tmp, '(');
254 tmp = ascii_lowercase(tmp);
255 if (!prefixIs(tmp, "@string")
256 && !prefixIs(tmp, "@preamble")) {
257 linebuf = split(linebuf, tmp, ',');
258 tmp = ltrim(tmp, " \t");
260 keys.push_back(pair<string,string>(tmp,string()));
263 } else if (!keys.empty()) {
264 keys.back().second += linebuf + "\n";
271 bool InsetBibtex::addDatabase(string const & db)
273 string contents(getContents());
274 if (!contains(contents, db)) {
275 if (!contents.empty())
277 setContents(contents + db);
284 bool InsetBibtex::delDatabase(string const & db)
286 if (contains(getContents(), db)) {
288 int const n = tokenPos(getContents(), ',', bd);
290 // Weird code, would someone care to explain this?(Lgb)
293 setContents(subst(getContents(), tmp, ", "));
295 setContents(split(getContents(), bd, ','));
303 void InsetBibtex::validate(LaTeXFeatures & features) const
305 if (features.bufferParams().use_bibtopic)
306 features.require("bibtopic");