alphapapa / org-web-tools

View, capture, and archive Web pages in Org-mode
GNU General Public License v3.0
647 stars 33 forks source link

fetch link at point #50

Open mooseyboots opened 2 years ago

mooseyboots commented 2 years ago

i thought to add this to org-web-tools, in order to say, run org-web-tools-read-url-as-org on a link in an elfeed entry or in eww, or on also an org link:

(defun org-web-tools--get-first-url ()
  "Return (shr) URL or org link URL at point, or URL in clipboard, or first URL in the `kill-ring', or nil if none."
  (or (shr-url-at-point nil) ; elfeed or eww links
      (plist-get (get-text-property (point) 'htmlize-link) :uri) ; org links
      (cl-loop for item in (append (list (gui-get-selection 'CLIPBOARD))
                                   kill-ring)
               when (and item (string-match (rx bol "http" (optional "s") "://") item))
               return item)))

could poss add some others, could poss find more rigorous way of fetching them...

alphapapa commented 2 years ago

That would more idiomatically belong in the org-web-tools-read-url-as-org function's interactive form. But it would seem to be a matter of preference, because some users might want the command to ignore any links at point.

mooseyboots commented 2 years ago

@alphapapa thanks for the hint.

i came up with this:

(defcustom org-web-tools-use-link-at-point nil
  "Whether `org-web-tools' should operate on the link at point."
    :type 'boolean)

(cl-defun org-web-tools-read-url-as-org (url &key (show-buffer-fn #'switch-to-buffer))
  "Read URL's readable content in an Org buffer.
Buffer is displayed using SHOW-BUFFER-FN."
  (interactive
   (list
    (if org-web-tools-use-link-at-point
        (or
         ;; shr link (modified from `elfeed-get-link-at-point'):
         (or (shr-url-at-point nil)
             (and (fboundp 'eww-current-url)
                  (funcall 'eww-current-url))
             (get-text-property (point) :nt-link))
         ;; org link:
         (plist-get (get-text-property (point) 'htmlize-link) :uri)
         ;; plain URL (from `elfeed-get-url-at-point'):
         (or (if (fboundp 'thing-at-point-url-at-point)
                    (thing-at-point-url-at-point)
                  (with-no-warnings (url-get-url-at-point)))
             (thing-at-point 'url))
         ;; kill ring as per usual:
         (org-web-tools--get-first-url))
      (org-web-tools--get-first-url))))
  (let ((entry (org-web-tools--url-as-readable-org url)))
    (when entry
      (funcall show-buffer-fn url)
      (org-mode)
      (insert entry)
      ;; Set buffer title
      (goto-char (point-min))
      (rename-buffer (cdr (org-web-tools--read-org-bracket-link))))))