Add org-protocol bookmark handler

This commit is contained in:
Jeremy Dormitzer 2020-04-17 13:25:46 -04:00
parent 0819cc0e55
commit 7d3ebeae7f

View File

@ -107,6 +107,7 @@ Load org-mode early to [[https://github.com/raxod502/straight.el#the-wrong-versi
;; Annoying that this is necessary... ;; Annoying that this is necessary...
(require 'org) (require 'org)
(require 'org-refile) (require 'org-refile)
(require 'org-protocol)
#+END_SRC #+END_SRC
* Doom themes * Doom themes
@ -1917,6 +1918,9 @@ Intelligently inserts an org-mode link from the clipboard.
:defer t :defer t
:after org :after org
:init :init
;; Org-capture setup
(defvar org-capture-bookmark-last-url nil) (defvar org-capture-bookmark-last-url nil)
(defvar org-capture-bookmark-last-title nil) (defvar org-capture-bookmark-last-title nil)
@ -1931,13 +1935,16 @@ Intelligently inserts an org-mode link from the clipboard.
(or (org-cliplink-retrieve-title-synchronously url) (or (org-cliplink-retrieve-title-synchronously url)
(read-string "Bookmark title: "))) (read-string "Bookmark title: ")))
(defun bookmark-file (title)
(concat (get-dropbox-directory)
(format "/org/%s.org" (org-roam--get-new-id title))))
(defun org-capture-bookmark-file () (defun org-capture-bookmark-file ()
(let* ((url (org-capture-bookmark-get-url)) (let* ((url (org-capture-bookmark-get-url))
(title (org-capture-bookmark-get-title url))) (title (org-capture-bookmark-get-title url)))
(setq org-capture-bookmark-last-url url) (setq org-capture-bookmark-last-url url)
(setq org-capture-bookmark-last-title title) (setq org-capture-bookmark-last-title title)
(concat (get-dropbox-directory) (bookmark-file title)))
(format "/org/%s.org" (org-roam--get-new-id title)))))
(defun org-capture-bookmark-link () (defun org-capture-bookmark-link ()
(format "[[%s][%s]]" (format "[[%s][%s]]"
@ -1947,6 +1954,19 @@ Intelligently inserts an org-mode link from the clipboard.
(defun org-capture-bookmark-title () (defun org-capture-bookmark-title ()
org-capture-bookmark-last-title) org-capture-bookmark-last-title)
(defun save-bookmark (url)
(save-excursion
(goto-char (point-min))
(when (search-forward "* Bookmark" nil t)
(org-board-new url)
(wallabag-add-entry url
(cl-function
(lambda (&key data &allow-other-keys)
(let ((entry-url (format "%s/view/%s"
wallabag-base-url
(alist-get 'id data))))
(message "Added bookmark to Wallabag: %s" entry-url))))))))
(defun org-capture-bookmark-after-finalize () (defun org-capture-bookmark-after-finalize ()
"Runs `org-board-new' on the captured entry. "Runs `org-board-new' on the captured entry.
Also saves to Wallabag." Also saves to Wallabag."
@ -1957,16 +1977,7 @@ Intelligently inserts an org-mode link from the clipboard.
(equal key "b") (equal key "b")
(equal desc "Bookmark") (equal desc "Bookmark")
org-capture-bookmark-last-url) org-capture-bookmark-last-url)
(goto-char (point-min)) (save-bookmark org-capture-bookmark-last-url)
(search-forward "* Bookmark")
(org-board-new org-capture-bookmark-last-url)
(wallabag-add-entry org-capture-bookmark-last-url
(cl-function
(lambda (&key data &allow-other-keys)
(let ((entry-url (format "%s/view/%s"
wallabag-base-url
(alist-get 'id data))))
(message "Added bookmark to Wallabag: %s" entry-url)))))
(setq org-capture-bookmark-last-url nil) (setq org-capture-bookmark-last-url nil)
(setq org-capture-bookmark-last-title nil)))) (setq org-capture-bookmark-last-title nil))))
@ -1976,7 +1987,30 @@ Intelligently inserts an org-mode link from the clipboard.
(add-to-list 'org-capture-templates (add-to-list 'org-capture-templates
'("b" "Bookmark" plain '("b" "Bookmark" plain
(file org-capture-bookmark-file) (file org-capture-bookmark-file)
"#+TITLE: %(org-capture-bookmark-title)\n\n- tags :: [[file:deft/bookmarks.org][Bookmarks]]\n- source :: %(org-capture-bookmark-link)\n%?\n* Bookmark")) "#+TITLE: %(org-capture-bookmark-title)\n\n- tags :: [[file:deft/bookmarks.org][Bookmarks]]\n- source :: %(org-capture-bookmark-link)\n%?\n* Bookmark"))
;; Org-protocol setup
(defun make-org-protocol-bookmark (url title)
(with-temp-buffer
(let ((filename (bookmark-file title)))
(save-excursion
(insert (concat (format "#+TITLE: %s\n\n" title)
"- tags :: [[file:deft/bookmarks.org][Bookmarks]]\n"
(format "- source :: [[%s][%s]]\n\n" url title)
"* Bookmark"))
(write-file filename)
(save-bookmark url)
(save-buffer)))))
(defun bookmark-via-org-protocol (url)
(org-cliplink-retrieve-title (url-unhex-string url) #'make-org-protocol-bookmark))
(add-to-list 'org-protocol-protocol-alist
'("Bookmark"
:protocol "bookmark"
:function bookmark-via-org-protocol
:kill-client t))
:config :config
(add-to-list 'org-board-wget-switches "--recursive") (add-to-list 'org-board-wget-switches "--recursive")
(add-to-list 'org-board-wget-switches "--level=1") (add-to-list 'org-board-wget-switches "--level=1")