Andre Poenitz wrote:
>> > Is there a particular reason why s&r is not LFUN-inified?
>>
>> None. I have a half-baked patch (attached) that adds the lfuns. At
>> the moment they are still handled in
>> frontends/controllers/ControlSearch.C but it's probably trivial to
>> move them to a more appropriate location if you're feeling
>> energetic.
>
> Could you commit that lest I get clashes when dabbling there myself?
Well given that the patch is several months old, I got clashes myself
when applying it. The compilable patch is attached. Note that all the
namespace lyx::find stuff should be moved into lyxfind.[Ch]. I'd be
grateful if you would do that (and merge in the existing
lyx::find::find and lyx::find::replace\(All\)* functions.)
I'm not going to apply this as-is, because I haven't tested it. I
guess that it also needs "word-find" and "word-replace" addtions to
LyXAction.C.
Nonetheless, I hope it makes your life easier...
--
Angus
Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.1769
diff -u -p -r1.1769 ChangeLog
--- src/ChangeLog 7 Jan 2004 14:47:24 -0000 1.1769
+++ src/ChangeLog 7 Jan 2004 14:51:32 -0000
@@ -1,3 +1,7 @@
+2004-01-07 Angus Leeming <[EMAIL PROTECTED]>
+
+ * lfuns.h: add LFUN_WORD_FIND and LFUN_WORD_REPLACE.
+
2004-01-07 Alfredo Braunstein <[EMAIL PROTECTED]>
* text.C (breakParagraph): remove an outdated #warning
Index: src/lfuns.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lfuns.h,v
retrieving revision 1.26
diff -u -p -r1.26 lfuns.h
--- src/lfuns.h 29 Dec 2003 13:55:42 -0000 1.26
+++ src/lfuns.h 7 Jan 2004 14:51:32 -0000
@@ -331,8 +331,11 @@ enum kb_action {
LFUN_FINISHED_UP,
LFUN_FINISHED_DOWN,
LFUN_INSERT_CHARSTYLE,
- LFUN_LASTACTION // end of the table
+ LFUN_WORD_FIND,
// 255
+ LFUN_WORD_REPLACE,
+
+ LFUN_LASTACTION // end of the table
};
std::ostream & operator<<(std::ostream &, kb_action);
Index: src/frontends/controllers/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ChangeLog,v
retrieving revision 1.405
diff -u -p -r1.405 ChangeLog
--- src/frontends/controllers/ChangeLog 14 Dec 2003 16:33:53 -0000 1.405
+++ src/frontends/controllers/ChangeLog 7 Jan 2004 14:51:35 -0000
@@ -1,3 +1,8 @@
+2004-01-07 Angus Leeming <[EMAIL PROTECTED]>
+
+ * ControlSearch.C: rewrite to use LFUN_WORD_FIND and LFUN_WORD_REPLACE.
+ The lfun-handling code should now be moved to lyxfind.[Ch].
+
2003-12-14 Angus Leeming <[EMAIL PROTECTED]>
* ControlBranch.[Ch] (branchlist): new member function.
Index: src/frontends/controllers/ControlSearch.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ControlSearch.C,v
retrieving revision 1.28
diff -u -p -r1.28 ControlSearch.C
--- src/frontends/controllers/ControlSearch.C 4 Nov 2003 12:01:10 -0000 1.28
+++ src/frontends/controllers/ControlSearch.C 7 Jan 2004 14:51:35 -0000
@@ -12,56 +12,222 @@
#include "ControlSearch.h"
+#include "debug.h"
+#include "funcrequest.h"
#include "gettext.h"
#include "lyxfind.h"
#include "frontends/LyXView.h"
+#include "support/lstrings.h"
#include "support/tostr.h"
+#include "support/std_sstream.h"
+using lyx::support::split;
+
+using std::ostringstream;
using std::string;
-ControlSearch::ControlSearch(LyXView & lv, Dialogs & d)
- : ControlDialogBD(lv, d)
-{}
+namespace lyx {
+namespace find {
+
+/** Encode the parameters needed to find \param search as a string
+ * that can be dispatched to the LyX core in a FuncRequest wrapper.
+ */
+string const find2string(string const & search,
+ bool casesensitive, bool matchword, bool forward);
+
+/** Encode the parameters needed to replace \param search with \param replace
+ * as a string that can be dispatched to the LyX core in a FuncRequest
+ * wrapper.
+ */
+string const replace2string(string const & search, string const & replace,
+ bool casesensitive, bool matchword,
+ bool all, bool once);
+/** Parse the string encoding of the find request that is found in
+ * \param ev.argument and act on it.
+ */
+void LyXFind(FuncRequest const & ev);
+
+/** Parse the string encoding of the replace request that is found in
+ * \param ev.argument and act on it.
+ */
+void LyXReplace(FuncRequest const &);
+
+} // namespace find
+} // namespace lyx
+
+/* This class is in a state of flux from the 'old' to the 'new' dialog design.
+ The 'lyxfind' stuff below should be moved into the LyX core now that it
+ is seen to be working.
+*/
+#include "BufferView.h"
+
+namespace lyx {
+namespace find {
-void ControlSearch::find(string const & search,
- bool casesensitive, bool matchword, bool forward)
+string const find2string(string const & search,
+ bool casesensitive, bool matchword, bool forward)
{
- bool const found = lyx::find::find(bufferview(), search,
- casesensitive, matchword,
- forward);
+ ostringstream ss;
+ ss << search << '\n'
+ << int(casesensitive) << ' '
+ << int(matchword) << ' '
+ << int(forward);
+
+ return ss.str();
+}
+
+
+string const replace2string(string const & search, string const & replace,
+ bool casesensitive, bool matchword,
+ bool all, bool forward)
+{
+ ostringstream ss;
+ ss << search << '\n'
+ << replace << '\n'
+ << int(casesensitive) << ' '
+ << int(matchword) << ' '
+ << int(all) << ' '
+ << int(forward);
+
+ return ss.str();
+}
+
+namespace {
+
+bool parse_bool(string & howto)
+{
+ if (howto.empty())
+ return false;
+ string var;
+ howto = split(howto, var, ' ');
+ return (var == "1");
+}
+
+} // namespace anon
+
+
+void find(FuncRequest const & ev)
+{
+ if (!ev.view() || ev.action != LFUN_WORD_FIND)
+ return;
+
+ // data is of the form
+ // <search> + "\n <casesensitive> <matchword> <forward>"
+ string search;
+ string howto = split(ev.argument, search, '\n');
+
+ bool casesensitive = parse_bool(howto);
+ bool matchword = parse_bool(howto);
+ bool forward = parse_bool(howto);
+
+ BufferView * bv = ev.view();
+ bool const found = lyx::find::find(bv, search,
+ forward, casesensitive,
+ matchword);
if (!found)
- lv_.message(_("String not found!"));
+ bv->owner()->message(_("String not found!"));
}
-void ControlSearch::replace(string const & search, string const & replace,
- bool casesensitive, bool matchword,
- bool forward, bool all)
+void replace(FuncRequest const & ev)
{
- // If not replacing all instances of the word, then do not
- // move on to the next instance once the present instance has been
- // changed
- int const replace_count = all ?
- lyx::find::replaceAll(bufferview(), search, replace,
- casesensitive, matchword)
- : lyx::find::replace(bufferview(), search, replace,
- casesensitive, matchword, forward);
+ if (!ev.view() || ev.action != LFUN_WORD_REPLACE)
+ return;
+
+ // data is of the form
+ // <search> + '\n' + <replace> +
+ // "\n <casesensitive> <matchword> <all> <once>"
+ string search;
+ string replace;
+ string howto = split(ev.argument, search, '\n');
+ howto = split(howto, replace, '\n');
+
+ bool casesensitive = parse_bool(howto);
+ bool matchword = parse_bool(howto);
+ bool all = parse_bool(howto);
+ bool forward = parse_bool(howto);
+
+ BufferView * bv = ev.view();
+ LyXView * lv = bv->owner();
+ int const replace_count = all ?
+ lyx::find::replaceAll(bv, search, replace,
+ casesensitive, matchword) :
+ lyx::find::replace(bv, search, replace,
+ casesensitive, matchword, forward);
+
if (replace_count == 0) {
- lv_.message(_("String not found!"));
+ lv->message(_("String not found!"));
} else {
if (replace_count == 1) {
- lv_.message(_("String has been replaced."));
+ lv->message(_("String has been replaced."));
} else {
string str = tostr(replace_count);
str += _(" strings have been replaced.");
- lv_.message(str);
+ lv->message(str);
}
}
+}
+
+} // namespace find
+} // namespace lyx
+
+
+namespace {
+
+void test_dispatch(FuncRequest const & ev)
+{
+ string argument = ev.argument;
+ kb_action action = ev.action;
+
+ switch (action) {
+ case LFUN_WORD_FIND:
+ lyx::find::find(ev);
+ break;
+ case LFUN_WORD_REPLACE:
+ lyx::find::replace(ev);
+ break;
+ default:
+ break;
+ }
+}
+
+} // namespace anon
+
+
+/* The ControlSeach class is now in a fit state to derive from
+ Dialog::Controller
+*/
+ControlSearch::ControlSearch(LyXView & lv, Dialogs & d)
+ : ControlDialogBD(lv, d)
+{}
+
+
+void ControlSearch::find(string const & search,
+ bool casesensitive, bool matchword, bool forward)
+{
+ string const data =
+ lyx::find::find2string(search,
+ casesensitive, matchword, forward);
+ FuncRequest const fr(bufferview(), LFUN_WORD_FIND, data);
+ test_dispatch(fr);
+}
+
+
+void ControlSearch::replace(string const & search, string const & replace,
+ bool casesensitive, bool matchword,
+ bool forward, bool all)
+{
+ string const data =
+ lyx::find::replace2string(search, replace,
+ casesensitive, matchword,
+ all, forward);
+ FuncRequest const fr(bufferview(), LFUN_WORD_REPLACE, data);
+ test_dispatch(fr);
}