]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/biblio.C
Fixes to References dialog (read-only status) and to Bibtex bibliography
[lyx.git] / src / frontends / controllers / biblio.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file biblio.C
11  * \author Angus Leeming <a.leeming@ic.ac.uk>
12  */
13
14 #include <config.h>
15
16 #include <vector>
17 #include <algorithm>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "LString.h"
24 #include "biblio.h"
25 #include "gettext.h" // for _()
26 #include "helper_funcs.h"
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
29 #include "support/LRegex.h"
30
31 using std::find;
32 using std::min;
33 using std::vector;
34 using std::sort;
35
36 namespace biblio 
37 {
38
39 namespace {
40
41 using namespace biblio;
42     
43 char const * const citeCommands[] = {
44         "cite", "citet", "citep", "citealt", "citealp", "citeauthor", 
45         "citeyear", "citeyearpar" };
46
47 unsigned int const nCiteCommands =
48         sizeof(citeCommands) / sizeof(char *);
49
50 CiteStyle const citeStyles[] = {
51         CITE, CITET, CITEP, CITEALT, CITEALP,
52         CITEAUTHOR, CITEYEAR, CITEYEARPAR };
53
54 unsigned int const nCiteStyles =
55         sizeof(citeStyles) / sizeof(CiteStyle);
56
57 CiteStyle const citeStylesFull[] = {
58         CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
59
60 unsigned int const nCiteStylesFull =
61         sizeof(citeStylesFull) / sizeof(CiteStyle);
62
63 CiteStyle const citeStylesUCase[] = {
64         CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
65
66 unsigned int const nCiteStylesUCase =
67         sizeof(citeStylesUCase) / sizeof(CiteStyle);
68  
69
70 // The functions doing the dirty work for the search.
71 vector<string>::const_iterator
72 simpleSearch(InfoMap const & theMap,
73              vector<string> const & keys,
74              string const & expr,
75              vector<string>::const_iterator start,
76              Direction dir,
77              bool caseSensitive)
78 {
79         string tmp = expr;
80         if (!caseSensitive)
81                 tmp = lowercase(tmp);
82
83         vector<string> searchwords = getVectorFromString(tmp, " ");
84
85         // Loop over all keys from start...
86         for (vector<string>::const_iterator it = start;
87              // End condition is direction-dependent.
88              (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
89              // increment is direction-dependent.
90              (dir == FORWARD) ? (++it) : (--it)) {
91
92                 string data = (*it);
93                 InfoMap::const_iterator info = theMap.find(*it);
94                 if (info != theMap.end())
95                         data += " " + info->second;
96                 if (!caseSensitive)
97                         data = lowercase(data);
98
99                 bool found = true;
100
101                 // Loop over all search words...
102                 for (vector<string>::const_iterator sit = searchwords.begin();
103                      sit != searchwords.end(); ++sit) {
104                         if (data.find(*sit) == string::npos) {
105                                 found = false;
106                                 break;
107                         }
108                 }
109                 
110                 if (found) return it;
111         }
112
113         return keys.end();
114 }
115
116  
117 vector<string>::const_iterator
118 regexSearch(InfoMap const & theMap,
119             vector<string> const & keys,
120             string const & expr,
121             vector<string>::const_iterator start,
122             Direction dir)
123 {
124         LRegex reg(expr);
125
126         for (vector<string>::const_iterator it = start;
127              // End condition is direction-dependent.
128              (dir == FORWARD) ? (it<keys.end()) : (it>=keys.begin());
129              // increment is direction-dependent.
130              (dir == FORWARD) ? (++it) : (--it)) {
131
132                 string data = (*it);
133                 InfoMap::const_iterator info = theMap.find(*it);
134                 if (info != theMap.end())
135                         data += " " + info->second;
136
137                 if (reg.exec(data).size() > 0)
138                         return it;
139         }
140
141         return keys.end();
142 }
143
144 string const familyName(string const & name)
145 {
146         // Very simple parser
147         string fname = name;
148
149         string::size_type idx = fname.rfind(".");
150         if (idx != string::npos)
151                 fname = frontStrip(fname.substr(idx+1));
152
153         return fname;
154 }
155
156
157 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
158 {
159         lyx::Assert(!map.empty());
160
161         InfoMap::const_iterator it = map.find(key);
162
163         string author;
164         if (it != map.end()) {
165                 author = parseBibTeX(it->second, "author");
166                 if (author.empty())
167                         author = parseBibTeX(it->second, "editor");
168
169                 vector<string> authors = getVectorFromString(author, "and");
170
171                 if (!authors.empty()) {
172                         author.erase();
173
174                         for (vector<string>::iterator it = authors.begin();
175                              it != authors.end(); ++it) {
176                                 *it = familyName(strip(*it));
177                         }
178
179                         author = authors[0];
180                         if (authors.size() == 2)
181                                 author += _(" and ") + authors[1];
182                         else if (authors.size() > 2)
183                                 author += _(" et al.");
184                 }
185         }
186
187         if (author.empty())
188                 author = _("Caesar et al.");
189
190         return author;
191 }
192
193
194 string const getYear(InfoMap const & map, string const & key)
195 {
196         lyx::Assert(!map.empty());
197
198         InfoMap::const_iterator it = map.find(key);
199
200         string year;
201
202         if (it != map.end())
203                 year = parseBibTeX(it->second, "year");
204
205         if (year.empty())
206                 year = "50BC";
207
208         return year;
209 }
210
211 } // namespace anon 
212
213
214
215
216
217
218
219 // A functor for use with std::sort, leading to case insensitive sorting
220 struct compareNoCase: public std::binary_function<string, string, bool> 
221 {
222         bool operator()(string const & s1, string const & s2) const {
223                 return compare_no_case(s1, s2) < 0;
224         }
225 };
226
227 vector<string> const getKeys(InfoMap const & map)
228 {
229         vector<string> bibkeys;
230
231         for (InfoMap::const_iterator it = map.begin(); it != map.end(); ++it) {
232                 bibkeys.push_back(it->first);
233         }
234
235         sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
236         return bibkeys;
237 }
238
239
240 string const getInfo(InfoMap const & map, string const & key)
241 {
242         lyx::Assert(!map.empty());
243
244         InfoMap::const_iterator it = map.find(key);
245         if (it == map.end()) return string();
246
247         // Search for all possible "required" keys
248         string author = parseBibTeX(it->second, "author");
249         if (author.empty())
250                 author = parseBibTeX(it->second, "editor");
251
252         string year       = parseBibTeX(it->second, "year");
253         string title      = parseBibTeX(it->second, "title");
254         string booktitle  = parseBibTeX(it->second, "booktitle");
255         string chapter    = parseBibTeX(it->second, "chapter");
256         string number     = parseBibTeX(it->second, "number");
257         string volume     = parseBibTeX(it->second, "volume");
258         string pages      = parseBibTeX(it->second, "pages");
259
260         string media      = parseBibTeX(it->second, "journal");
261         if (media.empty())
262                 media = parseBibTeX(it->second, "publisher");
263         if (media.empty())
264                 media = parseBibTeX(it->second, "school");
265         if (media.empty())
266                 media = parseBibTeX(it->second, "institution");
267
268         ostringstream result;
269         result << author;
270         if (!title.empty())
271                 result << ", " << title;
272         if (!booktitle.empty())
273                 result << ", in " << booktitle;
274         if (!chapter.empty())
275                 result << ", Ch. " << chapter;
276         if (!media.empty())
277                 result << ", " << media;
278         if (!volume.empty())
279                 result << ", vol. " << volume;
280         if (!number.empty())
281                 result << ", no. " << number;
282         if (!pages.empty())
283                 result << ", pp. " << pages;
284         if (!year.empty())
285                 result << ", " << year;
286
287         if (result.str().empty()) // not a BibTeX record
288                 result << it->second;
289
290         return result.str().c_str();
291 }
292  
293
294 vector<string>::const_iterator
295 searchKeys(InfoMap const & theMap,
296            vector<string> const & keys,
297            string const & expr,
298            vector<string>::const_iterator start,
299            Search type,
300            Direction dir,
301            bool caseSensitive)
302 {
303         // Preliminary checks
304         if(start < keys.begin() || start >= keys.end())
305                 return keys.end();
306         
307         string search_expr = frontStrip(strip(expr));
308         if (search_expr.empty())
309                 return keys.end();
310
311         if (type == SIMPLE)
312                 return simpleSearch(theMap, keys, search_expr, start, dir,
313                                     caseSensitive);
314
315         return regexSearch(theMap, keys, search_expr, start, dir);
316 }
317
318
319 string const parseBibTeX(string data, string const & findkey)
320 {
321         string keyvalue;
322         
323         for (string::iterator it=data.begin(); it<data.end(); ++it) {
324                 if ((*it) == '\n' || (*it) == '\t')
325                         (*it)= ' ';
326         }
327         
328         data = frontStrip(data);
329         while (!data.empty() && data[0] != '=' && 
330                (data.find(' ') != string::npos || data.find('=') != string::npos)) {
331
332                 string::size_type keypos = min(data.find(' '), data.find('='));
333                 string key = lowercase(data.substr(0, keypos));
334       
335                 data = data.substr(keypos, data.length()-1);
336                 data = frontStrip(strip(data));
337                 if (data.length() > 1 && data[0]=='=') {
338                         data = frontStrip(data.substr(1, data.length()-1));
339                         if (!data.empty()) {
340                                 keypos = 1;
341                                 string value;
342                                 char enclosing;
343
344                                 if (data[0]=='{') {
345                                         enclosing = '}';
346                                 } else if (data[0]=='"') {
347                                         enclosing = '"';
348                                 } else {
349                                         keypos=0;
350                                         enclosing=' ';
351                                 }
352
353                                 if (keypos &&
354                                     data.find(enclosing)!=string::npos &&
355                                     data.length()>1) {
356                                         string tmp = data.substr(keypos,
357                                                                  data.length()-1);
358                                         while (tmp.find('{') != string::npos &&
359                                                tmp.find('}') != string::npos &&
360                                                tmp.find('{') < tmp.find('}') &&
361                                                tmp.find('{') < tmp.find(enclosing)) {
362
363                                                 keypos += tmp.find('{')+1;
364                                                 tmp = data.substr(keypos,
365                                                                   data.length()-1);
366                                                 keypos += tmp.find('}')+1;
367                                                 tmp = data.substr(keypos,
368                                                                   data.length()-1);
369                                         }
370
371                                         if (tmp.find(enclosing)==string::npos)
372                                                 return keyvalue;
373                                         else {
374                                                 keypos += tmp.find(enclosing);
375                                                 tmp = data.substr(keypos,
376                                                                   data.length()-1);
377                                         }
378
379                                         value = data.substr(1, keypos-1);
380
381                                         if (keypos+1<data.length()-1)
382                                                 data = frontStrip(data.substr(keypos+1, data.length()-1));
383                                         else
384                                                 data = "";
385
386                                 } else if (!keypos &&
387                                            (data.find(' ') ||
388                                             data.find(','))) {
389                                         keypos = data.length()-1;
390                                         if (data.find(' ') != string::npos)
391                                                 keypos = data.find(' ');
392                                         if (data.find(',') != string::npos &&
393                                             keypos > data.find(','))
394                                                 keypos = data.find(',');
395
396                                         value = data.substr(0, keypos);
397                   
398                                         if (keypos+1<data.length()-1)
399                                                 data = frontStrip(data.substr(keypos+1, data.length()-1));
400                                         else
401                                                 data = "";
402                                 }
403                                 else
404                                         return keyvalue;
405
406                                 if (findkey == key) {
407                                         keyvalue = value;
408                                         return keyvalue;
409                                 } 
410
411                                 data = frontStrip(frontStrip(data,','));
412                         }
413                 }
414                 else return keyvalue;
415         }
416         return keyvalue;
417 }
418
419
420 CitationStyle const getCitationStyle(string const & command)
421 {
422         if (command.empty()) return CitationStyle();
423     
424         CitationStyle cs;
425         string cmd = command;
426
427         if (cmd[0] == 'C') {
428                 cs.forceUCase = true;
429                 cmd[0] = 'c';
430         }
431
432         size_t n = cmd.size()-1;
433         if (cmd[n] == '*') {
434                 cs.full = true;
435                 cmd = cmd.substr(0,n);
436         }
437
438         char const * const * const last = citeCommands + nCiteCommands;
439         char const * const * const ptr = std::find(citeCommands, last, cmd);
440
441         if (ptr != last) {
442                 size_t idx = ptr - citeCommands;
443                 cs.style = citeStyles[idx];
444         }
445
446         return cs;
447 }
448
449
450 string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
451 {
452         string cite = citeCommands[command];
453         if (full) {
454                 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
455                 if (std::find(citeStylesFull, last, command) != last)
456                         cite += "*";
457         }
458
459         if (forceUCase) {
460                 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
461                 if (std::find(citeStylesUCase, last, command) != last)
462                         cite[0] = 'C';
463         }
464
465         return cite;
466 }
467
468         
469 vector<CiteStyle> const getCiteStyles(bool usingNatbib)
470 {
471         unsigned int nStyles = 1;
472         unsigned int start = 0;
473         if (usingNatbib) {
474                 nStyles = nCiteStyles - 1;
475                 start = 1;
476         }
477
478         vector<CiteStyle> styles(nStyles);
479
480         vector<CiteStyle>::size_type i = 0;
481         int j = start;
482         for (; i != styles.size(); ++i, ++j) {
483                 styles[i] = citeStyles[j];
484         }
485
486         return styles;
487 }
488
489
490 vector<string> const
491 getNumericalStrings(string const & key,
492                     InfoMap const & map, vector<CiteStyle> const & styles)
493 {
494         if (map.empty()) {
495                 vector<string> vec(1);
496                 vec[0] = _("No database");
497                 return vec;
498         }
499         
500         vector<string> vec(styles.size());
501
502         string const author = getAbbreviatedAuthor(map, key);
503         string const year   = getYear(map, key);
504         
505         for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
506                 string str;
507
508                 switch (styles[i]) {
509                 case CITE:
510                 case CITEP:
511                         str = "[#ID]";
512                         break;
513                         
514                 case CITET:
515                         str = author + " [#ID]";
516                         break;
517                         
518                 case CITEALT:
519                         str = author + " #ID";
520                         break;
521                         
522                 case CITEALP:
523                         str = "#ID";
524                         break;
525                         
526                 case CITEAUTHOR:
527                         str = author;
528                         break;
529                         
530                 case CITEYEAR:
531                         str = year;
532                         break;
533                         
534                 case CITEYEARPAR:
535                         str = "(" + year + ")";
536                         break;
537                 }
538
539                 vec[i] = str;
540         }
541         
542         return vec;
543 }
544
545
546 vector<string> const
547 getAuthorYearStrings(string const & key,
548                     InfoMap const & map, vector<CiteStyle> const & styles)
549 {
550         if (map.empty()) {
551                 vector<string> vec(1);
552                 vec[0] = _("No database");
553                 return vec;
554         }
555         
556         vector<string> vec(styles.size());
557
558         string const author = getAbbreviatedAuthor(map, key);
559         string const year   = getYear(map, key);
560         
561         for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
562                 string str;
563
564                 switch (styles[i]) {
565                 case CITET:
566                         str = author + " (" + year + ")";
567                         break;
568                         
569                 case CITE:
570                 case CITEP:
571                         str = "(" + author + ", " + year + ")";
572                         break;
573                         
574                 case CITEALT:
575                         str = author + " " + year ;
576                         break;
577                         
578                 case CITEALP:
579                         str = author + ", " + year ;
580                         break;
581                         
582                 case CITEAUTHOR:
583                         str = author;
584                         break;
585                         
586                 case CITEYEAR:
587                         str = year;
588                         break;
589                         
590                 case CITEYEARPAR:
591                         str = "(" + year + ")";
592                         break;
593                 }
594
595                 vec[i] = str;
596         }
597         
598         return vec;
599 }
600
601 } // namespace biblio