Update my/denote/url, allow URLs to be paired with titles

This commit is contained in:
Bram Schoenmakers 2024-01-02 22:32:42 +01:00
parent ea7d613ace
commit 0bc4ca1137
Signed by: bram
GPG key ID: 0CCD19DFDC63258F
2 changed files with 78 additions and 66 deletions

View file

@ -730,19 +730,6 @@ Based on an answer at the [[https://emacs.stackexchange.com/a/77480/34645][Emacs
* Create note with Denote from a URL :emacs:
#+begin_src elisp :tangle gists/denote-create-note-url.el
(defconst my/termux-p (getenv "ANDROID_ROOT"))
(defun my/clipboard-get ()
"Return the text on the system clipboard.
This function treats Termux systems differently, the clipboard is only
accessible through the termux-clipboard-get commandline interface,
part of the the termux-api package."
(if my/termux-p
(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.
@ -766,36 +753,55 @@ Based on an answer at the [[https://emacs.stackexchange.com/a/77480/34645][Emacs
(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.")
"List of function symbols to call to get an URL candidate.
Each function should return a string with the URL or a cons
cell (URL . TITLE), where title is either a string or a function
returning a string.")
(defun my/denote/url (url &optional title)
"Create a new Org-based note based on an URL.
"Create a new Org-based note based on a 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."
URL can be a string or a cons cell (URL . TITLE). The TITLE, in
turn, can be a string or a function (without arguments) to
retrieve the title.
When called interactively, the candidate URLs are obtained from
the variable `my/denote/url-functions' (e.g. takes the URL from
the clipboard).
In case no TTTLE is passed to this function, or the URL wasn't
paired with a title value, the title is obtained by curl(1) by
looking at the <title> tags."
(interactive (list
(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)))
(let* ((prompt (format-prompt "URL" ""))
(candidate-urls (-non-nil (mapcar #'funcall my/denote/url-functions)))
(url (if (eql 1 (length candidate-urls))
(read-string prompt (car candidate-urls))
(completing-read prompt candidate-urls))))
;; `candidate-urls' is a mix of strings and cons
;; cells. If the selected URL comes from a cons
;; cell, (assoc) will return it. If it comes from a
;; string valuo, (assoc) will return nil. In that
;; case return the URL as is.
(or (assoc url candidate-urls #'string=) url))
nil))
(denote
(read-string (format-prompt "Title" "") (or title (my/get-url-title url)))
(read-string (format-prompt "Title" "")
;; initial input. If no title was passed, see if it
;; can be obtained from the URL value (the cdr if the
;; url was a cons cell.
(or
title
(cond ((and (consp url) (stringp (cdr url))) (cdr url))
((and (consp url) (functionp (cdr url))) (funcall (cdr url)))
((stringp url) (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

@ -1,16 +1,3 @@
(defconst my/termux-p (getenv "ANDROID_ROOT"))
(defun my/clipboard-get ()
"Return the text on the system clipboard.
This function treats Termux systems differently, the clipboard is only
accessible through the termux-clipboard-get commandline interface,
part of the the termux-api package."
(if my/termux-p
(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.
@ -34,33 +21,52 @@ be found."
(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.")
"List of function symbols to call to get an URL candidate.
Each function should return a string with the URL or a cons
cell (URL . TITLE), where title is either a string or a function
returning a string.")
(defun my/denote/url (url &optional title)
"Create a new Org-based note based on an URL.
"Create a new Org-based note based on a 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."
URL can be a string or a cons cell (URL . TITLE). The TITLE, in
turn, can be a string or a function (without arguments) to
retrieve the title.
When called interactively, the candidate URLs are obtained from
the variable `my/denote/url-functions' (e.g. takes the URL from
the clipboard).
In case no TTTLE is passed to this function, or the URL wasn't
paired with a title value, the title is obtained by curl(1) by
looking at the <title> tags."
(interactive (list
(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)))
(let* ((prompt (format-prompt "URL" ""))
(candidate-urls (-non-nil (mapcar #'funcall my/denote/url-functions)))
(url (if (eql 1 (length candidate-urls))
(read-string prompt (car candidate-urls))
(completing-read prompt candidate-urls))))
;; `candidate-urls' is a mix of strings and cons
;; cells. If the selected URL comes from a cons
;; cell, (assoc) will return it. If it comes from a
;; string valuo, (assoc) will return nil. In that
;; case return the URL as is.
(or (assoc url candidate-urls #'string=) url))
nil))
(denote
(read-string (format-prompt "Title" "") (or title (my/get-url-title url)))
(read-string (format-prompt "Title" "")
;; initial input. If no title was passed, see if it
;; can be obtained from the URL value (the cdr if the
;; url was a cons cell.
(or
title
(cond ((and (consp url) (stringp (cdr url))) (cdr url))
((and (consp url) (functionp (cdr url))) (funcall (cdr url)))
((stringp url) (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)