Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Reload file #287

Merged
merged 3 commits into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ Menu::Menu() {
<attribute name='action'>app.open_folder</attribute>
</item>
</section>
<section>
<item>
<attribute name='label' translatable='yes'>_Reload _File</attribute>
<attribute name='action'>app.reload_file</attribute>
</item>
</section>
<section>
<item>
<attribute name='label' translatable='yes'>_Save</attribute>
Expand Down
32 changes: 32 additions & 0 deletions src/notebook.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,38 @@ std::vector<Source::View*> &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();
Expand Down
1 change: 1 addition & 0 deletions src/notebook.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Notebook : public Gtk::HPaned {
std::vector<Source::View*> &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();
Expand Down
5 changes: 5 additions & 0 deletions src/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ void Window::set_menu_actions() {
Directories::get().open(path);
});

menu.add_action("reload_file", [this]() {
auto path = Notebook::get().get_current_view()->file_path;
Notebook::get().reload(path);
});

menu.add_action("save", [this]() {
if(auto view=Notebook::get().get_current_view()) {
if(Notebook::get().save_current()) {
Expand Down