[LyX/master] Allow relative statistics values in statusbar.

Pavel Sanda sanda at lyx.org
Fri Apr 5 17:44:00 UTC 2024


commit 7976cc2dac4ec69e35441b64f5e6b37f9a7f51b9
Author: Pavel Sanda <sanda at lyx.org>
Date:   Fri Apr 5 19:34:29 2024 +0200

    Allow relative statistics values in statusbar.
    
    https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg221311.html
---
 lib/ui/stdcontext.inc        |  2 ++
 src/BufferView.cpp           | 53 ++++++++++++++++++++++++++++++++++++++++++++
 src/BufferView.h             |  6 +++++
 src/FuncCode.h               |  1 +
 src/LyXAction.cpp            | 10 +++++++++
 src/frontends/qt/GuiView.cpp |  9 ++++----
 6 files changed, 77 insertions(+), 4 deletions(-)

diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc
index 32d76e603e..98d23bf177 100644
--- a/lib/ui/stdcontext.inc
+++ b/lib/ui/stdcontext.inc
@@ -774,6 +774,8 @@ Menuset
 		Item "Word Count|W" "ui-toggle statistics-w"
 		Item "Character Count|C" "ui-toggle statistics-cb"
 		Item "Character Count (No Blanks)|h" "ui-toggle statistics-c"
+		Item "Start Statistics Relative to Current Count|R" "statistics-reference-clamp"
+		OptItem "Reset to Absolute Statistics Count|A" "statistics-reference-clamp reset"
 	End
 
 End
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index c03f928c4d..92f231c227 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -298,6 +298,12 @@ struct BufferView::Private
 	frontend::CaretGeometry caret_geometry_;
 	///
 	bool mouse_selecting_ = false;
+	/// Reference value for statistics (essentially subtract this from the actual value to see relative counts)
+	/// (words/chars/chars no blanks)
+	int stats_ref_value_w_ = 0;
+	int stats_ref_value_c_ = 0;
+	int stats_ref_value_nb_ = 0;
+
 };
 
 
@@ -1343,6 +1349,17 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
 		flag.setEnabled(cur.selection());
 		break;
 
+	case LFUN_STATISTICS_REFERENCE_CLAMP: {
+		// disable optitem reset if clamp not used
+		if  (cmd.argument() == "reset" && d->stats_ref_value_c_ == 0) {
+				flag.setEnabled(false);
+				break;
+		}
+		flag.setEnabled(true);
+		break;
+
+	}
+
 	default:
 		return false;
 	}
@@ -2014,6 +2031,24 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 	}
 		break;
 
+	case LFUN_STATISTICS_REFERENCE_CLAMP: {
+		if  (cmd.argument() == "reset") {
+			d->stats_ref_value_w_ = d->stats_ref_value_c_ = d->stats_ref_value_nb_ = 0;
+			break;
+		}
+
+		DocIterator from, to;
+		from = doc_iterator_begin(&buffer_);
+		to = doc_iterator_end(&buffer_);
+		buffer_.updateStatistics(from, to);
+
+		d->stats_ref_value_w_ = buffer_.wordCount();
+		d->stats_ref_value_c_ = buffer_.charCount(true);
+		d->stats_ref_value_nb_ = buffer_.charCount(false);
+		break;
+	}
+
+
 	case LFUN_SCREEN_UP:
 	case LFUN_SCREEN_DOWN: {
 		Point p = getPos(cur);
@@ -2623,6 +2658,24 @@ bool BufferView::mouseSelecting() const
 }
 
 
+int BufferView::stats_ref_value_w() const
+{
+	return d->stats_ref_value_w_;
+}
+
+
+int BufferView::stats_ref_value_c() const
+{
+	return d->stats_ref_value_c_;
+}
+
+
+int BufferView::stats_ref_value_nb() const
+{
+	return d->stats_ref_value_nb_;
+}
+
+
 void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
 {
 	//lyxerr << "[ cmd0 " << cmd0 << "]" << endl;
diff --git a/src/BufferView.h b/src/BufferView.h
index 327536a916..6821efeb20 100644
--- a/src/BufferView.h
+++ b/src/BufferView.h
@@ -401,6 +401,12 @@ public:
 	/// Are we currently performing a selection with the mouse?
 	bool mouseSelecting() const;
 
+	/// Reference value for statistics (essentially subtract this from the actual value to see relative counts)
+	/// (words/chars/chars no blanks)
+	int stats_ref_value_w() const;
+	int stats_ref_value_c() const;
+	int stats_ref_value_nb() const;
+
 private:
 	/// noncopyable
 	BufferView(BufferView const &);
diff --git a/src/FuncCode.h b/src/FuncCode.h
index 19f41295b1..a5f638d6d7 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -508,6 +508,7 @@ enum FuncCode
 	LFUN_TAB_GROUP_NEXT,            // daniel 20220130
 	LFUN_TAB_GROUP_PREVIOUS,        // daniel 20220130
 	LFUN_BIBTEX_DATABASE_LIST,      // bpiwowar, 20221218
+	LFUN_STATISTICS_REFERENCE_CLAMP,// sanda, 20240324
 	LFUN_LASTACTION                 // end of the table
 };
 
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index 0b3d0a25ad..f4030ec40d 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -3925,6 +3925,16 @@ void LyXAction::init()
  * \endvar
  */
 		{ LFUN_STATISTICS, "statistics", ReadOnly, System },
+/*!
+ * \var lyx::FuncCode lyx::LFUN_STATISTICS_REFERENCE_CLAMP
+ * \li Action: Count statistics relative to the current value.
+               In other words all future values will be subtracted by this value.
+ * \li Syntax: statistics-reference-clamp [reset]
+ * \li Params: reset: remove the clamp, i.e. count in the absolute numbers again
+ * \li Origin: sanda, Mar 28 2024
+ * \endvar
+ */
+		{ LFUN_STATISTICS_REFERENCE_CLAMP, "statistics-reference-clamp", ReadOnly, System },
 
 /*!
  * \var lyx::FuncCode lyx::LFUN_TABULAR_FEATURE
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index facdb81b67..ac0606e985 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -1481,7 +1481,7 @@ void GuiView::showStats()
 
 	QStringList stats;
 	if (word_count_enabled_) {
-		int const words = buf->wordCount();
+		int const words = buf->wordCount() - bv->stats_ref_value_w();
 		if (words == 1)
 			stats << toqstr(bformat(_("%1$d Word"), words));
 		else
@@ -1489,13 +1489,14 @@ void GuiView::showStats()
 	}
 	int const chars_with_blanks = buf->charCount(true);
 	if (char_count_enabled_) {
+		int const chars_with_blanks_disp = chars_with_blanks - bv->stats_ref_value_c();
 		if (chars_with_blanks == 1)
-			stats << toqstr(bformat(_("%1$d Character"), chars_with_blanks));
+			stats << toqstr(bformat(_("%1$d Character"), chars_with_blanks_disp));
 		else
-			stats << toqstr(bformat(_("%1$d Characters"), chars_with_blanks));
+			stats << toqstr(bformat(_("%1$d Characters"), chars_with_blanks_disp));
 	}
 	if (char_nb_count_enabled_) {
-		int const chars = buf->charCount(false);
+		int const chars = buf->charCount(false) - bv->stats_ref_value_nb();
 		if (chars == 1)
 			stats << toqstr(bformat(_("%1$d Character (no Blanks)"), chars));
 		else


More information about the lyx-cvs mailing list