]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/biblio.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / biblio.C
index f4d978cc88fdbf318e13cc55b48b05aec7b57c6b..32d4f8af3610f172bc9b250fe5d6afe14d716add 100644 (file)
  * Licence details can be found in the file COPYING.
  *
  * \author Angus Leeming
- * \author Herbert Voss
+ * \author Herbert Voß
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
-#include "LString.h"
 #include "biblio.h"
-#include "gettext.h" // for _()
-#include "helper_funcs.h"
+
+#include "buffer.h"
+#include "bufferparams.h"
+#include "gettext.h"
+
 #include "support/lstrings.h"
-#include "support/LAssert.h"
 
 #include <boost/regex.hpp>
 
 #include <algorithm>
+#include <sstream>
 
+using std::string;
+using std::ostringstream;
 using std::vector;
 
+
+namespace lyx {
+
+using support::ascii_lowercase;
+using support::bformat;
+using support::compare_ascii_no_case;
+using support::contains;
+using support::getVectorFromString;
+using support::ltrim;
+using support::prefixIs;
+using support::rtrim;
+using support::split;
+using support::subst;
+using support::token;
+using support::trim;
+
 namespace biblio {
 
+namespace {
+
+vector<string> const init_possible_cite_commands()
+{
+       char const * const pos[] = {
+               "cite",
+               "citet", "citep", "citealt", "citealp",
+               "citeauthor", "citeyear", "citeyearpar",
+               "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
+               "Citet",  "Citep",  "Citealt",  "Citealp",  "Citeauthor",
+               "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
+               "fullcite",
+               "footcite", "footcitet", "footcitep", "footcitealt",
+               "footcitealp", "footciteauthor", "footciteyear",
+               "footciteyearpar",
+               "citefield",
+               "citetitle",
+               "cite*"
+       };
+       size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
+
+       return vector<string>(pos, pos + size_pos);
+}
+
+
+vector<string> const & possible_cite_commands()
+{
+       static vector<string> const pos = init_possible_cite_commands();
+       return pos;
+}
+
+
+bool is_possible_cite_command(string const & input)
+{
+       vector<string> const & possibles = possible_cite_commands();
+       vector<string>::const_iterator const end = possibles.end();
+       return std::find(possibles.begin(), end, input) != end;
+}
+
+
+string const default_cite_command(CiteEngine engine)
+{
+       string str;
+       switch (engine) {
+       case ENGINE_BASIC:
+               str = "cite";
+               break;
+       case ENGINE_NATBIB_AUTHORYEAR:
+               str = "citet";
+               break;
+       case ENGINE_NATBIB_NUMERICAL:
+               str = "citep";
+               break;
+       case ENGINE_JURABIB:
+               str = "cite";
+               break;
+       }
+       return str;
+}
+
+} // namespace anon
+
+
+string const asValidLatexCommand(string const & input,
+                                CiteEngine_enum const & engine)
+{
+       string const default_str = default_cite_command(engine);
+       if (!is_possible_cite_command(input))
+               return default_str;
+
+       string output;
+       switch (engine) {
+       case ENGINE_BASIC:
+               output = default_str;
+               break;
+
+       case ENGINE_NATBIB_AUTHORYEAR:
+       case ENGINE_NATBIB_NUMERICAL:
+               if (input == "cite" || input == "citefield" ||
+                   input == "citetitle" || input == "cite*")
+                       output = default_str;
+               else if (prefixIs(input, "foot"))
+                       output = input.substr(4);
+               else
+                       output = input;
+               break;
+
+       case ENGINE_JURABIB: {
+               // Jurabib does not support the 'uppercase' natbib style.
+               if (input[0] == 'C')
+                       output = string(1, 'c') + input.substr(1);
+               else
+                       output = input;
+
+               // Jurabib does not support the 'full' natbib style.
+               string::size_type const n = output.size() - 1;
+               if (output != "cite*" && output[n] == '*')
+                       output = output.substr(0, n);
+
+               break;
+       }
+       }
+
+       return output;
+}
+
+
 string const familyName(string const & name)
 {
        // Very simple parser
@@ -40,10 +163,10 @@ string const familyName(string const & name)
        // "Surname, F."
        // "FirstName Surname"
        // "F. Surname"
-       string::size_type idx = fname.find(",");
+       string::size_type idx = fname.find(',');
        if (idx != string::npos)
                return ltrim(fname.substr(0, idx));
-       idx = fname.rfind(".");
+       idx = fname.rfind('.');
        if (idx != string::npos)
                fname = ltrim(fname.substr(idx + 1));
        // test if we have a LaTeX Space in front
@@ -56,7 +179,7 @@ string const familyName(string const & name)
 
 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
 {
-       lyx::Assert(!map.empty());
+       BOOST_ASSERT(!map.empty());
 
        InfoMap::const_iterator it = map.find(key);
        if (it == map.end())
@@ -80,6 +203,7 @@ string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
        }
 
        string author = parseBibTeX(data, "author");
+
        if (author.empty())
                author = parseBibTeX(data, "editor");
 
@@ -94,19 +218,20 @@ string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
        if (authors.empty())
                return author;
 
-       author = familyName(authors[0]);
        if (authors.size() == 2)
-               author += _(" and ") + familyName(authors[1]);
-       else if (authors.size() > 2)
-               author += _(" et al.");
+               return bformat(_("%1$s and %2$s"),
+                       familyName(authors[0]), familyName(authors[1]));
+
+       if (authors.size() > 2)
+               return bformat(_("%1$s et al."), familyName(authors[0]));
 
-       return author;
+       return familyName(authors[0]);
 }
 
 
 string const getYear(InfoMap const & map, string const & key)
 {
-       lyx::Assert(!map.empty());
+       BOOST_ASSERT(!map.empty());
 
        InfoMap::const_iterator it = map.find(key);
        if (it == map.end())
@@ -144,8 +269,9 @@ string const getYear(InfoMap const & map, string const & key)
 namespace {
 
 // A functor for use with std::sort, leading to case insensitive sorting
-struct compareNoCase: public std::binary_function<string, string, bool>
+class compareNoCase: public std::binary_function<string, string, bool>
 {
+public:
        bool operator()(string const & s1, string const & s2) const {
                return compare_ascii_no_case(s1, s2) < 0;
        }
@@ -170,7 +296,7 @@ vector<string> const getKeys(InfoMap const & map)
 
 string const getInfo(InfoMap const & map, string const & key)
 {
-       lyx::Assert(!map.empty());
+       BOOST_ASSERT(!map.empty());
 
        InfoMap::const_iterator it = map.find(key);
        if (it == map.end())
@@ -198,6 +324,7 @@ string const getInfo(InfoMap const & map, string const & key)
        string number     = parseBibTeX(data, "number");
        string volume     = parseBibTeX(data, "volume");
        string pages      = parseBibTeX(data, "pages");
+       string annote     = parseBibTeX(data, "annote");
 
        string media      = parseBibTeX(data, "journal");
        if (media.empty())
@@ -226,8 +353,10 @@ string const getInfo(InfoMap const & map, string const & key)
                result << ", pp. " << pages;
        if (!year.empty())
                result << ", " << year;
+       if (!annote.empty())
+               result << "\n\n" << annote;
 
-       string const result_str = rtrim(STRCONV(result.str()));
+       string const result_str = rtrim(result.str());
        if (!result_str.empty())
                return result_str;
 
@@ -239,36 +368,35 @@ string const getInfo(InfoMap const & map, string const & key)
 namespace {
 
 // Escape special chars.
-// All characters are literals except: .|*?+(){}[]^$\
+// All characters are literals except: '.|*?+(){}[]^$\'
 // These characters are literals when preceded by a "\", which is done here
 string const escape_special_chars(string const & expr)
 {
-       // Search for all chars .|*?+(){}[^$]\
-       // Note that '[', ']' and '\' must be escaped.
+       // Search for all chars '.|*?+(){}[^$]\'
+       // Note that '[' and '\' must be escaped.
        // This is a limitation of boost::regex, but all other chars in BREs
        // are assumed literal.
-        boost::RegEx reg("[.|*?+(){}^$\\[\\]\\\\]");
+       boost::RegEx reg("[].|*?+(){}^$\\[\\\\]");
 
        // $& is a perl-like expression that expands to all of the current match
        // The '$' must be prefixed with the escape character '\' for
        // boost to treat it as a literal.
        // Thus, to prefix a matched expression with '\', we use:
-        string const fmt("\\\\$&");
-
-       return reg.Merge(expr, fmt);
+       return reg.Merge(expr, "\\\\$&");
 }
 
 
 // A functor for use with std::find_if, used to ascertain whether a
 // data entry matches the required regex_
-struct RegexMatch
+class RegexMatch : public std::unary_function<string, bool>
 {
+public:
        // re and icase are used to construct an instance of boost::RegEx.
        // if icase is true, then matching is insensitive to case
        RegexMatch(InfoMap const & m, string const & re, bool icase)
                : map_(m), regex_(re, icase) {}
 
-       bool operator()(string const & key) {
+       bool operator()(string const & key) const {
                if (!validRE())
                        return false;
 
@@ -277,7 +405,7 @@ struct RegexMatch
                string data = key;
                InfoMap::const_iterator info = map_.find(key);
                if (info != map_.end())
-                       data += " " + info->second;
+                       data += ' ' + info->second;
 
                // Attempts to find a match for the current RE
                // somewhere in data.
@@ -288,7 +416,7 @@ struct RegexMatch
 
 private:
        InfoMap const map_;
-       boost::RegEx regex_;
+       mutable boost::RegEx regex_;
 };
 
 } // namespace anon
@@ -355,14 +483,18 @@ string const parseBibTeX(string data, string const & findkey)
                string::size_type const idx =
                        dummy.empty() ? string::npos : dummy.find('%');
                if (idx != string::npos)
-                       dummy.erase(idx, string::npos);
+                       // Check if this is really a comment or just "\%"
+                       if (idx == 0 || dummy[idx - 1] != '\\')
+                               dummy.erase(idx, string::npos);
+                       else  //  This is "\%", so just erase the '\'
+                               dummy.erase(idx - 1, 1);
                // do we have a new token or a new line of
                // the same one? In the first case we ignore
                // the \n and in the second we replace it
                // with a space
                if (!dummy.empty()) {
-                       if (!contains(dummy, "="))
-                               data_ += (' ' + dummy);
+                       if (!contains(dummy, '='))
+                               data_ += ' ' + dummy;
                        else
                                data_ += dummy;
                }
@@ -404,7 +536,7 @@ string const parseBibTeX(string data, string const & findkey)
        keyvalue = dummy;
        dummy = token(data, ',', Entries++);
        while (!contains(dummy, '=') && !dummy.empty()) {
-               keyvalue += (',' + dummy);
+               keyvalue += ',' + dummy;
                dummy = token(data, ',', Entries++);
        }
 
@@ -470,7 +602,6 @@ string const parseBibTeX(string data, string const & findkey)
 
 namespace {
 
-using namespace biblio;
 
 char const * const citeCommands[] = {
        "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
@@ -501,21 +632,21 @@ unsigned int const nCiteStylesUCase =
 } // namespace anon
 
 
-CitationStyle const getCitationStyle(string const & command)
+CitationStyle::CitationStyle(string const & command)
+       : style(CITE), full(false), forceUCase(false)
 {
-       if (command.empty()) return CitationStyle();
+       if (command.empty())
+               return;
 
-       CitationStyle cs;
        string cmd = command;
-
        if (cmd[0] == 'C') {
-               cs.forceUCase = true;
+               forceUCase = true;
                cmd[0] = 'c';
        }
 
-       size_t n = cmd.size() - 1;
-       if (cmd[n] == '*') {
-               cs.full = true;
+       string::size_type const n = cmd.size() - 1;
+       if (cmd != "cite" && cmd[n] == '*') {
+               full = true;
                cmd = cmd.substr(0,n);
        }
 
@@ -524,25 +655,23 @@ CitationStyle const getCitationStyle(string const & command)
 
        if (ptr != last) {
                size_t idx = ptr - citeCommands;
-               cs.style = citeStyles[idx];
+               style = citeStyles[idx];
        }
-
-       return cs;
 }
 
 
-string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
+string const CitationStyle::asLatexStr() const
 {
-       string cite = citeCommands[command];
+       string cite = citeCommands[style];
        if (full) {
                CiteStyle const * last = citeStylesFull + nCiteStylesFull;
-               if (std::find(citeStylesFull, last, command) != last)
-                       cite += "*";
+               if (std::find(citeStylesFull, last, style) != last)
+                       cite += '*';
        }
 
        if (forceUCase) {
                CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
-               if (std::find(citeStylesUCase, last, command) != last)
+               if (std::find(citeStylesUCase, last, style) != last)
                        cite[0] = 'C';
        }
 
@@ -550,22 +679,40 @@ string const getCiteCommand(CiteStyle command, bool full, bool forceUCase)
 }
 
 
-vector<CiteStyle> const getCiteStyles(bool usingNatbib)
+CiteEngine_enum getEngine(Buffer const & buffer)
 {
-       unsigned int nStyles = 1;
+       return buffer.params().cite_engine;
+}
+
+
+vector<CiteStyle> const getCiteStyles(CiteEngine_enum const & engine)
+{
+       unsigned int nStyles = 0;
        unsigned int start = 0;
-       if (usingNatbib) {
+
+       switch (engine) {
+       case ENGINE_BASIC:
+               nStyles = 1;
+               start = 0;
+               break;
+       case ENGINE_NATBIB_AUTHORYEAR:
+       case ENGINE_NATBIB_NUMERICAL:
                nStyles = nCiteStyles - 1;
                start = 1;
+               break;
+       case ENGINE_JURABIB:
+               nStyles = nCiteStyles;
+               start = 0;
+               break;
        }
 
-       vector<CiteStyle> styles(nStyles);
+       typedef vector<CiteStyle> cite_vec;
 
-       vector<CiteStyle>::size_type i = 0;
+       cite_vec styles(nStyles);
+       cite_vec::size_type i = 0;
        int j = start;
-       for (; i != styles.size(); ++i, ++j) {
+       for (; i != styles.size(); ++i, ++j)
                styles[i] = citeStyles[j];
-       }
 
        return styles;
 }
@@ -615,7 +762,7 @@ getNumericalStrings(string const & key,
                        break;
 
                case CITEYEARPAR:
-                       str = "(" + year + ")";
+                       str = '(' + year + ')';
                        break;
                }
 
@@ -645,16 +792,21 @@ getAuthorYearStrings(string const & key,
 
                switch (styles[i]) {
                case CITE:
+                       // jurabib only: Author/Annotator
+                       // (i.e. the "before" field, 2nd opt arg)
+                       str = author + "/<" + _("before") + '>';
+                       break;
+
                case CITET:
-                       str = author + " (" + year + ")";
+                       str = author + " (" + year + ')';
                        break;
 
                case CITEP:
-                       str = "(" + author + ", " + year + ")";
+                       str = '(' + author + ", " + year + ')';
                        break;
 
                case CITEALT:
-                       str = author + " " + year ;
+                       str = author + ' ' + year ;
                        break;
 
                case CITEALP:
@@ -670,7 +822,7 @@ getAuthorYearStrings(string const & key,
                        break;
 
                case CITEYEARPAR:
-                       str = "(" + year + ")";
+                       str = '(' + year + ')';
                        break;
                }
 
@@ -681,3 +833,4 @@ getAuthorYearStrings(string const & key,
 }
 
 } // namespace biblio
+} // namespace lyx