| 1 |
/* |
| 2 |
* Copyright 2010-2014 Jiri Techet <[email protected]> |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <sys/time.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include "config.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <geanyplugin.h> |
| 27 |
|
| 28 |
#include "readtags.h" |
| 29 |
|
| 30 |
#include <errno.h> |
| 31 |
#include <glib/gstdio.h> |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <signal.h> |
| 39 |
|
| 40 |
|
| 41 |
/* Pre-GTK 2.24 compatibility */ |
| 42 |
#ifndef GTK_COMBO_BOX_TEXT |
| 43 |
# define GTK_COMBO_BOX_TEXT GTK_COMBO_BOX |
| 44 |
# define gtk_combo_box_text_new gtk_combo_box_new_text |
| 45 |
# define gtk_combo_box_text_append_text gtk_combo_box_append_text |
| 46 |
#endif |
| 47 |
|
| 48 |
|
| 49 |
static GeanyPlugin *geany_plugin = NULL; |
| 50 |
static GeanyData *geany_data = NULL; |
| 51 |
|
| 52 |
|
| 53 |
static GtkWidget *s_context_fdec_item, *s_context_fdef_item, *s_context_sep_item, |
| 54 |
*s_gt_item, *s_sep_item, *s_ft_item; |
| 55 |
|
| 56 |
static struct |
| 57 |
{ |
| 58 |
GtkWidget *widget; |
| 59 |
|
| 60 |
GtkWidget *combo; |
| 61 |
GtkWidget *combo_match; |
| 62 |
GtkWidget *case_sensitive; |
| 63 |
GtkWidget *declaration; |
| 64 |
} s_ft_dialog = {NULL, NULL, NULL, NULL, NULL}; |
| 65 |
|
| 66 |
|
| 67 |
enum |
| 68 |
{ |
| 69 |
KB_FIND_TAG, |
| 70 |
KB_GENERATE_TAGS, |
| 71 |
KB_COUNT |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static void set_widgets_sensitive(gboolean sensitive) |
| 77 |
{ |
| 78 |
gtk_widget_set_sensitive(GTK_WIDGET(s_gt_item), sensitive); |
| 79 |
gtk_widget_set_sensitive(GTK_WIDGET(s_ft_item), sensitive); |
| 80 |
gtk_widget_set_sensitive(GTK_WIDGET(s_context_fdec_item), sensitive); |
| 81 |
gtk_widget_set_sensitive(GTK_WIDGET(s_context_fdef_item), sensitive); |
| 82 |
} |
| 83 |
|
| 84 |
static void on_project_open(G_GNUC_UNUSED GObject * obj, GKeyFile * config, G_GNUC_UNUSED gpointer user_data) |
| 85 |
{ |
| 86 |
set_widgets_sensitive(TRUE); |
| 87 |
} |
| 88 |
|
| 89 |
static void on_project_close(G_GNUC_UNUSED GObject * obj, G_GNUC_UNUSED gpointer user_data) |
| 90 |
{ |
| 91 |
set_widgets_sensitive(FALSE); |
| 92 |
} |
| 93 |
|
| 94 |
static void on_project_save(G_GNUC_UNUSED GObject * obj, GKeyFile * config, G_GNUC_UNUSED gpointer user_data) |
| 95 |
{ |
| 96 |
set_widgets_sensitive(TRUE); |
| 97 |
} |
| 98 |
|
| 99 |
static PluginCallback plugin_geanyctags_callbacks[] = { |
| 100 |
{"project-open", (GCallback) & on_project_open, TRUE, NULL}, |
| 101 |
{"project-close", (GCallback) & on_project_close, TRUE, NULL}, |
| 102 |
{"project-save", (GCallback) & on_project_save, TRUE, NULL}, |
| 103 |
{NULL, NULL, FALSE, NULL} |
| 104 |
}; |
| 105 |
|
| 106 |
static void plugin_geanyctags_help (G_GNUC_UNUSED GeanyPlugin *plugin, G_GNUC_UNUSED gpointer pdata) |
| 107 |
{ |
| 108 |
utils_open_browser("https://plugins.geany.org/geanyctags.html"); |
| 109 |
} |
| 110 |
|
| 111 |
static void spawn_cmd(const gchar *cmd, const gchar *dir) |
| 112 |
{ |
| 113 |
GError *error = NULL; |
| 114 |
gchar **argv = NULL; |
| 115 |
gchar *working_dir; |
| 116 |
gchar *utf8_working_dir; |
| 117 |
gchar *utf8_cmd_string; |
| 118 |
gchar *out; |
| 119 |
gint exitcode; |
| 120 |
gboolean success; |
| 121 |
GString *output; |
| 122 |
|
| 123 |
#ifndef G_OS_WIN32 |
| 124 |
/* run within shell so we can use pipes */ |
| 125 |
argv = g_new0(gchar *, 4); |
| 126 |
argv[0] = g_strdup("/bin/sh"); |
| 127 |
argv[1] = g_strdup("-c"); |
| 128 |
argv[2] = g_strdup(cmd); |
| 129 |
argv[3] = NULL; |
| 130 |
#endif |
| 131 |
|
| 132 |
utf8_cmd_string = utils_get_utf8_from_locale(cmd); |
| 133 |
utf8_working_dir = g_strdup(dir); |
| 134 |
working_dir = utils_get_locale_from_utf8(utf8_working_dir); |
| 135 |
|
| 136 |
msgwin_clear_tab(MSG_MESSAGE); |
| 137 |
msgwin_switch_tab(MSG_MESSAGE, TRUE); |
| 138 |
msgwin_msg_add(COLOR_BLUE, -1, NULL, _("%s (in directory: %s)"), utf8_cmd_string, utf8_working_dir); |
| 139 |
g_free(utf8_working_dir); |
| 140 |
g_free(utf8_cmd_string); |
| 141 |
|
| 142 |
output = g_string_new(NULL); |
| 143 |
#ifndef G_OS_WIN32 |
| 144 |
success = spawn_sync(working_dir, NULL, argv, NULL, |
| 145 |
NULL, NULL, output, &exitcode, &error); |
| 146 |
#else |
| 147 |
success = spawn_sync(working_dir, cmd, NULL, NULL, |
| 148 |
NULL, output, NULL, &exitcode, &error); |
| 149 |
#endif |
| 150 |
out = g_string_free(output, FALSE); |
| 151 |
if (!success || exitcode != 0) |
| 152 |
{ |
| 153 |
if (error != NULL) |
| 154 |
{ |
| 155 |
msgwin_msg_add(COLOR_RED, -1, NULL, _("Process execution failed (%s)"), error->message); |
| 156 |
g_error_free(error); |
| 157 |
} |
| 158 |
msgwin_msg_add(COLOR_RED, -1, NULL, "%s", out); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
msgwin_msg_add(COLOR_BLACK, -1, NULL, "%s", out); |
| 163 |
} |
| 164 |
|
| 165 |
/* cppcheck-suppress mismatchAllocDealloc symbolName=argv |
| 166 |
* argv is built manually, but is a valid GStrv */ |
| 167 |
g_strfreev(argv); |
| 168 |
g_free(working_dir); |
| 169 |
g_free(out); |
| 170 |
} |
| 171 |
|
| 172 |
static gchar *get_tags_filename(void) |
| 173 |
{ |
| 174 |
gchar *ret = NULL; |
| 175 |
|
| 176 |
if (geany_data->app->project) |
| 177 |
{ |
| 178 |
ret = utils_remove_ext_from_filename(geany_data->app->project->file_name); |
| 179 |
SETPTR(ret, g_strconcat(ret, ".tags", NULL)); |
| 180 |
} |
| 181 |
return ret; |
| 182 |
} |
| 183 |
|
| 184 |
static gchar *generate_find_string(GeanyProject *prj) |
| 185 |
{ |
| 186 |
gchar *ret; |
| 187 |
|
| 188 |
ret = g_strdup("find -L . -not -path '*/\\.*' -type f"); |
| 189 |
|
| 190 |
if (!EMPTY(prj->file_patterns)) |
| 191 |
{ |
| 192 |
guint i; |
| 193 |
|
| 194 |
SETPTR(ret, g_strconcat(ret, " \\( -name \"", prj->file_patterns[0], "\"", NULL)); |
| 195 |
for (i = 1; prj->file_patterns[i]; i++) |
| 196 |
SETPTR(ret, g_strconcat(ret, " -o -name \"", prj->file_patterns[i], "\"", NULL)); |
| 197 |
SETPTR(ret, g_strconcat(ret, " \\)", NULL)); |
| 198 |
} |
| 199 |
return ret; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
static gchar *get_base_path(void) |
| 204 |
{ |
| 205 |
gchar *ret; |
| 206 |
GeanyProject *prj = geany_data->app->project; |
| 207 |
gchar *project_dir_utf8; |
| 208 |
|
| 209 |
if (!prj) |
| 210 |
return NULL; |
| 211 |
|
| 212 |
if (g_path_is_absolute(prj->base_path)) |
| 213 |
return g_strdup(prj->base_path); |
| 214 |
|
| 215 |
project_dir_utf8 = g_path_get_dirname(prj->file_name); |
| 216 |
ret = g_build_filename(project_dir_utf8, prj->base_path, NULL); |
| 217 |
g_free(project_dir_utf8); |
| 218 |
return ret; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
static void |
| 223 |
on_generate_tags(GtkMenuItem *menuitem, gpointer user_data) |
| 224 |
{ |
| 225 |
GeanyProject *prj; |
| 226 |
|
| 227 |
prj = geany_data->app->project; |
| 228 |
if (prj) |
| 229 |
{ |
| 230 |
gchar *cmd; |
| 231 |
gchar *tag_filename; |
| 232 |
gchar *base_path; |
| 233 |
|
| 234 |
tag_filename = get_tags_filename(); |
| 235 |
|
| 236 |
#ifndef G_OS_WIN32 |
| 237 |
gchar *find_string = generate_find_string(prj); |
| 238 |
cmd = g_strconcat(find_string, |
| 239 |
" | ctags --totals --fields=fKsSt --extra=-fq --c-kinds=+p --sort=foldcase --excmd=number -L - -f '", |
| 240 |
tag_filename, "'", NULL); |
| 241 |
g_free(find_string); |
| 242 |
#else |
| 243 |
/* We don't have find and | on windows, generate tags for all files in the project (-R recursively) */ |
| 244 |
|
| 245 |
/* Unfortunately, there's a bug in ctags - when run with -R, the first line is |
| 246 |
* empty, ctags doesn't recognize the tags file as a valid ctags file and |
| 247 |
* refuses to overwrite it. Therefore, we need to delete the tags file manually. */ |
| 248 |
g_unlink(tag_filename); |
| 249 |
|
| 250 |
cmd = g_strconcat("ctags.exe -R --totals --fields=fKsSt --extra=-fq --c-kinds=+p --sort=foldcase --excmd=number -f \"", |
| 251 |
tag_filename, "\"", NULL); |
| 252 |
#endif |
| 253 |
|
| 254 |
base_path = get_base_path(); |
| 255 |
spawn_cmd(cmd, base_path); |
| 256 |
|
| 257 |
g_free(cmd); |
| 258 |
g_free(tag_filename); |
| 259 |
g_free(base_path); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
static void show_entry(tagEntry *entry) |
| 264 |
{ |
| 265 |
const gchar *kind; |
| 266 |
const gchar *signature; |
| 267 |
const gchar *scope; |
| 268 |
const gchar *file; |
| 269 |
const gchar *name; |
| 270 |
gchar *scope_str; |
| 271 |
gchar *kind_str; |
| 272 |
|
| 273 |
file = entry->file; |
| 274 |
if (!file) |
| 275 |
file = ""; |
| 276 |
|
| 277 |
name = entry->name; |
| 278 |
if (!name) |
| 279 |
name = ""; |
| 280 |
|
| 281 |
signature = tagsField(entry, "signature"); |
| 282 |
if (!signature) |
| 283 |
signature = ""; |
| 284 |
|
| 285 |
scope = tagsField(entry, "class"); |
| 286 |
if (!scope) |
| 287 |
scope = tagsField(entry, "struct"); |
| 288 |
if (!scope) |
| 289 |
scope = tagsField(entry, "union"); |
| 290 |
if (!scope) |
| 291 |
scope = tagsField(entry, "enum"); |
| 292 |
|
| 293 |
if (scope) |
| 294 |
scope_str = g_strconcat(scope, "::", NULL); |
| 295 |
else |
| 296 |
scope_str = g_strdup(""); |
| 297 |
|
| 298 |
kind = entry->kind; |
| 299 |
if (kind) |
| 300 |
{ |
| 301 |
kind_str = g_strconcat(kind, ": ", NULL); |
| 302 |
SETPTR(kind_str, g_strdup_printf("%-14s", kind_str)); |
| 303 |
} |
| 304 |
else |
| 305 |
kind_str = g_strdup(""); |
| 306 |
|
| 307 |
msgwin_msg_add(COLOR_BLACK, -1, NULL, "%s:%lu:\n %s%s%s%s", file, |
| 308 |
entry->address.lineNumber, kind_str, scope_str, name, signature); |
| 309 |
|
| 310 |
g_free(scope_str); |
| 311 |
g_free(kind_str); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
static gchar *get_selection(void) |
| 316 |
{ |
| 317 |
gchar *ret = NULL; |
| 318 |
GeanyDocument *doc = document_get_current(); |
| 319 |
GeanyEditor *editor; |
| 320 |
|
| 321 |
if (!doc) |
| 322 |
return NULL; |
| 323 |
|
| 324 |
editor = doc->editor; |
| 325 |
|
| 326 |
if (sci_has_selection(editor->sci)) |
| 327 |
ret = sci_get_selection_contents(editor->sci); |
| 328 |
else |
| 329 |
ret = editor_get_word_at_pos(editor, -1, GEANY_WORDCHARS); |
| 330 |
|
| 331 |
return ret; |
| 332 |
} |
| 333 |
|
| 334 |
/* TODO: Not possible to do it the way below because some of the API is private |
| 335 |
* in Geany. This means the cursor has to be placed at the symbol first and |
| 336 |
* then right-clicked (right-clicking without having the cursor at the symbol |
| 337 |
* doesn't work) */ |
| 338 |
|
| 339 |
/* |
| 340 |
static gchar *get_selection() |
| 341 |
{ |
| 342 |
gchar *ret = NULL; |
| 343 |
GeanyDocument *doc = document_get_current(); |
| 344 |
|
| 345 |
if (!doc) |
| 346 |
return NULL; |
| 347 |
|
| 348 |
if (!sci_has_selection(doc->editor->sci)) |
| 349 |
sci_set_current_position(doc->editor->sci, editor_info.click_pos, FALSE); |
| 350 |
|
| 351 |
if (sci_has_selection(doc->editor->sci)) |
| 352 |
return sci_get_selection_contents(doc->editor->sci); |
| 353 |
|
| 354 |
gint len = sci_get_selected_text_length2(doc->editor->sci); |
| 355 |
|
| 356 |
ret = g_malloc(len + 1); |
| 357 |
sci_get_selected_text(doc->editor->sci, ret); |
| 358 |
|
| 359 |
editor_find_current_word(doc->editor, -1, |
| 360 |
editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL); |
| 361 |
|
| 362 |
return editor_info.current_word != 0 ? g_strdup(editor_info.current_word) : NULL; |
| 363 |
} |
| 364 |
*/ |
| 365 |
|
| 366 |
typedef enum |
| 367 |
{ |
| 368 |
MATCH_FULL, |
| 369 |
MATCH_PREFIX, |
| 370 |
MATCH_PATTERN |
| 371 |
} MatchType; |
| 372 |
|
| 373 |
static gboolean find_first(tagFile *tf, tagEntry *entry, const gchar *name, MatchType match_type) |
| 374 |
{ |
| 375 |
gboolean ret; |
| 376 |
|
| 377 |
if (match_type == MATCH_PATTERN) |
| 378 |
ret = tagsFirst(tf, entry) == TagSuccess; |
| 379 |
else |
| 380 |
{ |
| 381 |
int options = TAG_IGNORECASE; |
| 382 |
|
| 383 |
options |= match_type == MATCH_PREFIX ? TAG_PARTIALMATCH : TAG_FULLMATCH; |
| 384 |
ret = tagsFind(tf, entry, name, options) == TagSuccess; |
| 385 |
} |
| 386 |
return ret; |
| 387 |
} |
| 388 |
|
| 389 |
static gboolean find_next(tagFile *tf, tagEntry *entry, MatchType match_type) |
| 390 |
{ |
| 391 |
gboolean ret; |
| 392 |
|
| 393 |
if (match_type == MATCH_PATTERN) |
| 394 |
ret = tagsNext(tf, entry) == TagSuccess; |
| 395 |
else |
| 396 |
ret = tagsFindNext(tf, entry) == TagSuccess; |
| 397 |
return ret; |
| 398 |
} |
| 399 |
|
| 400 |
#if ! GLIB_CHECK_VERSION(2, 70, 0) |
| 401 |
# define g_pattern_spec_match_string g_pattern_match_string |
| 402 |
#endif |
| 403 |
|
| 404 |
static gboolean filter_tag(tagEntry *entry, GPatternSpec *name, gboolean declaration, gboolean case_sensitive) |
| 405 |
{ |
| 406 |
gboolean filter = TRUE; |
| 407 |
gchar *entry_name; |
| 408 |
|
| 409 |
if (!EMPTY(entry->kind)) |
| 410 |
{ |
| 411 |
gboolean is_prototype; |
| 412 |
|
| 413 |
is_prototype = g_strcmp0(entry->kind, "prototype") == 0; |
| 414 |
filter = (declaration && !is_prototype) || (!declaration && is_prototype); |
| 415 |
if (filter) |
| 416 |
return TRUE; |
| 417 |
} |
| 418 |
|
| 419 |
if (case_sensitive) |
| 420 |
entry_name = g_strdup(entry->name); |
| 421 |
else |
| 422 |
entry_name = g_utf8_strdown(entry->name, -1); |
| 423 |
|
| 424 |
filter = !g_pattern_spec_match_string(name, entry_name); |
| 425 |
|
| 426 |
g_free(entry_name); |
| 427 |
|
| 428 |
return filter; |
| 429 |
} |
| 430 |
|
| 431 |
static void find_tags(const gchar *name, gboolean declaration, gboolean case_sensitive, MatchType match_type) |
| 432 |
{ |
| 433 |
tagFile *tf; |
| 434 |
GeanyProject *prj; |
| 435 |
gchar *tag_filename = NULL; |
| 436 |
gchar *base_path; |
| 437 |
tagEntry entry; |
| 438 |
tagFileInfo info; |
| 439 |
int last_line_number = 0; |
| 440 |
|
| 441 |
prj = geany_data->app->project; |
| 442 |
if (!prj) |
| 443 |
return; |
| 444 |
|
| 445 |
base_path = get_base_path(); |
| 446 |
msgwin_clear_tab(MSG_MESSAGE); |
| 447 |
msgwin_set_messages_dir(base_path); |
| 448 |
|
| 449 |
tag_filename = get_tags_filename(); |
| 450 |
tf = tagsOpen(tag_filename, &info); |
| 451 |
|
| 452 |
if (tf) |
| 453 |
{ |
| 454 |
if (find_first(tf, &entry, name, match_type)) |
| 455 |
{ |
| 456 |
GPatternSpec *name_pat; |
| 457 |
gchar *name_case; |
| 458 |
gchar *path = NULL; |
| 459 |
gint num = 0; |
| 460 |
|
| 461 |
if (case_sensitive) |
| 462 |
name_case = g_strdup(name); |
| 463 |
else |
| 464 |
name_case = g_utf8_strdown(name, -1); |
| 465 |
|
| 466 |
SETPTR(name_case, g_strconcat("*", name_case, "*", NULL)); |
| 467 |
name_pat = g_pattern_spec_new(name_case); |
| 468 |
|
| 469 |
if (!filter_tag(&entry, name_pat, declaration, case_sensitive)) |
| 470 |
{ |
| 471 |
path = g_build_filename(base_path, entry.file, NULL); |
| 472 |
show_entry(&entry); |
| 473 |
last_line_number = entry.address.lineNumber; |
| 474 |
num++; |
| 475 |
} |
| 476 |
|
| 477 |
while (find_next(tf, &entry, match_type)) |
| 478 |
{ |
| 479 |
if (!filter_tag(&entry, name_pat, declaration, case_sensitive)) |
| 480 |
{ |
| 481 |
if (!path) |
| 482 |
path = g_build_filename(base_path, entry.file, NULL); |
| 483 |
show_entry(&entry); |
| 484 |
last_line_number = entry.address.lineNumber; |
| 485 |
num++; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
if (num == 1) |
| 490 |
{ |
| 491 |
GeanyDocument *old_doc = document_get_current(); |
| 492 |
GeanyDocument *doc = document_open_file(path, FALSE, NULL, NULL); |
| 493 |
if (doc != NULL) |
| 494 |
{ |
| 495 |
navqueue_goto_line(old_doc, doc, last_line_number); |
| 496 |
gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci)); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
g_pattern_spec_free(name_pat); |
| 501 |
g_free(name_case); |
| 502 |
g_free(path); |
| 503 |
} |
| 504 |
|
| 505 |
tagsClose(tf); |
| 506 |
} |
| 507 |
|
| 508 |
msgwin_switch_tab(MSG_MESSAGE, TRUE); |
| 509 |
|
| 510 |
g_free(tag_filename); |
| 511 |
g_free(base_path); |
| 512 |
} |
| 513 |
|
| 514 |
static void on_find_declaration(GtkMenuItem *menuitem, gpointer user_data) |
| 515 |
{ |
| 516 |
gchar *name; |
| 517 |
|
| 518 |
name = get_selection(); |
| 519 |
if (name) |
| 520 |
find_tags(name, TRUE, TRUE, MATCH_FULL); |
| 521 |
g_free(name); |
| 522 |
} |
| 523 |
|
| 524 |
static void on_find_definition(GtkMenuItem *menuitem, gpointer user_data) |
| 525 |
{ |
| 526 |
gchar *name; |
| 527 |
|
| 528 |
name = get_selection(); |
| 529 |
if (name) |
| 530 |
find_tags(name, FALSE, TRUE, MATCH_FULL); |
| 531 |
g_free(name); |
| 532 |
} |
| 533 |
|
| 534 |
static void create_dialog_find_file(void) |
| 535 |
{ |
| 536 |
GtkWidget *label, *vbox, *ebox, *entry; |
| 537 |
GtkSizeGroup *size_group; |
| 538 |
|
| 539 |
if (s_ft_dialog.widget) |
| 540 |
return; |
| 541 |
|
| 542 |
s_ft_dialog.widget = gtk_dialog_new_with_buttons( |
| 543 |
_("Find Tag"), GTK_WINDOW(geany->main_widgets->window), |
| 544 |
GTK_DIALOG_DESTROY_WITH_PARENT, |
| 545 |
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL); |
| 546 |
gtk_dialog_add_button(GTK_DIALOG(s_ft_dialog.widget), "gtk-find", GTK_RESPONSE_ACCEPT); |
| 547 |
gtk_dialog_set_default_response(GTK_DIALOG(s_ft_dialog.widget), GTK_RESPONSE_ACCEPT); |
| 548 |
|
| 549 |
vbox = ui_dialog_vbox_new(GTK_DIALOG(s_ft_dialog.widget)); |
| 550 |
gtk_box_set_spacing(GTK_BOX(vbox), 9); |
| 551 |
|
| 552 |
size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL); |
| 553 |
|
| 554 |
label = gtk_label_new_with_mnemonic(_("_Search for:")); |
| 555 |
gtk_label_set_xalign(GTK_LABEL(label), 0.0); |
| 556 |
gtk_size_group_add_widget(size_group, label); |
| 557 |
|
| 558 |
s_ft_dialog.combo = gtk_combo_box_text_new_with_entry(); |
| 559 |
entry = gtk_bin_get_child(GTK_BIN(s_ft_dialog.combo)); |
| 560 |
|
| 561 |
ui_entry_add_clear_icon(GTK_ENTRY(entry)); |
| 562 |
gtk_entry_set_width_chars(GTK_ENTRY(entry), 40); |
| 563 |
gtk_label_set_mnemonic_widget(GTK_LABEL(label), entry); |
| 564 |
ui_entry_add_clear_icon(GTK_ENTRY(entry)); |
| 565 |
gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE); |
| 566 |
|
| 567 |
ebox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6); |
| 568 |
gtk_box_pack_start(GTK_BOX(ebox), label, FALSE, FALSE, 0); |
| 569 |
gtk_box_pack_start(GTK_BOX(ebox), s_ft_dialog.combo, TRUE, TRUE, 0); |
| 570 |
gtk_box_pack_start(GTK_BOX(vbox), ebox, TRUE, FALSE, 0); |
| 571 |
|
| 572 |
label = gtk_label_new_with_mnemonic(_("_Match type:")); |
| 573 |
gtk_label_set_xalign(GTK_LABEL(label), 0.0); |
| 574 |
gtk_size_group_add_widget(size_group, label); |
| 575 |
|
| 576 |
s_ft_dialog.combo_match = gtk_combo_box_text_new(); |
| 577 |
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(s_ft_dialog.combo_match), _("exact")); |
| 578 |
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(s_ft_dialog.combo_match), _("prefix")); |
| 579 |
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(s_ft_dialog.combo_match), _("pattern")); |
| 580 |
gtk_combo_box_set_active(GTK_COMBO_BOX(s_ft_dialog.combo_match), 1); |
| 581 |
gtk_label_set_mnemonic_widget(GTK_LABEL(label), s_ft_dialog.combo_match); |
| 582 |
|
| 583 |
ebox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6); |
| 584 |
gtk_box_pack_start(GTK_BOX(ebox), label, FALSE, FALSE, 0); |
| 585 |
gtk_box_pack_start(GTK_BOX(ebox), s_ft_dialog.combo_match, TRUE, TRUE, 0); |
| 586 |
gtk_box_pack_start(GTK_BOX(vbox), ebox, TRUE, FALSE, 0); |
| 587 |
|
| 588 |
s_ft_dialog.case_sensitive = gtk_check_button_new_with_mnemonic(_("C_ase sensitive")); |
| 589 |
gtk_widget_set_focus_on_click(s_ft_dialog.case_sensitive, FALSE); |
| 590 |
|
| 591 |
s_ft_dialog.declaration = gtk_check_button_new_with_mnemonic(_("_Declaration")); |
| 592 |
gtk_widget_set_focus_on_click(s_ft_dialog.declaration, FALSE); |
| 593 |
|
| 594 |
g_object_unref(G_OBJECT(size_group)); /* auto destroy the size group */ |
| 595 |
|
| 596 |
gtk_container_add(GTK_CONTAINER(vbox), s_ft_dialog.case_sensitive); |
| 597 |
gtk_container_add(GTK_CONTAINER(vbox), s_ft_dialog.declaration); |
| 598 |
gtk_widget_show_all(vbox); |
| 599 |
} |
| 600 |
|
| 601 |
static void on_find_tag(GtkMenuItem *menuitem, gpointer user_data) |
| 602 |
{ |
| 603 |
gchar *selection; |
| 604 |
GtkWidget *entry; |
| 605 |
|
| 606 |
create_dialog_find_file(); |
| 607 |
|
| 608 |
entry = gtk_bin_get_child(GTK_BIN(s_ft_dialog.combo)); |
| 609 |
|
| 610 |
selection = get_selection(); |
| 611 |
if (selection) |
| 612 |
gtk_entry_set_text(GTK_ENTRY(entry), selection); |
| 613 |
g_free(selection); |
| 614 |
|
| 615 |
gtk_widget_grab_focus(entry); |
| 616 |
|
| 617 |
if (gtk_dialog_run(GTK_DIALOG(s_ft_dialog.widget)) == GTK_RESPONSE_ACCEPT) |
| 618 |
{ |
| 619 |
const gchar *name; |
| 620 |
gboolean case_sensitive, declaration; |
| 621 |
MatchType match_type; |
| 622 |
|
| 623 |
name = gtk_entry_get_text(GTK_ENTRY(entry)); |
| 624 |
case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(s_ft_dialog.case_sensitive)); |
| 625 |
declaration = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(s_ft_dialog.declaration)); |
| 626 |
match_type = gtk_combo_box_get_active(GTK_COMBO_BOX(s_ft_dialog.combo_match)); |
| 627 |
|
| 628 |
ui_combo_box_add_to_history(GTK_COMBO_BOX_TEXT(s_ft_dialog.combo), name, 0); |
| 629 |
|
| 630 |
find_tags(name, declaration, case_sensitive, match_type); |
| 631 |
} |
| 632 |
|
| 633 |
gtk_widget_hide(s_ft_dialog.widget); |
| 634 |
} |
| 635 |
|
| 636 |
static gboolean kb_callback(guint key_id) |
| 637 |
{ |
| 638 |
switch (key_id) |
| 639 |
{ |
| 640 |
case KB_FIND_TAG: |
| 641 |
on_find_tag(NULL, NULL); |
| 642 |
return TRUE; |
| 643 |
case KB_GENERATE_TAGS: |
| 644 |
on_generate_tags(NULL, NULL); |
| 645 |
return TRUE; |
| 646 |
} |
| 647 |
return FALSE; |
| 648 |
} |
| 649 |
|
| 650 |
static gboolean plugin_geanyctags_init(GeanyPlugin *plugin, G_GNUC_UNUSED gpointer pdata) |
| 651 |
{ |
| 652 |
GeanyKeyGroup *key_group; |
| 653 |
|
| 654 |
geany_plugin = plugin; |
| 655 |
geany_data = plugin->geany_data; |
| 656 |
|
| 657 |
key_group = plugin_set_key_group(geany_plugin, "GeanyCtags", KB_COUNT, kb_callback); |
| 658 |
|
| 659 |
s_context_sep_item = gtk_separator_menu_item_new(); |
| 660 |
gtk_widget_show(s_context_sep_item); |
| 661 |
gtk_menu_shell_prepend(GTK_MENU_SHELL(geany->main_widgets->editor_menu), s_context_sep_item); |
| 662 |
|
| 663 |
s_context_fdec_item = gtk_menu_item_new_with_mnemonic(_("Find Tag Declaration (geanyctags)")); |
| 664 |
gtk_widget_show(s_context_fdec_item); |
| 665 |
gtk_menu_shell_prepend(GTK_MENU_SHELL(geany->main_widgets->editor_menu), s_context_fdec_item); |
| 666 |
g_signal_connect((gpointer) s_context_fdec_item, "activate", G_CALLBACK(on_find_declaration), NULL); |
| 667 |
|
| 668 |
s_context_fdef_item = gtk_menu_item_new_with_mnemonic(_("Find Tag Definition (geanyctags)")); |
| 669 |
gtk_widget_show(s_context_fdef_item); |
| 670 |
gtk_menu_shell_prepend(GTK_MENU_SHELL(geany->main_widgets->editor_menu), s_context_fdef_item); |
| 671 |
g_signal_connect((gpointer) s_context_fdef_item, "activate", G_CALLBACK(on_find_definition), NULL); |
| 672 |
|
| 673 |
s_sep_item = gtk_separator_menu_item_new(); |
| 674 |
gtk_widget_show(s_sep_item); |
| 675 |
gtk_container_add(GTK_CONTAINER(geany->main_widgets->project_menu), s_sep_item); |
| 676 |
|
| 677 |
s_gt_item = gtk_menu_item_new_with_mnemonic(_("Generate tags")); |
| 678 |
gtk_widget_show(s_gt_item); |
| 679 |
gtk_container_add(GTK_CONTAINER(geany->main_widgets->project_menu), s_gt_item); |
| 680 |
g_signal_connect((gpointer) s_gt_item, "activate", G_CALLBACK(on_generate_tags), NULL); |
| 681 |
keybindings_set_item(key_group, KB_GENERATE_TAGS, NULL, |
| 682 |
0, 0, "generate_tags", _("Generate tags"), s_gt_item); |
| 683 |
|
| 684 |
s_ft_item = gtk_menu_item_new_with_mnemonic(_("Find tag...")); |
| 685 |
gtk_widget_show(s_ft_item); |
| 686 |
gtk_container_add(GTK_CONTAINER(geany->main_widgets->project_menu), s_ft_item); |
| 687 |
g_signal_connect((gpointer) s_ft_item, "activate", G_CALLBACK(on_find_tag), NULL); |
| 688 |
keybindings_set_item(key_group, KB_FIND_TAG, NULL, |
| 689 |
0, 0, "find_tag", _("Find tag"), s_ft_item); |
| 690 |
|
| 691 |
set_widgets_sensitive(geany_data->app->project != NULL); |
| 692 |
|
| 693 |
return TRUE; |
| 694 |
} |
| 695 |
|
| 696 |
static void plugin_geanyctags_cleanup(G_GNUC_UNUSED GeanyPlugin *plugin, G_GNUC_UNUSED gpointer pdata) |
| 697 |
{ |
| 698 |
gtk_widget_destroy(s_context_fdec_item); |
| 699 |
gtk_widget_destroy(s_context_fdef_item); |
| 700 |
gtk_widget_destroy(s_context_sep_item); |
| 701 |
|
| 702 |
gtk_widget_destroy(s_ft_item); |
| 703 |
gtk_widget_destroy(s_gt_item); |
| 704 |
gtk_widget_destroy(s_sep_item); |
| 705 |
|
| 706 |
if (s_ft_dialog.widget) |
| 707 |
gtk_widget_destroy(s_ft_dialog.widget); |
| 708 |
s_ft_dialog.widget = NULL; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
/* Load module */ |
| 713 |
G_MODULE_EXPORT |
| 714 |
void geany_load_module(GeanyPlugin *plugin) |
| 715 |
{ |
| 716 |
/* Setup translation */ |
| 717 |
main_locale_init(LOCALEDIR, GETTEXT_PACKAGE); |
| 718 |
|
| 719 |
/* Set metadata */ |
| 720 |
plugin->info->name = "GeanyCtags"; |
| 721 |
plugin->info->description = _("Ctags generation and search plugin for geany projects"); |
| 722 |
plugin->info->version = VERSION; |
| 723 |
plugin->info->author = "Jiri Techet <[email protected]>"; |
| 724 |
|
| 725 |
/* Set functions */ |
| 726 |
plugin->funcs->init = plugin_geanyctags_init; |
| 727 |
plugin->funcs->cleanup = plugin_geanyctags_cleanup; |
| 728 |
plugin->funcs->help = plugin_geanyctags_help; |
| 729 |
plugin->funcs->callbacks = plugin_geanyctags_callbacks; |
| 730 |
|
| 731 |
/* Register! */ |
| 732 |
GEANY_PLUGIN_REGISTER(plugin, 226); |
| 733 |
} |