[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/subed 6b1a6f2 367/389: Add computing and displaying charac
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/subed 6b1a6f2 367/389: Add computing and displaying characters per second |
Date: |
Fri, 3 Dec 2021 11:01:00 -0500 (EST) |
branch: elpa/subed
commit 6b1a6f2be4a784e3fc3681c6e440d7f3306383ba
Author: Marcin Borkowski <mbork@mbork.pl>
Commit: Marcin Borkowski <mbork@mbork.pl>
Add computing and displaying characters per second
---
subed/subed-common.el | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 73 insertions(+)
diff --git a/subed/subed-common.el b/subed/subed-common.el
index e5d3fa8..f62ddac 100644
--- a/subed/subed-common.el
+++ b/subed/subed-common.el
@@ -1028,5 +1028,78 @@ Return nil if function `buffer-file-name' returns nil."
(when (file-exists-p file-stem-video)
(throw 'found-videofile file-stem-video))))))))
+;;; Characters per second computation
+
+(defun subed-show-cps-p ()
+ "Whether CPS is shown for the current subtitle."
+ (member #'subed--update-cps-overlay after-change-functions))
+
+(defun subed-enable-show-cps ()
+ "Enable showing CPS next to the subtitle heading."
+ (interactive)
+ (add-hook 'after-change-functions #'subed--update-cps-overlay nil t)
+ (add-hook 'subed-subtitle-motion-hook
#'subed--move-cps-overlay-to-current-subtitle nil t)
+ (add-hook 'after-save-hook #'subed--move-cps-overlay-to-current-subtitle nil
t))
+
+(defun subed-disable-show-cps ()
+ "Enable showing CPS next to the subtitle heading."
+ (interactive)
+ (remove-hook 'after-change-functions #'subed--update-cps-overlay t)
+ (remove-hook 'subed-subtitle-motion-hook
#'subed--move-cps-overlay-to-current-subtitle t)
+ (remove-hook 'after-save-hook #'subed--move-cps-overlay-to-current-subtitle
t))
+
+(defvar subed-transform-for-cps #'subed--strip-tags)
+
+(defun subed--strip-tags (string)
+ "Strip HTML-like tags from STRING."
+ (with-temp-buffer
+ (insert string)
+ (goto-char 1)
+ (while (re-search-forward "</?[^>]+>" nil t)
+ (delete-region (match-beginning 0) (match-end 0)))
+ (buffer-string)))
+
+(defun subed-calculate-cps (&optional print-message)
+ "Calculate characters per second of the current subtitle."
+ (interactive "p")
+ (let* ((msecs-start (subed-subtitle-msecs-start))
+ (msecs-stop (subed-subtitle-msecs-stop))
+ (text (if (fboundp subed-transform-for-cps)
+ (funcall subed-transform-for-cps (subed-subtitle-text))
+ (subed-subtitle-text)))
+ (length (length text))
+ (cps (when (and (numberp msecs-stop)
+ (numberp msecs-start))
+ (/ length 0.001 (- msecs-stop msecs-start)))))
+ (if (and print-message cps)
+ (message "%.1f characters per second" cps)
+ cps)))
+
+(defvar-local subed--cps-overlay nil)
+
+(defun subed--move-cps-overlay-to-current-subtitle ()
+ "Move the CPS overlay to the current subtitle."
+ (let* ((begin (save-excursion
+ (subed-jump-to-subtitle-time-start)
+ (point)))
+ (end (save-excursion
+ (goto-char begin)
+ (line-end-position))))
+ (if (overlayp subed--cps-overlay)
+ (move-overlay subed--cps-overlay begin end (current-buffer))
+ (setq subed--cps-overlay (make-overlay begin end)))
+ (subed--update-cps-overlay)))
+
+(defun subed--update-cps-overlay (&rest _rest)
+ "Update the CPS overlay.
+This accepts and ignores any number of arguments so that it can
+be run in `after-change-functions'."
+ (let ((cps (subed-calculate-cps)))
+ (when (numberp cps)
+ (overlay-put
+ subed--cps-overlay
+ 'after-string
+ (propertize (format " %.1f CPS" cps) 'face 'shadow 'display '(height
0.9))))))
+
(provide 'subed-common)
;;; subed-common.el ends here
- [nongnu] elpa/subed b28f528 364/389: Revert indentation changes, (continued)
- [nongnu] elpa/subed b28f528 364/389: Revert indentation changes, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 5b868c3 366/389: Fix minor issues in README.org added in last commit, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 6949622 365/389: Adopt REUSE standard for copyright and licensing, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 9f55817 369/389: Wrap start/stop calculating functions in `ignore-errors', ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed beda7dc 371/389: Add functions to insert HTML-like tags, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed c23dd84 373/389: Add info about the new features to README.org, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 6a5802c 374/389: Add video movement per frame, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 828764f 375/389: Change keybindings and use a transient keymap, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 5de6f21 377/389: Add msecs-to-timestamp as a common function, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed b42f563 378/389: Add audio extensions, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 6b1a6f2 367/389: Add computing and displaying characters per second,
ELPA Syncer <=
- [nongnu] elpa/subed 3ba295b 336/389: Fix "service name too long", ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 961e5eb 343/389: More subed-vtt fixes: require subed-vtt, validate better, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 87b3823 353/389: subed-split-subtitle works at the beginning or end of subtitle text, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 3b4b56a 384/389: Merge pull request #49 from sachac/fix-vtt-spaces, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed bee9e63 342/389: subed-vtt: Fix syncing point with player, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 016c6a5 363/389: subed-split-subtitle: Handle more cases, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 6164f88 380/389: Keep track of video being played, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed 70561f0 381/389: Make hours optional in VTT files, following the spec, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed bf3e832 382/389: Check if subed--cps-overlay exists before trying to update it, ELPA Syncer, 2021/12/03
- [nongnu] elpa/subed b6e08c2 383/389: Handle spaces in between VTT subtitles, ELPA Syncer, 2021/12/03