branch: elpa/subed
commit b1ad383ea83e3a8c78aaf309f8d8781ecfdf0c02
Author: Sacha Chua <[email protected]>
Commit: Sacha Chua <[email protected]>

    subed-vtt: iterating shouldn't get confused by blank lines at end
    
    * subed/subed-common.el (subed-for-each-subtitle): Scan backward for
    subtitle ID.
    * subed/subed-vtt.el (subed--backward-subtitle-id): Don't get confused
    by blank lines at the end of the buffer.
    * tests/test-subed-vtt.el ("VTT"): Add test that handles blank lines
    at the end of the buffer.
---
 subed/subed-common.el   | 26 ++++++++++++++------------
 subed/subed-vtt.el      | 14 +++++++-------
 tests/test-subed-vtt.el | 15 +++++++++++++--
 3 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/subed/subed-common.el b/subed/subed-common.el
index 5a81703603..aa8c150fb2 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -110,7 +110,7 @@ If BEG and END are both nil, run BODY only on the subtitle 
at point.
 If REVERSE is non-nil, start on the subtitle at END and move backwards.
 Before BODY is run, point is placed on the subtitle's ID."
   (declare (indent defun)
-           (debug 1))
+           (debug t))
   `(atomic-change-group
      (if (not ,beg)
          ;; Run body on subtitle at point
@@ -123,17 +123,19 @@ Before BODY is run, point is placed on the subtitle's ID."
          ;; Run body on multiple subtitles
          (if ,reverse
              ;; Iterate backwards
-             (save-excursion (goto-char endm)
-                             (subed-jump-to-subtitle-id)
-                             (catch 'first-subtitle-reached
-                               (while t
-                                 ;; The subtitle includes every character up 
to the next subtitle's ID (or eob)
-                                 (let ((sub-end (save-excursion 
(subed-jump-to-subtitle-end))))
-                                   (when (< sub-end begm)
-                                     (throw 'first-subtitle-reached t)))
-                                 (progn ,@body)
-                                 (unless (subed-backward-subtitle-id)
-                                   (throw 'first-subtitle-reached t)))))
+             (save-excursion
+               (goto-char endm)
+               (unless (subed-jump-to-subtitle-id)
+                 (subed-backward-subtitle-id))
+               (catch 'first-subtitle-reached
+                 (while t
+                   ;; The subtitle includes every character up to the next 
subtitle's ID (or eob)
+                   (let ((sub-end (save-excursion 
(subed-jump-to-subtitle-end))))
+                     (when (< sub-end begm)
+                       (throw 'first-subtitle-reached t)))
+                   (progn ,@body)
+                   (unless (subed-backward-subtitle-id)
+                     (throw 'first-subtitle-reached t)))))
            ;; Iterate forwards
            (save-excursion
              (goto-char begm)
diff --git a/subed/subed-vtt.el b/subed/subed-vtt.el
index 7e9dcf9ae4..93d4844277 100644
--- a/subed/subed-vtt.el
+++ b/subed/subed-vtt.el
@@ -217,13 +217,13 @@ format-specific function for MAJOR-MODE."
 Return point or nil if there is no previous subtitle.  Use the
 format-specific function for MAJOR-MODE."
   (let ((orig-point (point)))
-    (when (subed-jump-to-subtitle-id)
-      (or
-       (catch 'found
-         (while (re-search-backward subed--regexp-separator nil t)
-           (when (subed-jump-to-subtitle-id)
-             (throw 'found (point)))))
-       (progn (goto-char orig-point) nil)))))
+    (subed-jump-to-subtitle-id)
+    (or
+     (catch 'found
+       (while (re-search-backward subed--regexp-separator nil t)
+         (when (subed-jump-to-subtitle-id)
+           (throw 'found (point)))))
+     (progn (goto-char orig-point) nil))))
 
 ;;; Manipulation
 
diff --git a/tests/test-subed-vtt.el b/tests/test-subed-vtt.el
index 3ff4191027..06528fee92 100644
--- a/tests/test-subed-vtt.el
+++ b/tests/test-subed-vtt.el
@@ -418,6 +418,10 @@ Baz.
          (expect (looking-at (regexp-quote "00:03:15.5")) :to-be t)
          (expect (subed-backward-subtitle-id) :to-be 45)
          (expect (looking-at (regexp-quote "00:02:02.234")) :to-be t)))
+      (it "does not get confused by empty lines at the end of the buffer."
+        (with-temp-vtt-buffer
+         (insert mock-vtt-data "\n\n")
+         (expect (subed-backward-subtitle-id) :not :to-be nil)))
       (it "returns nil and doesn't move when there is no previous subtitle."
         (with-temp-vtt-buffer
          (expect (subed-backward-subtitle-id) :to-be nil))
@@ -1388,7 +1392,7 @@ Again")
          (expect (buffer-string) :to-equal "Hello\n\nNOTE 
Comment\n\nWorld\nAgain\n")))))
   (describe "iterating over subtitles"
     (describe "forwards"
-      (it "does not get confused by the header."
+      (it "handles headers."
         (with-temp-vtt-buffer
          (insert mock-vtt-data)
          (let (result)
@@ -1396,9 +1400,16 @@ Again")
              (add-to-list 'result (point)))
            (expect (length result) :to-equal 3)))))
     (describe "backwards"
-      (it "does not get confused by the header."
+      (it "handles headers."
         (with-temp-vtt-buffer
          (insert mock-vtt-data)
+         (let (result)
+           (subed-for-each-subtitle (point-min) (point-max) t
+             (add-to-list 'result (point)))
+           (expect (length result) :to-equal 3))))
+      (it "handles empty lines."
+        (with-temp-vtt-buffer
+         (insert mock-vtt-data "\n\n")
          (let (result)
            (subed-for-each-subtitle (point-min) (point-max) t
              (add-to-list 'result (point)))

Reply via email to