diff --git a/CMakeLists.txt b/CMakeLists.txt index 477b55b..d05fa6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required (VERSION 2.8.8) project(juci) -set(JUCI_VERSION "1.2.1.7") +set(JUCI_VERSION "1.2.1.8") set(CPACK_PACKAGE_NAME "jucipp") set(CPACK_PACKAGE_CONTACT "Ole Christian Eidheim ") diff --git a/src/files.h b/src/files.h index 4b53c0e..e399b65 100644 --- a/src/files.h +++ b/src/files.h @@ -86,6 +86,7 @@ R"RAW( "new_folder": "n", "open_file": "o", "open_folder": "o", + "reload_file": "", "save": "s", "save_as": "s", "print": "p", diff --git a/src/notebook.cc b/src/notebook.cc index af36c47..ebb35e1 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -128,38 +128,6 @@ 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(); @@ -552,20 +520,7 @@ bool Notebook::close(size_t index) { if(on_close_page) on_close_page(view); - size_t cursor_locations_index=0; - for(auto it=cursor_locations.begin();it!=cursor_locations.end();) { - if(it->view==view) { - it=cursor_locations.erase(it); - if(current_cursor_location!=static_cast(-1) && current_cursor_location>cursor_locations_index) - --current_cursor_location; - } - else { - ++it; - ++cursor_locations_index; - } - } - if(current_cursor_location>=cursor_locations.size()) - current_cursor_location=cursor_locations.size()-1; + delete_cursor_locations(view); if(auto clang_view=dynamic_cast(view)) clang_view->async_delete(); @@ -579,6 +534,24 @@ bool Notebook::close(size_t index) { return true; } +void Notebook::delete_cursor_locations(Source::View *view) { + size_t cursor_locations_index=0; + for(auto it=cursor_locations.begin();it!=cursor_locations.end();) { + if(it->view==view) { + view->get_buffer()->delete_mark(it->mark); + it=cursor_locations.erase(it); + if(current_cursor_location!=static_cast(-1) && current_cursor_location>cursor_locations_index) + --current_cursor_location; + } + else { + ++it; + ++cursor_locations_index; + } + } + if(current_cursor_location>=cursor_locations.size()) + current_cursor_location=cursor_locations.size()-1; +} + bool Notebook::close_current() { return close(get_index(get_current_view())); } diff --git a/src/notebook.h b/src/notebook.h index a5bee1d..344699e 100644 --- a/src/notebook.h +++ b/src/notebook.h @@ -37,7 +37,6 @@ 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(); @@ -64,6 +63,7 @@ public: std::vector cursor_locations; size_t current_cursor_location=-1; bool disable_next_update_cursor_locations=false; + void delete_cursor_locations(Source::View *view); private: size_t get_index(Source::View *view); diff --git a/src/source.cc b/src/source.cc index 6479cdd..8a87e0f 100644 --- a/src/source.cc +++ b/src/source.cc @@ -90,16 +90,7 @@ const std::regex Source::View::no_bracket_statement_regex("^([ \\t]*)(if|for|els const std::regex Source::View::no_bracket_no_para_statement_regex("^([ \\t]*)(else) *$"); Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr language): Gsv::View(), SpellCheckView(), DiffView(file_path), language(language), status_diagnostics(0, 0, 0) { - get_source_buffer()->begin_not_undoable_action(); - if(language) { - if(filesystem::read_non_utf8(file_path, 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, get_buffer())==-1) - Terminal::get().print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true); - } - get_source_buffer()->end_not_undoable_action(); + load(); get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0)); @@ -343,6 +334,30 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtrbegin_not_undoable_action(); + get_buffer()->erase(get_buffer()->begin(), get_buffer()->end()); + bool status=true; + if(language) { + if(filesystem::read_non_utf8(file_path, 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, get_buffer())==-1) { + Terminal::get().print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true); + status=false; + } + } + get_source_buffer()->end_not_undoable_action(); + + boost::system::error_code ec; + last_write_time=boost::filesystem::last_write_time(file_path, ec); + if(ec) + last_write_time=static_cast(-1); + + return status; +} + void Source::View::rename(const boost::filesystem::path &path) { { std::unique_lock lock(file_path_mutex); diff --git a/src/source.h b/src/source.h index 1d2360f..3816e1c 100644 --- a/src/source.h +++ b/src/source.h @@ -44,6 +44,7 @@ namespace Source { View(const boost::filesystem::path &file_path, Glib::RefPtr language); ~View(); + bool load(); void rename(const boost::filesystem::path &path); virtual bool save(const std::vector &views); diff --git a/src/window.cc b/src/window.cc index 87a139e..635806f 100644 --- a/src/window.cc +++ b/src/window.cc @@ -348,8 +348,36 @@ void Window::set_menu_actions() { }); menu.add_action("reload_file", [this]() { - auto path = Notebook::get().get_current_view()->file_path; - Notebook::get().reload(path); + if(auto view=Notebook::get().get_current_view()) { + Gtk::MessageDialog dialog(*static_cast(get_toplevel()), "Reload file!", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); + dialog.set_default_response(Gtk::RESPONSE_YES); + dialog.set_secondary_text("Do you want to reload: " + view->file_path.string()+" ? The current buffer will be lost."); + int result = dialog.run(); + if(result==Gtk::RESPONSE_YES) { + if(boost::filesystem::exists(view->file_path)) { + std::ifstream can_read(view->file_path.string()); + if(!can_read) { + Terminal::get().print("Error: could not read "+view->file_path.string()+"\n", true); + return; + } + can_read.close(); + } + else { + Terminal::get().print("Error: "+view->file_path.string()+" does not exist\n", true); + return; + } + + int line = view->get_buffer()->get_insert()->get_iter().get_line(); + int offset = view->get_buffer()->get_insert()->get_iter().get_line_offset(); + view->load(); + while(Gtk::Main::events_pending()) + Gtk::Main::iteration(false); + Notebook::get().delete_cursor_locations(view); + view->place_cursor_at_line_offset(line, offset); + view->scroll_to_cursor_delayed(view, true, false); + view->get_buffer()->set_modified(false); + } + } }); menu.add_action("save", [this]() { @@ -1107,6 +1135,7 @@ void Window::activate_menu_items() { auto &menu = Menu::get(); auto view=Notebook::get().get_current_view(); + menu.actions["reload_file"]->set_enabled(view); menu.actions["source_spellcheck"]->set_enabled(view); menu.actions["source_spellcheck_clear"]->set_enabled(view); menu.actions["source_spellcheck_next_error"]->set_enabled(view);