medit/moo/mooedit/plugins/moofind.c

916 lines
28 KiB
C
Raw Normal View History

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4; coding: utf-8 -*-
*
* moofind.c
*
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef MOO_VERSION
#define MOO_VERSION NULL
#endif
2005-09-11 23:57:29 -07:00
#include "mooedit/mooplugin-macro.h"
#include "mooedit/plugins/moofind-glade.h"
#include "mooedit/plugins/mooeditplugins.h"
#include "mooutils/moofileview/moofileentry.h"
#include "mooedit/moocmdview.h"
2005-09-08 10:08:31 -07:00
#include "mooutils/moostock.h"
#include "mooutils/mooglade.h"
#include "mooutils/moohistoryentry.h"
#include "mooutils/moodialogs.h"
#include <gtk/gtkdialog.h>
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtktogglebutton.h>
2005-09-08 10:08:31 -07:00
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#define FIND_PLUGIN_ID "find"
2005-09-11 12:43:23 -07:00
enum {
CMD_GREP = 1,
CMD_FIND
};
typedef struct {
2005-09-11 23:57:29 -07:00
MooPlugin parent;
2005-09-16 10:51:20 -07:00
guint ui_merge_id;
2005-09-11 23:57:29 -07:00
} FindPlugin;
typedef struct {
MooWinPlugin parent;
2005-09-11 23:57:29 -07:00
2005-09-11 12:43:23 -07:00
GtkWidget *grep_dialog;
MooGladeXML *grep_xml;
MooFileEntryCompletion *grep_completion;
char *current_file;
GtkWidget *find_dialog;
MooGladeXML *find_xml;
MooFileEntryCompletion *find_completion;
2005-09-08 10:08:31 -07:00
MooEditWindow *window;
MooCmdView *output;
2005-09-08 10:08:31 -07:00
GtkTextTag *line_number_tag;
GtkTextTag *match_tag;
GtkTextTag *file_tag;
GtkTextTag *error_tag;
GtkTextTag *message_tag;
guint match_count;
2005-09-11 12:43:23 -07:00
int cmd;
2005-09-11 23:57:29 -07:00
} FindWindowPlugin;
#define WindowStuff FindWindowPlugin
2005-09-08 10:08:31 -07:00
typedef struct {
char *filename;
int line;
} FileLinePair;
2005-09-11 12:43:23 -07:00
static void do_grep (MooEditWindow *window,
2005-09-08 10:08:31 -07:00
WindowStuff *stuff);
2005-09-11 12:43:23 -07:00
static void create_grep_dialog (MooEditWindow *window,
2005-09-08 10:08:31 -07:00
WindowStuff *stuff);
2005-09-11 12:43:23 -07:00
static void init_grep_dialog (MooEditWindow *window,
2005-09-08 10:08:31 -07:00
WindowStuff *stuff);
2005-09-11 12:43:23 -07:00
static void execute_grep (const char *pattern,
2005-09-08 10:08:31 -07:00
const char *glob,
const char *dir,
const char *skip_files,
2005-09-08 10:08:31 -07:00
gboolean case_sensitive,
WindowStuff *stuff);
2005-09-11 12:43:23 -07:00
static void do_find (MooEditWindow *window,
WindowStuff *stuff);
static void create_find_dialog (MooEditWindow *window,
WindowStuff *stuff);
static void init_find_dialog (MooEditWindow *window,
WindowStuff *stuff);
static void execute_find (const char *pattern,
const char *dir,
const char *skip_files,
WindowStuff *stuff);
static gboolean output_activate (WindowStuff *stuff,
2005-11-11 12:04:57 -08:00
int line);
static gboolean command_exit (MooLineView *view,
int status,
WindowStuff *stuff);
static gboolean process_line (MooLineView *view,
const char *line,
WindowStuff *stuff);
2005-09-08 10:08:31 -07:00
static void
2005-09-08 10:08:31 -07:00
find_in_files_cb (MooEditWindow *window)
{
2005-09-08 10:08:31 -07:00
WindowStuff *stuff;
int response;
stuff = moo_win_plugin_lookup (FIND_PLUGIN_ID, window);
2005-09-08 10:08:31 -07:00
g_return_if_fail (stuff != NULL);
2005-09-11 12:43:23 -07:00
if (!stuff->grep_dialog)
{
create_grep_dialog (window, stuff);
g_return_if_fail (stuff->grep_dialog != NULL);
}
init_grep_dialog (window, stuff);
response = gtk_dialog_run (GTK_DIALOG (stuff->grep_dialog));
gtk_widget_hide (stuff->grep_dialog);
if (response == GTK_RESPONSE_OK)
do_grep (window, stuff);
}
static void
find_file_cb (MooEditWindow *window)
{
WindowStuff *stuff;
int response;
stuff = moo_win_plugin_lookup (FIND_PLUGIN_ID, window);
2005-09-11 12:43:23 -07:00
g_return_if_fail (stuff != NULL);
if (!stuff->find_dialog)
2005-09-08 10:08:31 -07:00
{
2005-09-11 12:43:23 -07:00
create_find_dialog (window, stuff);
g_return_if_fail (stuff->find_dialog != NULL);
2005-09-08 10:08:31 -07:00
}
2005-09-11 12:43:23 -07:00
init_find_dialog (window, stuff);
2005-09-08 10:08:31 -07:00
2005-09-11 12:43:23 -07:00
response = gtk_dialog_run (GTK_DIALOG (stuff->find_dialog));
gtk_widget_hide (stuff->find_dialog);
2005-09-08 10:08:31 -07:00
if (response == GTK_RESPONSE_OK)
do_find (window, stuff);
}
static void
2005-11-02 12:45:26 -08:00
ensure_output (WindowStuff *stuff)
{
2005-09-08 10:08:31 -07:00
GtkWidget *swin;
MooPaneLabel *label;
MooEditWindow *window = MOO_WIN_PLUGIN (stuff)->window;
2005-09-11 23:57:29 -07:00
2005-11-02 12:45:26 -08:00
if (stuff->output)
return;
2005-11-02 12:45:26 -08:00
label = moo_pane_label_new (MOO_STOCK_FIND_IN_FILES, NULL, NULL,
"Search Results", "Search Results");
stuff->output = g_object_new (MOO_TYPE_CMD_VIEW,
2005-09-08 10:08:31 -07:00
"highlight-current-line", TRUE,
NULL);
2005-11-18 05:02:42 -08:00
moo_edit_window_add_stop_client (window, stuff->output);
g_signal_connect_swapped (stuff->output, "activate",
G_CALLBACK (output_activate), stuff);
stuff->line_number_tag =
moo_line_view_create_tag (MOO_LINE_VIEW (stuff->output),
NULL, "weight", PANGO_WEIGHT_BOLD, NULL);
stuff->match_tag =
moo_line_view_create_tag (MOO_LINE_VIEW (stuff->output), NULL,
2005-11-20 23:56:17 -08:00
"foreground", "#0000C0", NULL);
stuff->file_tag =
moo_line_view_create_tag (MOO_LINE_VIEW (stuff->output), NULL,
2005-11-20 23:56:17 -08:00
"foreground", "#008040", NULL);
stuff->error_tag =
moo_text_view_lookup_tag (MOO_TEXT_VIEW (stuff->output), "error");
2005-11-20 23:56:17 -08:00
g_object_set (stuff->error_tag, "foreground", "#C00000", NULL);
stuff->message_tag =
moo_text_view_lookup_tag (MOO_TEXT_VIEW (stuff->output), "message");
g_signal_connect (stuff->output, "cmd-exit",
G_CALLBACK (command_exit), stuff);
g_signal_connect (stuff->output, "stdout-line",
G_CALLBACK (process_line), stuff);
2005-09-08 10:08:31 -07:00
swin = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (swin), GTK_WIDGET (stuff->output));
gtk_widget_show_all (swin);
moo_edit_window_add_pane (window, FIND_PLUGIN_ID,
2005-09-08 10:08:31 -07:00
swin, label, MOO_PANE_POS_BOTTOM);
2005-11-06 22:56:33 -08:00
moo_pane_label_free (label);
2005-09-08 10:08:31 -07:00
}
2005-11-02 12:45:26 -08:00
static void
find_window_plugin_create (WindowStuff *stuff)
{
stuff->window = MOO_WIN_PLUGIN (stuff)->window;
}
2005-09-08 10:08:31 -07:00
static gboolean
2005-09-16 10:51:20 -07:00
find_plugin_init (FindPlugin *plugin)
2005-09-08 10:08:31 -07:00
{
MooWindowClass *klass = g_type_class_ref (MOO_TYPE_EDIT_WINDOW);
2005-09-16 10:51:20 -07:00
MooEditor *editor = moo_editor_instance ();
MooUIXML *xml = moo_editor_get_ui_xml (editor);
2005-09-08 10:08:31 -07:00
g_return_val_if_fail (klass != NULL, FALSE);
2005-09-16 10:51:20 -07:00
g_return_val_if_fail (editor != NULL, FALSE);
2005-09-08 10:08:31 -07:00
moo_window_class_new_action (klass, "FindInFiles",
"name", "Find In Files",
"label", "Find In Files",
"tooltip", "Find In Files",
"icon-stock-id", MOO_STOCK_FIND_IN_FILES,
2005-11-19 19:59:01 -08:00
"closure-callback", find_in_files_cb,
NULL);
moo_window_class_new_action (klass, "FindFile",
"name", "Find File",
"label", "Find File",
"tooltip", "Find File",
"icon-stock-id", MOO_STOCK_FIND_FILE,
2005-11-19 19:59:01 -08:00
"closure-callback", find_file_cb,
NULL);
2005-09-11 12:43:23 -07:00
2005-09-16 10:51:20 -07:00
plugin->ui_merge_id = moo_ui_xml_new_merge_id (xml);
moo_ui_xml_add_item (xml, plugin->ui_merge_id,
"Editor/Menubar/Search",
"FindInFiles", "FindInFiles", -1);
moo_ui_xml_add_item (xml, plugin->ui_merge_id,
"Editor/Menubar/Search",
"FindFile", "FindFile", -1);
2005-09-08 10:08:31 -07:00
g_type_class_unref (klass);
return TRUE;
}
static void
2005-09-16 10:51:20 -07:00
find_plugin_deinit (FindPlugin *plugin)
2005-09-08 10:08:31 -07:00
{
MooWindowClass *klass = g_type_class_ref (MOO_TYPE_EDIT_WINDOW);
2005-09-16 10:51:20 -07:00
MooEditor *editor = moo_editor_instance ();
MooUIXML *xml = moo_editor_get_ui_xml (editor);
2005-09-15 14:49:51 -07:00
moo_window_class_remove_action (klass, "FindInFiles");
moo_window_class_remove_action (klass, "FindFile");
2005-09-15 14:49:51 -07:00
2005-09-16 10:51:20 -07:00
if (plugin->ui_merge_id)
moo_ui_xml_remove_ui (xml, plugin->ui_merge_id);
plugin->ui_merge_id = 0;
2005-09-15 14:49:51 -07:00
2005-09-16 10:51:20 -07:00
g_type_class_unref (klass);
}
2005-09-08 10:08:31 -07:00
static void
pattern_entry_changed (GtkEntry *entry,
GtkDialog *dialog)
{
const char *text = gtk_entry_get_text (entry);
gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK,
text[0] != 0);
}
static void
2005-09-11 12:43:23 -07:00
create_grep_dialog (MooEditWindow *window,
WindowStuff *stuff)
2005-09-08 10:08:31 -07:00
{
GtkWidget *dir_entry, *pattern_entry;
GtkWidget *skip_combo;
MooHistoryList *skip_list;
2005-09-08 10:08:31 -07:00
stuff->grep_xml = moo_glade_xml_new_empty ();
moo_glade_xml_map_id (stuff->grep_xml, "pattern_combo", MOO_TYPE_HISTORY_ENTRY);
moo_glade_xml_map_id (stuff->grep_xml, "glob_combo", MOO_TYPE_HISTORY_ENTRY);
moo_glade_xml_map_id (stuff->grep_xml, "dir_combo", MOO_TYPE_HISTORY_ENTRY);
moo_glade_xml_map_id (stuff->grep_xml, "skip_combo", MOO_TYPE_HISTORY_ENTRY);
moo_glade_xml_parse_memory (stuff->grep_xml, MOO_FIND_GLADE_XML, -1, "grep_dialog");
2005-09-08 10:08:31 -07:00
2005-09-11 12:43:23 -07:00
stuff->grep_dialog = moo_glade_xml_get_widget (stuff->grep_xml, "grep_dialog");
g_return_if_fail (stuff->grep_dialog != NULL);
2005-09-08 10:08:31 -07:00
2005-09-11 12:43:23 -07:00
gtk_dialog_set_default_response (GTK_DIALOG (stuff->grep_dialog),
2005-09-08 10:08:31 -07:00
GTK_RESPONSE_OK);
2005-09-11 12:43:23 -07:00
gtk_dialog_set_response_sensitive (GTK_DIALOG (stuff->grep_dialog),
2005-09-08 10:08:31 -07:00
GTK_RESPONSE_OK, FALSE);
moo_position_window (stuff->grep_dialog, GTK_WIDGET (window), FALSE, FALSE, 0, 0);
2005-09-08 10:08:31 -07:00
2005-09-11 12:43:23 -07:00
g_signal_connect (stuff->grep_dialog, "delete-event",
2005-09-08 10:08:31 -07:00
G_CALLBACK (gtk_widget_hide_on_delete), NULL);
2005-09-11 12:43:23 -07:00
pattern_entry = moo_glade_xml_get_widget (stuff->grep_xml, "pattern_combo");
pattern_entry = MOO_COMBO(pattern_entry)->entry;
2005-09-08 10:08:31 -07:00
g_signal_connect (pattern_entry, "changed",
2005-09-11 12:43:23 -07:00
G_CALLBACK (pattern_entry_changed), stuff->grep_dialog);
2005-09-08 10:08:31 -07:00
2005-09-11 12:43:23 -07:00
dir_entry = moo_glade_xml_get_widget (stuff->grep_xml, "dir_combo");
dir_entry = MOO_COMBO(dir_entry)->entry;
2005-09-11 12:43:23 -07:00
stuff->grep_completion = g_object_new (MOO_TYPE_FILE_ENTRY_COMPLETION,
"directories-only", TRUE,
"case-sensitive", TRUE,
"show-hidden", FALSE,
NULL);
moo_file_entry_completion_set_entry (stuff->grep_completion, GTK_ENTRY (dir_entry));
skip_combo = moo_glade_xml_get_widget (stuff->grep_xml, "skip_combo");
skip_list = moo_history_entry_get_list (MOO_HISTORY_ENTRY (skip_combo));
moo_history_list_add_full (skip_list, ".svn/;CVS/", "CVS and SVN dirs");
2005-09-11 12:43:23 -07:00
}
static void
create_find_dialog (MooEditWindow *window,
WindowStuff *stuff)
{
GtkWidget *dir_entry, *pattern_entry;
stuff->find_xml = moo_glade_xml_new_empty ();
moo_glade_xml_map_id (stuff->find_xml, "pattern_combo", MOO_TYPE_HISTORY_ENTRY);
moo_glade_xml_map_id (stuff->find_xml, "dir_combo", MOO_TYPE_HISTORY_ENTRY);
moo_glade_xml_map_id (stuff->find_xml, "skip_combo", MOO_TYPE_HISTORY_ENTRY);
moo_glade_xml_parse_memory (stuff->find_xml, MOO_FIND_GLADE_XML, -1, "find_dialog");
2005-09-11 12:43:23 -07:00
stuff->find_dialog = moo_glade_xml_get_widget (stuff->find_xml, "find_dialog");
g_return_if_fail (stuff->find_dialog != NULL);
gtk_dialog_set_default_response (GTK_DIALOG (stuff->find_dialog),
GTK_RESPONSE_OK);
gtk_dialog_set_response_sensitive (GTK_DIALOG (stuff->find_dialog),
GTK_RESPONSE_OK, FALSE);
moo_position_window (stuff->find_dialog, GTK_WIDGET (window), FALSE, FALSE, 0, 0);
2005-09-11 12:43:23 -07:00
g_signal_connect (stuff->find_dialog, "delete-event",
G_CALLBACK (gtk_widget_hide_on_delete), NULL);
pattern_entry = moo_glade_xml_get_widget (stuff->find_xml, "pattern_combo");
pattern_entry = MOO_COMBO(pattern_entry)->entry;
2005-09-11 12:43:23 -07:00
g_signal_connect (pattern_entry, "changed",
G_CALLBACK (pattern_entry_changed), stuff->find_dialog);
dir_entry = moo_glade_xml_get_widget (stuff->find_xml, "dir_combo");
dir_entry = MOO_COMBO(dir_entry)->entry;
2005-09-11 12:43:23 -07:00
stuff->find_completion = g_object_new (MOO_TYPE_FILE_ENTRY_COMPLETION,
"directories-only", TRUE,
"case-sensitive", TRUE,
"show-hidden", FALSE,
NULL);
moo_file_entry_completion_set_entry (stuff->find_completion, GTK_ENTRY (dir_entry));
2005-09-08 10:08:31 -07:00
}
static void
2005-09-11 12:43:23 -07:00
init_grep_dialog (MooEditWindow *window,
WindowStuff *stuff)
2005-09-08 10:08:31 -07:00
{
MooEdit *doc;
GtkWidget *dir_entry, *pattern_entry, *glob_entry;
dir_entry = MOO_COMBO(moo_glade_xml_get_widget (stuff->grep_xml, "dir_combo"))->entry;
pattern_entry = MOO_COMBO(moo_glade_xml_get_widget (stuff->grep_xml, "pattern_combo"))->entry;
glob_entry = MOO_COMBO(moo_glade_xml_get_widget (stuff->grep_xml, "glob_combo"))->entry;
2005-09-08 10:08:31 -07:00
doc = moo_edit_window_get_active_doc (window);
if (doc)
{
char *sel = moo_text_view_get_selection (MOO_TEXT_VIEW (doc));
2005-09-08 10:08:31 -07:00
if (sel && !strchr (sel, '\n'))
gtk_entry_set_text (GTK_ENTRY (pattern_entry), sel);
g_free (sel);
}
if (!gtk_entry_get_text(GTK_ENTRY (dir_entry))[0])
{
if (doc && moo_edit_get_filename (doc))
{
char *dir = g_path_get_dirname (moo_edit_get_filename (doc));
2005-09-11 12:43:23 -07:00
moo_file_entry_completion_set_path (stuff->grep_completion, dir);
2005-09-08 10:08:31 -07:00
g_free (dir);
}
else
{
2005-09-11 12:43:23 -07:00
moo_file_entry_completion_set_path (stuff->grep_completion, g_get_home_dir ());
2005-09-08 10:08:31 -07:00
}
}
if (!gtk_entry_get_text(GTK_ENTRY (glob_entry))[0])
gtk_entry_set_text (GTK_ENTRY (glob_entry), "*");
gtk_widget_grab_focus (pattern_entry);
}
static void
2005-09-11 12:43:23 -07:00
init_find_dialog (G_GNUC_UNUSED MooEditWindow *window,
WindowStuff *stuff)
{
GtkWidget *dir_entry, *pattern_entry;
dir_entry = MOO_COMBO(moo_glade_xml_get_widget (stuff->find_xml, "dir_combo"))->entry;
pattern_entry = MOO_COMBO(moo_glade_xml_get_widget (stuff->find_xml, "pattern_combo"))->entry;
2005-09-11 12:43:23 -07:00
if (!gtk_entry_get_text(GTK_ENTRY (dir_entry))[0])
moo_file_entry_completion_set_path (stuff->find_completion, g_get_home_dir ());
gtk_widget_grab_focus (pattern_entry);
}
static void
do_grep (MooEditWindow *window,
2005-09-08 10:08:31 -07:00
WindowStuff *stuff)
{
GtkWidget *pane;
GtkWidget *dir_entry, *pattern_entry, *glob_entry;
GtkWidget *skip_entry, *case_sensitive_button;
const char *dir_utf8, *pattern, *glob, *skip;
gboolean case_sensitive;
char *dir;
2005-11-02 12:45:26 -08:00
ensure_output (stuff);
pane = moo_edit_window_get_pane (window, FIND_PLUGIN_ID);
2005-09-08 10:08:31 -07:00
g_return_if_fail (pane != NULL);
dir_entry = MOO_COMBO (moo_glade_xml_get_widget (stuff->grep_xml, "dir_combo"))->entry;
pattern_entry = MOO_COMBO (moo_glade_xml_get_widget (stuff->grep_xml, "pattern_combo"))->entry;
glob_entry = MOO_COMBO (moo_glade_xml_get_widget (stuff->grep_xml, "glob_combo"))->entry;
skip_entry = MOO_COMBO (moo_glade_xml_get_widget (stuff->grep_xml, "skip_combo"))->entry;
2005-09-11 12:43:23 -07:00
case_sensitive_button = moo_glade_xml_get_widget (stuff->grep_xml, "case_sensitive_button");
2005-09-08 10:08:31 -07:00
dir_utf8 = gtk_entry_get_text (GTK_ENTRY (dir_entry));
dir = g_filename_from_utf8 (dir_utf8, -1, NULL, NULL, NULL);
g_return_if_fail (dir != NULL);
pattern = gtk_entry_get_text (GTK_ENTRY (pattern_entry));
glob = gtk_entry_get_text (GTK_ENTRY (glob_entry));
skip = gtk_entry_get_text (GTK_ENTRY (skip_entry));
case_sensitive = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (case_sensitive_button));
moo_line_view_clear (MOO_LINE_VIEW (stuff->output));
2005-09-08 10:08:31 -07:00
moo_big_paned_present_pane (window->paned, pane);
2005-09-11 12:43:23 -07:00
execute_grep (pattern, glob, dir, skip,
2005-09-08 10:08:31 -07:00
case_sensitive, stuff);
}
2005-09-11 12:43:23 -07:00
static void
do_find (MooEditWindow *window,
WindowStuff *stuff)
{
GtkWidget *pane;
GtkWidget *dir_entry, *pattern_entry, *skip_entry;
const char *dir_utf8, *pattern, *skip;
char *dir;
2005-11-02 12:45:26 -08:00
ensure_output (stuff);
2005-09-11 12:43:23 -07:00
pane = moo_edit_window_get_pane (window, FIND_PLUGIN_ID);
g_return_if_fail (pane != NULL);
dir_entry = MOO_COMBO (moo_glade_xml_get_widget (stuff->find_xml, "dir_combo"))->entry;
pattern_entry = MOO_COMBO (moo_glade_xml_get_widget (stuff->find_xml, "pattern_combo"))->entry;
skip_entry = MOO_COMBO (moo_glade_xml_get_widget (stuff->find_xml, "skip_combo"))->entry;
2005-09-11 12:43:23 -07:00
dir_utf8 = gtk_entry_get_text (GTK_ENTRY (dir_entry));
dir = g_filename_from_utf8 (dir_utf8, -1, NULL, NULL, NULL);
g_return_if_fail (dir != NULL);
pattern = gtk_entry_get_text (GTK_ENTRY (pattern_entry));
skip = gtk_entry_get_text (GTK_ENTRY (skip_entry));
moo_line_view_clear (MOO_LINE_VIEW (stuff->output));
2005-09-11 12:43:23 -07:00
moo_big_paned_present_pane (window->paned, pane);
execute_find (pattern, dir, skip, stuff);
}
2005-09-08 10:08:31 -07:00
static FileLinePair*
file_line_pair_new (const char *filename,
int line)
{
FileLinePair *pair = g_new (FileLinePair, 1);
pair->filename = g_strdup (filename);
pair->line = line;
return pair;
}
static void
file_line_pair_free (FileLinePair *pair)
{
if (pair)
{
g_free (pair->filename);
g_free (pair);
}
}
static gboolean
process_grep_line (MooLineView *view,
2005-09-11 12:43:23 -07:00
const char *line,
WindowStuff *stuff)
2005-09-08 10:08:31 -07:00
{
char *filename = NULL;
char *number = NULL;
const char *colon, *p;
int view_line;
int line_no;
guint64 line_no_64;
/* 'Binary file blah matches' */
if (g_str_has_prefix (line, "Binary file "))
return FALSE;
2005-09-08 10:08:31 -07:00
p = line;
if (!(colon = strchr (p, ':')) || !colon[1])
goto parse_error;
filename = g_strndup (p, colon - p);
p = colon + 1;
if (!(colon = strchr (p, ':')) || !colon[1])
goto parse_error;
number = g_strndup (p, colon - p);
p = colon + 1;
if (!stuff->current_file || strcmp (stuff->current_file, filename))
{
g_free (stuff->current_file);
stuff->current_file = filename;
view_line = moo_line_view_write_line (view, filename, -1,
2005-09-08 10:08:31 -07:00
stuff->file_tag);
2005-11-11 12:04:57 -08:00
moo_line_view_set_data (view, view_line,
file_line_pair_new (filename, -1),
(GDestroyNotify) file_line_pair_free);
2005-09-08 10:08:31 -07:00
}
else
{
g_free (filename);
}
errno = 0;
2005-09-08 10:08:31 -07:00
line_no_64 = g_ascii_strtoull (number, NULL, 0);
if (errno)
{
g_warning ("%s: could not parse number '%s'",
G_STRLOC, number);
line_no = -1;
}
else if (line_no_64 > G_MAXINT)
{
g_warning ("%s: number '%s' is too large",
G_STRLOC, number);
line_no = -1;
}
else if (line_no_64 == 0)
{
g_warning ("%s: number '%s' is zero",
G_STRLOC, number);
line_no = -1;
}
else
{
line_no = line_no_64 - 1;
}
view_line = moo_line_view_start_line (view);
moo_line_view_write (view, number, -1, stuff->line_number_tag);
moo_line_view_write (view, ": ", -1, NULL);
moo_line_view_write (view, p, -1, stuff->match_tag);
moo_line_view_end_line (view);
2005-09-08 10:08:31 -07:00
2005-11-11 12:04:57 -08:00
moo_line_view_set_data (view, view_line,
file_line_pair_new (stuff->current_file, line_no),
(GDestroyNotify) file_line_pair_free);
stuff->match_count++;
2005-09-08 10:08:31 -07:00
g_free (number);
return TRUE;
2005-09-08 10:08:31 -07:00
parse_error:
g_warning ("%s: could not parse line '%s'",
G_STRLOC, line);
g_free (filename);
g_free (number);
return FALSE;
2005-09-08 10:08:31 -07:00
}
2005-09-11 12:43:23 -07:00
static gboolean
process_find_line (MooLineView *view,
2005-09-11 12:43:23 -07:00
const char *line,
WindowStuff *stuff)
{
int view_line;
view_line = moo_line_view_write_line (view, line, -1, stuff->match_tag);
2005-11-11 12:04:57 -08:00
moo_line_view_set_data (view, view_line,
file_line_pair_new (line, -1),
(GDestroyNotify) file_line_pair_free);
2005-09-11 12:43:23 -07:00
stuff->match_count++;
return TRUE;
}
static gboolean
process_line (MooLineView *view,
2005-09-11 12:43:23 -07:00
const char *line,
WindowStuff *stuff)
{
switch (stuff->cmd)
{
case CMD_GREP:
return process_grep_line (view, line, stuff);
case CMD_FIND:
return process_find_line (view, line, stuff);
default:
g_return_val_if_reached (FALSE);
}
}
2005-09-08 10:08:31 -07:00
static void
2005-09-11 12:43:23 -07:00
execute_grep (const char *pattern,
2005-09-08 10:08:31 -07:00
const char *glob,
const char *dir,
const char *skip_files,
2005-09-08 10:08:31 -07:00
gboolean case_sensitive,
WindowStuff *stuff)
{
GString *command = NULL;
char **globs = NULL;
g_return_if_fail (stuff->output != NULL);
g_return_if_fail (pattern && pattern[0]);
2005-09-11 12:43:23 -07:00
g_return_if_fail (dir && dir[0]);
2005-09-08 10:08:31 -07:00
g_free (stuff->current_file);
stuff->current_file = NULL;
stuff->match_count = 0;
2005-09-08 10:08:31 -07:00
command = g_string_new ("");
g_string_printf (command, "find '%s'", dir);
2005-11-18 05:02:42 -08:00
if (glob && glob[0] && strcmp (glob, "*"))
2005-09-08 10:08:31 -07:00
{
globs = g_strsplit (glob, ";", 0);
if (globs)
{
char **p;
gboolean first = TRUE;
for (p = globs; *p != NULL; p++)
{
if (first)
{
g_string_append_printf (command, " \\( -name \"%s\"", *p);
first = FALSE;
}
else
{
g_string_append_printf (command, " -o -name \"%s\"", *p);
}
}
g_string_append (command, " \\)");
}
g_strfreev (globs);
}
g_string_append_printf (command, " -print -follow");
2005-11-18 05:02:42 -08:00
if (skip_files && skip_files[0])
2005-09-08 10:08:31 -07:00
{
globs = g_strsplit (skip_files, ";", 0);
2005-09-08 10:08:31 -07:00
if (globs)
{
char **p;
for (p = globs; *p != NULL; p++)
g_string_append_printf (command, " | grep -v \"%s\"", *p);
2005-09-08 10:08:31 -07:00
}
g_strfreev (globs);
}
2005-11-18 05:02:42 -08:00
g_string_append_printf (command, " | sed -e \"s/ /\\\\\\ /g\" -e \"s/'/\\\\\\'/g\" "
"| xargs egrep -H -n %s-e '%s'",
2005-09-08 10:08:31 -07:00
!case_sensitive ? "-i " : "", pattern);
2005-09-11 12:43:23 -07:00
stuff->cmd = CMD_GREP;
2005-11-18 05:02:42 -08:00
moo_cmd_view_run_command (stuff->output, command->str, "Find in Files");
2005-09-11 12:43:23 -07:00
g_string_free (command, TRUE);
}
static void
execute_find (const char *pattern,
const char *dir,
const char *skip_files,
WindowStuff *stuff)
{
GString *command = NULL;
char **globs = NULL;
char **p;
g_return_if_fail (stuff->cmd == 0);
g_return_if_fail (pattern && pattern[0]);
g_return_if_fail (dir && dir[0]);
g_free (stuff->current_file);
stuff->current_file = NULL;
stuff->match_count = 0;
globs = g_strsplit (pattern, ";", 0);
g_return_if_fail (globs != NULL);
for (p = globs; *p != NULL; p++)
{
if (!command)
{
command = g_string_new ("");
g_string_printf (command, "find '%s' \\( -name \"%s\"", dir, *p);
}
else
{
g_string_append_printf (command, " -o -name \"%s\"", *p);
}
}
g_string_append (command, " \\)");
g_strfreev (globs);
g_string_append_printf (command, " -print");
if (skip_files)
{
globs = g_strsplit (skip_files, ";", 0);
if (globs)
{
char **p;
for (p = globs; *p != NULL; p++)
g_string_append_printf (command, " | grep -v \"%s\"", *p);
}
g_strfreev (globs);
}
stuff->cmd = CMD_FIND;
2005-11-18 05:02:42 -08:00
moo_cmd_view_run_command (stuff->output, command->str, "Find File");
g_string_free (command, TRUE);
2005-09-08 10:08:31 -07:00
}
static void
2005-09-11 23:57:29 -07:00
find_window_plugin_destroy (WindowStuff *stuff)
2005-09-08 10:08:31 -07:00
{
MooEditWindow *window = MOO_WIN_PLUGIN(stuff)->window;
2005-09-16 15:35:16 -07:00
2005-11-02 12:45:26 -08:00
if (stuff->output)
{
g_signal_handlers_disconnect_by_func (stuff->output,
(gpointer) command_exit,
stuff);
g_signal_handlers_disconnect_by_func (stuff->output,
(gpointer) process_line,
stuff);
moo_cmd_view_abort (stuff->output);
moo_edit_window_remove_pane (window, FIND_PLUGIN_ID);
}
2005-09-11 23:57:29 -07:00
if (stuff->grep_dialog)
gtk_widget_destroy (stuff->grep_dialog);
if (stuff->find_dialog)
gtk_widget_destroy (stuff->find_dialog);
if (stuff->grep_xml)
moo_glade_xml_unref (stuff->grep_xml);
if (stuff->find_xml)
moo_glade_xml_unref (stuff->find_xml);
if (stuff->grep_completion)
g_object_unref (stuff->grep_completion);
if (stuff->find_completion)
g_object_unref (stuff->find_completion);
g_free (stuff->current_file);
2005-09-08 10:08:31 -07:00
}
static gboolean
command_exit (MooLineView *view,
int status,
WindowStuff *stuff)
2005-09-08 10:08:31 -07:00
{
2005-09-11 12:43:23 -07:00
int cmd = stuff->cmd;
g_return_val_if_fail (cmd != 0, FALSE);
stuff->cmd = 0;
if (WIFEXITED (status))
2005-09-08 10:08:31 -07:00
{
2005-09-11 12:43:23 -07:00
char *msg = NULL;
guint8 exit_code = WEXITSTATUS (status);
/* xargs exits with code 123 if it's command exited with status 1-125*/
2005-09-11 12:43:23 -07:00
if (cmd == CMD_GREP && (!exit_code || exit_code == 123))
msg = g_strdup_printf ("*** %d matches found ***",
stuff->match_count);
else if (cmd == CMD_FIND && !exit_code)
msg = g_strdup_printf ("*** %d files found ***",
stuff->match_count);
else
return FALSE;
moo_line_view_write_line (view, msg, -1,
2005-09-11 12:43:23 -07:00
stuff->message_tag);
g_free (msg);
return TRUE;
2005-09-08 10:08:31 -07:00
}
return FALSE;
2005-09-08 10:08:31 -07:00
}
static gboolean
output_activate (WindowStuff *stuff,
2005-11-11 12:04:57 -08:00
int line)
2005-09-08 10:08:31 -07:00
{
MooEditor *editor;
MooEdit *doc;
2005-11-11 12:04:57 -08:00
FileLinePair *line_data;
line_data = moo_line_view_get_data (MOO_LINE_VIEW (stuff->output), line);
2005-09-08 10:08:31 -07:00
if (!line_data)
return FALSE;
editor = moo_edit_window_get_editor (stuff->window);
moo_editor_open_file (editor, stuff->window, NULL,
line_data->filename, NULL);
doc = moo_editor_get_doc (editor, line_data->filename);
2005-09-11 12:43:23 -07:00
if (!doc)
return TRUE;
2005-09-08 10:08:31 -07:00
moo_editor_set_active_doc (editor, doc);
gtk_widget_grab_focus (GTK_WIDGET (doc));
if (line_data->line >= 0)
moo_text_view_move_cursor (MOO_TEXT_VIEW (doc),
line_data->line, -1, TRUE);
2005-09-08 10:08:31 -07:00
return TRUE;
}
2005-09-11 23:57:29 -07:00
MOO_PLUGIN_DEFINE_INFO (find, FIND_PLUGIN_ID,
"Find", "Finds everything",
"Yevgen Muntyan <muntyan@tamu.edu>",
MOO_VERSION);
MOO_WIN_PLUGIN_DEFINE (Find, find,
find_window_plugin_create,
find_window_plugin_destroy);
MOO_PLUGIN_DEFINE_FULL (Find, find,
find_plugin_init, find_plugin_deinit,
NULL, NULL, NULL, NULL, NULL,
find_window_plugin_get_type (), 0);
2005-09-11 23:57:29 -07:00
gboolean
_moo_find_plugin_init (void)
2005-09-11 23:57:29 -07:00
{
return moo_plugin_register (find_plugin_get_type ());
}