1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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>
30 #include "search-replace.h"
31 #include "search-replace_backend.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
,
50 FileBuffer
* fb
= file_buffer_new_from_te (te
);
51 SearchExpression
* se
= g_new0(SearchExpression
, 1);
55 se
->search_str
= expression
;
58 se
->ignore_case
= TRUE
;
59 se
->whole_word
= FALSE
;
60 se
->whole_line
= FALSE
;
61 se
->word_start
= FALSE
;
66 info
= get_next_match(fb
, dir
, se
);
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
),
75 g_object_unref (start
);
82 match_info_free(info
);
90 on_find1_activate (GtkAction
* action
, gpointer user_data
)
92 anjuta_search_replace_activate(FALSE
, FALSE
);
96 on_find_and_replace1_activate (GtkAction
* action
, gpointer user_data
)
98 anjuta_search_replace_activate(TRUE
, FALSE
);
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 */
109 on_findnext1_activate (GtkAction
* action
, gpointer user_data
)
111 search_replace_next();
115 on_findprevious1_activate (GtkAction
* action
, gpointer user_data
)
117 search_replace_previous();
121 on_prev_occur(GtkAction
* action
, gpointer user_data
)
124 IAnjutaDocumentManager
*docman
;
125 IAnjutaDocument
* doc
;
126 SearchPlugin
*plugin
;
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
;
136 if ((buffer
= ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te
), NULL
)))
147 buffer
= ianjuta_editor_get_current_word(te
, NULL
);
151 return_
= find_incremental(te
, buffer
, SD_BACKWARD
);
157 on_next_occur(GtkAction
* action
, gpointer user_data
)
160 IAnjutaDocumentManager
*docman
;
161 IAnjutaDocument
* doc
;
162 SearchPlugin
*plugin
;
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
;
172 if ((buffer
= ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te
), NULL
)))
183 buffer
= ianjuta_editor_get_current_word(te
, NULL
);
187 return_
= find_incremental(te
, buffer
, SD_FORWARD
);
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"),
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..."),
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
;
227 activate_plugin (AnjutaPlugin
*plugin
)
229 static gboolean init
= FALSE
;
231 SearchPlugin
* splugin
= ANJUTA_PLUGIN_SEARCH (plugin
);
232 IAnjutaDocumentManager
* docman
= anjuta_shell_get_interface(ANJUTA_PLUGIN(plugin
)->shell
,
233 IAnjutaDocumentManager
, NULL
);
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
);
245 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
246 anjuta_ui_add_action_group_entries (ui
, "ActionGroupSearch",
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
);
261 deactivate_plugin (AnjutaPlugin
*plugin
)
268 dispose (GObject
*obj
)
270 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
272 GNOME_CALL_PARENT (G_OBJECT_CLASS
, dispose
, (obj
));
276 search_plugin_instance_init (GObject
*obj
)
278 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
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
);
294 ANJUTA_SIMPLE_PLUGIN (SearchPlugin
, search_plugin
);