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"
18 #include "support/filetools.h"
19 #include "support/path.h"
20 #include "support/os.h"
21 #include "support/lstrings.h"
22 #include "support/LAssert.h"
35 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
40 InsetBibtex::~InsetBibtex()
42 InsetCommandMailer mailer("bibtex", *this);
47 string const InsetBibtex::getScreenLabel(Buffer const *) const
49 return _("BibTeX Generated References");
53 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
54 bool /*fragile*/, bool/*fs*/) const
56 // changing the sequence of the commands
57 // 1. \bibliographystyle{style}
58 // 2. \addcontentsline{...} - if option bibtotoc set
59 // 3. \bibliography{database}
61 string db_in = getContents();
62 db_in = split(db_in, adb, ',');
65 string style = getOptions(); // maybe empty! and with bibtotoc
67 if (prefixIs(style, "bibtotoc")) {
68 bibtotoc = "bibtotoc";
69 if (contains(style, ',')) {
70 style = split(style, bibtotoc, ',');
75 && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
76 style = MakeAbsPath(style, buffer->filePath());
79 if (!style.empty()) { // we want no \biblio...{}
80 os << "\\bibliographystyle{" << style << "}\n";
84 if (!bibtotoc.empty()) {
85 // maybe a problem when a textclass has no "art" as
86 // part of its name, because it's than book.
87 // For the "official" lyx-layouts it's no problem to support
89 if (!contains(buffer->params.getLyXTextClass().name(),
91 if (buffer->params.sides == LyXTextClass::OneSide) {
96 os << "\\cleardoublepage";
100 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
104 os << "\\addcontentsline{toc}{section}{\\refname}";
109 // If we generate in a temp dir, we might need to give an
110 // absolute path there. This is a bit complicated since we can
111 // have a comma-separated list of bibliographies
113 while (!adb.empty()) {
114 if (!buffer->niceFile &&
115 IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
116 adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
119 db_in = split(db_in, adb,',');
121 db_out = rtrim(db_out, ",");
122 os << "\\bibliography{" << db_out << "}\n";
127 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
129 // Doesn't appear to be used (Angus, 31 July 2001)
130 Path p(buffer.filePath());
135 string bibfiles = getContents();
136 bibfiles = split(bibfiles, tmp, ',');
137 while (!tmp.empty()) {
138 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
139 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
141 // If we didn't find a matching file name just fail silently
145 // Get next file name
146 bibfiles = split(bibfiles, tmp, ',');
153 // This method returns a comma separated list of Bibtex entries
154 void InsetBibtex::fillWithBibKeys
155 (Buffer const * buffer, vector<pair<string, string> > & keys) const
158 vector<string> const files = getFiles(*buffer);
159 for (vector<string>::const_iterator it = files.begin();
160 it != files.end(); ++ it) {
161 // This is a _very_ simple parser for Bibtex database
162 // files. All it does is to look for lines starting
163 // in @ and not being @preamble and @string entries.
164 // It does NOT do any syntax checking!
165 ifstream ifs(it->c_str());
167 while (getline(ifs, linebuf0)) {
168 string linebuf = trim(linebuf0);
169 if (linebuf.empty()) continue;
170 if (prefixIs(linebuf, "@")) {
171 linebuf = subst(linebuf, '{', '(');
173 linebuf = split(linebuf, tmp, '(');
174 tmp = ascii_lowercase(tmp);
175 if (!prefixIs(tmp, "@string")
176 && !prefixIs(tmp, "@preamble")) {
177 linebuf = split(linebuf, tmp, ',');
178 tmp = ltrim(tmp, " \t");
180 keys.push_back(pair<string,string>(tmp,string()));
183 } else if (!keys.empty()) {
184 keys.back().second += linebuf + "\n";
191 void InsetBibtex::edit(BufferView *, int, int, mouse_button::state)
193 InsetCommandMailer mailer("bibtex", *this);
198 void InsetBibtex::edit(BufferView * bv, bool)
200 edit(bv, 0, 0, mouse_button::none);
204 bool InsetBibtex::addDatabase(string const & db)
206 string contents(getContents());
207 if (!contains(contents, db)) {
208 if (!contents.empty())
210 setContents(contents + db);
217 bool InsetBibtex::delDatabase(string const & db)
219 if (contains(getContents(), db)) {
221 int const n = tokenPos(getContents(), ',', bd);
223 // Weird code, would someone care to explain this?(Lgb)
226 setContents(subst(getContents(), tmp, ", "));
228 setContents(split(getContents(), bd, ','));