]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
Bibtex fix from Angus ; first try at fixing mathed crash
[lyx.git] / src / insets / insetbib.C
1 #include <config.h>
2
3 #include <fstream>
4 #include <cstdlib>
5
6 #ifdef __GNUG__
7 #pragma implementation
8 #endif
9
10 #include FORMS_H_LOCATION  
11 #include "insetbib.h"
12 #include "buffer.h"
13 #include "debug.h"
14 #include "lyx_gui_misc.h"
15 #include "BufferView.h"
16 #include "gettext.h"
17 #include "bibforms.h"
18 #include "lyxtext.h"
19 #include "support/filetools.h"
20 #include "support/path.h"
21 #include "lyxrc.h"
22
23 using std::ostream;
24 using std::ifstream;
25 using std::getline;
26 using std::endl;
27 using std::vector;
28 using std::pair;
29
30 FD_bibitem_form * bibitem_form = 0;
31
32 FD_bibitem_form * create_form_bibitem_form(void);
33
34 extern BufferView * current_view;
35
36 // This is foul!
37 // called from both InsetBibKey and InsetBibtex dialogs yet cast off
38 // only to InsetBibKey holder. Real problems can ensue.
39 extern "C"
40 void bibitem_cb(FL_OBJECT *, long data)
41 {
42         InsetBibKey::Holder * holder =
43                 static_cast<InsetBibKey::Holder*>
44                 (bibitem_form->bibitem_form->u_vdata);
45
46         holder->inset->callback( bibitem_form, data );
47 }
48
49
50 FD_bibitem_form * create_form_bibitem_form(void)
51 {
52         FL_OBJECT * obj;
53         FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
54
55         fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
56         obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
57         fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
58         fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
59         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
60         obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
61         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
62         fl_set_object_callback(obj, bibitem_cb, 1);
63         obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
64         fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
65         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
66         fl_set_object_callback(obj, bibitem_cb, 0);
67         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
68         fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
69         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
70         fl_end_form();
71
72         //fdui->bibitem_form->fdui = fdui;
73
74         return fdui;
75 }
76
77
78 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
79         : InsetCommand(p)
80 {
81         counter = 1;
82 }
83
84
85 InsetBibKey::~InsetBibKey()
86 {
87         if(bibitem_form && bibitem_form->bibitem_form
88            && bibitem_form->bibitem_form->visible
89            && bibitem_form->bibitem_form->u_vdata == &holder)
90                 fl_hide_form(bibitem_form->bibitem_form);
91 }
92
93
94 Inset * InsetBibKey::Clone(Buffer const &) const
95 {
96         InsetBibKey * b = new InsetBibKey(params());
97         b->setCounter(counter);
98         return b;
99 }
100
101
102 void InsetBibKey::callback( FD_bibitem_form * form, long data )
103 {
104         switch (data) {
105         case 1:
106                 // Do NOT change this to
107                 // holder.view->buffer() as this code is used by both
108                 // InsetBibKey and InsetBibtex! Ughhhhhhh!!!!
109                 if(!current_view->buffer()->isReadonly()) {
110                         setContents(fl_get_input(form->key));
111                         setOptions(fl_get_input(form->label));
112                         // shouldn't mark the buffer dirty unless
113                         // something was actually altered
114                         current_view->updateInset( this, true );
115                 } // fall through to Cancel
116         case 0:
117                 fl_hide_form(form->bibitem_form);
118                 break;
119         }
120 }
121
122
123 void InsetBibKey::setCounter(int c) 
124
125         counter = c; 
126     
127         if (getCmdName().empty())
128                 setCmdName( tostr(counter) );
129 }
130
131
132 // I'm sorry but this is still necessary because \bibitem is used also
133 // as a LyX 2.x command, and lyxlex is not enough smart to understand
134 // real LaTeX commands. Yes, that could be fixed, but would be a waste 
135 // of time cause LyX3 won't use lyxlex anyway.  (ale)
136 void InsetBibKey::Write(Buffer const *, ostream & os) const
137 {
138         os << "\\bibitem ";
139         if (! getOptions().empty()) {
140                 os << '['
141                    << getOptions() << ']';
142         }
143         os << '{'
144            << getContents() << "}\n";
145 }
146
147
148 // This is necessary here because this is written without begin_inset
149 // This should be changed!!! (Jug)
150 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
151 {    
152         string token;
153
154         if (lex.EatLine()) {
155                 token = lex.GetString();
156                 scanCommand(token);
157         } else
158                 lex.printError("InsetCommand: Parse error: `$$Token'");
159 }
160
161
162 string const InsetBibKey::getScreenLabel() const
163 {
164         if (! getOptions().empty())
165                 return getOptions();
166     
167         return tostr(counter);
168 }
169
170
171 /**
172   The value in "Key:" isn't allways set right after a few bibkey insets have
173   been added/removed.  Perhaps the wrong object is deleted/used somewhere
174   upwards?
175   (Joacim 1998-03-04)
176 */
177 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
178 {
179         if(bv->buffer()->isReadonly())
180                 WarnReadonly(bv->buffer()->fileName());
181         
182         if (!bibitem_form) {
183                 bibitem_form = create_form_bibitem_form();
184                 fl_set_form_atclose(bibitem_form->bibitem_form, 
185                                     CancelCloseBoxCB, 0);
186         }
187
188         holder.inset = this;
189         holder.view = bv;
190         
191         bibitem_form->bibitem_form->u_vdata = &holder;
192         // InsetBibtex uses the same form, with different labels
193         fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
194         fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
195         fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
196         fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
197         fl_set_input(bibitem_form->key, getContents().c_str());
198         fl_set_input(bibitem_form->label, getOptions().c_str());
199         if (bibitem_form->bibitem_form->visible) {
200                 fl_raise_form(bibitem_form->bibitem_form);
201         } else {
202                 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
203                              FL_FULLBORDER,
204                              _("Bibliography item"));
205         }   
206 }
207
208
209 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
210         : InsetCommand(p)
211 {}
212
213
214 InsetBibtex::~InsetBibtex()
215 {
216         if(bibitem_form && bibitem_form->bibitem_form
217            && bibitem_form->bibitem_form->visible
218            && bibitem_form->bibitem_form->u_vdata == &holder)
219                 fl_hide_form(bibitem_form->bibitem_form);
220 }
221
222
223 string const InsetBibtex::getScreenLabel() const
224 {
225         return _("BibTeX Generated References");
226 }
227
228
229 int InsetBibtex::Latex(Buffer const * buffer, ostream & os,
230                        bool /*fragile*/, bool/*fs*/) const
231 {
232         // If we generate in a temp dir, we might need to give an
233         // absolute path there. This is a bit complicated since we can
234         // have a comma-separated list of bibliographies
235         string adb, db_out;
236         string db_in = getContents();
237         db_in = split(db_in, adb, ',');
238         while(!adb.empty()) {
239                 if (!buffer->niceFile &&
240                     IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib")) 
241                          adb = MakeAbsPath(adb, buffer->filepath);
242
243                 db_out += adb;
244                 db_out += ',';
245                 db_in= split(db_in, adb,',');
246         }
247         db_out = strip(db_out, ',');
248         // Idem, but simpler
249         string style;
250         if (!buffer->niceFile 
251             && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
252                               + ".bst")) 
253                 style = MakeAbsPath(getOptions(), buffer->filepath);
254         else
255                 style = getOptions();
256
257         os << "\\bibliographystyle{" << style << "}\n"
258            << "\\bibliography{" << db_out << "}\n";
259         return 2;
260 }
261
262
263 // This method returns a comma separated list of Bibtex entries
264 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
265 {
266         Path p(buffer->filepath);
267
268         vector<pair<string,string> > keys;
269         string tmp;
270         string bibfiles = getContents();
271         bibfiles = split(bibfiles, tmp, ',');
272         while(!tmp.empty()) {
273                 string fil = findtexfile(ChangeExtension(tmp, "bib"),
274                                          "bib");
275                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
276                 // If we didn't find a matching file name just fail silently
277                 if (!fil.empty()) {
278                         // This is a _very_ simple parser for Bibtex database
279                         // files. All it does is to look for lines starting
280                         // in @ and not being @preamble and @string entries.
281                         // It does NOT do any syntax checking!
282                         ifstream ifs(fil.c_str());
283                         string linebuf0;
284                         while (getline(ifs, linebuf0)) {
285                                 string linebuf = frontStrip(strip(linebuf0));
286                                 if( linebuf.empty() ) continue;
287                                 if (prefixIs(linebuf, "@")) {
288                                         linebuf = subst(linebuf, '{', '(');
289                                         linebuf = split(linebuf, tmp, '(');
290                                         tmp = lowercase(tmp);
291                                         if (!prefixIs(tmp, "@string")
292                                             && !prefixIs(tmp, "@preamble")) {
293                                                 linebuf = split(linebuf, tmp, ',');
294                                                 tmp = frontStrip(tmp);
295                                                 if (!tmp.empty()) {
296                                                         keys.push_back(pair<string,string>(tmp,string()));
297                                                 }
298                                         }
299                                 } else if( !keys.empty() ) {
300                                         keys.back().second += linebuf + "\n";
301                                 }
302                         }
303                 }
304                 // Get next file name
305                 bibfiles = split(bibfiles, tmp, ',');
306         }
307         return keys;
308 }
309
310
311 // BibTeX should have its own dialog. This is provisional.
312 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
313 {
314         if (!bibitem_form) {
315                 bibitem_form = create_form_bibitem_form();
316                 fl_set_form_atclose(bibitem_form->bibitem_form, 
317                                     CancelCloseBoxCB, 0);
318         }
319
320         holder.inset = this;
321         holder.view = bv;
322         bibitem_form->bibitem_form->u_vdata = &holder;
323
324         fl_set_object_label(bibitem_form->key, _("Database:"));
325         fl_set_object_label(bibitem_form->label, _("Style:  "));
326         fl_set_input(bibitem_form->key, getContents().c_str());
327         fl_set_input(bibitem_form->label, getOptions().c_str());
328         if (bibitem_form->bibitem_form->visible) {
329                 fl_raise_form(bibitem_form->bibitem_form);
330         } else {
331                 fl_show_form(bibitem_form->bibitem_form,
332                              FL_PLACE_MOUSE, FL_FULLBORDER,
333                              _("BibTeX"));
334         }   
335 }
336
337
338 bool InsetBibtex::addDatabase(string const & db)
339 {
340         string contents(getContents());
341         if (!contains(contents, db)) {
342                 if (!contents.empty()) 
343                         contents += ",";
344                 setContents(contents + db);
345                 return true;
346         }
347         return false;
348 }
349
350
351 bool InsetBibtex::delDatabase(string const & db)
352 {
353         if (contains(getContents(), db)) {
354                 string bd = db;
355                 int n = tokenPos(getContents(), ',', bd);
356                 if (n > 0) {
357                         // Weird code, would someone care to explain this?(Lgb)
358                         string tmp(", ");
359                         tmp += bd;
360                         setContents(subst(getContents(), tmp, ", "));
361                 } else if (n == 0)
362                         setContents(split(getContents(), bd, ','));
363                 else 
364                         return false;
365         }
366         return true;
367 }
368
369
370 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
371 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
372 {
373         int w = 0;
374         // Does look like a hack? It is! (but will change at 0.13)
375         LyXParagraph * par = bv->buffer()->paragraph;
376     
377         while (par) {
378                 if (par->bibkey) {
379                         int wx = par->bibkey->width(bv, font);
380                         if (wx > w) w = wx;
381                 }
382                 par = par->next;
383         }
384         return w;
385 }
386
387
388 // ale070405
389 string const bibitemWidest(Buffer const * buffer)
390 {
391         int w = 0;
392         // Does look like a hack? It is! (but will change at 0.13)
393         LyXParagraph * par = buffer->paragraph;
394         BufferView * bv = buffer->getUser();
395         InsetBibKey * bkey = 0;
396         LyXFont font;
397       
398         while (par) {
399                 if (par->bibkey) {
400                         int wx = par->bibkey->width(bv, font);
401                         if (wx > w) {
402                                 w = wx;
403                                 bkey = par->bibkey;
404                         }
405                 }
406                 par = par->next;
407         }
408     
409         if (bkey && !bkey->getScreenLabel().empty())
410                 return bkey->getScreenLabel();
411     
412         return "99";
413 }