]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetCitation.cpp
Fix text frame drawing.
[lyx.git] / src / insets / InsetCitation.cpp
index e1a727c4c4c2aa0835bd24e1a4c56f91f0082ef8..1cd5c8e4b0620831b55c690eb9fbb69bbdbe3185 100644 (file)
 
 namespace lyx {
 
-using support::ascii_lowercase;
-using support::contains;
 using support::FileName;
 using support::getStringFromVector;
 using support::getVectorFromString;
 using support::ltrim;
+using support::prefixIs;
 using support::rtrim;
 using support::split;
 using support::tokenPos;
 
 using std::endl;
-using std::replace;
 using std::string;
-using std::ostream;
 using std::vector;
-using std::map;
 
 namespace fs = boost::filesystem;
 
 
 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(biblio::CiteEngine engine)
+{
+       string str;
+       switch (engine) {
+               case biblio::ENGINE_BASIC:
+                       str = "cite";
+                       break;
+               case biblio::ENGINE_NATBIB_AUTHORYEAR:
+                       str = "citet";
+                       break;
+               case biblio::ENGINE_NATBIB_NUMERICAL:
+                       str = "citep";
+                       break;
+               case biblio::ENGINE_JURABIB:
+                       str = "cite";
+                       break;
+       }
+       return str;
+}
+
+               
+string const 
+               asValidLatexCommand(string const & input, biblio::CiteEngine const engine)
+{
+       string const default_str = default_cite_command(engine);
+       if (!is_possible_cite_command(input))
+               return default_str;
+
+       string output;
+       switch (engine) {
+               case biblio::ENGINE_BASIC:
+                       output = default_str;
+                       break;
+
+               case biblio::ENGINE_NATBIB_AUTHORYEAR:
+               case biblio::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 biblio::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;
+}
+
+
 docstring const getNatbibLabel(Buffer const & buffer,
-                           string const & citeType, string const & keyList,
+                           string const & citeType, docstring const & keyList,
                            docstring const & before, docstring const & after,
                            biblio::CiteEngine engine)
 {
@@ -65,13 +160,13 @@ docstring const getNatbibLabel(Buffer const & buffer,
                return docstring();
 
        // Cache the labels
-       typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
+       typedef std::map<Buffer const *, BiblioInfo> CachedMap;
        static CachedMap cached_keys;
 
        // and cache the timestamp of the bibliography files.
        static std::map<FileName, time_t> bibfileStatus;
 
-       biblio::InfoMap infomap;
+       BiblioInfo biblist;
 
        vector<FileName> const & bibfilesCache = buffer.getBibfilesCache();
        // compare the cached timestamps with the actual ones.
@@ -95,24 +190,15 @@ docstring const getNatbibLabel(Buffer const & buffer,
                }
        }
 
-       // build the keylist only if the bibfiles have been changed
+       // build the list only if the bibfiles have been changed
        if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
-               typedef vector<std::pair<string, docstring> > InfoType;
-               InfoType bibkeys;
-               buffer.fillWithBibKeys(bibkeys);
-
-               InfoType::const_iterator bit  = bibkeys.begin();
-               InfoType::const_iterator bend = bibkeys.end();
-
-               for (; bit != bend; ++bit)
-                       infomap[bit->first] = bit->second;
-
-               cached_keys[&buffer] = infomap;
+               biblist.fillWithBibKeys(&buffer);
+               cached_keys[&buffer] = biblist;
        } else
                // use the cached keys
-               infomap = cached_keys[&buffer];
+               biblist = cached_keys[&buffer];
 
-       if (infomap.empty())
+       if (biblist.empty())
                return docstring();
 
        // the natbib citation-styles
@@ -127,10 +213,12 @@ docstring const getNatbibLabel(Buffer const & buffer,
        // CITE:        author/<before field>
 
        // We don't currently use the full or forceUCase fields.
-       string cite_type = biblio::asValidLatexCommand(citeType, engine);
+       string cite_type = asValidLatexCommand(citeType, engine);
        if (cite_type[0] == 'C')
+               //If we were going to use them, this would mean ForceUCase
                cite_type = string(1, 'c') + cite_type.substr(1);
        if (cite_type[cite_type.size() - 1] == '*')
+               //and this would mean FULL
                cite_type = cite_type.substr(0, cite_type.size() - 1);
 
        docstring before_str;
@@ -170,13 +258,13 @@ docstring const getNatbibLabel(Buffer const & buffer,
        docstring const sep_str(from_ascii(sep) + ' ');
 
        docstring label;
-       vector<string> keys = getVectorFromString(keyList);
-       vector<string>::const_iterator it  = keys.begin();
-       vector<string>::const_iterator end = keys.end();
+       vector<docstring> keys = getVectorFromString(keyList);
+       vector<docstring>::const_iterator it  = keys.begin();
+       vector<docstring>::const_iterator end = keys.end();
        for (; it != end; ++it) {
                // get the bibdata corresponding to the key
-               docstring const author(biblio::getAbbreviatedAuthor(infomap, *it));
-               docstring const year(biblio::getYear(infomap, *it));
+               docstring const author(biblist.getAbbreviatedAuthor(*it));
+               docstring const year(biblist.getYear(*it));
 
                // Something isn't right. Fail safely.
                if (author.empty() || year.empty())
@@ -199,9 +287,7 @@ docstring const getNatbibLabel(Buffer const & buffer,
                                        year + cp + sep_str;
                                break;
                        case biblio::ENGINE_NATBIB_NUMERICAL:
-                               // FIXME UNICODE
-                               label += author + op_str + before_str +
-                                       '#' + from_utf8(*it) + cp + sep_str;
+                               label += author + op_str + before_str + '#' + *it + cp + sep_str;
                                break;
                        case biblio::ENGINE_JURABIB:
                                label += before_str + author + op_str +
@@ -215,8 +301,7 @@ docstring const getNatbibLabel(Buffer const & buffer,
                } else if (cite_type == "citep" ||
                           cite_type == "citealp") {
                        if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
-                               // FIXME UNICODE
-                               label += from_utf8(*it) + sep_str;
+                               label += *it + sep_str;
                        } else {
                                label += author + ", " + year + sep_str;
                        }
@@ -230,9 +315,7 @@ docstring const getNatbibLabel(Buffer const & buffer,
                                        year + sep_str;
                                break;
                        case biblio::ENGINE_NATBIB_NUMERICAL:
-                               // FIXME UNICODE
-                               label += author + ' ' + before_str +
-                                       '#' + from_utf8(*it) + sep_str;
+                               label += author + ' ' + before_str + '#' + *it + sep_str;
                                break;
                        case biblio::ENGINE_JURABIB:
                                label += before_str + author + ' ' +
@@ -283,6 +366,8 @@ docstring const getNatbibLabel(Buffer const & buffer,
 
 docstring const getBasicLabel(docstring const & keyList, docstring const & after)
 {
+       using support::contains;
+
        docstring keys(keyList);
        docstring label;
 
@@ -319,8 +404,7 @@ docstring const InsetCitation::generateLabel(Buffer const & buffer) const
        docstring label;
        biblio::CiteEngine const engine = buffer.params().getEngine();
        if (engine != biblio::ENGINE_BASIC) {
-               // FIXME UNICODE
-               label = getNatbibLabel(buffer, getCmdName(), to_utf8(getParam("key")),
+               label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
                                       before, after, engine);
        }
 
@@ -360,7 +444,7 @@ docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const
 
 
 int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
-                             OutputParams const &) const
+                            OutputParams const &) const
 {
        docstring str;
 
@@ -398,11 +482,11 @@ docstring const cleanupWhitespace(docstring const & citelist)
 }
 
 int InsetCitation::docbook(Buffer const &, odocstream & os,
-                           OutputParams const &) const
+                          OutputParams const &) const
 {
        os << "<citation>"
-           << cleanupWhitespace(getParam("key"))
-           << "</citation>";
+          << cleanupWhitespace(getParam("key"))
+          << "</citation>";
        return 0;
 }
 
@@ -419,12 +503,12 @@ int InsetCitation::textString(Buffer const & buf, odocstream & os,
 // citations and then changes his mind, turning natbib support off. The output
 // should revert to \cite[]{}
 int InsetCitation::latex(Buffer const & buffer, odocstream & os,
-                         OutputParams const &) const
+                        OutputParams const &) const
 {
        biblio::CiteEngine cite_engine = buffer.params().getEngine();
        // FIXME UNICODE
        docstring const cite_str = from_utf8(
-               biblio::asValidLatexCommand(getCmdName(), cite_engine));
+               asValidLatexCommand(getCmdName(), cite_engine));
 
        os << "\\" << cite_str;
 
@@ -461,7 +545,7 @@ void InsetCitation::replaceContents(string const & from, string const & to)
 {
        if (tokenPos(getContents(), ',', from) != -1) {
                vector<string> items = getVectorFromString(getContents());
-               replace(items.begin(), items.end(), from, to);
+               std::replace(items.begin(), items.end(), from, to);
                setContents(getStringFromVector(items));
        }
 }