]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
Make it compile when USE_BOOST_FORMAT is unset
[lyx.git] / src / insets / insetbib.C
1 /**
2  * \file insetbib.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 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "insetbib.h"
17 #include "buffer.h"
18 #include "debug.h"
19 #include "BufferView.h"
20 #include "gettext.h"
21 #include "lyxtext.h"
22 #include "lyxrc.h"
23 #include "lyxlex.h"
24 #include "frontends/font_metrics.h"
25 #include "frontends/LyXView.h"
26
27 #include "frontends/Dialogs.h"
28
29 #include "support/filetools.h"
30 #include "support/path.h"
31 #include "support/os.h"
32 #include "support/lstrings.h"
33 #include "support/LAssert.h"
34
35 #include <fstream>
36 #include <cstdlib>
37
38 using std::ostream;
39 using std::ifstream;
40 using std::getline;
41 using std::endl;
42 using std::vector;
43 using std::pair;
44 using std::max;
45
46 int InsetBibKey::key_counter = 0;
47 const string key_prefix = "key-";
48
49 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
50         : InsetCommand(p), counter(1)
51 {
52         if (getContents().empty())
53                 setContents(key_prefix + tostr(++key_counter));
54 }
55
56
57 InsetBibKey::~InsetBibKey()
58 {
59 }
60
61
62 Inset * InsetBibKey::clone(Buffer const &, bool) const
63 {
64         InsetBibKey * b = new InsetBibKey(params());
65         b->setCounter(counter);
66         return b;
67 }
68
69
70 void InsetBibKey::setCounter(int c)
71 {
72         counter = c;
73 }
74
75
76 // I'm sorry but this is still necessary because \bibitem is used also
77 // as a LyX 2.x command, and lyxlex is not enough smart to understand
78 // real LaTeX commands. Yes, that could be fixed, but would be a waste
79 // of time cause LyX3 won't use lyxlex anyway.  (ale)
80 void InsetBibKey::write(Buffer const *, ostream & os) const
81 {
82         os << "\\bibitem ";
83         if (! getOptions().empty()) {
84                 os << '['
85                    << getOptions() << ']';
86         }
87         os << '{'
88            << getContents() << "}\n";
89 }
90
91
92 // This is necessary here because this is written without begin_inset
93 // This should be changed!!! (Jug)
94 void InsetBibKey::read(Buffer const *, LyXLex & lex)
95 {
96         if (lex.eatLine()) {
97                 string const token = lex.getString();
98                 scanCommand(token);
99         } else {
100                 lex.printError("InsetCommand: Parse error: `$$Token'");
101         }
102
103         if (prefixIs(getContents(), key_prefix)) {
104                 int key = strToInt(getContents().substr(key_prefix.length()));
105                 key_counter = max(key_counter, key);
106         }
107 }
108
109 string const InsetBibKey::getBibLabel() const
110 {
111         if (! getOptions().empty())
112                 return getOptions();
113         return tostr(counter);
114 }
115
116 string const InsetBibKey::getScreenLabel(Buffer const *) const
117 {
118         return getContents() + " [" + getBibLabel() + "]";
119 }
120
121
122 void InsetBibKey::edit(BufferView * bv, int, int, mouse_button::state)
123 {
124         bv->owner()->getDialogs().showBibitem(this);
125 }
126
127
128 void InsetBibKey::edit(BufferView * bv, bool)
129 {
130         edit(bv, 0, 0, mouse_button::none);
131 }
132
133
134 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
135         : InsetCommand(p)
136 {}
137
138
139 InsetBibtex::~InsetBibtex()
140 {
141 }
142
143
144 string const InsetBibtex::getScreenLabel(Buffer const *) const
145 {
146         return _("BibTeX Generated References");
147 }
148
149
150 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
151                        bool /*fragile*/, bool/*fs*/) const
152 {
153         // changing the sequence of the commands
154         // 1. \bibliographystyle{style}
155         // 2. \addcontentsline{...} - if option bibtotoc set
156         // 3. \bibliography{database}
157         string adb;
158         string db_in = getContents();
159         db_in = split(db_in, adb, ',');
160
161         // Style-Options
162         string style = getOptions(); // maybe empty! and with bibtotoc
163         string bibtotoc;
164         if (prefixIs(style, "bibtotoc")) {
165                 bibtotoc = "bibtotoc";
166                 if (contains(style, ',')) {
167                         style = split(style, bibtotoc, ',');
168                 }
169         }
170
171         if (!buffer->niceFile
172             && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
173                 style = MakeAbsPath(style, buffer->filePath());
174         }
175
176         if (!style.empty()) { // we want no \biblio...{}
177                 os << "\\bibliographystyle{" << style << "}\n";
178         }
179
180         // bibtotoc-Option
181         if (!bibtotoc.empty()) {
182                 // maybe a problem when a textclass has no "art" as
183                 // part of its name, because it's than book.
184                 // For the "official" lyx-layouts it's no problem to support
185                 // all well
186                 if (!contains(buffer->params.getLyXTextClass().name(),
187                               "art")) {
188                         if (buffer->params.sides == LyXTextClass::OneSide) {
189                                 // oneside
190                                 os << "\\clearpage";
191                         } else {
192                                 // twoside
193                                 os << "\\cleardoublepage";
194                         }
195
196                         // bookclass
197                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
198
199                 } else {
200                         // article class
201                         os << "\\addcontentsline{toc}{section}{\\refname}";
202                 }
203         }
204
205         // database
206         // If we generate in a temp dir, we might need to give an
207         // absolute path there. This is a bit complicated since we can
208         // have a comma-separated list of bibliographies
209         string db_out;
210         while (!adb.empty()) {
211                 if (!buffer->niceFile &&
212                     IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
213                          adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
214                 db_out += adb;
215                 db_out += ',';
216                 db_in= split(db_in, adb,',');
217         }
218         db_out = rtrim(db_out, ",");
219         os   << "\\bibliography{" << db_out << "}\n";
220         return 2;
221 }
222
223
224 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
225 {
226         // Doesn't appear to be used (Angus, 31 July 2001)
227         Path p(buffer.filePath());
228
229         vector<string> vec;
230
231         string tmp;
232         string bibfiles = getContents();
233         bibfiles = split(bibfiles, tmp, ',');
234         while (!tmp.empty()) {
235                 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
236                 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
237
238                 // If we didn't find a matching file name just fail silently
239                 if (!file.empty())
240                         vec.push_back(file);
241
242                 // Get next file name
243                 bibfiles = split(bibfiles, tmp, ',');
244         }
245
246         return vec;
247 }
248
249 // This method returns a comma separated list of Bibtex entries
250 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
251 {
252         vector<pair<string,string> > keys;
253
254         lyx::Assert(buffer);
255         vector<string> const files = getFiles(*buffer);
256         for (vector<string>::const_iterator it = files.begin();
257              it != files.end(); ++ it) {
258                 // This is a _very_ simple parser for Bibtex database
259                 // files. All it does is to look for lines starting
260                 // in @ and not being @preamble and @string entries.
261                 // It does NOT do any syntax checking!
262                 ifstream ifs(it->c_str());
263                 string linebuf0;
264                 while (getline(ifs, linebuf0)) {
265                         string linebuf = trim(linebuf0);
266                         if (linebuf.empty()) continue;
267                         if (prefixIs(linebuf, "@")) {
268                                 linebuf = subst(linebuf, '{', '(');
269                                 string tmp;
270                                 linebuf = split(linebuf, tmp, '(');
271                                 tmp = ascii_lowercase(tmp);
272                                 if (!prefixIs(tmp, "@string")
273                                     && !prefixIs(tmp, "@preamble")) {
274                                         linebuf = split(linebuf, tmp, ',');
275                                         tmp = ltrim(tmp, " \t");
276                                         if (!tmp.empty()) {
277                                                 keys.push_back(pair<string,string>(tmp,string()));
278                                         }
279                                 }
280                         } else if (!keys.empty()) {
281                                 keys.back().second += linebuf + "\n";
282                         }
283                 }
284         }
285         return keys;
286 }
287
288
289 void InsetBibtex::edit(BufferView * bv, int, int, mouse_button::state)
290 {
291         bv->owner()->getDialogs().showBibtex(this);
292 }
293
294
295 void InsetBibtex::edit(BufferView * bv, bool)
296 {
297         edit(bv, 0, 0, mouse_button::none);
298 }
299
300
301 bool InsetBibtex::addDatabase(string const & db)
302 {
303         string contents(getContents());
304         if (!contains(contents, db)) {
305                 if (!contents.empty())
306                         contents += ",";
307                 setContents(contents + db);
308                 return true;
309         }
310         return false;
311 }
312
313
314 bool InsetBibtex::delDatabase(string const & db)
315 {
316         if (contains(getContents(), db)) {
317                 string bd = db;
318                 int const n = tokenPos(getContents(), ',', bd);
319                 if (n > 0) {
320                         // Weird code, would someone care to explain this?(Lgb)
321                         string tmp(", ");
322                         tmp += bd;
323                         setContents(subst(getContents(), tmp, ", "));
324                 } else if (n == 0)
325                         setContents(split(getContents(), bd, ','));
326                 else
327                         return false;
328         }
329         return true;
330 }
331
332
333 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
334 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
335 {
336         int w = 0;
337         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
338         // Does look like a hack? It is! (but will change at 0.13)
339         ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
340         ParagraphList::iterator end = bv->buffer()->paragraphs.end();
341         for (; it != end; ++it) {
342                 if (it->bibkey) {
343                         int const wx = it->bibkey->width(bv, font);
344                         if (wx > w)
345                                 w = wx;
346                 }
347         }
348         return w;
349 }
350
351
352 // ale070405
353 string const bibitemWidest(Buffer const * buffer)
354 {
355         int w = 0;
356         // Does look like a hack? It is! (but will change at 0.13)
357
358         InsetBibKey * bkey = 0;
359         LyXFont font;
360
361         ParagraphList::iterator it = buffer->paragraphs.begin();
362         ParagraphList::iterator end = buffer->paragraphs.end();
363         for (; it != end; ++it) {
364                 if (it->bibkey) {
365                         int const wx =
366                                 font_metrics::width(it->bibkey->getBibLabel(),
367                                                     font);
368                         if (wx > w) {
369                                 w = wx;
370                                 bkey = it->bibkey;
371                         }
372                 }
373         }
374
375         if (bkey && !bkey->getBibLabel().empty())
376                 return bkey->getBibLabel();
377
378         return "99";
379 }