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