diff --git a/CHANGELOG.md b/CHANGELOG.md index f3972046b2..16ce60295c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `urlencode`: Replace special characters in URLs with escape characters. - `docroot`: Utility to retrieve or define the root directory of Nelson Help. - `ismodule`: second input argument `isprotected` added. +- `editor('editor_command', cmd)` allows to change text editor in Nelson (for example: VS Code) ### Changed diff --git a/modules/gui/src/cpp/QtHistoryBrowser.cpp b/modules/gui/src/cpp/QtHistoryBrowser.cpp index 6abf29fe61..1ac327903f 100644 --- a/modules/gui/src/cpp/QtHistoryBrowser.cpp +++ b/modules/gui/src/cpp/QtHistoryBrowser.cpp @@ -78,7 +78,6 @@ QtHistoryBrowser::createContextMenu() m_toEditor = new QAction(TR("Create Script"), this); connect(m_toEditor, SIGNAL(triggered()), this, SLOT(toTextEditor())); - m_popup->addAction(m_execute); m_popup->addAction(m_clearall); m_popup->addAction(m_copy); @@ -94,6 +93,7 @@ QtHistoryBrowser::contextMenuEvent(QContextMenuEvent* event) m_toEditor->setEnabled(items.size() > 0); m_execute->setEnabled(items.size() > 0); m_copy->setEnabled(items.size() > 0); + m_toEditor->setVisible(NelsonConfiguration::getInstance()->useEmbeddedEditor()); m_popup->exec(event->globalPos()); } } diff --git a/modules/nelson_manager/src/cpp/NelsonConfiguration.cpp b/modules/nelson_manager/src/cpp/NelsonConfiguration.cpp index 2f9db840a6..7242c7d57c 100644 --- a/modules/nelson_manager/src/cpp/NelsonConfiguration.cpp +++ b/modules/nelson_manager/src/cpp/NelsonConfiguration.cpp @@ -39,8 +39,11 @@ NelsonConfiguration::NelsonConfiguration() currentAxesOnClick = true; currentFigureOnClick = true; currentLocale = getDefaultLocale(); + useEmbeddedEditorFlag = true; + editorCommandLine = L""; } //============================================================================= + NelsonConfiguration* NelsonConfiguration::getInstance() { @@ -480,5 +483,90 @@ NelsonConfiguration::getDefaultLocale() return L"en_US"; } //============================================================================= +static void +loadEditorUsed() +{ + std::wstring prefdir = NelsonConfiguration::getInstance()->getNelsonPreferencesDirectory(); + std::wstring editorFile = prefdir + L"/default_editor.json"; +#ifdef _MSC_VER + std::ifstream jsonFile(editorFile); +#else + std::ifstream jsonFile(wstring_to_utf8(editorFile)); +#endif + if (jsonFile.is_open()) { + nlohmann::json data; + std::wstring value; + try { + data = nlohmann::json::parse(jsonFile); + std::string _value = data["editor"]; + value = utf8_to_wstring(_value); + } catch (const nlohmann::json::exception&) { + value.clear(); + } + jsonFile.close(); + NelsonConfiguration::getInstance()->setCurrentEditor(value, false); + } +} +//============================================================================= +bool +NelsonConfiguration::useEmbeddedEditor() +{ + if (!embeddedEditorFlagLoaded) { + loadEditorUsed(); + embeddedEditorFlagLoaded = true; + } + return useEmbeddedEditorFlag; +} +//============================================================================= +static void +saveEditorCommandLine(const std::wstring& editorCommandLine) +{ + std::wstring prefdir = NelsonConfiguration::getInstance()->getNelsonPreferencesDirectory(); + std::wstring editorFile = prefdir + L"/default_editor.json"; + nlohmann::json jsonObject; + jsonObject["editor"] = wstring_to_utf8(editorCommandLine); +#ifdef _MSC_VER + std::wofstream file(editorFile); +#else + std::ofstream file(wstring_to_utf8(editorFile)); +#endif + // Serialize the JSON object to the file + if (file.is_open()) { +#ifdef _MSC_VER + file << utf8_to_wstring(jsonObject.dump(2)); + file << L"\n"; +#else + file << jsonObject.dump(2); + file << "\n"; +#endif + file.close(); + } +} +//============================================================================= +void +NelsonConfiguration::setCurrentEditor(const std::wstring& _editorCommandLine, bool save) +{ + if (_editorCommandLine.empty()) { + editorCommandLine = L""; + useEmbeddedEditorFlag = true; + } else { + editorCommandLine = _editorCommandLine; + useEmbeddedEditorFlag = false; + } + if (save) { + saveEditorCommandLine(editorCommandLine); + } +} +//============================================================================= +std::wstring +NelsonConfiguration::getCurrentEditor() +{ + if (!embeddedEditorFlagLoaded) { + loadEditorUsed(); + embeddedEditorFlagLoaded = true; + } + return editorCommandLine; +} +//============================================================================= } // namespace Nelson //============================================================================= diff --git a/modules/nelson_manager/src/include/NelsonConfiguration.hpp b/modules/nelson_manager/src/include/NelsonConfiguration.hpp index 95044941b5..150baa5091 100644 --- a/modules/nelson_manager/src/include/NelsonConfiguration.hpp +++ b/modules/nelson_manager/src/include/NelsonConfiguration.hpp @@ -222,6 +222,15 @@ class NLSNELSON_MANAGER_IMPEXP NelsonConfiguration std::wstring getDefaultLocale(); //============================================================================= + bool + useEmbeddedEditor(); + //============================================================================= + void + setCurrentEditor(const std::wstring& _editorCommandLine, bool save); + //============================================================================= + std::wstring + getCurrentEditor(); + //============================================================================= private: NelsonConfiguration(); //============================================================================= @@ -276,11 +285,15 @@ class NLSNELSON_MANAGER_IMPEXP NelsonConfiguration //============================================================================= bool currentAxesOnClick = true; //============================================================================= + bool useEmbeddedEditorFlag = true; + bool embeddedEditorFlagLoaded = false; + //============================================================================= std::wstring websiteUrl; std::wstring updateUrl; std::wstring bugTrackerUrl; std::wstring docBookUrl; std::wstring currentLocale; + std::wstring editorCommandLine; //============================================================================= }; //============================================================================= diff --git a/modules/text_editor/CMakeLists.txt b/modules/text_editor/CMakeLists.txt index 407b9bdbdc..a290d6b6a5 100644 --- a/modules/text_editor/CMakeLists.txt +++ b/modules/text_editor/CMakeLists.txt @@ -99,12 +99,15 @@ target_include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/src/include ${CMAKE_CURRENT_SOURCE_DIR}/../interpreter/src/include ${CMAKE_CURRENT_SOURCE_DIR}/../types/src/include + ${CMAKE_CURRENT_SOURCE_DIR}/../nelson_manager/src/include + ${CMAKE_CURRENT_SOURCE_DIR}/../os_functions/src/include ${CMAKE_CURRENT_SOURCE_DIR}/../stream_manager/src/include ${CMAKE_CURRENT_SOURCE_DIR}/../i18n/src/include ${CMAKE_CURRENT_SOURCE_DIR}/../engine/src/include) target_link_libraries(${module_library_builtin_name} ${module_library_name} - nlsError_manager nlsI18n nlsInterpreter nlsTypes) + nlsError_manager nlsI18n nlsInterpreter nlsTypes + nlsNelson_manager nlsOs_functions) # ============================================================================== endif() # ============================================================================== diff --git a/modules/text_editor/builtin/c/nlsText_editor_builtin.vcxproj b/modules/text_editor/builtin/c/nlsText_editor_builtin.vcxproj index ebb9f1b9dd..366825d880 100644 --- a/modules/text_editor/builtin/c/nlsText_editor_builtin.vcxproj +++ b/modules/text_editor/builtin/c/nlsText_editor_builtin.vcxproj @@ -95,7 +95,7 @@ Disabled WIN32;_DEBUG;_WINDOWS;_USRDLL;NLSTEXT_EDITOR_BUILTIN_EXPORTS;%(PreprocessorDefinitions) true - $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost + $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/os_functions/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)modules/nelson_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost true 4190 true @@ -116,7 +116,7 @@ Disabled WIN32;_DEBUG;_WINDOWS;_USRDLL;NLSTEXT_EDITOR_BUILTIN_EXPORTS;%(PreprocessorDefinitions) true - $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost + $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/os_functions/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)modules/nelson_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost true 4190 true @@ -139,7 +139,7 @@ true WIN32;NDEBUG;_WINDOWS;_USRDLL;NLSTEXT_EDITOR_BUILTIN_EXPORTS;%(PreprocessorDefinitions) true - $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost + $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/os_functions/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)modules/nelson_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost true 4190 true @@ -164,7 +164,7 @@ true WIN32;NDEBUG;_WINDOWS;_USRDLL;NLSTEXT_EDITOR_BUILTIN_EXPORTS;%(PreprocessorDefinitions) true - $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost + $(SolutionDir)modules/text_editor/builtin/include;$(SolutionDir)modules/text_editor/src/include;$(SolutionDir)modules/commons/src/include;$(SolutionDir)modules/os_functions/src/include;$(SolutionDir)modules/interpreter/src/include;$(SolutionDir)modules/core/src/include;$(SolutionDir)modules/types/src/include;$(SolutionDir)modules/stream_manager/src/include;$(SolutionDir)modules/overload/src/include;$(SolutionDir)modules/i18n/src/include;$(SolutionDir)modules/engine/src/include;$(SolutionDir)modules/error_manager/src/include;$(SolutionDir)modules/nelson_manager/src/include;$(SolutionDir)../NelSon-thirdparty-$(PlatformName)/Boost true 4190 true @@ -195,6 +195,12 @@ {17135b09-bc25-448e-9750-c09a87fde6f8} + + {e467b14f-0d56-4237-b649-eb5572b5e731} + + + {fa50a5eb-b1b3-4035-87bb-8d3124cb7c80} + {ab85e897-56b7-4792-8a8e-f3797be2b7cc} diff --git a/modules/text_editor/builtin/cpp/editorBuiltin.cpp b/modules/text_editor/builtin/cpp/editorBuiltin.cpp index a2b1ee1d99..01b2666c0e 100644 --- a/modules/text_editor/builtin/cpp/editorBuiltin.cpp +++ b/modules/text_editor/builtin/cpp/editorBuiltin.cpp @@ -12,7 +12,8 @@ #include "TextEditor.hpp" #include "InputOutputArgumentsCheckers.hpp" #include "PredefinedErrorMessages.hpp" - +#include "NelsonConfiguration.hpp" +#include "SystemCommand.hpp" //============================================================================= using namespace Nelson; //============================================================================= @@ -23,20 +24,49 @@ Nelson::TextEditorGateway::editorBuiltin(Evaluator* eval, int nLhs, const ArrayO nargoutcheck(nLhs, 0, 0); switch (argIn.size()) { case 0: { - textEditor(eval); + if (NelsonConfiguration::getInstance()->useEmbeddedEditor()) { + textEditor(eval); + } else { + std::wstring command; +#ifdef _MSC_VER + command = L"start /b " + NelsonConfiguration::getInstance()->getCurrentEditor(); +#else + command = NelsonConfiguration::getInstance()->getCurrentEditor() + L" &"; +#endif + SystemCommand(command, 15, false, eval->getID()); + } } break; case 1: { std::wstring filename = argIn[0].getContentAsWideString(); - textEditor(eval, filename); + if (NelsonConfiguration::getInstance()->useEmbeddedEditor()) { + textEditor(eval, filename); + } else { + std::wstring command; +#if _MSC_VER + command = L"start /b " + NelsonConfiguration::getInstance()->getCurrentEditor() + + std::wstring(L" ") + filename; +#else + command + = NelsonConfiguration::getInstance()->getCurrentEditor() + L" " + filename + L" &"; +#endif + SystemCommand(command, 15, false, eval->getID()); + } } break; case 2: { std::wstring option = argIn[0].getContentAsWideString(); if (option == L"new_file") { if (argIn[1].isEmpty() && argIn[1].isDoubleClass()) { - textEditor(eval, true); + if (NelsonConfiguration::getInstance()->useEmbeddedEditor()) { + textEditor(eval, true); + } } else { Error(_W("Wrong value for #2 argument.")); } + } else if (option == L"editor_command") { + std::wstring commandLine = argIn[1].getContentAsWideString(); + NelsonConfiguration::getInstance()->setCurrentEditor(commandLine, true); + } else { + Error(_W("Wrong value for #1 argument.")); } } break; default: { diff --git a/modules/text_editor/help/en_US/xml/editor.xml b/modules/text_editor/help/en_US/xml/editor.xml index 11007bcbb0..67f2834710 100644 --- a/modules/text_editor/help/en_US/xml/editor.xml +++ b/modules/text_editor/help/en_US/xml/editor.xml @@ -9,24 +9,62 @@ editor() editor(filename) + editor('editor_command', cmd) + + filename a string: filename to open. + + cmd + a string representing the command to launch your preferred code editor. + + +

editor opens an existing file in the nelson's editor.

-

editor must be considered as internal.

+

editor must be considered as internal and edit must be preferred.

+

Set another text editor as default: (example with VS code)

+

editor('editor_command', 'code')

+

To restore the default editor, use:

+

editor('editor_command', '')

+

Change text editor is persistent and will be saved in a configuration file.

+ + + nelson + + + + + + + + edit @@ -38,6 +76,12 @@ 1.0.0 initial version + + 1.10.0 + Option to change default text editor + +