]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
A little cleanup of the layout files.
[lyx.git] / src / mathed / InsetMathHull.cpp
index cc4672587d2d903dd3b337c32fc02c955eb144fa..b773b088c637a25884c938f94f35685ebe8ebc78 100644 (file)
@@ -3,7 +3,7 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author André Pönitz
+ * \author André Pönitz
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -46,6 +46,7 @@
 
 #include "insets/RenderPreview.h"
 #include "insets/InsetLabel.h"
+#include "insets/InsetRef.h"
 
 #include "graphics/PreviewImage.h"
 #include "graphics/PreviewLoader.h"
@@ -118,7 +119,8 @@ HullType hullType(docstring const & s)
        if (s == "multline")  return hullMultline;
        if (s == "gather")    return hullGather;
        if (s == "flalign")   return hullFlAlign;
-       lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
+       if (s == "regexp")    return hullRegexp;
+       lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
        return HullType(-1);
 }
 
@@ -137,7 +139,8 @@ docstring hullName(HullType type)
                case hullMultline:   return from_ascii("multline");
                case hullGather:     return from_ascii("gather");
                case hullFlAlign:    return from_ascii("flalign");
-               default:
+               case hullRegexp:     return from_ascii("regexp");
+               default:
                        lyxerr << "unknown hull type '" << type << "'" << endl;
                        return from_ascii("none");
        }
@@ -447,15 +450,7 @@ void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
 static docstring latexString(InsetMathHull const & inset)
 {
        odocstringstream ls;
-       // This has to be static, because a preview snippet containing math
-       // in text mode (such as $\text{$\phi$}$) gets processed twice. The
-       // first time as a whole, and the second time only the inner math.
-       // In this last case inset.buffer() would be invalid.
-       // FIXME: preview snippets should only be processed once, such that
-       // both static qualifier and isBufferValid() check can be dropped.
-       static Encoding const * encoding = 0;
-       if (inset.isBufferValid())
-               encoding = &(inset.buffer().params().encoding());
+       Encoding const * encoding = &(inset.buffer().params().encoding());
        WriteStream wi(ls, false, true, false, encoding);
        inset.write(wi);
        return ls.str();
@@ -481,10 +476,10 @@ void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
 bool InsetMathHull::notifyCursorLeaves(Cursor const & /*old*/, Cursor & cur)
 {
        if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
-               Buffer const & buffer = cur.buffer();
+               Buffer const * buffer = cur.buffer();
                docstring const snippet = latexString(*this);
-               preview_->addPreview(snippet, buffer);
-               preview_->startLoading(buffer);
+               preview_->addPreview(snippet, *buffer);
+               preview_->startLoading(*buffer);
                cur.updateFlags(Update::Force);
        }
        return false;
@@ -509,9 +504,10 @@ void InsetMathHull::label(row_type row, docstring const & label)
                        label_[row] = dummy_pointer;
                        // We need an update of the Buffer reference cache.
                        // This is achieved by updateLabels().
-                       lyx::updateLabels(buffer());
-               } else
+                       buffer().updateLabels();
+               } else {
                        label_[row]->updateCommand(label);
+               }
                return;
        }
        InsetCommandParams p(LABEL_CODE);
@@ -528,9 +524,14 @@ void InsetMathHull::numbered(row_type row, bool num)
        if (nonum_[row] && label_[row]) {
                delete label_[row];
                label_[row] = 0;
+               if (!buffer_) {
+                       // The buffer is set at the end of readInset.
+                       // When parsing the inset, buffer_ is 0.
+                       return;
+               }
                // We need an update of the Buffer reference cache.
                // This is achieved by updateLabels().
-               lyx::updateLabels(buffer());
+               buffer().updateLabels();
        }
 }
 
@@ -543,23 +544,23 @@ bool InsetMathHull::numbered(row_type row) const
 
 bool InsetMathHull::ams() const
 {
-       return
-               type_ == hullAlign ||
-               type_ == hullFlAlign ||
-               type_ == hullMultline ||
-               type_ == hullGather ||
-               type_ == hullAlignAt ||
-               type_ == hullXAlignAt ||
-               type_ == hullXXAlignAt;
+       return type_ == hullAlign
+               || type_ == hullFlAlign
+               || type_ == hullMultline
+               || type_ == hullGather
+               || type_ == hullAlignAt
+               || type_ == hullXAlignAt
+               || type_ == hullXXAlignAt;
 }
 
 
 Inset::DisplayType InsetMathHull::display() const
 {
-       return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
+       if (type_ == hullSimple || type_ == hullNone || type_ == hullRegexp)
+               return Inline;
+       return AlignCenter;
 }
 
