From 983bf3a1725ed7eb8df6973ca1393e75d3eb68e4 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Thu, 17 Nov 2022 12:50:07 +0800 Subject: bongo: Add cache * common.el (durand-take): Declare as pure and side-effect free. * durand-bongo.el (bongo-sub-redisplay): Do nothing when not playing, to avoid clearing the subtitles buffer when stopped. (durand-bongo-time-process): A helper to replace nil values by 0 in the return value of `parse-time-string'. (bongo-sub-status-string): Use a cache to avoid searching for the subtitle every second. --- common.el | 1 + durand-bongo.el | 194 ++++++++++++++++++++++++++++++-------------------------- 2 files changed, 104 insertions(+), 91 deletions(-) diff --git a/common.el b/common.el index 871f069..94cbe3c 100644 --- a/common.el +++ b/common.el @@ -461,6 +461,7 @@ current line; negative ARG means to put the line upwards." (defun durand-take (n ls) "Return the first N items of LS. If the length of LS is less than N, then return the whole LS." + (declare (pure t) (side-effect-free t)) (cond ((< (length ls) n) ls) ((let ((i 0) result) diff --git a/durand-bongo.el b/durand-bongo.el index a93053a..a3c9ba5 100644 --- a/durand-bongo.el +++ b/durand-bongo.el @@ -897,13 +897,14 @@ is the value of PLAYER's `time-update-delay-after-seek' property." (defun bongo-sub-redisplay () "Update the Bongo Subtitle buffer to reflect the current time." (interactive) - (unless bongo-sub-redisplaying - (let ((bongo-sub-redisplaying t) + (cond + ((or bongo-sub-redisplaying (not (bongo-playing-p)))) + ((let ((bongo-sub-redisplaying t) (sub (or (bongo-sub-status-string (window-width)) ""))) (let ((inhibit-read-only t)) (set-buffer durand-bongo-sub-buffer) (delete-region (point-min) (point-max)) - (insert sub))))) + (insert sub)))))) ;;;; Display string @@ -947,97 +948,108 @@ TIME must be a list of two to four elements." (cond ((/= ele 0) ele))) (list tu tp)))))) +(defun durand-bongo-time-process (time) + "Replace nil by 0 in time." + (declare (pure t) (side-effect-free t)) + (encode-time + (append + (mapcar (lambda (element) (or element 0)) + (durand-take 6 time)) + (nthcdr 6 time)))) + (defun bongo-sub-status-string (width) "Return the subtitle filled to WIDTH." - (cond - ((and (bongo-playing-p) - (bongo-player-get bongo-player 'sub-time) - (bongo-player-get bongo-player 'sub-file-buffer)) - (let ((fb (bongo-player-get bongo-player 'sub-file-buffer)) - (time (bongo-player-get bongo-player 'sub-time)) - found start start-sub end end-sub) - (setq - time - (let ((fake-time - (parse-time-string - (format-seconds "%.2h:%.2m:%.2s" time)))) - (encode-time - (append - (mapcar (lambda (ele) (or ele 0)) - (durand-take 6 fake-time)) - (nthcdr 6 fake-time))))) - (with-current-buffer fb - (goto-char (point-min)) - (save-match-data - (while (and - (not found) - (re-search-forward - (rx bol - (group - (1+ digit) ":" - (1+ digit) ":" (1+ digit) - (zero-or-one - "," (1+ digit))) - (zero-or-more " ") - "-->" - (zero-or-more " ") - (group - (1+ digit) ":" - (1+ digit) ":" (1+ digit) - (zero-or-one - "," (1+ digit))) - eol) - nil t)) - (setq start (match-string 1)) - (setq end (match-string 2)) - (cond - ((string-match ",\\([[:digit:]]+\\)$" start) - (setq start-sub (match-string 1 start)) - (setq - start - (durand-convert-sub-second - (let ((fake-time (parse-time-string - (replace-regexp-in-string - ",.*$" "" start)))) - (encode-time - (append - (mapcar (lambda (ele) (or ele 0)) - (durand-take 6 fake-time)) - (nthcdr 6 fake-time)))) - (string-to-number start-sub)))) - ((setq start (parse-time-string start)))) - (cond - ((string-match ",\\([[:digit:]]+\\)$" end) - (setq end-sub (match-string 1 end)) - (setq - end - (durand-convert-sub-second - (let ((fake-time (parse-time-string - (replace-regexp-in-string - ",.*$" "" end)))) - (encode-time - (append - (mapcar (lambda (ele) (or ele 0)) - (durand-take 6 fake-time)) - (nthcdr 6 fake-time)))) - (string-to-number end-sub)))) - ((setq end (parse-time-string end)))) - (cond - ((and (time-less-p start time) - (time-less-p time end)) - (setq found t)))) - (cond - (found + (with-bongo-playlist-buffer + (cond + ((and (bongo-playing-p) + (bongo-player-get bongo-player 'sub-time) + (bongo-player-get bongo-player 'sub-file-buffer)) + (let ((fb (bongo-player-get bongo-player 'sub-file-buffer)) + (time (bongo-player-get bongo-player 'sub-time)) + (bound (bongo-player-get bongo-player 'sub-bound)) + found start start-sub end end-sub end-pt) + (setq + time + (durand-bongo-time-process + (parse-time-string + (format-seconds "%.2h:%.2m:%.2s" time)))) + (cond + ((and (consp bound) + (time-less-p (car bound) time) + (time-less-p time (cadr bound))) + (caddr bound)) + ((with-current-buffer fb + (goto-char (point-min)) (save-match-data - (save-excursion - (setq - end - (cond - ((re-search-forward (rx bol (zero-or-more space) eol) nil t) - (point)) - ((point-max))))) - (buffer-substring-no-properties - (1+ (point)) end)))))))))) + (while (and + (not found) + (re-search-forward + (rx bol + (group + (1+ digit) ":" + (1+ digit) ":" (1+ digit) + (zero-or-one + "," (1+ digit))) + (zero-or-more " ") + "-->" + (zero-or-more " ") + (group + (1+ digit) ":" + (1+ digit) ":" (1+ digit) + (zero-or-one + "," (1+ digit))) + eol) + nil t)) + (setq start (match-string 1)) + (setq end (match-string 2)) + (cond + ((string-match ",\\([[:digit:]]+\\)$" start) + (setq start-sub (match-string 1 start)) + (setq + start + (durand-convert-sub-second + (durand-bongo-time-process + (parse-time-string + (replace-regexp-in-string + ",.*$" "" start))) + (string-to-number start-sub)))) + ((setq start (durand-bongo-time-process + (parse-time-string start))))) + (cond + ((string-match ",\\([[:digit:]]+\\)$" end) + (setq end-sub (match-string 1 end)) + (setq + end + (durand-convert-sub-second + (durand-bongo-time-process + (parse-time-string + (replace-regexp-in-string + ",.*$" "" end))) + (string-to-number end-sub)))) + ((setq end (durand-bongo-time-process + (parse-time-string end))))) + (cond + ((and (time-less-p start time) + (time-less-p time end)) + (setq found t)))) + (cond + (found + (let* ((end-pt + (save-match-data + (save-excursion + (cond + ((re-search-forward + (rx bol (zero-or-more space) eol) + nil t) + (point)) + ((point-max)))))) + (found + (buffer-substring-no-properties + (point) end-pt))) + (with-bongo-playlist-buffer + (bongo-player-put bongo-player 'sub-bound + (list start end found))) + found)))))))))))) ;; Normally we should do this, but I override the function by my ;; custom function, so I can just call this function there. -- cgit v1.2.3-18-g5258