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"
17 #include "funcrequest.h"
19 #include "latexrunparams.h"
20 #include "metricsinfo.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24 #include "support/os.h"
25 #include "support/path.h"
29 using namespace lyx::support;
40 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
45 InsetBibtex::~InsetBibtex()
47 InsetCommandMailer("bibtex", *this).hideDialog();
51 std::auto_ptr<InsetBase> InsetBibtex::clone() const
53 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
57 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
59 InsetCommand::metrics(mi, dim);
60 int center_indent = (mi.base.textwidth - dim.wid) / 2;
61 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
63 dim.wid = mi.base.textwidth;
68 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
70 InsetCommand::draw(pi, x + button().box().x1, y);
74 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
78 case LFUN_INSET_DIALOG_SHOW:
79 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
81 case LFUN_MOUSE_RELEASE:
82 if (button().box().contains(cmd.x, cmd.y))
83 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
86 case LFUN_INSET_MODIFY: {
88 InsetCommandMailer::string2params(cmd.argument, p);
89 if (p.getCmdName().empty())
96 return InsetCommand::localDispatch(cmd);
101 string const InsetBibtex::getScreenLabel(Buffer const &) const
103 return _("BibTeX Generated References");
107 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
108 LatexRunParams const & runparams) const
110 // changing the sequence of the commands
111 // 1. \bibliographystyle{style}
112 // 2. \addcontentsline{...} - if option bibtotoc set
113 // 3. \bibliography{database}
115 string db_in = getContents();
116 db_in = split(db_in, adb, ',');
119 string style = getOptions(); // maybe empty! and with bibtotoc
121 if (prefixIs(style, "bibtotoc")) {
122 bibtotoc = "bibtotoc";
123 if (contains(style, ',')) {
124 style = split(style, bibtotoc, ',');
129 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
130 style = MakeAbsPath(style, buffer.filePath());
133 if (!style.empty()) { // we want no \biblio...{}
134 os << "\\bibliographystyle{" << style << "}\n";
138 if (!bibtotoc.empty()) {
139 // maybe a problem when a textclass has no "art" as
140 // part of its name, because it's than book.
141 // For the "official" lyx-layouts it's no problem to support
143 if (!contains(buffer.params.getLyXTextClass().name(),
145 if (buffer.params.sides == LyXTextClass::OneSide) {
150 os << "\\cleardoublepage";
154 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
158 os << "\\addcontentsline{toc}{section}{\\refname}";
163 // If we generate in a temp dir, we might need to give an
164 // absolute path there. This is a bit complicated since we can
165 // have a comma-separated list of bibliographies
167 while (!adb.empty()) {
168 if (!runparams.nice &&
169 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
170 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
173 db_in = split(db_in, adb,',');
175 db_out = rtrim(db_out, ",");
176 os << "\\bibliography{" << db_out << "}\n";
181 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
183 Path p(buffer.filePath());
188 string bibfiles = getContents();
189 bibfiles = split(bibfiles, tmp, ',');
190 while (!tmp.empty()) {
191 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
192 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
194 // If we didn't find a matching file name just fail silently
198 // Get next file name
199 bibfiles = split(bibfiles, tmp, ',');
206 // This method returns a comma separated list of Bibtex entries
207 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
208 std::vector<std::pair<string, string> > & keys) const
210 vector<string> const files = getFiles(buffer);
211 for (vector<string>::const_iterator it = files.begin();
212 it != files.end(); ++ it) {
213 // This is a _very_ simple parser for Bibtex database
214 // files. All it does is to look for lines starting
215 // in @ and not being @preamble and @string entries.
216 // It does NOT do any syntax checking!
217 ifstream ifs(it->c_str());
219 while (getline(ifs, linebuf0)) {
220 string linebuf = trim(linebuf0);
221 if (linebuf.empty()) continue;
222 if (prefixIs(linebuf, "@")) {
223 linebuf = subst(linebuf, '{', '(');
225 linebuf = split(linebuf, tmp, '(');
226 tmp = ascii_lowercase(tmp);
227 if (!prefixIs(tmp, "@string")
228 && !prefixIs(tmp, "@preamble")) {
229 linebuf = split(linebuf, tmp, ',');
230 tmp = ltrim(tmp, " \t");
232 keys.push_back(pair<string,string>(tmp,string()));
235 } else if (!keys.empty()) {
236 keys.back().second += linebuf + "\n";
243 bool InsetBibtex::addDatabase(string const & db)
245 string contents(getContents());
246 if (!contains(contents, db)) {
247 if (!contents.empty())
249 setContents(contents + db);
256 bool InsetBibtex::delDatabase(string const & db)
258 if (contains(getContents(), db)) {
260 int const n = tokenPos(getContents(), ',', bd);
262 // Weird code, would someone care to explain this?(Lgb)
265 setContents(subst(getContents(), tmp, ", "));
267 setContents(split(getContents(), bd, ','));