]> git.lyx.org Git - lyx.git/blobdiff - src/BiblioInfo.cpp
get rid of the \use_escape_chars pref, which was not used
[lyx.git] / src / BiblioInfo.cpp
index 538922f0a2167ef32c12d781ea61c5a0a0928597..c5548af832caff6aa641cf2f6892589d04f40bb8 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Angus Leeming
- * \author Herbert Voß
+ * \author Herbert Voß
  * \author Richard Heck
  *
  * Full author contact details are available in file CREDITS.
@@ -16,6 +16,7 @@
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "buffer_funcs.h"
+#include "Encoding.h"
 #include "InsetIterator.h"
 #include "Paragraph.h"
 
 
 #include "support/docstream.h"
 #include "support/gettext.h"
+#include "support/lassert.h"
 #include "support/lstrings.h"
+#include "support/textutils.h"
 
 #include "boost/regex.hpp"
 
 using namespace std;
 using namespace lyx::support;
 
+
 namespace lyx {
 
+namespace {
+
+// gets the "family name" from an author-type string
+docstring familyName(docstring const & name)
+{
+       if (name.empty())
+               return docstring();
+
+       // first we look for a comma, and take the last name to be everything
+       // preceding the right-most one, so that we also get the "jr" part.
+       docstring::size_type idx = name.rfind(',');
+       if (idx != docstring::npos)
+               return ltrim(name.substr(0, idx));
+
+       // OK, so now we want to look for the last name. We're going to
+       // include the "von" part. This isn't perfect.
+       // Split on spaces, to get various tokens.
+       vector<docstring> pieces = getVectorFromString(name, from_ascii(" "));
+       // If we only get two, assume the last one is the last name
+       if (pieces.size() <= 2)
+               return pieces.back();
+
+       // Now we look for the first token that begins with a lower case letter.
+       vector<docstring>::const_iterator it = pieces.begin();
+       vector<docstring>::const_iterator en = pieces.end();
+       for (; it != en; ++it) {
+               if ((*it).size() == 0)
+                       continue;
+               char_type const c = (*it)[0];
+               if (isLower(c))
+                       break;
+       }
+
+       if (it == en) // we never found a "von"
+               return pieces.back();
+
+       // reconstruct what we need to return
+       docstring retval;
+       bool first = true;
+       for (; it != en; ++it) {
+               if (!first)
+                       retval += " ";
+               else 
+                       first = false;
+               retval += *it;
+       }
+       return retval;
+}
+
+// converts a string containing LaTeX commands into unicode
+// for display.
+docstring convertLaTeXCommands(docstring const & str)
+{
+       docstring val = str;
+       docstring ret;
+
+       bool scanning_cmd = false;
+       bool scanning_math = false;
+       bool escaped = false; // used to catch \$, etc.
+       while (val.size()) {
+               char_type const ch = val[0];
+
+               // if we're scanning math, we output everything until we
+               // find an unescaped $, at which point we break out.
+               if (scanning_math) {
+                       if (escaped)
+                               escaped = false;
+                       else if (ch == '\\')
+                               escaped = true;
+                       else if (ch == '$') 
+                               scanning_math = false;
+                       ret += ch;
+                       val = val.substr(1);
+                       continue;
+               }
+
+               // if we're scanning a command name, then we just
+               // discard characters until we hit something that
+               // isn't alpha.
+               if (scanning_cmd) {
+                       if (isAlphaASCII(ch)) {
+                               val = val.substr(1);
+                               escaped = false;
+                               continue;
+                       }
+                       // so we're done with this command.
+                       // now we fall through and check this character.
+                       scanning_cmd = false;
+               }
+
+               // was the last character a \? If so, then this is something like: \\,
+               // or \$, so we'll just output it. That's probably not always right...
+               if (escaped) {
+                       ret += ch;
+                       val = val.substr(1);
+                       escaped = false;
+                       continue;
+               }
+
+               if (ch == '$') {
+                       ret += ch;
+                       val = val.substr(1);
+                       scanning_math = true;
+                       continue;
+               }
+
+               // we just ignore braces
+               if (ch == '{' || ch == '}') {
+                       val = val.substr(1);
+                       continue;
+               }
+
+               // we're going to check things that look like commands, so if
+               // this doesn't, just output it.
+               if (ch != '\\') {
+                       ret += ch;
+                       val = val.substr(1);
+                       continue;
+               }
+
+               // ok, could be a command of some sort
+               // let's see if it corresponds to some unicode
+               // unicodesymbols has things in the form: \"{u},
+               // whereas we may see things like: \"u. So we'll
+               // look for that and change it, if necessary.
+               static boost::regex const reg("^\\\\\\W\\w");
+               if (boost::regex_search(to_utf8(val), reg)) {
+                       val.insert(3, from_ascii("}"));
+                       val.insert(2, from_ascii("{"));
+               }
+               docstring rem;
+               docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem,
+                                                       Encodings::TEXT_CMD);
+               if (!cnvtd.empty()) {
+                       // it did, so we'll take that bit and proceed with what's left
+                       ret += cnvtd;
+                       val = rem;
+                       continue;
+               }
+               // it's a command of some sort
+               scanning_cmd = true;
+               escaped = true;
+               val = val.substr(1);
+       }
+       return ret;
+}
+
+} // anon namespace
+
 
 //////////////////////////////////////////////////////////////////////
 //
