diff --git a/apps/code/app.cpp b/apps/code/app.cpp index f59b1b285..9b9d6402a 100644 --- a/apps/code/app.cpp +++ b/apps/code/app.cpp @@ -47,7 +47,7 @@ App::App(Container * container, Snapshot * snapshot) : // insertText() also moves the cursor. We need to re-move it to the // position we want (which is after the first parenthesis or before the // first point). - int deltaCursorLocation = - textArea->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommand(text); + int deltaCursorLocation = - textArea->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommandText(text); // WARNING: This is a dirty and only works because the cursor location we // want is always on the first line of the text we insert. Because of the // auto indentation, it would be difficult to compute the wanted cursor @@ -59,7 +59,7 @@ App::App(Container * container, Snapshot * snapshot) : if (!textField->isEditing()) { textField->setEditing(true); } - int newCursorLocation = textField->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommand(text); + int newCursorLocation = textField->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommandText(text); if (textField->insertTextAtLocation(text, textField->cursorLocation())) { textField->setCursorLocation(newCursorLocation); }}), diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index 9684642aa..81127b78e 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -128,13 +128,13 @@ void VariableBoxController::ContentViewController::insertTextInCaller(const char } int previousCursorLocation = m_textFieldCaller->cursorLocation(); m_textFieldCaller->insertTextAtLocation(commandBuffer, previousCursorLocation); - m_textFieldCaller->setCursorLocation(previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommand(commandBuffer)); + m_textFieldCaller->setCursorLocation(previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommandText(commandBuffer)); return; } if (m_textAreaCaller != nullptr) { int previousCursorLocation = m_textAreaCaller->cursorLocation(); m_textAreaCaller->insertText(commandBuffer); - int deltaCursorLocation = - m_textAreaCaller->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommand(commandBuffer); + int deltaCursorLocation = - m_textAreaCaller->cursorLocation() + previousCursorLocation + Shared::ToolboxHelpers::CursorIndexInCommandText(commandBuffer); m_textAreaCaller->moveCursor(deltaCursorLocation); } } diff --git a/apps/math_toolbox.cpp b/apps/math_toolbox.cpp index 6a82fc083..dc11829a7 100644 --- a/apps/math_toolbox.cpp +++ b/apps/math_toolbox.cpp @@ -115,15 +115,14 @@ void MathToolbox::setSenderAndAction(Responder * sender, Action action) { m_action = action; } -void MathToolbox::actionForEditableExpressionView(void * sender, ToolboxMessageTree * messageTree) { +void MathToolbox::actionForEditableExpressionView(void * sender, const char * text) { EditableExpressionView * expressionLayoutEditorSender = static_cast(sender); - // Translate the message and replace the arguments with Empty chars. - const char * textToInsert = I18n::translate(messageTree->insertedText()); - int strippedTextToInsertMaxLength = strlen(textToInsert); - char strippedTextToInsert[strlen(textToInsert)]; - Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandMessage(messageTree->insertedText(), strippedTextToInsert, strippedTextToInsertMaxLength); + // Replace the arguments with Empty chars. + int textToInsertMaxLength = strlen(text); + char textToInsert[textToInsertMaxLength]; + Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandText(text, textToInsert, textToInsertMaxLength); // Create the layout - Expression * resultExpression = Expression::parse(strippedTextToInsert); + Expression * resultExpression = Expression::parse(textToInsert); if (resultExpression == nullptr) { return; } @@ -146,25 +145,24 @@ void MathToolbox::actionForEditableExpressionView(void * sender, ToolboxMessageT expressionLayoutEditorSender->insertLayoutAtCursor(resultLayout, pointedLayout); } -void MathToolbox::actionForTextField(void * sender, ToolboxMessageTree * messageTree) { +void MathToolbox::actionForTextField(void * sender, const char * text) { TextField * textFieldSender = static_cast(sender); if (!textFieldSender->isEditing()) { textFieldSender->setEditing(true); } - const char * textToInsert = I18n::translate(messageTree->insertedText()); - int textToInsertLength = strlen(textToInsert); - char strippedTextToInsert[textToInsertLength]; + int maxTextToInsertLength = strlen(text); + char textToInsert[maxTextToInsertLength]; // Translate the message and remove the arguments. - Shared::ToolboxHelpers::TextToInsertForCommandMessage(messageTree->insertedText(), strippedTextToInsert, textToInsertLength); - textFieldSender->insertTextAtLocation(strippedTextToInsert, textFieldSender->cursorLocation()); - int newCursorLocation = textFieldSender->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommand(strippedTextToInsert); + Shared::ToolboxHelpers::TextToInsertForCommandText(text, textToInsert, maxTextToInsertLength); + textFieldSender->insertTextAtLocation(textToInsert, textFieldSender->cursorLocation()); + int newCursorLocation = textFieldSender->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommandText(text); textFieldSender->setCursorLocation(newCursorLocation); } bool MathToolbox::selectLeaf(ToolboxMessageTree * selectedMessageTree) { ToolboxMessageTree * messageTree = selectedMessageTree; m_selectableTableView.deselectTable(); - m_action(sender(), messageTree); + m_action(sender(), I18n::translate(messageTree->insertedText())); app()->dismissModalViewController(); return true; } diff --git a/apps/math_toolbox.h b/apps/math_toolbox.h index dbcc8e2cd..2f607863c 100644 --- a/apps/math_toolbox.h +++ b/apps/math_toolbox.h @@ -8,11 +8,11 @@ class MathToolbox : public Toolbox { public: - typedef void (*Action)(void * sender, ToolboxMessageTree * messageTree); + typedef void (*Action)(void * sender, const char * text); MathToolbox(); void setSenderAndAction(Responder * sender, Action action); - static void actionForEditableExpressionView(void * sender, ToolboxMessageTree * messageTree); - static void actionForTextField(void * sender, ToolboxMessageTree * messageTree); + static void actionForEditableExpressionView(void * sender, const char * text); + static void actionForTextField(void * sender, const char * text); protected: bool selectLeaf(ToolboxMessageTree * selectedMessageTree) override; const ToolboxMessageTree * rootModel() override; diff --git a/apps/shared/toolbox_helpers.cpp b/apps/shared/toolbox_helpers.cpp index 268162c8d..6186dce33 100644 --- a/apps/shared/toolbox_helpers.cpp +++ b/apps/shared/toolbox_helpers.cpp @@ -7,7 +7,7 @@ namespace Shared { namespace ToolboxHelpers { -int CursorIndexInCommand(const char * text) { +int CursorIndexInCommandText(const char * text) { for (size_t i = 0; i < strlen(text); i++) { if (text[i] == '(' || text[i] == '\'') { return i + 1; @@ -20,8 +20,7 @@ int CursorIndexInCommand(const char * text) { } void TextToInsertForCommandMessage(I18n::Message message, char * buffer, int bufferSize) { - const char * messageText = I18n::translate(message); - TextToInsertForCommandText(messageText, buffer, bufferSize); + TextToInsertForCommandText(I18n::translate(message), buffer, bufferSize); } void TextToInsertForCommandText(const char * command, char * buffer, int bufferSize) { @@ -58,7 +57,11 @@ void TextToInsertForCommandText(const char * command, char * buffer, int bufferS } void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer, int bufferSize) { - if (message == I18n::Message::MatrixCommandWithArg) { + TextToParseIntoLayoutForCommandText(I18n::translate(message), buffer, bufferSize); +} + +void TextToParseIntoLayoutForCommandText(const char * command, char * buffer, int bufferSize) { + if (command == I18n::translate(I18n::Message::MatrixCommandWithArg)) { assert(bufferSize >= 6); // Handle a new matrix command. buffer[0] = '['; @@ -69,8 +72,7 @@ void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer buffer[5] = 0; return; } - const char * messageText = I18n::translate(message); - TextToInsertForCommandText(messageText, buffer, bufferSize); + TextToInsertForCommandText(command, buffer, bufferSize); size_t bufferLength = strlen(buffer); for (size_t i = 0; i < bufferLength; i++) { if (buffer[i] == '(' || buffer[i] == '[' || buffer[i] == ',') { diff --git a/apps/shared/toolbox_helpers.h b/apps/shared/toolbox_helpers.h index d051f5847..0065993a2 100644 --- a/apps/shared/toolbox_helpers.h +++ b/apps/shared/toolbox_helpers.h @@ -6,8 +6,8 @@ namespace Shared { namespace ToolboxHelpers { -int CursorIndexInCommand(const char * text); -/* Returns the index of the cursor position in a Command, which is the smallest +int CursorIndexInCommandText(const char * text); +/* Returns the index of the cursor position in a command, which is the smallest * index between : * - After the first open parenthesis * - The end of the text */ @@ -18,6 +18,7 @@ void TextToInsertForCommandText(const char * command, char * buffer, int bufferS /* Removes the arguments from a command: * - Removes text between parentheses or brackets, except commas */ void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer, int bufferSize); +void TextToParseIntoLayoutForCommandText(const char * command, char * buffer, int bufferSize); /* Removes the arguments from a command and replaces them with empty chars. */ }