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;
39 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
44 InsetBibtex::~InsetBibtex()
46 InsetCommandMailer("bibtex", *this).hideDialog();
50 std::auto_ptr<InsetBase> InsetBibtex::clone() const
52 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
56 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
58 InsetCommand::metrics(mi, dim);
59 int center_indent = (mi.base.textwidth - dim.wid) / 2;
60 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
62 dim.wid = mi.base.textwidth;
67 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
69 InsetCommand::draw(pi, x + button().box().x1, y);
73 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
77 case LFUN_INSET_DIALOG_SHOW:
78 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
80 case LFUN_MOUSE_RELEASE:
81 if (button().box().contains(cmd.x, cmd.y))
82 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
85 case LFUN_INSET_MODIFY: {
87 InsetCommandMailer::string2params(cmd.argument, p);
88 if (p.getCmdName().empty())
95 return InsetCommand::localDispatch(cmd);
100 string const InsetBibtex::getScreenLabel(Buffer const &) const
102 return _("BibTeX Generated References");
106 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
107 LatexRunParams const & runparams) const
109 // changing the sequence of the commands
110 // 1. \bibliographystyle{style}
111 // 2. \addcontentsline{...} - if option bibtotoc set
112 // 3. \bibliography{database}
114 string db_in = getContents();
115 db_in = split(db_in, adb, ',');
118 string style = getOptions(); // maybe empty! and with bibtotoc
120 if (prefixIs(style, "bibtotoc")) {
121 bibtotoc = "bibtotoc";
122 if (contains(style, ',')) {
123 style = split(style, bibtotoc, ',');
128 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
129 style = MakeAbsPath(style, buffer.filePath());
132 if (!style.empty()) { // we want no \biblio...{}
133 os << "\\bibliographystyle{" << style << "}\n";
137 if (!bibtotoc.empty()) {
138 // maybe a problem when a textclass has no "art" as
139 // part of its name, because it's than book.
140 // For the "official" lyx-layouts it's no problem to support
142 if (!contains(buffer.params.getLyXTextClass().name(),
144 if (buffer.params.sides == LyXTextClass::OneSide) {
149 os << "\\cleardoublepage";
153 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
157 os << "\\addcontentsline{toc}{section}{\\refname}";
162 // If we generate in a temp dir, we might need to give an
163 // absolute path there. This is a bit complicated since we can
164 // have a comma-separated list of bibliographies
166 while (!adb.empty()) {
167 if (!runparams.nice &&
168 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
169 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
172 db_in = split(db_in, adb,',');
174 db_out = rtrim(db_out, ",");
175 os << "\\bibliography{" << db_out << "}\n";
180 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
182 Path p(buffer.filePath());
187 string bibfiles = getContents();
188 bibfiles = split(bibfiles, tmp, ',');
189 while (!tmp.empty()) {
190 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
191 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
193 // If we didn't find a matching file name just fail silently
197 // Get next file name
198 bibfiles = split(bibfiles, tmp, ',');
205 // This method returns a comma separated list of Bibtex entries
206 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
207 std::vector<std::pair<string, string> > & keys) const
209 vector<string> const files = getFiles(buffer);
210 for (vector<string>::const_iterator it = files.begin();
211 it != files.end(); ++ it) {
212 // This is a _very_ simple parser for Bibtex database
213 // files. All it does is to look for lines starting
214 // in @ and not being @preamble and @string entries.
215 // It does NOT do any syntax checking!
216 ifstream ifs(it->c_str());
218 while (getline(ifs, linebuf0)) {
219 string linebuf = trim(linebuf0);
220 if (linebuf.empty()) continue;
221 if (prefixIs(linebuf, "@")) {
222 linebuf = subst(linebuf, '{', '(');
224 linebuf = split(linebuf, tmp, '(');
225 tmp = ascii_lowercase(tmp);
226 if (!prefixIs(tmp, "@string")
227 && !prefixIs(tmp, "@preamble")) {
228 linebuf = split(linebuf, tmp, ',');
229 tmp = ltrim(tmp, " \t");
231 keys.push_back(pair<string,string>(tmp,string()));
234 } else if (!keys.empty()) {
235 keys.back().second += linebuf + "\n";
242 bool InsetBibtex::addDatabase(string const & db)
244 string contents(getContents());
245 if (!contains(contents, db)) {
246 if (!contents.empty())
248 setContents(contents + db);
255 bool InsetBibtex::delDatabase(string const & db)
257 if (contains(getContents(), db)) {
259 int const n = tokenPos(getContents(), ',', bd);
261 // Weird code, would someone care to explain this?(Lgb)
264 setContents(subst(getContents(), tmp, ", "));
266 setContents(split(getContents(), bd, ','));