Skip to content

Instantly share code, notes, and snippets.

@mooseyboots
Created February 2, 2022 10:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mooseyboots/d9a183795e5704d3f517878703407184 to your computer and use it in GitHub Desktop.
Save mooseyboots/d9a183795e5704d3f517878703407184 to your computer and use it in GitHub Desktop.
(defun subed-next-start-or-prev-stop (&optional next)
"Fetch the stop time of previous subtitle without moving mpv.
With arg NEXT, fetch the start time of the next subtitle."
(let (adj-sub-stop-or-start)
(subed-save-excursion
(let ((subed-subtitle-time-adjusted-hook nil)) ;dont move mpv
(if next
(progn
(subed-forward-subtitle-text)
(setq adj-sub-stop-or-start (subed-subtitle-msecs-start)))
;; else previous:
(subed-backward-subtitle-text)
(setq adj-sub-stop-or-start (subed-subtitle-msecs-stop)))))
adj-sub-stop-or-start))
(defun subed-decrease-or-increase-and-adjust-previous (&optional increase)
"Decrease stop of previous subtitle then start of current one.
With arg INCREASE, increase start of current subtitle then stop of previous one."
(let ((subed-subtitle-spacing 0)) ; maintain current spacing
(if increase
(progn
(subed-increase-start-time)
(subed-decrease-or-increase-previous 'increase))
(subed-decrease-or-increase-previous)
(subed-decrease-start-time))
(subed--update-cps-overlay)))
(defun subed-decrease-or-increase-previous (&optional increase)
"Decrease previous subtitle end.
With arg INCREASE, increase previous subtitle end."
(subed-save-excursion
(let ((subed-subtitle-time-adjusted-hook nil)) ;dont move mpv
(subed-backward-subtitle-text)
(if increase
(subed-increase-stop-time)
(subed-decrease-stop-time)))))
(defun subed-decrease-or-increase-and-adjust-next (&optional increase)
"Decrease subtitle stop then start of next one.
With arg INCREASE, increase next subtitle start then current one."
(let ((subed-subtitle-spacing 0)) ; maintain current spacing
(if increase
(progn
(subed-decrease-or-increase-next 'increase)
(subed-increase-stop-time))
(subed-decrease-stop-time)
(subed-decrease-or-increase-next))
;; (subed-decrease-start-time))
(subed--update-cps-overlay)))
(defun subed-decrease-or-increase-next (&optional increase)
"Decrease next subtitle start.
With arg INCREASE, increase next subtitle start."
(subed-save-excursion
(let ((subed-subtitle-time-adjusted-hook nil)) ;dont move mpv
(subed-forward-subtitle-text)
(if increase
(subed-increase-start-time)
(subed-decrease-start-time)))))
(defun subed-decrease-start-time-adjust-previous ()
"Subtract `subed-milliseconds-adjust' milliseconds from start
time of the current subtitle and shorten the previous one by the
same amount.
If subtitles are further apart than `subed-milliseconds-adjust',
just run `subed-decrease-start-time'."
(interactive)
(let ((prev-sub-stop (subed-next-start-or-prev-stop)))
(if (< (+ prev-sub-stop subed-milliseconds-adjust)
(subed-subtitle-msecs-start))
(subed-decrease-start-time)
(subed-decrease-or-increase-and-adjust-previous))))
(defun subed-increase-start-time-adjust-previous ()
"Add `subed-milliseconds-adjust' milliseconds to start time
of the current subtitle and lengthen the previous one by the same
amount.
If subtitles are further apart than `subed-subtitle-spacing',
just run `subed-increase-start-time'."
(interactive)
(let ((prev-sub-stop (subed-next-start-or-prev-stop)))
(if (< (+ prev-sub-stop subed-milliseconds-adjust)
(subed-subtitle-msecs-start))
(subed-increase-start-time)
(subed-decrease-or-increase-and-adjust-previous 'increase))))
(defun subed-decrease-stop-time-adjust-next ()
"Subtract `subed-milliseconds-adjust' milliseconds from stop
time of the current subtitle and shorten the next one by the same
amount.
If subtitles are further apart than `subed-subtitle-spacing',
just run `subed-decrease-stop-time'."
(interactive)
(let ((next-sub-start (subed-next-start-or-prev-stop 'next)))
(if (< (subed-subtitle-msecs-stop)
(- next-sub-start subed-milliseconds-adjust))
(subed-decrease-stop-time)
(subed-decrease-or-increase-and-adjust-next))))
(defun subed-increase-stop-time-adjust-next ()
"Add `subed-milliseconds-adjust' milliseconds to stop time of
the current subtitle and lengthen the next one by the same
amount.
If subtitles are further apart than `subed-milliseconds-adjust',
just run `subed-increase-stop-time'."
(interactive)
(let ((next-sub-start (subed-next-start-or-prev-stop 'next)))
(if (< (subed-subtitle-msecs-stop)
(- next-sub-start subed-milliseconds-adjust))
(subed-increase-stop-time)
(subed-decrease-or-increase-and-adjust-next 'increase))))
(defun subed-copy-player-pos-to-stop-time-adjust-next ()
(interactive)
(let ((subed-subtitle-spacing 0))
(subed-save-excursion
(let ((subed-subtitle-time-adjusted-hook nil)) ;dont move mpv
(subed-forward-subtitle-text)
(subed-copy-player-pos-to-start-time)))
(subed-copy-player-pos-to-stop-time)
(subed--update-cps-overlay)))
(defun subed-copy-player-pos-to-start-time-adjust-previous ()
(interactive)
(let ((subed-subtitle-spacing 0))
(subed-copy-player-pos-to-start-time)
(subed-save-excursion
(let ((subed-subtitle-time-adjusted-hook nil)) ;dont move mpv
(subed-backward-subtitle-text)
(subed-copy-player-pos-to-stop-time)))
(subed--update-cps-overlay)))
(define-key subed-mode-map (kbd "C-M-]") 'subed-increase-start-time-adjust-previous)
(define-key subed-mode-map (kbd "C-M-[") 'subed-decrease-start-time-adjust-previous)
(define-key subed-mode-map (kbd "C-M-}") 'subed-increase-stop-time-adjust-next)
(define-key subed-mode-map (kbd "C-M-{") 'subed-decrease-stop-time-adjust-next)
(define-key subed-mode-map (kbd "C-c {") 'subed-copy-player-pos-to-start-time-adjust-previous)
(define-key subed-mode-map (kbd "C-c }") 'subed-copy-player-pos-to-stop-time-adjust-next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment