From c33b0ab8a4c9a8b80712b79b6e5dd246b901f9f3 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Sat, 25 Aug 2018 13:22:02 +0200 Subject: [PATCH] eww: Allow eww-add-bookmark to modify tags and title --- .emacs.d/lisp/init-eww.el | 52 +++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/.emacs.d/lisp/init-eww.el b/.emacs.d/lisp/init-eww.el index 1d5dd30f..827ba102 100644 --- a/.emacs.d/lisp/init-eww.el +++ b/.emacs.d/lisp/init-eww.el @@ -171,32 +171,58 @@ word(s) will be searched for via `eww-search-prefix'." ;; TODO: Add bookmark editing functions such as edit title, tags, quickmark, ;; search-engine. Use eww-buffers and Helm. (defun ambrevar/eww-add-bookmark (&optional url title) - "Bookmark the current page." + "Bookmark the current page. +With prefix argument, prompt for bookmark title." (setq url (or url (plist-get eww-data :url))) (setq title (or title (plist-get eww-data :title))) (interactive) (eww-read-bookmarks) - (let (tag-list) + (let (tag-list existing-bookmark) (dolist (bookmark eww-bookmarks) (when (equal ;; PATCH: Ignore protocol when sorting. ;; TODO: Include "sort-bookmarks": Warn for unique tags, warn for same URL up to paragraph. Make this customizable. (replace-regexp-in-string "^https?" "" url) (replace-regexp-in-string "^https?" "" (plist-get bookmark :url))) - (user-error "Already bookmarked")) + (if existing-bookmark + (user-error "Duplicate bookmarks: %s, %s" existing-bookmark bookmark) + (setq existing-bookmark bookmark))) (setq tag-list (append tag-list (plist-get bookmark :tags)))) (cl-delete-duplicates tag-list) - (let ((tags (completing-read-multiple "Tags for bookmark (comma separated): " tag-list)) - (title (replace-regexp-in-string "[\n\t\r]" " " title))) - (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title)) - (push `(:url ,url - :title ,title - :time ,(current-time-string) - ,@(if tags (list :tags tags))) - eww-bookmarks) + (let ((tags (completing-read-multiple (format "%s for bookmark (comma separated): " + (if existing-bookmark "Update tags" "Tags")) + tag-list + nil nil nil nil + (and existing-bookmark + (mapconcat 'identity + (plist-get existing-bookmark :tags) ",")))) + (existing-title (or (and existing-bookmark (plist-get existing-bookmark :title)) + "")) + (sanitize-title (lambda (title) + (setq title (string-trim title)) + (setq title (replace-regexp-in-string "[\n\t\r]" " " title))))) + (setq title (funcall sanitize-title title)) + (setq existing-title (funcall sanitize-title existing-title)) + (setq title (or (and (not current-prefix-arg) + (not (string-empty-p (if existing-bookmark existing-title title))) + (if existing-bookmark existing-title title)) + (completing-read "Title: " + (if existing-bookmark + (list title existing-title) + (list title)) + nil nil nil nil + (if existing-bookmark existing-title title)))) + (setq title (funcall sanitize-title title)) + (let ((new-bookmark `(:url ,url + :title ,title + :time ,(or (and existing-bookmark (plist-get existing-bookmark :time)) + (current-time-string)) + ,@(if tags (list :tags tags))))) + (if existing-bookmark + (cl-nsubstitute new-bookmark existing-bookmark eww-bookmarks :count 1) + (push new-bookmark eww-bookmarks))) (eww-write-bookmarks) - (message "Bookmarked %s (%s)" url - (plist-get eww-data :title))))) + (message "Bookmarked %s (%s)" url title)))) (advice-add 'eww-add-bookmark :override 'ambrevar/eww-add-bookmark) (defun ambrevar/eww-read-bookmarks ()