Initial commit.

This commit is contained in:
Adrian Malacoda 2014-08-16 04:35:41 -05:00
commit 5a55e9f74d
10 changed files with 1430 additions and 0 deletions

341
COPYING Normal file
View File

@ -0,0 +1,341 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
CC=gcc
all:
$(CC) -c external-tools.c -fPIC `pkg-config --cflags geany`
$(CC) external-tools.o -o external-tools.so -shared `pkg-config --libs geany`
install:
cp external-tools.so ~/.config/geany/plugins/
clean:
rm -f *.o *.so

48
README.md Normal file
View File

@ -0,0 +1,48 @@
# Geany External Tools plugin
This is a plugin that allows you to define commands or scripts, which can be added to the menu and/or bound to keyboard shortcuts. It is similar to the External Tools plugin for gedit.
## Installation
Install the build dependencies for `geany` and `geany-plugins`:
apt-get build-dep geany geany-plugins
Then build and install the plugin
make && make install
If Geany is already running, restart it.
## Tool Options
### Flags
The following flags modify the behavior or discoverability of the tool:
* **Save**: Causes the active document to be saved when the tool is run.
* **Menu**: Causes the tool to become available under the **Tools** menu.
* **Shortcut**: Causes the tool to become available under the **Edit -> Preferences -> Keybindings** dialog, where a key binding can be assigned to it.
### Output
This option controls where the tool sends its output:
* **None**: The tool's output is not shown.
* **Message Text**: The tool's output is displayed in the bottom panel, in the **Tools** section.
* **Replace Selected**: The tool's output replaces the currently selected text.
* **Replace Line**: The tool's output replaces the line under the editor cursor.
* **Append Current Document**: The tool's output is appended to the active document.
* **New Document**: A new document containing the tool's output is created.
### Environment Variables
Tools have the following environment variables available to them:
* `GEANY_PROJECT_DIRECTORY`: Root directory of currently open project, as defined in project configuration.
* `GEANY_SELECTION`: The currently selected text.
* `GEANY_FILE_TYPE_NAME`: Name of the active document's type, e.g. "Markdown" or "Python"
* `GEANY_FILE_PATH`: Path to current file.
* `GEANY_SELECTED_LINE`: The line that the editor cursor is on.
* `GEANY_LINE_NUMBER`: The line number of the line under the editor cursor.
* `GEANY_FILE_MIME_TYPE`: MIME type of active document, e.g. "text/x-markdown" or "text/x-python"
## Known Issues
* "New Document" and current selection do not play well. This seems to be because the new document is created and is given focus before the selection is grabbed, resulting in an empty selection.
## Acknowledgments
The original author of this plugin is Steven Blatnick <steve8track@yahoo.com>.

291
dialog.c Normal file
View File

