]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
Fix working of the spellchecker dialog with ispell when there are no
[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 "support/LAssert.h"
22 #include "lyxrc.h"
23 #include "font.h"
24 #include "LyXView.h" 
25
26 using std::ostream;
27 using std::ifstream;
28 using std::getline;
29 using std::endl;
30 using std::vector;
31 using std::pair;
32 using std::max;
33
34 int InsetBibKey::key_counter = 0;
35 const string key_prefix = "key-";
36
37 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
38         : InsetCommand(p), counter(1)
39 {
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 &, bool) 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         if (lex.eatLine()) {
85                 string const token = lex.getString();
86                 scanCommand(token);
87         } else {
88                 lex.printError("InsetCommand: Parse error: `$$Token'");
89         }
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         // changing the sequence of the commands
142         // 1. \bibliographystyle{style}
143         // 2. \addcontentsline{...} - if option bibtotoc set
144         // 3. \bibliography{database}
145         string adb, db_out;
146         string db_in = getContents();
147         db_in = split(db_in, adb, ',');
148
149         // Style-Options
150         string style = getOptions(); // maybe empty! and with bibtotoc
151         string bibtotoc;
152         if (prefixIs(style,"bibtotoc")) {
153                 bibtotoc = "bibtotoc";
154                 if (contains(style,',')) {
155                         style = split(style,bibtotoc,',');
156                 }
157         }
158
159         if (!buffer->niceFile 
160             && IsFileReadable(MakeAbsPath(style, buffer->filepath) + ".bst")) {
161                 style = MakeAbsPath(style, buffer->filepath);
162         }
163
164         if (!style.empty()) { // we want no \biblio...{}
165                 os << "\\bibliographystyle{" << style << "}\n";
166         }
167
168         // bibtotoc-Option
169         if (!bibtotoc.empty()) {
170                 // maybe a problem when a textclass has no "art" as
171                 // part of its name, because it's than book.
172                 // For the "official" lyx-layouts it's no problem to support
173                 // all well
174                 if (!contains(textclasslist.NameOfClass(buffer->params.textclass),"art")) {
175                         if (buffer->params.sides == LyXTextClass::OneSide) {
176                                 // oneside
177                                 os << "\\clearpage";
178                         } else {
179                                 // twoside
180                                 os << "\\cleardoublepage";
181                         }
182
183                         // bookclass
184                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
185
186                 } else {
187                         // article class
188                         os << "\\addcontentsline{toc}{section}{\\refname}";     
189                 }
190         }
191
192         // database
193         // If we generate in a temp dir, we might need to give an
194         // absolute path there. This is a bit complicated since we can
195         // have a comma-separated list of bibliographies
196         while(!adb.empty()) {
197                 if (!buffer->niceFile &&
198                     IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib")) 
199                          adb = os::external_path(MakeAbsPath(adb, buffer->filepath));
200                 db_out += adb;
201                 db_out += ',';
202                 db_in= split(db_in, adb,',');
203         }
204         db_out = strip(db_out, ',');
205         os   << "\\bibliography{" << db_out << "}\n";
206         return 2;
207 }
208
209
210 vector<string> const InsetBibtex::getFiles(Buffer const &) const
211 {
212         // Doesn't appear to be used (Angus, 31 July 2001)
213         // Path p(buffer->filepath);
214
215         vector<string> vec;
216
217         string tmp;
218         string bibfiles = getContents();
219         bibfiles = split(bibfiles, tmp, ',');
220         while(!tmp.empty()) {
221                 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
222                 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
223
224                 // If we didn't find a matching file name just fail silently
225                 if (!file.empty())
226                         vec.push_back(file);
227                 
228                 // Get next file name
229                 bibfiles = split(bibfiles, tmp, ',');
230         }
231
232         return vec;
233 }
234
235 // This method returns a comma separated list of Bibtex entries
236 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
237 {
238         vector<pair<string,string> > keys;
239
240         lyx::Assert(buffer);
241         vector<string> const files = getFiles(*buffer);
242         for (vector<string>::const_iterator it = files.begin();
243              it != files.end(); ++ it) {
244                 // This is a _very_ simple parser for Bibtex database
245                 // files. All it does is to look for lines starting
246                 // in @ and not being @preamble and @string entries.
247                 // It does NOT do any syntax checking!
248                 ifstream ifs(it->c_str());
249                 string linebuf0;
250                 while (getline(ifs, linebuf0)) {
251                         string linebuf = frontStrip(strip(linebuf0));
252                         if (linebuf.empty() ) continue;
253                         if (prefixIs(linebuf, "@")) {
254                                 linebuf = subst(linebuf, '{', '(');
255                                 string tmp;
256                                 linebuf = split(linebuf, tmp, '(');
257                                 tmp = lowercase(tmp);
258                                 if (!prefixIs(tmp, "@string")
259                                     && !prefixIs(tmp, "@preamble")) {
260                                         linebuf = split(linebuf, tmp, ',');
261                                         tmp = frontStrip(tmp);
262                                         if (!tmp.empty()) {
263                                                 keys.push_back(pair<string,string>(tmp,string()));
264                                         }
265                                 }
266                         } else if (!keys.empty()) {
267                                 keys.back().second += linebuf + "\n";
268                         }
269                 }
270         }
271         return keys;
272 }
273
274
275 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
276 {
277         bv->owner()->getDialogs()->showBibtex(this);
278 }
279
280
281 void InsetBibtex::edit(BufferView * bv, bool)
282 {
283         edit(bv, 0, 0, 0);
284 }
285
286
287 bool InsetBibtex::addDatabase(string const & db)
288 {
289         string contents(getContents());
290         if (!contains(contents, db)) {
291                 if (!contents.empty()) 
292                         contents += ",";
293                 setContents(contents + db);
294                 return true;
295         }
296         return false;
297 }
298
299
300 bool InsetBibtex::delDatabase(string const & db)
301 {
302         if (contains(getContents(), db)) {
303                 string bd = db;
304                 int const n = tokenPos(getContents(), ',', bd);
305                 if (n > 0) {
306                         // Weird code, would someone care to explain this?(Lgb)
307                         string tmp(", ");
308                         tmp += bd;
309                         setContents(subst(getContents(), tmp, ", "));
310                 } else if (n == 0)
311                         setContents(split(getContents(), bd, ','));
312                 else 
313                         return false;
314         }
315         return true;
316 }
317
318
319 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
320 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
321 {
322         int w = 0;
323         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
324         // Does look like a hack? It is! (but will change at 0.13)
325         Paragraph * par = bv->buffer()->paragraph;
326     
327         while (par) {
328                 if (par->bibkey) {
329                         int const wx = par->bibkey->width(bv, font);
330                         if (wx > w) w = wx;
331                 }
332                 par = par->next();
333         }
334         return w;
335 }
336
337
338 // ale070405
339 string const bibitemWidest(Buffer const * buffer)
340 {
341         int w = 0;
342         // Does look like a hack? It is! (but will change at 0.13)
343         Paragraph * par = buffer->paragraph;
344         InsetBibKey * bkey = 0;
345         LyXFont font;
346       
347         while (par) {
348                 if (par->bibkey) {
349                         int const wx =
350                                 lyxfont::width(par->bibkey->getBibLabel(),
351                                                font);
352                         if (wx > w) {
353                                 w = wx;
354                                 bkey = par->bibkey;
355                         }
356                 }
357                 par = par->next();
358         }
359     
360         if (bkey && !bkey->getBibLabel().empty())
361                 return bkey->getBibLabel();
362     
363         return "99";
364 }