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 if (cmd.action != LFUN_INSET_APPLY)
55 InsetCommandMailer::string2params(cmd.argument, p);
56 if (p.getCmdName().empty())
59 if (view() && p.getContents() != params().getContents()) {
60 view()->ChangeCitationsIfUnique(params().getContents(),
66 view()->updateInset(this, true);
71 string const InsetBibtex::getScreenLabel(Buffer const *) const
73 return _("BibTeX Generated References");
77 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
78 bool /*fragile*/, bool/*fs*/) const
80 // changing the sequence of the commands
81 // 1. \bibliographystyle{style}
82 // 2. \addcontentsline{...} - if option bibtotoc set
83 // 3. \bibliography{database}
85 string db_in = getContents();
86 db_in = split(db_in, adb, ',');
89 string style = getOptions(); // maybe empty! and with bibtotoc
91 if (prefixIs(style, "bibtotoc")) {
92 bibtotoc = "bibtotoc";
93 if (contains(style, ',')) {
94 style = split(style, bibtotoc, ',');
99 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
100 style = MakeAbsPath(style, buffer->filePath());
103 if (!style.empty()) { // we want no \biblio...{}
104 os << "\\bibliographystyle{" << style << "}\n";
108 if (!bibtotoc.empty()) {
109 // maybe a problem when a textclass has no "art" as
110 // part of its name, because it's than book.
111 // For the "official" lyx-layouts it's no problem to support
113 if (!contains(buffer->params.getLyXTextClass().name(),
115 if (buffer->params.sides == LyXTextClass::OneSide) {
120 os << "\\cleardoublepage";
124 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
128 os << "\\addcontentsline{toc}{section}{\\refname}";
133 // If we generate in a temp dir, we might need to give an
134 // absolute path there. This is a bit complicated since we can
135 // have a comma-separated list of bibliographies
137 while (!adb.empty()) {
138 if (!buffer->niceFile &&
139 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
140 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
143 db_in = split(db_in, adb,',');
145 db_out = rtrim(db_out, ",");
146 os << "\\bibliography{" << db_out << "}\n";
151 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
153 // Doesn't appear to be used (Angus, 31 July 2001)
154 Path p(buffer.filePath());
159 string bibfiles = getContents();
160 bibfiles = split(bibfiles, tmp, ',');
161 while (!tmp.empty()) {
162 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
163 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
165 // If we didn't find a matching file name just fail silently
169 // Get next file name
170 bibfiles = split(bibfiles, tmp, ',');
177 // This method returns a comma separated list of Bibtex entries
178 void InsetBibtex::fillWithBibKeys
179 (Buffer const * buffer, vector<pair<string, string> > & keys) const
182 vector<string> const files = getFiles(*buffer);
183 for (vector<string>::const_iterator it = files.begin();
184 it != files.end(); ++ it) {
185 // This is a _very_ simple parser for Bibtex database
186 // files. All it does is to look for lines starting
187 // in @ and not being @preamble and @string entries.
188 // It does NOT do any syntax checking!
189 ifstream ifs(it->c_str());
191 while (getline(ifs, linebuf0)) {
192 string linebuf = trim(linebuf0);
193 if (linebuf.empty()) continue;
194 if (prefixIs(linebuf, "@")) {
195 linebuf = subst(linebuf, '{', '(');
197 linebuf = split(linebuf, tmp, '(');
198 tmp = ascii_lowercase(tmp);
199 if (!prefixIs(tmp, "@string")
200 && !prefixIs(tmp, "@preamble")) {
201 linebuf = split(linebuf, tmp, ',');
202 tmp = ltrim(tmp, " \t");
204 keys.push_back(pair<string,string>(tmp,string()));
207 } else if (!keys.empty()) {
208 keys.back().second += linebuf + "\n";
215 void InsetBibtex::edit(BufferView *, int, int, mouse_button::state)
217 InsetCommandMailer mailer("bibtex", *this);
222 void InsetBibtex::edit(BufferView * bv, bool)
224 edit(bv, 0, 0, mouse_button::none);
228 bool InsetBibtex::addDatabase(string const & db)
230 string contents(getContents());
231 if (!contains(contents, db)) {
232 if (!contents.empty())
234 setContents(contents + db);
241 bool InsetBibtex::delDatabase(string const & db)
243 if (contains(getContents(), db)) {
245 int const n = tokenPos(getContents(), ',', bd);
247 // Weird code, would someone care to explain this?(Lgb)
250 setContents(subst(getContents(), tmp, ", "));
252 setContents(split(getContents(), bd, ','));