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