emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/subed f8ce6d4e6c 1/5: Enforce time boundaries when setting


From: ELPA Syncer
Subject: [nongnu] elpa/subed f8ce6d4e6c 1/5: Enforce time boundaries when setting start or stop time
Date: Tue, 7 Mar 2023 19:01:15 -0500 (EST)

branch: elpa/subed
commit f8ce6d4e6cbe50e7f26c6825e2346f4bff7c8f17
Author: Sacha Chua <sacha@sachachua.com>
Commit: Sacha Chua <sacha@sachachua.com>

    Enforce time boundaries when setting start or stop time
    
    * subed/subed-common.el (set-subtitle-time-start): Add
    ignore-negative-duration argument. Throw an error if duration will end
    up negative,ignore-negative-duration is nil, and
    subed-enforce-time-boundaries is non-nil.
    (set-subtitle-time-stop): Add
    ignore-negative-duration argument. Throw an error if duration will end
    up negative,ignore-negative-duration is nil, and
    subed-enforce-time-boundaries is non-nil.
    (subed-adjust-subtitle-time-start): Fix documentation. Pass
    ignore-negative-duration down to subed-set-subtitle-time-start.
    (subed-adjust-subtitle-time-stop): Fix documentation. Pass
    ignore-negative-duration down to subed-set-subtitle-time-stop.
    * tests/test-subed-common.el ("COMMON"): Add tests for setting
    subtitle start time and stop time.
    * tests/test-subed-srt.el: Ignore time boundaries in older tests.
    * tests/test-subed-vtt.el: Ignore time boundaries in older tests.
---
 subed/subed-common.el      | 51 ++++++++++++++++++++++++++++++++++------------
 tests/test-subed-common.el | 41 ++++++++++++++++++++++++++++++-------
 tests/test-subed-srt.el    | 12 +++++------
 tests/test-subed-vtt.el    | 20 +++++++++---------
 4 files changed, 88 insertions(+), 36 deletions(-)

diff --git a/subed/subed-common.el b/subed/subed-common.el
index 83d296c131..8b0f6ea9ab 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -347,31 +347,56 @@ If SUB-ID is not given, set the text of the current 
subtitle."
   "Return the comment preceding this subtitle."
   nil)
 
