[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Is there a better (built-in) way to insert an org link with title as des
From: |
Arthur Miller |
Subject: |
Is there a better (built-in) way to insert an org link with title as description? |
Date: |
Wed, 19 Jul 2023 14:06:05 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
Hello Org experts,
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, so I coded my own version of Miros idea:
#+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
(if (string-match-p "^\\(http\\|https\\)://" (current-kill 0))
(current-kill 0)
(read-string "URL: ")))
(title nil))
(when url
(url-retrieve url
(lambda (buffer)
(goto-char (point-min))
(when (re-search-forward "<title>\\(.*\\)</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 (read-string "Description: "))))))
nil t t))))
#+end_src
While my function is not very big, I would still like to learn how to use the
suggested built-in stuff from that discussion. I also dislike the interactive
fallback in asynchronous callback if the title is not found, but I would also
dislike to have a bunch of "No description found" strings in my notes too, so I
am not sure which one is less evil there.
Thankful for any help and advice.