diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a5e60e1a..d4f60453 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,373 +1,374 @@ # handle data files, .desktop & .cmake add_subdirectory(data) # jscripts for the part add_subdirectory( script/data ) # set right defines for libgit2 usage if(LIBGIT2_FOUND) SET (CMAKE_REQUIRED_LIBRARIES LibGit2::LibGit2) set (KTEXTEDITOR_OPTIONAL_LIBS ${KTEXTEDITOR_OPTIONAL_LIBS} LibGit2::LibGit2) endif() if(EditorConfig_FOUND) SET (CMAKE_REQUIRED_LIBRARIES ${EditorConfig_LIBRARIES}) set (KTEXTEDITOR_OPTIONAL_LIBS ${KTEXTEDITOR_OPTIONAL_LIBS} ${EditorConfig_LIBRARIES}) endif() # handle include files, both normal ones and generated ones add_subdirectory(include) # includes include_directories( # for config.h ${CMAKE_BINARY_DIR} # for generated ktexteditor headers ${CMAKE_CURRENT_BINARY_DIR}/include # for normal sources ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include/ktexteditor ${CMAKE_CURRENT_SOURCE_DIR}/buffer ${CMAKE_CURRENT_SOURCE_DIR}/completion ${CMAKE_CURRENT_SOURCE_DIR}/dialogs ${CMAKE_CURRENT_SOURCE_DIR}/document ${CMAKE_CURRENT_SOURCE_DIR}/script ${CMAKE_CURRENT_SOURCE_DIR}/mode ${CMAKE_CURRENT_SOURCE_DIR}/render ${CMAKE_CURRENT_SOURCE_DIR}/search ${CMAKE_CURRENT_SOURCE_DIR}/syntax ${CMAKE_CURRENT_SOURCE_DIR}/schema ${CMAKE_CURRENT_SOURCE_DIR}/undo ${CMAKE_CURRENT_SOURCE_DIR}/utils ${CMAKE_CURRENT_SOURCE_DIR}/inputmode ${CMAKE_CURRENT_SOURCE_DIR}/view ${CMAKE_CURRENT_SOURCE_DIR}/swapfile ${CMAKE_CURRENT_SOURCE_DIR}/variableeditor) # KTextEditor interface sources set(ktexteditor_LIB_SRCS # text buffer & buffer helpers buffer/katesecuretextbuffer.cpp buffer/katetextbuffer.cpp buffer/katetextblock.cpp buffer/katetextline.cpp buffer/katetextcursor.cpp buffer/katetextrange.cpp buffer/katetexthistory.cpp buffer/katetextfolding.cpp # completion (widget, model, delegate, ...) completion/katecompletionwidget.cpp completion/katecompletionmodel.cpp completion/katecompletiontree.cpp completion/katecompletionconfig.cpp completion/kateargumenthinttree.cpp completion/kateargumenthintmodel.cpp completion/katecompletiondelegate.cpp completion/expandingtree/expandingwidgetmodel.cpp completion/expandingtree/expandingdelegate.cpp completion/expandingtree/expandingtree.cpp # simple internal word completion completion/katewordcompletion.cpp # internal syntax-file based keyword completion completion/katekeywordcompletion.cpp # dialogs dialogs/kateconfigpage.cpp dialogs/katedialogs.cpp dialogs/kateconfigpage.cpp # document (THE document, buffer, lines/cursors/..., CORE STUFF) document/katedocument.cpp document/katebuffer.cpp # undo undo/kateundo.cpp undo/katemodifiedundo.cpp undo/kateundomanager.cpp # scripting script/katescript.cpp script/kateindentscript.cpp script/katecommandlinescript.cpp script/katescriptmanager.cpp script/katescriptaction.cpp # scripting wrappers +script/katescripteditor.cpp script/katescriptdocument.cpp script/katescriptview.cpp script/katescripthelpers.cpp # mode (modemanager and co) mode/katemodemanager.cpp mode/katemodeconfigpage.cpp mode/katemodemenu.cpp mode/katewildcardmatcher.cpp # modeline variable editor variableeditor/variablelineedit.cpp variableeditor/variablelistview.cpp variableeditor/variableeditor.cpp variableeditor/variableitem.cpp variableeditor/katehelpbutton.cpp # printing classes printing/kateprinter.cpp printing/printpainter.cpp printing/printconfigwidgets.cpp # rendering stuff (katerenderer and helpers) render/katerenderer.cpp render/katerenderrange.cpp render/katelayoutcache.cpp render/katetextlayout.cpp render/katelinelayout.cpp # search stuff search/kateregexp.cpp search/kateplaintextsearch.cpp search/kateregexpsearch.cpp search/katematch.cpp search/katesearchbar.cpp # syntax related stuff (highlighting, xml file parsing, ...) syntax/katesyntaxmanager.cpp syntax/katehighlight.cpp syntax/katehighlighthelpers.cpp syntax/katehighlightmenu.cpp syntax/katesyntaxdocument.cpp syntax/katehighlightingcmds.cpp # view stuff (THE view and its helpers) view/kateview.cpp view/kateviewinternal.cpp view/kateviewhelpers.cpp view/katemessagewidget.cpp view/katefadeeffect.cpp view/kateanimation.cpp view/katetextanimation.cpp view/katetextpreview.cpp view/katestatusbar.cpp view/wordcounter.cpp # spell checking spellcheck/prefixstore.h spellcheck/prefixstore.cpp spellcheck/ontheflycheck.h spellcheck/ontheflycheck.cpp spellcheck/spellcheck.h spellcheck/spellcheck.cpp spellcheck/spellcheckdialog.h spellcheck/spellcheckdialog.cpp spellcheck/spellcheckbar.cpp spellcheck/spellingmenu.h spellcheck/spellingmenu.cpp # generic stuff, unsorted... utils/katecmds.cpp utils/kateconfig.cpp utils/katebookmarks.cpp utils/kateautoindent.cpp utils/katetemplatehandler.cpp utils/kateglobal.cpp utils/katecmd.cpp utils/ktexteditor.cpp utils/document.cpp utils/range.cpp utils/documentcursor.cpp utils/attribute.cpp utils/codecompletioninterface.cpp utils/codecompletionmodel.cpp utils/codecompletionmodelcontrollerinterface.cpp utils/configinterface.cpp utils/movinginterface.cpp utils/movingcursor.cpp utils/movingrange.cpp utils/movingrangefeedback.cpp utils/messageinterface.cpp utils/application.cpp utils/mainwindow.cpp utils/katedefaultcolors.cpp utils/katecommandrangeexpressionparser.cpp utils/katesedcmd.cpp # schema schema/kateschema.cpp schema/kateschemaconfig.cpp schema/katestyletreewidget.cpp schema/katecolortreewidget.cpp schema/katecategorydrawer.cpp # swapfile swapfile/kateswapdiffcreator.cpp swapfile/kateswapfile.cpp # export as HTML export/exporter.cpp export/htmlexporter.cpp # input modes inputmode/kateabstractinputmode.cpp inputmode/kateabstractinputmodefactory.cpp inputmode/katenormalinputmode.cpp inputmode/katenormalinputmodefactory.cpp ) # optionally compile with EditorConfig support if(EditorConfig_FOUND) set(ktexteditor_LIB_SRCS ${ktexteditor_LIB_SRCS} document/editorconfig.cpp) endif() ki18n_wrap_ui(ktexteditor_LIB_SRCS dialogs/textareaappearanceconfigwidget.ui dialogs/bordersappearanceconfigwidget.ui dialogs/commandmenuconfigwidget.ui dialogs/commandmenueditwidget.ui dialogs/completionconfigtab.ui dialogs/navigationconfigwidget.ui dialogs/editconfigwidget.ui dialogs/filetypeconfigwidget.ui dialogs/indentationconfigwidget.ui dialogs/opensaveconfigwidget.ui dialogs/opensaveconfigadvwidget.ui dialogs/completionconfigwidget.ui search/searchbarincremental.ui search/searchbarpower.ui spellcheck/spellcheckbar.ui dialogs/spellcheckconfigwidget.ui schema/howtoimportschema.ui ) # add the resource files, the one with mascot + ui file and the generated ones qt5_add_resources( ktexteditor_LIB_SRCS data/ktexteditor.qrc "${CMAKE_CURRENT_BINARY_DIR}/script/data/script.qrc") if (BUILD_VIMODE) ki18n_wrap_ui(ktexteditor_LIB_SRCS vimode/config/configwidget.ui) set(ktexteditor_LIB_SRCS ${ktexteditor_LIB_SRCS} inputmode/kateviinputmode.cpp inputmode/kateviinputmodefactory.cpp # vi input mode vimode/config/configtab.cpp vimode/modes/insertvimode.cpp vimode/modes/modebase.cpp vimode/modes/normalvimode.cpp vimode/modes/replacevimode.cpp vimode/modes/visualvimode.cpp vimode/appcommands.cpp vimode/cmds.cpp vimode/inputmodemanager.cpp vimode/command.cpp vimode/motion.cpp vimode/range.cpp vimode/keyparser.cpp vimode/globalstate.cpp vimode/emulatedcommandbar/emulatedcommandbar.cpp vimode/emulatedcommandbar/matchhighlighter.cpp vimode/emulatedcommandbar/completer.cpp vimode/emulatedcommandbar/activemode.cpp vimode/emulatedcommandbar/interactivesedreplacemode.cpp vimode/emulatedcommandbar/searchmode.cpp vimode/emulatedcommandbar/commandmode.cpp vimode/commandrangeexpressionparser.cpp vimode/keymapper.cpp vimode/marks.cpp vimode/jumps.cpp vimode/history.cpp vimode/macros.cpp vimode/mappings.cpp vimode/registers.cpp vimode/searcher.cpp vimode/completion.cpp vimode/completionrecorder.cpp vimode/completionreplayer.cpp vimode/macrorecorder.cpp vimode/lastchangerecorder.cpp ) endif() add_library(KF5TextEditor ${ktexteditor_LIB_SRCS} ${KTEXTEDITOR_PUBLIC_HEADERS}) generate_export_header(KF5TextEditor BASE_NAME KTextEditor) add_library(KF5::TextEditor ALIAS KF5TextEditor) target_include_directories(KF5TextEditor INTERFACE "$") # API is more or less KParts++, other stuff is used only internally target_link_libraries(KF5TextEditor PUBLIC KF5::Parts PRIVATE Qt5::Qml Qt5::PrintSupport KF5::I18n KF5::Archive KF5::GuiAddons KF5::IconThemes KF5::ItemViews KF5::SonnetCore KF5::SyntaxHighlighting ${KTEXTEDITOR_OPTIONAL_LIBS} ) set_target_properties(KF5TextEditor PROPERTIES VERSION ${KTEXTEDITOR_VERSION_STRING} SOVERSION ${KTEXTEDITOR_SOVERSION} EXPORT_NAME "TextEditor" ) install(TARGETS KF5TextEditor EXPORT KF5TextEditorTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ktexteditor_export.h DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KTextEditor COMPONENT Devel ) if(BUILD_QCH) ecm_add_qch( KF5TextEditor_QCH NAME KTextEditor BASE_NAME KF5TextEditor VERSION ${KF5_VERSION} ORG_DOMAIN org.kde SOURCES # using only public headers, to cover only public API ${KTEXTEDITOR_PUBLIC_HEADERS} "${CMAKE_SOURCE_DIR}/docs/apidocs-groups.dox" "${CMAKE_SOURCE_DIR}/docs/coding-guidelines.dox" "${CMAKE_SOURCE_DIR}/docs/design.dox" "${CMAKE_SOURCE_DIR}/docs/porting.dox" MD_MAINPAGE "${CMAKE_SOURCE_DIR}/README.md" IMAGE_DIRS "${CMAKE_SOURCE_DIR}/docs/pics" LINK_QCHS KF5Parts_QCH BLANK_MACROS KTEXTEDITOR_EXPORT KTEXTEDITOR_DEPRECATED KTEXTEDITOR_DEPRECATED_EXPORT TAGFILE_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} QCH_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} COMPONENT Devel ) endif() include(ECMGeneratePriFile) ecm_generate_pri_file(BASE_NAME KTextEditor LIB_NAME KF5TextEditor DEPS "KParts" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/KTextEditor) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) add_executable(kauth_ktexteditor_helper buffer/katesecuretextbuffer.cpp) target_link_libraries(kauth_ktexteditor_helper KF5::Auth ) install(TARGETS kauth_ktexteditor_helper DESTINATION ${KAUTH_HELPER_INSTALL_DIR} ) kauth_install_helper_files(kauth_ktexteditor_helper org.kde.ktexteditor.katetextbuffer root) kauth_install_actions(org.kde.ktexteditor.katetextbuffer buffer/org.kde.ktexteditor.katetextbuffer.actions) # add part add_subdirectory(part) diff --git a/src/script/katescript.cpp b/src/script/katescript.cpp index b8fa16ad..09d36c2e 100644 --- a/src/script/katescript.cpp +++ b/src/script/katescript.cpp @@ -1,255 +1,253 @@ // This file is part of the KDE libraries // Copyright (C) 2008 Paul Giannaros // Copyright (C) 2009, 2010 Dominik Haumann // Copyright (C) 2010 Joseph Wenninger // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) version 3. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public License // along with this library; see the file COPYING.LIB. If not, write to // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #include "katescript.h" +#include "katescripteditor.h" #include "katescriptdocument.h" #include "katescriptview.h" #include "katescripthelpers.h" #include "kateview.h" #include "katedocument.h" #include "katepartdebug.h" #include #include #include #include #include #include #include #include KateScript::KateScript(const QString &urlOrScript, enum InputType inputType) - : m_loaded(false) - , m_loadSuccessful(false) - , m_url(inputType == InputURL ? urlOrScript : QString()) - , m_engine(nullptr) - , m_document(nullptr) - , m_view(nullptr) + : m_url(inputType == InputURL ? urlOrScript : QString()) , m_inputType(inputType) , m_script(inputType == InputSCRIPT ? urlOrScript : QString()) { } KateScript::~KateScript() { if (m_loadSuccessful) { // remove data... + delete m_editor; delete m_document; delete m_view; delete m_engine; } } QString KateScript::backtrace(const QJSValue &error, const QString &header) { QString bt; if (!header.isNull()) { bt += header + QLatin1String(":\n"); } if (error.isError()) { bt += error.toString() + QLatin1Char('\n'); } return bt; } void KateScript::displayBacktrace(const QJSValue &error, const QString &header) { if (!m_engine) { std::cerr << "KateScript::displayBacktrace: no engine, cannot display error\n"; return; } std::cerr << "\033[31m" << qPrintable(backtrace(error, header)) << "\033[0m" << '\n'; } void KateScript::clearExceptions() { if (!load()) { return; } } QJSValue KateScript::global(const QString &name) { // load the script if necessary if (!load()) { return QJSValue::UndefinedValue; } return m_engine->globalObject().property(name); } QJSValue KateScript::function(const QString &name) { QJSValue value = global(name); if (!value.isCallable()) { return QJSValue::UndefinedValue; } return value; } bool KateScript::load() { if (m_loaded) { return m_loadSuccessful; } m_loaded = true; m_loadSuccessful = false; // here set to false, and at end of function to true // read the script file into memory QString source; if (m_inputType == InputURL) { if (!Kate::Script::readFile(m_url, source)) { return false; } } else { source = m_script; } // create script engine, register meta types m_engine = new QJSEngine(); // export read & require function and add the require guard object QJSValue functions = m_engine->newQObject(new Kate::ScriptHelper(m_engine)); m_engine->globalObject().setProperty(QStringLiteral("functions"), functions); m_engine->globalObject().setProperty(QStringLiteral("read"), functions.property(QStringLiteral("read"))); m_engine->globalObject().setProperty(QStringLiteral("require"), functions.property(QStringLiteral("require"))); m_engine->globalObject().setProperty(QStringLiteral("require_guard"), m_engine->newObject()); // export debug function m_engine->globalObject().setProperty(QStringLiteral("debug"), functions.property(QStringLiteral("debug"))); // export translation functions m_engine->globalObject().setProperty(QStringLiteral("i18n"), functions.property(QStringLiteral("_i18n"))); m_engine->globalObject().setProperty(QStringLiteral("i18nc"), functions.property(QStringLiteral("_i18nc"))); m_engine->globalObject().setProperty(QStringLiteral("i18np"), functions.property(QStringLiteral("_i18np"))); m_engine->globalObject().setProperty(QStringLiteral("i18ncp"), functions.property(QStringLiteral("_i18ncp"))); // register default styles as ds* global properties m_engine->globalObject().setProperty(QStringLiteral("dsNormal"), KTextEditor::dsNormal); m_engine->globalObject().setProperty(QStringLiteral("dsKeyword"), KTextEditor::dsKeyword); m_engine->globalObject().setProperty(QStringLiteral("dsFunction"), KTextEditor::dsFunction); m_engine->globalObject().setProperty(QStringLiteral("dsVariable"), KTextEditor::dsVariable); m_engine->globalObject().setProperty(QStringLiteral("dsControlFlow"), KTextEditor::dsControlFlow); m_engine->globalObject().setProperty(QStringLiteral("dsOperator"), KTextEditor::dsOperator); m_engine->globalObject().setProperty(QStringLiteral("dsBuiltIn"), KTextEditor::dsBuiltIn); m_engine->globalObject().setProperty(QStringLiteral("dsExtension"), KTextEditor::dsExtension); m_engine->globalObject().setProperty(QStringLiteral("dsPreprocessor"), KTextEditor::dsPreprocessor); m_engine->globalObject().setProperty(QStringLiteral("dsAttribute"), KTextEditor::dsAttribute); m_engine->globalObject().setProperty(QStringLiteral("dsChar"), KTextEditor::dsChar); m_engine->globalObject().setProperty(QStringLiteral("dsSpecialChar"), KTextEditor::dsSpecialChar); m_engine->globalObject().setProperty(QStringLiteral("dsString"), KTextEditor::dsString); m_engine->globalObject().setProperty(QStringLiteral("dsVerbatimString"), KTextEditor::dsVerbatimString); m_engine->globalObject().setProperty(QStringLiteral("dsSpecialString"), KTextEditor::dsSpecialString); m_engine->globalObject().setProperty(QStringLiteral("dsImport"), KTextEditor::dsImport); m_engine->globalObject().setProperty(QStringLiteral("dsDataType"), KTextEditor::dsDataType); m_engine->globalObject().setProperty(QStringLiteral("dsDecVal"), KTextEditor::dsDecVal); m_engine->globalObject().setProperty(QStringLiteral("dsBaseN"), KTextEditor::dsBaseN); m_engine->globalObject().setProperty(QStringLiteral("dsFloat"), KTextEditor::dsFloat); m_engine->globalObject().setProperty(QStringLiteral("dsConstant"), KTextEditor::dsConstant); m_engine->globalObject().setProperty(QStringLiteral("dsComment"), KTextEditor::dsComment); m_engine->globalObject().setProperty(QStringLiteral("dsDocumentation"), KTextEditor::dsDocumentation); m_engine->globalObject().setProperty(QStringLiteral("dsAnnotation"), KTextEditor::dsAnnotation); m_engine->globalObject().setProperty(QStringLiteral("dsCommentVar"), KTextEditor::dsCommentVar); m_engine->globalObject().setProperty(QStringLiteral("dsRegionMarker"), KTextEditor::dsRegionMarker); m_engine->globalObject().setProperty(QStringLiteral("dsInformation"), KTextEditor::dsInformation); m_engine->globalObject().setProperty(QStringLiteral("dsWarning"), KTextEditor::dsWarning); m_engine->globalObject().setProperty(QStringLiteral("dsAlert"), KTextEditor::dsAlert); m_engine->globalObject().setProperty(QStringLiteral("dsOthers"), KTextEditor::dsOthers); m_engine->globalObject().setProperty(QStringLiteral("dsError"), KTextEditor::dsError); // register scripts itself QJSValue result = m_engine->evaluate(source, m_url); if (hasException(result, m_url)) { return false; } // AFTER SCRIPT: set the view/document objects as necessary + m_engine->globalObject().setProperty(QStringLiteral("editor"), m_engine->newQObject(m_editor = new KateScriptEditor(m_engine))); m_engine->globalObject().setProperty(QStringLiteral("document"), m_engine->newQObject(m_document = new KateScriptDocument(m_engine))); m_engine->globalObject().setProperty(QStringLiteral("view"), m_engine->newQObject(m_view = new KateScriptView(m_engine))); // yip yip! m_loadSuccessful = true; return true; } QJSValue KateScript::evaluate(const QString& program, const FieldMap& env) { if ( !load() ) { qWarning() << "load of script failed:" << program; return QJSValue(); } // Wrap the arguments in a function to avoid poluting the global object QString programWithContext = QStringLiteral("function(") + QStringList(env.keys()).join(QLatin1Char(',')) + QStringLiteral(") { return ") + program + QStringLiteral("}"); QJSValue programFunction = m_engine->evaluate(programWithContext); Q_ASSERT(programFunction.isCallable()); QJSValueList args; for ( auto it = env.begin(); it != env.end(); it++ ) { args << it.value(); } QJSValue result = programFunction.call(args); if (result.isError()) qWarning() << "Error evaluating script: " << result.toString(); return result; } bool KateScript::hasException(const QJSValue &object, const QString &file) { if (object.isError()) { displayBacktrace(object, i18n("Error loading script %1\n", file)); m_errorMessage = i18n("Error loading script %1", file); delete m_engine; m_engine = nullptr; m_loadSuccessful = false; return true; } return false; } bool KateScript::setView(KTextEditor::ViewPrivate *view) { if (!load()) { return false; } // setup the stuff m_document->setDocument(view->doc()); m_view->setView(view); return true; } void KateScript::setGeneralHeader(const KateScriptHeader &generalHeader) { m_generalHeader = generalHeader; } KateScriptHeader &KateScript::generalHeader() { return m_generalHeader; } diff --git a/src/script/katescript.h b/src/script/katescript.h index 345002a4..6844cb8b 100644 --- a/src/script/katescript.h +++ b/src/script/katescript.h @@ -1,229 +1,231 @@ // This file is part of the KDE libraries // Copyright (C) 2008 Paul Giannaros // Copyright (C) 2009 Dominik Haumann // Copyright (C) 2010 Joseph Wenninger // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) version 3. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public License // along with this library; see the file COPYING.LIB. If not, write to // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, // Boston, MA 02110-1301, USA. #ifndef KATE_SCRIPT_H #define KATE_SCRIPT_H #include #include #include class QJSEngine; namespace KTextEditor { class ViewPrivate; } +class KateScriptEditor; class KateScriptDocument; class KateScriptView; namespace Kate { enum ScriptType { /** The script is an indenter */ IndentationScript, /** The script contains command line commands */ CommandLineScript, /** Don't know what kind of script this is */ UnknownScript }; } //BEGIN KateScriptHeader class KateScriptHeader { public: KateScriptHeader() {} virtual ~KateScriptHeader() {} inline void setLicense(const QString &license) { m_license = license; } inline const QString &license() const { return m_license; } inline void setAuthor(const QString &author) { m_author = author; } inline const QString &author() const { return m_author; } inline void setRevision(int revision) { m_revision = revision; } inline int revision() const { return m_revision; } inline void setKateVersion(const QString &kateVersion) { m_kateVersion = kateVersion; } inline const QString &kateVersion() const { return m_kateVersion; } inline void setScriptType(Kate::ScriptType scriptType) { m_scriptType = scriptType; } inline Kate::ScriptType scriptType() const { return m_scriptType; } private: QString m_license; ///< the script's license, e.g. LGPL QString m_author; ///< the script author, e.g. "John Smith " int m_revision = 0; ///< script revision, a simple number, e.g. 1, 2, 3, ... QString m_kateVersion; ///< required katepart version Kate::ScriptType m_scriptType = Kate::UnknownScript; ///< the script type }; //END //BEGIN KateScript /** * KateScript objects represent a script that can be executed and inspected. */ class KateScript { public: enum InputType { InputURL, InputSCRIPT }; typedef QMap FieldMap; /** * Create a new script representation, passing either a file or the script * content @p urlOrScript to it. * In case of a file, loading of the script will happen lazily. */ KateScript(const QString &urlOrScript, enum InputType inputType = InputURL); virtual ~KateScript(); /** The script's URL */ const QString &url() { return m_url; } /** * Load the script. If loading is successful, returns true. Otherwise, returns * returns false and an error message will be set (see errorMessage()). * Note that you don't have to call this -- it is called as necessary by the * functions that require it. * Subsequent calls to load will return the value it returned the first time. */ bool load(); /** * set view for this script for the execution * will trigger load! */ bool setView(KTextEditor::ViewPrivate *view); /** * Get a QJSValue for a global item in the script given its name, or an * invalid QJSValue if no such global item exists. */ QJSValue global(const QString &name); /** * Return a function in the script of the given name, or an invalid QJSValue * if no such function exists. */ QJSValue function(const QString &name); /** Return a context-specific error message */ const QString &errorMessage() { return m_errorMessage; } /** Returns the backtrace when a script has errored out */ QString backtrace(const QJSValue &error, const QString &header = QString()); /** Execute a piece of code **/ QJSValue evaluate(const QString& program, const FieldMap& env = FieldMap()); /** Displays the backtrace when a script has errored out */ void displayBacktrace(const QJSValue &error, const QString &header = QString()); /** Clears any uncaught exceptions in the script engine. */ void clearExceptions(); /** set the general header after construction of the script */ void setGeneralHeader(const KateScriptHeader &generalHeader); /** Return the general header */ KateScriptHeader &generalHeader(); protected: /** Checks for exception and gives feedback on the console. */ bool hasException(const QJSValue &object, const QString &file); private: /** Whether or not there has been a call to load */ - bool m_loaded; + bool m_loaded = false; /** Whether or not the script loaded successfully into memory */ - bool m_loadSuccessful; + bool m_loadSuccessful = false; /** The script's URL */ QString m_url; /** An error message set when an error occurs */ QString m_errorMessage; protected: /** The Qt interpreter for this script */ - QJSEngine *m_engine; + QJSEngine *m_engine = nullptr; private: /** general header data */ KateScriptHeader m_generalHeader; - /** document/view wrapper objects */ - KateScriptDocument *m_document; - KateScriptView *m_view; + /** wrapper objects */ + KateScriptEditor *m_editor = nullptr; + KateScriptDocument *m_document = nullptr; + KateScriptView *m_view = nullptr; private: /** if input is script or url**/ enum InputType m_inputType; QString m_script; }; //END #endif diff --git a/src/script/katescripteditor.cpp b/src/script/katescripteditor.cpp new file mode 100644 index 00000000..29c54ded --- /dev/null +++ b/src/script/katescripteditor.cpp @@ -0,0 +1,50 @@ +/* This file is part of the KDE libraries. + * + * Copyright (C) 2017 Dominik Haumann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "katescripteditor.h" + +#include "kateglobal.h" + +#include +#include +#include + +using KTextEditor::EditorPrivate; + +KateScriptEditor::KateScriptEditor(QJSEngine *engine, QObject *parent) + : QObject(parent) + , m_engine(engine) +{ +} + +QString KateScriptEditor::clipboardText() const +{ + return QApplication::clipboard()->text(); +} + +QStringList KateScriptEditor::clipboardHistory() const +{ + return KTextEditor::EditorPrivate::self()->clipboardHistory(); +} + +void KateScriptEditor::setClipboardText(const QString &text) +{ + KTextEditor::EditorPrivate::self()->copyToClipboard(text); +} diff --git a/src/script/katescripteditor.h b/src/script/katescripteditor.h new file mode 100644 index 00000000..311a1571 --- /dev/null +++ b/src/script/katescripteditor.h @@ -0,0 +1,50 @@ +/* This file is part of the KDE libraries. + * + * Copyright (C) 2017 Dominik Haumann + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifndef KATE_SCRIPT_EDITOR_H +#define KATE_SCRIPT_EDITOR_H + +#include +#include + +#include + +namespace KTextEditor { class ViewPrivate; } +class QJSEngine; + +/** + * This class wraps the global editor instance KateGlobal, exposing some + * helper methods such as the clipboard history etc. + */ +class KTEXTEDITOR_EXPORT KateScriptEditor : public QObject +{ + Q_OBJECT + +public: + KateScriptEditor(QJSEngine *engine, QObject *parent = nullptr); + + Q_INVOKABLE QString clipboardText() const; + Q_INVOKABLE QStringList clipboardHistory() const; + Q_INVOKABLE void setClipboardText(const QString &text); + +private: + QJSEngine *m_engine = nullptr; +}; + +#endif