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"
15 #include "BufferView.h"
17 #include "funcrequest.h"
19 #include "latexrunparams.h"
21 #include "support/filetools.h"
22 #include "support/path.h"
23 #include "support/os.h"
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
38 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
43 InsetBibtex::~InsetBibtex()
45 InsetCommandMailer mailer("bibtex", *this);
50 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
55 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
58 case LFUN_INSET_MODIFY: {
60 InsetCommandMailer::string2params(cmd.argument, p);
61 if (p.getCmdName().empty())
64 if (view() && p.getContents() != params().getContents()) {
65 view()->ChangeCitationsIfUnique(params().getContents(),
70 cmd.view()->updateInset(this);
75 return InsetCommand::localDispatch(cmd);
80 string const InsetBibtex::getScreenLabel(Buffer const *) const
82 return _("BibTeX Generated References");
86 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
87 LatexRunParams const & runparams,
90 // changing the sequence of the commands
91 // 1. \bibliographystyle{style}
92 // 2. \addcontentsline{...} - if option bibtotoc set
93 // 3. \bibliography{database}
95 string db_in = getContents();
96 db_in = split(db_in, adb, ',');
99 string style = getOptions(); // maybe empty! and with bibtotoc
101 if (prefixIs(style, "bibtotoc")) {
102 bibtotoc = "bibtotoc";
103 if (contains(style, ',')) {
104 style = split(style, bibtotoc, ',');
109 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
110 style = MakeAbsPath(style, buffer->filePath());
113 if (!style.empty()) { // we want no \biblio...{}
114 os << "\\bibliographystyle{" << style << "}\n";
118 if (!bibtotoc.empty()) {
119 // maybe a problem when a textclass has no "art" as
120 // part of its name, because it's than book.
121 // For the "official" lyx-layouts it's no problem to support
123 if (!contains(buffer->params.getLyXTextClass().name(),
125 if (buffer->params.sides == LyXTextClass::OneSide) {
130 os << "\\cleardoublepage";
134 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
138 os << "\\addcontentsline{toc}{section}{\\refname}";
143 // If we generate in a temp dir, we might need to give an
144 // absolute path there. This is a bit complicated since we can
145 // have a comma-separated list of bibliographies
147 while (!adb.empty()) {
148 if (!runparams.nice &&
149 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
150 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
153 db_in = split(db_in, adb,',');
155 db_out = rtrim(db_out, ",");
156 os << "\\bibliography{" << db_out << "}\n";
161 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
163 // Doesn't appear to be used (Angus, 31 July 2001)
164 Path p(buffer.filePath());
169 string bibfiles = getContents();
170 bibfiles = split(bibfiles, tmp, ',');
171 while (!tmp.empty()) {
172 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
173 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
175 // If we didn't find a matching file name just fail silently
179 // Get next file name
180 bibfiles = split(bibfiles, tmp, ',');
187 // This method returns a comma separated list of Bibtex entries
188 void InsetBibtex::fillWithBibKeys
189 (Buffer const * buffer, vector<pair<string, string> > & keys) const
192 vector<string> const files = getFiles(*buffer);
193 for (vector<string>::const_iterator it = files.begin();
194 it != files.end(); ++ it) {
195 // This is a _very_ simple parser for Bibtex database
196 // files. All it does is to look for lines starting
197 // in @ and not being @preamble and @string entries.
198 // It does NOT do any syntax checking!
199 ifstream ifs(it->c_str());
201 while (getline(ifs, linebuf0)) {
202 string linebuf = trim(linebuf0);
203 if (linebuf.empty()) continue;
204 if (prefixIs(linebuf, "@")) {
205 linebuf = subst(linebuf, '{', '(');
207 linebuf = split(linebuf, tmp, '(');
208 tmp = ascii_lowercase(tmp);
209 if (!prefixIs(tmp, "@string")
210 && !prefixIs(tmp, "@preamble")) {
211 linebuf = split(linebuf, tmp, ',');
212 tmp = ltrim(tmp, " \t");
214 keys.push_back(pair<string,string>(tmp,string()));
217 } else if (!keys.empty()) {
218 keys.back().second += linebuf + "\n";
225 bool InsetBibtex::addDatabase(string const & db)
227 string contents(getContents());
228 if (!contains(contents, db)) {
229 if (!contents.empty())
231 setContents(contents + db);
238 bool InsetBibtex::delDatabase(string const & db)
240 if (contains(getContents(), db)) {
242 int const n = tokenPos(getContents(), ',', bd);
244 // Weird code, would someone care to explain this?(Lgb)
247 setContents(subst(getContents(), tmp, ", "));
249 setContents(split(getContents(), bd, ','));