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