From 7d3ebeae7fc7e9709f52c5a2d041a9aaa97b9a1a Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Fri, 17 Apr 2020 13:25:46 -0400 Subject: [PATCH] Add org-protocol bookmark handler --- emacs/init.org | 60 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/emacs/init.org b/emacs/init.org index 98bb9ee..4d165d8 100755 --- a/emacs/init.org +++ b/emacs/init.org @@ -107,6 +107,7 @@ Load org-mode early to [[https://github.com/raxod502/straight.el#the-wrong-versi ;; Annoying that this is necessary... (require 'org) (require 'org-refile) + (require 'org-protocol) #+END_SRC * Doom themes @@ -1917,6 +1918,9 @@ Intelligently inserts an org-mode link from the clipboard. :defer t :after org :init + + ;; Org-capture setup + (defvar org-capture-bookmark-last-url 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) (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 () (let* ((url (org-capture-bookmark-get-url)) (title (org-capture-bookmark-get-title url))) (setq org-capture-bookmark-last-url url) (setq org-capture-bookmark-last-title title) - (concat (get-dropbox-directory) - (format "/org/%s.org" (org-roam--get-new-id title))))) + (bookmark-file title))) (defun org-capture-bookmark-link () (format "[[%s][%s]]" @@ -1947,6 +1954,19 @@ Intelligently inserts an org-mode link from the clipboard. (defun org-capture-bookmark-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 () "Runs `org-board-new' on the captured entry. Also saves to Wallabag." @@ -1957,16 +1977,7 @@ Intelligently inserts an org-mode link from the clipboard. (equal key "b") (equal desc "Bookmark") org-capture-bookmark-last-url) - (goto-char (point-min)) - (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))))) + (save-bookmark org-capture-bookmark-last-url) (setq org-capture-bookmark-last-url 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 '("b" "Bookmark" plain (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 (add-to-list 'org-board-wget-switches "--recursive") (add-to-list 'org-board-wget-switches "--level=1")