]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
The BIG UNDO patch. Recodes undo handling for better use inside InsetText.
[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 "lyxrc.h"
21 #include "font.h"
22 #include "LyXView.h" 
23
24 using std::ostream;
25 using std::ifstream;
26 using std::getline;
27 using std::endl;
28 using std::vector;
29 using std::pair;
30 using std::max;
31
32 int InsetBibKey::key_counter = 0;
33 const string key_prefix = "key-";
34
35 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
36         : InsetCommand(p)
37 {
38         counter = 1;
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() 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 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
117         : InsetCommand(p)
118 {}
119
120
121 InsetBibtex::~InsetBibtex()
122 {
123 }
124
125
126 string const InsetBibtex::getScreenLabel() const
127 {
128         return _("BibTeX Generated References");
129 }
130
131
132 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
133                        bool /*fragile*/, bool/*fs*/) const
134 {
135         // If we generate in a temp dir, we might need to give an
136         // absolute path there. This is a bit complicated since we can
137         // have a comma-separated list of bibliographies
138         string adb, db_out;
139         string db_in = getContents();
140         db_in = split(db_in, adb, ',');
141         while(!adb.empty()) {
142                 if (!buffer->niceFile &&
143                     IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib")) 
144                          adb = os::external_path(MakeAbsPath(adb, buffer->filepath));
145
146                 db_out += adb;
147                 db_out += ',';
148                 db_in= split(db_in, adb,',');
149         }
150         db_out = strip(db_out, ',');
151         // Idem, but simpler
152         string style;
153         if (!buffer->niceFile 
154             && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
155                               + ".bst")) 
156                 style = MakeAbsPath(getOptions(), buffer->filepath);
157         else
158                 style = getOptions();
159
160         os << "\\bibliographystyle{" << style << "}\n"
161            << "\\bibliography{" << db_out << "}\n";
162         return 2;
163 }
164
165
166 // This method returns a comma separated list of Bibtex entries
167 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
168 {
169         Path p(buffer->filepath);
170
171         vector<pair<string,string> > keys;
172         string tmp;
173         string bibfiles = getContents();
174         bibfiles = split(bibfiles, tmp, ',');
175         while(!tmp.empty()) {
176                 string fil = findtexfile(ChangeExtension(tmp, "bib"),
177                                          "bib");
178                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
179                 // If we didn't find a matching file name just fail silently
180                 if (!fil.empty()) {
181                         // This is a _very_ simple parser for Bibtex database
182                         // files. All it does is to look for lines starting
183                         // in @ and not being @preamble and @string entries.
184                         // It does NOT do any syntax checking!
185                         ifstream ifs(fil.c_str());
186                         string linebuf0;
187                         while (getline(ifs, linebuf0)) {
188                                 string linebuf = frontStrip(strip(linebuf0));
189                                 if (linebuf.empty() ) continue;
190                                 if (prefixIs(linebuf, "@")) {
191                                         linebuf = subst(linebuf, '{', '(');
192                                         linebuf = split(linebuf, tmp, '(');
193                                         tmp = lowercase(tmp);
194                                         if (!prefixIs(tmp, "@string")
195                                             && !prefixIs(tmp, "@preamble")) {
196                                                 linebuf = split(linebuf, tmp, ',');
197                                                 tmp = frontStrip(tmp);
198                                                 if (!tmp.empty()) {
199                                                         keys.push_back(pair<string,string>(tmp,string()));
200                                                 }
201                                         }
202                                 } else if (!keys.empty()) {
203                                         keys.back().second += linebuf + "\n";
204                                 }
205                         }
206                 }
207                 // Get next file name
208                 bibfiles = split(bibfiles, tmp, ',');
209         }
210         return keys;
211 }
212
213
214 void InsetBibtex::edit(BufferView * bv, int, int, unsigned int)
215 {
216         bv->owner()->getDialogs()->showBibtex(this);
217 }
218
219
220 bool InsetBibtex::addDatabase(string const & db)
221 {
222         string contents(getContents());
223         if (!contains(contents, db)) {
224                 if (!contents.empty()) 
225                         contents += ",";
226                 setContents(contents + db);
227                 return true;
228         }
229         return false;
230 }
231
232
233 bool InsetBibtex::delDatabase(string const & db)
234 {
235         if (contains(getContents(), db)) {
236                 string bd = db;
237                 int const n = tokenPos(getContents(), ',', bd);
238                 if (n > 0) {
239                         // Weird code, would someone care to explain this?(Lgb)
240                         string tmp(", ");
241                         tmp += bd;
242                         setContents(subst(getContents(), tmp, ", "));
243                 } else if (n == 0)
244                         setContents(split(getContents(), bd, ','));
245                 else 
246                         return false;
247         }
248         return true;
249 }
250
251
252 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
253 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
254 {
255         int w = 0;
256         // Does look like a hack? It is! (but will change at 0.13)
257         Paragraph * par = bv->buffer()->paragraph;
258     
259         while (par) {
260                 if (par->bibkey) {
261                         int const wx = par->bibkey->width(bv, font);
262                         if (wx > w) w = wx;
263                 }
264                 par = par->next();
265         }
266         return w;
267 }
268
269
270 // ale070405
271 string const bibitemWidest(Buffer const * buffer)
272 {
273         int w = 0;
274         // Does look like a hack? It is! (but will change at 0.13)
275         Paragraph * par = buffer->paragraph;
276         InsetBibKey * bkey = 0;
277         LyXFont font;
278       
279         while (par) {
280                 if (par->bibkey) {
281                         int const wx =
282                                 lyxfont::width(par->bibkey->getBibLabel(),
283                                                font);
284                         if (wx > w) {
285                                 w = wx;
286                                 bkey = par->bibkey;
287                         }
288                 }
289                 par = par->next();
290         }
291     
292         if (bkey && !bkey->getBibLabel().empty())
293                 return bkey->getBibLabel();
294     
295         return "99";
296 }