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"
16 #include "bufferparams.h"
18 #include "funcrequest.h"
20 #include "latexrunparams.h"
21 #include "metricsinfo.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "support/os.h"
26 #include "support/path.h"
30 using namespace lyx::support;
41 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
46 InsetBibtex::~InsetBibtex()
48 InsetCommandMailer("bibtex", *this).hideDialog();
52 std::auto_ptr<InsetBase> InsetBibtex::clone() const
54 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
58 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
60 InsetCommand::metrics(mi, dim);
61 int center_indent = (mi.base.textwidth - dim.wid) / 2;
62 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
64 dim.wid = mi.base.textwidth;
69 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
71 InsetCommand::draw(pi, x + button().box().x1, y);
75 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
79 case LFUN_INSET_DIALOG_SHOW:
80 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
82 case LFUN_MOUSE_RELEASE:
83 if (button().box().contains(cmd.x, cmd.y))
84 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
87 case LFUN_INSET_MODIFY: {
89 InsetCommandMailer::string2params(cmd.argument, p);
90 if (p.getCmdName().empty())
97 return InsetCommand::localDispatch(cmd);
102 string const InsetBibtex::getScreenLabel(Buffer const &) const
104 return _("BibTeX Generated References");
108 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
109 LatexRunParams const & runparams) const
111 // changing the sequence of the commands
112 // 1. \bibliographystyle{style}
113 // 2. \addcontentsline{...} - if option bibtotoc set
114 // 3. \bibliography{database}
116 string db_in = getContents();
117 db_in = split(db_in, adb, ',');
120 string style = getOptions(); // maybe empty! and with bibtotoc
122 if (prefixIs(style, "bibtotoc")) {
123 bibtotoc = "bibtotoc";
124 if (contains(style, ',')) {
125 style = split(style, bibtotoc, ',');
130 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
131 style = MakeAbsPath(style, buffer.filePath());
134 if (!style.empty()) { // we want no \biblio...{}
135 os << "\\bibliographystyle{" << style << "}\n";
139 if (!bibtotoc.empty()) {
140 // maybe a problem when a textclass has no "art" as
141 // part of its name, because it's than book.
142 // For the "official" lyx-layouts it's no problem to support
144 if (!contains(buffer.params().getLyXTextClass().name(),
146 if (buffer.params().sides == LyXTextClass::OneSide) {
151 os << "\\cleardoublepage";
155 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
159 os << "\\addcontentsline{toc}{section}{\\refname}";
164 // If we generate in a temp dir, we might need to give an
165 // absolute path there. This is a bit complicated since we can
166 // have a comma-separated list of bibliographies
168 while (!adb.empty()) {
169 if (!runparams.nice &&
170 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
171 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
174 db_in = split(db_in, adb,',');
176 db_out = rtrim(db_out, ",");
177 os << "\\bibliography{" << db_out << "}\n";
182 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
184 Path p(buffer.filePath());
189 string bibfiles = getContents();
190 bibfiles = split(bibfiles, tmp, ',');
191 while (!tmp.empty()) {
192 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
193 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
195 // If we didn't find a matching file name just fail silently
199 // Get next file name
200 bibfiles = split(bibfiles, tmp, ',');
207 // This method returns a comma separated list of Bibtex entries
208 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
209 std::vector<std::pair<string, string> > & keys) const
211 vector<string> const files = getFiles(buffer);
212 for (vector<string>::const_iterator it = files.begin();
213 it != files.end(); ++ it) {
214 // This is a _very_ simple parser for Bibtex database
215 // files. All it does is to look for lines starting
216 // in @ and not being @preamble and @string entries.
217 // It does NOT do any syntax checking!
218 ifstream ifs(it->c_str());
220 while (getline(ifs, linebuf0)) {
221 string linebuf = trim(linebuf0);
222 if (linebuf.empty()) continue;
223 if (prefixIs(linebuf, "@")) {
224 linebuf = subst(linebuf, '{', '(');
226 linebuf = split(linebuf, tmp, '(');
227 tmp = ascii_lowercase(tmp);
228 if (!prefixIs(tmp, "@string")
229 && !prefixIs(tmp, "@preamble")) {
230 linebuf = split(linebuf, tmp, ',');
231 tmp = ltrim(tmp, " \t");
233 keys.push_back(pair<string,string>(tmp,string()));
236 } else if (!keys.empty()) {
237 keys.back().second += linebuf + "\n";
244 bool InsetBibtex::addDatabase(string const & db)
246 string contents(getContents());
247 if (!contains(contents, db)) {
248 if (!contents.empty())
250 setContents(contents + db);
257 bool InsetBibtex::delDatabase(string const & db)
259 if (contains(getContents(), db)) {
261 int const n = tokenPos(getContents(), ',', bd);
263 // Weird code, would someone care to explain this?(Lgb)
266 setContents(subst(getContents(), tmp, ", "));
268 setContents(split(getContents(), bd, ','));