Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pinner plugin #1308

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5020967
Add Pinner plugin
andy5995 Feb 13, 2024
5def3d7
Check for duplicates
andy5995 Feb 14, 2024
3a29307
check if doc eq NULL
andy5995 Feb 14, 2024
4bb86a9
use g_strdup instead of strlcpy, point better
andy5995 Feb 14, 2024
18b58b3
Add non-working popup menu and func to clear list
andy5995 Feb 14, 2024
73322e4
add header to Makefile.am
andy5995 Feb 14, 2024
c1899e4
Make list global
andy5995 Feb 14, 2024
afeeb59
Remove header, make all functions static
andy5995 Feb 14, 2024
093184d
Add UNpin function
andy5995 Feb 15, 2024
6a96d0c
Make documents clickable and open them
andy5995 Feb 15, 2024
b8d66c4
Don't auto-switch to the "Pinned" tab
andy5995 Feb 15, 2024
a1fe957
fix double-free error when freeing list
andy5995 Feb 16, 2024
cf40001
fix 'stack-use-after-return` in pin_cleanup
andy5995 Feb 16, 2024
5391654
implement unpin, use hashtable instead of GSList
andy5995 Feb 16, 2024
87b8b4e
Add right-click menu/option to clear pinned list
andy5995 Feb 16, 2024
295737b
Conditionally add sanitize flag
andy5995 Feb 16, 2024
a0f4bc8
Add credit for ChatGPT
andy5995 Feb 16, 2024
259571e
remove sanitize flag
andy5995 Feb 16, 2024
e0d734c
Don't use 'with_mnemonic' (fix missing underscores)
andy5995 Feb 16, 2024
7aa35e5
Acommodate for long filenames by using an ellipse
andy5995 Feb 17, 2024
cd1f687
Implment keybindings and add README.md
andy5995 Feb 17, 2024
b3c3b67
Add "unpin document" to right-click menu
andy5995 Feb 17, 2024
b58b98b
Add "Unpin" item to right-click popup menu
andy5995 Feb 17, 2024
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
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ if ENABLE_PAIRTAGHIGHLIGHTER
SUBDIRS += pairtaghighlighter
endif

if ENABLE_PINNER
SUBDIRS += pinner
endif

if ENABLE_POHELPER
SUBDIRS += pohelper
endif
Expand Down
9 changes: 9 additions & 0 deletions build/pinner.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AC_DEFUN([GP_CHECK_PINNER],
[
GP_ARG_DISABLE([pinner], [auto])
GP_COMMIT_PLUGIN_STATUS([Pinner])

AC_CONFIG_FILES([
pinner/Makefile
])
])
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ GP_CHECK_LIPSUM
GP_CHECK_MARKDOWN
GP_CHECK_OVERVIEW
GP_CHECK_PAIRTAGHIGHLIGHTER
GP_CHECK_PINNER
GP_CHECK_POHELPER
GP_CHECK_PRETTYPRINTER
GP_CHECK_PROJECTORGANIZER
Expand Down
25 changes: 25 additions & 0 deletions pinner/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include $(top_srcdir)/build/vars.build.mk

plugin = pinner

geanyplugins_LTLIBRARIES = pinner.la

pinner_la_SOURCES = \
pinner.c

pinner_la_CPPFLAGS = $(AM_CPPFLAGS) \
-DG_LOG_DOMAIN=\"Pinner\"

pinner_la_CFLAGS = \
$(AM_CFLAGS) \
-fsanitize=address,undefined
andy5995 marked this conversation as resolved.
Show resolved Hide resolved

pinner_la_LDFLAGS = \
$(AM_LDFLAGS) \
-fsanitize=address,undefined

pinner_la_LIBADD = \
$(COMMONLIBS)

AM_CPPCHECKFLAGS = -DSCE_PAS_DEFAULT=0
include $(top_srcdir)/build/cppcheck.mk
187 changes: 187 additions & 0 deletions pinner/pinner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* pinner.c
*
* Copyright 2024 Andy Alt <arch_stanton5995@proton.me>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/

#include "pinner.h"


static GtkWidget *pinned_view_vbox;
static gint page_number = 0;

struct pindata {
GeanyPlugin *plugin;
GSList *list;
};

static struct pindata *init_pindata(void)
andy5995 marked this conversation as resolved.
Show resolved Hide resolved
{
static struct pindata container = {
NULL,
NULL
};

return &container;
}


void slist_free_wrapper(gpointer pdata)
{
struct pindata *container = pdata;
GSList *list = container->list;
GSList *iter;
for (iter = list; iter != NULL; iter = g_slist_next(iter))
g_free(iter->data);

g_slist_free_full(list, g_free);
container->list = NULL;
}