-
 bool InsetMathHull::numberedType() const
 {
        if (type_ == hullNone)
@@ -568,6 +569,8 @@ bool InsetMathHull::numberedType() const
                return false;
        if (type_ == hullXXAlignAt)
                return false;
+       if (type_ == hullRegexp)
+               return false;
        for (row_type row = 0; row < nrows(); ++row)
                if (!nonum_[row])
                        return true;
@@ -631,6 +634,10 @@ void InsetMathHull::header_write(WriteStream & os) const
                  << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
                break;
 
+       case hullRegexp:
+               os << "\\regexp{";
+               break;
+
        default:
                os << "\\begin{unknown" << star(n) << '}';
                break;
@@ -672,6 +679,10 @@ void InsetMathHull::footer_write(WriteStream & os) const
                os << "\\end{" << hullName(type_) << "}\n";
                break;
 
+       case hullRegexp:
+               os << "}";
+               break;
+
        default:
                os << "\\end{unknown" << star(n) << '}';
                break;
@@ -1217,6 +1228,31 @@ void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
        }
 
+       case LFUN_LABEL_COPY_AS_REF: {
+               row_type row;
+               if (cmd.argument().empty() && &cur.inset() == this)
+                       // if there is no argument and we're inside math, we retrieve
+                       // the row number from the cursor position.
+                       row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
+               else {
+                       // if there is an argument, find the corresponding label, else
+                       // check whether there is at least one label.
+                       for (row = 0; row != nrows(); ++row)
+                               if (!nonum_[row] && label_[row]
+                                         && (cmd.argument().empty() || label(row) == cmd.argument()))
+                                       break;
+               }
+
+               if (row == nrows())
+                       break;
+
+               InsetCommandParams p(REF_CODE, "ref");
+               p["reference"] = label(row);
+               cap::clearSelection();
+               cap::copyInset(cur, new InsetRef(*cur.buffer(), p), label(row));
+               break;
+       }
+
        case LFUN_WORD_DELETE_FORWARD:
        case LFUN_CHAR_DELETE_FORWARD:
                if (col(cur.idx()) + 1 == ncols()
@@ -1320,36 +1356,71 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_DOWN:
        case LFUN_NEWLINE_INSERT:
        case LFUN_MATH_EXTERN:
-       case LFUN_MATH_MUTATE:
        case LFUN_MATH_DISPLAY:
                // we handle these
                status.setEnabled(true);
                return true;
+
+       case LFUN_MATH_MUTATE: {
+               HullType ht = hullType(cmd.argument());
+               status.setOnOff(type_ == ht);
+               status.setEnabled(true);
+               return true;
+       }
+
        case LFUN_MATH_NUMBER_TOGGLE:
                // FIXME: what is the right test, this or the one of
                // LABEL_INSERT?
                status.setEnabled(display());
                status.setOnOff(numberedType());
                return true;
+
        case LFUN_MATH_NUMBER_LINE_TOGGLE: {
                // FIXME: what is the right test, this or the one of
                // LABEL_INSERT?
-               bool const enable = (type_ == hullMultline) ?
-                       (nrows() - 1 == cur.row()) : display();
+               bool const enable = (type_ == hullMultline)
+                       ? (nrows() - 1 == cur.row())
+                       : display() != Inline && nrows() > 1;
                row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
                status.setEnabled(enable);
-               status.setOnOff(numbered(r));
+               status.setOnOff(enable && numbered(r));
                return true;
        }
+
        case LFUN_LABEL_INSERT:
                status.setEnabled(type_ != hullSimple);
                return true;
+
+       case LFUN_LABEL_COPY_AS_REF: {
+               bool enabled = false;
+               row_type row;
+               if (cmd.argument().empty() && &cur.inset() == this) {
+                       // if there is no argument and we're inside math, we retrieve
+                       // the row number from the cursor position.
+                       row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
+                       enabled = numberedType() && label_[row] && !nonum_[row];
+               } else {
+                       // if there is an argument, find the corresponding label, else
+                       // check whether there is at least one label.
+                       for (row_type row = 0; row != nrows(); ++row) {
+                               if (!nonum_[row] && label_[row] && 
+                                       (cmd.argument().empty() || label(row) == cmd.argument())) {
+                                               enabled = true;
+                                               break;
+                               }
+                       }
+               }
+               status.setEnabled(enabled);
+               return true;
+       }
+
        case LFUN_INSET_INSERT:
                if (cmd.getArg(0) == "label") {
                        status.setEnabled(type_ != hullSimple);
                        return true;
                }
                return InsetMathGrid::getStatus(cur, cmd, status);
+
        case LFUN_TABULAR_FEATURE: {
                istringstream is(to_utf8(cmd.argument()));
                string s;
@@ -1399,6 +1470,7 @@ bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
                }
                return InsetMathGrid::getStatus(cur, cmd, status);
        }
+
        default:
                return InsetMathGrid::getStatus(cur, cmd, status);
        }
@@ -1585,11 +1657,12 @@ void InsetMathHull::read(Lexer & lex)
 }
 
 
-void InsetMathHull::readQuiet(Lexer & lex)
+bool InsetMathHull::readQuiet(Lexer & lex)
 {
        MathAtom at;
-       mathed_parse_normal(at, lex, Parse::QUIET);
+       bool result = mathed_parse_normal(at, lex, Parse::QUIET);
        operator=(*at->asHullInset());
+       return result;
 }