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"
20 #include "support/filetools.h"
21 #include "support/path.h"
22 #include "support/os.h"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
37 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
42 InsetBibtex::~InsetBibtex()
44 InsetCommandMailer mailer("bibtex", *this);
49 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
54 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
57 case LFUN_INSET_MODIFY: {
59 InsetCommandMailer::string2params(cmd.argument, p);
60 if (p.getCmdName().empty())
63 if (view() && p.getContents() != params().getContents()) {
64 view()->ChangeCitationsIfUnique(params().getContents(),
69 cmd.view()->updateInset(this);
74 return InsetCommand::localDispatch(cmd);
79 string const InsetBibtex::getScreenLabel(Buffer const *) const
81 return _("BibTeX Generated References");
85 int InsetBibtex::latex(Buffer const * buffer, ostream & os, LatexRunParams const &,
86 bool /*fragile*/, bool/*fs*/) const
88 // changing the sequence of the commands
89 // 1. \bibliographystyle{style}
90 // 2. \addcontentsline{...} - if option bibtotoc set
91 // 3. \bibliography{database}
93 string db_in = getContents();
94 db_in = split(db_in, adb, ',');
97 string style = getOptions(); // maybe empty! and with bibtotoc
99 if (prefixIs(style, "bibtotoc")) {
100 bibtotoc = "bibtotoc";
101 if (contains(style, ',')) {
102 style = split(style, bibtotoc, ',');
106 if (!buffer->niceFile
107 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
108 style = MakeAbsPath(style, buffer->filePath());
111 if (!style.empty()) { // we want no \biblio...{}
112 os << "\\bibliographystyle{" << style << "}\n";
116 if (!bibtotoc.empty()) {
117 // maybe a problem when a textclass has no "art" as
118 // part of its name, because it's than book.
119 // For the "official" lyx-layouts it's no problem to support
121 if (!contains(buffer->params.getLyXTextClass().name(),
123 if (buffer->params.sides == LyXTextClass::OneSide) {
128 os << "\\cleardoublepage";
132 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
136 os << "\\addcontentsline{toc}{section}{\\refname}";
141 // If we generate in a temp dir, we might need to give an
142 // absolute path there. This is a bit complicated since we can
143 // have a comma-separated list of bibliographies
145 while (!adb.empty()) {
146 if (!buffer->niceFile &&
147 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
148 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
151 db_in = split(db_in, adb,',');
153 db_out = rtrim(db_out, ",");
154 os << "\\bibliography{" << db_out << "}\n";
159 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
161 // Doesn't appear to be used (Angus, 31 July 2001)
162 Path p(buffer.filePath());
167 string bibfiles = getContents();
168 bibfiles = split(bibfiles, tmp, ',');
169 while (!tmp.empty()) {
170 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
171 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
173 // If we didn't find a matching file name just fail silently
177 // Get next file name
178 bibfiles = split(bibfiles, tmp, ',');
185 // This method returns a comma separated list of Bibtex entries
186 void InsetBibtex::fillWithBibKeys
187 (Buffer const * buffer, vector<pair<string, string> > & keys) const
190 vector<string> const files = getFiles(*buffer);
191 for (vector<string>::const_iterator it = files.begin();
192 it != files.end(); ++ it) {
193 // This is a _very_ simple parser for Bibtex database
194 // files. All it does is to look for lines starting
195 // in @ and not being @preamble and @string entries.
196 // It does NOT do any syntax checking!
197 ifstream ifs(it->c_str());
199 while (getline(ifs, linebuf0)) {
200 string linebuf = trim(linebuf0);
201 if (linebuf.empty()) continue;
202 if (prefixIs(linebuf, "@")) {
203 linebuf = subst(linebuf, '{', '(');
205 linebuf = split(linebuf, tmp, '(');
206 tmp = ascii_lowercase(tmp);
207 if (!prefixIs(tmp, "@string")
208 && !prefixIs(tmp, "@preamble")) {
209 linebuf = split(linebuf, tmp, ',');
210 tmp = ltrim(tmp, " \t");
212 keys.push_back(pair<string,string>(tmp,string()));
215 } else if (!keys.empty()) {
216 keys.back().second += linebuf + "\n";
223 bool InsetBibtex::addDatabase(string const & db)
225 string contents(getContents());
226 if (!contains(contents, db)) {
227 if (!contents.empty())
229 setContents(contents + db);
236 bool InsetBibtex::delDatabase(string const & db)
238 if (contains(getContents(), db)) {
240 int const n = tokenPos(getContents(), ',', bd);
242 // Weird code, would someone care to explain this?(Lgb)
245 setContents(subst(getContents(), tmp, ", "));
247 setContents(split(getContents(), bd, ','));