@ -0,0 +1,291 @@
#include <geanyplugin.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
extern const gchar *tools;
static GtkWidget *scrollable, *tree, *title, *saveCheckbox, *menuCheckbox,
*shortcutCheckbox, *outputCombo, *editButton, *shortcutButton;
static GtkTreeStore *list, *outputs;
static GtkTreeIter row;
static GtkDialog *configDialog;
static Tool* get_active_tool()
{
GtkTreeSelection *selected = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
GtkTreeIter iter;
GtkTreeModel *model;
if(gtk_tree_selection_get_selected(selected, &model, &iter)) {
Tool *tool;
gtk_tree_model_get(model, &iter, 0, &tool, -1);
return tool;
}
else {
return NULL;
}
}
static void selected_changed(GtkTreeView *view, gpointer data)
{
Tool* tool = get_active_tool();
if(tool == NULL) {
gtk_widget_set_sensitive(GTK_WIDGET(title), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(saveCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(menuCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(shortcutCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(shortcutCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(outputCombo), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(editButton), FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(saveCheckbox), FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(menuCheckbox), FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(shortcutCheckbox), FALSE);
gtk_entry_set_text(GTK_ENTRY(title), "");
}
else {
gtk_widget_set_sensitive(GTK_WIDGET(title), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(saveCheckbox), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(menuCheckbox), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(shortcutCheckbox), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(shortcutCheckbox), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(outputCombo), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(editButton), TRUE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(saveCheckbox), tool->save);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(menuCheckbox), tool->menu);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(shortcutCheckbox), tool->shortcut);
gtk_combo_box_set_active(GTK_COMBO_BOX(outputCombo), tool->output == -1 ? 0 : tool->output);
gtk_entry_set_text(GTK_ENTRY(title), tool->name);
}
}
static void combo_changed(GtkComboBox *combo, gpointer data)
{
Tool* tool = get_active_tool();
if(tool != NULL) {
GtkTreeIter iter;
gint value;
GtkTreeModel *model;
if(gtk_combo_box_get_active_iter(combo, &iter)) {
model = gtk_combo_box_get_model(combo);
gtk_tree_model_get(model, &iter, 1, &value, -1);
}
tool->output = value;
}
}
static int add_tool(Tool *tool)
{
gtk_tree_store_append(list, &row, NULL);
gtk_tree_store_set(list, &row, 0, tool, -1);
}
static int add_output(gchar *label, gint value)
{
gtk_tree_store_append(outputs, &row, NULL);
gtk_tree_store_set(outputs, &row, 0, label, 1, value, -1);
}
static void delete_tool(GtkButton *button, gpointer data)
{
GtkTreeSelection *selected = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
GtkTreeIter iter;
GtkTreeModel *model;
if(gtk_tree_selection_get_selected(selected, &model, &iter))
{
Tool *tool;
gtk_tree_model_get(model, &iter, 0, &tool, -1);
g_remove(tool->id);
free_tool(tool, TRUE);
gtk_tree_selection_unselect_iter(selected, &iter);
gtk_tree_store_remove(list, &iter);
}
}
static void new_tool_entry(GtkButton *button, gpointer data)
{
Tool *tool = new_tool();
add_tool(tool);
}
static void cell_data(GtkTreeViewColumn *tree_column, GtkCellRenderer *render,
GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
{
Tool *tool;
gtk_tree_model_get(model, iter, 0, &tool, -1);
g_object_set(render, "text", tool->name, NULL);
}
static void toggle_checkbox(GtkToggleButton *checkbox, gpointer data)
{
Tool* tool = get_active_tool();
if(tool != NULL) {
gboolean value = gtk_toggle_button_get_active(checkbox);
gint property = GPOINTER_TO_INT(data);
switch(property) {
case SAVE:
tool->save = value;
break;
case MENU:
tool->menu = value;
break;
case SHORTCUT:
tool->shortcut = value;
break;
}
}
}
static GtkWidget* checkbox(const gchar *label, const gchar *tooltip, gchar *key)
{
GtkWidget *check = gtk_check_button_new_with_label(_(label));
ui_widget_set_tooltip_text(check, _(tooltip));
g_signal_connect(G_OBJECT(check), "toggled", G_CALLBACK(toggle_checkbox), key);
return check;
}
static void dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
{
GtkTreeIter iter;
gboolean valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(list), &iter);
while(valid)
{
Tool *tool;
gtk_tree_model_get(GTK_TREE_MODEL(list), &iter, 0, &tool, -1);
save_tool(tool);
valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(list), &iter);
}
//Remove all shortcuts to tools from the UI for a refresh:
clean_tools();
//We need to re-establish all of the tools in the UI:
reload_tools();
}
static gboolean on_change(GtkWidget *entry, GdkEventKey *event, gpointer user_data)
{
Tool* tool = get_active_tool();
if(tool != NULL) {
g_free(tool->name);
tool->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
}
return FALSE;
}
static void on_edit(GtkButton* button, gpointer data)
{
Tool* tool = get_active_tool();
gtk_dialog_response(configDialog, GTK_RESPONSE_OK);
document_open_file(tool->id, FALSE, NULL, NULL);
}
static void on_shortcut(GtkButton* button, gpointer data)
{
Tool* tool = get_active_tool();
gtk_dialog_response(configDialog, GTK_RESPONSE_OK);
keybindings_dialog_show_prefs_scroll("External Tools");
}
GtkWidget* plugin_configure(GtkDialog *dialog)
{
configDialog = dialog;
GtkCellRenderer *render;
GtkWidget* vbox = gtk_vbox_new(FALSE, 6);
GtkWidget* hbox = gtk_hbox_new(FALSE, 6);
GtkWidget* hboxButtons = gtk_hbox_new(FALSE, 6);
GtkWidget* settingsBox = gtk_vbox_new(FALSE, 6);
GtkWidget* buttonBox = gtk_hbox_new(FALSE, 6);
//Tools (left half):
list = gtk_tree_store_new(1, G_TYPE_POINTER);
tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list));
render = gtk_cell_renderer_text_new();
GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Tool Name", render, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), TRUE);
gtk_tree_view_column_set_cell_data_func(column, render,
(GtkTreeCellDataFunc)cell_data, NULL, NULL);
scrollable = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollable),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(scrollable), tree);
GtkTreeSelection *select = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
g_signal_connect(G_OBJECT(select), "changed",
G_CALLBACK(selected_changed), NULL);
GtkWidget* delete = gtk_button_new_with_label("Delete");
GtkWidget* new = gtk_button_new_with_label("New");
g_signal_connect(delete, "clicked", G_CALLBACK(delete_tool), NULL);
g_signal_connect(new, "clicked", G_CALLBACK(new_tool_entry), NULL);
gtk_box_pack_start(GTK_BOX(buttonBox), delete, FALSE, FALSE, 10);
gtk_box_pack_end(GTK_BOX(buttonBox), new, FALSE, FALSE, 10);
gtk_box_pack_start(GTK_BOX(vbox), scrollable, TRUE, TRUE, 10);
gtk_box_pack_start(GTK_BOX(vbox), buttonBox, FALSE, FALSE, 10);
//Properties (right half):
gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 10);
outputs = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_INT);
outputCombo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(outputs));
GtkCellRenderer *cell = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(outputCombo), cell, TRUE );
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(outputCombo), cell, "text", 0, NULL );
add_output(_("None"), TOOL_OUTPUT_NONE);
add_output(_("Message Text"), TOOL_OUTPUT_MESSAGE_TEXT);
add_output(_("Message Table"), TOOL_OUTPUT_MESSAGE_TABLE);
add_output(_("Replace Selected"), TOOL_OUTPUT_REPLACE_SELECTED);
add_output(_("Replace Line"), TOOL_OUTPUT_REPLACE_LINE);
add_output(_("Replace Word"), TOOL_OUTPUT_REPLACE_WORD);
add_output(_("Append Current Document"), TOOL_OUTPUT_APPEND_CURRENT_DOCUMENT);
add_output(_("New Document"), TOOL_OUTPUT_NEW_DOCUMENT);
g_signal_connect(G_OBJECT(outputCombo), "changed", G_CALLBACK(combo_changed), NULL);
saveCheckbox = checkbox("Save", "Should the active document be saved when this tool is run?", GINT_TO_POINTER(SAVE));
menuCheckbox = checkbox("Menu", "Should there be a menu for this tool?", GINT_TO_POINTER(MENU));
shortcutCheckbox = checkbox("Shortcut", "Should there be a keyboard shortcut for this tool?", GINT_TO_POINTER(SHORTCUT));
title = gtk_entry_new();
g_signal_connect(title, "focus-out-event", G_CALLBACK(on_change), NULL);
gtk_box_pack_start(GTK_BOX(settingsBox), title, FALSE, FALSE, 2);
gtk_box_pack_start(GTK_BOX(settingsBox), saveCheckbox, FALSE, FALSE, 10);
gtk_box_pack_start(GTK_BOX(settingsBox), menuCheckbox, FALSE, FALSE, 10);
gtk_box_pack_start(GTK_BOX(settingsBox), shortcutCheckbox, FALSE, FALSE, 10);
gtk_box_pack_start(GTK_BOX(settingsBox), outputCombo, FALSE, FALSE, 10);
editButton = gtk_button_new_with_label(_("Edit"));
g_signal_connect(editButton, "clicked", G_CALLBACK(on_edit), NULL);
gtk_box_pack_start(GTK_BOX(hboxButtons), editButton, TRUE, TRUE, 2);
shortcutButton = gtk_button_new_with_label(_("Shortcut"));
g_signal_connect(shortcutButton, "clicked", G_CALLBACK(on_shortcut), NULL);
gtk_box_pack_start(GTK_BOX(hboxButtons), shortcutButton, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(settingsBox), hboxButtons, FALSE, FALSE, 10);
gtk_box_pack_end(GTK_BOX(hbox), settingsBox, TRUE, TRUE, 10);
gtk_widget_show_all(hbox);
g_signal_connect(dialog, "response", G_CALLBACK(dialog_response), NULL);
load_tools(add_tool);
gtk_widget_set_sensitive(GTK_WIDGET(title), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(saveCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(menuCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(shortcutCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(shortcutCheckbox), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(editButton), FALSE);
return hbox;
}

60
external-tools.c Normal file
View File

@ -0,0 +1,60 @@
#include <geanyplugin.h>
#include <gdk/gdkkeysyms.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include "panel.c"
#include "table.c"
#include "tool.c"
#include "dialog.c"
GeanyPlugin *geany_plugin;
GeanyData *geany_data;
GeanyFunctions *geany_functions;
PLUGIN_VERSION_CHECK(211)
PLUGIN_SET_INFO("External Tools", "Allow external tools to be integrated into many common actions.", "0.1", "Steven Blatnick <steve8track@yahoo.com>");
const gchar *path, *conf, *tools, *home;
GRegex *name_regex, *path_regex, *match_regex;
static GtkWidget *tool_menu_item = NULL;
static void menu_callback(GtkMenuItem *menuitem, gpointer gdata)
{
plugin_show_configure(geany_plugin);
}
void plugin_init(GeanyData *data)
{
path_regex = g_regex_new("^\\.|^build$", G_REGEX_OPTIMIZE | G_REGEX_CASELESS, 0, NULL);
name_regex = g_regex_new("^\\.|\\.(o|so|exe|class|pyc)$", G_REGEX_OPTIMIZE | G_REGEX_CASELESS, 0, NULL);
match_regex = g_regex_new(g_strconcat("[^", G_DIR_SEPARATOR_S, "]+\\.[^\\s]+", NULL), G_REGEX_OPTIMIZE | G_REGEX_CASELESS, 0, NULL);
home = g_getenv("HOME");
if (!home) {
home = g_get_home_dir();
}
path = g_build_path(G_DIR_SEPARATOR_S, geany_data->app->configdir, "plugins", "external-tools", NULL);
conf = g_build_path(G_DIR_SEPARATOR_S, path, "external-tools.conf", NULL);
tools = g_build_path(G_DIR_SEPARATOR_S, path, "tools", NULL);
g_mkdir_with_parents(tools, S_IRUSR | S_IWUSR | S_IXUSR);
reload_tools();
tool_menu_item = gtk_menu_item_new_with_mnemonic("External Tools...");
gtk_widget_show(tool_menu_item);
gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), tool_menu_item);
g_signal_connect(tool_menu_item, "activate", G_CALLBACK(menu_callback), NULL);
panel_init();
}
void plugin_cleanup(void)
{
g_regex_unref(path_regex);
g_regex_unref(name_regex);
g_regex_unref(match_regex);
clean_tools();
gtk_container_remove(GTK_CONTAINER(geany->main_widgets->tools_menu), tool_menu_item);
panel_cleanup();
}

