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

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

[nongnu] elpa/subed 8acbd46e54 8/9: Add subed-jump-to-subtitle-start-pos


From: ELPA Syncer
Subject: [nongnu] elpa/subed 8acbd46e54 8/9: Add subed-jump-to-subtitle-start-pos and related function.
Date: Sat, 25 Nov 2023 16:00:55 -0500 (EST)

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

    Add subed-jump-to-subtitle-start-pos and related function.
    
    * subed/subed-common.el (jump-to-subtitle-start-pos): New function.
    This should make killing subtitles easier for subtitles that have
    comments or other extra elements before the ID.
    * subed/subed-srt.el (subed--forward-subtitle-id): Back up a little if 
needed.
    * tests/test-subed-vtt.el ("VTT"): Add tests for jumping to subtitle
    start pos.
---
 subed/subed-common.el   | 46 +++++++++++++++++++++++++--------
 subed/subed-srt.el      |  8 ++++--
 tests/test-subed-vtt.el | 69 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+), 13 deletions(-)

diff --git a/subed/subed-common.el b/subed/subed-common.el
index c3dd0ab104..381a260977 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -194,6 +194,15 @@ Return nil if TIME-STRING doesn't match the pattern.")
   "Return the ID of the subtitle at MSECS milliseconds.
 Return nil if there is no subtitle at MSECS.")
 
