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