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)
51 Inset::RESULT result = UNDISPATCHED;
54 case LFUN_INSET_MODIFY: {
56 InsetCommandMailer::string2params(cmd.argument, p);
57 if (p.getCmdName().empty())
60 if (view() && p.getContents() != params().getContents()) {
61 view()->ChangeCitationsIfUnique(params().getContents(),
66 cmd.view()->updateInset(this);
71 result = InsetCommand::localDispatch(cmd);
77 string const InsetBibtex::getScreenLabel(Buffer const *) const
79 return _("BibTeX Generated References");
83 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
84 bool /*fragile*/, bool/*fs*/) const
86 // changing the sequence of the commands
87 // 1. \bibliographystyle{style}
88 // 2. \addcontentsline{...} - if option bibtotoc set
89 // 3. \bibliography{database}
91 string db_in = getContents();
92 db_in = split(db_in, adb, ',');
95 string style = getOptions(); // maybe empty! and with bibtotoc
97 if (prefixIs(style, "bibtotoc")) {
98 bibtotoc = "bibtotoc";
99 if (contains(style, ',')) {
100 style = split(style, bibtotoc, ',');
104 if (!buffer->niceFile
105 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
106 style = MakeAbsPath(style, buffer->filePath());
109 if (!style.empty()) { // we want no \biblio...{}
110 os << "\\bibliographystyle{" << style << "}\n";
114 if (!bibtotoc.empty()) {
115 // maybe a problem when a textclass has no "art" as
116 // part of its name, because it's than book.
117 // For the "official" lyx-layouts it's no problem to support
119 if (!contains(buffer->params.getLyXTextClass().name(),
121 if (buffer->params.sides == LyXTextClass::OneSide) {
126 os << "\\cleardoublepage";
130 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
134 os << "\\addcontentsline{toc}{section}{\\refname}";
139 // If we generate in a temp dir, we might need to give an
140 // absolute path there. This is a bit complicated since we can
141 // have a comma-separated list of bibliographies
143 while (!adb.empty()) {
144 if (!buffer->niceFile &&
145 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
146 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
149 db_in = split(db_in, adb,',');
151 db_out = rtrim(db_out, ",");
152 os << "\\bibliography{" << db_out << "}\n";
157 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
159 // Doesn't appear to be used (Angus, 31 July 2001)
160 Path p(buffer.filePath());
165 string bibfiles = getContents();
166 bibfiles = split(bibfiles, tmp, ',');
167 while (!tmp.empty()) {
168 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
169 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
171 // If we didn't find a matching file name just fail silently
175 // Get next file name
176 bibfiles = split(bibfiles, tmp, ',');
183 // This method returns a comma separated list of Bibtex entries
184 void InsetBibtex::fillWithBibKeys
185 (Buffer const * buffer, vector<pair<string, string> > & keys) const
188 vector<string> const files = getFiles(*buffer);
189 for (vector<string>::const_iterator it = files.begin();
190 it != files.end(); ++ it) {
191 // This is a _very_ simple parser for Bibtex database
192 // files. All it does is to look for lines starting
193 // in @ and not being @preamble and @string entries.
194 // It does NOT do any syntax checking!
195 ifstream ifs(it->c_str());
197 while (getline(ifs, linebuf0)) {
198 string linebuf = trim(linebuf0);
199 if (linebuf.empty()) continue;
200 if (prefixIs(linebuf, "@")) {
201 linebuf = subst(linebuf, '{', '(');
203 linebuf = split(linebuf, tmp, '(');
204 tmp = ascii_lowercase(tmp);
205 if (!prefixIs(tmp, "@string")
206 && !prefixIs(tmp, "@preamble")) {
207 linebuf = split(linebuf, tmp, ',');
208 tmp = ltrim(tmp, " \t");
210 keys.push_back(pair<string,string>(tmp,string()));
213 } else if (!keys.empty()) {
214 keys.back().second += linebuf + "\n";
221 void InsetBibtex::edit(BufferView * bv, int, int, mouse_button::state)
223 InsetCommandMailer mailer("bibtex", *this);
224 mailer.showDialog(bv);
228 void InsetBibtex::edit(BufferView * bv, bool)
230 edit(bv, 0, 0, mouse_button::none);
234 bool InsetBibtex::addDatabase(string const & db)
236 string contents(getContents());
237 if (!contains(contents, db)) {
238 if (!contents.empty())
240 setContents(contents + db);
247 bool InsetBibtex::delDatabase(string const & db)
249 if (contains(getContents(), db)) {
251 int const n = tokenPos(getContents(), ',', bd);
253 // Weird code, would someone care to explain this?(Lgb)
256 setContents(subst(getContents(), tmp, ", "));
258 setContents(split(getContents(), bd, ','));