From 4fb419db0d56010986c2b1b9b2bdba933f21b382 Mon Sep 17 00:00:00 2001 From: rebawest Date: Sat, 8 Mar 2014 18:45:39 -0200 Subject: [PATCH] Issue #16 _mainTextBoxMoveLastWordDown and _mainTextBoxMoveFirstWordFromNextUp Lines changed: from 6994 to 7199 --- src/Forms/Main.cs | 202 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 173 insertions(+), 29 deletions(-) diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 1dc0405e5..58d88d15e 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -6991,7 +6991,7 @@ namespace Nikse.SubtitleEdit.Forms InsertAfter(); e.SuppressKeyPress = true; } - else if (_mainTextBoxMoveLastWordDown == e.KeyData) + else if (_mainTextBoxMoveLastWordDown == e.KeyData) // *** { int firstIndex = FirstSelectedIndex; if (firstIndex >= 0) @@ -7002,27 +7002,92 @@ namespace Nikse.SubtitleEdit.Forms { string s = p.Text.Trim(); int idx = s.LastIndexOf(" "); - if (idx > 0) + if (idx > 0 || s.Length > 0) + // A last word was found or the first subtitle is not empty (has one word). { - string lastWord = s.Substring(idx).Trim(); - p.Text = Utilities.AutoBreakLine(s.Substring(0, idx).Trim()); - next.Text = Utilities.AutoBreakLine(lastWord.Trim() + " " + next.Text.Trim()); - SubtitleListview1.SetText(firstIndex, p.Text); - SubtitleListview1.SetText(firstIndex + 1, next.Text); - } - else if (s.Length > 0) - { - next.Text = Utilities.AutoBreakLine(p.Text.Trim() + " " + next.Text.Trim()); - p.Text = string.Empty; - SubtitleListview1.SetText(firstIndex, p.Text); - SubtitleListview1.SetText(firstIndex + 1, next.Text); + // Undo + MakeHistoryForUndo(_language.BeforeLineUpdatedInListView); + + // Define lastWord. If idx > 0, there is a last word. + // If not, lastWord is the whole text of the first subtitle. + string lastWord = (idx > 0 ? s.Substring(idx).Trim() : p.Text); + + // Remove last word from the first subtitle. + p.Text = (idx > 0 ? Utilities.AutoBreakLine(s.Substring(0, idx).Trim()) : string.Empty); + + // If the first subtitle ends with a tag (): + String endTag = ""; + if (lastWord.EndsWith(">") && lastWord.Contains("<")) + { + // Save the end tag. + endTag = lastWord.Substring(lastWord.LastIndexOf("<"), lastWord.Length - lastWord.LastIndexOf("<")); + // Remove the end tag from the last word. + lastWord = lastWord.Remove(lastWord.LastIndexOf("<")); + } + + // If the first subtitle ends with "...": + Boolean firstSubtitleEndsWithEllipsis = lastWord.EndsWith("..."); + if (firstSubtitleEndsWithEllipsis) + { + // Remove "..." from the last word. + lastWord = lastWord.TrimEnd('.'); + } + + // If the second subtitle (next) starts with a position tag, like {\an8}: + String positionTag = ""; + if (next.Text.StartsWith("{") && next.Text.Contains("}")) + { + // Save the start tag. + positionTag = next.Text.Substring(next.Text.IndexOf("{"), next.Text.IndexOf("}") + 1); + // Remove the position tag from next subtitle. + next.Text = next.Text.Remove(0, next.Text.IndexOf("}") + 1); + } + + // If the second subtitle (next) starts with a tag: + String startTag = ""; + if (next.Text.StartsWith("<") && next.Text.Contains(">")) + { + // Save the start tag. + startTag = next.Text.Substring(next.Text.IndexOf("<"), next.Text.IndexOf(">") + 1); + // Remove the start tag from next subtitle. + next.Text = next.Text.Remove(0, next.Text.IndexOf(">") + 1); + } + + // If the second subtitle (next) starts with a dialogue ("-"): + String dialogueMarker = ""; + if (next.Text.StartsWith("-")) + { + // Save the dialogue marker ("-" or "- "). + dialogueMarker = (next.Text.StartsWith("- ") ? "- " : "-"); + // Remove the dialogue marker from the next subtitle. + next.Text = next.Text.Remove(0, dialogueMarker.Length); + } + + // If the second subtitle starts with "...": + Boolean nextSubtitleStartsWithEllipsis = next.Text.StartsWith("..."); + if (nextSubtitleStartsWithEllipsis) + { + // Remove "..." from the beginning of 'next'. + next.Text = next.Text.TrimStart('.'); + } + + // Add text + "..." + endTag to first subtitle. + if (idx > 0) + p.Text = p.Text + (firstSubtitleEndsWithEllipsis ? "..." : "") + endTag; + + // Add positionTag + startTag + dialogueMarker + "..." + lastWord to 'next'. + next.Text = (idx > 0 ? positionTag : "") + (idx > 0 ? startTag : "") + dialogueMarker + (nextSubtitleStartsWithEllipsis && idx > 0 ? "..." : "") + Utilities.AutoBreakLine(lastWord.Trim() + " " + next.Text.Trim()); } + + SubtitleListview1.SetText(firstIndex, p.Text); + SubtitleListview1.SetText(firstIndex + 1, next.Text); + textBoxListViewText.Text = p.Text; } } e.SuppressKeyPress = true; } - else if (_mainTextBoxMoveFirstWordFromNextUp == e.KeyData) + else if (_mainTextBoxMoveFirstWordFromNextUp == e.KeyData) // *** { int firstIndex = FirstSelectedIndex; if (firstIndex >= 0) @@ -7032,22 +7097,101 @@ namespace Nikse.SubtitleEdit.Forms if (p != null && next != null) { string s = next.Text.Trim(); + // Find the first space. int idx = s.IndexOf(" "); - if (idx > 0) + // If the first space is after a "-", even if there is "{\an8}" before, find the second space. + if(idx > 0 && s.Substring(idx - 1, 2) == "- ") + idx = idx + 1 + s.Substring(idx + 1).IndexOf(" "); + + if (idx > 0 || s.Length > 0) + // A first word was found or next subtitle is not empty (has one word). { - string firstWord = s.Substring(0, idx).Trim(); - p.Text = Utilities.AutoBreakLine(p.Text.Trim() + " " + firstWord); - next.Text = Utilities.AutoBreakLine(s.Substring(idx).Trim()); - SubtitleListview1.SetText(firstIndex, p.Text); - SubtitleListview1.SetText(firstIndex + 1, next.Text); - } - else - { - p.Text = Utilities.AutoBreakLine(p.Text.Trim() + " " + next.Text.Trim()); - next.Text = string.Empty; - SubtitleListview1.SetText(firstIndex, p.Text); - SubtitleListview1.SetText(firstIndex + 1, next.Text); + // Undo + MakeHistoryForUndo(_language.BeforeLineUpdatedInListView); + + // Define firstWord. If idx > 0, there is a first word. + // If not, firstWord is the whole text of the next subtitle. + string firstWord = ( idx > 0 ? s.Substring(0,idx).Trim() : next.Text); + + // Remove first word from the next subtitle. + // If there is only one word, 'next' will be empty. + next.Text = (idx > 0 ? Utilities.AutoBreakLine(s.Substring(idx+1).Trim()) : string.Empty); + + // If the first subtitle ends with a tag (): + String endTag = ""; + if (p.Text.EndsWith(">") && p.Text.Contains("<")) + { + // Save the end tag. + endTag = p.Text.Substring(p.Text.LastIndexOf("<"), p.Text.Length - p.Text.LastIndexOf("<")); + // Remove the endTag from first subtitle. + p.Text = p.Text.Remove(p.Text.LastIndexOf("<")); + } + + // If the first subtitle ends with "...": + Boolean firstSubtitleEndsWithEllipsis = p.Text.EndsWith("..."); + if (firstSubtitleEndsWithEllipsis) + { + // Remove "..." from first subtitle. + p.Text = p.Text.TrimEnd('.'); + } + + // If the second subtitle (next) starts with a position tag, like {\an8}: + String positionTag = ""; + if (firstWord.StartsWith("{") && firstWord.Contains("}")) + { + // Save the start tag. + positionTag = firstWord.Substring(firstWord.IndexOf("{"), firstWord.IndexOf("}") + 1); + // Remove the position tag from the first word. + firstWord = firstWord.Remove(0, firstWord.IndexOf("}") + 1); + } + + // If the second subtitle (next) starts with a tag: + String startTag = ""; + if (firstWord.StartsWith("<") && firstWord.Contains(">")) + { + // Save the start tag. + startTag = firstWord.Substring(firstWord.IndexOf("<"), firstWord.IndexOf(">") + 1); + // Remove the start tag from the first word. + firstWord = firstWord.Remove(0, firstWord.IndexOf(">") + 1); + } + + // If the second subtitle ends with a tag and there's only one word in it: + if (next.Text.EndsWith(">") && next.Text.Contains("<") && next.Text.IndexOf(" ") < 0) + { + // Remove the end tag. + next.Text = next.Text.Remove(next.Text.LastIndexOf("<")); + } + + // If the second subtitle (next) starts with a dialogue ("-"): + String dialogueMarker = ""; + if (firstWord.StartsWith("-")) + { + // Save the dialogue marker ("-" or "- "). + dialogueMarker = (firstWord.StartsWith("- ") ? "- " : "-"); + // Remove the dialogue marker from the first word. + firstWord = firstWord.Remove(0, dialogueMarker.Length); + } + + // If the second subtitle starts with "...": + Boolean nextSubtitleStartsWithEllipsis = firstWord.StartsWith("..."); + if (nextSubtitleStartsWithEllipsis) + { + // Remove "..." from the beginning of first word. + firstWord = firstWord.TrimStart('.'); + } + + // Add positionTag + startTag + dialogueMarker + "..." + text to 'next'. + if (idx > 0) + next.Text = positionTag + startTag + dialogueMarker + (nextSubtitleStartsWithEllipsis ? "..." : "") + Utilities.AutoBreakLine(next.Text.Trim()); + + // Add text + firstWord + "..." + endTag to First line. + p.Text = (idx == 0 ? startTag : "") + Utilities.AutoBreakLine(p.Text.Trim() + " " + firstWord.Trim()) + (idx > 0 && firstSubtitleEndsWithEllipsis ? "..." : "") + endTag; + } + + SubtitleListview1.SetText(firstIndex, p.Text); + SubtitleListview1.SetText(firstIndex + 1, next.Text); + textBoxListViewText.Text = p.Text; } } @@ -18852,4 +18996,4 @@ namespace Nikse.SubtitleEdit.Forms } } -} \ No newline at end of file +}