From 5455025f1ba237dc48d40c47100e5b32d84d0ed3 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 2 Feb 2022 18:28:45 -0600 Subject: more dwimmy --- lisp/+org.el | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'lisp') diff --git a/lisp/+org.el b/lisp/+org.el index 06e70e4..48b3d42 100644 --- a/lisp/+org.el +++ b/lisp/+org.el @@ -253,6 +253,18 @@ instead of the true count." ;;; org-insert-link-dwim - https://xenodium.com/emacs-dwim-do-what-i-mean/ +(defun +org-insert--get-title-and-headings (url) + "Retrieve title and headings from URL. +Return as a list." + (with-current-buffer (url-retrieve-synchronously url) + (let ((dom (libxml-parse-html-region (point-min) (point-max)))) + (cl-remove-if + (lambda (i) (string= i "")) + (apply #'append (mapcar (lambda (tag) + (mapcar #'dom-text + (dom-by-tag dom tag))) + '(title h1 h2 h3 h4 h5 h6))))))) + (defun +org-insert-link-dwim (&optional interactivep) "Like `org-insert-link' but with personal dwim preferences." (interactive '(t)) @@ -267,20 +279,16 @@ instead of the true count." (region-content (when (region-active-p) (buffer-substring-no-properties (region-beginning) (region-end)))) - (org-link (when clipboard-url + (org-link (when (and clipboard-url (not point-in-link)) (org-link-make-string (string-trim clipboard-url) (or region-content - (read-string "title: " - (with-current-buffer - (url-retrieve-synchronously - clipboard-url) - (dom-text - (car - (dom-by-tag (libxml-parse-html-region - (point-min) - (point-max)) - 'title)))))))))) + (let ((clipboard-headings + (+org-insert--get-title-and-headings clipboard-url))) + (read-string "title (edit): " + (completing-read + "title: " clipboard-headings + nil nil nil nil (car clipboard-headings))))))))) (if interactivep (cond ((and region-content clipboard-url (not point-in-link)) (delete-region (region-beginning) (region-end)) -- cgit 1.4.1-21-gabe81