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)
43 // InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
44 // : InsetCommand(p, false)
48 InsetBibtex::~InsetBibtex()
50 InsetCommandMailer mailer("bibtex", *this);
55 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
60 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
63 case LFUN_INSET_MODIFY: {
65 InsetCommandMailer::string2params(cmd.argument, p);
66 if (p.getCmdName().empty())
69 if (view() && p.getContents() != params().getContents()) {
70 view()->ChangeCitationsIfUnique(params().getContents(),
75 cmd.view()->updateInset(this);
80 return InsetCommand::localDispatch(cmd);
85 string const InsetBibtex::getScreenLabel(Buffer const *) const
87 return _("BibTeX Generated References");
91 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
92 LatexRunParams const & runparams) const
94 // changing the sequence of the commands
95 // 1. \bibliographystyle{style}
96 // 2. \addcontentsline{...} - if option bibtotoc set
97 // 3. \bibliography{database}
99 string db_in = getContents();
100 db_in = split(db_in, adb, ',');
103 string style = getOptions(); // maybe empty! and with bibtotoc
105 if (prefixIs(style, "bibtotoc")) {
106 bibtotoc = "bibtotoc";
107 if (contains(style, ',')) {
108 style = split(style, bibtotoc, ',');
113 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
114 style = MakeAbsPath(style, buffer->filePath());
117 if (!style.empty()) { // we want no \biblio...{}
118 os << "\\bibliographystyle{" << style << "}\n";
122 if (!bibtotoc.empty()) {
123 // maybe a problem when a textclass has no "art" as
124 // part of its name, because it's than book.
125 // For the "official" lyx-layouts it's no problem to support
127 if (!contains(buffer->params.getLyXTextClass().name(),
129 if (buffer->params.sides == LyXTextClass::OneSide) {
134 os << "\\cleardoublepage";
138 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
142 os << "\\addcontentsline{toc}{section}{\\refname}";
147 // If we generate in a temp dir, we might need to give an
148 // absolute path there. This is a bit complicated since we can
149 // have a comma-separated list of bibliographies
151 while (!adb.empty()) {
152 if (!runparams.nice &&
153 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
154 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
157 db_in = split(db_in, adb,',');
159 db_out = rtrim(db_out, ",");
160 os << "\\bibliography{" << db_out << "}\n";
165 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
167 // Doesn't appear to be used (Angus, 31 July 2001)
168 Path p(buffer.filePath());
173 string bibfiles = getContents();
174 bibfiles = split(bibfiles, tmp, ',');
175 while (!tmp.empty()) {
176 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
177 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
179 // If we didn't find a matching file name just fail silently
183 // Get next file name
184 bibfiles = split(bibfiles, tmp, ',');
191 // This method returns a comma separated list of Bibtex entries
192 void InsetBibtex::fillWithBibKeys
193 (Buffer const * buffer, vector<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, ','));