Andre Poenitz wrote:
> On Wed, Jan 07, 2004 at 02:46:56PM +0100, Andre' Poenitz wrote:
>>
>> This makes the logic a bit clearer IMO
>
> 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.
--
Angus
Index: src/lfuns.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lfuns.h,v
retrieving revision 1.8
diff -u -p -r1.8 lfuns.h
--- src/lfuns.h 26 Apr 2003 19:01:33 -0000 1.8
+++ src/lfuns.h 30 Apr 2003 20:27:12 -0000
@@ -337,6 +337,8 @@ enum kb_action {
LFUN_INSET_SETTINGS,
LFUN_PARAGRAPH_APPLY,
LFUN_PARAGRAPH_UPDATE,
+ LFUN_WORD_FIND,
+ LFUN_WORD_REPLACE,
LFUN_LASTACTION // end of the table
};
Index: src/frontends/controllers/ControlSearch.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ControlSearch.C,v
retrieving revision 1.23
diff -u -p -r1.23 ControlSearch.C
--- src/frontends/controllers/ControlSearch.C 13 Feb 2003 16:52:48 -0000 1.23
+++ src/frontends/controllers/ControlSearch.C 30 Apr 2003 20:27:13 -0000
@@ -13,6 +13,8 @@
#include "ControlSearch.h"
+#include "debug.h"
+#include "funcrequest.h"
#include "gettext.h"
#include "lyxfind.h"
@@ -20,45 +22,198 @@
#include "support/lstrings.h"
+namespace lyxfind {
-ControlSearch::ControlSearch(LyXView & lv, Dialogs & d)
- : ControlDialogBD(lv, d)
-{}
+/** 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 lyxfind
+
+/* 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 lyxfind {
-void ControlSearch::find(string const & search,
+string const find2string(string const & search,
bool casesensitive, bool matchword, bool forward)
{
- bool const found = lyxfind::LyXFind(bufferview(), search,
+ 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 once)
+{
+ ostringstream ss;
+ ss << search << '\n'
+ << replace << '\n'
+ << int(casesensitive) << ' '
+ << int(matchword) << ' '
+ << int(all) << ' '
+ << int(once);
+
+ 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 LyXFind(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 = lyxfind::LyXFind(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 all)
+void LyXReplace(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
- bool const once = !all;
+ 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 once = parse_bool(howto);
+
+ BufferView * bv = ev.view();
+ LyXView * lv = bv->owner();
+
int const replace_count =
- lyxfind::LyXReplace(bufferview(),
- search, replace, true, casesensitive,
+ lyxfind::LyXReplace(bv, search, replace, true, casesensitive,
matchword, all, once);
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 lyxfind
+
+
+namespace {
+
+void test_dispatch(FuncRequest const & ev)
+{
+ string argument = ev.argument;
+ kb_action action = ev.action;
+
+ switch (action) {
+ case LFUN_WORD_FIND:
+ lyxfind::LyXFind(ev);
+ break;
+ case LFUN_WORD_REPLACE:
+ lyxfind::LyXReplace(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 =
+ lyxfind::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 all)
+{
+ // If not replacing all instances of the word, then do not
+ // move on to the next instance once the present instance has been
+ // changed
+ bool const once = !all;
+
+ string const data =
+ lyxfind::replace2string(search, replace,
+ casesensitive, matchword, all, once);
+ FuncRequest const fr(bufferview(), LFUN_WORD_REPLACE, data);
+ test_dispatch(fr);
}