From 87222b0f5622308af6dd04fd09db6c12eddba082 Mon Sep 17 00:00:00 2001 From: Sacha Chua Date: Thu, 17 Oct 2024 10:14:03 -0400 Subject: [PATCH] v1.2.17: Merge without including the current subtitle * subed/subed-common.el (merge-region): Merge without including the current subtitle if the point is at the beginning. (subed-create-file): Update docstring. (subtitle-start-pos): * tests/test-subed-tsv.el ("subed-tsv"): Add test for merging and subtitle-start-pos. * tests/test-subed-srt.el ("subed-srt"): Add test for subtitle-start-pos. * tests/test-subed-vtt.el ("subed-vtt"): Add test for subtitle-start-pos. --- Makefile | 3 +++ NEWS.org | 5 ++++ subed/subed-common.el | 17 +++++++++++-- subed/subed.el | 2 +- tests/test-subed-srt.el | 12 ++++++++++ tests/test-subed-tsv.el | 53 +++++++++++++++++++++++++++++++---------- tests/test-subed-vtt.el | 24 +++++++++++++++---- 7 files changed, 97 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 76c2089..d3cdca3 100644 --- a/Makefile +++ b/Makefile @@ -61,3 +61,6 @@ test-compile: compile clean test-emacs: emacs -Q -L ./subed --eval "(require 'subed-autoloads)" + +watch: + nodemon -w subed -w tests -e el --exec make test diff --git a/NEWS.org b/NEWS.org index 7c9926b..848f6e9 100644 --- a/NEWS.org +++ b/NEWS.org @@ -2,6 +2,11 @@ * subed news +** Version 1.2.17 - 2024-10-17 - Sacha Chua + +- subed-word-data: Load JSON data from WhisperX. +- Merging should not include the current subtitle if the point is at the beginning of the subtitle. + ** Version 1.2.16 - 2024-10-06 - Sacha Chua - subed-merge-with-next separates subtitles diff --git a/subed/subed-common.el b/subed/subed-common.el index 7026472..b7f6dbd 100644 --- a/subed/subed-common.el +++ b/subed/subed-common.el @@ -204,6 +204,16 @@ 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 subtitle-start-pos (&optional sub-id) + "Return the position of the start of the subtitle. +If SUB-ID is not given, use the current subtitle." + (interactive) + (save-excursion + (or + (subed-jump-to-subtitle-comment sub-id) + (subed-jump-to-subtitle-id sub-id)) + (point))) + (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. @@ -1594,7 +1604,10 @@ Update the end timestamp accordingly." (interactive "r") (save-restriction (narrow-to-region (progn (goto-char beg) (or (subed-jump-to-subtitle-id) (point))) - (progn (goto-char end) (or (subed-jump-to-subtitle-end) (point)))) + (progn (goto-char end) + (if (= (point) (subed-subtitle-start-pos)) + (point) + (or (subed-jump-to-subtitle-end) (point))))) (goto-char beg) (while (save-excursion (subed-forward-subtitle-id)) (subed-merge-with-next)))) @@ -2297,7 +2310,7 @@ If LIST is nil, use the subtitles in the current buffer." nil) (defun subed-create-file (filename subtitles &optional ok-if-exists init-func) - "Create FILENAME, set it to MODE, and prepopulate it with SUBTITLES. + "Create FILENAME and prepopulate it with SUBTITLES. If OK-IF-EXISTS is non-nil, overwrite existing files. If INIT-FUNC is non-nil, call that function to initialize." (when (and (file-exists-p filename) (not ok-if-exists)) diff --git a/subed/subed.el b/subed/subed.el index 0f033c7..cb2c3a8 100644 --- a/subed/subed.el +++ b/subed/subed.el @@ -1,6 +1,6 @@ ;;; subed.el --- A major mode for editing subtitles -*- lexical-binding: t; -*- -;; Version: 1.2.16 +;; Version: 1.2.17 ;; Maintainer: Sacha Chua ;; Author: Random User ;; Keywords: convenience, files, hypermedia, multimedia diff --git a/tests/test-subed-srt.el b/tests/test-subed-srt.el index e147dd1..efa3221 100644 --- a/tests/test-subed-srt.el +++ b/tests/test-subed-srt.el @@ -195,6 +195,18 @@ Baz. (insert "foo") (expect (subed-subtitle-relative-point) :to-equal nil))) ) + (describe "the subtitle start position" + (it "returns the start from inside a subtitle." + (with-temp-srt-buffer + (insert mock-srt-data) + (re-search-backward "Bar") + (expect (subed-subtitle-start-pos) :to-equal 39))) + (it "returns the start from the beginning of the line." + (with-temp-srt-buffer + (insert mock-srt-data) + (re-search-backward "^2\n") + (expect (subed-subtitle-start-pos) :to-equal (point))))) + ) (describe "Converting to msecs" (it "works with numbers." diff --git a/tests/test-subed-tsv.el b/tests/test-subed-tsv.el index 741b30a..a3baa61 100644 --- a/tests/test-subed-tsv.el +++ b/tests/test-subed-tsv.el @@ -28,22 +28,32 @@ (it "returns nil if time can't be found." (with-temp-tsv-buffer (expect (subed-subtitle-msecs-start) :to-be nil) - (expect (subed-subtitle-msecs-stop) :to-be nil))) - ) + (expect (subed-subtitle-msecs-stop) :to-be nil)))) + (describe "the subtitle start position" + (it "returns the start from inside a subtitle." + (with-temp-tsv-buffer + (insert mock-tsv-data) + (re-search-backward "This is") + (expect (subed-subtitle-start-pos) :to-equal (line-beginning-position)))) + (it "returns the start from the beginning of the line." + (with-temp-tsv-buffer + (insert mock-tsv-data) + (re-search-backward "14\\.0000") + (expect (subed-subtitle-start-pos) :to-equal (line-beginning-position))))) (describe "the subtitle text" - (describe "when text is empty" - (it "and at the beginning with a trailing newline." + (describe "when text is empty" + (it "and at the beginning with a trailing newline." + (with-temp-tsv-buffer + (insert mock-tsv-data) + (subed-jump-to-subtitle-text "14.000000") + (kill-line) + (expect (subed-subtitle-text) :to-equal ""))))) + (describe "when text is not empty" + (it "and has no linebreaks." (with-temp-tsv-buffer (insert mock-tsv-data) (subed-jump-to-subtitle-text "14.000000") - (kill-line) - (expect (subed-subtitle-text) :to-equal ""))))) - (describe "when text is not empty" - (it "and has no linebreaks." - (with-temp-tsv-buffer - (insert mock-tsv-data) - (subed-jump-to-subtitle-text "14.000000") - (expect (subed-subtitle-text) :to-equal "This is a test."))))) + (expect (subed-subtitle-text) :to-equal "This is a test."))))) (describe "Converting to msecs" (it "works with numbers, although these use seconds because that's what TSV uses." (expect (with-temp-tsv-buffer @@ -378,6 +388,25 @@ "11.120000\t14.000000\tHello, world! 14.000000\t16.800000\tThis is a test. "))) + (describe "Merging" + (it "is limited to the region when at the start of the line." + (with-temp-tsv-buffer + (insert "5.673000 5.913000 phone +5.953000 6.013000 to +6.053000 6.213000 write +6.253000 6.333000 the +6.373000 6.713000 text, +") + (goto-char (point-min)) + (forward-line 3) + (subed-merge-region (point-min) (point)) + (expect (buffer-string) :to-equal +"5.673000 6.213000 phone to write +6.253000 6.333000 the +6.373000 6.713000 text, +" + ) + ))) (describe "Converting msecs to timestamp" (it "uses the right format" (with-temp-tsv-buffer diff --git a/tests/test-subed-vtt.el b/tests/test-subed-vtt.el index 6db4a58..08535c4 100644 --- a/tests/test-subed-vtt.el +++ b/tests/test-subed-vtt.el @@ -36,12 +36,12 @@ Baz. (with-temp-vtt-buffer (expect (subed-subtitle-id) :to-equal nil))) (it "handles extra attributes" - (with-temp-vtt-buffer - (insert "WEBVTT + (with-temp-vtt-buffer + (insert "WEBVTT 00:00:01.000 --> 00:00:02.000 align:start position:0% Hello world") - (expect (subed-subtitle-id) :to-equal "00:00:01.000")))) + (expect (subed-subtitle-id) :to-equal "00:00:01.000")))) (describe "the subtitle ID at playback time" (it "returns subtitle ID if time is equal to start time." (with-temp-vtt-buffer @@ -172,7 +172,23 @@ Hello world") (insert "foo") (expect (subed-subtitle-relative-point) :to-equal nil))) ) - ) + (describe "the subtitle start position" + (it "returns the start from inside a subtitle." + (with-temp-vtt-buffer + (insert mock-vtt-data) + (re-search-backward "Bar") + (expect (subed-subtitle-start-pos) :to-equal 45))) + (it "returns the start from the beginning of the line." + (with-temp-vtt-buffer + (insert mock-vtt-data) + (re-search-backward "00:02:02\\.234") + (expect (subed-subtitle-start-pos) :to-equal 45))) + (it "returns the start of a comment" + (with-temp-vtt-buffer + (insert mock-vtt-data) + (re-search-backward "00:02:02\\.234") + (insert "NOTE\n\nThis is a comment\n\n") + (expect (subed-subtitle-start-pos) :to-equal 45))))) (describe "Converting to msecs" (it "works with numbers." (expect (with-temp-vtt-buffer (subed-to-msecs 5123)) :to-equal 5123))