]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
layout as string
[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;
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[buffer->params.textclass].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 = lowercase(tmp);
263                                 if (!prefixIs(tmp, "@string")
264                                     && !prefixIs(tmp, "@preamble")) {
265                                         linebuf = split(linebuf, tmp, ',');
266                                         tmp = frontStrip(tmp);
267                                         if (!tmp.empty()) {
268                                                 keys.push_back(pair<string,string>(tmp,string()));
269                                         }
270                                 }
271                         } else if (!keys.empty()) {
272                                 keys.back().second += linebuf + "\n";
273                         }
274                 }
275         }
276         return keys;
277 }
278
279
280 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
281 {
282         bv->owner()->getDialogs()->showBibtex(this);
283 }
284
285
286 void InsetBibtex::edit(BufferView * bv, bool)
287 {
288         edit(bv, 0, 0, 0);
289 }
290
291
292 bool InsetBibtex::addDatabase(string const & db)
293 {
294         string contents(getContents());
295         if (!contains(contents, db)) {
296                 if (!contents.empty()) 
297                         contents += ",";
298                 setContents(contents + db);
299                 return true;
300         }
301         return false;
302 }
303
304
305 bool InsetBibtex::delDatabase(string const & db)
306 {
307         if (contains(getContents(), db)) {
308                 string bd = db;
309                 int const n = tokenPos(getContents(), ',', bd);
310                 if (n > 0) {
311                         // Weird code, would someone care to explain this?(Lgb)
312                         string tmp(", ");
313                         tmp += bd;
314                         setContents(subst(getContents(), tmp, ", "));
315                 } else if (n == 0)
316                         setContents(split(getContents(), bd, ','));
317                 else 
318                         return false;
319         }
320         return true;
321 }
322
323
324 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
325 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
326 {
327         int w = 0;
328         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
329         // Does look like a hack? It is! (but will change at 0.13)
330         Paragraph * par = bv->buffer()->paragraph;
331     
332         while (par) {
333                 if (par->bibkey) {
334                         int const wx = par->bibkey->width(bv, font);
335                         if (wx > w) w = wx;
336                 }
337                 par = par->next();
338         }
339         return w;
340 }
341
342
343 // ale070405
344 string const bibitemWidest(Buffer const * buffer)
345 {
346         int w = 0;
347         // Does look like a hack? It is! (but will change at 0.13)
348         Paragraph * par = buffer->paragraph;
349         InsetBibKey * bkey = 0;
350         LyXFont font;
351       
352         while (par) {
353                 if (par->bibkey) {
354                         int const wx =
355                                 lyxfont::width(par->bibkey->getBibLabel(),
356                                                font);
357                         if (wx > w) {
358                                 w = wx;
359                                 bkey = par->bibkey;
360                         }
361                 }
362                 par = par->next();
363         }
364     
365         if (bkey && !bkey->getBibLabel().empty())
366                 return bkey->getBibLabel();
367     
368         return "99";
369 }