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