]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
738e4ecd920d586f669093aedf350a8a4bb475c2
[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 "lyxlex.h"
15 #include "frontends/font_metrics.h"
16 #include "frontends/LyXView.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, mouse_button::state)
114 {
115         bv->owner()->getDialogs()->showBibitem(this);
116 }
117
118
119 void InsetBibKey::edit(BufferView * bv, bool)
120 {
121         edit(bv, 0, 0, mouse_button::none);
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;
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(buffer->params.getLyXTextClass().name(),
178                               "art")) {
179                         if (buffer->params.sides == LyXTextClass::OneSide) {
180                                 // oneside
181                                 os << "\\clearpage";
182                         } else {
183                                 // twoside
184                                 os << "\\cleardoublepage";
185                         }
186
187                         // bookclass
188                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
189
190                 } else {
191                         // article class
192                         os << "\\addcontentsline{toc}{section}{\\refname}";
193                 }
194         }
195
196         // database
197         // If we generate in a temp dir, we might need to give an
198         // absolute path there. This is a bit complicated since we can
199         // have a comma-separated list of bibliographies
200         string db_out;
201         while (!adb.empty()) {
202                 if (!buffer->niceFile &&
203                     IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
204                          adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
205                 db_out += adb;
206                 db_out += ',';
207                 db_in= split(db_in, adb,',');
208         }
209         db_out = strip(db_out, ",");
210         os   << "\\bibliography{" << db_out << "}\n";
211         return 2;
212 }
213
214
215 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
216 {
217         // Doesn't appear to be used (Angus, 31 July 2001)
218         Path p(buffer.filePath());
219
220         vector<string> vec;
221
222         string tmp;
223         string bibfiles = getContents();
224         bibfiles = split(bibfiles, tmp, ',');
225         while (!tmp.empty()) {
226                 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
227                 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
228
229                 // If we didn't find a matching file name just fail silently
230                 if (!file.empty())
231                         vec.push_back(file);
232
233                 // Get next file name
234                 bibfiles = split(bibfiles, tmp, ',');
235         }
236
237         return vec;
238 }
239
240 // This method returns a comma separated list of Bibtex entries
241 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
242 {
243         vector<pair<string,string> > keys;
244
245         lyx::Assert(buffer);
246         vector<string> const files = getFiles(*buffer);
247         for (vector<string>::const_iterator it = files.begin();
248              it != files.end(); ++ it) {
249                 // This is a _very_ simple parser for Bibtex database
250                 // files. All it does is to look for lines starting
251                 // in @ and not being @preamble and @string entries.
252                 // It does NOT do any syntax checking!
253                 ifstream ifs(it->c_str());
254                 string linebuf0;
255                 while (getline(ifs, linebuf0)) {
256                         string linebuf = frontStrip(strip(linebuf0));
257                         if (linebuf.empty()) continue;
258                         if (prefixIs(linebuf, "@")) {
259                                 linebuf = subst(linebuf, '{', '(');
260                                 string tmp;
261                                 linebuf = split(linebuf, tmp, '(');
262                                 tmp = ascii_lowercase(tmp);
263                                 if (!prefixIs(tmp, "@string")
264                                     && !prefixIs(tmp, "@preamble")) {
265                                         linebuf = split(linebuf, tmp, ',');
266                                         tmp = frontStrip(tmp);
267                                         tmp = frontStrip(tmp,"\t");
268                                         if (!tmp.empty()) {
269                                                 keys.push_back(pair<string,string>(tmp,string()));
270                                         }
271                                 }
272                         } else if (!keys.empty()) {
273                                 keys.back().second += linebuf + "\n";
274                         }
275                 }
276         }
277         return keys;
278 }
279
280
281 void InsetBibtex::edit(BufferView * bv, int, int, mouse_button::state)
282 {
283         bv->owner()->getDialogs()->showBibtex(this);
284 }
285
286
287 void InsetBibtex::edit(BufferView * bv, bool)
288 {
289         edit(bv, 0, 0, mouse_button::none);
290 }
291
292
293 bool InsetBibtex::addDatabase(string const & db)
294 {
295         string contents(getContents());
296         if (!contains(contents, db)) {
297                 if (!contents.empty())
298                         contents += ",";
299                 setContents(contents + db);
300                 return true;
301         }
302         return false;
303 }
304
305
306 bool InsetBibtex::delDatabase(string const & db)
307 {
308         if (contains(getContents(), db)) {
309                 string bd = db;
310                 int const n = tokenPos(getContents(), ',', bd);
311                 if (n > 0) {
312                         // Weird code, would someone care to explain this?(Lgb)
313                         string tmp(", ");
314                         tmp += bd;
315                         setContents(subst(getContents(), tmp, ", "));
316                 } else if (n == 0)
317                         setContents(split(getContents(), bd, ','));
318                 else
319                         return false;
320         }
321         return true;
322 }
323
324
325 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
326 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
327 {
328         int w = 0;
329         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
330         // Does look like a hack? It is! (but will change at 0.13)
331         Paragraph * par = bv->buffer()->paragraph;
332
333         while (par) {
334                 if (par->bibkey) {
335                         int const wx = par->bibkey->width(bv, font);
336                         if (wx > w) w = wx;
337                 }
338                 par = par->next();
339         }
340         return w;
341 }
342
343
344 // ale070405
345 string const bibitemWidest(Buffer const * buffer)
346 {
347         int w = 0;
348         // Does look like a hack? It is! (but will change at 0.13)
349         Paragraph * par = buffer->paragraph;
350         InsetBibKey * bkey = 0;
351         LyXFont font;
352
353         while (par) {
354                 if (par->bibkey) {
355                         int const wx =
356                                 font_metrics::width(par->bibkey->getBibLabel(),
357                                                font);
358                         if (wx > w) {
359                                 w = wx;
360                                 bkey = par->bibkey;
361                         }
362                 }
363                 par = par->next();
364         }
365
366         if (bkey && !bkey->getBibLabel().empty())
367                 return bkey->getBibLabel();
368
369         return "99";
370 }