File: demopluginext.c

package info (click to toggle)
geany 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,448 kB
  • sloc: ansic: 168,606; cpp: 77,562; sh: 5,344; makefile: 1,694; cs: 1,233; javascript: 1,024; python: 580; f90: 537; vhdl: 504; sql: 503; lisp: 436; fortran: 389; php: 278; ada: 201; ruby: 163; java: 131; asm: 131; perl: 119; cobol: 88; tcl: 77; erlang: 73; xml: 66; ml: 27; sed: 16; pascal: 15; haskell: 6
file content (130 lines) | stat: -rw-r--r-- 4,219 bytes parent folder | download
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
/*
 *      demopluginext.c - this file is part of Geany, a fast and lightweight IDE
 *
 *      Copyright 2024 The Geany contributors
 *
 *      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.
 */

/**
 * Demo plugin extension - example of a plugin using the PluginExtension API
 * to provide simple autocompletion of Python keywords. This is the plugin
 * example used in the documentation with full implementation of
 * @c autocomplete_perform().
 *
 * Note: This is not installed by default, but (on *nix) you can build it as follows:
 * cd plugins
 * make demopluginext.so
 *
 * Then copy or symlink the plugins/demopluginext.so file to ~/.config/geany/plugins
 * - it will be loaded at next startup.
 */

#include <geanyplugin.h>

static gboolean autocomplete_provided(GeanyDocument *doc, gpointer data)
{
    return doc->file_type->id == GEANY_FILETYPES_PYTHON;
}


static void autocomplete_perform(GeanyDocument *doc, gboolean force, gpointer data)
{
    const gchar *kwd_str = "False None True and as assert async await break case class continue def del elif else except finally for from global if import in is lambda match nonlocal not or pass raise return try while with yield";
    gint pos = sci_get_current_position(doc->editor->sci);
    gchar *word = editor_get_word_at_pos(doc->editor, pos, NULL);
    gchar **kwd;

    if (word && *word)
    {
        GString *words = g_string_sized_new(100);
        gchar **kwds = g_strsplit(kwd_str, " ", -1);

        foreach_strv(kwd, kwds)
        {
            if (g_str_has_prefix(*kwd, word))
            {
                if (words->len > 0)
                    g_string_append(words, "\n");

                g_string_append(words, *kwd);
            }
        }

        scintilla_send_message(doc->editor->sci, SCI_AUTOCSHOW, strlen(word), (sptr_t) words->str);
        g_string_free(words, TRUE);
        g_strfreev(kwds);
    }

    g_free(word);
}


static PluginExtension extension = {
    .autocomplete_provided = autocomplete_provided,
    .autocomplete_perform = autocomplete_perform
};


static gboolean on_editor_notify(G_GNUC_UNUSED GObject *obj, GeanyEditor *editor, SCNotification *nt,
    G_GNUC_UNUSED gpointer user_data)
{
    if (nt->nmhdr.code == SCN_AUTOCSELECTION)
    {
        if (plugin_extension_autocomplete_provided(editor->document, &extension))
        {
            /* we can be sure it was us who performed the autocompletion and
             * not Geany or some other plugin extension */
            msgwin_status_add("PluginExtensionDemo autocompleted '%s'", nt->text);
        }
    }

    return FALSE;
}


static PluginCallback plugin_callbacks[] = {
    {"editor-notify", (GCallback) &on_editor_notify, FALSE, NULL},
    {NULL, NULL, FALSE, NULL}
};


static gboolean init_func(GeanyPlugin *plugin, gpointer pdata)
{
    plugin_extension_register(&extension, "Python keyword autocompletion", 450, NULL);
    return TRUE;
}


static void cleanup_func(GeanyPlugin *plugin, gpointer pdata)
{
    plugin_extension_unregister(&extension);
}


G_MODULE_EXPORT
void geany_load_module(GeanyPlugin *plugin)
{
    plugin->info->name = "PluginExtensionDemo";
    plugin->info->description = "Demo performing simple Python keyword autocompletion";
    plugin->info->version = "1.0";
    plugin->info->author = "John Doe <john.doe@example.org>";

    plugin->funcs->init = init_func;
    plugin->funcs->cleanup = cleanup_func;
    plugin->funcs->callbacks = plugin_callbacks;

    GEANY_PLUGIN_REGISTER(plugin, 248);
}