Integrate adding files with the file manager
[anjuta-git-plugin.git] / plugins / search / plugin.c
blob8f4dfb5f28d0a2c388f9b68fa7384f3cde0c2599
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 "plugin.h"
28 #include "search-replace.h"
29 #include "search-replace_backend.h"
30 #include "config.h"
32 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-search.ui"
33 #define ICON_FILE "anjuta-search-plugin-48.png"
35 #define ANJUTA_PIXMAP_MATCH_NEXT "anjuta-go-match-next"
36 #define ANJUTA_PIXMAP_MATCH_PREV "anjuta-go-match-prev"
37 #define ANJUTA_STOCK_MATCH_NEXT "anjuta-match-next"
38 #define ANJUTA_STOCK_MATCH_PREV "anjuta-match-prev"
40 /* Find next occurence of expression in Editor
41 Caching of FileBuffer might be useful here to improve performance
42 Returns: TRUE = found, FALSE = not found
45 static gboolean find_incremental(IAnjutaEditor* te, gchar* expression,
46 SearchDirection dir)
48 FileBuffer* fb = file_buffer_new_from_te (te);
49 SearchExpression* se = g_new0(SearchExpression, 1);
50 MatchInfo* info;
51 gboolean ret;
53 se->search_str = expression;
54 se->regex = FALSE;
55 se->greedy = FALSE;
56 se->ignore_case = TRUE;
57 se->whole_word = FALSE;
58 se->whole_line = FALSE;
59 se->word_start = FALSE;
60 se->no_limit = FALSE;
61 se->actions_max = 1;
62 se->regex_info = NULL;
64 info = get_next_match(fb, dir, se);
66 if (info != NULL)
68 IAnjutaIterable *start, *end;
69 start = ianjuta_editor_get_position_from_offset (te, info->pos, NULL);
70 end = ianjuta_editor_get_position_from_offset (te, info->pos + info->len, NULL);
71 ianjuta_editor_selection_set (IANJUTA_EDITOR_SELECTION (te),
72 start, end, NULL);
73 g_object_unref (start);
74 g_object_unref (end);
75 ret = TRUE;
77 else
78 ret = FALSE;
80 match_info_free(info);
81 file_buffer_free(fb);
82 g_free(se);
84 return ret;
87 static void
88 on_find1_activate (GtkAction * action, gpointer user_data)
90 anjuta_search_replace_activate(FALSE, FALSE);
93 static void
94 on_find_and_replace1_activate (GtkAction * action, gpointer user_data)
96 anjuta_search_replace_activate(TRUE, FALSE);
99 static void
100 on_find_in_files1_activate (GtkAction * action, gpointer user_data)
102 anjuta_search_replace_activate(FALSE, TRUE);
105 /* *user_data : TRUE=Forward False=Backward */
106 static void
107 on_findnext1_activate (GtkAction * action, gpointer user_data)
109 search_replace_next();
112 static void
113 on_findprevious1_activate (GtkAction * action, gpointer user_data)
115 search_replace_previous();
118 static void
119 on_prev_occur(GtkAction * action, gpointer user_data)
121 IAnjutaEditor* te;
122 IAnjutaDocumentManager *docman;
123 IAnjutaDocument* doc;
124 SearchPlugin *plugin;
125 gint return_;
126 gchar *buffer = NULL;
128 plugin = ANJUTA_PLUGIN_SEARCH (user_data);
129 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
130 IAnjutaDocumentManager, NULL);
131 doc = ianjuta_document_manager_get_current_document(docman, NULL);
132 te = IANJUTA_IS_EDITOR(doc) ? IANJUTA_EDITOR(doc) : NULL;
133 if(!te) return;
134 if ((buffer = ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te), NULL)))
136 g_strstrip(buffer);
137 if ('\0' == *buffer)
139 g_free(buffer);
140 buffer = NULL;
143 if (NULL == buffer)
145 buffer = ianjuta_editor_get_current_word(te, NULL);
146 if (!buffer)
147 return;
149 return_= find_incremental(te, buffer, SD_BACKWARD);
151 g_free(buffer);
154 static void
155 on_next_occur(GtkAction * action, gpointer user_data)
157 IAnjutaEditor* te;
158 IAnjutaDocumentManager *docman;
159 IAnjutaDocument* doc;
160 SearchPlugin *plugin;
161 gint return_;
162 gchar *buffer = NULL;
164 plugin = ANJUTA_PLUGIN_SEARCH (user_data);
165 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
166 IAnjutaDocumentManager, NULL);
167 doc = ianjuta_document_manager_get_current_document(docman, NULL);
168 te = IANJUTA_IS_EDITOR(doc) ? IANJUTA_EDITOR(doc) : NULL;
169 if(!te) return;
170 if ((buffer = ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te), NULL)))
172 g_strstrip(buffer);
173 if ('\0' == *buffer)
175 g_free(buffer);
176 buffer = NULL;
179 if (NULL == buffer)
181 buffer = ianjuta_editor_get_current_word(te, NULL);
182 if (!buffer)
183 return;
185 return_= find_incremental(te, buffer, SD_FORWARD);
187 g_free(buffer);
190 static GtkActionEntry actions_search[] = {
191 { "ActionMenuEditSearch", NULL, N_("_Search"), NULL, NULL, NULL},
192 { "ActionEditSearchFind", GTK_STOCK_FIND, N_("_Find..."), "<control><alt>f",
193 N_("Search for a string or regular expression in the editor"),
194 G_CALLBACK (on_find1_activate)},
195 { "ActionEditSearchFindNext", GTK_STOCK_FIND, N_("Find _Next"), "<control>g",
196 N_("Repeat the last Find command"),
197 G_CALLBACK (on_findnext1_activate)},
198 { "ActionEditSearchFindPrevious", GTK_STOCK_FIND, N_("Find _Previous"),
199 "<control><shift>g",
200 N_("Repeat the last Find command"),
201 G_CALLBACK (on_findprevious1_activate)},
202 { "ActionEditSearchReplace", GTK_STOCK_FIND_AND_REPLACE, N_("Find and R_eplace..."),
203 "<control>h",
204 N_("Search for and replace a string or regular expression with another string"),
205 G_CALLBACK (on_find_and_replace1_activate)},
206 { "ActionEditAdvancedSearch", GTK_STOCK_FIND, N_("Search and Replace"),
207 NULL, N_("Search and Replace"),
208 G_CALLBACK (on_find1_activate)},
209 { "ActionEditSearchInFiles", NULL, N_("Fin_d in Files..."), NULL,
210 N_("Search for a string in multiple files or directories"),
211 G_CALLBACK (on_find_in_files1_activate)},
212 { "ActionEditGotoOccuranceNext", ANJUTA_STOCK_MATCH_NEXT,
213 N_("Ne_xt Occurrence"), NULL,
214 N_("Find the next occurrence of current word"),
215 G_CALLBACK (on_next_occur)},
216 { "ActionEditGotoOccurancePrev",ANJUTA_STOCK_MATCH_PREV,
217 N_("Pre_vious Occurrence"), NULL,
218 N_("Find the previous occurrence of current word"),
219 G_CALLBACK (on_prev_occur)},
222 gpointer parent_class;
224 static gboolean
225 activate_plugin (AnjutaPlugin *plugin)
227 static gboolean init = FALSE;
228 AnjutaUI *ui;
229 SearchPlugin* splugin = ANJUTA_PLUGIN_SEARCH (plugin);
230 IAnjutaDocumentManager* docman = anjuta_shell_get_interface(ANJUTA_PLUGIN(plugin)->shell,
231 IAnjutaDocumentManager, NULL);
234 if (!init)
236 BEGIN_REGISTER_ICON (plugin);
237 REGISTER_ICON_FULL (ANJUTA_PIXMAP_MATCH_NEXT, ANJUTA_STOCK_MATCH_NEXT);
238 REGISTER_ICON_FULL (ANJUTA_PIXMAP_MATCH_PREV, ANJUTA_STOCK_MATCH_PREV);
239 END_REGISTER_ICON;
240 init = TRUE;
243 ui = anjuta_shell_get_ui (plugin->shell, NULL);
244 anjuta_ui_add_action_group_entries (ui, "ActionGroupSearch",
245 _("Searching..."),
246 actions_search,
247 G_N_ELEMENTS (actions_search),
248 GETTEXT_PACKAGE, TRUE, plugin);
251 splugin->uiid = anjuta_ui_merge (ui, UI_FILE);
252 splugin->docman = docman;
253 search_and_replace_init(docman);
255 return TRUE;
258 static gboolean
259 deactivate_plugin (AnjutaPlugin *plugin)
262 return TRUE;
265 static void
266 dispose (GObject *obj)
268 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
270 G_OBJECT_CLASS (parent_class)->dispose (obj);
273 static void
274 finalize (GObject *obj)
276 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
278 G_OBJECT_CLASS (parent_class)->finalize (obj);
281 static void
282 search_plugin_instance_init (GObject *obj)
284 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
287 static void
288 search_plugin_class_init (GObjectClass *klass)
290 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
292 parent_class = g_type_class_peek_parent (klass);
294 plugin_class->activate = activate_plugin;
295 plugin_class->deactivate = deactivate_plugin;
296 klass->dispose = dispose;
297 klass->finalize = finalize;
299 ANJUTA_PLUGIN_BEGIN (SearchPlugin, search_plugin);
300 ANJUTA_PLUGIN_END;
301 ANJUTA_SIMPLE_PLUGIN (SearchPlugin, search_plugin);