aboutsummaryrefslogtreecommitdiff
path: root/Emacs/.config/emacs/elisp/org-bookmark.el
blob: 192a75d8b2abd157584680a1fac96af1ad460c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
;;; org-bookmark.el --- Bookmark manager using org-mode  -*- lexical-binding: t; -*-

;; Copyright (C) 2024  Aryadev Chavali

;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
;; Keywords:

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License Version
;; 2 as published by the Free Software Foundation.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'org)

(defvar +bookmark/file (expand-file-name (concat org-directory "/bookmarks.org")))
(defvar +bookmark/mpv-args "--ytdl-raw-options=force-ipv4= --ytdl-format=22 -v")

(defun +bookmark/--get-heading-data ()
  "In an org-mode buffer, with point on a heading: get the title,
tags and url."
  (let ((heading-components (org-heading-components))
        (tags (org-get-tags))
        (url (org-entry-get (point-marker) "URL")))
    (list
     (nth 4 heading-components)
     (cl-remove-if #'(lambda (tag) (string= tag "bookmark")) tags)
     url)))

(defun +bookmark/--get-all-heading-data ()
  "In an org-mode buffer, get all the heading data (titles, tags and
urls)."
  (cl-remove-if
   #'(lambda (x) (member "DONE" (nth 1 x)))
   ;; Extract all headings with :bookmark: tag
   (org-scan-tags
    #'+bookmark/--get-heading-data
    (cdr (org-make-tags-matcher ":bookmark:"))
    nil)))

(defun +bookmark/--format-heading-data (data)
  "Format the heading data extracted into a pair of (TITLE+TAG
. URL)."
  (cl-destructuring-bind (name tags url) data
    (cons
     (format "%s %s" name
             (substring-no-properties
              (cl-reduce #'(lambda (x y) (concat x ":" y))
                         tags
                         :initial-value "")))
     url)))

(defvar +bookmark/--cache nil
  "Cached alist constructed from bookmarks file of form (TITLE+TAG
. URL).")
(defvar +bookmark/--cache-last-modified nil
  "Last modified time for bookmarks file as a float.")

(defun +bookmark/bookmarks ()
  "Get all bookmarks from +BOOKMARK/FILE in alist format.  Results
are cached for faster lookup."
  (with-current-buffer (find-file-noselect +bookmark/file)
    (let ((cur-last-modified (float-time (visited-file-modtime))))
      ;; If no cache, or no last-modified or the file has been modified
      ;; since we've last cached then recache.
      (when (or (null +bookmark/--cache)
               (null +bookmark/--cache-last-modified)
               (not (= cur-last-modified +bookmark/--cache-last-modified)))
        (setq +bookmark/--cache-last-modified cur-last-modified
              +bookmark/--cache (mapcar
                                 #'+bookmark/--format-heading-data
                                 (+bookmark/--get-all-heading-data))))))
  +bookmark/--cache)

(defun +bookmark/open-mpv (url)
  (interactive)
  (message "[bookmark]: Starting MPV process")
  (with-current-buffer (get-buffer-create "*mpv*")
    (ansi-color-for-comint-mode-on)
    (comint-mode))
  (set-process-filter (start-process-shell-command
                       "bookmark-mpv" "*mpv*"
                       (concat "mpv " +bookmark/mpv-args " \"" url "\""))
                      #'comint-output-filter))

(defconst +bookmark/dispatch-list
  '((("^https://\\(www.\\)?youtu\\(.\\)?be"
      "\\.mp4$")
     . +bookmark/open-mpv)
    (otherwise . eww))
  "List of pairs of type (PATTERNS . FUNC) which is used in
+BOOKMARK/OPEN-BOOKMARK to handle opening urls.

PATTERNS is a list of one or more regexp patterns (as strings)
which should match particular URLs.  It can also be 'OTHERWISE
which represents the default case if no previous patterns match
the URL.

FUNC is a callable or function which takes one parameter (the URL
as a string) and produces some action of opening it.")

(defun +bookmark/open-bookmark ()
  "Open a bookmark.  Uses +BOOKMARK/DISPATCH-LIST to dispatch
opening the url to some handler function."
  (interactive)
  (let* ((bookmarks (+bookmark/bookmarks))
         (choice (completing-read "Choose bookmark: " (mapcar #'car bookmarks) nil t))
         (pair (assoc choice bookmarks #'string=)))
    (if (null pair)
        (error (format "`%s' is not a valid bookmark" choice))
      (message "[bookmark]: Opening `%s` (`%s`)" (car pair) (cdr pair))
      (let* ((url (cdr pair))
             (dispatch-choice
              (cl-loop
               for (patterns . func) in +bookmark/dispatch-list
               if (or (eq patterns 'otherwise)
                     (cl-some
                      #'(lambda (pattern) (string-match pattern url))
                      patterns))
               return func)))
        (message "dispatch-choice=%S" dispatch-choice)
        (funcall dispatch-choice (cdr pair))))))

(provide 'org-bookmark)
;;; bookmark.el ends here