This repository was archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathjuci.cc
137 lines (122 loc) · 4.29 KB
/
juci.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "juci.h"
#include "window.h"
#include "notebook.h"
#include "directories.h"
#include "menu.h"
#include "config.h"
#include "terminal.h"
#ifndef _WIN32
#include <csignal>
#endif
int Application::on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine> &cmd) {
Glib::set_prgname("juci");
Glib::OptionContext ctx("[PATH ...]");
Glib::OptionGroup gtk_group(gtk_get_option_group(true));
ctx.add_group(gtk_group);
int argc;
char **argv = cmd->get_arguments(argc);
ctx.parse(argc, argv);
if(argc>=2) {
boost::system::error_code current_path_ec;
auto current_path=boost::filesystem::current_path(current_path_ec);
if(current_path_ec)
errors.emplace_back("Error: could not find current path\n");
for(int c=1;c<argc;c++) {
boost::filesystem::path path(argv[c]);
if(path.is_relative() && !current_path_ec)
path=current_path/path;
if(boost::filesystem::exists(path)) {
if(boost::filesystem::is_regular_file(path))
files.emplace_back(path, 0);
else if(boost::filesystem::is_directory(path))
directories.emplace_back(path);
}
//Open new file if parent path exists
else {
if(path.is_absolute() && boost::filesystem::is_directory(path.parent_path()))
files.emplace_back(path, 0);
else
errors.emplace_back("Error: could not create "+path.string()+".\n");
}
}
}
activate();
return 0;
}
void Application::on_activate() {
std::vector<std::pair<int, int>> file_offsets;
std::string current_file;
Window::get().load_session(directories, files, file_offsets, current_file, directories.empty() && files.empty());
Window::get().add_widgets();
add_window(Window::get());
Window::get().show();
bool first_directory=true;
for(auto &directory: directories) {
if(first_directory) {
Directories::get().open(directory);
first_directory=false;
}
else {
std::string files_in_directory;
for(auto it=files.begin();it!=files.end();) {
if(it->first.generic_string().compare(0, directory.generic_string().size()+1, directory.generic_string()+'/')==0) {
files_in_directory+=" "+it->first.string();
it=files.erase(it);
}
else
it++;
}
std::thread another_juci_app([directory, files_in_directory](){
Terminal::get().async_print("Executing: juci "+directory.string()+files_in_directory+"\n");
Terminal::get().process("juci "+directory.string()+files_in_directory, "", false);
});
another_juci_app.detach();
}
}
for(size_t i=0;i<files.size();++i) {
Notebook::get().open(files[i].first, files[i].second);
if(i<file_offsets.size()) {
if(auto view=Notebook::get().get_current_view()) {
view->place_cursor_at_line_offset(file_offsets[i].first, file_offsets[i].second);
view->hide_tooltips();
}
}
}
for(auto &error: errors)
Terminal::get().print(error, true);
if(!current_file.empty()) {
Notebook::get().open(current_file);
if(auto view=Notebook::get().get_current_view()) {
auto iter=view->get_buffer()->get_insert()->get_iter();
// To update cursor history
view->place_cursor_at_line_offset(iter.get_line(), iter.get_line_offset());
view->hide_tooltips();
}
}
while(Gtk::Main::events_pending())
Gtk::Main::iteration(false);
for(auto view: Notebook::get().get_views())
view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5);
}
void Application::on_startup() {
Gtk::Application::on_startup();
Menu::get().build();
if (!Menu::get().juci_menu || !Menu::get().window_menu) {
std::cerr << "Menu not found." << std::endl;
}
else {
set_app_menu(Menu::get().juci_menu);
set_menubar(Menu::get().window_menu);
}
}
Application::Application() : Gtk::Application("no.sout.juci", Gio::APPLICATION_NON_UNIQUE | Gio::APPLICATION_HANDLES_COMMAND_LINE) {
Glib::set_application_name("juCi++");
//Gtk::MessageDialog without buttons caused text to be selected, this prevents that
Gtk::Settings::get_default()->property_gtk_label_select_on_focus()=false;
}
int main(int argc, char *argv[]) {
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN); // Do not terminate application when writing to a process fails
#endif
return Application().run(argc, argv);
}