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) const
89 // changing the sequence of the commands
90 // 1. \bibliographystyle{style}
91 // 2. \addcontentsline{...} - if option bibtotoc set
92 // 3. \bibliography{database}
94 string db_in = getContents();
95 db_in = split(db_in, adb, ',');
98 string style = getOptions(); // maybe empty! and with bibtotoc
100 if (prefixIs(style, "bibtotoc")) {
101 bibtotoc = "bibtotoc";
102 if (contains(style, ',')) {
103 style = split(style, bibtotoc, ',');
108 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
109 style = MakeAbsPath(style, buffer->filePath());
112 if (!style.empty()) { // we want no \biblio...{}
113 os << "\\bibliographystyle{" << style << "}\n";
117 if (!bibtotoc.empty()) {
118 // maybe a problem when a textclass has no "art" as
119 // part of its name, because it's than book.
120 // For the "official" lyx-layouts it's no problem to support
122 if (!contains(buffer->params.getLyXTextClass().name(),
124 if (buffer->params.sides == LyXTextClass::OneSide) {
129 os << "\\cleardoublepage";
133 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
137 os << "\\addcontentsline{toc}{section}{\\refname}";
142 // If we generate in a temp dir, we might need to give an
143 // absolute path there. This is a bit complicated since we can
144 // have a comma-separated list of bibliographies
146 while (!adb.empty()) {
147 if (!runparams.nice &&
148 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
149 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
152 db_in = split(db_in, adb,',');
154 db_out = rtrim(db_out, ",");
155 os << "\\bibliography{" << db_out << "}\n";
160 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
162 // Doesn't appear to be used (Angus, 31 July 2001)
163 Path p(buffer.filePath());
168 string bibfiles = getContents();
169 bibfiles = split(bibfiles, tmp, ',');
170 while (!tmp.empty()) {
171 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
172 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
174 // If we didn't find a matching file name just fail silently
178 // Get next file name
179 bibfiles = split(bibfiles, tmp, ',');
186 // This method returns a comma separated list of Bibtex entries
187 void InsetBibtex::fillWithBibKeys
188 (Buffer const * buffer, vector<pair<string, string> > & keys) const
191 vector<string> const files = getFiles(*buffer);
192 for (vector<string>::const_iterator it = files.begin();
193 it != files.end(); ++ it) {
194 // This is a _very_ simple parser for Bibtex database
195 // files. All it does is to look for lines starting
196 // in @ and not being @preamble and @string entries.
197 // It does NOT do any syntax checking!
198 ifstream ifs(it->c_str());
200 while (getline(ifs, linebuf0)) {
201 string linebuf = trim(linebuf0);
202 if (linebuf.empty()) continue;
203 if (prefixIs(linebuf, "@")) {
204 linebuf = subst(linebuf, '{', '(');
206 linebuf = split(linebuf, tmp, '(');
207 tmp = ascii_lowercase(tmp);
208 if (!prefixIs(tmp, "@string")
209 && !prefixIs(tmp, "@preamble")) {
210 linebuf = split(linebuf, tmp, ',');
211 tmp = ltrim(tmp, " \t");
213 keys.push_back(pair<string,string>(tmp,string()));
216 } else if (!keys.empty()) {
217 keys.back().second += linebuf + "\n";
224 bool InsetBibtex::addDatabase(string const & db)
226 string contents(getContents());
227 if (!contains(contents, db)) {
228 if (!contents.empty())
230 setContents(contents + db);
237 bool InsetBibtex::delDatabase(string const & db)
239 if (contains(getContents(), db)) {
241 int const n = tokenPos(getContents(), ',', bd);
243 // Weird code, would someone care to explain this?(Lgb)
246 setContents(subst(getContents(), tmp, ", "));
248 setContents(split(getContents(), bd, ','));