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