1

What I mean is, can I add a URL to my Emacs bookmarks, such that selecting the bookmark runs the URL through browse-url?

Desired behavior

What I would like is a bookmark-set-url function that prompts for a url, say emacs.stackexchange.com. Then when I run bookmark-jump, emacs.stackechange.com appears in my bookmarks, and selecting it runs (browse-url "emacs.stackexchange.com").

This is analogous to bookmark-set, which sets a bookmark that points at the current location.

Is this behavior—or something similar—available in Emacs? If not is it possible to augment bookmarks (or bookmarks+) to support it?

The reason I want this is that bookmark-jump feels like a general purpose targeted navigation command. Browser URLs feel like an extension of that idea, despite the fact that they switch to a different application.

2 Answers 2

4

The following seems working from here (I don't use the Bookmarks feature, and I ain't familir with its protocol.), I tested it with:

M-x your-bookmark-set-url https://example.com
M-x bookmark-jump https://example.com
(defun your-bookmark-set-url (url)
  (interactive "sBookmark URL: ")
  (if (assoc url bookmark-alist)
      (user-error "%s is already bookmarked" url)
    (push `(,url . ((handler . ,(lambda (bookmark)
                                  (browse-url (car bookmark))))))
          bookmark-alist)))
1
  • I tried this approach and it works, but then I can't see my bookmarks list again. I ended up instead for installing Bookmark+. Its not quite as hard as the documentation makes it sound. Then you can just call bmkp-url-target-set. Mar 2 at 18:39
0

I wrote a couple of functions based on the previous answer that allows for setting a name that will be visible when you set a URL bookmark. It closely resembles the bookmark function of the EWW browser.

(defun my/url-bookmark-handler (bookmark)
  "Browse location (url) of BOOKMARK."
  (browse-url (alist-get 'location bookmark)))

(defun my/bookmark-set-url (name url)
  "Set a bookmark named NAME and URL as location."
  (interactive "sBookmark Name: \nsBookmark URL: ")
  (let ((existing-bookmark (cl-find-if (lambda (bookmark)
                                         (string= url (alist-get 'location (cdr bookmark))))
                                       bookmark-alist)))
    (if existing-bookmark
        (user-error "URL %s is already bookmarked as %s" url (car existing-bookmark))
      (push `(,name . ((location . ,url)
                       (handler . my/url-bookmark-handler)))
            bookmark-alist))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.