Commit 87aa9b9a authored by Sebastian Ramacher's avatar Sebastian Ramacher

implement :write and :write!

parent 36bdb57b
......@@ -91,5 +91,45 @@ cmd_print(girara_session_t* session, girara_list_t* argument_list)
bool
cmd_save(girara_session_t* session, girara_list_t* argument_list)
{
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
if (zathura->document == NULL) {
girara_error("no document as been opened");
return false;
}
if (girara_list_size(argument_list) == 1) {
document_save(zathura, girara_list_nth(argument_list, 0), false);
}
else {
girara_error("invalid arguments");
return false;
}
return true;
}
bool
cmd_savef(girara_session_t* session, girara_list_t* argument_list)
{
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
if (zathura->document == NULL) {
girara_error("no document as been opened");
return false;
}
if (girara_list_size(argument_list) == 1) {
document_save(zathura, girara_list_nth(argument_list, 0), true);
}
else {
girara_error("invalid arguments");
return false;
}
return true;
}
......@@ -78,4 +78,14 @@ bool cmd_print(girara_session_t* session, girara_list_t* argument_list);
*/
bool cmd_save(girara_session_t* session, girara_list_t* argument_list);
/**
* Save the current file and overwrite existing files
*
* @param session The used girara session
* @param argument_list List of passed arguments
* @return true if no error occured
*/
bool cmd_savef(girara_session_t* session, girara_list_t* argument_list);
#endif // COMMANDS_H
......@@ -114,7 +114,8 @@ config_load_default(zathura_t* zathura)
girara_inputbar_command_add(gsession, "info", NULL, cmd_info, NULL, "Show file information");
girara_inputbar_command_add(gsession, "open", "o", cmd_open, cc_open, "Open document");
girara_inputbar_command_add(gsession, "print", NULL, cmd_print, NULL, "Print document");
girara_inputbar_command_add(gsession, "save", NULL, cmd_save, NULL, "Save document");
girara_inputbar_command_add(gsession, "write", NULL, cmd_save, NULL, "Save document");
girara_inputbar_command_add(gsession, "write!", NULL, cmd_savef, NULL, "Save document (and force overwriting)");
/* add shortcut mappings */
girara_shortcut_mapping_add(gsession, "abort", sc_abort);
......
......@@ -160,7 +160,9 @@ sc_scroll(girara_session_t* session, girara_argument_t* argument, unsigned int t
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(argument != NULL, false);
g_return_val_if_fail(zathura->document != NULL, false);
if (zathura->document == NULL) {
return false;
}
GtkAdjustment* adjustment = NULL;
if ( (argument->n == LEFT) || (argument->n == RIGHT) )
......
......@@ -309,6 +309,27 @@ error_out:
return false;
}
bool
document_save(zathura_t* zathura, const char* path, bool overwrite)
{
g_return_val_if_fail(zathura, false);
g_return_val_if_fail(zathura->document, false);
g_return_val_if_fail(path, false);
gchar* file_path = girara_fix_path(path);
if (!overwrite && g_file_test(file_path, G_FILE_TEST_EXISTS))
{
gchar* message = g_strdup_printf("File already exists: %s. Use :write! to overwrite it.", file_path);
girara_error(message);
g_free(message);
return false;
}
bool res = zathura_document_save_as(zathura->document, file_path);
g_free(file_path);
return res;
}
static void
remove_page_from_table(GtkWidget* page, gpointer permanent)
{
......
......@@ -114,6 +114,18 @@ void zathura_free(zathura_t* zathura);
*/
bool document_open(zathura_t* zathura, const char* path, const char* password);
/**
* Save a open file
*
* @param zathura The zathura session
* @param path The path
* @param overwrite Overwrite existing file
*
* @return If no error occured true, otherwise false, is returned.
*/
bool document_save(zathura_t* zathura, const char* path, bool overwrite);
/**
* Closes the current opened document
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment