[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is there a better (built-in) way to insert an org link with title as
From: |
Arthur Miller |
Subject: |
Re: Is there a better (built-in) way to insert an org link with title as description? |
Date: |
Fri, 21 Jul 2023 16:18:22 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Ihor Radchenko <yantar92@posteo.net> writes:
> Arthur Miller <arthur.miller@live.com> writes:
>
>> I want to auto insert a title from an HTML page as description for an org
>> link in
>> my notes. I stumbled upon some old message by Miro Bezjak on this list:
>>
>> https://lists.gnu.org/archive/html/emacs-orgmode/2012-09/msg01435.html
>>
>> I have seen the replies, but I am not sure how to use
>> org-make-link-description-function
>
> Since that time, Org got a capability to set description function
> per link type. Just use
>
> (org-link-set-parameters "http" :insert-description #'your-function)
> (org-link-set-parameters "https" :insert-description #'your-function)
Thanks, after some thinkering I got it:
#+begin_src emacs-lisp
(defun my-org-insert-link ()
"Insert org link where default description is set to html title."
(interactive)
(let* ((url (or (current-kill 0) (read-string "URL: "))))
(org-insert-link nil url)))
(defun org-desc-from-clipboard (url _desc)
"Insert an org link into current buffer from an URL in clipboard."
(with-current-buffer (url-retrieve-synchronously url t)
(goto-char (point-min))
(let ((title "<title>\\(.*\\)\\(/>\\|</title>\\)"))
(if (re-search-forward title nil t)
(string-trim (match-string-no-properties 1))
url))))
(org-link-set-parameters "http" :insert-description #'org-desc-from-clipboard)
(org-link-set-parameters "https" :insert-description #'org-desc-from-clipboard)
#+end_src
And I can do it async too, *but*; this will affect all insertions of links,
right?
I am not sure if it is safe/possible always to access the internet or do
it asynchronously, so I'll abandon the ship and revert to home-cooked one just
for the precautios measures:
#+begin_src emacs-lisp
(defun org-link-from-clipboard ()
"Insert an org link into current buffer from an URL in clipboard."
(interactive)
(let ((marker (point-marker))
(url (or (current-kill 0) (read-string "URL: "))))
(url-retrieve
url
(lambda (_status title)
(goto-char (point-min))
(when (re-search-forward title nil t)
(setq title (string-trim (match-string-no-properties 1))))
(with-current-buffer (marker-buffer marker)
(save-excursion
(goto-char (marker-position marker))
(org-insert-link
nil url (or title url)))))
'("<title>\\(.*\\)\\(/>\\|</title>\\)") t t)))
#end_src
But it was a bit of learning, thanks for pointing me in the right direction.