diff --git a/src/source.cc b/src/source.cc index f874a8f..81242ce 100644 --- a/src/source.cc +++ b/src/source.cc @@ -91,7 +91,6 @@ const std::regex Source::View::no_bracket_no_para_statement_regex("^([ \\t]*)(el 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(); - last_read_time=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); 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"); @@ -454,7 +453,10 @@ bool Source::View::save(const std::vector &views) { cleanup_whitespace_characters(); if(filesystem::write(file_path, get_buffer())) { - last_read_time=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + boost::system::error_code ec; + last_write_time=boost::filesystem::last_write_time(file_path, ec); + if(ec) + last_write_time=static_cast(-1); get_buffer()->set_modified(false); Directories::get().on_save_file(file_path); return true; @@ -2049,14 +2051,6 @@ bool Source::View::on_button_press_event(GdkEventButton *event) { return Gsv::View::on_button_press_event(event); } -bool Source::View::on_focus_in_event(GdkEventFocus* focus_event){ - boost::system::error_code ec; - auto last_write_time=boost::filesystem::last_write_time(file_path, ec); - if(!ec && last_write_time>last_read_time) - Info::get().print("Caution: " + file_path.filename().string() + " was altered outside of juCi++"); - return Gsv::View::on_focus_in_event(focus_event); -}; - std::pair Source::View::find_tab_char_and_size() { std::unordered_map tab_chars; std::unordered_map tab_sizes; diff --git a/src/source.h b/src/source.h index 8da8c82..f4c0760 100644 --- a/src/source.h +++ b/src/source.h @@ -103,7 +103,6 @@ namespace Source { virtual void soft_reparse() {soft_reparse_needed=false;} virtual void full_reparse() {full_reparse_needed=false;} protected: - std::time_t last_read_time; bool parsed=false; Tooltips diagnostic_tooltips; Tooltips type_tooltips; @@ -146,7 +145,6 @@ namespace Source { bool on_key_press_event_smart_brackets(GdkEventKey* key); bool on_key_press_event_smart_inserts(GdkEventKey* key); bool on_button_press_event(GdkEventButton *event) override; - bool on_focus_in_event(GdkEventFocus* focus_event) override; std::pair find_tab_char_and_size(); unsigned tab_size; diff --git a/src/source_diff.cc b/src/source_diff.cc index 834dfba..adf7078 100644 --- a/src/source_diff.cc +++ b/src/source_diff.cc @@ -55,6 +55,16 @@ Source::DiffView::DiffView(const boost::filesystem::path &file_path) : Gsv::View renderer->tag_removed_below=get_buffer()->create_tag(); renderer->tag_removed_above=get_buffer()->create_tag(); + boost::system::error_code ec; + last_write_time=boost::filesystem::last_write_time(file_path, ec); + if(ec) + last_write_time=static_cast(-1); + + signal_focus_in_event().connect([this](GdkEventFocus *event) { + check_last_write_time(); + return false; + }); + configure(); } @@ -74,6 +84,15 @@ Source::DiffView::~DiffView() { } } +void Source::DiffView::check_last_write_time() { + if(has_focus()) { + boost::system::error_code ec; + auto last_write_time=boost::filesystem::last_write_time(file_path, ec); + if(!ec && this->last_write_time!=static_cast(-1) && last_write_time!=this->last_write_time) + Info::get().print("Caution: " + file_path.filename().string() + " was changed outside of juCi++"); + } +} + void Source::DiffView::configure() { if(Config::get().source.show_git_diff) { if(repository) @@ -158,6 +177,7 @@ void Source::DiffView::configure() { if(monitor_event!=Gio::FileMonitorEvent::FILE_MONITOR_EVENT_CHANGES_DONE_HINT) { delayed_monitor_changed_connection.disconnect(); delayed_monitor_changed_connection=Glib::signal_timeout().connect([this]() { + check_last_write_time(); monitor_changed=true; parse_state=ParseState::STARTING; std::unique_lock lock(parse_mutex); diff --git a/src/source_diff.h b/src/source_diff.h index 2578410..63df609 100644 --- a/src/source_diff.h +++ b/src/source_diff.h @@ -36,6 +36,8 @@ namespace Source { boost::filesystem::path file_path; protected: std::mutex file_path_mutex; + std::time_t last_write_time; + void check_last_write_time(); public: virtual void configure();