]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibtex.C
68dbbf29aa8cc26c4240b28b6038162b88b5780c
[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 #include <config.h>
11
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 std::ostream;
31 using std::ifstream;
32 using std::getline;
33 using std::endl;
34 using std::vector;
35 using std::pair;
36
37
38 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
39         : InsetCommand(p)
40 {}
41
42
43 InsetBibtex::~InsetBibtex()
44 {
45         InsetCommandMailer mailer("bibtex", *this);
46         mailer.hideDialog();
47 }
48
49
50 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
51 {
52         switch (cmd.action) {
53
54         case LFUN_INSET_EDIT:
55                 InsetCommandMailer("bibtex", *this).showDialog(cmd.view());
56                 return DISPATCHED;
57
58         case LFUN_INSET_MODIFY: {
59                 InsetCommandParams p;
60                 InsetCommandMailer::string2params(cmd.argument, p);
61                 if (p.getCmdName().empty())
62                         return DISPATCHED;
63
64                 if (view() && p.getContents() != params().getContents()) {
65                         view()->ChangeCitationsIfUnique(params().getContents(),
66                                                         p.getContents());
67                 }
68
69                 setParams(p);
70                 cmd.view()->updateInset(this);
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         // Doesn't appear to be used (Angus, 31 July 2001)
163         Path p(buffer.filePath());
164
165         vector<string> vec;
166
167         string tmp;
168         string bibfiles = getContents();
169         bibfiles = split(bibfiles, tmp, ',');
170         while (!tmp.empty()) {
171                 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
172                 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
173
174                 // If we didn't find a matching file name just fail silently
175                 if (!file.empty())
176                         vec.push_back(file);
177
178                 // Get next file name
179                 bibfiles = split(bibfiles, tmp, ',');
180         }
181
182         return vec;
183 }
184
185
186 // This method returns a comma separated list of Bibtex entries
187 void InsetBibtex::fillWithBibKeys
188         (Buffer const * buffer, vector<pair<string, string> > & keys) const
189 {
190         lyx::Assert(buffer);
191         vector<string> const files = getFiles(*buffer);
192         for (vector<string>::const_iterator it = files.begin();
193              it != files.end(); ++ it) {
194                 // This is a _very_ simple parser for Bibtex database
195                 // files. All it does is to look for lines starting
196                 // in @ and not being @preamble and @string entries.
197                 // It does NOT do any syntax checking!
198                 ifstream ifs(it->c_str());
199                 string linebuf0;
200                 while (getline(ifs, linebuf0)) {
201                         string linebuf = trim(linebuf0);
202                         if (linebuf.empty()) continue;
203                         if (prefixIs(linebuf, "@")) {
204                                 linebuf = subst(linebuf, '{', '(');
205                                 string tmp;
206                                 linebuf = split(linebuf, tmp, '(');
207                                 tmp = ascii_lowercase(tmp);
208                                 if (!prefixIs(tmp, "@string")
209                                     && !prefixIs(tmp, "@preamble")) {
210                                         linebuf = split(linebuf, tmp, ',');
211                                         tmp = ltrim(tmp, " \t");
212                                         if (!tmp.empty()) {
213                                                 keys.push_back(pair<string,string>(tmp,string()));
214                                         }
215                                 }
216                         } else if (!keys.empty()) {
217                                 keys.back().second += linebuf + "\n";
218                         }
219                 }
220         }
221 }
222
223
224 bool InsetBibtex::addDatabase(string const & db)
225 {
226         string contents(getContents());
227         if (!contains(contents, db)) {
228                 if (!contents.empty())
229                         contents += ',';
230                 setContents(contents + db);
231                 return true;
232         }
233         return false;
234 }
235
236
237 bool InsetBibtex::delDatabase(string const & db)
238 {
239         if (contains(getContents(), db)) {
240                 string bd = db;
241                 int const n = tokenPos(getContents(), ',', bd);
242                 if (n > 0) {
243                         // Weird code, would someone care to explain this?(Lgb)
244                         string tmp(", ");
245                         tmp += bd;
246                         setContents(subst(getContents(), tmp, ", "));
247                 } else if (n == 0)
248                         setContents(split(getContents(), bd, ','));
249                 else
250                         return false;
251         }
252         return true;
253 }