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