Compare commits

...

2 Commits

Author SHA1 Message Date
de123f7cea
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`.
2025-03-02 18:11:13 +01:00
76815c0171
Use more generic location slot for storing URLs in bookmarks
This might allow easier relocation of such bookmarks later on.
2025-03-02 17:38:42 +01:00
2 changed files with 43 additions and 4 deletions

View File

@ -380,6 +380,8 @@
db/pretty-print-xml db/pretty-print-xml
db/bookmark-add-external db/bookmark-add-external
db/bookmark-add-url db/bookmark-add-url
db/bookmark-relocate
db/bookmark-bmenu-relocate
db/lookup-smime-key db/lookup-smime-key
db/dired-from-shell-command db/dired-from-shell-command
db/system-open db/system-open
@ -2363,7 +2365,9 @@ eventuelly be set to nil, however)."
(use-package bookmark (use-package bookmark
:init (setq bookmark-default-file (expand-file-name "bookmarks" emacs-d-userdata) :init (setq bookmark-default-file (expand-file-name "bookmarks" emacs-d-userdata)
bookmark-menu-confirm-deletion t 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 (use-package dumb-jump
:commands (dumb-jump-xref-activate) :commands (dumb-jump-xref-activate)

View File

@ -845,14 +845,14 @@ number of bytes has been inserted."
HANDLER is a function receiving a single argument, namely HANDLER is a function receiving a single argument, namely
LOCATION. If a bookmark named NAME is already present, replace LOCATION. If a bookmark named NAME is already present, replace
it." it."
(let ((new-record `((filename . ,location) (let ((new-record `((location . ,location)
(handler . ,handler)))) (handler . ,handler))))
(bookmark-update-last-modified new-record) (bookmark-update-last-modified new-record)
(bookmark-store name new-record nil))) (bookmark-store name new-record nil)))
(defun db/bookmark-browse-url (bmk) (defun db/bookmark-browse-url (bmk)
"Extract filename from bookmark BMK and apply `browse-url' to it." "Extract filename from bookmark BMK and apply `browse-url' to it."
(browse-url (bookmark-get-filename bmk))) (browse-url (bookmark-location bmk)))
;; https://takeonrules.com/2024/12/17/extending-built-in-emacs-bookmark-package/ ;; https://takeonrules.com/2024/12/17/extending-built-in-emacs-bookmark-package/
(put 'db/bookmark-browse-url 'bookmark-handler-type "URL") (put 'db/bookmark-browse-url 'bookmark-handler-type "URL")
@ -863,7 +863,7 @@ it."
(defun db/bookmark-eww (bmk) (defun db/bookmark-eww (bmk)
"Extract filename from bookmark BMK and apply `eww' to it." "Extract filename from bookmark BMK and apply `eww' to it."
(eww (bookmark-get-filename bmk))) (eww (bookmark-location bmk)))
(put 'db/bookmark-eww 'bookmark-handler-type "EWW") (put 'db/bookmark-eww 'bookmark-handler-type "EWW")
@ -888,6 +888,41 @@ as completing instead."
(interactive "sURL: \nsName: ") (interactive "sURL: \nsName: ")
(db/bookmark-add-with-handler name url #'db/bookmark-eww)) (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 ;;; Switching Themes