[LyX/master] Introduce search-string-set (#8055)

Juergen Spitzmueller spitz at lyx.org
Fri Feb 12 13:29:22 UTC 2021


commit 7f90e3b7d2375e424ea7d3cb29bad4018cc60bee
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Fri Feb 12 14:30:50 2021 +0100

    Introduce search-string-set (#8055)
    
    This stores its argument, the currently selected text or the word under
    cursor in the search cache that is used by word-find[-backward|-forward]
    if no argument is given to those.
    
    Prerequisite for a feature apparently expected on the Mac.
---
 lib/RELEASE-NOTES  |    4 ++++
 src/BufferView.cpp |   41 +++++++++++++++++++++++++++++------------
 src/FuncCode.h     |    1 +
 src/LyXAction.cpp  |   11 +++++++++++
 4 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/lib/RELEASE-NOTES b/lib/RELEASE-NOTES
index 1f8069b..4e92ccf 100644
--- a/lib/RELEASE-NOTES
+++ b/lib/RELEASE-NOTES
@@ -97,6 +97,10 @@
 * paragraph-select is a new convenience function to select the paragraph
   surrounding the actual cursor position.
 
+* search-string-set [arg] stores the <arg>, the currently selected text or the word under
+  cursor in the search cache that is used by word-find[-backward|-forward] if no argument
+  is given to those. 
+
 * inset-split is a new convenience function that splits an inset into two at the given
   cursor position. This is only implemented for text insets currently.
 
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index 88400d7..3764253 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -275,7 +275,7 @@ struct BufferView::Private
 	frontend::GuiBufferViewDelegate * gui_;
 
 	/// Cache for Find Next
-	FuncRequest search_request_cache_;
+	docstring search_request_cache_;
 
 	///
 	map<string, Inset *> edited_insets_;
@@ -1155,6 +1155,7 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
 	case LFUN_MARK_OFF:
 	case LFUN_MARK_ON:
 	case LFUN_MARK_TOGGLE:
+	case LFUN_SEARCH_STRING_SET:
 	case LFUN_SCREEN_RECENTER:
 	case LFUN_SCREEN_SHOW_CURSOR:
 	case LFUN_BIBTEX_DATABASE_ADD:
@@ -1609,16 +1610,13 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 
 	case LFUN_WORD_FIND_FORWARD:
 	case LFUN_WORD_FIND_BACKWARD: {
-		// FIXME THREAD
-		// Would it maybe be better if this variable were view specific anyway?
-		static docstring last_search;
 		docstring searched_string;
 
 		if (!cmd.argument().empty()) {
-			last_search = cmd.argument();
+			d->search_request_cache_ = cmd.argument();
 			searched_string = cmd.argument();
 		} else {
-			searched_string = last_search;
+			searched_string = d->search_request_cache_;
 		}
 
 		if (searched_string.empty())
@@ -1636,19 +1634,38 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 	}
 
 	case LFUN_WORD_FIND: {
-		FuncRequest req = cmd;
-		if (cmd.argument().empty() && !d->search_request_cache_.argument().empty())
-			req = d->search_request_cache_;
-		if (req.argument().empty()) {
+		docstring arg = cmd.argument();
+		if (arg.empty() && !d->search_request_cache_.empty())
+			arg = d->search_request_cache_;
+		if (arg.empty()) {
 			lyx::dispatch(FuncRequest(LFUN_DIALOG_SHOW, "findreplace"));
 			break;
 		}
-		if (lyxfind(this, req))
+		if (lyxfind(this, FuncRequest(act, arg)))
 			dr.screenUpdate(Update::Force | Update::FitCursor);
 		else
 			dr.setMessage(_("Search string not found!"));
 
-		d->search_request_cache_ = req;
+		d->search_request_cache_ = arg;
+		break;
+	}
+
+	case LFUN_SEARCH_STRING_SET: {
+		docstring pattern = cmd.argument();
+		if (!pattern.empty()) {
+			d->search_request_cache_ = pattern;
+			break;
+		}
+		if (cur.selection())
+			pattern = cur.selectionAsString(false);
+		else {
+			pos_type spos = cur.pos();
+			cur.innerText()->selectWord(cur, WHOLE_WORD);
+			pattern = cur.selectionAsString(false);
+			cur.selection(false);
+			cur.pos() = spos;
+		}
+		d->search_request_cache_ = pattern;
 		break;
 	}
 
diff --git a/src/FuncCode.h b/src/FuncCode.h
index a720c64..f473cda 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -495,6 +495,7 @@ enum FuncCode
 	// 385
 	LFUN_INSET_SPLIT,               // jspitzm 20201222
 	LFUN_LYXFILES_OPEN,             // jspitzm 20210210
+	LFUN_SEARCH_STRING_SET,         // stwitt/jspitzm 20210212
 	LFUN_LASTACTION                 // end of the table
 };
 
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index 38fa2c3..0d43ea5 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -4346,6 +4346,17 @@ void LyXAction::init()
  */
 		{ LFUN_WORD_FIND, "word-find", ReadOnly, Edit },
 
+/*! 
+ * \var lyx::FuncCode lyx::LFUN_SEARCH_STRING_SET
+ * \li Action: Set search string buffer.
+ * \li Syntax: search-string-set [<DATA>] 
+ * \li Params: <DATA>: string to search for. If no parameter is given,
+ *                     use (word under) selection.
+ * \li Origin: stwitt, spitz, Feb 12 2021 
+ * \endvar 
+ */ 
+		{ LFUN_SEARCH_STRING_SET, "search-string-set", ReadOnly, Edit }, 
+
 /*!
  * \var lyx::FuncCode lyx::LFUN_WORD_FINDADV
  * \li Action: Search for next occurrence of a pattern.


More information about the lyx-cvs mailing list