branch: elpa/subed commit 06a4681303c0941d9c24819f3c455fe93d8f7e6f Author: Random User <rnd...@posteo.de> Commit: Random User <rnd...@posteo.de>
Adjusting functions return milliseconds moved --- subed/subed-srt.el | 20 ++++++++-- tests/test-subed-srt.el | 97 ++++++++++++++++++++++--------------------------- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/subed/subed-srt.el b/subed/subed-srt.el index 1a703df..fa15d56 100644 --- a/subed/subed-srt.el +++ b/subed/subed-srt.el @@ -298,7 +298,13 @@ Return point or nil if there is no previous subtitle." (defun subed-srt--adjust-subtitle-start (msecs) "Add MSECS milliseconds to start time (use negative value to subtract). -Return new start time in milliseconds or nil if it didn't change." + +If the adjustment would result in overlapping subtitles, reduce +MSECS so that there are at least `subed-subtitle-spacing' +milliseconds between subtitles. + +Return the number of milliseconds the start time was adjusted or +nil if nothing was adjusted." (subed-disable-sync-point-to-player-temporarily) (let* ((msecs-start (subed-srt--subtitle-msecs-start)) (msecs-new (when msecs-start (+ msecs-start msecs))) @@ -321,11 +327,17 @@ Return new start time in milliseconds or nil if it didn't change." (when (looking-at subed-srt--regexp-timestamp) (replace-match (subed-srt--msecs-to-timestamp msecs-new)) (subed--run-subtitle-time-adjusted-hook) - msecs-new)))))) + (- msecs-new msecs-start))))))) (defun subed-srt--adjust-subtitle-stop (msecs) "Add MSECS milliseconds to stop time (use negative value to subtract). -Return new stop time in milliseconds or nil if it didn't change." + +If the adjustment would result in overlapping subtitles, reduce +MSECS so that there are at least `subed-subtitle-spacing' +milliseconds between subtitles. + +Return the number of milliseconds the stop time was adjusted or +nil if nothing was adjusted." (subed-disable-sync-point-to-player-temporarily) (let* ((msecs-stop (subed-srt--subtitle-msecs-stop)) (msecs-new (when msecs-stop (+ msecs-stop msecs))) @@ -348,7 +360,7 @@ Return new stop time in milliseconds or nil if it didn't change." (when (looking-at subed-srt--regexp-timestamp) (replace-match (subed-srt--msecs-to-timestamp msecs-new)) (subed--run-subtitle-time-adjusted-hook) - msecs-new)))))) + (- msecs-new msecs-stop))))))) (defun subed-srt-subtitle-insert (&optional arg) "Insert subtitle(s). diff --git a/tests/test-subed-srt.el b/tests/test-subed-srt.el index 2433f08..97bc32d 100644 --- a/tests/test-subed-srt.el +++ b/tests/test-subed-srt.el @@ -611,71 +611,71 @@ Baz. (add-hook 'subed-subtitle-time-adjusted-hook 'foo) (with-temp-buffer (insert mock-srt-data) - (expect (subed-increase-start-time) :to-equal 183550) + (expect (subed-increase-start-time) :to-equal 100) (expect 'foo :to-have-been-called-with 3 183550) (expect 'foo :to-have-been-called-times 1) (subed-srt-jump-to-subtitle-id 1) - (expect (subed-increase-stop-time) :to-equal 65223) + (expect (subed-increase-stop-time) :to-equal 100) (expect 'foo :to-have-been-called-with 1 61000) (expect 'foo :to-have-been-called-times 2) (subed-srt-jump-to-subtitle-end 2) - (expect (subed-decrease-start-time) :to-equal 122134) + (expect (subed-decrease-start-time) :to-equal -100) (expect 'foo :to-have-been-called-with 2 122134) (expect 'foo :to-have-been-called-times 3) (subed-srt-jump-to-subtitle-text 3) - (expect (subed-decrease-stop-time) :to-equal 195400) + (expect (subed-decrease-stop-time) :to-equal -100) (expect 'foo :to-have-been-called-with 3 183550) (expect 'foo :to-have-been-called-times 4)))) (it "adjusts the start/stop time." (with-temp-buffer (insert mock-srt-data) (subed-srt-jump-to-subtitle-id 1) - (expect (subed-increase-start-time) :to-equal 61100) + (expect (subed-increase-start-time) :to-equal 100) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:01,100 --> 00:01:05,123\n") - (expect (subed-decrease-start-time) :to-equal 61000) - (expect (subed-decrease-start-time) :to-equal 60900) + (expect (subed-decrease-start-time) :to-equal -100) + (expect (subed-decrease-start-time) :to-equal -100) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:00,900 --> 00:01:05,123\n") - (expect (subed-increase-stop-time) :to-equal 65223) - (expect (subed-increase-stop-time) :to-equal 65323) + (expect (subed-increase-stop-time) :to-equal 100) + (expect (subed-increase-stop-time) :to-equal 100) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:00,900 --> 00:01:05,323\n") - (expect (subed-decrease-stop-time) :to-equal 65223) + (expect (subed-decrease-stop-time) :to-equal -100) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:00,900 --> 00:01:05,223\n"))) (it "adjusts the start/stop time if milliseconds lack digits." (with-temp-buffer (insert mock-srt-data) (subed-srt-jump-to-subtitle-id 3) - (expect (subed-increase-start-time 200) :to-equal 183650) - (expect (subed-decrease-start-time 100) :to-equal 183550) + (expect (subed-increase-start-time 200) :to-equal 200) + (expect (subed-decrease-start-time 100) :to-equal -100) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:03:03,550 --> 00:03:15,5\n") - (expect (subed-increase-stop-time 100) :to-equal 195600) - (expect (subed-decrease-stop-time 200) :to-equal 195400) + (expect (subed-increase-stop-time 100) :to-equal 100) + (expect (subed-decrease-stop-time 200) :to-equal -200) (expect (save-excursion (subed-srt-jump-to-subtitle-time-stop) (thing-at-point 'line)) :to-equal "00:03:03,550 --> 00:03:15,400\n"))) - (it "sets the number of seconds if given an argument." + (it "sets the number of milliseconds if given an argument." (with-temp-buffer (insert mock-srt-data) (subed-srt-jump-to-subtitle-id 1) - (expect (subed-increase-start-time 200) :to-equal 61200) - (expect (subed-increase-start-time) :to-equal 61400) - (expect (subed-increase-start-time) :to-equal 61600) + (expect (subed-increase-start-time 200) :to-equal 200) + (expect (subed-increase-start-time) :to-equal 200) + (expect (subed-increase-start-time) :to-equal 200) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:01,600 --> 00:01:05,123\n") - (expect (subed-decrease-start-time 50) :to-be 61550) - (expect (subed-decrease-start-time) :to-be 61500) + (expect (subed-decrease-start-time 50) :to-be -50) + (expect (subed-decrease-start-time) :to-be -50) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:01,500 --> 00:01:05,123\n") - (expect (subed-decrease-stop-time 1000) :to-be 64123) - (expect (subed-decrease-stop-time) :to-be 63123) - (expect (subed-decrease-stop-time) :to-be 62123) + (expect (subed-decrease-stop-time 1000) :to-be -1000) + (expect (subed-decrease-stop-time) :to-be -1000) + (expect (subed-decrease-stop-time) :to-be -1000) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:01,500 --> 00:01:02,123\n") - (expect (subed-increase-stop-time 2000) :to-be 64123) - (expect (subed-increase-stop-time) :to-be 66123) + (expect (subed-increase-stop-time 2000) :to-be 2000) + (expect (subed-increase-stop-time) :to-be 2000) (expect (save-excursion (subed-srt-jump-to-subtitle-time-start) (thing-at-point 'line)) :to-equal "00:01:01,500 --> 00:01:06,123\n"))) (describe "enforces limits" @@ -684,9 +684,9 @@ Baz. (with-temp-buffer (insert mock-srt-data) (subed-srt-jump-to-subtitle-id 1) - (expect (subed-decrease-start-time 60999) :to-be 1) + (expect (subed-decrease-start-time 60999) :to-be -60999) (expect (subed-srt--subtitle-msecs-start) :to-be 1) - (expect (subed-decrease-start-time 1) :to-be 0) + (expect (subed-decrease-start-time 1) :to-be -1) (expect (subed-srt--subtitle-msecs-start) :to-be 0) (expect (subed-decrease-start-time 1) :to-be nil) (expect (subed-srt--subtitle-msecs-start) :to-be 0))) @@ -694,17 +694,15 @@ Baz. (with-temp-buffer (insert mock-srt-data) (subed-srt-jump-to-subtitle-id 2) + (message (buffer-string)) (expect (subed-decrease-start-time (- (subed-srt--subtitle-msecs-start 2) (subed-srt--subtitle-msecs-stop 1) subed-subtitle-spacing - 1)) :to-be (+ (subed-srt--subtitle-msecs-stop 1) - subed-subtitle-spacing - 1)) + 1)) :to-be -57010) (expect (subed-srt--subtitle-msecs-start) :to-be (+ (subed-srt--subtitle-msecs-stop 1) subed-subtitle-spacing 1)) - (expect (subed-decrease-start-time 1) :to-be (+ (subed-srt--subtitle-msecs-stop 1) - subed-subtitle-spacing)) + (expect (subed-decrease-start-time 1) :to-be -1) (expect (subed-srt--subtitle-msecs-start) :to-be (+ (subed-srt--subtitle-msecs-stop 1) subed-subtitle-spacing)) (expect (subed-decrease-start-time 1) :to-be nil) @@ -717,9 +715,9 @@ Baz. (subed-srt-jump-to-subtitle-id 2) (expect (subed-increase-start-time (- (subed-srt--subtitle-msecs-stop 2) (subed-srt--subtitle-msecs-start 2) - 1)) :to-be (- (subed-srt--subtitle-msecs-stop 2) 1)) + 1)) :to-be 8110) (expect (subed-srt--subtitle-msecs-start 2) :to-be (- (subed-srt--subtitle-msecs-stop 2) 1)) - (expect (subed-increase-start-time 1) :to-be (subed-srt--subtitle-msecs-stop 2)) + (expect (subed-increase-start-time 1) :to-be 1) (expect (subed-srt--subtitle-msecs-start 2) :to-be (subed-srt--subtitle-msecs-stop 2)) (expect (subed-increase-start-time 1) :to-be nil) (expect (subed-srt--subtitle-msecs-start 2) :to-be (subed-srt--subtitle-msecs-stop 2)))) @@ -727,13 +725,13 @@ Baz. (with-temp-buffer (insert mock-srt-data) (subed-srt-jump-to-subtitle-id 2) - (subed-decrease-stop-time (- (subed-srt--subtitle-msecs-stop 2) - (subed-srt--subtitle-msecs-start 2) - 1)) + (expect (subed-decrease-stop-time (- (subed-srt--subtitle-msecs-stop 2) + (subed-srt--subtitle-msecs-start 2) + 1)) :to-be -8110) (expect (subed-srt--subtitle-msecs-stop 2) :to-be (+ (subed-srt--subtitle-msecs-start 2) 1)) - (subed-decrease-stop-time 1) + (expect (subed-decrease-stop-time 1) :to-be -1) (expect (subed-srt--subtitle-msecs-stop 2) :to-be (subed-srt--subtitle-msecs-start 2)) - (subed-decrease-stop-time 1) + (expect (subed-decrease-stop-time 1) :to-be nil) (expect (subed-srt--subtitle-msecs-stop 2) :to-be (subed-srt--subtitle-msecs-start 2)))) (describe "when increasing stop time" (it "of the last subtitle." @@ -742,22 +740,16 @@ Baz. (subed-srt-jump-to-subtitle-id 3) (expect (subed-increase-stop-time (- (* 99 3600000) (subed-srt--subtitle-msecs-stop 3))) - :to-be (* 99 3600000)) + :to-be 356204500) (expect (subed-srt--subtitle-msecs-stop 3) :to-be (* 99 3600000)) - (expect (subed-increase-stop-time (* 59 60000)) :to-be (+ (* 99 3600000) - (* 59 60000))) + (expect (subed-increase-stop-time (* 59 60000)) :to-be (* 59 60000)) (expect (subed-srt--subtitle-msecs-stop 3) :to-be (+ (* 99 3600000) (* 59 60000))) - (expect (subed-increase-stop-time (* 59 1000)) :to-be (+ (* 99 3600000) - (* 59 60000) - (* 59 1000))) + (expect (subed-increase-stop-time (* 59 1000)) :to-be (* 59 1000)) (expect (subed-srt--subtitle-msecs-stop 3) :to-be (+ (* 99 3600000) (* 59 60000) (* 59 1000))) - (expect (subed-increase-stop-time 999) :to-be (+ (* 99 3600000) - (* 59 60000) - (* 59 1000) - 999)) + (expect (subed-increase-stop-time 999) :to-be 999) (expect (subed-srt--subtitle-msecs-stop 3) :to-be (+ (* 99 3600000) (* 59 60000) (* 59 1000) @@ -769,14 +761,11 @@ Baz. (expect (subed-increase-stop-time (- (subed-srt--subtitle-msecs-start 3) (subed-srt--subtitle-msecs-stop 2) subed-subtitle-spacing - 1)) :to-be (- (subed-srt--subtitle-msecs-start 3) - subed-subtitle-spacing - 1)) + 1)) :to-be 53004) (expect (subed-srt--subtitle-msecs-stop 2) :to-be (- (subed-srt--subtitle-msecs-start 3) subed-subtitle-spacing 1)) - (expect (subed-increase-stop-time 1) :to-be (- (subed-srt--subtitle-msecs-start 3) - subed-subtitle-spacing)) + (expect (subed-increase-stop-time 1) :to-be 1) (expect (subed-srt--subtitle-msecs-stop 2) :to-be (- (subed-srt--subtitle-msecs-start 3) subed-subtitle-spacing)) (expect (subed-increase-stop-time 1) :to-be nil)