]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibtex.C
The markDirty() and fitCursor() changes
[lyx.git] / src / insets / insetbibtex.C
1 /**
2  * \file insetbibtex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  *
8  * Full author contact details are available in file CREDITS
9  */
10 #include <config.h>
11
12
13 #include "insetbibtex.h"
14 #include "buffer.h"
15 #include "BufferView.h"
16 #include "debug.h"
17 #include "funcrequest.h"
18 #include "gettext.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
36
37 InsetBibtex::InsetBibtex(InsetCommandParams const & p, bool)
38         : InsetCommand(p)
39 {}
40
41
42 InsetBibtex::~InsetBibtex()
43 {
44         InsetCommandMailer mailer("bibtex", *this);
45         mailer.hideDialog();
46 }
47
48
49 dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
50 {
51         Inset::RESULT result = UNDISPATCHED;
52
53         switch (cmd.action) {
54         case LFUN_INSET_MODIFY: {
55                 InsetCommandParams p;
56                 InsetCommandMailer::string2params(cmd.argument, p);
57                 if (p.getCmdName().empty())
58                         break;
59
60                 if (view() && p.getContents() != params().getContents()) {
61                         view()->ChangeCitationsIfUnique(params().getContents(),
62                                                         p.getContents());
63                 }
64
65                 setParams(p);
66                 cmd.view()->updateInset(this);
67                 result = DISPATCHED;
68         }
69         break;
70         default:
71                 result = InsetCommand::localDispatch(cmd);
72         }
73
74         return result;
75 }
76
77 string const InsetBibtex::getScreenLabel(Buffer const *) const
78 {
79         return _("BibTeX Generated References");
80 }
81
82
83 int InsetBibtex::latex(Buffer const * buffer, ostream & os,
84                        bool /*fragile*/, bool/*fs*/) const
85 {
86         // changing the sequence of the commands
87         // 1. \bibliographystyle{style}
88         // 2. \addcontentsline{...} - if option bibtotoc set
89         // 3. \bibliography{database}
90         string adb;
91         string db_in = getContents();
92         db_in = split(db_in, adb, ',');
93
94         // Style-Options
95         string style = getOptions(); // maybe empty! and with bibtotoc
96         string bibtotoc;
97         if (prefixIs(style, "bibtotoc")) {
98                 bibtotoc = "bibtotoc";
99                 if (contains(style, ',')) {
100                         style = split(style, bibtotoc, ',');
101                 }
102         }
103
104         if (!buffer->niceFile
105             && IsFileReadable(MakeAbsPath(style, buffer->filePath()) + ".bst")) {
106                 style = MakeAbsPath(style, buffer->filePath());
107         }
108
109         if (!style.empty()) { // we want no \biblio...{}
110                 os << "\\bibliographystyle{" << style << "}\n";
111         }
112
113         // bibtotoc-Option
114         if (!bibtotoc.empty()) {
115                 // maybe a problem when a textclass has no "art" as
116                 // part of its name, because it's than book.
117                 // For the "official" lyx-layouts it's no problem to support
118                 // all well
119                 if (!contains(buffer->params.getLyXTextClass().name(),
120                               "art")) {
121                         if (buffer->params.sides == LyXTextClass::OneSide) {
122                                 // oneside
123                                 os << "\\clearpage";
124                         } else {
125                                 // twoside
126                                 os << "\\cleardoublepage";
127                         }
128
129                         // bookclass
130                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
131
132                 } else {
133                         // article class
134                         os << "\\addcontentsline{toc}{section}{\\refname}";
135                 }
136         }
137
138         // database
139         // If we generate in a temp dir, we might need to give an
140         // absolute path there. This is a bit complicated since we can
141         // have a comma-separated list of bibliographies
142         string db_out;
143         while (!adb.empty()) {
144                 if (!buffer->niceFile &&
145                     IsFileReadable(MakeAbsPath(adb, buffer->filePath())+".bib"))
146                          adb = os::external_path(MakeAbsPath(adb, buffer->filePath()));
147                 db_out += adb;
148                 db_out += ',';
149                 db_in = split(db_in, adb,',');
150         }
151         db_out = rtrim(db_out, ",");
152         os   << "\\bibliography{" << db_out << "}\n";
153         return 2;
154 }
155
156
157 vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
158 {
159         // Doesn't appear to be used (Angus, 31 July 2001)
160         Path p(buffer.filePath());
161
162         vector<string> vec;
163
164         string tmp;
165         string bibfiles = getContents();
166         bibfiles = split(bibfiles, tmp, ',');
167         while (!tmp.empty()) {
168                 string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
169                 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
170
171                 // If we didn't find a matching file name just fail silently
172                 if (!file.empty())
173                         vec.push_back(file);
174
175                 // Get next file name
176                 bibfiles = split(bibfiles, tmp, ',');
177         }
178
179         return vec;
180 }
181
182
183 // This method returns a comma separated list of Bibtex entries
184 void InsetBibtex::fillWithBibKeys
185         (Buffer const * buffer, vector<pair<string, string> > & keys) const
186 {
187         lyx::Assert(buffer);
188         vector<string> const files = getFiles(*buffer);
189         for (vector<string>::const_iterator it = files.begin();
190              it != files.end(); ++ it) {
191                 // This is a _very_ simple parser for Bibtex database
192                 // files. All it does is to look for lines starting
193                 // in @ and not being @preamble and @string entries.
194                 // It does NOT do any syntax checking!
195                 ifstream ifs(it->c_str());
196                 string linebuf0;
197                 while (getline(ifs, linebuf0)) {
198                         string linebuf = trim(linebuf0);
199                         if (linebuf.empty()) continue;
200                         if (prefixIs(linebuf, "@")) {
201                                 linebuf = subst(linebuf, '{', '(');
202                                 string tmp;
203                                 linebuf = split(linebuf, tmp, '(');
204                                 tmp = ascii_lowercase(tmp);
205                                 if (!prefixIs(tmp, "@string")
206                                     && !prefixIs(tmp, "@preamble")) {
207                                         linebuf = split(linebuf, tmp, ',');
208                                         tmp = ltrim(tmp, " \t");
209                                         if (!tmp.empty()) {
210                                                 keys.push_back(pair<string,string>(tmp,string()));
211                                         }
212                                 }
213                         } else if (!keys.empty()) {
214                                 keys.back().second += linebuf + "\n";
215                         }
216                 }
217         }
218 }
219
220
221 void InsetBibtex::edit(BufferView * bv, int, int, mouse_button::state)
222 {
223         InsetCommandMailer mailer("bibtex", *this);
224         mailer.showDialog(bv);
225 }
226
227
228 void InsetBibtex::edit(BufferView * bv, bool)
229 {
230         edit(bv, 0, 0, mouse_button::none);
231 }
232
233
234 bool InsetBibtex::addDatabase(string const & db)
235 {
236         string contents(getContents());
237         if (!contains(contents, db)) {
238                 if (!contents.empty())
239                         contents += ',';
240                 setContents(contents + db);
241                 return true;
242         }
243         return false;
244 }
245
246
247 bool InsetBibtex::delDatabase(string const & db)
248 {
249         if (contains(getContents(), db)) {
250                 string bd = db;
251                 int const n = tokenPos(getContents(), ',', bd);
252                 if (n > 0) {
253                         // Weird code, would someone care to explain this?(Lgb)
254                         string tmp(", ");
255                         tmp += bd;
256                         setContents(subst(getContents(), tmp, ", "));
257                 } else if (n == 0)
258                         setContents(split(getContents(), bd, ','));
259                 else
260                         return false;
261         }
262         return true;
263 }