(Emacs/config|elisp)~+bookmark->org-bookmark and better mpv-args

This commit is contained in:
2024-07-27 04:36:28 +01:00
parent ca1e31f4ee
commit 9449d51aed
2 changed files with 31 additions and 31 deletions

View File

@@ -3038,7 +3038,7 @@ to the list ~org-capture-templates~.
:load-path "elisp/" :load-path "elisp/"
:general :general
(file-leader (file-leader
"b" #'+bookmark/open-bookmark) "b" #'org-bookmark/open-bookmark)
:init :init
(with-eval-after-load "org-capture" (with-eval-after-load "org-capture"
(add-to-list (add-to-list

View File

@@ -28,10 +28,12 @@
(autoload #'org-entry-get "org") (autoload #'org-entry-get "org")
(autoload #'org-make-tags-matcher "org") (autoload #'org-make-tags-matcher "org")
(defvar +bookmark/file (expand-file-name (concat org-directory "/bookmarks.org"))) (defvar org-bookmark/file (expand-file-name (concat org-directory "/bookmarks.org")))
(defvar +bookmark/mpv-args "--ytdl-raw-options=force-ipv4= --ytdl-format=22 -v") (defvar org-bookmark/mpv-args "-v --ytdl-raw-options=force-ipv4= \
--ytdl-format=\"bestvideo[height<=1080][fps<=60]+bestaudio/best[height<=1920]\" \
--profile=fast --hwdec=auto-copy")
(defun +bookmark/--get-heading-data () (defun org-bookmark/--get-heading-data ()
"In an org-mode buffer, with point on a heading: get the title, "In an org-mode buffer, with point on a heading: get the title,
tags and url." tags and url."
(let ((heading-components (org-heading-components)) (let ((heading-components (org-heading-components))
@@ -42,18 +44,18 @@ tags and url."
(cl-remove-if #'(lambda (tag) (string= tag "bookmark")) tags) (cl-remove-if #'(lambda (tag) (string= tag "bookmark")) tags)
url))) url)))
(defun +bookmark/--get-all-heading-data () (defun org-bookmark/--get-all-heading-data ()
"In an org-mode buffer, get all the heading data (titles, tags and "In an org-mode buffer, get all the heading data (titles, tags and
urls)." urls)."
(cl-remove-if (cl-remove-if
#'(lambda (x) (member "DONE" (nth 1 x))) #'(lambda (x) (member "DONE" (nth 1 x)))
;; Extract all headings with :bookmark: tag ;; Extract all headings with :bookmark: tag
(org-scan-tags (org-scan-tags
#'+bookmark/--get-heading-data #'org-bookmark/--get-heading-data
(cdr (org-make-tags-matcher ":bookmark:")) (cdr (org-make-tags-matcher ":bookmark:"))
nil))) nil)))
(defun +bookmark/--format-heading-data (data) (defun org-bookmark/--format-heading-data (data)
"Format the heading data extracted into a pair of (TITLE+TAG "Format the heading data extracted into a pair of (TITLE+TAG
. URL)." . URL)."
(cl-destructuring-bind (name tags url) data (cl-destructuring-bind (name tags url) data
@@ -63,29 +65,27 @@ urls)."
(string-join tags ":"))) (string-join tags ":")))
url))) url)))
(defvar +bookmark/--cache nil (defvar org-bookmark/--cache nil
"Cached alist constructed from bookmarks file of form (TITLE+TAG "Cached alist constructed from bookmarks file of form (TITLE+TAG
. URL).") . URL).")
(defvar +bookmark/--cache-last-modified nil (defvar org-bookmark/--cache-last-modified nil
"Last modified time for bookmarks file as a float.") "Last modified time for bookmarks file as a float.")
(defun +bookmark/bookmarks () (defun org-bookmark/bookmarks ()
"Get all bookmarks from +BOOKMARK/FILE in alist format. Results "Get all bookmarks from ORG-BOOKMARK/FILE in alist format. Results
are cached for faster lookup." are cached for faster lookup."
(with-current-buffer (find-file-noselect +bookmark/file) (with-current-buffer (find-file-noselect org-bookmark/file)
(let ((cur-last-modified (float-time (visited-file-modtime)))) (let ((cur-last-modified (float-time (visited-file-modtime))))
;; If no cache, or no last-modified or the file has been modified (when (or (null org-bookmark/--cache) ; no cache
;; since we've last cached then recache. (null org-bookmark/--cache-last-modified) ; no last modified
(when (or (null +bookmark/--cache) (not (= cur-last-modified org-bookmark/--cache-last-modified))) ; file has been modified
(null +bookmark/--cache-last-modified) (setq org-bookmark/--cache-last-modified cur-last-modified
(not (= cur-last-modified +bookmark/--cache-last-modified))) org-bookmark/--cache (mapcar
(setq +bookmark/--cache-last-modified cur-last-modified #'org-bookmark/--format-heading-data
+bookmark/--cache (mapcar (org-bookmark/--get-all-heading-data))))))
#'+bookmark/--format-heading-data org-bookmark/--cache)
(+bookmark/--get-all-heading-data))))))
+bookmark/--cache)
(defun +bookmark/open-mpv (url) (defun org-bookmark/open-mpv (url)
(interactive) (interactive)
(message "[bookmark]: Starting MPV process") (message "[bookmark]: Starting MPV process")
(with-current-buffer (get-buffer-create "*mpv*") (with-current-buffer (get-buffer-create "*mpv*")
@@ -93,16 +93,16 @@ are cached for faster lookup."
(comint-mode)) (comint-mode))
(set-process-filter (start-process-shell-command (set-process-filter (start-process-shell-command
"bookmark-mpv" "*mpv*" "bookmark-mpv" "*mpv*"
(concat "mpv " +bookmark/mpv-args " \"" url "\"")) (concat "mpv " org-bookmark/mpv-args " \"" url "\""))
#'comint-output-filter)) #'comint-output-filter))
(defconst +bookmark/dispatch-list (defconst org-bookmark/dispatch-list
'((("^https://\\(www.\\)?youtu\\(.\\)?be" '((("^https://\\(www.\\)?youtu\\(.\\)?be"
"\\.mp4$") "\\.mp4$")
. +bookmark/open-mpv) . org-bookmark/open-mpv)
(otherwise . eww)) (otherwise . eww))
"List of pairs of type (PATTERNS . FUNC) which is used in "List of pairs of type (PATTERNS . FUNC) which is used in
+BOOKMARK/OPEN-BOOKMARK to handle opening urls. ORG-BOOKMARK/OPEN-BOOKMARK to handle opening urls.
PATTERNS is a list of one or more regexp patterns (as strings) PATTERNS is a list of one or more regexp patterns (as strings)
which should match particular URLs. It can also be 'OTHERWISE which should match particular URLs. It can also be 'OTHERWISE
@@ -112,11 +112,11 @@ the URL.
FUNC is a callable or function which takes one parameter (the URL FUNC is a callable or function which takes one parameter (the URL
as a string) and produces some action of opening it.") as a string) and produces some action of opening it.")
(defun +bookmark/open-bookmark () (defun org-bookmark/open-bookmark ()
"Open a bookmark. Uses +BOOKMARK/DISPATCH-LIST to dispatch "Open a bookmark. Uses ORG-BOOKMARK/DISPATCH-LIST to dispatch
opening the url to some handler function." opening the url to some handler function."
(interactive) (interactive)
(let* ((bookmarks (+bookmark/bookmarks)) (let* ((bookmarks (org-bookmark/bookmarks))
(choice (completing-read "Choose bookmark: " (mapcar #'car bookmarks) nil t)) (choice (completing-read "Choose bookmark: " (mapcar #'car bookmarks) nil t))
(pair (assoc choice bookmarks #'string=))) (pair (assoc choice bookmarks #'string=)))
(if (null pair) (if (null pair)
@@ -125,7 +125,7 @@ opening the url to some handler function."
(let* ((url (cdr pair)) (let* ((url (cdr pair))
(dispatch-choice (dispatch-choice
(cl-loop (cl-loop
for (patterns . func) in +bookmark/dispatch-list for (patterns . func) in org-bookmark/dispatch-list
if (or (eq patterns 'otherwise) if (or (eq patterns 'otherwise)
(cl-some (cl-some
#'(lambda (pattern) (string-match pattern url)) #'(lambda (pattern) (string-match pattern url))