173
output.c Normal file
View File

@ -0,0 +1,173 @@
extern GeanyPlugin *geany_plugin;
extern GeanyData *geany_data;
extern GeanyFunctions *geany_functions;
extern const gchar *tools, *home;
extern GtkWidget *panel;
static Tool *executed_tool;
static void output_focus()
{
//Focus the tab:
gtk_notebook_set_current_page(
GTK_NOTEBOOK(geany->main_widgets->message_window_notebook),
gtk_notebook_page_num(GTK_NOTEBOOK(geany->main_widgets->message_window_notebook), panel)
);
}
static void output_prepare()
{
GeanyDocument *doc;
switch(executed_tool->output) {
case TOOL_OUTPUT_REPLACE_LINE:
doc = document_get_current();
gint line = sci_get_current_line(doc->editor->sci);
gint length = sci_get_lines_selected(doc->editor->sci) - 1;
gint start = sci_get_pos_at_line_sel_start(doc->editor->sci, line);
gint end = sci_get_pos_at_line_sel_end(doc->editor->sci, line + length);
sci_set_selection_start(doc->editor->sci, start);
sci_set_selection_end(doc->editor->sci, end);
break;
case TOOL_OUTPUT_MESSAGE_TABLE:
table_prepare();
case TOOL_OUTPUT_MESSAGE_TEXT:
panel_prepare();
output_focus();
break;
case TOOL_OUTPUT_NEW_DOCUMENT:
document_new_file(NULL, NULL, NULL);
break;
}
}
static gboolean output_out(GIOChannel *channel, GIOCondition cond, gpointer type)
{
gchar *string;
if(cond == G_IO_HUP)
{
g_io_channel_unref(channel);
return FALSE;
}
GeanyDocument *doc = document_get_current();
GIOStatus st;
if((st = g_io_channel_read_line(channel, &string, NULL, NULL, NULL)) == G_IO_STATUS_NORMAL && string)
{
if(GPOINTER_TO_UINT(type) == 1) {
output_focus();
panel_print(string, "error");
}
else {
switch(executed_tool->output) {
case TOOL_OUTPUT_MESSAGE_TEXT:
panel_print(string, NULL);
break;
case TOOL_OUTPUT_MESSAGE_TABLE:
table_print(string, NULL);
break;
case TOOL_OUTPUT_REPLACE_SELECTED:
case TOOL_OUTPUT_REPLACE_LINE:
sci_replace_sel(doc->editor->sci, string);
break;
case TOOL_OUTPUT_REPLACE_WORD:
//TODO
break;
case TOOL_OUTPUT_APPEND_CURRENT_DOCUMENT:
case TOOL_OUTPUT_NEW_DOCUMENT:
sci_insert_text(doc->editor->sci, sci_get_length(doc->editor->sci), string);
break;
}
}
g_free(string);
}
return TRUE;
}
void execute(Tool *tool)
{
executed_tool = tool;
printf("TOOL EXECUTED: %s\n", tool->name);
output_prepare();
GeanyDocument *doc = document_get_current();
if(tool->save) {
document_save_file(doc, TRUE);
}
GError *error = NULL;
gint std_out, std_err;
gchar *cmd = tool->id;
char geany_line_number[32];
gint line = sci_get_current_line(doc->editor->sci);
snprintf(geany_line_number, 32, "%d", line + 1);
const gchar *project_dir;
GeanyProject *project = geany->app->project;
if(project) {
project_dir = project->base_path;
}
else {
project_dir = home;
}
gchar **env = utils_copy_environment(
NULL,
"GEANY_LINE_NUMBER", geany_line_number,
"GEANY_SELECTION", sci_get_selection_contents(doc->editor->sci),
"GEANY_SELECTED_LINE", sci_get_line(doc->editor->sci, line),
"GEANY_FILE_PATH", doc->file_name,
"GEANY_FILE_MIME_TYPE", doc->file_type->mime_type,
"GEANY_FILE_TYPE_NAME", doc->file_type->name,
"GEANY_PROJECT_DIRECTORY", project_dir,
NULL
);
gchar **argv;
if(g_shell_parse_argv(cmd, NULL, &argv, &error))
{
if(g_spawn_async_with_pipes(
project_dir,
argv,
env,
0, NULL, NULL, NULL, NULL,
&std_out,
&std_err,
&error
))
{
#ifdef G_OS_WIN32
GIOChannel *err_channel = g_io_channel_win32_new_fd(std_err);
GIOChannel *out_channel = g_io_channel_win32_new_fd(std_out);
#else
GIOChannel *err_channel = g_io_channel_unix_new(std_err);
GIOChannel *out_channel = g_io_channel_unix_new(std_out);
#endif
g_io_channel_set_flags(out_channel, G_IO_FLAG_NONBLOCK, NULL);
g_io_channel_set_encoding(out_channel, NULL, NULL);
g_io_add_watch(out_channel, G_IO_IN | G_IO_HUP, (GIOFunc)output_out, GUINT_TO_POINTER(0));
g_io_channel_set_flags(err_channel, G_IO_FLAG_NONBLOCK, NULL);
g_io_channel_set_encoding(err_channel, NULL, NULL);
g_io_add_watch(err_channel, G_IO_IN | G_IO_HUP, (GIOFunc)output_out, GUINT_TO_POINTER(1));
}
else {
printf("ERROR %s: %s (%d, %d)\n", cmd, error->message, std_out, std_err);
ui_set_statusbar(TRUE, _("ERROR %s: %s (%d, %d)"), cmd, error->message, std_out, std_err);
g_error_free(error);
}
}
else
{
ui_set_statusbar(TRUE, _("Tool failed: %s"), error->message);
g_error_free(error);
}
g_free(env);
}

