Expand the my/denote/url functionality

This commit is contained in:
Bram Schoenmakers 2024-01-02 15:23:45 +01:00
parent 0fae583d4e
commit ea7d613ace
Signed by: bram
GPG key ID: 0CCD19DFDC63258F
2 changed files with 78 additions and 34 deletions

View file

@ -743,37 +743,59 @@ Based on an answer at the [[https://emacs.stackexchange.com/a/77480/34645][Emacs
(shell-command-to-string "termux-clipboard-get")
(current-kill 0)))
(defun my/get-url-title (url)
"Attempt to retrieve the title string from the given URL.
Assuming the URL points to an HTML source.
Assuming the URL points to an HTML source.
Returns nil if there is a non-200 return status or no title could
be found."
Returns nil if there is a non-200 return status or no title could
be found."
(let* ((command (format "curl --fail --silent %s" url))
(html (shell-command-to-string command))
(regexp (rx (seq "<title>"
(group (+ (not (any "<" ">"))))
"</title>")))
(matches (string-match regexp html))
(suggested-title (match-string 1 html)))
(read-string (format-prompt "Title" "") suggested-title t)))
(matches (string-match regexp html)))
(match-string 1 html)))
(defun my/denote/url (url)
(defun my/denote/url-clipboard ()
"Return the URL from the system clipboard, if any."
(let ((clipboard (my/clipboard-get)))
(when (org-url-p clipboard)
clipboard)))
(defvar my/denote/url-functions
'(thing-at-point-url-at-point my/denote/url-clipboard)
"List of function symbols to call to get an URL candidate.")
(defun my/denote/url (url &optional title)
"Create a new Org-based note based on an URL.
Use the URL on the clipboard if there is one. Then, the title
is retrieved from the HTML source to suggest the title for the
note."
Use the URL on the clipboard if there is one. Then, the title is
retrieved from the HTML source to suggest the title for the note."
(interactive (list
(let* ((clipboard (my/clipboard-get))
(clipboard-url (when (org-url-p clipboard) clipboard)))
(read-string (format-prompt "URL" "")
clipboard-url
t))))
(denote (my/get-url-title url) (denote-keywords-prompt) 'org (denote-subdirectory-prompt))
(let ((candidate-urls (-non-nil (mapcar #'funcall my/denote/url-functions)))
(prompt (format-prompt "URL" "")))
(if (eql 1 (length candidate-urls))
(read-string prompt (car candidate-urls))
(completing-read prompt candidate-urls)))
nil))
(denote
(read-string (format-prompt "Title" "") (or title (my/get-url-title url)))
(denote-keywords-prompt)
'org
(denote-subdirectory-prompt))
(org-set-property "URL" url))
;;; Elfeed integration
(defun my/elfeed/entry-url ()
"Return the URL of the current elfeed entry."
(when-let ((entry (or elfeed-show-entry
(elfeed-search-selected :ignore-region))))
(elfeed-entry-link entry)))
(add-to-list 'my/denote/url-functions #'my/elfeed/entry-url)
#+end_src
* Using gpg-agent inside Emacs in Termux

View file

@ -11,34 +11,56 @@ part of the the termux-api package."
(shell-command-to-string "termux-clipboard-get")
(current-kill 0)))
(defun my/get-url-title (url)
"Attempt to retrieve the title string from the given URL.
Assuming the URL points to an HTML source.
Assuming the URL points to an HTML source.
Returns nil if there is a non-200 return status or no title could
be found."
Returns nil if there is a non-200 return status or no title could
be found."
(let* ((command (format "curl --fail --silent %s" url))
(html (shell-command-to-string command))
(regexp (rx (seq "<title>"
(group (+ (not (any "<" ">"))))
"</title>")))
(matches (string-match regexp html))
(suggested-title (match-string 1 html)))
(read-string (format-prompt "Title" "") suggested-title t)))
(matches (string-match regexp html)))
(match-string 1 html)))
(defun my/denote/url (url)
(defun my/denote/url-clipboard ()
"Return the URL from the system clipboard, if any."
(let ((clipboard (my/clipboard-get)))
(when (org-url-p clipboard)
clipboard)))
(defvar my/denote/url-functions
'(thing-at-point-url-at-point my/denote/url-clipboard)
"List of function symbols to call to get an URL candidate.")
(defun my/denote/url (url &optional title)
"Create a new Org-based note based on an URL.
Use the URL on the clipboard if there is one. Then, the title
is retrieved from the HTML source to suggest the title for the
note."
Use the URL on the clipboard if there is one. Then, the title is
retrieved from the HTML source to suggest the title for the note."
(interactive (list
(let* ((clipboard (my/clipboard-get))
(clipboard-url (when (org-url-p clipboard) clipboard)))
(read-string (format-prompt "URL" "")
clipboard-url
t))))
(denote (my/get-url-title url) (denote-keywords-prompt) 'org (denote-subdirectory-prompt))
(let ((candidate-urls (-non-nil (mapcar #'funcall my/denote/url-functions)))
(prompt (format-prompt "URL" "")))
(if (eql 1 (length candidate-urls))
(read-string prompt (car candidate-urls))
(completing-read prompt candidate-urls)))
nil))
(denote
(read-string (format-prompt "Title" "") (or title (my/get-url-title url)))
(denote-keywords-prompt)
'org
(denote-subdirectory-prompt))
(org-set-property "URL" url))
;;; Elfeed integration
(defun my/elfeed/entry-url ()
"Return the URL of the current elfeed entry."
(when-let ((entry (or elfeed-show-entry
(elfeed-search-selected :ignore-region))))
(elfeed-entry-link entry)))
(add-to-list 'my/denote/url-functions #'my/elfeed/entry-url)