diff --git a/juci/config.json b/juci/config.json index e4ff1f5..68bb33e 100644 --- a/juci/config.json +++ b/juci/config.json @@ -23,7 +23,8 @@ "new_h_file": "h", "new_cc_file": "c", "close_tab": "w", - "open_folder": "o" + "open_folder": "o", + "edit_undo": "z" }, "directoryfilter": { "ignore": [ diff --git a/juci/menu.xml b/juci/menu.xml index 930c460..f427348 100644 --- a/juci/menu.xml +++ b/juci/menu.xml @@ -16,7 +16,8 @@ - + + @@ -26,6 +27,6 @@ - + diff --git a/juci/notebook.cc b/juci/notebook.cc index 695591b..6ba31f7 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -93,6 +93,16 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller [this]() { OnEditPaste(); }); + + keybindings.action_group_menu()-> + add(Gtk::Action::create("EditUndo", + "Undo"), + Gtk::AccelKey(keybindings.config_ + .key_map()["edit_undo"]), + [this]() { + OnUndo(); + }); + keybindings.action_group_hidden()-> add(Gtk::Action::create("EditPaste", Gtk::Stock::PASTE), @@ -219,6 +229,7 @@ void Notebook::Controller::OnNewPage(std::string name) { Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); Notebook().set_focus_child(text_vec_.at(Pages()-1)->view()); + NewBufferHistory(text_vec_.back()->view().get_buffer()); } void Notebook::Controller::OnOpenFile(std::string path) { @@ -232,6 +243,9 @@ void Notebook::Controller::OnOpenFile(std::string path) { std::cout << "current page set" << std::endl; Notebook().set_focus_child(text_vec_.back()->view()); OnBufferChange(); + NewBufferHistory(text_vec_.back()->view().get_buffer()); + + } void Notebook::Controller::OnCreatePage() { @@ -383,13 +397,15 @@ void Notebook::Controller::OnBufferChange() { delete scroll; } Gtk::TextIter start, end; - std::string word; + std::string word, last_word; start = Buffer(text_vec_.at(page))->get_insert()->get_iter(); end = Buffer(text_vec_.at(page))->get_insert()->get_iter(); start.backward_char(); word = Buffer(text_vec_.at(page))->get_text(start, end); + last_word = Buffer(text_vec_.at(page))->get_text(--start, --end); if (word == ".") { // TODO(Forgie) Zalox,Forgie) Remove TEST + UpdateHistory(); std::vector TEST; TEST.push_back("toString()"); TEST.push_back("toLower()"); @@ -446,5 +462,93 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr [this]() { OnBufferChange(); }); + buffer->signal_end_user_action().connect( + [this]() { + UpdateHistory(); + }); +} + + +// History methods +void Notebook::Controller:: +NewBufferHistory(Glib::RefPtr buffer) { + Glib::ustring text = buffer->get_text(); + std::deque queue; + queue.push_back(text); + history_.push_back(queue); } +void Notebook::Controller::UpdateHistory() { + Gtk::TextIter start, end; + std::string word, last_word; + int page = CurrentPage(); + start = Buffer(text_vec_.at(page))->get_insert()->get_iter(); + end = Buffer(text_vec_.at(page))->get_insert()->get_iter(); + start.backward_char(); + word = Buffer(text_vec_.at(page))->get_text(start, end); + last_word = Buffer(text_vec_.at(page))->get_text(--start, --end); + /*if(word == "." + || word == " " + || word == ";" + || word == ":" + || word == "}" + || word == ")" + || word == "]" + || word == ">") { + if(last_word != "." + && last_word != " " + && last_word != ";" + && last_word != ":" + && last_word != "}" + && last_word != ")" + && last_word != "]" + && last_word != ">") {*/ + AppendBufferState(); + // } + // } +} +void Notebook::Controller:: +AppendBufferState() { + Glib::ustring text = CurrentTextView().get_buffer()->get_text(); + std::cout << "buf.size(): " << text.size() << std::endl; + if(BufferHistory().size() < kHistorySize) { + BufferHistory().push_back(text); + } else { + BufferHistory().pop_front(); + BufferHistory().push_back(text); + } +} + +void Notebook::Controller::RemoveBufferHistory() { + history_.erase(history_.begin()+CurrentPage()); +} + +std::deque& Notebook::Controller::BufferHistory() { + return history_.at(CurrentPage()); +} + +Glib::ustring& Notebook::Controller::LastBufferState() { + if(BufferHistory().size() > 1) { + BufferHistory().pop_back(); + }else { + std::cout << "Reached end of history, can't undo any more" << std::endl; + } + return BufferHistory().back(); +} + +void Notebook::Controller::OnUndo() { + // PrintQue(); + // std::cout << "UNDOING SOMETHING TERRIBLE" << std::endl; + Glib::RefPtr buf = CurrentTextView().get_buffer(); + buf->set_text(LastBufferState()); + std::cout << "Undoing.."<< std::endl; + PrintQue(); +} + +void Notebook::Controller::PrintQue(){ + int size = BufferHistory().back().size(); + std::cout << "buffer size: "<< size << std::endl + << "buffer #: " << CurrentPage() << std::endl + << "historylength: "<< BufferHistory().size() << std::endl; + } +//TODO remove first history event after open file diff --git a/juci/notebook.h b/juci/notebook.h index 4a657fe..d0a8fad 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -9,6 +9,7 @@ #include #include #include +#include namespace Notebook { class Model { @@ -69,7 +70,19 @@ namespace Notebook { protected: void BufferChangeHandler(Glib::RefPtr buffer); + private: + std::vector> history_; + const int kHistorySize = 30; + void NewBufferHistory(Glib::RefPtr buffer); + void RemoveBufferHistory(); + void UpdateHistory(); + void AppendBufferState(); + std::deque& BufferHistory(); + void PrintQue(); + Glib::ustring& LastBufferState(); + void OnUndo(); + void CreateKeybindings(Keybindings::Controller& keybindings); Glib::RefPtr m_refBuilder; Glib::RefPtr refActionGroup; @@ -79,6 +92,7 @@ namespace Notebook { Model model_; bool is_new_file_; Entry::Controller entry_; + std::vector text_vec_, linenumbers_vec_; std::vector scrolledtext_vec_, scrolledline_vec_; std::vector editor_vec_;