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 "metricsinfo.h"
22 #include "outputparams.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)
58 : InsetCommand(p, "bibtex")
62 std::auto_ptr<InsetBase> InsetBibtex::clone() const
64 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
68 void InsetBibtex::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
72 case LFUN_INSET_MODIFY: {
74 InsetCommandMailer::string2params("bibtex", cmd.argument, p);
75 if (!p.getCmdName().empty())
81 InsetCommand::priv_dispatch(cur, cmd);
87 string const InsetBibtex::getScreenLabel(Buffer const &) const
89 return _("BibTeX Generated References");
93 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
94 OutputParams const & runparams) const
96 // changing the sequence of the commands
97 // 1. \bibliographystyle{style}
98 // 2. \addcontentsline{...} - if option bibtotoc set
99 // 3. \bibliography{database}
101 string db_in = getContents();
102 db_in = split(db_in, adb, ',');
105 string style = getOptions(); // maybe empty! and with bibtotoc
107 if (prefixIs(style, "bibtotoc")) {
108 bibtotoc = "bibtotoc";
109 if (contains(style, ',')) {
110 style = split(style, bibtotoc, ',');
115 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
116 style = MakeAbsPath(style, buffer.filePath());
119 if (!style.empty()) { // we want no \biblio...{}
120 os << "\\bibliographystyle{" << style << "}\n";
124 if (!bibtotoc.empty()) {
125 // maybe a problem when a textclass has no "art" as
126 // part of its name, because it's than book.
127 // For the "official" lyx-layouts it's no problem to support
129 if (!contains(buffer.params().getLyXTextClass().name(),
131 if (buffer.params().sides == LyXTextClass::OneSide) {
136 os << "\\cleardoublepage";
140 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
144 os << "\\addcontentsline{toc}{section}{\\refname}";
149 // If we generate in a temp dir, we might need to give an
150 // absolute path there. This is a bit complicated since we can
151 // have a comma-separated list of bibliographies
153 while (!adb.empty()) {
154 if (!runparams.nice &&
155 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
156 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
159 db_in = split(db_in, adb,',');
161 db_out = rtrim(db_out, ",");
162 os << "\\bibliography{" << db_out << "}\n";
167 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
169 Path p(buffer.filePath());
174 string bibfiles = getContents();
175 bibfiles = split(bibfiles, tmp, ',');
176 while (!tmp.empty()) {
177 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
178 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
180 // If we didn't find a matching file name just fail silently
184 // Get next file name
185 bibfiles = split(bibfiles, tmp, ',');
192 // This method returns a comma separated list of Bibtex entries
193 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
194 std::vector<std::pair<string, string> > & keys) const
196 vector<string> const files = getFiles(buffer);
197 for (vector<string>::const_iterator it = files.begin();
198 it != files.end(); ++ it) {
199 // This is a _very_ simple parser for Bibtex database
200 // files. All it does is to look for lines starting
201 // in @ and not being @preamble and @string entries.
202 // It does NOT do any syntax checking!
203 ifstream ifs(it->c_str());
205 while (getline(ifs, linebuf0)) {
206 string linebuf = trim(linebuf0);
207 if (linebuf.empty()) continue;
208 if (prefixIs(linebuf, "@")) {
209 linebuf = subst(linebuf, '{', '(');
211 linebuf = split(linebuf, tmp, '(');
212 tmp = ascii_lowercase(tmp);
213 if (!prefixIs(tmp, "@string")
214 && !prefixIs(tmp, "@preamble")) {
215 linebuf = split(linebuf, tmp, ',');
216 tmp = ltrim(tmp, " \t");
218 keys.push_back(pair<string,string>(tmp,string()));
221 } else if (!keys.empty()) {
222 keys.back().second += linebuf + "\n";
229 bool InsetBibtex::addDatabase(string const & db)
231 string contents(getContents());
232 if (!contains(contents, db)) {
233 if (!contents.empty())
235 setContents(contents + db);
242 bool InsetBibtex::delDatabase(string const & db)
244 if (contains(getContents(), db)) {
246 int const n = tokenPos(getContents(), ',', bd);
248 // Weird code, would someone care to explain this?(Lgb)
251 setContents(subst(getContents(), tmp, ", "));
253 setContents(split(getContents(), bd, ','));