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 lyx::support::ascii_lowercase;
31 using lyx::support::ChangeExtension;
32 using lyx::support::contains;
33 using lyx::support::findtexfile;
34 using lyx::support::IsFileReadable;
35 using lyx::support::ltrim;
36 using lyx::support::MakeAbsPath;
37 using lyx::support::os;
38 using lyx::support::Path;
39 using lyx::support::prefixIs;
40 using lyx::support::rtrim;
41 using lyx::support::split;
42 using lyx::support::subst;
43 using lyx::support::tokenPos;
44 using lyx::support::trim;
55 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
60 InsetBibtex::~InsetBibtex()
62 InsetCommandMailer("bibtex", *this).hideDialog();
66 std::auto_ptr<InsetBase> InsetBibtex::clone() const
68 return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
72 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
74 InsetCommand::metrics(mi, dim);
75 int center_indent = (mi.base.textwidth - dim.wid) / 2;
76 Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
78 dim.wid = mi.base.textwidth;
83 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
85 InsetCommand::draw(pi, x + button().box().x1, y);
89 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
93 case LFUN_INSET_DIALOG_SHOW:
94 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
96 case LFUN_MOUSE_RELEASE:
97 if (button().box().contains(cmd.x, cmd.y))
98 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
101 case LFUN_INSET_MODIFY: {
102 InsetCommandParams p;
103 InsetCommandMailer::string2params(cmd.argument, p);
104 if (p.getCmdName().empty())
111 return InsetCommand::localDispatch(cmd);
116 string const InsetBibtex::getScreenLabel(Buffer const &) const
118 return _("BibTeX Generated References");
122 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
123 LatexRunParams const & runparams) const
125 // changing the sequence of the commands
126 // 1. \bibliographystyle{style}
127 // 2. \addcontentsline{...} - if option bibtotoc set
128 // 3. \bibliography{database}
130 string db_in = getContents();
131 db_in = split(db_in, adb, ',');
134 string style = getOptions(); // maybe empty! and with bibtotoc
136 if (prefixIs(style, "bibtotoc")) {
137 bibtotoc = "bibtotoc";
138 if (contains(style, ',')) {
139 style = split(style, bibtotoc, ',');
144 && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
145 style = MakeAbsPath(style, buffer.filePath());
148 if (!style.empty()) { // we want no \biblio...{}
149 os << "\\bibliographystyle{" << style << "}\n";
153 if (!bibtotoc.empty()) {
154 // maybe a problem when a textclass has no "art" as
155 // part of its name, because it's than book.
156 // For the "official" lyx-layouts it's no problem to support
158 if (!contains(buffer.params().getLyXTextClass().name(),
160 if (buffer.params().sides == LyXTextClass::OneSide) {
165 os << "\\cleardoublepage";
169 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
173 os << "\\addcontentsline{toc}{section}{\\refname}";
178 // If we generate in a temp dir, we might need to give an
179 // absolute path there. This is a bit complicated since we can
180 // have a comma-separated list of bibliographies
182 while (!adb.empty()) {
183 if (!runparams.nice &&
184 IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
185 adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
188 db_in = split(db_in, adb,',');
190 db_out = rtrim(db_out, ",");
191 os << "\\bibliography{" << db_out << "}\n";
196 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
198 Path p(buffer.filePath());
203 string bibfiles = getContents();
204 bibfiles = split(bibfiles, tmp, ',');
205 while (!tmp.empty()) {
206 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
207 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
209 // If we didn't find a matching file name just fail silently
213 // Get next file name
214 bibfiles = split(bibfiles, tmp, ',');
221 // This method returns a comma separated list of Bibtex entries
222 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
223 std::vector<std::pair<string, string> > & keys) const
225 vector<string> const files = getFiles(buffer);
226 for (vector<string>::const_iterator it = files.begin();
227 it != files.end(); ++ it) {
228 // This is a _very_ simple parser for Bibtex database
229 // files. All it does is to look for lines starting
230 // in @ and not being @preamble and @string entries.
231 // It does NOT do any syntax checking!
232 ifstream ifs(it->c_str());
234 while (getline(ifs, linebuf0)) {
235 string linebuf = trim(linebuf0);
236 if (linebuf.empty()) continue;
237 if (prefixIs(linebuf, "@")) {
238 linebuf = subst(linebuf, '{', '(');
240 linebuf = split(linebuf, tmp, '(');
241 tmp = ascii_lowercase(tmp);
242 if (!prefixIs(tmp, "@string")
243 && !prefixIs(tmp, "@preamble")) {
244 linebuf = split(linebuf, tmp, ',');
245 tmp = ltrim(tmp, " \t");
247 keys.push_back(pair<string,string>(tmp,string()));
250 } else if (!keys.empty()) {
251 keys.back().second += linebuf + "\n";
258 bool InsetBibtex::addDatabase(string const & db)
260 string contents(getContents());
261 if (!contains(contents, db)) {
262 if (!contents.empty())
264 setContents(contents + db);
271 bool InsetBibtex::delDatabase(string const & db)
273 if (contains(getContents(), db)) {
275 int const n = tokenPos(getContents(), ',', bd);
277 // Weird code, would someone care to explain this?(Lgb)
280 setContents(subst(getContents(), tmp, ", "));
282 setContents(split(getContents(), bd, ','));