]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcite.C
Make it compile when USE_BOOST_FORMAT is unset
[lyx.git] / src / insets / insetcite.C
index edcfeb1e7d50689e4b3ab287ea782d487b337db4..1be81b58d24850d173602d5823fb9d7de621212d 100644 (file)
@@ -1,10 +1,12 @@
 /**
  * \file insetcite.C
- * Copyright 2001 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Angus Leeming, a.leeming@ic.ac.uk
- * \author Herbert Voss, voss@lyx.org 2002-03-17
+ * \author Angus Leeming
+ * \author Herbert Voss
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
@@ -17,7 +19,7 @@
 #include "buffer.h"
 #include "BufferView.h"
 #include "LaTeXFeatures.h"
-#include "LyXView.h"
+#include "frontends/LyXView.h"
 #include "debug.h"
 #include "gettext.h"
 
@@ -40,43 +42,48 @@ using std::map;
 namespace {
 
 // An optimisation. We assume that until the first InsetCitation::edit is
-// called, we're loding the buffer and that, therefore, we don't need to
+// called, we're loading the buffer and that, therefore, we don't need to
 // reload the bibkey list
 std::map<Buffer const *, bool> loading_buffer;
 
-string const getNatbibLabel(Buffer const * buffer, 
+string const getNatbibLabel(Buffer const * buffer,
                            string const & citeType, string const & keyList,
                            string const & before, string const & after,
                            bool numerical)
 {
-       // Only reload the bibkeys if we have to...
-       map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
-       if (lit != loading_buffer.end())
-               loading_buffer[buffer] = true;
-
        typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
        static CachedMap cached_keys;
 
-       CachedMap::iterator kit = cached_keys.find(buffer);
+       // Only load the bibkeys once if we're loading up the buffer,
+       // else load them afresh each time.
+       map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
+       if (lit == loading_buffer.end())
+               loading_buffer[buffer] = true;
+
+       bool loadkeys = !loading_buffer[buffer];
+       if (!loadkeys) {
+               CachedMap::iterator kit = cached_keys.find(buffer);
+               loadkeys = kit == cached_keys.end();
+       }
 
-       if (!loading_buffer[buffer] || kit == cached_keys.end()) {
+       if (loadkeys) {
                // build the keylist
                typedef vector<std::pair<string, string> > InfoType;
                InfoType bibkeys = buffer->getBibkeyList();
 
                InfoType::const_iterator bit  = bibkeys.begin();
                InfoType::const_iterator bend = bibkeys.end();
-       
+
                biblio::InfoMap infomap;
                for (; bit != bend; ++bit) {
                        infomap[bit->first] = bit->second;
                }
                if (infomap.empty())
-               return string();
+                       return string();
 
                cached_keys[buffer] = infomap;
        }
-       
+
        biblio::InfoMap infomap = cached_keys[buffer];
 
        // the natbib citation-styles
@@ -90,12 +97,12 @@ string const getNatbibLabel(Buffer const * buffer,
 
        // We don't currently use the full or forceUCase fields.
        // bool const forceUCase = citeType[0] == 'C';
-       bool const full = citeType[citeType.size()-1] == '*';
+       bool const full = citeType[citeType.size() - 1] == '*';
 
        string const cite_type = full ?
-               lowercase(citeType.substr(0,citeType.size()-1)) :
-               lowercase(citeType);
-       
+               ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
+               ascii_lowercase(citeType);
+
        string before_str;
        if (!before.empty()) {
                // In CITET and CITEALT mode, the "before" string is
@@ -106,7 +113,7 @@ string const getNatbibLabel(Buffer const * buffer,
                if (cite_type == "citet" ||
                    cite_type == "citealt" ||
                    cite_type == "citep" ||
-                   cite_type == "citealp" || 
+                   cite_type == "citealp" ||
                    cite_type == "citeyearpar")
                        before_str = before + ' ';
        }
@@ -120,17 +127,18 @@ string const getNatbibLabel(Buffer const * buffer,
        // One day, these might be tunable (as they are in BibTeX).
        char const op  = '('; // opening parenthesis.
        char const cp  = ')'; // closing parenthesis.
-       char const sep = ';'; // puctuation mark separating citation entries.
+       // puctuation mark separating citation entries.
+       char const * const sep = ";";
 
        string const op_str(string(1, ' ')  + string(1, op));
        string const cp_str(string(1, cp)   + string(1, ' '));
-       string const sep_str(string(1, sep) + string(1, ' '));
+       string const sep_str(string(sep) + " ");
 
        string label;
        vector<string> keys = getVectorFromString(keyList);
        vector<string>::const_iterator it  = keys.begin();
        vector<string>::const_iterator end = keys.end();
-       for (; it != end; ++it) {
+       for (; it != end; ++it) {
                // get the bibdata corresponding to the key
                string const author(biblio::getAbbreviatedAuthor(infomap, *it));
                string const year(biblio::getYear(infomap, *it));
@@ -171,12 +179,12 @@ string const getNatbibLabel(Buffer const * buffer,
                        label += year + sep_str;
                }
        }
-       label = strip(strip(label), sep);
+       label = rtrim(rtrim(label), sep);
 
        if (!after_str.empty()) {
                if (cite_type == "citet") {
                        // insert "after" before last ')'
-                       label.insert(label.size()-1, after_str);
+                       label.insert(label.size() - 1, after_str);
                } else {
                        bool const add = !(numerical &&
                                           (cite_type == "citeauthor" ||
@@ -187,7 +195,7 @@ string const getNatbibLabel(Buffer const * buffer,
        }
 
        if (!before_str.empty() && (cite_type == "citep" ||
-                                   cite_type == "citealp" || 
+                                   cite_type == "citealp" ||
                                    cite_type == "citeyearpar")) {
                label = before_str + label;
        }
@@ -206,10 +214,10 @@ string const getBasicLabel(string const & keyList, string const & after)
 
        if (contains(keys, ",")) {
                // Final comma allows while loop to cover all keys
-               keys = frontStrip(split(keys, label, ',')) + ",";
+               keys = ltrim(split(keys, label, ',')) + ",";
                while (contains(keys, ",")) {
                        string key;
-                       keys = frontStrip(split(keys, key, ','));
+                       keys = ltrim(split(keys, key, ','));
                        label += ", " + key;
                }
        } else
@@ -217,7 +225,7 @@ string const getBasicLabel(string const & keyList, string const & after)
 
        if (!after.empty())
                label += ", " + after;
-       
+
        return "[" + label + "]";
 }
 
@@ -248,7 +256,7 @@ string const InsetCitation::generateLabel(Buffer const * buffer) const
                        else
                                cmd = "citet";
                }
-               label = getNatbibLabel(buffer, cmd, getContents(), 
+               label = getNatbibLabel(buffer, cmd, getContents(),
                                       before, after,
                                       buffer->params.use_numerical_citations);
        }
@@ -307,20 +315,26 @@ string const InsetCitation::getScreenLabel(Buffer const * buffer) const
 }
 
 
-void InsetCitation::edit(BufferView * bv, int, int, unsigned int)
+void InsetCitation::setLoadingBuffer(Buffer const * buffer, bool state) const
+{
+       // Doesn't matter if there is no bv->buffer() entry in the map.
+       loading_buffer[buffer] = state;
+}
+
+
+void InsetCitation::edit(BufferView * bv, int, int, mouse_button::state)
 {
        // A call to edit() indicates that we're no longer loading the
        // buffer but doing some real work.
-       // Doesn't matter if there is no bv->buffer() entry in the map.
-       loading_buffer[bv->buffer()] = false;
+       setLoadingBuffer(bv->buffer(), false);
 
-       bv->owner()->getDialogs()->showCitation(this);
+       bv->owner()->getDialogs().showCitation(this);
 }
 
 
 void InsetCitation::edit(BufferView * bv, bool)
 {
-       edit(bv, 0, 0, 0);
+       edit(bv, 0, 0, mouse_button::none);
 }
 
 
@@ -350,20 +364,36 @@ int InsetCitation::latex(Buffer const * buffer, ostream & os,
                os << getCmdName();
        else
                os << "cite";
-       
+
+#warning What is this code supposed to do? (Lgb)
+// my guess is that this is just waiting for when we support before,
+// so it's a oneliner. But this is very silly ! - jbl
+
+#if 1
+       // The current strange code
+
        string const before = string();
        string const after  = getOptions();
        if (!before.empty() && buffer->params.use_natbib)
                os << "[" << before << "][" << after << "]";
        else if (!after.empty())
                os << "[" << after << "]";
-
+#else
+       // and the cleaned up equvalent, should it just be changed? (Lgb)
+       string const after  = getOptions();
+       if (!after.empty())
+               os << "[" << after << "]";
+#endif
        string::const_iterator it  = getContents().begin();
        string::const_iterator end = getContents().end();
        // Paranoia check: make sure that there is no whitespace in here
        string content;
+       char last = ',';
        for (; it != end; ++it) {
-               if (*it != ' ') content += *it;
+               if (*it != ' ')
+                       last = *it;
+               if (*it != ' ' || last != ',')
+                       content += *it;
        }
 
        os << "{" << content << "}";