+(subed-define-generic-function jump-to-subtitle-start-pos (&optional sub-id)
+  "Move to the beginning of a subtitle and return point.
+If SUB-ID is not given, focus the current subtitle.
+Return point or nil if no subtitle could be found."
+  (interactive)
+  (or
+   (subed-jump-to-subtitle-comment sub-id)
+   (subed-jump-to-subtitle-id sub-id)))
+
 (subed-define-generic-function jump-to-subtitle-id (&optional sub-id)
   "Move to the ID of a subtitle and return point.
 If SUB-ID is not given, focus the current subtitle's ID.
@@ -222,7 +231,8 @@ Return point or nil if a the subtitle's text can't be 
found."
   "Move point on the first character of subtitle's comment.
 If SUB-ID is not given, use subtitle on point.
 Return point or nil if a the subtitle's comment can't be found."
-  (interactive))
+  (interactive)
+  nil)
 
 (subed-define-generic-function jump-to-subtitle-end (&optional sub-id)
   "Move point after the last character of the subtitle's text.
@@ -247,6 +257,20 @@ See also `subed-vtt--subtitle-id-at-msecs'."
   (when (subed-jump-to-subtitle-id-at-msecs msecs)
     (subed-jump-to-subtitle-text)))
 
+(subed-define-generic-function forward-subtitle-start-pos ()
+  "Move point to the beginning of the next subtitle.
+Return point or nil if there is no next subtitle."
+  (interactive)
+  (when (subed-forward-subtitle-id)
+    (subed-jump-to-subtitle-start-pos)))
+
+(subed-define-generic-function backward-subtitle-start-pos ()
+  "Move point to the beginning of the previous subtitle.
+Return point or nil if there is no previous subtitle."
+  (interactive)
+  (when (subed-backward-subtitle-id)
+    (subed-jump-to-subtitle-start-pos)))
+
 (subed-define-generic-function forward-subtitle-id ()
   "Move point to next subtitle's ID.
 Return point or nil if there is no next subtitle."
@@ -575,18 +599,18 @@ Return new point."
 (subed-define-generic-function kill-subtitle ()
   "Remove subtitle at point."
   (interactive)
-  (let ((beg (save-excursion (subed-jump-to-subtitle-id)
-                             (point)))
-        (end (save-excursion (subed-jump-to-subtitle-id)
-                             (when (subed-forward-subtitle-id)
-                               (point)))))
+  (let ((beg (save-excursion (subed-jump-to-subtitle-start-pos)))
+        (end (save-excursion (subed-forward-subtitle-start-pos))))
     (if (not end)
         ;; Removing the last subtitle because forward-subtitle-id returned nil
-        (setq beg (save-excursion (goto-char beg)
-                                  (subed-backward-subtitle-end)
-                                  (1+ (point)))
-              end (save-excursion (goto-char (point-max)))))
-    (delete-region beg end)))
+        (setq
+         beg (save-excursion (goto-char beg)
+                             (subed-backward-subtitle-end)
+                             (1+ (point)))
+         end (point-max)))
+    (when beg
+      (remove-overlays beg end)
+      (delete-region beg end))))
 
 (defun subed-parse-file (filename &optional mode-func)
   "Return the subtitles from FILENAME in a list.
diff --git a/subed/subed-srt.el b/subed/subed-srt.el
index d0d8aab3dc..70adc092a5 100644
--- a/subed/subed-srt.el
+++ b/subed/subed-srt.el
@@ -178,8 +178,12 @@ can be found.  Use the format-specific function for 
MAJOR-MODE."
   "Move point to next subtitle's ID.
 Return point or nil if there is no next subtitle.
 Use the format-specific function for MAJOR-MODE."
-  (when (re-search-forward (concat subed--regexp-separator "[0-9]+\n") nil t)
-    (subed-jump-to-subtitle-id)))
+  (let ((pos (point)))
+    (when (and (bolp) (> (point) (point-min))) (forward-char -1))
+    (if (re-search-forward (concat subed--regexp-separator "[0-9]+\n") nil t)
+        (subed-jump-to-subtitle-id)
+      (goto-char pos)
+      nil)))
 
 (cl-defmethod subed--backward-subtitle-id (&context (major-mode 
subed-srt-mode))
   "Move point to previous subtitle's ID.
diff --git a/tests/test-subed-vtt.el b/tests/test-subed-vtt.el
index b49a21ed47..7e54ad4c35 100644
--- a/tests/test-subed-vtt.el
+++ b/tests/test-subed-vtt.el
@@ -216,6 +216,75 @@ Baz.
          (expect (looking-at subed--regexp-timestamp) :to-be t)
          (expect (match-string 0) :to-equal "00:01:01.000")))
       )
+    (describe "to subtitle start pos"
+                       (describe "in the current subtitle"
+                               (it "returns nil in the header."
+                                       (with-temp-vtt-buffer
+                                        (insert mock-vtt-data)
+                                        (goto-char (point-min))
+                                        (expect 
(subed-jump-to-subtitle-start-pos) :to-be nil)))
+                               (it "goes to the ID if specified."
+                                       (with-temp-vtt-buffer
+                                        (insert "WEBVTT
+
+1
+00:01:01.000 --> 00:01:05.123
+Foo.
+
+00:02:02.234 --> 00:02:10.345
+Bar.
+")
+                                        (re-search-backward "Foo")
+                                        (expect 
(subed-jump-to-subtitle-start-pos) :not :to-be nil)
+                                        (expect (looking-at "1") :to-be t)))
+                               (it "goes to the timestamp if there is no ID."
+                                       (with-temp-vtt-buffer
+                                        (insert "WEBVTT
+
+1
+00:01:01.000 --> 00:01:05.123
+Foo.
+
+00:02:02.234 --> 00:02:10.345
+Bar.
+")
+                                        (re-search-backward "Bar")
+                                        (expect 
(subed-jump-to-subtitle-start-pos) :not :to-be nil)
+                                        (expect (looking-at "00:02:02.234") 
:to-be t)))
+        (it "goes to the comment if there is one."
+                                       (with-temp-vtt-buffer
+                                        (insert "WEBVTT
+
+NOTE This is a comment
+
+1
+00:01:01.000 --> 00:01:05.123
+Foo.
+
+00:02:02.234 --> 00:02:10.345
+Bar.
+")
+                                        (re-search-backward "Foo")
+                                        (expect 
(subed-jump-to-subtitle-start-pos) :not :to-be nil)
+                                        (expect (looking-at "NOTE This is a 
comment") :to-be t)))
+                               (describe "when called from a comment"
+                                       (it "goes to the start of the comment."
+                                               (with-temp-vtt-buffer
+                                                (insert "WEBVTT
+
+NOTE
+This is a comment
+
+1
+00:01:01.000 --> 00:01:05.123
+Foo.
+
+00:02:02.234 --> 00:02:10.345
+Bar.
+")
+                                                (re-search-backward "This is a 
comment")
+                                                (expect 
(subed-jump-to-subtitle-start-pos) :not :to-be nil)
+                                                (expect (looking-at 
"NOTE\nThis is a comment") :to-be t))))))
     (describe "to subtitle ID"
       (describe "in the current subtitle"
         (it "returns nil in the header."



reply via email to

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