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