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