dkellner-browser-bookmarks.el 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ;; dkellner-browser-bookmarks.el --- manage bookmarks in an org-file
  2. (bind-key "C-c b" #'dkellner/open-browser-bookmark)
  3. (defcustom dkellner-browser-bookmarks-file "~/org/bookmarks.org"
  4. "Org-file containing bookmarks as HTTP(S)-URLs.
  5. Currently only a very strict structure is supported, i.e. the
  6. first level headlines will be treated as sections/groups and the
  7. second level ones as bookmarks.
  8. Example:
  9. * Emacs
  10. ** https://www.gnu.org/software/emacs/
  11. ** http://sachachua.com/
  12. * Search Engines
  13. ** https://duckduckgo.com/")
  14. (defun dkellner/open-browser-bookmark ()
  15. "Interactively selects and opens a bookmark in the default browser.
  16. It uses `org-open-link-from-string' and thus `browse-url'
  17. internally for actually sending the URL to the browser. You
  18. should refer to its documentation if you want to change the
  19. browser."
  20. (interactive)
  21. (let ((bookmarks (dkellner/browser-bookmarks-in-org-file
  22. dkellner-browser-bookmarks-file)))
  23. (ivy-read "Open bookmark: " (map-keys bookmarks)
  24. :require-match t
  25. :action (lambda (e) (org-open-link-from-string
  26. (cdr (assoc e bookmarks)))))))
  27. (defun dkellner/browser-bookmarks-in-org-file (org-file)
  28. (with-current-buffer (find-file-noselect (expand-file-name org-file))
  29. (org-element-map (org-element-parse-buffer) 'headline
  30. (lambda (h)
  31. (when (= (org-element-property :level h) 2)
  32. (dkellner/browser-bookmark-to-key-value h))))))
  33. (defun dkellner/browser-bookmark-to-key-value (bookmark)
  34. (let* ((section (org-element-property :parent bookmark))
  35. (section-prefix (concat (org-element-property :raw-value section)
  36. " :: "))
  37. (raw-value (org-element-property :raw-value bookmark))
  38. (regexp "\\[\\[\\(.+?\\)]\\[\\(.+?\\)]]"))
  39. (if (string-match regexp raw-value)
  40. `(,(concat section-prefix (match-string 2 raw-value)) .
  41. ,(match-string 1 raw-value))
  42. `(,(concat section-prefix raw-value) . ,raw-value))))
  43. (provide 'dkellner-browser-bookmarks)