From a1a0d9270f021b384eebb950908483b12f01ce48 Mon Sep 17 00:00:00 2001 From: d3rrial Date: Fri, 9 Sep 2016 11:30:38 +0200 Subject: [PATCH 1/3] added rudimentary file_reload to menu --- src/menu.cc | 6 ++++++ src/window.cc | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/menu.cc b/src/menu.cc index ac6a464..def47c8 100644 --- a/src/menu.cc +++ b/src/menu.cc @@ -61,6 +61,12 @@ Menu::Menu() { app.open_folder +
+ + _Reload _File + app.reload_file + +
_Save diff --git a/src/window.cc b/src/window.cc index 521687e..bca6214 100644 --- a/src/window.cc +++ b/src/window.cc @@ -319,6 +319,13 @@ void Window::set_menu_actions() { Directories::get().open(path); }); + menu.add_action("reload_file", [this]() { + auto path = Notebook::get().get_current_view()->file_path; + std::cout<<"Path: "< Date: Fri, 9 Sep 2016 11:34:52 +0200 Subject: [PATCH 2/3] removed the std::cout<< path debug line --- src/window.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/window.cc b/src/window.cc index bca6214..0f33d75 100644 --- a/src/window.cc +++ b/src/window.cc @@ -321,7 +321,6 @@ void Window::set_menu_actions() { menu.add_action("reload_file", [this]() { auto path = Notebook::get().get_current_view()->file_path; - std::cout<<"Path: "< Date: Thu, 29 Sep 2016 13:15:26 +0200 Subject: [PATCH 3/3] Added reload functionality by re-writing the textbuffer --- src/notebook.cc | 32 ++++++++++++++++++++++++++++++++ src/notebook.h | 1 + src/window.cc | 3 +-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/notebook.cc b/src/notebook.cc index 509a1d0..d6028d8 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -128,6 +128,38 @@ std::vector &Notebook::get_views() { return source_views; } +void Notebook::reload(const boost::filesystem::path &file_path, size_t notebook_index) { + if(boost::filesystem::exists(file_path)) { + std::ifstream can_read(file_path.string()); + if(!can_read) { + Terminal::get().print("Error: could not open "+file_path.string()+"\n", true); + return; + } + can_read.close(); + } + + auto last_view=get_current_view(); + int offset = last_view->get_buffer()->get_insert()->get_iter().get_offset(); + int line = last_view->get_buffer()->get_insert()->get_iter().get_line(); + + auto language=Source::guess_language(file_path); + last_view->get_buffer()->erase(last_view->get_buffer()->begin(), last_view->get_buffer()->end()); + last_view->get_source_buffer()->begin_not_undoable_action(); + if(language) { + if(filesystem::read_non_utf8(file_path, last_view->get_buffer())==-1) + Terminal::get().print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n"); + } + else { + if(filesystem::read(file_path, last_view->get_buffer())==-1) + Terminal::get().print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true); + } + + last_view->get_source_buffer()->end_not_undoable_action(); + + last_view->place_cursor_at_line_offset(line, offset); + last_view->get_buffer()->set_modified(true); +} + void Notebook::open(const boost::filesystem::path &file_path, size_t notebook_index) { if(notebook_index==1 && !split) toggle_split(); diff --git a/src/notebook.h b/src/notebook.h index e35f98e..b0f7295 100644 --- a/src/notebook.h +++ b/src/notebook.h @@ -31,6 +31,7 @@ public: std::vector &get_views(); void open(const boost::filesystem::path &file_path, size_t notebook_index=-1); + void reload(const boost::filesystem::path &file_path, size_t notebook_index=-1); void configure(size_t index); bool save(size_t index); bool save_current(); diff --git a/src/window.cc b/src/window.cc index 0f33d75..30ef105 100644 --- a/src/window.cc +++ b/src/window.cc @@ -321,8 +321,7 @@ void Window::set_menu_actions() { menu.add_action("reload_file", [this]() { auto path = Notebook::get().get_current_view()->file_path; - Notebook::get().close_current(); - Notebook::get().open(path); + Notebook::get().reload(path); }); menu.add_action("save", [this]() {