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