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