]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetCitation.cpp
XHTML/DocBook: merge code duplicates for vertical alignment.
[features.git] / src / insets / InsetCitation.cpp
index 6672ea1260266bdb2ecd361393ff6daa5212b06d..0deb02472b3163d4ca93677a8147a431948f4fa7 100644 (file)
@@ -37,7 +37,6 @@
 #include "support/FileNameList.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
-#include "support/textutils.h"
 
 #include <algorithm>
 #include <climits>
@@ -51,6 +50,7 @@ InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
        : InsetCommand(buf, p)
 {
        buffer().removeBiblioTempFiles();
+       cleanKeys();
 }
 
 
@@ -168,6 +168,8 @@ void InsetCitation::doDispatch(Cursor & cur, FuncRequest & cmd)
        // fall through
        default:
                InsetCommand::doDispatch(cur, cmd);
+               if (cmd.action() == LFUN_INSET_MODIFY)
+                       cleanKeys();
        }
 }
 
@@ -284,7 +286,7 @@ bool InsetCitation::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 bool InsetCitation::addKey(string const & key)
 {
-       docstring const ukey = from_utf8(key);
+       docstring const ukey = from_utf8(trim(key));
        docstring const & curkeys = getParam("key");
        if (curkeys.empty()) {
                setParam("key", ukey);
@@ -293,10 +295,8 @@ bool InsetCitation::addKey(string const & key)
        }
 
        vector<docstring> keys = getVectorFromString(curkeys);
-       vector<docstring>::const_iterator it = keys.begin();
-       vector<docstring>::const_iterator en = keys.end();
-       for (; it != en; ++it) {
-               if (*it == ukey) {
+       for (auto const & k : keys) {
+               if (k == ukey) {
                        LYXERR0("Key " << key << " already present.");
                        return false;
                }
@@ -603,22 +603,25 @@ int InsetCitation::plaintext(odocstringstream & os,
 
 static docstring const cleanupWhitespace(docstring const & citelist)
 {
-       docstring::const_iterator it  = citelist.begin();
-       docstring::const_iterator end = citelist.end();
        // Paranoia check: make sure that there is no whitespace in here
        // -- at least not behind commas or at the beginning
        docstring result;
        char_type last = ',';
-       for (; it != end; ++it) {
-               if (*it != ' ')
-                       last = *it;
-               if (*it != ' ' || last != ',')
-                       result += *it;
+       for (char_type c : citelist) {
+               if (c != ' ')
+                       last = c;
+               if (c != ' ' || last != ',')
+                       result += c;
        }
        return result;
 }
 
 
+void InsetCitation::cleanKeys() {
+       docstring cleankeys = cleanupWhitespace(getParam("key"));
+       setParam("key", cleankeys);
+}
+
 void InsetCitation::docbook(XMLStream & xs, OutputParams const &) const
 {
        if (getCmdName() == "nocite")
@@ -776,15 +779,11 @@ void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const
                os << "}";
 }
 
+
 pair<int, int> InsetCitation::isWords() const
 {
        docstring const label = generateLabel(false);
-       int words = 1;
-       for (auto const & c : label) {
-               if (lyx::isSpace(c))
-                       words++;
-       }
-       return pair<int, int>(label.size(), words);
+       return pair<int, int>(label.size(), wordCount(label));
 }