alphapapa / org-web-tools

View, capture, and archive Web pages in Org-mode

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fetch link at point

mooseyboots opened this issue · comments

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...

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.

@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))))))