Updated Spanish translation
[anjuta-git-plugin.git] / plugins / search / plugin.c
blob254c81276eaa716d1ffb31847f4ee1ecc5a32428
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2000 Naba Kumar
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <libanjuta/interfaces/ianjuta-editor.h>
22 #include <libanjuta/interfaces/ianjuta-editor-selection.h>
23 #include <libanjuta/interfaces/ianjuta-document-manager.h>
24 #include <libanjuta/anjuta-shell.h>
25 #include <libanjuta/anjuta-debug.h>
27 #include <libegg/menu/egg-entry-action.h>
29 #include "plugin.h"
30 #include "search-replace.h"
31 #include "search-replace_backend.h"
32 #include "config.h"
34 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-search.ui"
35 #define ICON_FILE "anjuta-search-plugin-48.png"
37 #define ANJUTA_PIXMAP_MATCH_NEXT "anjuta-go-match-next"
38 #define ANJUTA_PIXMAP_MATCH_PREV "anjuta-go-match-prev"
39 #define ANJUTA_STOCK_MATCH_NEXT "anjuta-match-next"
40 #define ANJUTA_STOCK_MATCH_PREV "anjuta-match-prev"
42 /* Find next occurence of expression in Editor
43 Caching of FileBuffer might be useful here to improve performance
44 Returns: TRUE = found, FALSE = not found
47 static gboolean find_incremental(IAnjutaEditor* te, gchar* expression,
48 SearchDirection dir)
50 FileBuffer* fb = file_buffer_new_from_te (te);
51 SearchExpression* se = g_new0(SearchExpression, 1);
52 MatchInfo* info;
53 gboolean ret;
55 se->search_str = expression;
56 se->regex = FALSE;
57 se->greedy = FALSE;
58 se->ignore_case = TRUE;
59 se->whole_word = FALSE;
60 se->whole_line = FALSE;
61 se->word_start = FALSE;
62 se->no_limit = FALSE;
63 se->actions_max = 1;
64 se->re = NULL;
66 info = get_next_match(fb, dir, se);
68 if (info != NULL)
70 IAnjutaIterable *start, *end;
71 start = ianjuta_editor_get_cell_iter (te, info->pos, NULL);
72 end = ianjuta_editor_get_cell_iter (te, info->pos + info->len, NULL);
73 ianjuta_editor_selection_set (IANJUTA_EDITOR_SELECTION (te),
74 start, end, NULL);
75 g_object_unref (start);
76 g_object_unref (end);
77 ret = TRUE;
79 else
80 ret = FALSE;
82 match_info_free(info);
83 file_buffer_free(fb);
84 g_free(se);
86 return ret;
89 static void
90 on_find1_activate (GtkAction * action, gpointer user_data)
92 anjuta_search_replace_activate(FALSE, FALSE);
95 static void
96 on_find_and_replace1_activate (GtkAction * action, gpointer user_data)
98 anjuta_search_replace_activate(TRUE, FALSE);
101 static void
102 on_find_in_files1_activate (GtkAction * action, gpointer user_data)
104 anjuta_search_replace_activate(FALSE, TRUE);
107 /* *user_data : TRUE=Forward False=Backward */
108 static void
109 on_findnext1_activate (GtkAction * action, gpointer user_data)
111 search_replace_next();
114 static void
115 on_findprevious1_activate (GtkAction * action, gpointer user_data)
117 search_replace_previous();
120 static void
121 on_prev_occur(GtkAction * action, gpointer user_data)
123 IAnjutaEditor* te;
124 IAnjutaDocumentManager *docman;
125 IAnjutaDocument* doc;
126 SearchPlugin *plugin;
127 gint return_;
128 gchar *buffer = NULL;
130 plugin = ANJUTA_PLUGIN_SEARCH (user_data);
131 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
132 IAnjutaDocumentManager, NULL);
133 doc = ianjuta_document_manager_get_current_document(docman, NULL);
134 te = IANJUTA_IS_EDITOR(doc) ? IANJUTA_EDITOR(doc) : NULL;
135 if(!te) return;
136 if ((buffer = ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te), NULL)))
138 g_strstrip(buffer);
139 if ('\0' == *buffer)
141 g_free(buffer);
142 buffer = NULL;
145 if (NULL == buffer)
147 buffer = ianjuta_editor_get_current_word(te, NULL);
148 if (!buffer)
149 return;
151 return_= find_incremental(te, buffer, SD_BACKWARD);
153 g_free(buffer);
156 static void
157 on_next_occur(GtkAction * action, gpointer user_data)
159 IAnjutaEditor* te;
160 IAnjutaDocumentManager *docman;
161 IAnjutaDocument* doc;
162 SearchPlugin *plugin;
163 gint return_;
164 gchar *buffer = NULL;
166 plugin = ANJUTA_PLUGIN_SEARCH (user_data);
167 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
168 IAnjutaDocumentManager, NULL);
169 doc = ianjuta_document_manager_get_current_document(docman, NULL);
170 te = IANJUTA_IS_EDITOR(doc) ? IANJUTA_EDITOR(doc) : NULL;
171 if(!te) return;
172 if ((buffer = ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te), NULL)))
174 g_strstrip(buffer);
175 if ('\0' == *buffer)
177 g_free(buffer);
178 buffer = NULL;
181 if (NULL == buffer)
183 buffer = ianjuta_editor_get_current_word(te, NULL);
184 if (!buffer)
185 return;
187 return_= find_incremental(te, buffer, SD_FORWARD);
189 g_free(buffer);
192 static GtkActionEntry actions_search[] = {
193 { "ActionMenuEditSearch", NULL, N_("_Search"), NULL, NULL, NULL},
194 { "ActionEditSearchFind", GTK_STOCK_FIND, N_("_Find..."), "<shift><control>f",
195 N_("Search for a string or regular expression in the editor"),
196 G_CALLBACK (on_find1_activate)},
197 { "ActionEditSearchFindNext", GTK_STOCK_FIND, N_("Find _Next"), "<control>g",
198 N_("Repeat the last Find command"),
199 G_CALLBACK (on_findnext1_activate)},
200 { "ActionEditSearchFindPrevious", GTK_STOCK_FIND, N_("Find _Previous"),
201 "<control><shift>g",
202 N_("Repeat the last Find command"),
203 G_CALLBACK (on_findprevious1_activate)},
204 { "ActionEditSearchReplace", GTK_STOCK_FIND_AND_REPLACE, N_("Find and R_eplace..."),
205 "<control>h",
206 N_("Search for and replace a string or regular expression with another string"),
207 G_CALLBACK (on_find_and_replace1_activate)},
208 { "ActionEditAdvancedSearch", GTK_STOCK_FIND, N_("Advanced Search And Replace"),
209 NULL, N_("New advance search And replace stuff"),
210 G_CALLBACK (on_find1_activate)},
211 { "ActionEditSearchInFiles", NULL, N_("Fin_d in Files..."), NULL,
212 N_("Search for a string in multiple files or directories"),
213 G_CALLBACK (on_find_in_files1_activate)},
214 { "ActionEditGotoOccuranceNext", ANJUTA_STOCK_MATCH_NEXT,
215 N_("Ne_xt Occurrence"), NULL,
216 N_("Find the next occurrence of current word"),
217 G_CALLBACK (on_next_occur)},
218 { "ActionEditGotoOccurancePrev",ANJUTA_STOCK_MATCH_PREV,
219 N_("Pre_vious Occurrence"), NULL,
220 N_("Find the previous occurrence of current word"),
221 G_CALLBACK (on_prev_occur)},
224 gpointer parent_class;
226 static gboolean
227 activate_plugin (AnjutaPlugin *plugin)
229 static gboolean init = FALSE;
230 AnjutaUI *ui;
231 SearchPlugin* splugin = ANJUTA_PLUGIN_SEARCH (plugin);
232 IAnjutaDocumentManager* docman = anjuta_shell_get_interface(ANJUTA_PLUGIN(plugin)->shell,
233 IAnjutaDocumentManager, NULL);
236 if (!init)
238 BEGIN_REGISTER_ICON (plugin);
239 REGISTER_ICON_FULL (ANJUTA_PIXMAP_MATCH_NEXT, ANJUTA_STOCK_MATCH_NEXT);
240 REGISTER_ICON_FULL (ANJUTA_PIXMAP_MATCH_PREV, ANJUTA_STOCK_MATCH_PREV);
241 END_REGISTER_ICON;
242 init = TRUE;
245 ui = anjuta_shell_get_ui (plugin->shell, NULL);
246 anjuta_ui_add_action_group_entries (ui, "ActionGroupSearch",
247 _("Searching..."),
248 actions_search,
249 G_N_ELEMENTS (actions_search),
250 GETTEXT_PACKAGE, TRUE, plugin);
253 splugin->uiid = anjuta_ui_merge (ui, UI_FILE);
254 splugin->docman = docman;
255 search_and_replace_init(docman);
257 return TRUE;
260 static gboolean
261 deactivate_plugin (AnjutaPlugin *plugin)
264 return TRUE;
267 static void
268 dispose (GObject *obj)
270 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
272 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (obj));
275 static void
276 search_plugin_instance_init (GObject *obj)
278 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
281 static void
282 search_plugin_class_init (GObjectClass *klass)
284 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
286 parent_class = g_type_class_peek_parent (klass);
288 plugin_class->activate = activate_plugin;
289 plugin_class->deactivate = deactivate_plugin;
290 klass->dispose = dispose;
292 ANJUTA_PLUGIN_BEGIN (SearchPlugin, search_plugin);
293 ANJUTA_PLUGIN_END;
294 ANJUTA_SIMPLE_PLUGIN (SearchPlugin, search_plugin);