Skip to content

Commit

Permalink
Share get_doc_folder as sidebar_get_doc_folder and use it.
Browse files Browse the repository at this point in the history
Also remove copied code in notebook.c.
  • Loading branch information
konsolebox committed Jul 31, 2016
1 parent ed809af commit 6d99001
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 69 deletions.
69 changes: 2 additions & 67 deletions src/notebook.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "geanyobject.h"
#include "keybindings.h"
#include "main.h"
#include "project.h"
#include "sidebar.h"
#include "support.h"
#include "ui_utils.h"
#include "utils.h"
Expand Down Expand Up @@ -779,71 +779,6 @@ void notebook_remove_page(gint page_num)
}


/* Copied from sidebar.c. We can place this in one place like utils.c. */
static gboolean utils_filename_has_prefix(const gchar *str, const gchar *prefix)
{
gchar *head = g_strndup(str, strlen(prefix));
gboolean ret = utils_filenamecmp(head, prefix) == 0;

g_free(head);
return ret;
}


/* Copied from sidebar.c. We can place this in one place like utils.c,
* with a more formal naming convention. */
static gchar *get_doc_folder(const gchar *path)
{
gchar *tmp_dirname = g_strdup(path);
gchar *project_base_path;
gchar *dirname = NULL;
const gchar *home_dir = g_get_home_dir();
const gchar *rest;

/* replace the project base path with the project name */
project_base_path = project_get_base_path();

if (project_base_path != NULL)
{
gsize len = strlen(project_base_path);

/* remove trailing separator so we can match base path exactly */
if (project_base_path[len-1] == G_DIR_SEPARATOR)
project_base_path[--len] = '\0';

/* check whether the dir name matches or uses the project base path */
if (utils_filename_has_prefix(tmp_dirname, project_base_path))
{
rest = tmp_dirname + len;
if (*rest == G_DIR_SEPARATOR || *rest == '\0')
{
dirname = g_strdup_printf("%s%s", app->project->name, rest);
}
}
g_free(project_base_path);
}
if (dirname == NULL)
{
dirname = tmp_dirname;

/* If matches home dir, replace with tilde */
if (!EMPTY(home_dir) && utils_filename_has_prefix(dirname, home_dir))
{
rest = dirname + strlen(home_dir);
if (*rest == G_DIR_SEPARATOR || *rest == '\0')
{
dirname = g_strdup_printf("~%s", rest);
g_free(tmp_dirname);
}
}
}
else
g_free(tmp_dirname);

return dirname;
}


static gchar *get_doc_dirname(const GeanyDocument* doc)
{
gchar *dirname, *tmp_dirname;
Expand All @@ -853,7 +788,7 @@ static gchar *get_doc_dirname(const GeanyDocument* doc)
else
{
tmp_dirname = g_path_get_dirname(doc->file_name);
dirname = get_doc_folder(tmp_dirname);
dirname = sidebar_get_doc_folder(tmp_dirname);
g_free(tmp_dirname);
}

Expand Down
4 changes: 2 additions & 2 deletions src/sidebar.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ static gboolean utils_filename_has_prefix(const gchar *str, const gchar *prefix)
}


static gchar *get_doc_folder(const gchar *path)
gchar *sidebar_get_doc_folder(const gchar *path)
{
gchar *tmp_dirname = g_strdup(path);
gchar *project_base_path;
Expand Down Expand Up @@ -430,7 +430,7 @@ static GtkTreeIter *get_doc_parent(GeanyDocument *doc)
return NULL;

path = g_path_get_dirname(DOC_FILENAME(doc));
dirname = get_doc_folder(path);
dirname = sidebar_get_doc_folder(path);

if (gtk_tree_model_get_iter_first(model, &parent))
{
Expand Down
2 changes: 2 additions & 0 deletions src/sidebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ void sidebar_finalize(void);

void sidebar_update_tag_list(GeanyDocument *doc, gboolean update);

gchar* sidebar_get_doc_folder(const gchar *path);

void sidebar_openfiles_add(GeanyDocument *doc);

void sidebar_openfiles_update(GeanyDocument *doc);
Expand Down

0 comments on commit 6d99001

Please sign in to comment.