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