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