[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/subed 8263b33 360/389: subed-split-subtitle: Use offset or
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/subed 8263b33 360/389: subed-split-subtitle: Use offset or text fraction |
Date: |
Fri, 3 Dec 2021 11:00:57 -0500 (EST) |
branch: elpa/subed
commit 8263b3345bf12399edbf092779822232292396bc
Author: Sacha Chua <sacha@sachachua.com>
Commit: Sacha Chua <sacha@sachachua.com>
subed-split-subtitle: Use offset or text fraction
* subed/subed-common.el (subed-split-subtitle): Add an optional
argument OFFSET, which can be the number of milliseconds to split at
after the beginning (if positive) or before the end (if negative) of
the current subtitle. If it is not specified and MPV is not playing or
if the playback position is outside the current subtitle, use the
timestamp proportional to the point's position between start and stop
timestamp of the current subtitle.
If called interactively, calling it with one prefix argument
prompts for the offset in milliseconds. Calling it with two
prefix arguments uses the relative position of the point even if MPV
is playing.
---
subed/subed-common.el | 59 ++++++++++++++++++++++++++++++++++-----------------
1 file changed, 40 insertions(+), 19 deletions(-)
diff --git a/subed/subed-common.el b/subed/subed-common.el
index c14920e..8713cd4 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -621,41 +621,62 @@ following manner:
(subed-regenerate-ids-soon))
(point))
-(defun subed-split-subtitle ()
+(defun subed-split-subtitle (&optional offset)
"Split current subtitle at point.
The subtitle text after point is moved to a new subtitle that is
inserted after the current subtitle.
-If a video is playing, `subed-mpv-playback-position' is used as
-the new stop time of the current subtitle. Otherwise, the
-timestamp between the start and stop timestamp of the current
-subtitle is used.
+If OFFSET is a number, it is used as the offset in milliseconds
+from the starting timestamp if positive or from the ending
+timestamp if negative. Otherwise, if
+`subed-mpv-playback-position' is within the current subtitle, it
+is used as the new stop time of the current subtitle. Otherwise,
+the timestamp proportional to the point's position between start
+and stop timestamp of the current subtitle is used.
+
+If called interactively, calling it with one prefix
+argument (e.g. \\[universal-argument] \\[subed-split-subtitle])
+prompts for the offset in milliseconds. Calling it with two
+prefix arguments (e.g. \\[universal-argument]
+\\[universal-argument] \\[subed-split-subtitle]) uses the
+relative position of the point even if the video is playing in
+MPV.
The newly inserted subtitle starts `subed-subtitle-spacing'
milliseconds after the current subtitle's new end timestamp.
-Return point of the new subtitle.
-"
- (interactive)
+Move to the beginning of the new subtitle's text and return the
+position of the point."
+ (interactive (list
+ (cond
+ ((equal current-prefix-arg '(4))
+ (read-number "Offset (ms): "))
+ ((equal current-prefix-arg '(16)) t))))
(let ((text-beg (save-excursion (subed-jump-to-subtitle-text)))
(text-end (save-excursion (or (subed-jump-to-subtitle-end) (point)))))
;; Ensure point is on subtitle text
- (unless (and text-beg text-end (>= (point) text-beg) (<= (point) text-end))
+ (unless (and text-beg (>= (point) text-beg))
(subed-jump-to-subtitle-text))
- (let* ((split-timestamp (if subed-mpv-playback-position
- (+ subed-mpv-playback-position
subed-subtitle-spacing)
- (let ((sub-len (- (subed-subtitle-msecs-stop)
(subed-subtitle-msecs-start))))
- (+ (subed-subtitle-msecs-start) (/ sub-len
2)))))
+ (let* ((orig-start (subed-subtitle-msecs-start))
+ (orig-end (subed-subtitle-msecs-stop))
+ (text-fraction (/ (* 1.0 (- (point) text-beg)) (- text-end
text-beg)))
+ (time-fraction (floor (* text-fraction (- orig-end orig-start))))
+ (split-timestamp
+ (cond
+ ((and (numberp offset) (> offset 0)) (+ orig-start offset))
+ ((and (numberp offset) (< offset 0)) (+ orig-end offset))
+ ((or (equal offset t)
+ (null subed-mpv-playback-position)
+ (> subed-mpv-playback-position orig-end)
+ (< subed-mpv-playback-position orig-start))
+ (+ orig-start time-fraction))
+ (subed-mpv-playback-position subed-mpv-playback-position)))
(new-text (string-trim (buffer-substring (point) text-end)))
- (new-start-timestamp (+ split-timestamp subed-subtitle-spacing))
- (new-stop-timestamp (subed-subtitle-msecs-stop)))
- (when (or (> split-timestamp new-stop-timestamp)
- (< split-timestamp (subed-subtitle-msecs-start)))
- (error "Playback position is not at the subtitle on point."))
+ (new-start-timestamp (+ split-timestamp subed-subtitle-spacing)))
(subed-set-subtitle-time-stop split-timestamp)
(delete-region (point) text-end)
- (subed-append-subtitle nil new-start-timestamp new-stop-timestamp
new-text))
+ (subed-append-subtitle nil new-start-timestamp orig-end new-text))
(subed-regenerate-ids-soon)
(point)))
- [nongnu] elpa/subed c8cf4b7 281/389: Don't insert too many mock subtitles, (continued)
- [nongnu] elpa/subed c8cf4b7 281/389: Don't insert too many mock subtitles, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 3245922 282/389: Focus correct subtitle, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed d076bac 285/389: Make enforced subtitle boundaries optional, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed b5667c4 286/389: Add tests for subed-enforce-time-boundaries, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed dde5dfd 287/389: Simplify subed-mpv--client-buffer, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed b1c5b88 354/389: Some adjustments to subed-split-subtitle, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 5f1ad41 355/389: "mpv" instead of "MPV" (see `man mpv`, https://mpv.io etc), ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed a83ee74 357/389: Remove trailing space, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed da4dac9 358/389: Two spaces after sentence to make `make test` pass, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 49ddccc 359/389: Move subed--init-alist to subed.el, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 8263b33 360/389: subed-split-subtitle: Use offset or text fraction,
ELPA Syncer <=
- [nongnu] elpa/subed 1c52f1b 361/389: Add tests for splitting subtitles and handle more cases, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed e065632 362/389: Fix previous commit for subed-set-subtitle-text, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed ec7b222 368/389: Enable CPS showing by default and improve CPS toggling functions, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed f28ad22 379/389: Make subed-mpv-jump-to-current-subtitle interactive, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed c76ba50 387/389: Prompt for playback speed factor, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 9d0aa0f 388/389: Make make-subtitle a generic function, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed d0dfa1a 389/389: Preliminary support for .ass files (Advanced SubStation Alpha), ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed f30780e 249/389: Default keybinding: C-M-i -> subed-insert-subtitle-adjacent, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed f8793fb 330/389: Move motion hooks from subed-config.el to subed-common.el, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 31556c6 372/389: Add functions for bold and italic and change keybindings, ELPA Syncer, 2021/12/03