From 1774b592204159740c0cc13f6b348367bb5ae7d6 Mon Sep 17 00:00:00 2001
From: RhiobeT <rhiobet@gmail.com>
Date: Wed, 11 Sep 2019 15:55:14 +0200
Subject: [PATCH] Add command to insert space after syllab

---
 src/ass_karaoke.cpp              |  8 ++++++++
 src/ass_karaoke.h                |  5 +++++
 src/audio_timing.h               |  6 ++++++
 src/audio_timing_karaoke.cpp     | 11 +++++++++++
 src/command/time.cpp             | 15 +++++++++++++++
 src/libresrc/default_hotkey.json |  5 ++++-
 6 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/src/ass_karaoke.cpp b/src/ass_karaoke.cpp
index ee47115e7..3e45b6a0c 100644
--- a/src/ass_karaoke.cpp
+++ b/src/ass_karaoke.cpp
@@ -262,3 +262,11 @@ void AssKaraoke::SetLineTimes(int start_time, int end_time) {
 	}
 	syls[idx].duration = end_time - syls[idx].start_time;
 }
+
+std::string AssKaraoke::GetStrippedText(int syl_idx) const {
+	return syls[syl_idx].text;
+}
+
+void AssKaraoke::SetStrippedText(int syl_idx, std::string new_text) {
+	syls[syl_idx].text = new_text;
+}
diff --git a/src/ass_karaoke.h b/src/ass_karaoke.h
index 9802957fa..99cb68e32 100644
--- a/src/ass_karaoke.h
+++ b/src/ass_karaoke.h
@@ -82,5 +82,10 @@ public:
 	/// Set the tag type for all karaoke tags in this line
 	void SetTagType(std::string const& new_type);
 
+	/// Get syllab's text stripped of k tag
+	std::string GetStrippedText(int syl_idx) const;
+	/// Set syllab's text stripped of k tag
+	void SetStrippedText(int syl_idx, std::string new_text);
+
 	DEFINE_SIGNAL_ADDERS(AnnounceSyllablesChanged, AddSyllablesChangedListener)
 };
diff --git a/src/audio_timing.h b/src/audio_timing.h
index 780a6c26a..00d02904a 100644
--- a/src/audio_timing.h
+++ b/src/audio_timing.h
@@ -165,6 +165,12 @@ public:
 	/// controlling the mouse cursor.
 	virtual bool IsNearbyMarker(int ms, int sensitivity, bool alt_down) const = 0;
 
+	/// @brief Return the text of the currently selected syllab
+	virtual std::string GetCurrentSylText() const { return ""; }
+
+	/// @ brief Set the text for the currently selected syllab
+	virtual void SetCurrentSylText(std::string new_text) {}
+
 	/// @brief The user pressed the left mouse button on the audio
 	/// @param ms          The time in milliseconds the user clicked
 	/// @param ctrl_down   Is the user currently holding the ctrl key down?
diff --git a/src/audio_timing_karaoke.cpp b/src/audio_timing_karaoke.cpp
index 788cd8096..932d11250 100644
--- a/src/audio_timing_karaoke.cpp
+++ b/src/audio_timing_karaoke.cpp
@@ -149,6 +149,8 @@ public:
 	std::vector<AudioMarker*> OnLeftClick(int ms, bool, bool, int sensitivity, int) override;
 	std::vector<AudioMarker*> OnRightClick(int ms, bool ctrl_down, int, int) override;
 	void OnMarkerDrag(std::vector<AudioMarker*> const& marker, int new_position, int) override;
+	std::string GetCurrentSylText() const override;
+	void SetCurrentSylText(std::string new_text) override;
 
 	AudioTimingControllerKaraoke(agi::Context *c, AssKaraoke *kara, agi::signal::Connection& file_changed);
 };
@@ -579,3 +581,12 @@ void AudioTimingControllerKaraoke::GetLabels(TimeRange const& range, std::vector
 		return range.overlaps(l.range);
 	}), back_inserter(out));
 }
+
+std::string AudioTimingControllerKaraoke::GetCurrentSylText() const {
+	return kara->GetStrippedText(cur_syl);
+}
+
+void AudioTimingControllerKaraoke::SetCurrentSylText(std::string new_text) {
+	kara->SetStrippedText(cur_syl, new_text);
+	AnnounceChanges(true);
+}
diff --git a/src/command/time.cpp b/src/command/time.cpp
index 84c578980..f123e654f 100644
--- a/src/command/time.cpp
+++ b/src/command/time.cpp
@@ -465,6 +465,20 @@ struct time_opt_tap_to_time final : public Command {
 	}
 };
 
+struct time_add_space final : public Command {
+	CMD_NAME("time/add_space")
+	STR_MENU("Add Space")
+	STR_DISP("Add Space")
+	STR_HELP("Add a space at the end of the current syllab")
+	void operator()(agi::Context *c) override {
+		if (c->audioController->GetTimingController()) {
+			AudioTimingController *tc = c->audioController->GetTimingController();
+			tc->SetCurrentSylText(tc->GetCurrentSylText() + " ");
+			// tc->Next(AudioTimingController::TIMING_UNIT);
+		}
+	}
+};
+
 }
 
 namespace cmd {
@@ -490,5 +504,6 @@ namespace cmd {
 		reg(agi::make_unique<time_snap_start_video>());
 		reg(agi::make_unique<time_start_decrease>());
 		reg(agi::make_unique<time_start_increase>());
+		reg(agi::make_unique<time_add_space>());
 	}
 }
diff --git a/src/libresrc/default_hotkey.json b/src/libresrc/default_hotkey.json
index 0ebf477ed..8ffa90b42 100644
--- a/src/libresrc/default_hotkey.json
+++ b/src/libresrc/default_hotkey.json
@@ -110,6 +110,9 @@
 		"time/prev" : [
 			"A",
 			"Left"
+		],
+		"time/add_space" : [
+			"Ctrl-Space"
 		]
 	},
 	"Default" : {
@@ -226,7 +229,7 @@
 			"Ctrl-3"
 		],
 		"video/focus_seek" : [
-			"Ctrl-Space"
+			"Alt-Space"
 		],
 		"video/frame/next" : [
 			"Ctrl-KP_6"
-- 
GitLab