Commit 36bfa838 authored by Daniel Elstner's avatar Daniel Elstner
Browse files

:find_matches): Add a feedback slot parameter and invoke it for every

* src/filebuffer.{cc,h} (FileBuffer::find_matches): Add a feedback
slot parameter and invoke it for every matching line.  The current
line number and the subject string are passed as arguments to the
feedback slot.

* src/filetree.{cc,h} (FileTree::signal_feedback): New signal.
(FileTree::find_matches_at_path_iter): Pass a feedback slot to
FileBuffer::find_matches() that forwards to signal_feedback()
with the fileinfo object bound as third argument.

* src/mainwindow.{cc,h} (print_location): New handler for the
feedback signal which prints the location of the match to standard
output, in the same format as used by 'grep -n'.
(InitState::feedback): New bool member.
(MainWindow::initialize): Connect the filetree's signal_feedback
to print_location if the init state's feedback flag is true.

* src/main.cc (parse_command_line): Add the --line-number command
line argument, intended to work just like the grep equivalent.
parent 58c000c7
2004-07-14 Daniel Elstner <daniel.elstner@gmx.net>
* src/filebuffer.{cc,h} (FileBuffer::find_matches): Add a feedback
slot parameter and invoke it for every matching line. The current
line number and the subject string are passed as arguments to the
feedback slot.
* src/filetree.{cc,h} (FileTree::signal_feedback): New signal.
(FileTree::find_matches_at_path_iter): Pass a feedback slot to
FileBuffer::find_matches() that forwards to signal_feedback()
with the fileinfo object bound as third argument.
* src/mainwindow.{cc,h} (print_location): New handler for the
feedback signal which prints the location of the match to standard
output, in the same format as used by 'grep -n'.
(InitState::feedback): New bool member.
(MainWindow::initialize): Connect the filetree's signal_feedback
to print_location if the init state's feedback flag is true.
* src/main.cc (parse_command_line): Add the --line-number command
line argument, intended to work just like the grep equivalent.
2004-07-13 Daniel Elstner <daniel.elstner@gmx.net>
* src/mainwindow.{cc,h} (MainWindow::autorun_idle): New method
......
......@@ -259,7 +259,8 @@ bool FileBuffer::in_user_action() const
* If multiple is false then every line is matched only once, otherwise
* multiple matches per line will be found (like modifier /g in Perl).
*/
int FileBuffer::find_matches(Pcre::Pattern& pattern, bool multiple)
int FileBuffer::find_matches(Pcre::Pattern& pattern, bool multiple,
const sigc::slot<void,int,const Glib::ustring&>& feedback)
{
ScopedLock lock (*this);
......@@ -332,6 +333,9 @@ int FileBuffer::find_matches(Pcre::Pattern& pattern, bool multiple)
apply_tag(tagtable->match, start, stop);
if (offset == 0 && feedback)
feedback(line.get_line(), subject);
last_was_empty = (bounds.first == bounds.second);
offset = bounds.second;
}
......
......@@ -50,7 +50,8 @@ public:
bool is_freeable() const;
bool in_user_action() const;
int find_matches(Pcre::Pattern& pattern, bool multiple);
int find_matches(Pcre::Pattern& pattern, bool multiple,
const sigc::slot<void,int,const Glib::ustring&>& feedback);
int get_match_count() const;
int get_match_index() const;
......
......@@ -565,7 +565,15 @@ bool FileTree::find_matches_at_path_iter(const Gtk::TreeModel::Path& path,
Util::ScopedConnection conn (buffer->signal_pulse.connect(signal_pulse.make_slot()));
const int old_match_count = buffer->get_match_count();
const int new_match_count = buffer->find_matches(find_data.pattern, find_data.multiple);
// Optimize the common case and construct the feedback slot only if there
// are actually any handlers connected to the signal. find_matches() can
// then check whether the slot is empty to avoid providing arguments that
// are never going to be used.
const int new_match_count = buffer->find_matches(
find_data.pattern, find_data.multiple,
(signal_feedback.empty()) ? sigc::slot<void,int,const Glib::ustring&>()
: sigc::bind(signal_feedback.make_slot(), fileinfo));
if (new_match_count > 0)
{
......
......@@ -75,6 +75,9 @@ public:
sigc::signal<bool> signal_pulse;
sigc::signal<void,UndoActionPtr> signal_undo_stack_push;
// Provide line number, subject and file info for match location output.
sigc::signal<void,int,const Glib::ustring&,FileInfoPtr> signal_feedback;
protected:
virtual void on_style_changed(const Glib::RefPtr<Gtk::Style>& previous_style);
......
......@@ -98,13 +98,14 @@ std::auto_ptr<Regexxer::InitState> parse_command_line(int argc, char** argv)
static const poptOption option_table[] =
{
{ "pattern", 'p', STRING, 0, 'p', N_("Find files matching PATTERN"), N_("PATTERN") },
{ "no-recursion", 'R', NONE, 0, 'R', N_("Do not recurse into subdirectories"), 0 },
{ "hidden", 'h', NONE, 0, 'h', N_("Also find hidden files"), 0 },
{ "no-recursion", 'R', NONE, 0, 'R', N_("Do not recurse into subdirectories"), 0 },
{ "hidden", 'h', NONE, 0, 'h', N_("Also find hidden files"), 0 },
{ "regex", 'e', STRING, 0, 'e', N_("Find text matching REGEX"), N_("REGEX") },
{ "no-global", 'G', NONE, 0, 'G', N_("Find only the first match in a line"), 0 },
{ "ignore-case", 'i', NONE, 0, 'i', N_("Ignore case distinctions"), 0 },
{ "no-global", 'G', NONE, 0, 'G', N_("Find only the first match in a line"), 0 },
{ "ignore-case", 'i', NONE, 0, 'i', N_("Ignore case distinctions"), 0 },
{ "substitution", 's', STRING, 0, 's', N_("Replace matches with STRING"), N_("STRING") },
{ "no-autorun", 'A', NONE, 0, 'A', N_("Do not automatically start search"), 0 },
{ "line-number", 'n', NONE, 0, 'n', N_("Print match location to standard output"), 0 },
{ "no-autorun", 'A', NONE, 0, 'A', N_("Do not automatically start search"), 0 },
POPT_AUTOHELP
POPT_TABLEEND
};
......@@ -129,6 +130,7 @@ std::auto_ptr<Regexxer::InitState> parse_command_line(int argc, char** argv)
case 'h': init->hidden = true; break;
case 'G': init->global = false; break;
case 'i': init->ignorecase = true; break;
case 'n': init->feedback = true; break;
case 'A': autorun = false; break;
}
......
......@@ -36,6 +36,7 @@
#include <libglademm/xml.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <config.h>
......@@ -98,6 +99,21 @@ FileErrorDialog::FileErrorDialog(Gtk::Window& parent, const Glib::ustring& messa
FileErrorDialog::~FileErrorDialog()
{}
void print_location(int linenumber, const Glib::ustring& subject, Regexxer::FileInfoPtr fileinfo)
{
std::cout << fileinfo->fullname << ':' << linenumber + 1 << ':';
std::string charset;
if (Glib::get_charset(charset))
std::cout << subject.raw(); // charset is UTF-8
else
std::cout << Glib::convert_with_fallback(subject.raw(), charset, "UTF-8");
std::cout << std::endl;
}
} // anonymous namespace
......@@ -116,6 +132,7 @@ InitState::InitState()
hidden (false),
global (true),
ignorecase (false),
feedback (false),
autorun (false)
{}
......@@ -188,6 +205,9 @@ void MainWindow::initialize(std::auto_ptr<InitState> init)
button_multiple_ ->set_active(init->global);
button_caseless_ ->set_active(init->ignorecase);
if (init->feedback)
filetree_->signal_feedback.connect(&print_location);
if (init->autorun)
Glib::signal_idle().connect(sigc::mem_fun(*this, &MainWindow::autorun_idle));
}
......
......@@ -63,6 +63,7 @@ struct InitState
bool hidden;
bool global;
bool ignorecase;
bool feedback;
bool autorun;
InitState();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment