2

With Bookmark+, we can define our own bookmark types, and list bookmarks by type. I am not a lisp programmer. Can someone give me an example of how to define a new type, such as dev or web?

1 Answer 1

2

Two answers, depending on what you mean by a bookmark "type":

  • The first answer says how to create a new type of bookmark, in the sense of bookmarks that have some specific behavior.

  • The second answer speaks to the simple case where all you really want to do is designate a given set of bookmarks as belonging to some category -- the bookmarks don't need any particular behavior. Here, you just tag the bookmarks with a name.


You have to say what such a bookmark type (dev or web) is/does. Any specific behavior it has can't be guessed. And you'll need to code that behavior.

But here's a general recipe. This will get you the behaviors you see for the predefined Bookmark+ types (except for highlighting your new type specially in the display list).

I'll call your new bookmark type "MYTYPE", here.

Step 5 is really the answer to your question, as I understand it. That needs step 4, which needs step 3, which needs step 2, which needs step 1.

  • Step 1 makes it possible to identify a MYTYPE bookmark. It gives the bookmark a specific handler function.

  • Step 2 creates a bookmark record that uses that handler function.

  • Step 3 defines a predicate that returns true if a bookmark is a MYTYPE bookmark -- it just tests whether a bookmark has that type's handler.

  • Step 4 defines a function that returns only MYTYPE bookmarks, from all of the bookmarks that are current.

  • Step 5 defines a command to show only MYTYPE bookmarks in the bookmark-list display.

  • Steps 6 and 7 are optional. They bind the show command to a key and define a MYTYPE-specific jump command.

  1. Define a handler function to jump to a MYTYPE bookmark.
(defun MYTYPE-bookmark-jump (bookmark)
  "Bookmark handler for MYTYPE bookmarks."
  (;; Code whatever MYTYPE-specific behavior you need.
   ;; ...
   ;; Then invoke the default handler in the current buffer.
   (bookmark-default-handler
    `("" (buffer . ,(current-buffer))
          . ,(bookmark-get-bookmark-record bookmark)))
  1. Define a function to create a MYTYPE bookmark. The function should be able to be the value of variable bookmark-make-record-function.
(defun MYTYPE-bookmark-make-record ()
  "Make a MYTYPE bookmark record."
  `(,@(bookmark-make-record-default)
    ;; Whatever MYTYPE-specific properties you want.
    ;; E.g. property foo and its value. 
    (foo . ,(...))
    (handler . MYTYPE-bookmark-jump))) ; The MYTYPE handler.

That creates a bookmark record such as this:

((filename . "FILE WHERE CREATED")
 (buffer-name . BUFFER WHERE CREATED")
 (front-context-string . "STRING BEFORE CREATION POINT")
 (rear-context-string . "STRING AFTER CREATION POINT")
 (front-context-region-string)
 (rear-context-region-string)
 (visits . 0)
 (time 25447 19436 101446 0)
 (created 25447 19436 101446 0)
 (position . 1282)
 (foo . VALUE-FOR-FOO)    ; Example of a MYTYPE-specific field
 (handler . MYTYPE-bookmark-jump))
  1. Define a predicate that tests whether a bookmark is of type MYTYPE. It typically just tests the MYTYPE handler.
(defun MYTYPE-bookmark-p (bookmark)
  "Return non-nil if BOOKMARK is a MYTYPE bookmark."
  (eq (bookmark-get-handler bookmark) 'MYTYPE-bookmark-jump))
  1. Define a filter function, which returns only MYTYPE bookmarks.
(defun MYTYPE-alist-only ()
  "`bookmark-alist', filtered to retain only MYTYPE bookmarks.
A new list is returned (no side effects)."
  (bookmark-maybe-load-default-file)
  (bmkp-remove-if-not #'MYTYPE-bookmark-p bookmark-alist))
  1. Use macro bmkp-define-show-only-command (from file bookmark+-mac.el) to define a command bmkp-bmenu-show-only-MYTYPE-bookmarks, to show only bookmarks of type MYTYPE.
(bmkp-define-show-only-command
  MYTYPE
  "Display (only) MYTYPE bookmarks."
  MYTYPE-alist-only)
  1. Bind that show command to a key. E.g., bind it to C S:
(define-key bookmark-bmenu-mode-map "CS" 'bmkp-bmenu-show-only-MYTYPE-bookmarks)
  1. Define a command to jump to a MYTYPE bookmark. Use bmkp-read-bookmark-for-type in the interactive spec, to prompt for a MYTYPE bookmark name. (Or else just use C-x j :, which prompts you first for the type name - MYTYPE.)
(defun MYTYPE-jump (bookmark)
  "Jump to a MYTYPE bookmark."
  (interactive (list (bmkp-read-bookmark-for-type
                       "MYTYPE" (MYTYPE-alist-only))))
  (when (stringp bookmark)
    (setq bookmark  (bmkp-get-bookmark-in-alist
                      bookmark t (MYTYPE-alist-only))))
  (bmkp-jump-1 bookmark 'bmkp--pop-to-buffer-same-window))

Another possibility is that you don't really need any new bookmark types, e.g. because the bookmarks of the "types" you mention don't have any special behavior/definition.

That is, perhaps all you really mean by "type" is a name, that is, a tag that you assign to bookmarks you want to consider as being in the same category -- the category being just the tag (name).

For that, you just tag the bookmarks the way you want, e.g. with tag dev or web (or perhaps with both tags, if you want overlapping categories/types).

Then you can, if you want, act on only bookmarks that have one or the other (or both) of those tags. And you can sort the *Bookmark List* display according to tags, or list only bookmarks tagged a certain way, etc.

For this feature, see Bookmark Tags.

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.