]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
outstanding changes
[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 "frontends/Dialogs.h"
11 #include "insetbib.h"
12 #include "buffer.h"
13 #include "debug.h"
14 #include "BufferView.h"
15 #include "gettext.h"
16 #include "lyxtext.h"
17 #include "support/filetools.h"
18 #include "support/path.h"
19 #include "support/os.h"
20 #include "support/lstrings.h"
21 #include "lyxrc.h"
22 #include "font.h"
23 #include "LyXView.h" 
24
25 using std::ostream;
26 using std::ifstream;
27 using std::getline;
28 using std::endl;
29 using std::vector;
30 using std::pair;
31 using std::max;
32
33 int InsetBibKey::key_counter = 0;
34 const string key_prefix = "key-";
35
36 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
37         : InsetCommand(p), counter(1)
38 {
39         if (getContents().empty())
40                 setContents(key_prefix + tostr(++key_counter));
41 }
42
43
44 InsetBibKey::~InsetBibKey()
45 {
46 }
47
48
49 Inset * InsetBibKey::clone(Buffer const &, bool) const
50 {
51         InsetBibKey * b = new InsetBibKey(params());
52         b->setCounter(counter);
53         return b;
54 }
55
56
57 void InsetBibKey::setCounter(int c) 
58
59         counter = c; 
60 }
61
62
63 // I'm sorry but this is still necessary because \bibitem is used also
64 // as a LyX 2.x command, and lyxlex is not enough smart to understand
65 // real LaTeX commands. Yes, that could be fixed, but would be a waste 
66 // of time cause LyX3 won't use lyxlex anyway.  (ale)
67 void InsetBibKey::write(Buffer const *, ostream & os) const
68 {
69         os << "\\bibitem ";
70         if (! getOptions().empty()) {
71                 os << '['
72                    << getOptions() << ']';
73         }
74         os << '{'
75            << getContents() << "}\n";
76 }
77
78
79 // This is necessary here because this is written without begin_inset
80 // This should be changed!!! (Jug)
81 void InsetBibKey::read(Buffer const *, LyXLex & lex)
82 {
83         if (lex.eatLine()) {
84                 string const token = lex.getString();
85                 scanCommand(token);
86         } else {
87                 lex.printError("InsetCommand: Parse error: `$$Token'");
88         }
89
90         if (prefixIs(getContents(), key_prefix)) {
91                 int key = strToInt(getContents().substr(key_prefix.length()));
92                 key_counter = max(key_counter, key);
93         }
94 }
95
96 string const InsetBibKey::getBibLabel() const
97 {
98         if (! getOptions().empty())
99                 return getOptions();
100         return tostr(counter);
101 }
102
103 string const InsetBibKey::getScreenLabel(Buffer const *) const
104 {
105         return getContents() + " [" + getBibLabel() + "]";
106 }
107
108
109 void InsetBibKey::edit(BufferView * bv, int, int, unsigned int)
110
111         bv->owner()->getDialogs()->showBibitem(this);
112 }
113
114
115 void InsetBibKey::edit(BufferView * bv, bool)
116 {
117         edit(bv, 0, 0, 0);
118 }
119
120
121 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
122         : InsetCommand(p)
123 {}
124
125
126 InsetBibtex::~InsetBibtex()
127 {
128 }
129
130
131 string const InsetBibtex::getScreenLabel(Buffer const *) const
132 {
133         return _("BibTeX Generated References");
134 }
135
136
137 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
138                        bool /*fragile*/, bool/*fs*/) const
139 {
140         // If we generate in a temp dir, we might need to give an
141         // absolute path there. This is a bit complicated since we can
142         // have a comma-separated list of bibliographies
143         string adb, db_out;
144         string db_in = getContents();
145         db_in = split(db_in, adb, ',');
146         while(!adb.empty()) {
147                 if (!buffer->niceFile &&
148                     IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib")) 
149                          adb = os::external_path(MakeAbsPath(adb, buffer->filepath));
150
151                 db_out += adb;
152                 db_out += ',';
153                 db_in= split(db_in, adb,',');
154         }
155         db_out = strip(db_out, ',');
156         // Idem, but simpler
157         string style;
158         if (!buffer->niceFile 
159             && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
160                               + ".bst")) 
161                 style = MakeAbsPath(getOptions(), buffer->filepath);
162         else
163                 style = getOptions();
164
165         os << "\\bibliographystyle{" << style << "}\n"
166            << "\\bibliography{" << db_out << "}\n";
167         return 2;
168 }
169
170
171 // This method returns a comma separated list of Bibtex entries
172 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
173 {
174         Path p(buffer->filepath);
175
176         vector<pair<string,string> > keys;
177         string tmp;
178         string bibfiles = getContents();
179         bibfiles = split(bibfiles, tmp, ',');
180         while(!tmp.empty()) {
181                 string fil = findtexfile(ChangeExtension(tmp, "bib"),
182                                          "bib");
183                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
184                 // If we didn't find a matching file name just fail silently
185                 if (!fil.empty()) {
186                         // This is a _very_ simple parser for Bibtex database
187                         // files. All it does is to look for lines starting
188                         // in @ and not being @preamble and @string entries.
189                         // It does NOT do any syntax checking!
190                         ifstream ifs(fil.c_str());
191                         string linebuf0;
192                         while (getline(ifs, linebuf0)) {
193                                 string linebuf = frontStrip(strip(linebuf0));
194                                 if (linebuf.empty() ) continue;
195                                 if (prefixIs(linebuf, "@")) {
196                                         linebuf = subst(linebuf, '{', '(');
197                                         linebuf = split(linebuf, tmp, '(');
198                                         tmp = lowercase(tmp);
199                                         if (!prefixIs(tmp, "@string")
200                                             && !prefixIs(tmp, "@preamble")) {
201                                                 linebuf = split(linebuf, tmp, ',');
202                                                 tmp = frontStrip(tmp);
203                                                 if (!tmp.empty()) {
204                                                         keys.push_back(pair<string,string>(tmp,string()));
205                                                 }
206                                         }
207                                 } else if (!keys.empty()) {
208                                         keys.back().second += linebuf + "\n";
209                                 }
210                         }
211                 }
212                 // Get next file name
213                 bibfiles = split(bibfiles, tmp, ',');
214         }
215         return keys;
216 }
217
218
219 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
220 {
221         bv->owner()->getDialogs()->showBibtex(this);
222 }
223
224
225 void InsetBibtex::edit(BufferView * bv, bool)
226 {
227         edit(bv, 0, 0, 0);
228 }
229
230
231 bool InsetBibtex::addDatabase(string const & db)
232 {
233         string contents(getContents());
234         if (!contains(contents, db)) {
235                 if (!contents.empty()) 
236                         contents += ",";
237                 setContents(contents + db);
238                 return true;
239         }
240         return false;
241 }
242
243
244 bool InsetBibtex::delDatabase(string const & db)
245 {
246         if (contains(getContents(), db)) {
247                 string bd = db;
248                 int const n = tokenPos(getContents(), ',', bd);
249                 if (n > 0) {
250                         // Weird code, would someone care to explain this?(Lgb)
251                         string tmp(", ");
252                         tmp += bd;
253                         setContents(subst(getContents(), tmp, ", "));
254                 } else if (n == 0)
255                         setContents(split(getContents(), bd, ','));
256                 else 
257                         return false;
258         }
259         return true;
260 }
261
262
263 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
264 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
265 {
266         int w = 0;
267         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
268         // Does look like a hack? It is! (but will change at 0.13)
269         Paragraph * par = bv->buffer()->paragraph;
270     
271         while (par) {
272                 if (par->bibkey) {
273                         int const wx = par->bibkey->width(bv, font);
274                         if (wx > w) w = wx;
275                 }
276                 par = par->next();
277         }
278         return w;
279 }
280
281
282 // ale070405
283 string const bibitemWidest(Buffer const * buffer)
284 {
285         int w = 0;
286         // Does look like a hack? It is! (but will change at 0.13)
287         Paragraph * par = buffer->paragraph;
288         InsetBibKey * bkey = 0;
289         LyXFont font;
290       
291         while (par) {
292                 if (par->bibkey) {
293                         int const wx =
294                                 lyxfont::width(par->bibkey->getBibLabel(),
295                                                font);
296                         if (wx > w) {
297                                 w = wx;
298                                 bkey = par->bibkey;
299                         }
300                 }
301                 par = par->next();
302         }
303     
304         if (bkey && !bkey->getBibLabel().empty())
305                 return bkey->getBibLabel();
306     
307         return "99";
308 }