272
panel.c Normal file
View File

@ -0,0 +1,272 @@
#include <geanyplugin.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
#define foreach_slist_free(node, list) for (node = list, list = NULL; g_slist_free_1(list), node != NULL; list = node, node = node->next)
extern GeanyPlugin *geany_plugin;
extern GeanyData *geany_data;
extern GeanyFunctions *geany_functions;
extern const gchar *tools;
static GtkWidget *panel; //All contents of the panel
static GtkWidget *label;
static GtkWidget *text_view;
static GtkWidget *scrollable_text, *scrollable_table;
typedef struct
{
const gchar *text;
GRegex *regex;
} RegexSetting;
static RegexSetting fileRegexSetting = {"[\\w./-]+\\.[\\w./-]+(:\\d+)?", NULL};
static void open_path(gchar *path, gchar *line)
{
GeanyDocument *doc = document_open_file(path, FALSE, NULL, NULL);
if(line != NULL) {
editor_goto_line(doc->editor, atoi(line) - 1, 0);
}
}
static gboolean find_files(gchar *base, const gchar *file, gchar* line, gboolean open)
{
gchar *path = g_build_path(G_DIR_SEPARATOR_S, base, file, NULL);
if(g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
if(open) {
open_path(path, line);
}
g_free(path);
return TRUE;
}
g_free(path);
GDir *dir;
gchar const *file_name;
dir = g_dir_open(base, 0, NULL);
foreach_dir(file_name, dir)
{
gchar *path = g_build_path(G_DIR_SEPARATOR_S, base, file_name, NULL);
if(g_file_test(path, G_FILE_TEST_IS_DIR)) {
if(find_files(path, file, line, open)) {
g_dir_close(dir);
g_free(path);
return TRUE;
}
}
g_free(path);
}
g_dir_close(dir);
return FALSE;
}
static gboolean file_found(gchar *file_path, gboolean open)
{
gchar **column = g_strsplit(file_path, ":", 2);
gchar *file = column[0];
gchar *line = column[1];
//Find in abs path
if(g_path_is_absolute(file) && g_file_test(file, G_FILE_TEST_IS_REGULAR)) {
if(open) {
open_path(file, line);
}
g_strfreev(column);
return TRUE;
}
//Find in current doc path:
GeanyDocument *doc = document_get_current();
gchar *current = g_path_get_dirname(doc->file_name);
if(find_files(current, file, line, open)) {
g_free(current);
g_strfreev(column);
return TRUE;
}
g_free(current);
//Find in Project Directory
gchar *base_directory;
GeanyProject *project = geany->app->project;
if(project) {
base_directory = project->base_path;
}
else {
base_directory = geany->prefs->default_open_path;
}
if(find_files(base_directory, file, line, open)) {
g_strfreev(column);
return TRUE;
}
g_strfreev(column);
return FALSE;
}
static void goto_link(gchar *url)
{
if(strcmp("", url) != 0) {
file_found(url, TRUE);
}
}
static gchar* get_link_at_iter(GtkTextIter in)
{
GtkTextIter iter = in;
gchar *text = "";
GSList *list, *node;
list = gtk_text_iter_get_tags(&iter);
if(list != NULL) {
foreach_slist_free(node, list) {
GtkTextTag *tag = node->data;
gchar *name;
g_object_get(G_OBJECT(tag), "name", &name, NULL);
if(strcmp(name, "link") == 0) {
GtkTextIter end = iter;
gtk_text_iter_backward_to_tag_toggle(&iter, tag);
gtk_text_iter_forward_to_tag_toggle(&end, tag);
text = gtk_text_iter_get_text(&iter, &end);
g_free(name);
break;
}
g_free(name);
}
}
return text;
}
static gboolean on_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
GtkTextIter iter;
gint x, y;
gtk_text_view_window_to_buffer_coords(GTK_TEXT_VIEW(text_view), GTK_TEXT_WINDOW_TEXT, event->x, event->y, &x, &y);
gtk_text_view_get_iter_at_location(GTK_TEXT_VIEW(text_view), &iter, x, y);
gchar *text = get_link_at_iter(iter);
goto_link(text);
return FALSE;
}
static gboolean on_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
if(event->keyval == GDK_Return) {
GtkTextIter iter;
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "insert");
gtk_text_buffer_get_iter_at_mark(buffer, &iter, cursor);
gchar *link = get_link_at_iter(iter);
goto_link(link);
}
return FALSE;
}
static gboolean panel_focus_tab(GtkWidget *widget, GdkEvent *event, gpointer data)
{
GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_MESSAGE_WINDOW);
if (kb != NULL && event->key.keyval == kb->key && (event->key.state & gtk_accelerator_get_default_mod_mask()) == kb->mods) {
gint current = gtk_notebook_get_current_page(GTK_NOTEBOOK(geany->main_widgets->message_window_notebook));
gint tab = gtk_notebook_page_num(GTK_NOTEBOOK(geany->main_widgets->message_window_notebook), panel);
if(current == tab) {
gtk_widget_grab_focus(scrollable_text);
}
}
return FALSE;
}
void panel_init()
{
label = gtk_label_new(_("Tools"));
panel = gtk_vbox_new(FALSE, 6);
text_view = gtk_text_view_new();
gtk_widget_modify_font(GTK_WIDGET(text_view), pango_font_description_from_string("Monospace"));
gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE);
scrollable_text = gtk_scrolled_window_new(NULL, NULL);
scrollable_table = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollable_text), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollable_table), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(scrollable_text), text_view);
gtk_box_pack_start(GTK_BOX(panel), scrollable_table, TRUE, TRUE, 2);
gtk_box_pack_start(GTK_BOX(panel), scrollable_text, TRUE, TRUE, 2);
gtk_notebook_append_page(
GTK_NOTEBOOK(geany->main_widgets->message_window_notebook),
panel,
label
);
gtk_widget_show(label);
gtk_widget_show(panel);
gtk_widget_show_all(scrollable_text);
gtk_container_set_focus_child(GTK_CONTAINER(panel), scrollable_text);
g_signal_connect(geany->main_widgets->window, "key-release-event", G_CALLBACK(panel_focus_tab), NULL);
g_signal_connect(text_view, "button-press-event", G_CALLBACK(on_click), NULL);
g_signal_connect(text_view, "key-press-event", G_CALLBACK(on_keypress), NULL);
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
gtk_text_buffer_create_tag(buffer, "error", "foreground", "#ff0000", NULL);
gtk_text_buffer_create_tag(buffer, "link", "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, NULL);
fileRegexSetting.regex = g_regex_new(fileRegexSetting.text, G_REGEX_OPTIMIZE | G_REGEX_CASELESS, 0, NULL);
}
void panel_cleanup()
{
gtk_notebook_remove_page(
GTK_NOTEBOOK(geany->main_widgets->message_window_notebook),
gtk_notebook_page_num(GTK_NOTEBOOK(geany->main_widgets->message_window_notebook), panel)
);
g_regex_unref(fileRegexSetting.regex);
}
void panel_prepare()
{
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)), "", 0);
gtk_widget_hide(scrollable_table);
}
void panel_print(gchar *text, const gchar *tag)
{
GtkTextIter iter, start, end;
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
gtk_text_buffer_get_end_iter(buffer, &iter);
if(tag == NULL) {
gtk_text_buffer_insert(buffer, &iter, text, -1);
}
else {
gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, text, -1, tag, NULL);
}
end = iter;
gtk_text_iter_backward_line(&iter);
start = iter;
gchar *line_text = gtk_text_iter_get_text(&start, &end);
GMatchInfo *info;
if(g_regex_match(fileRegexSetting.regex, line_text, 0, &info)) {
while(g_match_info_matches(info))
{
gchar *file_path = g_match_info_fetch(info, 0);
if(file_found(file_path, FALSE)) {
gint start_pos, end_pos;
g_match_info_fetch_pos(info, 0, &start_pos, &end_pos);
GtkTextIter start = iter;
GtkTextIter end = iter;
gtk_text_iter_forward_chars(&start, start_pos);
gtk_text_iter_forward_chars(&end, end_pos);
gtk_text_buffer_apply_tag_by_name(buffer, "link", &start, &end);
}
g_free(file_path);
g_match_info_next(info, NULL);
}
}
g_match_info_free(info);
g_free(line_text);
//Scroll to bottom:
GtkTextMark *mark;
mark = gtk_text_buffer_get_insert(buffer);
gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(text_view), mark, 0.0, FALSE, 0.0, 0.0);
}

