Skip to content

Commit

Permalink
v1.2.17: Merge without including the current subtitle
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sachac committed Oct 17, 2024
1 parent f5f6c5a commit 87222b0
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions NEWS.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions subed/subed-common.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))))
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion subed/subed.el
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
;; Author: Random User
;; Keywords: convenience, files, hypermedia, multimedia
Expand Down
12 changes: 12 additions & 0 deletions tests/test-subed-srt.el
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
53 changes: 41 additions & 12 deletions tests/test-subed-tsv.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions tests/test-subed-vtt.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 87222b0

Please sign in to comment.