]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
More header file include dependency work
[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         string token;
84
85         if (lex.EatLine()) {
86                 token = lex.GetString();
87                 scanCommand(token);
88         } else
89                 lex.printError("InsetCommand: Parse error: `$$Token'");
90
91         if (prefixIs(getContents(), key_prefix)) {
92                 int key = strToInt(getContents().substr(key_prefix.length()));
93                 key_counter = max(key_counter, key);
94         }
95 }
96
97 string const InsetBibKey::getBibLabel() const
98 {
99         if (! getOptions().empty())
100                 return getOptions();
101         return tostr(counter);
102 }
103
104 string const InsetBibKey::getScreenLabel(Buffer const *) const
105 {
106         return getContents() + " [" + getBibLabel() + "]";
107 }
108
109
110 void InsetBibKey::edit(BufferView * bv, int, int, unsigned int)
111
112         bv->owner()->getDialogs()->showBibitem(this);
113 }
114
115
116 void InsetBibKey::edit(BufferView * bv, bool)
117 {
118         edit(bv, 0, 0, 0);
119 }
120
121
122 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
123         : InsetCommand(p)
124 {}
125
126
127 InsetBibtex::~InsetBibtex()
128 {
129 }
130
131
132 string const InsetBibtex::getScreenLabel(Buffer const *) const
133 {
134         return _("BibTeX Generated References");
135 }
136
137
138 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
139                        bool /*fragile*/, bool/*fs*/) const
140 {
141         // If we generate in a temp dir, we might need to give an
142         // absolute path there. This is a bit complicated since we can
143         // have a comma-separated list of bibliographies
144         string adb, db_out;
145         string db_in = getContents();
146         db_in = split(db_in, adb, ',');
147         while(!adb.empty()) {
148                 if (!buffer->niceFile &&
149                     IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib")) 
150                          adb = os::external_path(MakeAbsPath(adb, buffer->filepath));
151
152                 db_out += adb;
153                 db_out += ',';
154                 db_in= split(db_in, adb,',');
155         }
156         db_out = strip(db_out, ',');
157         // Idem, but simpler
158         string style;
159         if (!buffer->niceFile 
160             && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
161                               + ".bst")) 
162                 style = MakeAbsPath(getOptions(), buffer->filepath);
163         else
164                 style = getOptions();
165
166         os << "\\bibliographystyle{" << style << "}\n"
167            << "\\bibliography{" << db_out << "}\n";
168         return 2;
169 }
170
171
172 // This method returns a comma separated list of Bibtex entries
173 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
174 {
175         Path p(buffer->filepath);
176
177         vector<pair<string,string> > keys;
178         string tmp;
179         string bibfiles = getContents();
180         bibfiles = split(bibfiles, tmp, ',');
181         while(!tmp.empty()) {
182                 string fil = findtexfile(ChangeExtension(tmp, "bib"),
183                                          "bib");
184                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
185                 // If we didn't find a matching file name just fail silently
186                 if (!fil.empty()) {
187                         // This is a _very_ simple parser for Bibtex database
188                         // files. All it does is to look for lines starting
189                         // in @ and not being @preamble and @string entries.
190                         // It does NOT do any syntax checking!
191                         ifstream ifs(fil.c_str());
192                         string linebuf0;
193                         while (getline(ifs, linebuf0)) {
194                                 string linebuf = frontStrip(strip(linebuf0));
195                                 if (linebuf.empty() ) continue;
196                                 if (prefixIs(linebuf, "@")) {
197                                         linebuf = subst(linebuf, '{', '(');
198                                         linebuf = split(linebuf, tmp, '(');
199                                         tmp = lowercase(tmp);
200                                         if (!prefixIs(tmp, "@string")
201                                             && !prefixIs(tmp, "@preamble")) {
202                                                 linebuf = split(linebuf, tmp, ',');
203                                                 tmp = frontStrip(tmp);
204                                                 if (!tmp.empty()) {
205                                                         keys.push_back(pair<string,string>(tmp,string()));
206                                                 }
207                                         }
208                                 } else if (!keys.empty()) {
209                                         keys.back().second += linebuf + "\n";
210                                 }
211                         }
212                 }
213                 // Get next file name
214                 bibfiles = split(bibfiles, tmp, ',');
215         }
216         return keys;
217 }
218
219
220 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
221 {
222         bv->owner()->getDialogs()->showBibtex(this);
223 }
224
225
226 void InsetBibtex::edit(BufferView * bv, bool)
227 {
228         edit(bv, 0, 0, 0);
229 }
230
231
232 bool InsetBibtex::addDatabase(string const & db)
233 {
234         string contents(getContents());
235         if (!contains(contents, db)) {
236                 if (!contents.empty()) 
237                         contents += ",";
238                 setContents(contents + db);
239                 return true;
240         }
241         return false;
242 }
243
244
245 bool InsetBibtex::delDatabase(string const & db)
246 {
247         if (contains(getContents(), db)) {
248                 string bd = db;
249                 int const n = tokenPos(getContents(), ',', bd);
250                 if (n > 0) {
251                         // Weird code, would someone care to explain this?(Lgb)
252                         string tmp(", ");
253                         tmp += bd;
254                         setContents(subst(getContents(), tmp, ", "));
255                 } else if (n == 0)
256                         setContents(split(getContents(), bd, ','));
257                 else 
258                         return false;
259         }
260         return true;
261 }
262
263
264 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
265 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
266 {
267         int w = 0;
268 #warning Ha, now we are mainly at 1.2.0 and it is still here (Jug)
269         // Does look like a hack? It is! (but will change at 0.13)
270         Paragraph * par = bv->buffer()->paragraph;
271     
272         while (par) {
273                 if (par->bibkey) {
274                         int const wx = par->bibkey->width(bv, font);
275                         if (wx > w) w = wx;
276                 }
277                 par = par->next();
278         }
279         return w;
280 }
281
282
283 // ale070405
284 string const bibitemWidest(Buffer const * buffer)
285 {
286         int w = 0;
287         // Does look like a hack? It is! (but will change at 0.13)
288         Paragraph * par = buffer->paragraph;
289         InsetBibKey * bkey = 0;
290         LyXFont font;
291       
292         while (par) {
293                 if (par->bibkey) {
294                         int const wx =
295                                 lyxfont::width(par->bibkey->getBibLabel(),
296                                                font);
297                         if (wx > w) {
298                                 w = wx;
299                                 bkey = par->bibkey;
300                         }
301                 }
302                 par = par->next();
303         }
304     
305         if (bkey && !bkey->getBibLabel().empty())
306                 return bkey->getBibLabel();
307     
308         return "99";
309 }