-(subed-define-generic-function set-subtitle-time-start (msecs &optional sub-id)
+(subed-define-generic-function set-subtitle-time-start (msecs
+                                                                               
                                                                                
                                                                &optional sub-id
+                                                                               
                                                                                
                                                                
ignore-negative-duration)
   "Set subtitle start time to MSECS milliseconds.
 
 If SUB-ID is not given, set the start of the current subtitle.
 
+Unless either IGNORE-NEGATIVE-DURATION is non-nil or
+`subed-enforce-time-boundaries' is nil, throw an error if
+the start time will be after the stop time.  Zero-length
+subtitles are allowed.
+
 Return the new subtitle start time in milliseconds."
   (save-excursion
     (when (or (not sub-id)
               (and sub-id (subed-jump-to-subtitle-id sub-id)))
+                       (when (and subed-enforce-time-boundaries
+                                                                (not 
ignore-negative-duration)
+                                                                (> msecs 
(subed-subtitle-msecs-stop)))
+                               (error "Start time %d will be after stop time 
%d" msecs (subed-subtitle-msecs-stop)))
       (when (and (subed-jump-to-subtitle-time-start sub-id)
                  (looking-at subed--regexp-timestamp))
         (replace-match
          (save-match-data (subed-msecs-to-timestamp msecs)))))))
 
-(subed-define-generic-function set-subtitle-time-stop (msecs &optional sub-id)
+(subed-define-generic-function set-subtitle-time-stop (msecs &optional sub-id
+                                                                               
                                                                                
                                                                                
 ignore-negative-duration)
   "Set subtitle stop time to MSECS milliseconds.
 
 If SUB-ID is not given, set the stop of the current subtitle.
 
+Unless either IGNORE-NEGATIVE-DURATION is non-nil or
+`subed-enforce-time-boundaries' is nil, throw an error if
+the start time will be after the stop time.  Zero-length
+subtitles are allowed.
+
 Return the new subtitle stop time in milliseconds."
   (save-excursion
-    (when (and (subed-jump-to-subtitle-time-stop sub-id)
-               (looking-at subed--regexp-timestamp))
-      (replace-match
-       (save-match-data (subed-msecs-to-timestamp msecs))))))
+               (when (or (not sub-id)
+              (and sub-id (subed-jump-to-subtitle-id sub-id)))
+                       (when (subed-jump-to-subtitle-time-stop sub-id)
+                               (when (and subed-enforce-time-boundaries
+                                                                        (not 
ignore-negative-duration)
+                                                                        (< 
msecs (subed-subtitle-msecs-start)))
+                                       (error "Stop time %d will be before 
start time %d" msecs (subed-subtitle-msecs-start))))
+                       (when (and
+                                                
(subed-jump-to-subtitle-time-stop)
+                                                (looking-at 
subed--regexp-timestamp))
+                               (replace-match
+                                (save-match-data (subed-msecs-to-timestamp 
msecs)))))))
 
 (subed-define-generic-function make-subtitle (&optional id start stop text 
comment)
   "Generate new subtitle string.
@@ -583,13 +608,13 @@ scheduled call is canceled and another call is scheduled 
in
                                                ignore-overlap)
   "Add MSECS milliseconds to start time (use negative value to subtract).
 
-Unless either IGNORE-NEGATIVE-DURATION or
-`subed-enforce-time-boundaries' are non-nil, adjust MSECS so that
-the stop time isn't smaller than the start time.  Zero-length
+Unless either IGNORE-NEGATIVE-DURATION is non-nil or
+`subed-enforce-time-boundaries' is nil, adjust MSECS so that the
+stop time isn't smaller than the start time.  Zero-length
 subtitles are always allowed.
 
-Unless either IGNORE-OVERLAP or `subed-enforce-time-boundaries'
-are non-nil, ensure that there are no gaps between subtitles
+Unless either IGNORE-OVERLAP is non-nilor `subed-enforce-time-boundaries'
+is nil, ensure that there are no gaps between subtitles
 smaller than `subed-subtitle-spacing' milliseconds by adjusting
 MSECS if necessary.
 
@@ -619,7 +644,7 @@ nil if nothing changed."
       (when (and (>= msecs-new 0)                                  ;; Ignore 
negative times
                  (or (and (> msecs 0) (> msecs-new msecs-start))   ;; Adding
                      (and (< msecs 0) (< msecs-new msecs-start)))) ;; 
Subtracting
-        (subed-set-subtitle-time-start msecs-new)
+        (subed-set-subtitle-time-start msecs-new nil ignore-negative-duration)
         (subed--run-subtitle-time-adjusted-hook)
         (- msecs-new msecs-start)))))
 
@@ -664,7 +689,7 @@ nil if nothing changed."
       (when (and (>= msecs-new 0)                                  ;; Ignore 
negative times
                  (or (and (> msecs 0) (> msecs-new msecs-stop))    ;; Adding
                      (and (< msecs 0) (< msecs-new msecs-stop))))  ;; 
Subtracting
-        (subed-set-subtitle-time-stop msecs-new)
+        (subed-set-subtitle-time-stop msecs-new nil ignore-negative-duration)
         (subed--run-subtitle-time-adjusted-hook)
         (- msecs-new msecs-stop)))))
 
diff --git a/tests/test-subed-common.el b/tests/test-subed-common.el
index 0a78f10329..1ccb3c00de 100644
--- a/tests/test-subed-common.el
+++ b/tests/test-subed-common.el
@@ -161,6 +161,30 @@ Baz.
         )
       )
     )
+       (describe "Setting subtitle start time"
+               (describe "when it will result in invalid duration"
+                       :var ((temp-time (+ (* 3 60 1000) (* 17 1000))))
+                       (it "throws an error when enforcing time boundaries."
+                               (let ((subed-enforce-time-boundaries t))
+                                       (with-temp-srt-buffer
+                                        (insert mock-srt-data)
+                                        (subed-jump-to-subtitle-id 3)
+                                        (expect (subed-set-subtitle-time-start 
temp-time)
+                                                                        
:to-throw 'error))))
+                       (it "changes it when ignoring time boundaries."
+                               (let ((subed-enforce-time-boundaries nil))
+                                       (with-temp-srt-buffer
+                                        (insert mock-srt-data)
+                                        (subed-jump-to-subtitle-id 3)
+                                        (subed-set-subtitle-time-start 
temp-time)
+                                        (expect (subed-subtitle-msecs-start) 
:to-equal temp-time))))
+                       (it "changes it when negative durations are allowed."
+                               (let ((subed-enforce-time-boundaries t))
+                                       (with-temp-srt-buffer
+                                        (insert mock-srt-data)
+                                        (subed-jump-to-subtitle-id 3)
+                                        (subed-set-subtitle-time-start 
temp-time nil t)
+                                        (expect (subed-subtitle-msecs-start) 
:to-equal temp-time))))))
 
   (describe "Adjusting subtitle start/stop time"
     :var (subed-subtitle-time-adjusted-hook)
@@ -310,7 +334,7 @@ Baz.
          (expect (subed-adjust-subtitle-time-start -1) :to-be nil)
          (expect (subed-subtitle-msecs-start) :to-equal 2000)))
       )
-    (describe "ignores negative duration if the first argument is truthy"
+    (describe "ignores negative duration if the second argument is truthy"
       (it "when adjusting start time."
         (with-temp-srt-buffer
          (insert (concat "1\n"
@@ -482,12 +506,14 @@ Baz.
        (insert (concat "1\n"
                        "00:00:01,000 --> 00:00:02,000\n"
                        "Foo.\n"))
-       (let ((subed-mpv-playback-position (+ 60000 2000 123)))
+       (let ((subed-mpv-playback-position (+ 60000 2000 123))
+                                                (subed-enforce-time-boundaries 
nil))
          (expect (subed-copy-player-pos-to-start-time) :to-be 
subed-mpv-playback-position)
          (expect (buffer-string) :to-equal (concat "1\n"
                                                    "00:01:02,123 --> 
00:00:02,000\n"
                                                    "Foo.\n")))
-       (let ((subed-mpv-playback-position (+ 60000 5000 456)))
+       (let ((subed-mpv-playback-position (+ 60000 5000 456))
+                                                (subed-enforce-time-boundaries 
nil))
          (expect (subed-copy-player-pos-to-stop-time) :to-be 
subed-mpv-playback-position)
          (expect (buffer-string) :to-equal (concat "1\n"
                                                    "00:01:02,123 --> 
00:01:05,456\n"
@@ -497,7 +523,8 @@ Baz.
        (insert (concat "1\n"
                        "00:00:01,000 --> 00:00:02,000\n"
                        "Foo.\n"))
-       (let ((foo (setf (symbol-function 'foo) (lambda (msecs) ()))))
+       (let ((foo (setf (symbol-function 'foo) (lambda (msecs) ())))
+                                                (subed-enforce-time-boundaries 
nil))
          (spy-on 'foo)
          (add-hook 'subed-subtitle-time-adjusted-hook 'foo)
          (let ((subed-mpv-playback-position (+ 60000 2000 123)))
@@ -2331,10 +2358,10 @@ This is another.
            (insert text)
            (re-search-backward "subtitle")
            (end-of-line)
-           (subed-split-subtitle "00:01:03,100")
-           (expect (subed-subtitle-msecs-start) :to-equal 63100)
+           (subed-split-subtitle "00:01:43,100")
+           (expect (subed-subtitle-msecs-start) :to-equal 103100)
            (subed-backward-subtitle-time-start)
-           (expect (subed-subtitle-msecs-stop) :to-equal (- 63100 
subed-subtitle-spacing))))))
+           (expect (subed-subtitle-msecs-stop) :to-equal (- 103100 
subed-subtitle-spacing))))))
     (describe "when playing the media in MPV"
       (it "splits at point in the middle of the subtitle."
         (with-temp-srt-buffer
diff --git a/tests/test-subed-srt.el b/tests/test-subed-srt.el
index 57f53b5ebe..b28e616958 100644
--- a/tests/test-subed-srt.el
+++ b/tests/test-subed-srt.el
@@ -624,7 +624,7 @@ Baz.
       (with-temp-srt-buffer
        (insert mock-srt-data)
        (subed-jump-to-subtitle-end 2)
-       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 400))
+       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 400) nil t)
        (expect (buffer-string) :to-equal (concat "1\n"
                                                  "00:01:01,000 --> 
00:01:05,123\n"
                                                  "Foo.\n\n"
@@ -634,7 +634,7 @@ Baz.
                                                  "3\n"
                                                  "00:03:03,45 --> 00:03:15,5\n"
                                                  "Baz.\n"))
-       (subed-set-subtitle-time-stop (+ (* 5 60 60 1000) (* 6 60 1000) (* 7 
1000) 800))
+       (subed-set-subtitle-time-stop (+ (* 5 60 60 1000) (* 6 60 1000) (* 7 
1000) 800) nil t)
        (expect (buffer-string) :to-equal (concat "1\n"
                                                  "00:01:01,000 --> 
00:01:05,123\n"
                                                  "Foo.\n\n"
@@ -648,7 +648,7 @@ Baz.
       (with-temp-srt-buffer
        (insert mock-srt-data)
        (subed-jump-to-subtitle-time-stop 3)
-       (subed-set-subtitle-time-start (+ (* 2 60 60 1000) (* 4 60 1000) (* 6 
1000) 800) 1)
+       (subed-set-subtitle-time-start (+ (* 2 60 60 1000) (* 4 60 1000) (* 6 
1000) 800) 1 t)
        (expect (buffer-string) :to-equal (concat "1\n"
                                                  "02:04:06,800 --> 
00:01:05,123\n"
                                                  "Foo.\n\n"
@@ -659,7 +659,7 @@ Baz.
                                                  "00:03:03,45 --> 00:03:15,5\n"
                                                  "Baz.\n"))
        (subed-jump-to-subtitle-text 1)
-       (subed-set-subtitle-time-stop (+ (* 3 60 60 1000) (* 5 60 1000) (* 7 
1000) 900) 3)
+       (subed-set-subtitle-time-stop (+ (* 3 60 60 1000) (* 5 60 1000) (* 7 
1000) 900) 3 t)
        (expect (buffer-string) :to-equal (concat "1\n"
                                                  "02:04:06,800 --> 
00:01:05,123\n"
                                                  "Foo.\n\n"
@@ -673,10 +673,10 @@ Baz.
       (with-temp-srt-buffer
        (insert mock-srt-data)
        (subed-jump-to-subtitle-id 3)
-       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 4) 3)
+       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 4) 3 t)
        (expect (save-excursion (subed-jump-to-subtitle-time-start)
                                (thing-at-point 'line)) :to-equal "01:02:03,004 
--> 00:03:15,5\n")
-       (subed-set-subtitle-time-stop (+ (* 2 60 60 1000) (* 3 60 1000) (* 4 
1000) 60) 3)
+       (subed-set-subtitle-time-stop (+ (* 2 60 60 1000) (* 3 60 1000) (* 4 
1000) 60) 3 t)
        (expect (save-excursion (subed-jump-to-subtitle-time-start)
                                (thing-at-point 'line)) :to-equal "01:02:03,004 
--> 02:03:04,060\n")))
     )
diff --git a/tests/test-subed-vtt.el b/tests/test-subed-vtt.el
index da1698d9c3..82b827eec4 100644
--- a/tests/test-subed-vtt.el
+++ b/tests/test-subed-vtt.el
@@ -1,4 +1,4 @@
-;; -*- eval: (buttercup-minor-mode) -*-
+;; -*- eval: (buttercup-minor-mode); lexical-binding: t -*-
 
 (load-file "./tests/undercover-init.el")
 (require 'subed)
@@ -575,11 +575,11 @@ Baz.
     )
 
   (describe "Setting start/stop time"
-    (it "of current subtitle."
+    (it "of current subtitle updates it."
       (with-temp-vtt-buffer
        (insert mock-vtt-data)
        (subed-jump-to-subtitle-end "00:02:02.234")
-       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 400))
+       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 400) nil t)
        (expect (buffer-string) :to-equal (concat "WEBVTT\n\n"
                                                  "00:01:01.000 --> 
00:01:05.123\n"
                                                  "Foo.\n\n"
@@ -587,7 +587,7 @@ Baz.
                                                  "Bar.\n\n"
                                                  "00:03:03.45 --> 00:03:15.5\n"
                                                  "Baz.\n"))
-       (subed-set-subtitle-time-stop (+ (* 5 60 60 1000) (* 6 60 1000) (* 7 
1000) 800))
+       (subed-set-subtitle-time-stop (+ (* 5 60 60 1000) (* 6 60 1000) (* 7 
1000) 800) nil t)
        (expect (buffer-string) :to-equal (concat "WEBVTT\n\n"
                                                  "00:01:01.000 --> 
00:01:05.123\n"
                                                  "Foo.\n\n"
@@ -595,11 +595,11 @@ Baz.
                                                  "Bar.\n\n"
                                                  "00:03:03.45 --> 00:03:15.5\n"
                                                  "Baz.\n"))))
-    (it "of specific subtitle."
+    (it "of specific subtitle updates it."
       (with-temp-vtt-buffer
        (insert mock-vtt-data)
        (subed-jump-to-subtitle-time-stop "00:01:01.000")
-       (subed-set-subtitle-time-start (+ (* 2 60 60 1000) (* 4 60 1000) (* 6 
1000) 800) 1)
+       (subed-set-subtitle-time-start (+ (* 2 60 60 1000) (* 4 60 1000) (* 6 
1000) 800) 1 t)
        (expect (buffer-string) :to-equal (concat "WEBVTT\n\n"
                                                  "02:04:06.800 --> 
00:01:05.123\n"
                                                  "Foo.\n\n"
@@ -608,7 +608,7 @@ Baz.
                                                  "00:03:03.45 --> 00:03:15.5\n"
                                                  "Baz.\n"))
        (subed-jump-to-subtitle-text "00:03:03.45")
-       (subed-set-subtitle-time-stop (+ (* 3 60 60 1000) (* 5 60 1000) (* 7 
1000) 900) 3)
+       (subed-set-subtitle-time-stop (+ (* 3 60 60 1000) (* 5 60 1000) (* 7 
1000) 900) 3 t)
        (expect (buffer-string) :to-equal (concat "WEBVTT\n\n"
                                                  "02:04:06.800 --> 
00:01:05.123\n"
                                                  "Foo.\n\n"
@@ -616,15 +616,15 @@ Baz.
                                                  "Bar.\n\n"
                                                  "00:03:03.45 --> 
03:05:07.900\n"
                                                  "Baz.\n"))))
-    (it "when milliseconds lack digits."
+    (it "when milliseconds lack digits, fills the rest in."
       (with-temp-vtt-buffer
        (insert mock-vtt-data)
        (subed-jump-to-subtitle-id "00:03:03.45")
-       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 4) 3)
+       (subed-set-subtitle-time-start (+ (* 1 60 60 1000) (* 2 60 1000) (* 3 
1000) 4) 3 t)
        (expect (save-excursion (subed-jump-to-subtitle-time-start)
                                (thing-at-point 'line))
                :to-equal "01:02:03.004 --> 00:03:15.5\n")
-       (subed-set-subtitle-time-stop (+ (* 2 60 60 1000) (* 3 60 1000) (* 4 
1000) 60) 3)
+       (subed-set-subtitle-time-stop (+ (* 2 60 60 1000) (* 3 60 1000) (* 4 
1000) 60) 3 t)
        (expect (save-excursion (subed-jump-to-subtitle-time-start)
                                (thing-at-point 'line))
                :to-equal "01:02:03.004 --> 02:03:04.060\n"))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]