]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibtex.C
Fix problem in lockInsetInInset
[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::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;
45
46 using std::endl;
47 using std::getline;
48
49 using std::ifstream;
50 using std::ostream;
51 using std::pair;
52 using std::vector;
53
54
55 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
56         : InsetCommand(p)
57 {}
58
59
60 InsetBibtex::~InsetBibtex()
61 {
62         InsetCommandMailer("bibtex", *this).hideDialog();
63 }
64
65
66 std::auto_ptr<InsetBase> InsetBibtex::clone() const
67 {
68         return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
69 }
70
71
72 void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
73 {
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);
77         button().setBox(b);
78         dim.wid = mi.base.textwidth;
79         dim_ = dim;
80 }
81
82
83 void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
84 {
85         InsetCommand::draw(pi, x + button().box().x1, y);
86 }
87
88
89 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
90 {
91         switch (cmd.action) {
92
93         case LFUN_INSET_DIALOG_SHOW:
94                 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
95                 return DISPATCHED;
96         case LFUN_MOUSE_RELEASE:
97                 if (button().box().contains(cmd.x, cmd.y))
98                         InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
99                 return DISPATCHED;
100
101         case LFUN_INSET_MODIFY: {
102                 InsetCommandParams p;
103                 InsetCommandMailer::string2params(cmd.argument, p);
104                 if (p.getCmdName().empty())
105                         return DISPATCHED;
106                 setParams(p);
107                 return  DISPATCHED;
108         }
109
110         default:
111                 return InsetCommand::localDispatch(cmd);
112         }
113
114 }
115
116 string const InsetBibtex::getScreenLabel(Buffer const &) const
117 {
118         return _("BibTeX Generated References");
119 }
120
121
122 int InsetBibtex::latex(Buffer const & buffer, ostream & os,
123                        LatexRunParams const & runparams) const
124 {
125         // changing the sequence of the commands
126         // 1. \bibliographystyle{style}
127         // 2. \addcontentsline{...} - if option bibtotoc set
128         // 3. \bibliography{database}
129         string adb;
130         string db_in = getContents();
131         db_in = split(db_in, adb, ',');
132
133         // Style-Options
134         string style = getOptions(); // maybe empty! and with bibtotoc
135         string bibtotoc;
136         if (prefixIs(style, "bibtotoc")) {
137                 bibtotoc = "bibtotoc";
138                 if (contains(style, ',')) {
139                         style = split(style, bibtotoc, ',');
140                 }
141         }
142
143         if (!runparams.nice
144             && IsFileReadable(MakeAbsPath(style, buffer.filePath()) + ".bst")) {
145                 style = MakeAbsPath(style, buffer.filePath());
146         }
147
148         if (!style.empty()) { // we want no \biblio...{}
149                 os << "\\bibliographystyle{" << style << "}\n";
150         }
151
152         // bibtotoc-Option
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
157                 // all well
158                 if (!contains(buffer.params().getLyXTextClass().name(),
159                               "art")) {
160                         if (buffer.params().sides == LyXTextClass::OneSide) {
161                                 // oneside
162                                 os << "\\clearpage";
163                         } else {
164                                 // twoside
165                                 os << "\\cleardoublepage";
166                         }
167
168                         // bookclass
169                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
170
171                 } else {
172                         // article class
173                         os << "\\addcontentsline{toc}{section}{\\refname}";
174                 }
175         }
176
177         // database
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
181         string db_out;
182         while (!adb.empty()) {
183                 if (!runparams.nice &&
184                     IsFileReadable(MakeAbsPath(adb, buffer.filePath())+".bib"))
185                          adb = os::external_path(MakeAbsPath(adb, buffer.filePath()));
186                 db_out += adb;
187                 db_out += ',';
188                 db_in = split(db_in, adb,',');
189         }
190         db_out = rtrim(db_out, ",");
191         os   << "\\bibliography{" << db_out << "}\n";
192         return 2;
193 }
194
195
196 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
197 {
198         Path p(buffer.filePath());
199
200         vector<string> vec;
201
202         string tmp;
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;
208
209                 // If we didn't find a matching file name just fail silently
210                 if (!file.empty())
211                         vec.push_back(file);
212
213                 // Get next file name
214                 bibfiles = split(bibfiles, tmp, ',');
215         }
216
217         return vec;
218 }
219
220
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
224 {
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());
233                 string linebuf0;
234                 while (getline(ifs, linebuf0)) {
235                         string linebuf = trim(linebuf0);
236                         if (linebuf.empty()) continue;
237                         if (prefixIs(linebuf, "@")) {
238                                 linebuf = subst(linebuf, '{', '(');
239                                 string tmp;
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");
246                                         if (!tmp.empty()) {
247                                                 keys.push_back(pair<string,string>(tmp,string()));
248                                         }
249                                 }
250                         } else if (!keys.empty()) {
251                                 keys.back().second += linebuf + "\n";
252                         }
253                 }
254         }
255 }
256
257
258 bool InsetBibtex::addDatabase(string const & db)
259 {
260         string contents(getContents());
261         if (!contains(contents, db)) {
262                 if (!contents.empty())
263                         contents += ',';
264                 setContents(contents + db);
265                 return true;
266         }
267         return false;
268 }
269
270
271 bool InsetBibtex::delDatabase(string const & db)
272 {
273         if (contains(getContents(), db)) {
274                 string bd = db;
275                 int const n = tokenPos(getContents(), ',', bd);
276                 if (n > 0) {
277                         // Weird code, would someone care to explain this?(Lgb)
278                         string tmp(", ");
279                         tmp += bd;
280                         setContents(subst(getContents(), tmp, ", "));
281                 } else if (n == 0)
282                         setContents(split(getContents(), bd, ','));
283                 else
284                         return false;
285         }
286         return true;
287 }