Skip to content

Add Bookmark Support Examples

jeffphil edited this page Jul 10, 2024 · 1 revision

Some major-modes do not have Emacs Bookmark capabilities built-in, which causes activities not to be able to present a previously saved window for an activity. The message: Activities was unable to get a buffer named: xyz... will be shown instead when restoring the activity.

Here are some examples of how to add Emacs Bookmark capabilities for a mode or capability, when a package maintainer may not be active, available, or desire to add in Bookmark capabilities.

Persistent-Scratch

https://github.com/Fanael/persistent-scratch

(defun act/persistent-scratch-bookmark-make-record (&optional no-file no-context posn)
  "Create a bookmark record for current persistent-scratch buffer, with position."
  (if buffer-file-name
      (apply #'bookmark-make-record-default no-file no-context posn)
    (progn
      (let ((bookmark (apply #'bookmark-make-record-default 'NO-FILE no-context posn)))
        (bookmark-prop-set bookmark 'handler #'act/persistent-scratch-bookmark-jump)))))

(defun act/persistent-scratch-bookmark-jump (bookmark)
  "Restore persistent-scratch buffer when jumping to bookmark."
  (persistent-scratch-restore)
  (ignore-errors (goto-char (bookmark-prop-get bookmark 'position))))

(put 'act/persistent-scratch-bookmark-jump
     'bookmark-handler-type "act/persistent-scratch")

;; When restoring, make sure bookmarking added back into buffer's mode.
(advice-add 'persistent-scratch-restore :after
            (defun act/persistent-scratch-restore-after-helper (&rest _)
              (with-current-buffer (current-buffer)
                (setq-local bookmark-make-record-function
                            #'act/persistent-scratch-bookmark-make-record)))))
Clone this wiki locally