From 3cd445fbcce4e2d977bd3e6283d04130a23cb087 Mon Sep 17 00:00:00 2001 From: tedjk Date: Fri, 20 Feb 2015 15:36:56 +0100 Subject: [PATCH 1/6] progress --- juci/CMakeLists.txt | 2 +- juci/api.cc | 113 +++++++++++++++++---------- juci/api.h | 57 +++++++------- juci/api_ext.cc | 7 +- juci/cmake/Modules/FindTestlcl.cmake | 4 +- juci/juci.cc | 2 + juci/menu.cc | 8 +- juci/menu.h | 3 +- juci/source.cc | 4 +- juci/window.cc | 10 ++- juci/window.h | 7 +- 11 files changed, 130 insertions(+), 87 deletions(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index 57c6030..9f93102 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -104,7 +104,7 @@ include_directories( ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS} - "/home/zalox/bachelor/libclang++/headers/" + ${LCL_INCLUDE_DIRS} ) link_directories( ${GTKMM_LIBRARY_DIRS} diff --git a/juci/api.cc b/juci/api.cc index 42149d0..196b1f8 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -1,47 +1,80 @@ #include "api.h" -namespace juci_api{ - void cpp::ReplaceWord(const std::string word) { - //TODO implement api::ReplaceWord / change string to iter? - //some_namespace::controller::replaceWord(word_*); - std::cout << "unimplemented function: 'api::ReplaceWord()' called" - << std::endl; +std::shared_ptr libjuci::ApiServiceProvider::menu_; +std::shared_ptr libjuci::ApiServiceProvider::notebook_; - std::cout << "The string: " << g_test_string << std::endl; - } +///////////////////////////// +//// API ServiceProvider //// +///////////////////////////// +libjuci::ApiServiceProvider::ApiServiceProvider( ){ + std::cout << "Apiservice std.ctor" << std::endl; +} +void libjuci::ApiServiceProvider::ReplaceWord(std::string word){ + std::cout << word << std::endl; +} +void libjuci::ApiServiceProvider::ReplaceLine(std::string line){} - void cpp::ReplaceLine(const std::string line) { - //TODO implement api::ReplaceLine / change string to iter? - //some_namespace::controller::replaceLine(line_); - std::cout << "unimplemented function: 'api::ReplaceLine()' called" - << std::endl; - } - - //helpers - boost::python::api::object py::openPythonScript(const std::string path, - boost::python::api::object python_name_space) { - std::string temp = g_project_root + path + ".py"; - boost::python::str the_path(temp); - return boost::python::exec_file(the_path, python_name_space);//, python_name_space); - } +void libjuci::ApiServiceProvider::AddKeybinding() { + + libjuci::ApiServiceProvider::menu_->keybindings_.action_group_menu() + ->add(Gtk::Action::create("PluginAddSnippet", + "Add snippet"), + Gtk::AccelKey("space"), + []() { + std::cout << "ctrl alt space" << std::endl; + libjuci::LoadPlugin("juci_api_test"); + }); + std::cout << "addkeybinding" << std::endl; +} + +/////////////////////// +//// Api to python //// +/////////////////////// +void libjuci::ReplaceWord(const std::string word_) { + //TODO implement libjuci::ReplaceWord / change string to iter? + //some_namespace::controller::replaceWord(word_*); + //std::cout << "unimplemented function: 'libjuci::ReplaceWord()' called" + // << std::endl; + + //libjuci::ApiServiceProvider::ReplaceWord(word); - void py::LoadPlugin(const std::string& plugin_name) { - try{ - /* initialize python interpreter */ - Py_Initialize(); - boost::python::api::object main_module = boost::python::import("__main__"); - boost::python::api::object main_namespace = main_module.attr("__dict__"); - - /* runs script from python */ - //boost::python::object ignored1 = setPythonVar("word", word, main_namespace); - boost::python::api::object ignored2 = openPythonScript(plugin_name, main_namespace); - /* extracts desired values */ - //boost::python::object pySnippet = boost::python::eval("getSnippet()", main_namespace); - //word = boost::python::extract(pySnippet); - /* add snippet to textView */ - //TODO add snippet - }catch(boost::python::error_already_set const&) { - PyErr_Print(); - } + std::cout << "The string: " << word_ << std::endl; +} + +void libjuci::ReplaceLine(const std::string line) { + //TODO implement libjuci::ReplaceLine / change string to iter? + //some_namespace::controller::replaceLine(line_); + std::cout << "unimplemented function: 'libjuci::ReplaceLine()' called" + << std::endl; +} +////////////////////////////// +//// Boost.Python methods //// +////////////////////////////// + +boost::python::api::object libjuci::openPythonScript(const std::string path, + boost::python::api::object python_name_space) { + std::string temp = g_project_root + path + ".py"; + boost::python::str the_path(temp); + return boost::python::exec_file(the_path, python_name_space);//, python_name_space); +} + + void libjuci::LoadPlugin(const std::string& plugin_name) { + try{ + /* initialize python interpreter */ + Py_Initialize(); + boost::python::api::object main_module = boost::python::import("__main__"); + boost::python::api::object main_namespace = main_module.attr("__dict__"); + + /* runs script from python */ + //boost::python::object ignored1 = setPythonVar("word", word, main_namespace); + boost::python::api::object ignored2 = openPythonScript(plugin_name, main_namespace); + /* extracts desired values */ + //boost::python::object pySnippet = boost::python::eval("getSnippet()", main_namespace); + //word = boost::python::extract(pySnippet); + /* add snippet to textView */ + //TODO add snippet + }catch(boost::python::error_already_set const&) { + PyErr_Print(); } } + diff --git a/juci/api.h b/juci/api.h index 9bd42c4..9f096a2 100644 --- a/juci/api.h +++ b/juci/api.h @@ -4,44 +4,43 @@ #include #include #include +#include "notebook.h" +#include "menu.h" -// Plugin API -// -const std::string g_project_root("/home/forgie/bachelor/app/juci/"); -static std::string g_test_string("test"); -namespace juci_api { - // - // calls from python to C++ - // - namespace cpp { +const std::string g_project_root("/home/forgie/app/juci/"); - - // Replaceword: - // replaces a word in the editor with a string - +namespace libjuci { + +///////////////////////////// +//// API ServiceProvider //// +///////////////////////////// + struct ApiServiceProvider { + public: + static std::shared_ptr menu_; + static std::shared_ptr notebook_; + + ApiServiceProvider(); + static void ReplaceWord(const std::string word); + void ReplaceLine(const std::string line); + static void AddKeybinding(); + }; + +/////////////////////// +//// Api to python //// +/////////////////////// void ReplaceWord(const std::string word_); - - // - // ReplaceLine: - // Replaces a line in the editor with a string - // - void ReplaceLine(const std::string line_); - - }//namespace cpp - // - // calls from C++ to Python - // - namespace py { - + void ReplaceLine(const std::string line_); - //helpers +////////////////////////////// +//// Boost.Python methods //// +////////////////////////////// boost::python::api::object openPythonScript(const std::string path, boost::python::api::object python_name_space); //void snippet(std::string& word); void LoadPlugin(const std::string& plugin_name); - }// py -}//juci_api + +}//api #endif // JUCI_API_H diff --git a/juci/api_ext.cc b/juci/api_ext.cc index 8ea43b2..2104278 100644 --- a/juci/api_ext.cc +++ b/juci/api_ext.cc @@ -1,7 +1,8 @@ #include "api.h" + BOOST_PYTHON_MODULE(juci_to_python_api) { using namespace boost::python; // text editing - def("replaceLine", &juci_api::cpp::ReplaceLine); - def("replaceWord", &juci_api::cpp::ReplaceWord); - }// module::juci + def("replaceLine", &libjuci::ReplaceLine); + def("replaceWord", &libjuci::ReplaceWord); + }// module::juci \ No newline at end of file diff --git a/juci/cmake/Modules/FindTestlcl.cmake b/juci/cmake/Modules/FindTestlcl.cmake index 526cdfe..c28720f 100644 --- a/juci/cmake/Modules/FindTestlcl.cmake +++ b/juci/cmake/Modules/FindTestlcl.cmake @@ -10,11 +10,11 @@ find_package(PkgConfig) find_path(LCL_INCLUDE_DIR headers/TranslationUnit.h - HINTS "/home/zalox/bachelor/libclang++/" + HINTS "/home/forgie/code/libclangpp/" ) find_library(LCL_LIBRARY NAMES testlcl - HINTS "/home/zalox/bachelor/libclang++/lib") + HINTS "/home/forgie/code/libclangpp/lib") set(LCL_LIBRARIES ${LCL_LIBRARY} ) set(LCL_INCLUDE_DIRS ${LCL_INCLUDE_DIR} ) diff --git a/juci/juci.cc b/juci/juci.cc index 68a06ad..84b1e6f 100644 --- a/juci/juci.cc +++ b/juci/juci.cc @@ -7,5 +7,7 @@ int main(int argc, char *argv[]) { argv, "no.sout.juci"); Window window; + + //api::LoadPlugin("juci_api_test"); return app->run(window); } diff --git a/juci/menu.cc b/juci/menu.cc index 8f7bd15..1121674 100644 --- a/juci/menu.cc +++ b/juci/menu.cc @@ -36,12 +36,12 @@ Menu::Controller::Controller(Keybindings::Controller& keybindings) : "_Plugins")); keybindings_.action_group_menu()->add(Gtk::Action::create("PluginSnippet", "Snippet")); - keybindings_.action_group_menu()->add(Gtk::Action::create("PluginAddSnippet", + /*keybindings_.action_group_menu()->add(Gtk::Action::create("PluginAddSnippet", "Add snippet"), Gtk::AccelKey("space"), [this]() { OnPluginAddSnippet(); - }); + });*/ keybindings_.action_group_menu()->add(Gtk::Action::create("HelpMenu", Gtk::Stock::HELP)); keybindings_.action_group_menu()->add(Gtk::Action::create("HelpAbout", @@ -62,8 +62,8 @@ Gtk::Box &Menu::Controller::view() { } void Menu::Controller::OnPluginAddSnippet() { //TODO(Forgi add you snippet magic code) - std::cout << "Add snipper" << std::endl; - juci_api::py::LoadPlugin("snippet"); + std::cout << "Add snippet" << std::endl; + //juci_api::py::LoadPlugin("snippet"); } void Menu::Controller::OnFileOpenFile() { std::cout << "Open file clicked" << std::endl; diff --git a/juci/menu.h b/juci/menu.h index 8e00bad..f743999 100644 --- a/juci/menu.h +++ b/juci/menu.h @@ -4,7 +4,6 @@ #include #include "gtkmm.h" #include "keybindings.h" -#include "api.h" namespace Menu { class View { @@ -18,7 +17,7 @@ namespace Menu { public: explicit Controller(Keybindings::Controller& keybindings); Gtk::Box &view(); - private: + Keybindings::Controller &keybindings_; View menu_view_; void OnFileNewEmptyfile(); diff --git a/juci/source.cc b/juci/source.cc index 7b12dc1..b93bf4a 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -92,7 +92,7 @@ void Source::Theme::SetTagTable( //// Model //// /////////////// Source::Model::Model() : - theme_() { + theme_() {/* std::cout << "Model constructor run" << std::endl; boost::property_tree::ptree pt; boost::property_tree::json_parser::read_json("config.json", pt); @@ -108,7 +108,7 @@ Source::Model::Model() : // std::cout << "inserting type. " << pi.first << pi.second.get_value() << std::endl; } } - } + }*/ } Source::Theme& Source::Model::theme() { diff --git a/juci/window.cc b/juci/window.cc index 5db176e..3919c47 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -17,6 +17,11 @@ Window::Window() : [this]() { OnOpenFile(); }); + + libjuci::ApiServiceProvider::menu_ = std::shared_ptr(&menu_); + libjuci::ApiServiceProvider::notebook_ = std::shared_ptr(¬ebook_); + libjuci::ApiServiceProvider::AddKeybinding(); + add_accel_group(keybindings_.ui_manager_menu()->get_accel_group()); add_accel_group(keybindings_.ui_manager_hidden()->get_accel_group()); @@ -25,7 +30,10 @@ Window::Window() : window_box_.pack_start(notebook_.view()); show_all_children(); } // Window constructor -void Window::OnWindowHide() { +void Window::OnWindowHide(){ + //TODO forgie: find out how to 'remove' the pointers + libjuci::ApiServiceProvider::notebook_ = std::shared_ptr(nullptr); + libjuci::ApiServiceProvider::menu_ = std::shared_ptr(nullptr); hide(); } diff --git a/juci/window.h b/juci/window.h index d002781..eac2412 100644 --- a/juci/window.h +++ b/juci/window.h @@ -3,14 +3,14 @@ #include #include "gtkmm.h" -#include "menu.h" -#include "notebook.h" +#include "api.h" +#include class Window : public Gtk::Window { public: Window(); Gtk::Box window_box_; -private: +//private: Keybindings::Controller keybindings_; Menu::Controller menu_; Notebook::Controller notebook_; @@ -21,3 +21,4 @@ private: }; #endif // JUCI_WINDOW_H_ + From 5f7837458b7817d6e272e59cf67747c931aeb892 Mon Sep 17 00:00:00 2001 From: tedjk Date: Fri, 20 Feb 2015 15:51:52 +0100 Subject: [PATCH 2/6] BAB-21 #comment Working example #time 2h --- juci/api.cc | 19 ++++++++++++++++--- juci/notebook.h | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index 196b1f8..bfffb34 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -10,7 +10,20 @@ libjuci::ApiServiceProvider::ApiServiceProvider( ){ std::cout << "Apiservice std.ctor" << std::endl; } void libjuci::ApiServiceProvider::ReplaceWord(std::string word){ - std::cout << word << std::endl; + + Glib::RefPtr buffer = libjuci::ApiServiceProvider::notebook_->source_vec_.back()->view().get_buffer(); + + Gtk::TextIter text_iterator = buffer->get_insert()->get_iter(); + Gtk::TextIter end = buffer->get_insert()->get_iter(); + + while( !text_iterator.starts_word()){ + // text_iterator--; + text_iterator.backward_char(); + } + buffer->erase(text_iterator, end); + text_iterator = buffer->get_insert()->get_iter(); + buffer->insert(text_iterator, word); + } void libjuci::ApiServiceProvider::ReplaceLine(std::string line){} @@ -37,8 +50,8 @@ void libjuci::ReplaceWord(const std::string word_) { // << std::endl; //libjuci::ApiServiceProvider::ReplaceWord(word); - - std::cout << "The string: " << word_ << std::endl; + libjuci::ApiServiceProvider::ReplaceWord(word_); + //std::cout << "The string: " << word_ << std::endl; } void libjuci::ReplaceLine(const std::string line) { diff --git a/juci/notebook.h b/juci/notebook.h index 7dfe02f..b619b48 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -24,7 +24,7 @@ namespace Notebook { void OnNewPage(std::string name); void OnCloseCurrentPage(); void OnOpenFile(std::string filename); - private: + // private: View view_; Entry::Controller entry_; std::vector source_vec_; From 50c8f559c84147abd61a9511087675ee1a537727 Mon Sep 17 00:00:00 2001 From: tedjk Date: Mon, 23 Feb 2015 08:21:40 +0100 Subject: [PATCH 3/6] BAB-21 #comment Added function for replacing a word in the current document #time 5h --- juci/api.cc | 100 ++++++++++++++++++++++++++++++++++-------------- juci/api.h | 52 ++++++++++++++++--------- juci/api_ext.cc | 3 +- 3 files changed, 106 insertions(+), 49 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index bfffb34..a0625a1 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -10,33 +10,47 @@ libjuci::ApiServiceProvider::ApiServiceProvider( ){ std::cout << "Apiservice std.ctor" << std::endl; } void libjuci::ApiServiceProvider::ReplaceWord(std::string word){ + Glib::RefPtr buffer = libjuci::BufferFromNotebook(); + Gtk::TextIter word_start = libjuci::IterFromNotebook(); + Gtk::TextIter word_end = libjuci::IterFromNotebook(); - Glib::RefPtr buffer = libjuci::ApiServiceProvider::notebook_->source_vec_.back()->view().get_buffer(); + libjuci::IterToWordStart(word_start); + libjuci::IterToWordEnd(word_end); + if(word_start != word_end) { + buffer->erase(word_start, word_end); + Gtk::TextIter current = libjuci::IterFromNotebook(); + buffer->insert(current, word); + } +} +void libjuci::ApiServiceProvider::ReplaceLine(std::string line){} - Gtk::TextIter text_iterator = buffer->get_insert()->get_iter(); - Gtk::TextIter end = buffer->get_insert()->get_iter(); +std::string libjuci::ApiServiceProvider::GetWord(){ + Glib::RefPtr buffer = libjuci::BufferFromNotebook(); + Gtk::TextIter word_start = libjuci::IterFromNotebook(); + Gtk::TextIter word_end = libjuci::IterFromNotebook(); - while( !text_iterator.starts_word()){ - // text_iterator--; - text_iterator.backward_char(); - } - buffer->erase(text_iterator, end); - text_iterator = buffer->get_insert()->get_iter(); - buffer->insert(text_iterator, word); + libjuci::IterToWordStart(word_start); + libjuci::IterToWordEnd(word_end); + std::cout <<"start: " << word_start << std::endl << "end: " << word_end << std::endl; + if(word_start < word_end) { + std::string test = buffer->get_text(word_start, word_end); + std::cout << "test: " << test << std::endl; + return test; + } + return "no word, sry"; } -void libjuci::ApiServiceProvider::ReplaceLine(std::string line){} void libjuci::ApiServiceProvider::AddKeybinding() { libjuci::ApiServiceProvider::menu_->keybindings_.action_group_menu() - ->add(Gtk::Action::create("PluginAddSnippet", - "Add snippet"), - Gtk::AccelKey("space"), - []() { - std::cout << "ctrl alt space" << std::endl; - libjuci::LoadPlugin("juci_api_test"); - }); + ->add(Gtk::Action::create("PluginAddSnippet", + "Add snippet"), + Gtk::AccelKey("space"), + []() { + std::cout << "ctrl alt space" << std::endl; + libjuci::LoadPlugin("snippet"); + }); std::cout << "addkeybinding" << std::endl; } @@ -44,34 +58,32 @@ void libjuci::ApiServiceProvider::AddKeybinding() { //// Api to python //// /////////////////////// void libjuci::ReplaceWord(const std::string word_) { - //TODO implement libjuci::ReplaceWord / change string to iter? - //some_namespace::controller::replaceWord(word_*); - //std::cout << "unimplemented function: 'libjuci::ReplaceWord()' called" - // << std::endl; - - //libjuci::ApiServiceProvider::ReplaceWord(word); libjuci::ApiServiceProvider::ReplaceWord(word_); - //std::cout << "The string: " << word_ << std::endl; } void libjuci::ReplaceLine(const std::string line) { - //TODO implement libjuci::ReplaceLine / change string to iter? + //TODO forgie: implement libjuci::ReplaceLine / change string to iter? //some_namespace::controller::replaceLine(line_); std::cout << "unimplemented function: 'libjuci::ReplaceLine()' called" - << std::endl; + << std::endl; +} +std::string libjuci::GetWord() { + // boost::python::str converted(libjuci::ApiServiceProvider::GetWord() ); + return libjuci::ApiServiceProvider::GetWord(); + // return converted; } ////////////////////////////// //// Boost.Python methods //// ////////////////////////////// boost::python::api::object libjuci::openPythonScript(const std::string path, - boost::python::api::object python_name_space) { + boost::python::api::object python_name_space) { std::string temp = g_project_root + path + ".py"; boost::python::str the_path(temp); return boost::python::exec_file(the_path, python_name_space);//, python_name_space); } - void libjuci::LoadPlugin(const std::string& plugin_name) { +void libjuci::LoadPlugin(const std::string& plugin_name) { try{ /* initialize python interpreter */ Py_Initialize(); @@ -91,3 +103,33 @@ boost::python::api::object libjuci::openPythonScript(const std::string path, } } +/////////////////////// +//// Glib wrappers //// +/////////////////////// +void libjuci::IterToWordStart(Gtk::TextIter &iter) { + if(!iter.starts_line()) { + while(!iter.starts_word()) { + iter.backward_char(); + } + } +} +void libjuci::IterToWordEnd(Gtk::TextIter &iter) { + if(!iter.ends_line()) { + while(!iter.ends_word()) { + iter.forward_char(); + } + } +} + +Glib::RefPtr libjuci::BufferFromNotebook() { + return Glib::RefPtr(libjuci::ApiServiceProvider::notebook_->source_vec_.back()->view().get_buffer()); +} + +Gtk::TextIter libjuci::IterFromNotebook() { + // Glib::RefPtr buffer = libjuci::ApiServiceProvider::notebook_ + //->source_vec_.back()->view().get_buffer(); + return libjuci::BufferFromNotebook()->get_insert()->get_iter(); +} + + + diff --git a/juci/api.h b/juci/api.h index 9f096a2..a4b96cd 100644 --- a/juci/api.h +++ b/juci/api.h @@ -11,36 +11,50 @@ const std::string g_project_root("/home/forgie/app/juci/"); namespace libjuci { -///////////////////////////// -//// API ServiceProvider //// -///////////////////////////// + ///////////////////////////// + //// API ServiceProvider //// + ///////////////////////////// struct ApiServiceProvider { public: static std::shared_ptr menu_; static std::shared_ptr notebook_; + static std::string text; + ApiServiceProvider(); + static std::string GetWord(); static void ReplaceWord(const std::string word); - void ReplaceLine(const std::string line); + static void ReplaceLine(const std::string line); + static void AddKeybinding(); }; - -/////////////////////// -//// Api to python //// -/////////////////////// - void ReplaceWord(const std::string word_); - - void ReplaceLine(const std::string line_); - -////////////////////////////// -//// Boost.Python methods //// -////////////////////////////// - boost::python::api::object openPythonScript(const std::string path, - boost::python::api::object python_name_space); + /////////////////////// + //// Glib wrappers //// + /////////////////////// + + void IterToWordStart(Gtk::TextIter &iter); + void IterToWordEnd(Gtk::TextIter &iter); + Gtk::TextIter IterFromNotebook(); + Glib::RefPtr BufferFromNotebook(); + + /////////////////////// + //// Api to python //// + /////////////////////// + void ReplaceWord(const std::string word_); + + void ReplaceLine(const std::string line_); + + std::string GetWord(); + + ////////////////////////////// + //// Boost.Python methods //// + ////////////////////////////// + boost::python::api::object openPythonScript(const std::string path, + boost::python::api::object python_name_space); - //void snippet(std::string& word); + //void snippet(std::string& word); - void LoadPlugin(const std::string& plugin_name); + void LoadPlugin(const std::string& plugin_name); }//api #endif // JUCI_API_H diff --git a/juci/api_ext.cc b/juci/api_ext.cc index 2104278..8b1bb2b 100644 --- a/juci/api_ext.cc +++ b/juci/api_ext.cc @@ -5,4 +5,5 @@ BOOST_PYTHON_MODULE(juci_to_python_api) { // text editing def("replaceLine", &libjuci::ReplaceLine); def("replaceWord", &libjuci::ReplaceWord); - }// module::juci \ No newline at end of file + def("getWord", &libjuci::GetWord); + }// module::juci From 477c5a1e1a6e2854dbffdc3d7c8f0697cbc3d1d1 Mon Sep 17 00:00:00 2001 From: tedjk Date: Mon, 23 Feb 2015 08:39:16 +0100 Subject: [PATCH 4/6] BAB-21 #comment removed test-folder #time 2m --- juci/plugin/CMakeLists.txt | 57 ------------------ juci/plugin/plugin.cc | 118 ------------------------------------- juci/plugin/plugin.h | 28 --------- juci/plugin/plugin_ext.cc | 8 --- juci/plugin/randomtext.txt | 39 ------------ 5 files changed, 250 deletions(-) delete mode 100644 juci/plugin/CMakeLists.txt delete mode 100644 juci/plugin/plugin.cc delete mode 100644 juci/plugin/plugin.h delete mode 100644 juci/plugin/plugin_ext.cc delete mode 100644 juci/plugin/randomtext.txt diff --git a/juci/plugin/CMakeLists.txt b/juci/plugin/CMakeLists.txt deleted file mode 100644 index 2b05816..0000000 --- a/juci/plugin/CMakeLists.txt +++ /dev/null @@ -1,57 +0,0 @@ -cmake_minimum_required (VERSION 2.4) -set(project_name plugin) -project (${project_name}) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") - -include(FindPkgConfig) - -#Boost_FOUND - True if headers and requested libraries were found -#Boost_INCLUDE_DIRS - Boost include directories -#Boost_LIBRARY_DIRS - Link directories for Boost libraries -#Boost_LIBRARIES - Boost component libraries to be linked -find_package(Boost 1.55 REQUIRED COMPONENTS python system ) - -if(!${Boost_FOUND}) - message(FATAL_ERROR "Boost libraries not found") -endif() - -#GTKMM_FOUND - True if headers and requested libraries were found -#GTKMM_INCLUDE_DIRS - GTKMM include directories -#GTKMM_LIBRARY_DIRS - Link directories for GTKMM libraries -#GTKMM_LIBRARIES - GTKMM component libraries to be linked -pkg_check_modules(GTKMM REQUIRED gtkmm-3.0) - -if(!${GTKMM_FOUND}) - message(FATAL_ERROR "Gtkmm not found") -endif() - -#PYTHONLIBS_FOUND - True if headers and requested libraries were found -#PYTHON_INCLUDE_DIRS - GTKMM include directories -#PYTHON_LIBRARIES - GTKMM component libraries to be linked -#$PYTHONLIBS_VERSION_STRING - ersion string -find_package(PythonLibs 2.7 REQUIRED) - -if(!${PYTHONLIBS_FOUND}) - message(FATAL_ERROR "Pythonlibs not found") -else() - message("Found python libs ${PYTHONLIBS_VERSION_STRING}") -endif() - -ADD_LIBRARY(plugintest_ext MODULE - plugin.h - plugin.cc - plugin_ext.cc - ) - -set_target_properties(plugintest_ext PROPERTIES PREFIX "") -target_link_libraries(plugintest_ext ${Boost_LIBRARIES}) - -add_executable(${project_name} - plugin.h - plugin.cc - ) - -include_directories(${PYTHON_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS}) -link_directories(${GTKMM_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS} ${PYTHON_LIBRARIES}) -target_link_libraries(${project_name} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${GTKMM_LIBRARIES}) \ No newline at end of file diff --git a/juci/plugin/plugin.cc b/juci/plugin/plugin.cc deleted file mode 100644 index 1a76f57..0000000 --- a/juci/plugin/plugin.cc +++ /dev/null @@ -1,118 +0,0 @@ -#include "plugin.h" - -//////////////////////////////////////////////////////////////////////////////// -// -// setPythonVar: -// initiates a string value (k_var_value) to a declared variable name -// (k_var_name) within a given namespace(python_name_space) -// -//////////////////////////////////////////////////////////////////////////////// - -bp::object Plugin::setPythonVar(const std::string k_var_name, - const std::string k_var_value, - bp::object python_name_space ) { - std::string temp = k_var_name + " = \"" + k_var_value + "\""; - bp::str the_var(temp); - return bp::exec(the_var, python_name_space); -} - -//////////////////////////////////////////////////////////////////////////////// -// -// openPythonScript: -// Opens a python plugin script within a file path and a python namespace -// -//////////////////////////////////////////////////////////////////////////////// - -bp::object Plugin::openPythonScript(const std::string path, - bp::object python_name_space) { - std::string temp = g_project_root + "plugins/" + path + "/" + path + ".py"; - bp::str the_path(temp); - return bp::exec_file(the_path, python_name_space); -} - -//////////////////////////////////////////////////////////////////////////////// -// -// for testing purposes only -// -//////////////////////////////////////////////////////////////////////////////// - -std::string Plugin::get_test_value2(){ - return "STRING FROM WITHIN C++"; -} - -/* public functions */ - - -//////////////////////////////////////////////////////////////////////////////// -// -// snippet: -// takes a std::string and converts it into a matching snippet -// if no matching snippet, returns the same string -// -//////////////////////////////////////////////////////////////////////////////// - -void Plugin::snippet(std::string& word){ - try{ - /* initialize python interpreter */ - Py_Initialize(); - bp::object main_module = bp::import("__main__"); - bp::object main_namespace = main_module.attr("__dict__"); - - /* runs script from python */ - bp::object ignored1 = setPythonVar("word", word, main_namespace); - bp::object ignored2 = openPythonScript(__func__, main_namespace); - - /* extracts desired values */ - bp::object pySnippet = bp::eval("getSnippet()", main_namespace); - word = bp::extract(pySnippet); - /* add snippet to textView */ - //TODO add snippet - - }catch(bp::error_already_set const&){ - PyErr_Print(); - } -} - -//////////////////////////////////////////////////////////////////////////////// -// -// get_test_value: -// for testing purposes -// uses a python module generated from c++ -// calls c++ function from python -// returns string to c++ from python -// prints the string from python within c++ -// -//////////////////////////////////////////////////////////////////////////////// - - void Plugin::get_test_value(){ - try{ - /* initialize python interpreter */ - Py_Initialize(); - bp::object main_module = bp::import("__main__"); - bp::object main_namespace = main_module.attr("__dict__"); - - /* runs script from python */ - const std::string path("test"); - bp::object ignored2 = openPythonScript(path, main_namespace); - - /* extracts desired values */ - bp::object pySnippet = bp::eval("get_test_value()", main_namespace); - std::string mword = bp::extract(pySnippet); - /* add snippet to textView */ - std::cout << mword << std::endl; - //TODO add snippet - - }catch(bp::error_already_set const&){ - PyErr_Print(); - } - } - -int main(int argc, char *argv[]) -{ - std::string word("ifelse"); - Plugin::snippet(word); - //Plugin::get_test_value(); - std::cout << word << std::endl; - return 0; - -} diff --git a/juci/plugin/plugin.h b/juci/plugin/plugin.h deleted file mode 100644 index 0c6e1aa..0000000 --- a/juci/plugin/plugin.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef JUCI_PLUGIN_H_ -#define JUCI_PLUGIN_H_ - -#include -#include - -#include -#include - -namespace bp = boost::python; - -const std::string g_project_root("~/q6/testing/plugin/"); -//TODO (forgie) get current working directory.. - -class Plugin{ - public: - static void snippet(std::string& word); - static void get_test_value(); - static std::string get_test_value2(); - private: - static bp::object setPythonVar(const std::string varName, - const std::string varValue, - bp::object python_name_space); - static bp::object openPythonScript(const std::string path, - bp::object python_name_space); -}; - -#endif // JUCI_PLUGIN_H_ diff --git a/juci/plugin/plugin_ext.cc b/juci/plugin/plugin_ext.cc deleted file mode 100644 index 5067e10..0000000 --- a/juci/plugin/plugin_ext.cc +++ /dev/null @@ -1,8 +0,0 @@ -#include "plugin.h" -#include -using namespace boost::python; - -BOOST_PYTHON_MODULE(plugintest_ext) -{ - def("get_test_value2", &Plugin::get_test_value2); -} diff --git a/juci/plugin/randomtext.txt b/juci/plugin/randomtext.txt deleted file mode 100644 index 4bbb4f6..0000000 --- a/juci/plugin/randomtext.txt +++ /dev/null @@ -1,39 +0,0 @@ -/* -void theprogram() -{ - std::string input; - std::cout << "Enter string to transform: "; - std::getline(std::cin, input); - //std::string inttransformed = CallPythonPlugIn(input); - std::cout << "The transformed string is: " << input << std::endl; -} - -int test1() -{ - try{ - Py_Initialize(); - object main_module = import("__main__"); - object main_namespace = main_module.attr("__dict__"); - object ignored = exec("result = 5 ** 2", main_namespace); - int five_squared = extract(main_namespace["result"]); - - printf("%d\n", five_squared); - - return five_squared; - } - catch(error_already_set const&){ - PyErr_Print(); - } - - return -1; -} - -void test2(int theNum, int &test) -{ - int seven = 7; - - test = 7+ theNum; - - std::cout << theNum << std::endl; -} -*/ From 07f5a50ed3f0a71c18cccd0de666d016791f23b5 Mon Sep 17 00:00:00 2001 From: tedjk Date: Mon, 23 Feb 2015 08:50:53 +0100 Subject: [PATCH 5/6] Added python files to .gitignore --- .gitignore | 3 ++- juci/snippet.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 juci/snippet.py diff --git a/.gitignore b/.gitignore index c37aa89..c30b100 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ !*.cc !*.h !CMakeLists.txt -!config.json \ No newline at end of file +!config.json +!*.py diff --git a/juci/snippet.py b/juci/snippet.py new file mode 100644 index 0000000..0013bd7 --- /dev/null +++ b/juci/snippet.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +#snippet plugin +import juci_to_python_api + +print("From Python: Snippet run") + +snippets = {} +snippets["hoy"] = "hoyvalue" +snippets["for"] = """\ +for(#int i=0; #i<#v.size(); #i++) { + std::cout << v[i] << std::endl; +} +""" +snippets["if"] = """\ +if(#) { + # +} +""" +snippets["ifelse"] = """\ +if(#) { + # +} else { + # +} +""" +snippets["while"] = """\ +while(#) { + # +} +""" +snippets["main"] = """\ +int main(int argc, char *argv[]) { + //Do something +} +""" +snippets["hello"] = """\ +#include + +int main(int argc, char *argv[]) { + std::cout << "Hello, world! << std::endl; +} +""" +def getSnippet(word): + try: + output = snippets[word] + except KeyError: + output = word + return output + +theWord=juci_to_python_api.getWord() +output=getSnippet(theWord) + +print(theWord) +juci_to_python_api.replaceWord(output) From b0b2400f5f1786af6c82abbdedc0ff4fa5d98a2e Mon Sep 17 00:00:00 2001 From: tedjk Date: Mon, 23 Feb 2015 09:14:52 +0100 Subject: [PATCH 6/6] BAB-21 #comment code-cleanup #time 15m --- juci/api.cc | 37 ++++++++++++++++++------------------- juci/api.h | 15 +++++---------- juci/api_ext.cc | 3 ++- juci/snippet.py | 4 ---- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index a0625a1..ee38351 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -6,10 +6,11 @@ std::shared_ptr libjuci::ApiServiceProvider::notebook_; ///////////////////////////// //// API ServiceProvider //// ///////////////////////////// -libjuci::ApiServiceProvider::ApiServiceProvider( ){ +libjuci::ApiServiceProvider::ApiServiceProvider( ) { std::cout << "Apiservice std.ctor" << std::endl; } -void libjuci::ApiServiceProvider::ReplaceWord(std::string word){ + +void libjuci::ApiServiceProvider::ReplaceWord(std::string word) { Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); @@ -22,9 +23,13 @@ void libjuci::ApiServiceProvider::ReplaceWord(std::string word){ buffer->insert(current, word); } } -void libjuci::ApiServiceProvider::ReplaceLine(std::string line){} -std::string libjuci::ApiServiceProvider::GetWord(){ +void libjuci::ApiServiceProvider::ReplaceLine(std::string line) { + std::cout << "Unimplemented function: ApiServiceProvider::ReplaceLine(string)" + << std::endl; +} + +std::string libjuci::ApiServiceProvider::GetWord() { Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); @@ -34,11 +39,10 @@ std::string libjuci::ApiServiceProvider::GetWord(){ std::cout <<"start: " << word_start << std::endl << "end: " << word_end << std::endl; if(word_start < word_end) { - std::string test = buffer->get_text(word_start, word_end); - std::cout << "test: " << test << std::endl; - return test; + std::string word = buffer->get_text(word_start, word_end); + return word; } - return "no word, sry"; + return ""; } void libjuci::ApiServiceProvider::AddKeybinding() { @@ -85,19 +89,15 @@ boost::python::api::object libjuci::openPythonScript(const std::string path, void libjuci::LoadPlugin(const std::string& plugin_name) { try{ - /* initialize python interpreter */ Py_Initialize(); boost::python::api::object main_module = boost::python::import("__main__"); boost::python::api::object main_namespace = main_module.attr("__dict__"); - - /* runs script from python */ - //boost::python::object ignored1 = setPythonVar("word", word, main_namespace); boost::python::api::object ignored2 = openPythonScript(plugin_name, main_namespace); - /* extracts desired values */ - //boost::python::object pySnippet = boost::python::eval("getSnippet()", main_namespace); - //word = boost::python::extract(pySnippet); - /* add snippet to textView */ - //TODO add snippet + + /* for extracting return value from python: */ + //boost::python::object returnValue = boost::python::eval("function_name()", main_namespace); + //std::string return_value = boost::python::extract(pySnippet); + //do something with return_value }catch(boost::python::error_already_set const&) { PyErr_Print(); } @@ -113,6 +113,7 @@ void libjuci::IterToWordStart(Gtk::TextIter &iter) { } } } + void libjuci::IterToWordEnd(Gtk::TextIter &iter) { if(!iter.ends_line()) { while(!iter.ends_word()) { @@ -126,8 +127,6 @@ Glib::RefPtr libjuci::BufferFromNotebook() { } Gtk::TextIter libjuci::IterFromNotebook() { - // Glib::RefPtr buffer = libjuci::ApiServiceProvider::notebook_ - //->source_vec_.back()->view().get_buffer(); return libjuci::BufferFromNotebook()->get_insert()->get_iter(); } diff --git a/juci/api.h b/juci/api.h index a4b96cd..69e503c 100644 --- a/juci/api.h +++ b/juci/api.h @@ -18,20 +18,20 @@ namespace libjuci { public: static std::shared_ptr menu_; static std::shared_ptr notebook_; - static std::string text; ApiServiceProvider(); + static void AddKeybinding(); + + //for Python module: static std::string GetWord(); static void ReplaceWord(const std::string word); static void ReplaceLine(const std::string line); - - static void AddKeybinding(); }; + /////////////////////// //// Glib wrappers //// /////////////////////// - void IterToWordStart(Gtk::TextIter &iter); void IterToWordEnd(Gtk::TextIter &iter); Gtk::TextIter IterFromNotebook(); @@ -41,9 +41,7 @@ namespace libjuci { //// Api to python //// /////////////////////// void ReplaceWord(const std::string word_); - void ReplaceLine(const std::string line_); - std::string GetWord(); ////////////////////////////// @@ -51,10 +49,7 @@ namespace libjuci { ////////////////////////////// boost::python::api::object openPythonScript(const std::string path, boost::python::api::object python_name_space); - - //void snippet(std::string& word); - void LoadPlugin(const std::string& plugin_name); -}//api +}//libjuci #endif // JUCI_API_H diff --git a/juci/api_ext.cc b/juci/api_ext.cc index 8b1bb2b..2887831 100644 --- a/juci/api_ext.cc +++ b/juci/api_ext.cc @@ -6,4 +6,5 @@ BOOST_PYTHON_MODULE(juci_to_python_api) { def("replaceLine", &libjuci::ReplaceLine); def("replaceWord", &libjuci::ReplaceWord); def("getWord", &libjuci::GetWord); - }// module::juci + //something more + }// module::juci_to_python_api diff --git a/juci/snippet.py b/juci/snippet.py index 0013bd7..96c420c 100644 --- a/juci/snippet.py +++ b/juci/snippet.py @@ -2,10 +2,7 @@ #snippet plugin import juci_to_python_api -print("From Python: Snippet run") - snippets = {} -snippets["hoy"] = "hoyvalue" snippets["for"] = """\ for(#int i=0; #i<#v.size(); #i++) { std::cout << v[i] << std::endl; @@ -50,5 +47,4 @@ def getSnippet(word): theWord=juci_to_python_api.getWord() output=getSnippet(theWord) -print(theWord) juci_to_python_api.replaceWord(output)