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