/* A function adapted (pinched) from filebrowser.c */
static GtkWidget *create_popup_menu(gpointer pdata)
{
GtkWidget *item, *menu;

menu = gtk_menu_new();

/* 3? What should the size be? */
/* https://docs.gtk.org/gtk3/ctor.Image.new_from_icon_name.html */
item = gtk_image_new_from_icon_name("edit-clear", 3);
gtk_widget_show(item);
gtk_container_add(GTK_CONTAINER(menu), item);
g_signal_connect(item, "activate", G_CALLBACK(slist_free_wrapper), pdata);

return menu;
}

bool is_duplicate(GSList *list, const gchar* file_name)
{
GSList *iter;
for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
if (g_strcmp0((const gchar *)iter->data, file_name) == 0) {
/* We'll probably want to alert the user the document already
* is pinned */
return true;
}
}
return false;
}

static void pin_activate_cb(GtkMenuItem *menuitem, gpointer pdata)
{
struct pindata *container = pdata;
GeanyPlugin *plugin = container->plugin;
GSList *list = container->list;

GeanyDocument *doc = document_get_current();
if (doc == NULL)
return;

// DEBUG: Print the elements in the list
//printf("List elements:\n");
//for (GSList *iter = list; iter != NULL; iter = g_slist_next(iter))
//printf("%s\n", (gchar *)iter->data);

if (is_duplicate(list, doc->file_name))
return;

/* This must be freed when nodes are removed from the list */
gchar *tmp_file_name = g_strdup(doc->file_name);

container->list = g_slist_append(list, tmp_file_name);
GtkWidget *label = gtk_label_new_with_mnemonic(doc->file_name);
gtk_widget_show(label);
gtk_box_pack_start(GTK_BOX(pinned_view_vbox), label, FALSE, FALSE, 0);
gtk_notebook_set_current_page(GTK_NOTEBOOK(plugin->geany_data->main_widgets->sidebar_notebook), page_number);

return;
}

static gboolean on_button_press(GtkWidget *widget, GdkEventButton *event, gpointer pdata)
{
//if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
//{
//on_open_clicked(NULL, NULL);
//return TRUE;
//}
if (event->button == 3)
{
static GtkWidget *popup_menu = NULL;

if (popup_menu == NULL)
popup_menu = create_popup_menu(pdata);

gtk_menu_popup_at_pointer(GTK_MENU(popup_menu), (GdkEvent *) event);
/* don't return TRUE here, unless the selection won't be changed */
}
return FALSE;
}

static gboolean pin_init(GeanyPlugin *plugin, gpointer pdata)
{
GtkWidget *main_menu_item;

struct pindata *container = init_pindata();
container->plugin = plugin;

// Create a new menu item and show it
main_menu_item = gtk_menu_item_new_with_mnemonic("Pin Document");
gtk_widget_show(main_menu_item);
gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->tools_menu),
main_menu_item);
g_signal_connect(main_menu_item, "activate",
G_CALLBACK(pin_activate_cb), container);
g_signal_connect(pinned_view_vbox, "button-press-event",
andy5995 marked this conversation as resolved.
Show resolved Hide resolved
G_CALLBACK(on_button_press), container);

geany_plugin_set_data(plugin, main_menu_item, NULL);

pinned_view_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_show_all(pinned_view_vbox);
page_number = gtk_notebook_append_page(GTK_NOTEBOOK(plugin->geany_data->main_widgets->sidebar_notebook),
pinned_view_vbox, gtk_label_new(_("Pinned")));
return TRUE;
}


static void pin_cleanup(GeanyPlugin *plugin, gpointer pdata)
{
GtkWidget *main_menu_item = (GtkWidget *) pdata;
// g_slist_free(list);

gtk_widget_destroy(main_menu_item);
}


G_MODULE_EXPORT
void geany_load_module(GeanyPlugin *plugin)
{
plugin->info->name = "Pinner";
plugin->info->description = "Pin a document";
plugin->info->version = "0.1.0";
plugin->info->author = "Andy Alt <arch_stanton5995@proton.me>";

plugin->funcs->init = pin_init;
plugin->funcs->cleanup = pin_cleanup;

GEANY_PLUGIN_REGISTER(plugin, 225);
}
31 changes: 31 additions & 0 deletions pinner/pinner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* pinner.h
*
* Copyright 2024 Andy Alt <arch_stanton5995@proton.me>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/

#include <geanyplugin.h>
#include <stdbool.h>

static struct pindata *init_pindata(void);
void slist_free_wrapper(gpointer pdata);
static GtkWidget *create_popup_menu(gpointer pdata);
bool is_duplicate(GSList *list, const gchar* file_name);
static void pin_activate_cb(GtkMenuItem *menuitem, gpointer pdata);
3 changes: 3 additions & 0 deletions po/POTFILES.skip
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
# geanyvc
geanyvc/src/commit.glade

# Pinner
pinner/pinner.c

# WebHelper
webhelper/src/gwh-enum-types.c
Loading