[LyX/master] #8055 add support for system-wide find buffer on Mac

Stephan Witt switt at lyx.org
Sun Feb 14 17:53:12 UTC 2021


commit a476cc483945bdb3126602443ab9a1068bbb8397
Author: Stephan Witt <switt at lyx.org>
Date:   Sun Feb 14 18:56:25 2021 +0100

    #8055 add support for system-wide find buffer on Mac
    
    Command-E is standard for paste to find buffer on Mac. That's why the key binding is changed appropriately.
---
 lib/bind/mac.bind                 |    5 ++-
 src/BufferView.cpp                |   40 +++++++++++++++++++++++-------------
 src/BufferView.h                  |    3 ++
 src/frontends/Clipboard.h         |    7 ++++++
 src/frontends/qt/GuiClipboard.cpp |   21 +++++++++++++++++++
 src/frontends/qt/GuiClipboard.h   |    3 ++
 src/frontends/qt/GuiSearch.cpp    |   19 +++++++++++++++++
 src/frontends/qt/GuiSearch.h      |    1 +
 8 files changed, 82 insertions(+), 17 deletions(-)

diff --git a/lib/bind/mac.bind b/lib/bind/mac.bind
index f65895f..8d54e4c 100644
--- a/lib/bind/mac.bind
+++ b/lib/bind/mac.bind
@@ -139,8 +139,9 @@ Format 5
 \bind "C-d"                          "buffer-view dvi"	# 'd' for dvi
 \bind "C-M-d"                        "command-alternatives master-buffer-view dvi; buffer.view dvi"
 \bind "C-S-D"                        "buffer-update dvi"	# 'd' for dvi
-#  -: "Command-E"                    # Use the selection for a find
-\bind "C-e"                          "font-emph"
+#  +: "Command-E"                    # Use the selection for a find
+\bind "C-e"                          "search-string-set"
+\bind "C-M-e"                        "font-emph"
 #  +: "Command-F"                    # Open a Find window
 \bind "C-f"                          "dialog-toggle findreplace"
 \bind "C-S-f"                        "dialog-toggle findreplaceadv"
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index b67775e..7087973 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -61,6 +61,7 @@
 #include "frontends/NullPainter.h"
 #include "frontends/Painter.h"
 #include "frontends/Selection.h"
+#include "frontends/Clipboard.h"
 
 #include "support/convert.h"
 #include "support/debug.h"
@@ -274,9 +275,6 @@ struct BufferView::Private
 	  */
 	frontend::GuiBufferViewDelegate * gui_;
 
-	/// cache search string for simple search
-	docstring search_request_cache_;
-
 	///
 	map<string, Inset *> edited_insets_;
 
@@ -449,6 +447,22 @@ Buffer const & BufferView::buffer() const
 }
 
 
