]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibtex.C
Revert, revert, revert. Sorry about that, y'all.
[lyx.git] / src / insets / insetbibtex.C
1 /**
2  * \file insetbibtex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetbibtex.h"
14 #include "metricsinfo.h"
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "debug.h"
18 #include "funcrequest.h"
19 #include "gettext.h"
20 #include "latexrunparams.h"
21
22 #include "support/filetools.h"
23 #include "support/path.h"
24 #include "support/os.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27
28 #include <fstream>
29 #include <cstdlib>
30
31 using namespace lyx::support;
32
33 using std::ostream;
34 using std::ifstream;
35 using std::getline;
36 using std::endl;
37 using std::vector;
38 using std::pair;
39
40
41 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
42         : InsetCommand(p)
43 {}
44
45
46 InsetBibtex::~InsetBibtex()
47 {
48         InsetCommandMailer("bibtex", *this).hideDialog();
49 }
50
51
52 std::auto_ptr<InsetBase> InsetBibtex::clone() const
53 {
54         return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
55 }
56
57
58 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
59 {
60         InsetCommand::metrics(mi, dim);
61         center_indent_ = (mi.base.textwidth - dim.wid) / 2;
62         dim.wid = mi.base.textwidth;
63         dim_ = dim;
64 }
65
66
67 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
68 {
69         InsetCommand::draw(pi, x + center_indent_, y);
70 }
71
72
73 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
74 {
75         switch (cmd.action) {
76
77         case LFUN_INSET_EDIT:
78                 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
79                 return DISPATCHED;
80
81         case LFUN_INSET_MODIFY: {
82                 InsetCommandParams p;
83                 InsetCommandMailer::string2params(cmd.argument, p);
84                 if (p.getCmdName().empty())
85                         return DISPATCHED;
86                 setParams(p);
87                 return  DISPATCHED;
88         }
89
90         default:
91                 return InsetCommand::localDispatch(cmd);
92         }
93
94 }
95
96 string const InsetBibtex::getScreenLabel(Buffer const &) const
97 {
98         return _("BibTeX Generated References");
99 }
100
101
102 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
103                        LatexRunParams const & runparams) const
104 {
105         // changing the sequence of the commands
106         // 1. \bibliographystyle{style}
107         // 2. \addcontentsline{...} - if option bibtotoc set
108         // 3. \bibliography{database}
109         string adb;
110         string db_in = getContents();
111         db_in = split(db_in, adb, ',');
112
113         // Style-Options
114         string style = getOptions(); // maybe empty! and with bibtotoc
115         string bibtotoc;
116         if (prefixIs(style, "bibtotoc")) {
117                 bibtotoc = "bibtotoc";
118                 if (contains(style, ',')) {
119                         style = split(style, bibtotoc, ',');
120                 }
121         }
122
123         if (!runparams.nice
124             && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
125                 style = MakeAbsPath(style, buffer.filePath());
126         }
127
128         if (!style.empty()) { // we want no \biblio...{}
129                 os << "\\bibliographystyle{" << style << "}\n";
130         }
131
132         // bibtotoc-Option
133         if (!bibtotoc.empty()) {
134                 // maybe a problem when a textclass has no "art" as
135                 // part of its name, because it's than book.
136                 // For the "official" lyx-layouts it's no problem to support
137                 // all well
138                 if (!contains(buffer.params.getLyXTextClass().name(),
139                               "art")) {
140                         if (buffer.params.sides == LyXTextClass::OneSide) {
141                                 // oneside
142                                 os << "\\clearpage";
143                         } else {
144                                 // twoside
145                                 os << "\\cleardoublepage";
146                         }
147
148                         // bookclass
149                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
150
151                 } else {
152                         // article class
153                         os << "\\addcontentsline{toc}{section}{\\refname}";
154                 }
155         }
156
157         // database
158         // If we generate in a temp dir, we might need to give an
159         // absolute path there. This is a bit complicated since we can
160         // have a comma-separated list of bibliographies
161         string db_out;
162         while (!adb.empty()) {
163                 if (!runparams.nice &&
164                     IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
165                          adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
166                 db_out += adb;
167                 db_out += ',';
168                 db_in = split(db_in, adb,',');
169         }
170         db_out = rtrim(db_out, ",");
171         os   << "\\bibliography{" << db_out << "}\n";
172         return 2;
173 }
174
175
176 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
177 {
178         Path p(buffer.filePath());
179
180         vector<string> vec;
181
182         string tmp;
183         string bibfiles = getContents();
184         bibfiles = split(bibfiles, tmp, ',');
185         while (!tmp.empty()) {
186                 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
187                 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
188
189                 // If we didn't find a matching file name just fail silently
190                 if (!file.empty())
191                         vec.push_back(file);
192
193                 // Get next file name
194                 bibfiles = split(bibfiles, tmp, ',');
195         }
196
197         return vec;
198 }
199
200
201 // This method returns a comma separated list of Bibtex entries
202 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
203                                   std::vector<std::pair<string, string> > & keys) const
204 {
205         vector<string> const files = getFiles(buffer);
206         for (vector<string>::const_iterator it = files.begin();
207              it != files.end(); ++ it) {
208                 // This is a _very_ simple parser for Bibtex database
209                 // files. All it does is to look for lines starting
210                 // in @ and not being @preamble and @string entries.
211                 // It does NOT do any syntax checking!
212                 ifstream ifs(it->c_str());
213                 string linebuf0;
214                 while (getline(ifs, linebuf0)) {
215                         string linebuf = trim(linebuf0);
216                         if (linebuf.empty()) continue;
217                         if (prefixIs(linebuf, "@")) {
218                                 linebuf = subst(linebuf, '{', '(');
219                                 string tmp;
220                                 linebuf = split(linebuf, tmp, '(');
221                                 tmp = ascii_lowercase(tmp);
222                                 if (!prefixIs(tmp, "@string")
223                                     && !prefixIs(tmp, "@preamble")) {
224                                         linebuf = split(linebuf, tmp, ',');
225                                         tmp = ltrim(tmp, " \t");
226                                         if (!tmp.empty()) {
227                                                 keys.push_back(pair<string,string>(tmp,string()));
228                                         }
229                                 }
230                         } else if (!keys.empty()) {
231                                 keys.back().second += linebuf + "\n";
232                         }
233                 }
234         }
235 }
236
237
238 bool InsetBibtex::addDatabase(string const & db)
239 {
240         string contents(getContents());
241         if (!contains(contents, db)) {
242                 if (!contents.empty())
243                         contents += ',';
244                 setContents(contents + db);
245                 return true;
246         }
247         return false;
248 }
249
250
251 bool InsetBibtex::delDatabase(string const & db)
252 {
253         if (contains(getContents(), db)) {
254                 string bd = db;
255                 int const n = tokenPos(getContents(), ',', bd);
256                 if (n > 0) {
257                         // Weird code, would someone care to explain this?(Lgb)
258                         string tmp(", ");
259                         tmp += bd;
260                         setContents(subst(getContents(), tmp, ", "));
261                 } else if (n == 0)
262                         setContents(split(getContents(), bd, ','));
263                 else
264                         return false;
265         }
266         return true;
267 }