From 50c8f559c84147abd61a9511087675ee1a537727 Mon Sep 17 00:00:00 2001 From: tedjk Date: Mon, 23 Feb 2015 08:21:40 +0100 Subject: [PATCH] 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