42
table.c Normal file
View File

@ -0,0 +1,42 @@
#include <geanyplugin.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
extern GtkWidget *scrollable_table;
static GtkTreeStore *table;
static GtkWidget *tree;
static gboolean init;
void table_prepare()
{
printf("TABLE_PREPARE\n");
init = TRUE;
}
void table_print(gchar *text, const gchar *tag)
{
printf("TABLE_PRINT\n");
if(init) {
init = FALSE;
if(tree != NULL) {
gtk_container_remove(GTK_CONTAINER(scrollable_table), tree);
//G_OBJECT_FREE(tree);
}
table = gtk_tree_store_new(1, G_TYPE_STRING);
tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(table));
GtkCellRenderer *render = gtk_cell_renderer_text_new();
GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes(text, render, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
gtk_container_add(GTK_CONTAINER(scrollable_table), tree);
gtk_widget_show_all(scrollable_table);
}
else {
GtkTreeIter row;
gtk_tree_store_append(table, &row, NULL);
gtk_tree_store_set(table, &row, 0, text, -1);
}
}

159
tool.c Normal file
View File

@ -0,0 +1,159 @@
#include "tool.h"
#include "output.c"
extern GeanyPlugin *geany_plugin;
extern GeanyData *geany_data;
extern GeanyFunctions *geany_functions;
extern const gchar *tools, *conf;
static GKeyFile *config;
static GeanyKeyGroup *key_group;
static gint shortcutCount, menuCount;
static void key_callback(G_GNUC_UNUSED guint key_id)
{
plugin_show_configure(geany_plugin);
}
Tool* new_tool()
{
gchar* id = tempnam(tools, "tool");
g_creat(id, 0744);
gchar* name = g_strdup(_("New Tool"));
Tool init = {id, name, -1, FALSE, FALSE, FALSE};
Tool *tool = g_slice_new(Tool);
*tool = init;
return tool;
}
void free_tool(Tool* tool, gboolean purge)
{
if(purge) {
g_key_file_remove_group(config, tool->id, NULL);
}
g_free(tool->id);
g_free(tool->name);
g_slice_free(Tool, tool);
}
static void tool_menu_callback(GtkToggleButton *cb, gpointer data)
{
execute(data);
}
static void tool_shortcut_callback(G_GNUC_UNUSED guint key_id)
{
execute(shortcut_tools[key_id]);
}
int count_tools(Tool* tool)
{
if(tool->shortcut) {
shortcutCount++;
}
if(tool->menu) {
menuCount++;
}
}
int setup_tools(Tool* tool)
{
if(tool->shortcut) {
shortcut_tools[shortcutCount] = tool;
keybindings_set_item(key_group, shortcutCount, tool_shortcut_callback, 0, 0,
tool->id, tool->name, NULL);
shortcutCount++;
}
if(tool->menu) {
GtkWidget *tool_menu_item = gtk_menu_item_new_with_mnemonic(tool->name);
menu_tools[menuCount] = tool_menu_item;
gtk_widget_show(tool_menu_item);
gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), tool_menu_item);
g_signal_connect(tool_menu_item, "activate", G_CALLBACK(tool_menu_callback), tool);
menuCount++;
}
}
void clean_tools()
{
gchar *data = g_key_file_to_data(config, NULL, NULL);
utils_write_file(conf, data);
g_free(data);
gint i = 0;
while(i < menuCount) {
gtk_container_remove(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_tools[i]);
i++;
}
i = 0;
while(i < shortcutCount) {
free_tool(shortcut_tools[i], FALSE);
i++;
}
shortcutCount = 0;
menuCount = 0;
}
Tool* load_tool(gchar *id)
{
Tool init = {
id,
g_key_file_get_string(config, id, "name", NULL),
g_key_file_get_integer(config, id, "output", NULL),
g_key_file_get_boolean(config, id, "save", NULL),
g_key_file_get_boolean(config, id, "menu", NULL),
g_key_file_get_boolean(config, id, "shortcut", NULL)
};
Tool *tool = g_slice_new(Tool);
*tool = init;
return tool;
}
void load_tools(int (*callback)(Tool*))
{
if(callback == NULL) {
return;
}
//Load existing tools (in groups)
gsize len;
gchar **groups = g_key_file_get_groups(config, &len);
gsize i;
for(i = 0; i < len; i++)
{
Tool *tool = load_tool(groups[i]);
callback(tool);
}
}
void reload_tools()
{
shortcutCount = 0;
menuCount = 0;
if(config != NULL) {
g_key_file_free(config);
}
config = g_key_file_new();
g_key_file_load_from_file(config, conf, G_KEY_FILE_NONE, NULL);
load_tools(count_tools);
key_group = plugin_set_key_group(geany_plugin, "external_tools_keyboard_shortcut", shortcutCount + 1, NULL);
shortcut_tools = (Tool **) g_malloc(shortcutCount * sizeof(Tool*));
menu_tools = (GtkWidget **) g_malloc(menuCount * sizeof(GtkWidget*));
shortcutCount = 0;
menuCount = 0;
load_tools(setup_tools);
keybindings_set_item(key_group, shortcutCount, key_callback, 0, 0, "external_tools_keyboard_shortcut", _("External Tools..."), NULL);
keybindings_load_keyfile();
}
int save_tool(Tool* tool)
{
g_key_file_set_string(config, tool->id, "name", tool->name);
g_key_file_set_integer(config, tool->id, "output", tool->output);
g_key_file_set_boolean(config, tool->id, "save", tool->save);
g_key_file_set_boolean(config, tool->id, "menu", tool->menu);
g_key_file_set_boolean(config, tool->id, "shortcut", tool->shortcut);
}

35
tool.h Normal file
View File

@ -0,0 +1,35 @@
#include <geanyplugin.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
enum
{
SAVE = 0,
MENU,
SHORTCUT
};
enum
{
TOOL_OUTPUT_NONE = 0,
TOOL_OUTPUT_MESSAGE_TEXT,
TOOL_OUTPUT_MESSAGE_TABLE,
TOOL_OUTPUT_REPLACE_SELECTED,
TOOL_OUTPUT_REPLACE_LINE,
TOOL_OUTPUT_REPLACE_WORD,
TOOL_OUTPUT_APPEND_CURRENT_DOCUMENT,
TOOL_OUTPUT_NEW_DOCUMENT
};
typedef struct
{
gchar *id;
gchar *name;
gint output;
gboolean save;
gboolean menu;
gboolean shortcut;
} Tool;
static Tool **shortcut_tools;
static GtkWidget **menu_tools;