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