From de123f7cea8f85449804e225c6fd940bc13a7293 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sun, 2 Mar 2025 18:11:13 +0100 Subject: [PATCH] Allow relocation of custom URL bookmark types This is mostly copied from the current implementation in `bookmark.el`, adapted to non-filename bookmarks. Note that we are now using the `location` slot in bookmarks to store the URLs, and relocating bookmarks will set the `filename` slot to `nil`. --- init.el | 6 +++++- site-lisp/db-utils.el | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/init.el b/init.el index 4eb41d2..d291d25 100644 --- a/init.el +++ b/init.el @@ -380,6 +380,8 @@ db/pretty-print-xml db/bookmark-add-external db/bookmark-add-url + db/bookmark-relocate + db/bookmark-bmenu-relocate db/lookup-smime-key db/dired-from-shell-command db/system-open @@ -2363,7 +2365,9 @@ eventuelly be set to nil, however)." (use-package bookmark :init (setq bookmark-default-file (expand-file-name "bookmarks" emacs-d-userdata) bookmark-menu-confirm-deletion t - bookmark-save-flag 1)) + bookmark-save-flag 1) + :bind (:map bookmark-bmenu-mode-map + ("R" . db/bookmark-bmenu-relocate))) (use-package dumb-jump :commands (dumb-jump-xref-activate) diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index b24fccd..a8d9b02 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -888,6 +888,41 @@ as completing instead." (interactive "sURL: \nsName: ") (db/bookmark-add-with-handler name url #'db/bookmark-eww)) +(defun db/bookmark-relocate (bookmark-name) + "Relocate BOOKMARK-NAME to another location. + +Bookmarks with type “URL” or “EWW” can be set to arbitrary locations, +all other bookmarks are handled via the default `bookmark-relocate' +mechanism." + (interactive (list (bookmark-completing-read "Bookmark to relocate"))) + (bookmark-maybe-load-default-file) + (let ((bmk-type (bookmark-type-from-full-record (bookmark-get-bookmark bookmark-name)))) + ;; TODO: it might be nice to use `cl-defmethod' instead for easier extensibility instead of + ;; explicitly listing all currently available link types. + (cond + ((member bmk-type '("URL" "EWW")) + (let ((newloc (read-string (format "Relocate %s to: " bookmark-name) + (bookmark-location bookmark-name)))) + (when (bookmark-get-filename bookmark-name) + (bookmark-set-filename bookmark-name nil)) + (bookmark-prop-set bookmark-name 'location newloc) + (bookmark-update-last-modified bookmark-name) + (setq bookmark-alist-modification-count + (1+ bookmark-alist-modification-count)) + (when (bookmark-time-to-save-p) + (bookmark-save)) + (bookmark-bmenu-surreptitiously-rebuild-list))) + (t (bookmark-relocate bookmark-name))))) + +(defun db/bookmark-bmenu-relocate () + "Change location of bookmark at point in `bookmark-bmenu-mode'. +Uses `db/bookmark-relocate'." + ;; Adapted from `bookmark-bmenu-relocate'. + (interactive nil bookmark-bmenu-mode) + (let ((thispoint (point))) + (db/bookmark-relocate (bookmark-bmenu-bookmark)) + (goto-char thispoint))) + ;;; Switching Themes