@@ -42,77 +195,41 @@ namespace lyx {
 //
 //////////////////////////////////////////////////////////////////////
 
-BibTeXInfo::BibTeXInfo()
-       : isBibTeX(true)
+BibTeXInfo::BibTeXInfo(docstring const & key, docstring const & type)
+       : is_bibtex_(true), bib_key_(key), entry_type_(type), info_()
 {}
 
-       
+
 bool BibTeXInfo::hasField(docstring const & field) const
 {
        return count(field) == 1;
 }
 
 
-docstring const & BibTeXInfo::getValueForField(docstring const & field) const
-{
-       BibTeXInfo::const_iterator it = find(field);
-       if (it != end())
-               return it->second;
-       static docstring const empty_value = docstring();
-       return empty_value;
-}
-       
-       
-docstring const & BibTeXInfo::getValueForField(string const & field) const
-{
-       return getValueForField(from_ascii(field));
-}
-
-
-static docstring familyName(docstring const & name)
-{
-       if (name.empty())
-               return docstring();
-
-       // Very simple parser
-       docstring fname = name;
-
-       // possible authorname combinations are:
-       // "Surname, FirstName"
-       // "Surname, F."
-       // "FirstName Surname"
-       // "F. Surname"
-       docstring::size_type idx = fname.find(',');
-       if (idx != docstring::npos)
-               return ltrim(fname.substr(0, idx));
-       idx = fname.rfind('.');
-       if (idx != docstring::npos && idx + 1 < fname.size())
-               fname = ltrim(fname.substr(idx + 1));
-       // test if we have a LaTeX Space in front
-       if (fname[0] == '\\')
-               return fname.substr(2);
-       return rtrim(fname);
-}
-
-
 docstring const BibTeXInfo::getAbbreviatedAuthor() const
 {
-       if (!isBibTeX) 
-               return docstring();
-       docstring author = getValueForField("author");
+       if (!is_bibtex_) {
+               docstring const opt = label();
+               if (opt.empty())
+                       return docstring();
+
+               docstring authors;
+               split(opt, authors, '(');
+               return authors;
+       }
+
+       docstring author = convertLaTeXCommands(operator[]("author"));
        if (author.empty()) {
-               author = getValueForField("editor");
+               author = convertLaTeXCommands(operator[]("editor"));
                if (author.empty())
-                       return bibKey;
+                       return bib_key_;
        }
 
        // OK, we've got some names. Let's format them.
        // Try to split the author list on " and "
        vector<docstring> const authors =
                getVectorFromString(author, from_ascii(" and "));
-       
+
        if (authors.size() == 2)
                return bformat(_("%1$s and %2$s"),
                        familyName(authors[0]), familyName(authors[1]));
@@ -126,53 +243,70 @@ docstring const BibTeXInfo::getAbbreviatedAuthor() const
 
 docstring const BibTeXInfo::getYear() const
 {
-       if (!isBibTeX) 
+       if (is_bibtex_) 
+               return operator[]("year");
+
+       docstring const opt = label();
+       if (opt.empty())
                return docstring();
-       docstring year = getValueForField("year");
-       if (year.empty())
-               year = _("No year");
+
+       docstring authors;
+       docstring const tmp = split(opt, authors, '(');
+       docstring year;
+       split(tmp, year, ')');
        return year;
 }
 
 
-docstring const BibTeXInfo::getInfo() const
+docstring const BibTeXInfo::getXRef() const
 {
-       if (!isBibTeX) {
+       if (!is_bibtex_)
+               return docstring();
+       return operator[]("crossref");
+}
+
+
+docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref) const
+{
+       if (!info_.empty())
+               return info_;
+
+       if (!is_bibtex_) {
                BibTeXInfo::const_iterator it = find(from_ascii("ref"));
-               return it->second;
+               info_ = it->second;
+               return info_;
        }
  
        // FIXME
-       // This could be made a lot better using the entryType
+       // This could be made a lot better using the entry_type_
        // field to customize the output based upon entry type.
        
        // Search for all possible "required" fields
-       docstring author = getValueForField("author");
+       docstring author = getValueForKey("author", xref);
        if (author.empty())
-               author = getValueForField("editor");
+               author = getValueForKey("editor", xref);
  
-       docstring year      = getValueForField("year");
-       docstring title     = getValueForField("title");
-       docstring docLoc    = getValueForField("pages");
+       docstring year   = getValueForKey("year", xref);
+       docstring title  = getValueForKey("title", xref);
+       docstring docLoc = getValueForKey("pages", xref);
        if (docLoc.empty()) {
-               docLoc = getValueForField("chapter");
+               docLoc = getValueForKey("chapter", xref);
                if (!docLoc.empty())
-                       docLoc = from_ascii("Ch. ") + docLoc;
+                       docLoc = _("Ch. ") + docLoc;
        }       else {
-               docLoc = from_ascii("pp. ") + docLoc;
+               docLoc = _("pp. ") + docLoc;
        }
 
-       docstring media = getValueForField("journal");
+       docstring media = getValueForKey("journal", xref);
        if (media.empty()) {
-               media = getValueForField("publisher");
+               media = getValueForKey("publisher", xref);
                if (media.empty()) {
-                       media = getValueForField("school");
+                       media = getValueForKey("school", xref);
                        if (media.empty())
-                               media = getValueForField("institution");
+                               media = getValueForKey("institution");
                }
        }
-       docstring volume = getValueForField("volume");
+       docstring volume = getValueForKey("volume", xref);
 
        odocstringstream result;
        if (!author.empty())
@@ -182,16 +316,45 @@ docstring const BibTeXInfo::getInfo() const
        if (!media.empty())
                result << ", " << media;
        if (!year.empty())
-               result << ", " << year;
+               result << " (" << year << ")";
        if (!docLoc.empty())
                result << ", " << docLoc;
 
        docstring const result_str = rtrim(result.str());
-       if (!result_str.empty())
-               return result_str;
+       if (!result_str.empty()) {
+               info_ = convertLaTeXCommands(result_str);
+               return info_;
+       }
 
        // This should never happen (or at least be very unusual!)
-       return docstring();
+       static docstring e = docstring();
+       return e;
+}
+
+
+docstring const & BibTeXInfo::operator[](docstring const & field) const
+{
+       BibTeXInfo::const_iterator it = find(field);
+       if (it != end())
+               return it->second;
+       static docstring const empty_value = docstring();
+       return empty_value;
+}
+       
+       
+docstring const & BibTeXInfo::operator[](string const & field) const
+{
+       return operator[](from_ascii(field));
+}
+
+
+docstring BibTeXInfo::getValueForKey(string const & key, 
+               BibTeXInfo const * const xref) const
+{
+       docstring const ret = operator[](key);
+       if (!ret.empty() || !xref)
+               return ret;
+       return (*xref)[key];
 }
 
 
@@ -202,8 +365,8 @@ docstring const BibTeXInfo::getInfo() const
 //////////////////////////////////////////////////////////////////////
 
 namespace {
-// A functor for use with std::sort, leading to case insensitive sorting
-       class compareNoCase: public std::binary_function<docstring, docstring, bool>
+// A functor for use with sort, leading to case insensitive sorting
+       class compareNoCase: public binary_function<docstring, docstring, bool>
        {
                public:
                        bool operator()(docstring const & s1, docstring const & s2) const {
@@ -219,7 +382,7 @@ vector<docstring> const BiblioInfo::getKeys() const
        BiblioInfo::const_iterator it  = begin();
        for (; it != end(); ++it)
                bibkeys.push_back(it->first);
-       std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
+       sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
        return bibkeys;
 }
 
@@ -227,11 +390,11 @@ vector<docstring> const BiblioInfo::getKeys() const
 vector<docstring> const BiblioInfo::getFields() const
 {
        vector<docstring> bibfields;
-       set<docstring>::const_iterator it = fieldNames.begin();
-       set<docstring>::const_iterator end = fieldNames.end();
+       set<docstring>::const_iterator it = field_names_.begin();
+       set<docstring>::const_iterator end = field_names_.end();
        for (; it != end; ++it)
                bibfields.push_back(*it);
-       std::sort(bibfields.begin(), bibfields.end());
+       sort(bibfields.begin(), bibfields.end());
        return bibfields;
 }
 
@@ -239,11 +402,11 @@ vector<docstring> const BiblioInfo::getFields() const
 vector<docstring> const BiblioInfo::getEntries() const
 {
        vector<docstring> bibentries;
-       set<docstring>::const_iterator it = entryTypes.begin();
-       set<docstring>::const_iterator end = entryTypes.end();
+       set<docstring>::const_iterator it = entry_types_.begin();
+       set<docstring>::const_iterator end = entry_types_.end();
        for (; it != end; ++it)
                bibentries.push_back(*it);
-       std::sort(bibentries.begin(), bibentries.end());
+       sort(bibentries.begin(), bibentries.end());
        return bibentries;
 }
 
@@ -264,6 +427,18 @@ docstring const BiblioInfo::getYear(docstring const & key) const
        if (it == end())
                return docstring();
        BibTeXInfo const & data = it->second;
+       docstring year = data.getYear();
+       if (!year.empty())
+               return year;
+       // let's try the crossref
+       docstring const xref = data.getXRef();
+       if (xref.empty())
+               return _("No year"); // no luck
+       BiblioInfo::const_iterator const xrefit = find(xref);
+       if (xrefit == end())
+               return _("No year"); // no luck again
+       BibTeXInfo const & xref_data = xrefit->second;
+       return xref_data.getYear();
        return data.getYear();
 }
 
@@ -274,15 +449,22 @@ docstring const BiblioInfo::getInfo(docstring const & key) const
        if (it == end())
                return docstring();
        BibTeXInfo const & data = it->second;
-       return data.getInfo();
+       BibTeXInfo const * xrefptr = 0;
+       docstring const xref = data.getXRef();
+       if (!xref.empty()) {
+               BiblioInfo::const_iterator const xrefit = find(xref);
+               if (xrefit != end())
+                       xrefptr = &(xrefit->second);
+       }
+       return data.getInfo(xrefptr);
 }
 
 
 vector<docstring> const BiblioInfo::getCiteStrings(
        docstring const & key, Buffer const & buf) const
 {
-       biblio::CiteEngine const engine = buf.params().getEngine();
-       if (engine == biblio::ENGINE_NATBIB_NUMERICAL)
+       CiteEngine const engine = buf.params().citeEngine();
+       if (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL)
                return getNumericalStrings(key, buf);
        else
                return getAuthorYearStrings(key, buf);
@@ -300,40 +482,43 @@ vector<docstring> const BiblioInfo::getNumericalStrings(
        if (author.empty() || year.empty())
                return vector<docstring>();
 
-       vector<biblio::CiteStyle> const & styles = 
-               biblio::getCiteStyles(buf.params().getEngine());
+       vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
        
        vector<docstring> vec(styles.size());
        for (size_t i = 0; i != vec.size(); ++i) {
                docstring str;
 
                switch (styles[i]) {
-                       case biblio::CITE:
-                       case biblio::CITEP:
+                       case CITE:
+                       case CITEP:
                                str = from_ascii("[#ID]");
                                break;
 
-                       case biblio::CITET:
+                       case NOCITE:
+                               str = _("Add to bibliography only.");
+                               break;
+
+                       case CITET:
                                str = author + " [#ID]";
                                break;
 
-                       case biblio::CITEALT:
+                       case CITEALT:
                                str = author + " #ID";
                                break;
 
-                       case biblio::CITEALP:
+                       case CITEALP:
                                str = from_ascii("#ID");
                                break;
 
-                       case biblio::CITEAUTHOR:
+                       case CITEAUTHOR:
                                str = author;
                                break;
 
-                       case biblio::CITEYEAR:
+                       case CITEYEAR:
                                str = year;
                                break;
 
-                       case biblio::CITEYEARPAR:
+                       case CITEYEARPAR:
                                str = '(' + year + ')';
                                break;
                }
@@ -356,45 +541,48 @@ vector<docstring> const BiblioInfo::getAuthorYearStrings(
        if (author.empty() || year.empty())
                return vector<docstring>();
 
-       vector<biblio::CiteStyle> const & styles = 
-               getCiteStyles(buf.params().getEngine());
+       vector<CiteStyle> const & styles = citeStyles(buf.params().citeEngine());
        
        vector<docstring> vec(styles.size());
-       for (vector<docstring>::size_type i = 0; i != vec.size(); ++i) {
+       for (size_t i = 0; i != vec.size(); ++i) {
                docstring str;
 
                switch (styles[i]) {
-                       case biblio::CITE:
+                       case CITE:
                // jurabib only: Author/Annotator
                // (i.e. the "before" field, 2nd opt arg)
                                str = author + "/<" + _("before") + '>';
                                break;
 
-                       case biblio::CITET:
+                       case NOCITE:
+                               str = _("Add to bibliography only.");
+                               break;
+
+                       case CITET:
                                str = author + " (" + year + ')';
                                break;
 
-                       case biblio::CITEP:
+                       case CITEP:
                                str = '(' + author + ", " + year + ')';
                                break;
 
-                       case biblio::CITEALT:
+                       case CITEALT:
                                str = author + ' ' + year ;
                                break;
 
-                       case biblio::CITEALP:
+                       case CITEALP:
                                str = author + ", " + year ;
                                break;
 
-                       case biblio::CITEAUTHOR:
+                       case CITEAUTHOR:
                                str = author;
                                break;
 
-                       case biblio::CITEYEAR:
+                       case CITEYEAR:
                                str = year;
                                break;
 
-                       case biblio::CITEYEARPAR:
+                       case CITEYEARPAR:
                                str = '(' + year + ')';
                                break;
                }
@@ -404,27 +592,12 @@ vector<docstring> const BiblioInfo::getAuthorYearStrings(
 }
 
 
-void BiblioInfo::fillWithBibKeys(Buffer const * const buf)
-{      
-       /// if this is a child document and the parent is already loaded
-       /// use the parent's list instead  [ale990412]
-       Buffer const * const tmp = buf->masterBuffer();
-       BOOST_ASSERT(tmp);
-       if (tmp != buf) {
-               this->fillWithBibKeys(tmp);
-               return;
-       }
-
-       // Pre-load all child documents.
-       buf->loadChildDocuments();
-
-       for (InsetIterator it = inset_iterator_begin(buf->inset()); it; ++it)
-               it->fillWithBibKeys(*buf, *this, it);
+void BiblioInfo::mergeBiblioInfo(BiblioInfo const & info)
+{
+       bimap_.insert(info.begin(), info.end());
 }
 
 
-namespace biblio {
-
 //////////////////////////////////////////////////////////////////////
 //
 // CitationStyle
@@ -435,18 +608,18 @@ namespace {
 
 
 char const * const citeCommands[] = {
-       "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
-       "citeyear", "citeyearpar" };
+       "cite", "citet", "citep", "citealt", "citealp",
+       "citeauthor", "citeyear", "citeyearpar", "nocite" };
 
 unsigned int const nCiteCommands =
                sizeof(citeCommands) / sizeof(char *);
 
-CiteStyle const citeStyles[] = {
-       CITE, CITET, CITEP, CITEALT, CITEALP,
-CITEAUTHOR, CITEYEAR, CITEYEARPAR };
+CiteStyle const citeStylesArray[] = {
+       CITE, CITET, CITEP, CITEALT, CITEALP, 
+       CITEAUTHOR, CITEYEAR, CITEYEARPAR, NOCITE };
 
 unsigned int const nCiteStyles =
-               sizeof(citeStyles) / sizeof(CiteStyle);
+               sizeof(citeStylesArray) / sizeof(CiteStyle);
 
 CiteStyle const citeStylesFull[] = {
        CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
@@ -463,61 +636,61 @@ unsigned int const nCiteStylesUCase =
 } // namespace anon
 
 
-CitationStyle::CitationStyle(string const & command)
-       : style(CITE), full(false), forceUCase(false)
+CitationStyle citationStyleFromString(string const & command)
 {
+       CitationStyle s;
        if (command.empty())
-               return;
+               return s;
 
        string cmd = command;
        if (cmd[0] == 'C') {
-               forceUCase = true;
+               s.forceUpperCase = true;
                cmd[0] = 'c';
        }
 
-       string::size_type const n = cmd.size() - 1;
+       size_t const n = cmd.size() - 1;
        if (cmd != "cite" && cmd[n] == '*') {
-               full = true;
-               cmd = cmd.substr(0,n);
+               s.full = true;
+               cmd = cmd.substr(0, n);
        }
 
        char const * const * const last = citeCommands + nCiteCommands;
-       char const * const * const ptr = std::find(citeCommands, last, cmd);
+       char const * const * const ptr = find(citeCommands, last, cmd);
 
        if (ptr != last) {
                size_t idx = ptr - citeCommands;
-               style = citeStyles[idx];
+               s.style = citeStylesArray[idx];
        }
+       return s;
 }
 
 
-string const CitationStyle::asLatexStr() const
+string citationStyleToString(const CitationStyle & s)
 {
-       string cite = citeCommands[style];
-       if (full) {
+       string cite = citeCommands[s.style];
+       if (s.full) {
                CiteStyle const * last = citeStylesFull + nCiteStylesFull;
-               if (std::find(citeStylesFull, last, style) != last)
+               if (find(citeStylesFull, last, s.style) != last)
                        cite += '*';
        }
 
-       if (forceUCase) {
+       if (s.forceUpperCase) {
                CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
-               if (std::find(citeStylesUCase, last, style) != last)
+               if (find(citeStylesUCase, last, s.style) != last)
                        cite[0] = 'C';
        }
 
        return cite;
 }
 
-
-vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
+vector<CiteStyle> citeStyles(CiteEngine engine)
 {
        unsigned int nStyles = 0;
        unsigned int start = 0;
 
        switch (engine) {
                case ENGINE_BASIC:
-                       nStyles = 1;
+                       nStyles = 2;
                        start = 0;
                        break;
                case ENGINE_NATBIB_AUTHORYEAR:
@@ -531,17 +704,14 @@ vector<CiteStyle> const getCiteStyles(CiteEngine const engine)
                        break;
        }
 
-       typedef vector<CiteStyle> cite_vec;
-
-       cite_vec styles(nStyles);
+       vector<CiteStyle> styles(nStyles);
        size_t i = 0;
        int j = start;
        for (; i != styles.size(); ++i, ++j)
-               styles[i] = citeStyles[j];
+               styles[i] = citeStylesArray[j];
 
        return styles;
 }
 
-} // namespace biblio
 } // namespace lyx