]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlMath.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlMath.C
index 11bfdefbf28f0787ea36abe9df936db1096d9f55..3eae5af11da867d0c4916803a4ddaed9d5448cfe 100644 (file)
@@ -5,19 +5,30 @@
  *
  * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
 #include "ControlMath.h"
-#include "Kernel.h"
 #include "debug.h"
 #include "funcrequest.h"
 
+#include "support/lyxalgo.h" // sorted
 #include "support/lstrings.h"
 #include "support/filetools.h"
 
+#include <functional>
+
+using std::string;
+
+namespace lyx {
+
+using support::compare;
+using support::LibFileSearch;
+using support::subst;
+
+namespace frontend {
 
 ControlMath::ControlMath(Dialog & dialog)
        : Dialog::Controller(dialog)
@@ -38,13 +49,15 @@ void ControlMath::dispatchInsert(string const & name) const
 
 void ControlMath::dispatchSubscript() const
 {
-       dispatchFunc(LFUN_SUBSCRIPT);
+       dispatchFunc(LFUN_INSERT_MATH, "_");
+       dispatchFunc(LFUN_DOWN);
 }
 
 
 void ControlMath::dispatchSuperscript() const
 {
-       dispatchFunc(LFUN_SUPERSCRIPT);
+       dispatchFunc(LFUN_INSERT_MATH, "^");
+       dispatchFunc(LFUN_UP);
 }
 
 
@@ -166,7 +179,7 @@ char const * latex_misc[] = {
        "angle", "top", "bot", "Vert", "neg",
        "flat", "natural", "sharp", "surd", "triangle",
        "diamondsuit", "heartsuit", "clubsuit", "spadesuit",
-       "textrm Å", "textrm Ø", "mathcircumflex", "_",
+       "textrm \\AA", "textrm \\O", "mathcircumflex", "_",
        "mathrm T",
        "mathbb N", "mathbb Z", "mathbb Q",
        "mathbb R", "mathbb C", "mathbb H",
@@ -266,50 +279,90 @@ char const * latex_ams_ops[] = {
 int const nr_latex_ams_ops = sizeof(latex_ams_ops) / sizeof(char const *);
 
 
+namespace {
+
+struct XPMmap {
+       char const * key;
+       char const * value;
+};
+
+
+bool operator<(XPMmap const & lhs, XPMmap const & rhs)
+{
+               return compare(lhs.key, rhs.key) < 0;
+}
+
+
+class CompareKey : public std::unary_function<XPMmap, bool> {
+public:
+       CompareKey(string const & name) : name_(name) {}
+       bool operator()(XPMmap const & other) const {
+               return other.key == name_;
+       }
+private:
+       string const name_;
+};
+
+
+XPMmap sorted_xpm_map[] = {
+       { "Bumpeq", "bumpeq2" },
+       { "Cap", "cap2" },
+       { "Cup", "cup2" },
+       { "Delta", "delta2" },
+       { "Downarrow", "downarrow2" },
+       { "Gamma", "gamma2" },
+       { "Lambda", "lambda2" },
+       { "Leftarrow", "leftarrow2" },
+       { "Leftrightarrow", "leftrightarrow2" },
+       { "Longleftarrow", "longleftarrow2" },
+       { "Longleftrightarrow", "longleftrightarrow2" },
+       { "Longrightarrow", "longrightarrow2" },
+       { "Omega", "omega2" },
+       { "Phi", "phi2" },
+       { "Pi", "pi2" },
+       { "Psi", "psi2" },
+       { "Rightarrow", "rightarrow2" },
+       { "Sigma", "sigma2" },
+       { "Subset", "subset2" },
+       { "Supset", "supset2" },
+       { "Theta", "theta2" },
+       { "Uparrow", "uparrow2" },
+       { "Updownarrow", "updownarrow2" },
+       { "Upsilon", "upsilon2" },
+       { "Vdash", "vdash3" },
+       { "Xi", "xi2" },
+       { "nLeftarrow", "nleftarrow2" },
+       { "nLeftrightarrow", "nleftrightarrow2" },
+       { "nRightarrow", "nrightarrow2" },
+       { "nVDash", "nvdash3" },
+       { "nvDash", "nvdash2" },
+       { "textrm \\AA", "textrm_AA"},
+       { "textrm \\O", "textrm_Oe"},
+       { "vDash", "vdash2" }
+};
+
+size_t const nr_sorted_xpm_map = sizeof(sorted_xpm_map) / sizeof(XPMmap);
+
+} // namespace anon
+
+
 string const find_xpm(string const & name)
 {
-       string xpm_name = subst(name, "_", "underscore");
-       xpm_name = subst(xpm_name, ' ', '_');
-
-#warning Use a static table for this (Lgb)
-       // And get O(log n) lookup (Lgb)
-
-       if (xpm_name == "textrm_Å") xpm_name = "textrm_A";
-       else if (xpm_name == "textrm_Ø") xpm_name = "textrm_0";
-       else if (xpm_name == "Bumpeq") xpm_name = "bumpeq2";
-       else if (xpm_name == "Cap") xpm_name = "cap2";
-       else if (xpm_name == "Cup") xpm_name = "cup2";
-       else if (xpm_name == "Delta") xpm_name = "delta2";
-       else if (xpm_name == "Downarrow") xpm_name = "downarrow2";
-       else if (xpm_name == "Gamma") xpm_name = "gamma2";
-       else if (xpm_name == "Lambda") xpm_name = "lambda2";
-       else if (xpm_name == "Leftarrow") xpm_name = "leftarrow2";
-       else if (xpm_name == "Leftrightarrow") xpm_name = "leftrightarrow2";
-       else if (xpm_name == "Longleftarrow") xpm_name = "longleftarrow2";
-       else if (xpm_name == "Longleftrightarrow") xpm_name = "longleftrightarrow2";
-       else if (xpm_name == "Longrightarrow") xpm_name = "longrightarrow2";
-       else if (xpm_name == "nLeftarrow") xpm_name = "nleftarrow2";
-       else if (xpm_name == "nLeftrightarrow") xpm_name = "nleftrightarrow2";
-       else if (xpm_name == "nRightarrow") xpm_name = "nrightarrow2";
-       else if (xpm_name == "nvDash") xpm_name = "nvdash2";
-       else if (xpm_name == "nVDash") xpm_name = "nvdash3";
-       else if (xpm_name == "Omega") xpm_name = "omega2";
-       else if (xpm_name == "Phi") xpm_name = "phi2";
-       else if (xpm_name == "Pi") xpm_name = "pi2";
-       else if (xpm_name == "Psi") xpm_name = "psi2";
-       else if (xpm_name == "Rightarrow") xpm_name = "rightarrow2";
-       else if (xpm_name == "Sigma") xpm_name = "sigma2";
-       else if (xpm_name == "Subset") xpm_name = "subset2";
-       else if (xpm_name == "Supset") xpm_name = "supset2";
-       else if (xpm_name == "Theta") xpm_name = "theta2";
-       else if (xpm_name == "Uparrow") xpm_name = "uparrow2";
-       else if (xpm_name == "Updownarrow") xpm_name = "updownarrow2";
-       else if (xpm_name == "Upsilon") xpm_name = "upsilon2";
-       else if (xpm_name == "vDash") xpm_name = "vdash2";
-       else if (xpm_name == "Vdash") xpm_name = "vdash3";
-       else if (xpm_name == "Xi") xpm_name = "xi2";
+       XPMmap const * const begin = sorted_xpm_map;
+       XPMmap const * const end = begin + nr_sorted_xpm_map;
+       BOOST_ASSERT(sorted(begin, end));
+
+       XPMmap const * const it =
+               std::find_if(begin, end, CompareKey(name));
+
+       string xpm_name;
+       if (it != end)
+               xpm_name = it->value;
        else {
-               // slightly different so we can have "math-delim { }" on toolbar
+               xpm_name = subst(name, "_", "underscore");
+               xpm_name = subst(xpm_name, ' ', '_');
+
+               // This way we can have "math-delim { }" on the toolbar.
                xpm_name = subst(xpm_name, "(", "lparen");
                xpm_name = subst(xpm_name, ")", "rparen");
                xpm_name = subst(xpm_name, "[", "lbracket");
@@ -319,8 +372,12 @@ string const find_xpm(string const & name)
                xpm_name = subst(xpm_name, "|", "bars");
        }
 
-       lyxerr[Debug::GUI] << "Looking for math XPM called \""
-               << xpm_name << '"' << std::endl;
+       lyxerr[Debug::GUI] << "find_xpm(" << name << ")\n"
+                          << "Looking for math XPM called \""
+                          << xpm_name << '"' << std::endl;
 
        return LibFileSearch("images/math/", xpm_name, "xpm");
 }
+
+} // namespace frontend
+} // namespace lyx