]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
Some cleanup; fix a bug where bibtex entries list would have extraneous spaces.
[lyx.git] / src / insets / insetbib.C
1 #include <config.h>
2
3 #include <fstream>
4 using std::ifstream;
5
6 #include <cstdlib>
7
8 #ifdef __GNUG__
9 #pragma implementation
10 #endif
11
12 #include FORMS_H_LOCATION  
13 #include "insetbib.h"
14 #include "combox.h"
15 #include "buffer.h"
16 #include "debug.h"
17 #include "lyx_gui_misc.h"
18 #include "BufferView.h"
19 #include "gettext.h"
20 #include "bibforms.h"
21 #include "lyxtext.h"
22 #include "support/filetools.h"
23
24 extern BufferView * current_view;
25
26 FD_citation_form * citation_form = 0;
27 FD_bibitem_form * bibitem_form = 0;
28 static Combox * bibcombox = 0;
29
30 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
31 void BibitemUpdate(Combox *);
32 FD_citation_form * create_form_citation_form(void);
33 FD_bibitem_form * create_form_bibitem_form(void);
34
35
36 extern "C" void bibitem_cb(FL_OBJECT *, long data)
37 {
38         switch (data) {
39         case 1: // OK, citation
40         {
41                 if(!current_view->buffer()->isReadonly()) {
42                         InsetCommand * inset = static_cast<InsetCommand*>(citation_form->citation_form->u_vdata);
43                         inset->setContents(bibcombox->getline());
44                         inset->setOptions(fl_get_input(citation_form->label));
45                         fl_hide_form(citation_form->citation_form);
46                         // shouldn't mark the buffer dirty unless something
47                         // was actually altered
48                         UpdateInset(inset);
49                         break;
50                 }
51                 // fall through to Cancel on RO-mode
52         }       
53         case 0: fl_hide_form(citation_form->citation_form);
54                 break;
55         case 3: // OK, bibitem
56         {
57                 if(!current_view->buffer()->isReadonly()) {
58                         InsetCommand * inset = static_cast<InsetCommand*>(bibitem_form->bibitem_form->u_vdata);
59                         inset->setContents(fl_get_input(bibitem_form->key));
60                         inset->setOptions(fl_get_input(bibitem_form->label));
61                         fl_hide_form(bibitem_form->bibitem_form);
62                         // Does look like a hack? It is! (but will change at 0.13)
63                         current_view->text->RedoParagraph();
64                         current_view->update(1);
65                         break;
66                 } // fall through to Cancel on RO-mode
67         }
68         case 2: // Cancel, bibitem
69                 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
70                 break;
71         }
72 }
73
74
75 FD_citation_form * create_form_citation_form(void)
76 {
77         FL_OBJECT * obj;
78         FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
79
80         fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
81         obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
82         fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
83         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
84         fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
85
86         bibcombox = new Combox(FL_COMBOX_INPUT);
87         bibcombox->add(80, 10, 130, 30, 120);
88
89         obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
90         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
91         fl_set_object_callback(obj, bibitem_cb, 1);
92         obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
93         fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
94         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
95         fl_set_object_callback(obj, bibitem_cb, 0);
96         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
97         fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
98         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
99         fl_end_form();
100
101         //fdui->citation_form->fdui = fdui;
102
103         return fdui;
104 }
105 /*---------------------------------------*/
106
107 FD_bibitem_form * create_form_bibitem_form(void)
108 {
109         FL_OBJECT * obj;
110         FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
111
112         fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
113         obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
114         fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
115         fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
116         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
117         obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
118         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
119         fl_set_object_callback(obj, bibitem_cb, 3);
120         obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
121         fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
122         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
123         fl_set_object_callback(obj, bibitem_cb, 2);
124         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
125         fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
126         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
127         fl_end_form();
128
129         //fdui->bibitem_form->fdui = fdui;
130
131         return fdui;
132 }
133 /*---------------------------------------*/
134
135
136 InsetCitation::InsetCitation(string const & key, string const & note)
137         : InsetCommand("cite", key, note)
138 {
139
140 }
141
142
143 InsetCitation::~InsetCitation()
144 {
145         if(citation_form && citation_form->citation_form
146            && citation_form->citation_form->visible
147            && citation_form->citation_form->u_vdata == this)
148                 fl_hide_form(citation_form->citation_form);
149 }
150
151
152 void InsetCitation::Edit(int, int)
153 {
154         if(current_view->buffer()->isReadonly())
155                 WarnReadonly(current_view->buffer()->fileName());
156
157         if (!citation_form) {
158                 citation_form = create_form_citation_form();
159                 fl_set_form_atclose(citation_form->citation_form, 
160                                     CancelCloseBoxCB, 0);
161         }
162         citation_form->citation_form->u_vdata = this;
163
164         BibitemUpdate(bibcombox);
165         if (!bibcombox->select_text(getContents().c_str()))
166                 bibcombox->addline(getContents().c_str());
167             
168         fl_set_input(citation_form->label, getOptions().c_str());
169         if (citation_form->citation_form->visible) {
170                 fl_raise_form(citation_form->citation_form);
171         } else {
172                 fl_show_form(citation_form->citation_form,
173                              FL_PLACE_MOUSE, FL_FULLBORDER,
174                              _("Citation"));
175         }   
176 }
177
178
179 string InsetCitation::getScreenLabel() const
180 {
181         string temp("[");
182
183         temp += contents;
184
185         if (!options.empty()) {
186                 temp += ", " + options;
187         }
188
189         return temp + ']';
190 }
191
192
193 InsetBibKey::InsetBibKey(string const & key, string const & label):
194         InsetCommand("bibitem", key, label)
195 {
196         counter = 1;
197         if (key.empty())
198                 contents = ' ';
199 }
200
201
202 InsetBibKey::InsetBibKey(InsetBibKey const * b):
203         InsetCommand("bibitem", b->contents, b->options)
204 {
205         counter = b->counter;
206 }
207
208
209 InsetBibKey::~InsetBibKey()
210 {
211         if(bibitem_form && bibitem_form->bibitem_form
212            && bibitem_form->bibitem_form->visible)
213                 fl_hide_form(bibitem_form->bibitem_form);
214 }
215
216 void InsetBibKey::setCounter(int c) 
217
218         counter = c; 
219     
220         if (contents.empty())
221                 contents += tostr(counter);
222 }
223
224
225 // I'm sorry but this is still necessary because \bibitem is used also
226 // as a LyX 2.x command, and lyxlex is not enough smart to understand
227 // real LaTeX commands. Yes, that could be fixed, but would be a waste 
228 // of time cause LyX3 won't use lyxlex anyway.  (ale)
229 void InsetBibKey::Write(ostream & os)
230 {
231         string s;
232         if (!options.empty()) {
233                 s += '[';
234                 s += options + ']';
235         }
236         s += '{';
237         s += contents + '}';
238         os << "\\bibitem " << s << "\n";
239 }
240
241
242 string InsetBibKey::getScreenLabel() const
243 {
244         if (!options.empty())
245                 return options;
246     
247         return tostr(counter);
248 }
249
250
251 /*
252   The value in "Key:" isn't allways set right after a few bibkey insets have
253   been added/removed.  Perhaps the wrong object is deleted/used somewhere
254   upwards?
255   (Joacim 1998-03-04)
256 */
257 void InsetBibKey::Edit(int, int)
258 {
259         if(current_view->buffer()->isReadonly())
260                 WarnReadonly(current_view->buffer()->fileName());
261         
262         if (!bibitem_form) {
263                 bibitem_form = create_form_bibitem_form();
264                 fl_set_form_atclose(bibitem_form->bibitem_form, 
265                                     CancelCloseBoxCB, 0);
266         }
267         bibitem_form->bibitem_form->u_vdata = this;
268         // InsetBibtex uses the same form, with different labels
269         fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
270         fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
271         fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
272         fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
273         fl_set_input(bibitem_form->key, getContents().c_str());
274         fl_set_input(bibitem_form->label, getOptions().c_str());
275         if (bibitem_form->bibitem_form->visible) {
276                 fl_raise_form(bibitem_form->bibitem_form);
277         } else {
278                 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
279                              FL_FULLBORDER,
280                              _("Bibliography item"));
281         }   
282 }
283
284
285
286 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
287                          Buffer * o)
288         : InsetCommand("BibTeX", dbase, style), owner(o)
289 {
290 }
291
292
293 string InsetBibtex::getScreenLabel() const
294 {
295         return _("BibTeX Generated References");
296 }
297
298
299 int InsetBibtex::Latex(ostream & os, signed char /*fragile*/)
300 {
301         string bib;
302         signed char dummy = 0;
303         int result = Latex(bib, dummy);
304         os << bib;
305         return result;
306 }
307
308
309 int InsetBibtex::Latex(string & file, signed char /*fragile*/)
310 {
311         // this looks like an horrible hack and it is :) The problem
312         // is that owner is not initialized correctly when the bib
313         // inset is cut and pasted. Such hacks will not be needed
314         // later (JMarc)
315         if (!owner) {
316                 owner = current_view->buffer();
317         }
318         // If we generate in a temp dir, we might need to give an
319         // absolute path there. This is a bit complicated since we can
320         // have a comma-separated list of bibliographies
321         string adb, db_out;
322         string db_in = getContents();
323         db_in = split(db_in, adb, ',');
324         while(!adb.empty()) {
325                 if (!owner->niceFile &&
326                     IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib")) 
327                         adb = MakeAbsPath(adb, owner->filepath);
328                 db_out += adb;
329                 db_out += ',';
330                 db_in= split(db_in, adb,',');
331         }
332         db_out = strip(db_out, ',');
333         // Idem, but simpler
334         string style;
335         if (!owner->niceFile 
336             && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
337                               + ".bst")) 
338                 style = MakeAbsPath(getOptions(), owner->filepath);
339         else
340                 style = getOptions();
341
342         file += "\\bibliographystyle{";
343         file += style;
344         file += "}\n";
345         file += "\\bibliography{";
346         file += db_out;
347         file += "}\n";
348         return 2;
349 }
350
351
352 // This method returns a comma separated list of Bibtex entries
353 string InsetBibtex::getKeys(char delim)
354 {
355         // This hack is copied from InsetBibtex::Latex.
356         // Is it still needed? Probably yes.
357         if (!owner) {
358                 owner = current_view->buffer();
359         }
360         
361         string tmp, keys;
362         string bibfiles = getContents();
363         bibfiles = split(bibfiles, tmp, ',');
364         while(!tmp.empty()) {
365                 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
366                                          "bib");
367                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
368                 // If we didn't find a matching file name just fail silently
369                 if (!fil.empty()) {
370                         // This is a _very_ simple parser for Bibtex database
371                         // files. All it does is to look for lines starting
372                         // in @ and not being @preamble and @string entries.
373                         // It does NOT do any syntax checking!
374                         ifstream ifs(fil.c_str());
375                         string linebuf;
376                         while (getline(ifs, linebuf)) {
377                                 linebuf = frontStrip(linebuf);
378                                 if (prefixIs(linebuf, "@")) {
379                                         linebuf = subst(linebuf, '{', '(');
380                                         linebuf = split(linebuf, tmp, '(');
381                                         tmp = lowercase(tmp);
382                                         if (!prefixIs(tmp, "@string")
383                                             && !prefixIs(tmp, "@preamble")) {
384                                                 linebuf = split(linebuf,
385                                                                 tmp, ',');
386                                                 tmp = frontStrip(strip(tmp));
387                                                 if (!tmp.empty()) {
388                                                         keys += tmp;
389                                                         keys += delim;
390                                                 }
391                                         }
392                                 }
393                         }
394                 }
395                 // Get next file name
396                 bibfiles = split(bibfiles, tmp, ',');
397         }
398         return keys;
399 }
400
401
402 // BibTeX should have its own dialog. This is provisional.
403 void InsetBibtex::Edit(int, int)
404 {
405         if (!bibitem_form) {
406                 bibitem_form = create_form_bibitem_form();
407                 fl_set_form_atclose(bibitem_form->bibitem_form, 
408                                     CancelCloseBoxCB, 0);
409         }
410
411         bibitem_form->bibitem_form->u_vdata = this;
412         fl_set_object_label(bibitem_form->key, _("Database:"));
413         fl_set_object_label(bibitem_form->label, _("Style:  "));
414         fl_set_input(bibitem_form->key, getContents().c_str());
415         fl_set_input(bibitem_form->label, getOptions().c_str());
416         if (bibitem_form->bibitem_form->visible) {
417                 fl_raise_form(bibitem_form->bibitem_form);
418         } else {
419                 fl_show_form(bibitem_form->bibitem_form,
420                              FL_PLACE_MOUSE, FL_FULLBORDER,
421                              _("BibTeX"));
422         }   
423 }
424
425
426 bool InsetBibtex::addDatabase(string const & db)
427 {
428         if (!contains(contents, db.c_str())) {
429                 if (!contents.empty()) 
430                         contents += ',';
431                 contents += db;
432                 return true;
433         }
434         return false;
435 }
436
437
438 bool InsetBibtex::delDatabase(string const & db)
439 {
440         if (contains(contents, db.c_str())) {
441                 string bd = db;
442                 int n = tokenPos(contents, ',', bd);
443                 if (n > 0) {
444                         // Weird code, would someone care to explain this?(Lgb)
445                         string tmp(", ");
446                         tmp += bd;
447                         contents = subst(contents, tmp.c_str(), ", ");
448                 } else if (n == 0)
449                         contents = split(contents, bd, ',');
450                 else 
451                         return false;
452         }
453         return true;
454 }
455
456
457 // This function should be in LyXView when multiframe works ale970302
458 void BibitemUpdate(Combox * combox)
459 {
460         combox->clear();
461         
462         if (!current_view->available()) return;
463         
464         string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
465         bibkeys = split(bibkeys, tmp,',');
466         while (!tmp.empty()) {
467                 combox->addto(tmp.c_str());
468                 bibkeys = split(bibkeys, tmp,',');
469         }
470 }
471
472
473
474 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
475 int bibitemMaxWidth(LyXFont const & font)
476 {
477         int w = 0;
478         // Does look like a hack? It is! (but will change at 0.13)
479         LyXParagraph * par = current_view->buffer()->paragraph;
480     
481         while (par) {
482                 if (par->bibkey) {
483                         int wx = par->bibkey->Width(font);
484                         if (wx > w) w = wx;
485                 }
486                 par = par->next;
487         }
488         return w;
489 }
490
491
492 // ale070405 
493 string bibitemWidthest()
494 {
495         int w = 0;
496         // Does look like a hack? It is! (but will change at 0.13)
497         LyXParagraph * par = current_view->buffer()->paragraph;
498         InsetBibKey * bkey = 0;
499         LyXFont font;
500       
501         while (par) {
502                 if (par->bibkey) {
503                         int wx = par->bibkey->Width(font);
504                         if (wx > w) {
505                                 w = wx;
506                                 bkey = par->bibkey;
507                         }
508                 }
509                 par = par->next;
510         }
511     
512         if (bkey && !bkey->getScreenLabel().empty())
513                 return bkey->getScreenLabel();
514     
515         return "99";
516 }