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