eww: Allow eww-add-bookmark to modify tags and title

master
Pierre Neidhardt 2018-08-25 13:22:02 +02:00
parent 6899f47ced
commit c33b0ab8a4
1 changed files with 39 additions and 13 deletions

View File

@ -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 ()