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