]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcite.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / insets / insetcite.C
index 998338787bb4c355920b9879e04afe612995e5ac..d0cf1c7f48fcd67b11648f54060fde98466353a9 100644 (file)
@@ -6,35 +6,25 @@
  * \author Angus Leeming
  * \author Herbert Voss
  *
- * 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 "insetcite.h"
+#include "funcrequest.h"
 #include "buffer.h"
 #include "BufferView.h"
 #include "LaTeXFeatures.h"
-#include "frontends/LyXView.h"
-#include "debug.h"
-#include "gettext.h"
 
 #include "frontends/controllers/biblio.h"
-#include "frontends/Dialogs.h"
 
-#include "support/filetools.h"
-#include "support/lstrings.h"
-#include "support/path.h"
-#include "support/os.h"
 #include "support/lstrings.h"
-#include "support/LAssert.h"
 
 #include <map>
 
+using namespace lyx::support;
+
 using std::ostream;
 using std::vector;
 using std::map;
@@ -46,25 +36,31 @@ namespace {
 // 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;
 
-       if (!loading_buffer[buffer] || kit == cached_keys.end()) {
+       bool loadkeys = !loading_buffer[&buffer];
+       if (!loadkeys) {
+               CachedMap::iterator kit = cached_keys.find(&buffer);
+               loadkeys = kit == cached_keys.end();
+       }
+
+       if (loadkeys) {
                // build the keylist
                typedef vector<std::pair<string, string> > InfoType;
-               InfoType bibkeys = buffer->getBibkeyList();
+               InfoType bibkeys;
+               buffer.fillWithBibKeys(bibkeys);
 
                InfoType::const_iterator bit  = bibkeys.begin();
                InfoType::const_iterator bend = bibkeys.end();
@@ -74,12 +70,12 @@ string const getNatbibLabel(Buffer const * buffer,
                        infomap[bit->first] = bit->second;
                }
                if (infomap.empty())
-               return string();
+                       return string();
 
-               cached_keys[buffer] = infomap;
+               cached_keys[&buffer] = infomap;
        }
 
-       biblio::InfoMap infomap = cached_keys[buffer];
+       biblio::InfoMap infomap = cached_keys[&buffer];
 
        // the natbib citation-styles
        // CITET:       author (year)
@@ -125,9 +121,9 @@ string const getNatbibLabel(Buffer const * buffer,
        // 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(sep) + " ");
+       string const op_str(' ' + string(1, op));
+       string const cp_str(string(1, cp) + ' ');
+       string const sep_str(string(sep) + ' ');
 
        string label;
        vector<string> keys = getVectorFromString(keyList);
@@ -209,7 +205,7 @@ string const getBasicLabel(string const & keyList, string const & after)
 
        if (contains(keys, ",")) {
                // Final comma allows while loop to cover all keys
-               keys = ltrim(split(keys, label, ',')) + ",";
+               keys = ltrim(split(keys, label, ',')) + ',';
                while (contains(keys, ",")) {
                        string key;
                        keys = ltrim(split(keys, key, ','));
@@ -221,24 +217,36 @@ string const getBasicLabel(string const & keyList, string const & after)
        if (!after.empty())
                label += ", " + after;
 
-       return "[" + label + "]";
+       return '[' + label + ']';
 }
 
 } // anon namespace
 
 
-InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
+InsetCitation::InsetCitation(InsetCommandParams const & p)
        : InsetCommand(p)
 {}
 
 
-string const InsetCitation::generateLabel(Buffer const * buffer) const
+// InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
+//     : InsetCommand(p, false)
+// {}
+
+
+InsetCitation::~InsetCitation()
+{
+       InsetCommandMailer mailer("citation", *this);
+       mailer.hideDialog();
+}
+
+
+string const InsetCitation::generateLabel(Buffer const & buffer) const
 {
        string const before = string();
        string const after  = getOptions();
 
        string label;
-       if (buffer->params.use_natbib) {
+       if (buffer.params.use_natbib) {
                string cmd = getCmdName();
                if (cmd == "cite") {
                        // We may be "upgrading" from an older LyX version.
@@ -246,14 +254,14 @@ string const InsetCitation::generateLabel(Buffer const * buffer) const
                        // author/year info is not present in the biblio
                        // database, then getNatbibLabel will exit gracefully
                        // and we'll call getBasicLabel.
-                       if (buffer->params.use_numerical_citations)
+                       if (buffer.params.use_numerical_citations)
                                cmd = "citep";
                        else
                                cmd = "citet";
                }
                label = getNatbibLabel(buffer, cmd, getContents(),
                                       before, after,
-                                      buffer->params.use_numerical_citations);
+                                      buffer.params.use_numerical_citations);
        }
 
        // Fallback to fail-safe
@@ -265,12 +273,12 @@ string const InsetCitation::generateLabel(Buffer const * buffer) const
 }
 
 
-InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
+InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const & buffer) const
 {
        Cache::Style style = Cache::BASIC;
 
-       if (buffer->params.use_natbib) {
-               if (buffer->params.use_numerical_citations) {
+       if (buffer.params.use_natbib) {
+               if (buffer.params.use_numerical_citations) {
                        style = Cache::NATBIB_NUM;
                } else {
                        style = Cache::NATBIB_AY;
@@ -281,7 +289,7 @@ InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
 }
 
 
-string const InsetCitation::getScreenLabel(Buffer const * buffer) const
+string const InsetCitation::getScreenLabel(Buffer const & buffer) const
 {
        Cache::Style const style = getStyle(buffer);
        if (cache.params == params() && cache.style == style)
@@ -310,33 +318,35 @@ string const InsetCitation::getScreenLabel(Buffer const * buffer) const
 }
 
 
-void InsetCitation::edit(BufferView * bv, int, int, mouse_button::state)
+void InsetCitation::setLoadingBuffer(Buffer const & buffer, bool state) const
 {
-       // 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;
-
-       bv->owner()->getDialogs().showCitation(this);
+       loading_buffer[&buffer] = state;
 }
 
 
-void InsetCitation::edit(BufferView * bv, bool)
+dispatch_result InsetCitation::localDispatch(FuncRequest const & cmd)
 {
-       edit(bv, 0, 0, mouse_button::none);
+       switch (cmd.action) {
+       case LFUN_INSET_EDIT:
+               // A call to edit indicates that we're no longer loading the
+               // buffer but doing some real work.
+               setLoadingBuffer(*cmd.view()->buffer(), false);
+               InsetCommandMailer("citation", *this).showDialog(cmd.view());
+               return DISPATCHED;
+
+       default:
+               return InsetCommand::localDispatch(cmd);
+       }
 }
 
 
-int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
+int InsetCitation::ascii(Buffer const & buffer, ostream & os, int) const
 {
-       string label;
-
        if (cache.params == params() && cache.style == getStyle(buffer))
-               label = cache.generated_label;
+               os << cache.generated_label;
        else
-               label = generateLabel(buffer);
-
-       os << label;
+               os << generateLabel(buffer);
        return 0;
 }
 
@@ -345,11 +355,11 @@ int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
 // citations and then changes his mind, turning natbib support off. The output
 // should revert to \cite[]{}
-int InsetCitation::latex(Buffer const * buffer, ostream & os,
-                       bool /*fragile*/, bool/*fs*/) const
+int InsetCitation::latex(Buffer const & buffer, ostream & os,
+                        LatexRunParams const &) const
 {
        os << "\\";
-       if (buffer->params.use_natbib)
+       if (buffer.params.use_natbib)
                os << getCmdName();
        else
                os << "cite";
@@ -363,15 +373,15 @@ int InsetCitation::latex(Buffer const * buffer, ostream & os,
 
        string const before = string();
        string const after  = getOptions();
-       if (!before.empty() && buffer->params.use_natbib)
-               os << "[" << before << "][" << after << "]";
+       if (!before.empty() && buffer.params.use_natbib)
+               os << '[' << before << "][" << after << ']';
        else if (!after.empty())
-               os << "[" << after << "]";
+               os << '[' << after << ']';
 #else
        // and the cleaned up equvalent, should it just be changed? (Lgb)
        string const after  = getOptions();
        if (!after.empty())
-               os << "[" << after << "]";
+               os << '[' << after << ']';
 #endif
        string::const_iterator it  = getContents().begin();
        string::const_iterator end = getContents().end();
@@ -385,7 +395,7 @@ int InsetCitation::latex(Buffer const * buffer, ostream & os,
                        content += *it;
        }
 
-       os << "{" << content << "}";
+       os << '{' << content << '}';
 
        return 0;
 }