+docstring const & BufferView::searchRequestCache() const
+{
+	return theClipboard().getFindBuffer();
+}
+
+
+void BufferView::setSearchRequestCache(docstring const & text)
+{
+	bool casesensitive;
+	bool matchword;
+	bool forward;
+	docstring const search = string2find(text, casesensitive, matchword, forward);
+	theClipboard().setFindBuffer(search);
+}
+
+
 bool BufferView::needsFitCursor() const
 {
 	if (cursorStatus(d->cursor_) == CUR_INSIDE) {
@@ -1620,21 +1634,17 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 		docstring searched_string;
 
 		if (!cmd.argument().empty()) {
-			d->search_request_cache_ = cmd.argument();
+			setSearchRequestCache(cmd.argument());
 			searched_string = cmd.argument();
 		} else {
-			searched_string = d->search_request_cache_;
+			searched_string = searchRequestCache();
 		}
 
 		if (searched_string.empty())
 			break;
 
-		bool casesensitive;
-		bool matchword;
-		bool forward;
-		docstring const search = string2find(searched_string, casesensitive, matchword, forward);
 		docstring const data =
-			find2string(search, casesensitive, matchword, act == LFUN_WORD_FIND_FORWARD);
+			find2string(searched_string, false, false, act == LFUN_WORD_FIND_FORWARD);
 		bool found = lyxfind(this, FuncRequest(LFUN_WORD_FIND, data));
 		if (found)
 			dr.screenUpdate(Update::Force | Update::FitCursor);
@@ -1645,8 +1655,8 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 
 	case LFUN_WORD_FIND: {
 		docstring arg = cmd.argument();
-		if (arg.empty() && !d->search_request_cache_.empty())
-			arg = d->search_request_cache_;
+		if (arg.empty())
+			arg = searchRequestCache();
 		if (arg.empty()) {
 			lyx::dispatch(FuncRequest(LFUN_DIALOG_SHOW, "findreplace"));
 			break;
@@ -1656,14 +1666,14 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 		else
 			dr.setMessage(_("Search string not found!"));
 
-		d->search_request_cache_ = arg;
+		setSearchRequestCache(arg);
 		break;
 	}
 
 	case LFUN_SEARCH_STRING_SET: {
 		docstring pattern = cmd.argument();
 		if (!pattern.empty()) {
-			d->search_request_cache_ = pattern;
+			setSearchRequestCache(pattern);
 			break;
 		}
 		if (cur.selection())
@@ -1675,7 +1685,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 			cur.selection(false);
 			cur.pos() = spos;
 		}
-		d->search_request_cache_ = pattern;
+		setSearchRequestCache(pattern);
 		break;
 	}
 
diff --git a/src/BufferView.h b/src/BufferView.h
index 1b76da2..830c2d7 100644
--- a/src/BufferView.h
+++ b/src/BufferView.h
@@ -109,6 +109,9 @@ public:
 	/// bottom margin
 	int bottomMargin() const;
 
+	docstring const & searchRequestCache() const;
+	void setSearchRequestCache(docstring const & text);
+
 	/// return the on-screen size of this length
 	/*
 	 *  This is a wrapper around Length::inPixels that uses the
diff --git a/src/frontends/Clipboard.h b/src/frontends/Clipboard.h
index c06a17f..5e16a26 100644
--- a/src/frontends/Clipboard.h
+++ b/src/frontends/Clipboard.h
@@ -65,6 +65,8 @@ public:
 	/// Get the contents of the window system clipboard as graphics file.
 	virtual support::FileName getAsGraphics(Cursor const & cur, GraphicsType type) const = 0;
 
+	virtual docstring const & getFindBuffer() { return find_buffer_; }
+
 	/**
 	 * Fill the system clipboard. The format of \p lyx is as written in
 	 * .lyx files, the format of \p text is plain text.
@@ -79,6 +81,8 @@ public:
 	/// Put a general string on the system clipboard (not LyX text)
 	virtual void put(std::string const & text) const = 0;
 
+	virtual void setFindBuffer(docstring const & text) { find_buffer_ = text;}
+
 	/// Does the clipboard contain text contents?
 	virtual bool hasTextContents(TextType type = AnyTextType) const = 0;
 	/// Does the clipboard contain graphics contents of a certain type?
@@ -94,6 +98,9 @@ public:
 	/// \returns true if both the LyX and the plaintext versions of the
 	/// clipboard are empty, and no supported graphics format is available.
 	virtual bool empty() const = 0;
+
+private:
+	docstring find_buffer_;
 };
 
 } // namespace frontend
diff --git a/src/frontends/qt/GuiClipboard.cpp b/src/frontends/qt/GuiClipboard.cpp
index f78ab1c..697977d 100644
--- a/src/frontends/qt/GuiClipboard.cpp
+++ b/src/frontends/qt/GuiClipboard.cpp
@@ -12,6 +12,7 @@
 
 #include <config.h>
 
+#include "GuiApplication.h"
 #include "GuiClipboard.h"
 
 #include "Buffer.h"
@@ -111,6 +112,11 @@ GuiClipboard::GuiClipboard()
 {
 	connect(qApp->clipboard(), SIGNAL(dataChanged()),
 		this, SLOT(on_dataChanged()));
+	if(qApp->clipboard()->supportsFindBuffer()) {
+		connect(qApp->clipboard(), SIGNAL(findBufferChanged()),
+			this, SLOT(on_findChanged()));
+		on_findChanged();
+	}
 	// initialize clipboard status.
 	update();
 }
@@ -544,6 +550,21 @@ bool GuiClipboard::hasInternal() const
 }
 
 
+void GuiClipboard::setFindBuffer(docstring const & text)
+{
+	LYXERR(Debug::CLIPBOARD, "new findbuffer: " << text);
+	Clipboard::setFindBuffer(text);
+	qApp->clipboard()->setText(toqstr(text), QClipboard::FindBuffer);
+}
+
+
+void GuiClipboard::on_findChanged()
+{
+	Clipboard::setFindBuffer(from_utf8(fromqstr(
+		qApp->clipboard()->text(QClipboard::FindBuffer))));
+}
+
+
 void GuiClipboard::on_dataChanged()
 {
 	update();
diff --git a/src/frontends/qt/GuiClipboard.h b/src/frontends/qt/GuiClipboard.h
index 278adb8..cdd4e85 100644
--- a/src/frontends/qt/GuiClipboard.h
+++ b/src/frontends/qt/GuiClipboard.h
@@ -82,8 +82,11 @@ public:
 	support::FileName getPastedGraphicsFileName(Cursor const & cur,
 		Clipboard::GraphicsType & type) const;
 
+	void setFindBuffer(docstring const & text) override;
+
 private Q_SLOTS:
 	void on_dataChanged();
+	void on_findChanged();
 	void update();
 
 private:
diff --git a/src/frontends/qt/GuiSearch.cpp b/src/frontends/qt/GuiSearch.cpp
index d6ec3f8..5aec60b 100644
--- a/src/frontends/qt/GuiSearch.cpp
+++ b/src/frontends/qt/GuiSearch.cpp
@@ -12,6 +12,7 @@
 
 #include <config.h>
 
+#include "GuiApplication.h"
 #include "GuiSearch.h"
 
 #include "lyxfind.h"
@@ -28,8 +29,11 @@
 
 #include "support/debug.h"
 #include "support/gettext.h"
+#include "support/debug.h"
 #include "frontends/alert.h"
+#include "frontends/Clipboard.h"
 
+#include <QClipboard>
 #include <QLineEdit>
 #include <QSettings>
 #include <QShowEvent>
@@ -71,6 +75,11 @@ GuiSearchWidget::GuiSearchWidget(QWidget * parent)
 	connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
 	connect(findCO, SIGNAL(editTextChanged(QString)),
 		this, SLOT(findChanged()));
+	if(qApp->clipboard()->supportsFindBuffer()) {
+		connect(qApp->clipboard(), SIGNAL(findBufferChanged()),
+			this, SLOT(findBufferChanged()));
+		findBufferChanged();
+	}
 
 	setFocusProxy(findCO);
 
@@ -149,6 +158,16 @@ void GuiSearchWidget::showEvent(QShowEvent * e)
 }
 
 
+void GuiSearchWidget::findBufferChanged()
+{
+	docstring search = theClipboard().getFindBuffer();
+	if (!search.empty()) {
+		LYXERR(Debug::CLIPBOARD, "from findbuffer: " << search);
+		findCO->setCurrentText(toqstr(search));
+	}
+}
+
+
 void GuiSearchWidget::findChanged()
 {
 	findPB->setEnabled(!findCO->currentText().isEmpty());
diff --git a/src/frontends/qt/GuiSearch.h b/src/frontends/qt/GuiSearch.h
index b74f9f0..92324df 100644
--- a/src/frontends/qt/GuiSearch.h
+++ b/src/frontends/qt/GuiSearch.h
@@ -42,6 +42,7 @@ public:
 
 private Q_SLOTS:
 	void findChanged();
+	void findBufferChanged();
 	void findClicked(bool const backwards = false);
 	void findPrevClicked();
 	void replaceClicked(bool const backwards = false);


More information about the lyx-cvs mailing list