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>
28 #include "search-replace.h"
29 #include "search-replace_backend.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
,
48 FileBuffer
* fb
= file_buffer_new_from_te (te
);
49 SearchExpression
* se
= g_new0(SearchExpression
, 1);
53 se
->search_str
= expression
;
56 se
->ignore_case
= TRUE
;
57 se
->whole_word
= FALSE
;
58 se
->whole_line
= FALSE
;
59 se
->word_start
= FALSE
;
62 se
->regex_info
= NULL
;
64 info
= get_next_match(fb
, dir
, se
);
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
),
73 g_object_unref (start
);
80 match_info_free(info
);
88 on_find1_activate (GtkAction
* action
, gpointer user_data
)
90 anjuta_search_replace_activate(FALSE
, FALSE
);
94 on_find_and_replace1_activate (GtkAction
* action
, gpointer user_data
)
96 anjuta_search_replace_activate(TRUE
, FALSE
);
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 */
107 on_findnext1_activate (GtkAction
* action
, gpointer user_data
)
109 search_replace_next();
113 on_findprevious1_activate (GtkAction
* action
, gpointer user_data
)
115 search_replace_previous();
119 on_prev_occur(GtkAction
* action
, gpointer user_data
)
122 IAnjutaDocumentManager
*docman
;
123 IAnjutaDocument
* doc
;
124 SearchPlugin
*plugin
;
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
;
134 if ((buffer
= ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te
), NULL
)))
145 buffer
= ianjuta_editor_get_current_word(te
, NULL
);
149 return_
= find_incremental(te
, buffer
, SD_BACKWARD
);
155 on_next_occur(GtkAction
* action
, gpointer user_data
)
158 IAnjutaDocumentManager
*docman
;
159 IAnjutaDocument
* doc
;
160 SearchPlugin
*plugin
;
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
;
170 if ((buffer
= ianjuta_editor_selection_get (IANJUTA_EDITOR_SELECTION (te
), NULL
)))
181 buffer
= ianjuta_editor_get_current_word(te
, NULL
);
185 return_
= find_incremental(te
, buffer
, SD_FORWARD
);
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"),
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..."),
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
;
225 activate_plugin (AnjutaPlugin
*plugin
)
227 static gboolean init
= FALSE
;
229 SearchPlugin
* splugin
= ANJUTA_PLUGIN_SEARCH (plugin
);
230 IAnjutaDocumentManager
* docman
= anjuta_shell_get_interface(ANJUTA_PLUGIN(plugin
)->shell
,
231 IAnjutaDocumentManager
, NULL
);
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
);
243 ui
= anjuta_shell_get_ui (plugin
->shell
, NULL
);
244 anjuta_ui_add_action_group_entries (ui
, "ActionGroupSearch",
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
);
259 deactivate_plugin (AnjutaPlugin
*plugin
)
266 dispose (GObject
*obj
)
268 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
270 G_OBJECT_CLASS (parent_class
)->dispose (obj
);
274 finalize (GObject
*obj
)
276 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
278 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
282 search_plugin_instance_init (GObject
*obj
)
284 //SearchPlugin *plugin = ANJUTA_PLUGIN_SEARCH (obj);
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
);
301 ANJUTA_SIMPLE_PLUGIN (SearchPlugin
, search_plugin
);